PARAMETERS o_c_dokl
PRIVATE m.hladajcdokl, m.hladajcstredisk,m.hladajmodul,m.hladajsetupskup,m.hladajvarname, m.bolfooddat, m.hladajbourskup, m.menobuffera, m.boliobraty
SELECT 0
bolfooddat=.F.
hladajcdokl=m.o_c_dokl

IF m.issql
	USE (m.sqlprefix+"r1doklady"+m.sqlsuffix) ALIAS r1doklady
ELSE
	USE v1doklady ALIAS r1doklady
ENDIF

hladajcstredisk=r1doklady.c_partner
SELECT 0
IF m.issql
	USE (m.sqlprefix+"rfooddatriadok") ALIAS bour_skup
ELSE
	IF !USED("food_dat")
		USE (m.fn_food_dat) ALIAS food_dat IN 0
		bolfooddat=.F.
	ELSE
		bolfooddat=.T.
	ENDIF
	SELECT 0
	USE vfooddatriadok ALIAS bour_skup
ENDIF
SELECT bour_skup
GO TOP 
IF "KAN" $ bour_skup.pgm_param
	WAIT "vydej do kanalu" WIND TIME .5 AT 22, 22
ELSE
	hladajmodul="MAN"
	hladajsetupskup=ALLTRIM(bour_skup.setup_skup)
	hladajvarname="auto_privl"
	hladajbourskup=ALLTRIM(bour_skup.bour_skup)

	IF m.issql
		USE (m.sqlprefix+"rdefsuborvarname") ALIAS defsubriadok IN 0
	ELSE
		IF FILE(m.gchom_path+"s_"+ALLTRIM(bour_skup.pgm)+"_"+ALLTRIM(bour_skup.setup_skup)+".dbf")
			USE (m.gchom_path+"s_"+ALLTRIM(bour_skup.pgm)+"_"+ALLTRIM(bour_skup.setup_skup)) IN 0 ALIAS defsubor
			USE vdefsuborvarname ALIAS defsubriadok IN 0
		ELSE 
			SELECT 0
			CREATE CURSOR defsubriadok (var_name c(10), var_value c(10))
		ENDIF 
	ENDIF
	SELECT defsubriadok

	IF RECCOUNT("defsubriadok")=1 .AND. ALLTRIM(LOWER(var_value))=".t."
		IF m.issql
			USE (m.sqlprefix+"rinventurstrediska") ALIAS inventur IN 0
		ELSE
			IF FILE (m.gcdat_path + "inventur.dbf")
				SELECT 0
				USE (m.gcdat_path + "inventur") NOUPDATE
			ENDIF
		ENDIF 

		IF USED("inventur")
			SELECT inventur
			LOCATE FOR c_stredisk = m.hladajcstredisk
			IF FOUND()
				MESSAGEBOX( jaz("form_prij_rozprac_inv") + CHR(13) + jaz("prg_nelze_udelat_auto_prijem"), 1+64+0, jaz("prg_auto_prijem_intermi"), 2222 )
				IF USED("buffer")
					=TABLEREVERT(.t.,"buffer")
				ENDIF 
				IF USED("buf_dokl")
					=TABLEREVERT(.t.,"buf_dokl")
				ENDIF 
				pokracuj=.f.
			ELSE
				pokracuj=.t.
			ENDIF 
		ELSE
			pokracuj=.t.
		ENDIF 
		IF m.pokracuj
			IF m.issql
				DO open_database
				SELECT 0
				USE (m.sqlprefix+"rkontrolatriggerovobraty") ALIAS kkxx
				=RECCOUNT()
				LOCATE FOR ALLTRIM(LOWER(tgenabled))="d"
				IF FOUND()
					m.pokracuj=.f.
				ENDIF 
				DO pozavirej WITH "kkxx"
				IF m.pokracuj
					DO FORM dorobprepocet
				ENDIF 
			ELSE 
				m.pokracuj=.t.
			ENDIF 
		ENDIF 
		IF m.pokracuj
			DO WHILE .T.
				IF n_slock_ex("BUF_OUT_" + bour_skup.id)
					IF m.issql
						USE (m.sqlprefix+"rpriintriadky"+m.sqlsuffix) ALIAS a_pv_obr IN 0
						USE (m.sqlprefix+"rpriintburanie"+m.sqlsuffix) ALIAS burr_2 IN 0
					ELSE
						IF !USED("obraty")
							USE (m.fn_obraty) ALIAS obraty IN 0
							boliobraty=.F.
						ELSE
							boliobraty=.T.
						ENDIF
						USE vpriintriadky ALIAS a_pv_obr IN 0
						USE vpriintburanie ALIAS burr_2 IN 0
						IF !m.boliobraty
							DO pozavirej WITH "obraty"
						ENDIF
						** bacha, v cena2 je koef_ceny !!
					ENDIF

					SELECT a_pv_obr
					REPLACE ALL cena2 WITH 1
					UPDATE a_pv_obr SET a_pv_obr.c_karty2=burr_2.c_karty2,a_pv_obr.nazev2=burr_2.nazev, a_pv_obr.c_druhs=burr_2.c_druhs, a_pv_obr.c_druhstr=burr_2.c_druhstr, a_pv_obr.c_druhk=burr_2.c_druhk, a_pv_obr.cena2=burr_2.koef_ceny, a_pv_obr.mnozstvi2=a_pv_obr.mnozstvi2*burr_2.koef_mnoz FROM burr_2 WHERE a_pv_obr.c_karty=burr_2.c_karty
					REPLACE ALL mnozstvi2 WITH ROUND(mnozstvi2,4)
					USE IN burr_2
					SELECT a_pv_obr
					REPLACE ALL cena2 WITH ROUND(cena / cena2, 4)
					IF "SKL" $ bour_skup.pgm_param
						REPLACE ALL c_druh WITH c_druhs
					ELSE
						REPLACE ALL c_druh WITH c_druhstr
					ENDIF
					INDEX ON poradi TAG poradi UNIQUE
					SET ORDER TO
					SET FILTER TO c_karty2 = c_karty .AND. EMPTY(c_druh)
					IF ! prazdna()
						wait_txt = jaz("prg_nelze_udelat_auto_prijem")
						SCAN
							wait_txt = m.wait_txt + CHR(13) + a_pv_obr.nazev + STR(a_pv_obr.mnozstvi2, 9,2)
						ENDSCAN
						= MESSAGEBOX(wait_txt , 0+16+256, jaz("prg_nelze_udelat_auto_prijem"), 7777)
						DO n_sunlock WITH ("BUF_OUT_" + bour_skup.id)
					ELSE
						IF m.issql
							menobuffera=LOWER(SUBSTR(SYS(2015),4))
							otevira_se = "rbufferd"
							USE (m.sqlprefix+"rbufferd")	ALIAS buf_dokl1 IN 0
							otevira_se = "rbuffer"
							USE (m.sqlprefix+"rbuffer")	ALIAS buffer1 IN 0
							SELECT buf_dokl1
							=CURSORSETPROP("Buffering",5,"buf_dokl1")
							INSERT INTO buf_dokl1 (datum_dokl, c_data, menobuf) VALUES (DATE(), m.cdata, m.menobuffera)
							SELECT buffer1
							=CURSORSETPROP("Buffering",5,"buffer1")
							INSERT INTO buffer1 (poradi, c_karty, cena, mnozstvi, menobuf, c_data, operace, c_stredisk) SELECT poradi, c_karty2 AS c_karty, cena2 AS cena, mnozstvi2 AS mnozstvi, m.menobuffera, m.cdata AS c_data, "PV" AS operace, m.hladajcstredisk AS c_stredisk FROM a_pv_obr
							=tux(.T.,.T.,"buffer1")
							=REQUERY("buffer1")

							SELECT 0
							USE (m.sqlprefix+"rpriextkartybuf") ALIAS rkartybuf

							LOCATE FOR nazev1=m.smazana_karta
							IF FOUND()
								=MESSAGEBOX(jazparam("form_karta_vymaz_nejde_prijat",rkartybuf.nazev),16,"")
								DO n_sunlock WITH ("BUF_OUT_" + bour_skup.id)
							ELSE
								SELECT buffer1
								IF r1doklady.c_mena<>m.giakt_mena
									REPLACE ALL cena WITH NTOM(MTON(cena)*en__meny(r1doklady.c_mena,m.giakt_mena))
									=tux(.T.,.T.,"buffer1")
									=REQUERY("buffer1")
								ENDIF
								SELECT buf_dokl1
								REPLACE ALL c_dokl WITH 0, datum_dokl WITH r1doklady.datum_dokl, pz1 WITH r1doklady.pz1, ;
									c_stredisk WITH m.hladajcstredisk, c_partner WITH r1doklady.c_stredisk, pz2 WITH STR(r1doklady.c_dokl,15), operace WITH "PV", c_mena WITH r1doklady.c_mena, provedl WITH m.gcuser_id, kdy WITH DATETIME(), poznamka WITH r1doklady.poznamka
								=tux(.T.,.T.,"buf_dokl1")
								=REQUERY("buf_dokl1")
								GO TOP
								SELECT buffer1
								DO buff_out2 WITH .F.,r1doklady.c_mena,m.hladajcstredisk,0,.F., m.menobuffera,.f.,.f.

								SELECT buf_dokl1
								=REQUERY("buf_dokl1")
								GO TOP
								n_c_dokl = buf_dokl1.c_dokl
								SELECT r1doklady
								REPLACE pz2 WITH STR(n_c_dokl, 15)
								=tux(.T.,.T.,"r1doklady")

								DO prn_obra WITH m.n_c_dokl

								SELECT buffer1
								DELETE ALL
								=tux(.t.,.t.,"buffer1")
								USE 
								SELECT buf_dokl1
								DELETE ALL
								=tux(.t.,.t.,"buf_dokl1")
								USE 
								DO n_sunlock WITH ("BUF_OUT_" + bour_skup.id)

							ENDIF
							DO pozavirej WITH "rkartybuf"
						ELSE
							IF !USED("obraty")
								USE (m.fn_obraty) ALIAS obraty IN 0
								boliobraty=.F.
							ELSE
								boliobraty=.T.
							ENDIF

    						SELECT a_pv_obr
							SET FILTER TO
							= i_max("c_dokl")
							SET FILTER TO
							n_c_dokl = o_c_dokl + 1
							SELECT doklady
							LOCATE FOR c_dokl = m.n_c_dokl
							DO WHILE FOUND()
								n_c_dokl = m.n_c_dokl + 1
								LOCATE FOR c_dokl = m.n_c_dokl
							ENDDO

							LOCATE FOR c_dokl = m.o_c_dokl
							REPLACE pz2 WITH STR(n_c_dokl, 15)
							SCATTER MEMVAR
                            IF m.operace = "VV"
                                auto_operace = "PV"
                            ELSE
                                auto_operace = "PE"
                                SELECT a_pv_obr
                                REPLACE ALL cena2 WITH cena_ve
                                =tux(.t.,.t.,"a_pv_obr")
                            ENDIF 

							doklad = i_max("doklad" + STR(m.c_partner) + m.auto_operace)
							INSERT INTO (m.fn_doklady) (c_stredisk, c_dokl, c_partner, doklad, datum_dokl, operace, pz1, pz2, platba, provedl, kdy,c_mena) ;
								VALUES (m.c_partner, n_c_dokl, m.c_stredisk, m.doklad, m.datum_dokl, m.auto_operace, m.pz1, STR(m.o_c_dokl, 15), "auto", m.provedl, m.kdy,m.c_mena)

							SELECT obraty
							DO zamkni
							INSERT INTO obraty (c_stredisk,           operace,                                c_dokl, c_karty,          mnozstvi,      cena, poradi) ;
                                   SELECT m.c_partner as c_stredisk, m.auto_operace as operace, m.n_c_dokl as c_dokl, c_karty2, ROUND(mnozstvi2,4), ROUND(cena2, 4), poradi FROM a_pv_obr
							UNLOCK

							SELECT c_karty2 as c_karty, SUM(mnozstvi2) AS mnozstvi, $0 AS zustatek,$0 AS cena_prum,0 AS c_mena, m.c_partner AS c_stredisk FROM a_pv_obr GROUP BY c_karty INTO CURSOR PREP READWRITE
							INDEX ON c_karty TAG c_karty
							INDEX ON STR(c_stredisk,12,0)+STR(c_karty,12,0) TAG csck
							SELECT PREP
							DO prepocet2 WITH "prep", 0, m.n_c_dokl, .T.,.T.
							DO pozavirej WITH "prep"
							DO n_sunlock WITH ("BUF_OUT_" + bour_skup.id)
							DO prn_obra WITH m.n_c_dokl
							IF !m.boliobraty
								DO pozavirej WITH "obraty"
							ENDIF
						ENDIF
					ENDIF
					EXIT 
				ELSE
					m.kto=n_checks_ex("BUF_OUT_"+bour_skup.id)
					IF MESSAGEBOX(jaz("bezi_vydaj")+" "+ALLTRIM(m.kto)+". "+jaz("chcete_pockat"),4+32+256, "FoodMan")=6
					ELSE
						EXIT
					ENDIF
				ENDIF
			ENDDO
		ENDIF
	ENDIF
ENDIF 
DO pozavirej WITH "defsubor, defsubriadok, bour_skup, inventur, burr_2, a_pv_obr, r1doklady, buffer1, buf_dokl1"
IF !m.bolfooddat
	DO pozavirej WITH "food_dat"
ENDIF
