PARAMETERS lloprava, c_mena, cstr, m.cdoklod, neodomkni, menobuf, neriesdlhy, sqlautopv
PRIVATE hladajcdokl, m.teraz, m.prikaz, m.vysledok, m.o_sel, m.boladb, pocetriadkov, zmenenestred, m.menobuffera, m.pocetopakovani
m.menobuffera=m.menobuf
m.boladb=.f.
m.zmenenestred=.f.
IF "buf"$m.fn_obraty
	m.cstr=m.gic_stredisk
ELSE 
	IF m.cstr=0 .or. m.cstr<>m.gic_stredisk
		m.zmenenestred=.t.
		=novestredisko(m.cstr)
	ELSE  
		m.cstr=m.gic_stredisk
	ENDIF
ENDIF 
m.o_sel=SELECT()
SELECT 0
DO open_database
had = CREATEOBJECT("had", jaz("prg_buff_out"))
had.set_max(RECCOUNT() * 2.1)
had.SHOW()
IF m.issql
	zacatek_prepoctu = DATETIME()
	DO FORM snake NAME lsnake_buf_out LINKED WITH jaz("form zapis dokladu")
	lsnake_buf_out.DRAW()
	TRY
		IF m.sqlautopv
			SELECT buf_dokl1
			REPLACE autor WITH m.gcuser_id FOR EMPTY(autor)
			=tux(.t.,.t.,"buf_dokl1", "REQUERY")
		ELSE
			SELECT buf_dokl
			REPLACE autor WITH m.gcuser_id FOR EMPTY(autor)
			=tux(.t.,.t.,"buf_dokl", "REQUERY")
		ENDIF 
	CATCH 
	ENDTRY 
	m.vysledok=sql_exec("SELECT food600.buff_out"+m.sqlsuffix+"('"+m.menobuf+"',"+ ALLTRIM(STR(m.cdata,2,0))+","+ALLTRIM(STR(m.giakt_mena,2,0))+",'"+ALLTRIM(m.faktu_prefix)+"','"+ ALLTRIM(m.faktury_cislovani)+"',"+ALLTRIM(STR(IIF(m.neriesdlhy,1,0),1,0))+")")
	DO FORM dorobprepocet
	IF m.sqlautopv
		=REQUERY("buf_dokl1")	
		SELECT buf_dokl1
	    doba_trvani = DATETIME() - m.zacatek_prepoctu
	    doba_trvani = IIF(m.doba_trvani < 9, " ", " cas:" + ALLTRIM(STR(m.doba_trvani)) + "s ")
		SCAN ALL 
	        parovaky = " " + ALLTRIM(buf_dokl1.pz1) + IIF( EMPTY(buf_dokl1.pz2), "", "|" + ALLTRIM(buf_dokl1.pz2))
			DO zurnal_log WITH "OPERA", "BUFF_OUT", ;
	           buf_dokl1.operace + IIF(m.lloprava, "-opr", "") + m.doba_trvani + DTOS(buf_dokl1.datum_dokl) + m.parovaky, buf_dokl1.c_dokl, m.gic_stredisk, buf_dokl1.c_partner
		ENDSCAN 
		lsnake_buf_out.release()
		IF m.is_alko474
			SELECT buffer1
			=REQUERY("buffer1")
			SELECT buf_dokl1
			=REQUERY("buf_dokl1")
			DO buf_out_alko 
		ENDIF 
	ELSE 
		=REQUERY("buf_dokl")	
		SELECT buf_dokl
	    doba_trvani = DATETIME() - m.zacatek_prepoctu
	    doba_trvani = IIF(m.doba_trvani < 9, " ", " cas:" + ALLTRIM(STR(m.doba_trvani)) + "s ")
		SCAN ALL 
	        parovaky = " " + ALLTRIM(buf_dokl.pz1) + IIF( EMPTY(buf_dokl.pz2), "", "|" + ALLTRIM(buf_dokl.pz2))
			DO zurnal_log WITH "OPERA", "BUFF_OUT", ;
	           buf_dokl.operace + IIF(m.lloprava, "-opr", "") + m.doba_trvani + DTOS(buf_dokl.datum_dokl) + m.parovaky, buf_dokl.c_dokl, m.gic_stredisk, buf_dokl.c_partner
		ENDSCAN 
		lsnake_buf_out.release()
		IF m.is_alko474
			SELECT buffer
			=TABLEREVERT(.t.,"buffer")
			SELECT buffer
			IF GETNEXTMODIFIED(0,"buffer")<>0
				m.pocetopakovani=0
				DO WHILE .t.
					IF TABLEREVERT(.t.,"buffer")=0
						EXIT
					ELSE
						m.pocetopakovani=m.pocetopakovani+1
						IF m.pocetopakovani>10
							EXIT
						ENDIF 
					ENDIF 
				ENDDO 
				IF m.pocetopakovani<10
					=REQUERY("buffer")
				ENDIF 
			ELSE
				=TABLEREVERT(.t.,"buffer")
				=REQUERY("buffer")
			ENDIF 

			SELECT buf_dokl
			=REQUERY("buf_dokl")
			DO buf_out_alko 
		ENDIF 
	ENDIF 
