PARAMETERS hladajdavku, datum_dokl, id_kas, cen_hlad, pocethostu, cmena
PRIVATE m.cdokldo, m.hladajcstredisk, m.menobuffera, m.lic_dokl, m.hladajcdokl, m.prach_prod, m.nakl_celk, m.storno, m.ret, m.pz1, m.pz2, m.cen_hlad_davk
PRIVATE m.hladajuzaverka, m.lcoperace, m.hladajoperace, m.typpohybu
m.typpohybu=0
m.hladajcstredisk=m.gic_stredisk
m.storno = (PARAMETERS() > 6)
m.prach_prod = 0
m.lic_dokl = -8888
m.id_kas = LEFT(ALLT(m.id_kas), 2)
m.nakl_celk = -11.11
m.lcoperace="VK"
m.hladajoperace="VK"
IF m.pocethostu = 0
	m.pocethostu = 1
ENDIF
m.menobuffera=LOWER(SUBSTR(SYS(2015),4))
chyba_pri_otevirani = .F.
DO pozavirej WITH "typypohybov"
TRY
	IF m.issql
		otevira_se = "rbufferd"
		USE (m.sqlprefix+"rbufferd")	ALIAS buf_dokl IN 0
		otevira_se = "rbuffer"
		USE (m.sqlprefix+"rbuffer")	ALIAS BUFFER IN 0
		m.hladajoperace="VK"
		rv('typypohybov_operace','volby2')
	ELSE
		otevira_se = m.fn_buff_r
		USE (m.fn_buff_r)     ALIAS buff_r     IN 0
		otevira_se = m.fn_buff_d
		USE (m.fn_buff_d)     ALIAS buff_d     IN 0
		otevira_se = "vbuffer"
		USE vbuffer  ALIAS BUFFER IN 0
		otevira_se = "vbufferd"
		USE vbufferd ALIAS buf_dokl IN 0
    	otevira_se = (m.gchom_path+"typypohybov")
		USE (m.gchom_path+"typypohybov")     ALIAS typypohybov     IN 0 
		USE vtypypohybov_operace ALIAS volby2 IN 0
	ENDIF
CATCH
	chyba_pri_otevirani = .T.
ENDTRY
IF chyba_pri_otevirani
	=MESSAGEBOX(jaz("prg_dbf_neprist_skuste_pozdeji")+CHR(13)+m.otevira_se,64,"FoodMan",2000)
	DO zurnal_log WITH "OPERA", "Odpočet dávky", jaz("nejde_otevrit") + m.otevira_se
	RETURN .F.
ENDIF
=CURSORSETPROP("Buffering" ,5,"buffer")
=CURSORSETPROP("Buffering" ,5,"buf_dokl")


m.hladajuzaverka=dajpoleuzaverka(m.datum_dokl)
m.hladajuzaverka=STRTRAN(m.hladajuzaverka,":","*")

cen_hlad_davk = IIF(EMPTY(m.cen_hlad), "1", RIGHT(m.cen_hlad,1))
pz1 = hladajdavku
IF m.slovenskaverzia OR m.issql
	pz2 = SUBSTR(m.hladajuzaverka,3,12) + "/" + m.id_kas
ELSE
	pz2 = m.hladajuzaverka + "/" + m.id_kas
ENDIF 
zrc_title = ALLTRIM(m.pz1) + "  " + m.pz2

IF m.issql
	u_cis = ALLTRIM(m.id_kas) + TRANSFORM(SECOND()/8.8, "@L 99999")
ELSE 
	u_cis = ALLTRIM(m.id_kas) + "D" + TRANSFORM(SECOND()/8.8, "@L 9999")
ENDIF 
DO zurnal_log WITH "OPERA", "U_CIS", ">" + m.id_kas + "< >" + m.u_cis + "<"

SELECT c_karty, mnozstvi, mnozstvi * cena_prod AS prachy, mnozstvi AS prachyprum, ;
	poradi, id_man, m.id_kas AS id_kas, m.hladajuzaverka AS uzaverka, SPACE(12) AS kod, ;
	m.label_yes AS k_odpoctu, m.cen_hlad_davk AS cen_hlad, dan, nazev, 9999.9 AS marze, m.cmena AS c_mena, c_sklad ;
	FROM buf_dav INTO CURSOR sk_pom READWRITE WHERE mnozstvi<>0