ELSE 
	SELECT buf_dokl
	SCAN
		zacatek_prepoctu = DATETIME()
		had.INCREMENT
		SCATTER MEMVAR memo  
		o_c_dokl   = m.c_dokl
		m.hladajcdokl=m.o_c_dokl		
		m.teraz=DATETIME()
		SELECT 0
		USE v1doklady
		=CURSORSETPROP("Buffering",5,"v1doklady")
		SELECT 0
		USE v1obraty
		=CURSORSETPROP("Buffering",5,"v1obraty")
		IF m.lloprava
			IF !USED("dokl_sto")
				SELECT 0
				USE (m.fn_dokl_sto) ALIAS dokl_sto
			ELSE
				SELECT dokl_sto
			ENDIF 
			IF !USED("obra_sto")
				SELECT 0
				USE (m.fn_obra_sto) ALIAS obra_sto
			ELSE
				SELECT obra_sto
			ENDIF 
			m.teraz=DATETIME()
			SELECT 0
			USE v1doklsto
			=CURSORSETPROP("Buffering",5,"v1doklsto")
			INSERT INTO v1doklsto (c_stredisk, c_dokl, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, trzba1,splatnost,sleva_abs,sleva_proc,cen_hlad,zaloha,halere,provedl,uctsuma,uctexport,c_mena, kdy, poznamka);
				SELECT c_stredisk, c_dokl, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, trzba1,splatnost,sleva_abs,sleva_proc,cen_hlad,zaloha,halere,provedl,uctsuma,uctexport,c_mena,m.teraz as kdy, poznamka FROM v1doklady
			=tux(.t.,.t.,"v1doklsto")
			SELECT 0
			USE v1obrasto
			=CURSORSETPROP("Buffering",5,"v1obrasto")
			INSERT INTO v1obrasto (c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, spdan, spstupne, dan, serial_no,  poradi, kdy);
				SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, spdan, spstupne, dan, serial_no,  poradi, m.teraz as kdy FROM v1obraty
			=tux(.t.,.t.,"v1obrasto")
			DO pozavirej WITH "v1doklsto,v1obrasto,obra_sto, dokl_sto"
			SELECT v1doklady
			DELETE ALL
			SELECT v1obraty
			DELETE ALL 
			m.uctexport=.T.
		ELSE
			IF m.operace="PR" AND m.c_dokl<>0
			ELSE 
				m.c_dokl     = i_max("c_dokl")
			ENDIF 
			DO CASE 
				CASE m.operace = "VR"
					m.doklad = i_max("doklad" + STR(gic_stredisk) + "PE")
				CASE m.operace = "SF"
					m.doklad = i_max("doklad" + STR(0) + "SF")
			OTHERWISE 
				m.doklad = i_max("doklad" + STR(gic_stredisk) + m.operace)
			ENDCASE 
			c_mena=m.giakt_mena
			IF "SF" $ m.platba
				m.pz2 = ""
			ELSE
				IF m.operace = "VE" OR m.operace="SF"
					DO CASE
                    CASE buf_dokl.c_partner < 0
                            m.pz2 = ""
					CASE "STRED" $ UPPER(m.faktury_cislovani)    && kazde stredisko ma svou ciselnou radu faktur
							m.pz2 = m.faktu_prefix + ALLTRIM(STR(i_max("doklad" + STR(m.gic_stredisk) + "FAKTURA"), 9))
					CASE "DOKLAD" $ UPPER(m.faktury_cislovani)   && faktura ma stejne cislo, jako cislo dokladu
							m.pz2 = m.faktu_prefix + ALLTRIM(STR(m.doklad, 9))
					CASE "NECISLO" $ UPPER(m.faktury_cislovani)   && pz je volne editovatelny, negeneruje se cislo faktury
							*
					OTHERWISE                                   && vsechna strediska maji spolecnou ciselnou radu faktur
							m.pz2 = m.faktu_prefix + ALLTRIM(STR(i_max("doklad" + STR(0) + "FAKTURA"), 9))
					ENDCASE
				ENDIF
			ENDIF
		ENDIF
		m.provedl    = m.gcuser_id
		m.kdy        = DATETIME()
		INSERT INTO v1doklady FROM MEMVAR
		SELECT buffer
		SET FILTER TO
		DELETE FOR EMPTY(c_karty)
		**   **   **   **
		LOCATE FOR c_dokl = m.o_c_dokl
		IF .NOT. FOUND()
			INSERT INTO buffer (c_dokl, menobuf, c_data) VALUES (m.o_c_dokl, m.menobuf, m.cdata)
			=tux(.t.,.t.,"buffer", "REQUERY")
		ELSE
			IF m.spotrebnadan<>0
				SELECT 0
				USE (m.fn_spotrebnadan) ALIAS spdan ORDER c_karty
				SET FILTER TO zmazane=0
				SELECT buffer
				SET RELATION TO c_karty INTO spdan
				SELECT buffer
				REPLACE buffer.spdan WITH m.spotrebnadan, buffer.spstupne WITH spdan.koeficient FOR !EMPTY(spdan.c_karty)
				USE IN spdan
			ENDIF 
		ENDIF
		**   **   **   **
		SELECT buffer 
		IF m.operace="VP"
			REPLACE c_stredisk WITH m.c_stredisk, c_dokl WITH m.c_dokl FOR c_dokl = m.o_c_dokl 
		ELSE
			REPLACE c_stredisk WITH m.c_stredisk, c_dokl WITH m.c_dokl, operace WITH m.operace FOR c_dokl = m.o_c_dokl 
		ENDIF 
		REPLACE FOR operace = "VV" mnozstvi WITH - ABS(mnozstvi)
		REPLACE FOR operace = "PV" mnozstvi WITH   ABS(mnozstvi)
		REPLACE FOR operace = "VE" .AND. EMPTY(dan) dan WITH 1.0
		IF CURSORGETPROP("Buffering","buffer")>1
			=tux(.t.,.t.,"buffer", "REQUERY")
		ENDIF 
		SELECT v1obraty
		DO zamkni
		IF INLIST(m.operace, "PI", "PR")
			INSERT INTO v1obraty (c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, spdan, spstupne, dan, serial_no, poradi) SELECT  c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena as cena_prum, spdan, spstupne, dan, serial_no, poradi FROM buffer WHERE c_dokl=m.c_dokl
		ELSE
			INSERT INTO v1obraty (c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena_prum, spdan, spstupne, dan, serial_no, poradi) SELECT  c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part, zustatek, cena as cena_prum, spdan, spstupne, dan, serial_no, poradi FROM buffer WHERE c_dokl=m.c_dokl .and. mnozstvi<>0
		ENDIF

		**   **   **   **
		LOCATE FOR c_dokl = m.c_dokl
		IF .NOT. FOUND()
			INSERT INTO v1obraty (c_dokl, c_stredisk, operace) VALUES (m.c_dokl, m.c_stredisk, m.operace)
		ENDIF
		**   **   **   **
		UNLOCK
		SELECT v1doklady
		=tux(.t.,.t.,"v1doklady")
		USE 
		SELECT v1obraty
		=tux(.t.,.t.,"v1obraty")
		USE 
		SELECT buf_dokl
		REPLACE c_dokl WITH m.c_dokl
        doba_trvani = DATETIME() - m.zacatek_prepoctu
        doba_trvani = IIF(m.doba_trvani < 9, " ", " cas:" + ALLTRIM(STR(m.doba_trvani)) + "s ")
        parovaky = " " + ALLTRIM(pz1) + IIF( EMPTY(pz2), "", "|"+ALLTRIM(pz2))
		DO zurnal_log WITH "OPERA", "BUFF_OUT", ;
           m.operace + IIF(m.lloprava, "-opr", "") + m.doba_trvani + DTOS(buf_dokl.datum_dokl) + m.parovaky, m.c_dokl, m.gic_stredisk, buf_dokl.c_partner
	ENDSCAN
	SELECT buf_dokl
	IF CURSORGETPROP("Buffering","buf_dokl")>1
		=tux(.t.,.t.,"buf_dokl", "REQUERY")
	ENDIF 
	IF m.is_alko474
		DO buf_out_alko 
	ENDIF 
	IF LEFT(m.operace,1)<>"Z" .and. m.operace<>"SF"
		IF !lloprava
			SELECT buf_dokl
			IF CURSORGETPROP("Buffering","buf_dokl")>1
				=tux(.t.,.t.,"buf_dokl", "REQUERY")
			ENDIF 
			SELECT MAX(c_dokl) as c_dokl FROM buf_dokl INTO CURSOR maxcdokl
			m.c_dokl=maxcdokl.c_dokl
			USE IN maxcdokl
			IF m.operace # "PI"
				SELECT prep
				DO prepocet2 WITH "prep", m.cdoklod, m.c_dokl, .t., .t., m.menobuffera, m.c_mena, m.cstr
			ELSE
				IF "KOMPLET" $ m.pz2
					had.HIDE()
					IF MESSAGEBOX(jaz("prg_vzapeti_ostatni_invetur") + CHR(13) + jaz("prg_staci_prepocitat_posledni") + CHR(13) + jaz("prg_report_dotiskni_v_hledani"), 4+32, jaz("prg_provest_prepocet_skladu")+"?") = 6
						DO FORM prep_all WITH 0
						m.prepocteno_po_inventure = .T.
					ENDIF
				ELSE
					SELECT prep
					DO prepocet2 WITH "prep", m.cdoklod, m.c_dokl, .t.,.t.
					m.prepocteno_po_inventure = .T.
				ENDIF
			ENDIF
		ELSE
			DO gen_stav WITH m.cdoklod,0
			DO prep_all_od WITH m.cdoklod
		ENDIF 
	ENDIF 
	IF ! lloprava .AND. m.operace = "VV"
		SELECT 0
		m.hladajcdokl=m.c_dokl
		USE v1obraty
		=CURSORSETPROP("Buffering",5,"v1obraty")
		REPLACE ALL cena_part WITH cena
		=tux(.t.,.t.,"v1obraty")
		USE 
	ENDIF
ENDIF 
IF m.zmenenestred
	DO vratstredisko
ENDIF
IF !m.neodomkni
	DO n_sunlock WITH "BUF_OUT_"+m.gcpgm_id
ENDIF 
RELEASE had