IF USED("sklad")
	UPDATE sk_pom SET prachyprum=prachyprum*sklad.cena_prum*en__meny(sklad.c_mena,m.giakt_mena) from sklad WHERE sk_pom.c_karty=sklad.c_karty AND sk_pom.c_sklad=sklad.c_stredisk
ELSE
	UPDATE sk_pom SET prachyprum=0 WHERE .t.
ENDIF 


REPLACE FOR EMPTY(dan) dan WITH m.dph1
REPLACE FOR ABS(prachyprum)>0.1 marze WITH ( prachy/dan - prachyprum ) / prachyprum * 100

IF prazdna()
	RETURN .F.
ENDIF

SUM prachy/dan, prachyprum TO trzby, naklady

IF isfoodcost
	REPLACE FOR ABS(prachy)>0.1 marze WITH prachyprum / prachy * dan * 100
	IF m.trzby # 0
		marze = naklady / trzby * 100
	ELSE
		marze = 999.9
	ENDIF
ELSE
	REPLACE FOR ABS(prachyprum)>0.1 marze WITH ( prachy/dan - prachyprum ) / prachyprum * 100
	IF m.naklady # 0
		marze = ( trzby - naklady ) / naklady * 100
	ELSE
		marze = 999.9
	ENDIF
ENDIF
LOCATE FOR poradi # 0
IF FOUND()
	INDEX ON poradi TAG poradi
ENDIF
IF m.davka_tlacit_reporty
	DO pr_report WITH "davk_odp"
	DO pr_report WITH "davk_od2"
ENDIF 
had = CREATEOBJECT("had", jaz("prg_odpocet_davky")+" " + m.hladajdavku )
had.SHOW()

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


***************   tady zacne obsluha odpoctu z vice stredisek   ***************
IF m.issql
	SELECT DISTINCT c_sklad, id_man AS id_sklad, 0000000000 AS cdokldo;
		WHERE !EMPTY(c_sklad) FROM buf_dav INTO CURSOR davk_stred READWRITE
	SCAN ALL
		m.hladajcstredisk=davk_stred.c_sklad
		SELECT 0
		USE (m.sqlprefix+"rmaxcdokl") ALIAS aaa
		m.cdokldo=aaa.c_dokl
		USE IN aaa
		SELECT davk_stred
		REPLACE cdokldo WITH m.cdokldo
	ENDSCAN
	m.vysledok=sql_exec("SELECT food600.odpocet_multidavky('"+m.menobuffera+"',"+ALLTRIM(STR(m.cdata,2,0))+",'"+ALLTRIM(m.hladajuzaverka)+"','"+ALLTRIM(m.id_kas)+"',"+ALLTRIM(STR(m.gic_stredisk,10,0))+","+ALLTRIM(STR(m.giakt_mena,2,0))+",'"+m.hladajdavku+"')")
	=REQUERY("buffer")
	=REQUERY("buf_dokl")
ELSE
	SELECT strediska
	SET FILTER TO 
	LOCATE FOR ID = m.id_kas
	c_partner = strediska.c_stredisk
	SELECT strediska
	SET FILTER TO ALLTRIM(LOWER(pgm))="man"
	SELECT karty
	SET ORDER TO c_karty
	SELECT polozky
	SET RELATION TO c_karty INTO karty
	REPLACE ALL pocet WITH karty.pocet
	SET RELATION TO

	SELECT c_karty, zustatek + 0.0000 AS k_odpoctu, vycisleni, cena_prum, .F. AS prepocet, m.giakt_mena AS c_mena, zustatek, c_stredisk ;
		FROM sklad INTO CURSOR a_skl READWRITE
	INDEX ON c_karty TAG c_karty
	SELECT c_karty, 000000.0000 AS mnozstvi, $0 AS zustatek,$0 AS cena_prum,0 AS c_mena, m.gic_stredisk AS c_stredisk FROM a_skl GROUP BY c_karty INTO CURSOR PREP WHERE .F. READWRITE
	INDEX ON c_karty TAG c_karty
	INDEX ON STR(c_stredisk,12,0)+STR(c_karty,12,0) TAG csck


	SELECT DISTINCT c_sklad , id_man AS id_sklad, 0000000000 AS cdokldo;
		WHERE !EMPTY(c_sklad)FROM buf_dav INTO CURSOR davk_stred READWRITE
	SCAN
		SCATTER MEMVAR
		IF !USED("obraty")
			USE (m.fn_obraty) ALIAS obraty IN 0
		ENDIF
		SELECT MAX(c_dokl) AS c_dokl FROM obraty WHERE c_stredisk=m.c_sklad .AND. LEFT(obraty.operace, 1) $ "VP" INTO CURSOR aaa
		m.cdokldo=aaa.c_dokl
		USE IN aaa
		SELECT davk_stred
		REPLACE davk_stred.cdokldo WITH m.cdokldo

		SELECT a_skl
		SET FILTER TO c_stredisk=m.c_sklad

		SELECT mnozstvi * cena_prod AS prach_prod WHERE c_sklad = m.c_sklad FROM buf_dav INTO CURSOR prach_prod
		SCATTER MEMVAR
		USE

		SELECT c_karty, SUM(mnozstvi) AS mnozstvi, 0000000.0000 AS odecteno ;
			GROUP BY c_karty WHERE c_sklad = m.c_sklad FROM buf_dav INTO CURSOR sk1 READWRITE

		DO kalkuluj WITH m.c_sklad,.f.,m.cdokldo

		INSERT INTO BUFFER (c_karty, dluh_mnoz, mnozstvi, LABEL, c_dokl, operace, c_stredisk, menobuf, c_data) SELECT c_karty, SUM(mnozstvi) AS dluh_mnoz, SUM(odecteno) AS mnozstvi, ;
			"Q" AS LABEL, m.lic_dokl AS c_dokl, m.lcoperace AS operace, m.c_sklad AS c_stredisk, m.menobuffera AS menobuf, m.cdata AS c_data FROM sk1 GROUP BY c_karty WHERE sk1.mnozstvi # 0 .OR. sk1.odecteno # 0
		SELECT buffer
		REPLACE FOR EMPTY(nazev) nazev WITH m.hladajuzaverka + "  " + m.cen_hlad

		INSERT INTO buf_dokl (operace, c_dokl, datum_dokl, pz1, pz2, c_partner, cen_hlad, menobuf, c_data, c_stredisk) ;
			VALUES (m.lcoperace, m.lic_dokl, m.datum_dokl, m.pz1, m.pz2, m.c_partner, m.cen_hlad_davk, m.menobuffera, m.cdata, m.c_sklad)
		m.lic_dokl = m.lic_dokl + 1

		lic_dokl = m.lic_dokl + 1
		SELECT davk_stred
	ENDSCAN
	DO pozavirej WITH "sk1, sk2, qqq, a_skl"
	=tux(.t.,.t.,"buffer")
	=REQUERY("buffer")
	SELECT buf_dokl
	=tux(.t.,.t.,"buf_dokl")
	=REQUERY("buf_dokl")
	DELETE FROM buf_dokl WHERE !(c_dokl IN (SELECT DISTINCT c_dokl FROM buffer))
	=tux(.t.,.t.,"buf_dokl")
	=REQUERY("buf_dokl")
	SELECT BUFFER
	REPLACE ALL poradi WITH RECNO()
	IF m.storno                            && odecet stornodavky
		REPLACE ALL mnozstvi WITH - mnozstvi - dluh_mnoz, dluh_mnoz WITH 0
		SELECT sk_pom
		REPLACE ALL mnozstvi WITH - mnozstvi, prachy WITH - prachy
	ENDIF
ENDIF 
RELEASE had
SELECT MAX(cdokldo) as cdokldo FROM davk_stred INTO CURSOR cdokldo
m.cdokldo=cdokldo.cdokldo
USE IN cdokldo
IF USED("volby2") AND RECCOUNT("volby2")>0
	DO pozavirej WITH "typypohybov"
	SELECT *, SPACE(10) as typpohybu FROM volby2 INTO CURSOR typypohybov WHERE typ_spotreby=2 READWRITE 
	IF RECCOUNT("typypohybov")>0
		INSERT INTO typypohybov (id_typ, meno) VALUES (-1, lng_oper(m.hladajoperace))
		INDEX on id_typ TAG id_typ
		REPLACE ALL typpohybu WITH STR(id_typ,10,0)
		DO FORM dajdruh WITH "","typypohybov","typpohybu",1,"meno",250 TO m.typpohybu
		IF EMPTY(m.typpohybu)
			m.typpohybu=0
		ELSE
			m.typpohybu=VAL(m.typpohybu)
			SELECT buf_dokl
			REPLACE typ_pohybu WITH m.typpohybu, provedl WITH m.gcuser_id
			=tux(.t.,.t.,"buf_dokl")
			=REQUERY("buf_dokl")
		ENDIF 
	ENDIF 
ENDIF 
DO pozavirej WITH "volby2, typypohybov"

IF zrccr_or(.F.,.F.,m.cdokldo, m.menobuffera)
	SELECT zrc
	SUM mnoz_ok* cena_prum TO nakl_celk
	SELECT buf_dokl
	=REQUERY("buf_dokl")
	m.hladajcdokl=buf_dokl.c_dokl
	DO ins_sk
	m.ret=.T.
ELSE
	m.ret=.F.
ENDIF
DO vymaz_buffer WITH m.menobuffera
DO pozavirej WITH "obraty, prep, buffer, buf_dokl, buff_r, buff_d"
RETURN m.ret

	*!*****************************************************************************
PROCEDURE ins_sk
PRIVATE idriadok, m.hladajidkas, m.hladajckasy, m.hladajcuzaverka
PRIVATE idriadok
SELECT 0
IF !m.issql
	IF !USED("sk_pol")
		USE (m.gccen_path+"sk_pol") IN 0
	ENDIF
	IF !USED("sk_plt")
		USE (m.gccen_path+"sk_plt") IN 0
	ENDIF
	IF !USED("sk_fin")
		USE (m.gccen_path+"sk_fin") IN 0
	ENDIF
	IF !USED("sk_sez")
		USE (m.gccen_path+"sk_sez") IN 0
	ENDIF
ENDIF

lcodecteno = STR(m.hladajcdokl, 12)
IF m.issql
	m.hladajidkas=m.id_kas
	SELECT strediska
	LOCATE FOR id=m.hladajidkas
	m.hladajckasy=strediska.c_stredisk
	SELECT 0
	USE (m.sqlprefix+"rskuzahladajcuzaverky") ALIAS maxuz 
	IF RECCOUNT("maxuz")=0
		INSERT INTO maxuz (uzaverka, id_kas, c_data) VALUES (m.hladajuzaverka, m.hladajidkas, m.cdata)
		=tux(.t.,.t.,"maxuz")
		=REQUERY("maxuz")
	ENDIF 
	GO TOP 
	m.hladajcuzaverka=maxuz.idriadok
	USE IN maxuz

	SELECT 0
	USE (m.sqlprefix+"rskpoluzaverka") ALIAS sk_pol
	=CURSORSETPROP("Buffering",5,"sk_pol")
	SELECT c_karty, SUM(mnozstvi) AS mnozstvi, SUM(mnozstvi * cena_part) AS prachy,  ;
		m.u_cis AS u_cis, davk_stred.id_sklad AS id_man, m.id_kas AS id_kas, m.hladajuzaverka AS uzaverka, m.giakt_mena AS c_mena,;
		m.label_yes AS k_odpoctu, m.cen_hlad_davk AS cen_hlad, dan, nazev, c_data, 000000000 AS poradi, SUM(mnozstvi*cena_prod) AS cenadan, 000000000000 as c_stredisk ;
		GROUP BY c_karty FROM buf_dav inner JOIN davk_stred ON buf_dav.c_sklad=davk_stred.c_sklad INTO CURSOR sk_pom READWRITE 
	UPDATE sk_pom SET c_stredisk=strediska.c_stredisk FROM strediska WHERE sk_pom.id_man=strediska.id
	DELETE FOR mnozstvi=0
	REPLACE dan WITH 1 FOR dan=0
	REPLACE ALL cenadan WITH ROUND(prachy*(dan-1)/dan,2)
	REPLACE ALL poradi WITH RECNO()
	SELECT sk_pol
	INSERT INTO sk_pol (c_uzaverka, ucet, c_stredisk,c_karty, prachy, prachy_puv, dan, dan_sazba, mnozstvi, cen_hlad, k_odpoctu, poradi, dotacia, dil_porce, id_fmenu, c_data, kompez, stor_mno) ;
		SELECT m.hladajcuzaverka, VAL(SUBSTR(m.u_cis,3)) AS ucet, c_stredisk, c_karty, prachy, prachy, cenadan, (dan*100-100) as dan, mnozstvi, cen_hlad, k_odpoctu, poradi,"", 1, "", c_data, 0 AS kompez, 0 AS stor_mno FROM sk_pom
	REPLACE cen_hlad WITH ALLTRIM(STR(VAL(cen_hlad),2,0)) FOR VAL(cen_hlad)>0
	=tux(.T.,.T.,"sk_pol")
	=REQUERY("sk_pol")
	SELECT sk_pol
	SUM prachy TO m.cena_celk
ELSE
	SELECT c_karty, SUM(mnozstvi) AS mnozstvi, SUM(mnozstvi * cena_prod) AS prachy,  ;
		m.u_cis AS u_cis, davk_stred.id_sklad AS id_man, m.id_kas AS id_kas, m.hladajuzaverka AS uzaverka, m.giakt_mena AS c_mena,;
		m.label_yes AS k_odpoctu, m.cen_hlad_davk AS cen_hlad, dan, nazev, c_karty AS idriadok;
		GROUP BY c_karty FROM buf_dav inner JOIN davk_stred ON buf_dav.c_sklad=davk_stred.c_sklad INTO CURSOR sk_pom READWRITE
	DELETE FOR mnozstvi=0
	m.idriadok=i_max("idriadok sk_pol",RECCOUNT("sk_pom"))
	REPLACE ALL idriadok WITH m.idriadok+1-RECNO()
	REPLACE dan WITH 1 FOR dan=0
	SELECT sk_pol
	INSERT INTO sk_pol (c_karty, nazev, u_cis, id_kas, id_man, mnozstvi, stor_mno, prachy, uzaverka, cen_hlad, k_odpoctu, dan, idriadok) SELECT c_karty, nazev, u_cis, id_kas, id_man, mnozstvi, mnozstvi AS stor_mno, prachy, hladajuzaverka, cen_hlad, k_odpoctu, dan, idriadok FROM sk_pom
	SELECT sk_pom
ENDIF
* * * * * * * *

IF m.issql
	SELECT 0
	USE (m.sqlprefix+"rskfinuzaverka") ALIAS sk_fin
	=CURSORSETPROP("Buffering",5,"sk_fin")
	INSERT INTO sk_fin (c_uzaverka, ucet, autor, u_cas, u_minut, mutace, cas_atrib, pocet_h, stul, text_sleva, text_link, ducis, round50, pohladavka, c_data) ;
		VALUES (m.hladajcuzaverka, VAL(SUBSTR(m.u_cis,3)), m.gcuser_id, ser_time(), 1, "", "Q", m.pocethostu, 0,  "     "+m.hladajdavku,  "     "+m.hladajdavku,"0001", 0, 0, m.cdata)
	=tux(.T.,.T.,"sk_fin")
ELSE
	STORE 0 TO m.trzba_dph1, m.trzba_dph2, m.trzba_dph3, m.trzba_dph4, m.cena_celk, m.dan_z
	STORE 9 TO m.odp_dph1, m.odp_dph2, m.odp_dph3, m.odp_dph4

	SELECT dan, SUM(prachy) AS prachy GROUP BY dan FROM sk_pom INTO CURSOR odp_dane READWRITE
	DELETE FOR EMPTY(prachy)
	IF !prazdna()
		SUM prachy TO m.cena_celk
		SUM prachy / dan TO dan_z
	ELSE
		m.cena_celk=0
		m.dan_z=0
	ENDIF
	SCAN
		iii = STR(RECNO(),1)
		trzba_dph&iii = odp_dane.prachy
		odp_dph&iii   = odp_dane.dan
	ENDSCAN
	USE IN odp_dane
	m.dan_z=ROUND(m.dan_z, en__meny(m.cmena,5))
	m.cas_atrib = "Q"
	m.pocet_h = 33
	INSERT INTO sk_fin ;
		(id_kas, uzaverka, AUTOR, cena_celk, cena_slev, dph_celk, cas_atrib, pocet_h, ;
		dan1, dan2, dan3, dan4, trzba_dph1, trzba_dph2, trzba_dph3, trzba_dph4, text_sleva, u_cis, u_cas, u_open, idriadok) ;
		VALUE (m.id_kas, m.hladajuzaverka, m.gcuser_id, m.cena_celk, m.cena_celk, m.cena_celk-m.dan_z, m.cas_atrib, m.pocethostu, ;
		m.odp_dph1, m.odp_dph2, m.odp_dph3, m.odp_dph4, m.trzba_dph1, m.trzba_dph2, m.trzba_dph3, m.trzba_dph4, "     "+m.hladajdavku, m.u_cis, ser_time(), "DAVKA", i_max("idriadok sk_fin"))
ENDIF
IF m.issql
	SELECT 0
	USE (m.sqlprefix+"rskpltuzaverka") ALIAS sk_plt
	=CURSORSETPROP("Buffering",5,"sk_plt")
	INSERT INTO sk_plt (c_uzaverka, ucet, druh_pl, cena_pl, tip, mena, cena_mena, poradi, text, c_data) ;
		VALUE (m.hladajcuzaverka, VAL(SUBSTR(m.u_cis,3)), "D_DAVKA", m.cena_celk, 0, en__meny(m.giakt_mena,3), m.cena_celk, 1, "", m.cdata)
	=tux(.T.,.T.,"sk_plt")
ELSE
	INSERT INTO sk_plt (cena_pl, druh_pl, u_cis, uzaverka, mena, cena_mena, idriadok) ;
		VALUE (m.cena_celk, "D_DAVKA", m.u_cis, m.hladajuzaverka, en__meny(m.giakt_mena,3), m.cena_celk, i_max("idriadok sk_plt"))
ENDIF
IF m.issql
	SELECT 0
	USE (m.sqlprefix+"rsksezuzaverka") ALIAS sk_sez NODATA
	=CURSORSETPROP("Buffering",5,"sk_sez")
	SELECT id_sklad as id_man, m.id_kas AS id_kas, m.hladajuzaverka AS uzaverka, m.gcuser_id AS AUTOR, 1 AS odecteno, "D_" + m.hladajdavku AS lastuzav,m.giakt_mena AS c_mena, m.cdata AS c_data, m.hladajcuzaverka as c_uzaverka, m.hladajckasy as c_kas, 000000000000 as c_stredisk FROM davk_stred INTO CURSOR sk_pom READWRITE 
	UPDATE sk_pom SET c_stredisk=strediska.c_stredisk FROM strediska WHERE sk_pom.id_man=strediska.id

	INSERT INTO sk_sez (id_man, id_kas, uzaverka, c_stredisk, c_kas, c_uzaverka, c_uzav_kas, AUTOR, odecteno, uz_cislo, lastuzav, c_mena, c_data, uct_den, priznak) ;
		SELECT id_man, id_kas, uzaverka, c_stredisk, c_kas, c_uzaverka, 0, m.gcuser_id, 1, 1, "D_" + m.hladajdavku,m.giakt_mena, m.cdata, m.datum_dokl, "" FROM sk_pom 
	=tux(.T.,.T.,"sk_sez")
ELSE
	INSERT INTO (m.fn_sk_sez) (id_man, id_kas, uzaverka, AUTOR, odecteno, lastuzav,c_mena, idriadok) ;
		SELECT id_sklad, m.id_kas AS id_kas, m.hladajuzaverka AS uzaverka, m.gcuser_id AS AUTOR, m.lcodecteno AS odecteno, "D_" + m.hladajdavku AS lastuzav,m.giakt_mena AS c_mena, i_max("idriadok sk_sez") AS c_data FROM davk_stred
ENDIF
DO pozavirej WITH "sk_fin,sk_pol,sk_plt,sk_sez,sk_pom"
