PARAMETERS hladajdavku, datum_dokl, id_kas, cen_hlad, pocethostu, cmena, poznamka
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.hladajcuzaverka, m.hladajoperace, m.typpohybu, m.hladajdavkuold
typpohybu=0
hladajcstredisk=m.gic_stredisk

storno = .F.    &&&  (PARAMETERS() > 6) prestalo fungovat pridanim parametru poznamka


*** M.B. 11.09.2019, pre sql data si vytvorim novu premennu, kde ulozim povodne meno davky, aby som ho vedel pouzit vo volani postgresovej funkcie
hladajdavkuold = m.hladajdavku
IF !EMPTY(m.poznamka)
	hladajdavku = m.poznamka
ENDIF 
prach_prod = 0
lic_dokl = -8888
id_kas = LEFT(ALLT(m.id_kas), 2)
nakl_celk = -11.11
lcoperace="VK"
IF m.pocethostu = 0
	pocethostu = 1
ENDIF
menobuffera=LOWER(SUBSTR(SYS(2015),4))
chyba_pri_otevirani = .F.
hladajoperace="VK"

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
		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
		SELECT 0
		otevira_se = "vodpoctyaskl"
		USE vodpoctyaskl ALIAS a_skl
		INDEX on c_karty TAG c_karty
		otevira_se = "prep"
		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
	ENDIF 
CATCH TO oErr 
    otevira_se = m.otevira_se + CHR(13) + ALLTRIM(STR(oErr.ErrorNo)) + " " + oErr.Message 
    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")

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

IF VAL(m.cen_hlad)>0
	cen_hlad = TRANSFORM(VAL(m.cen_hlad), "@L 99")
ELSE 
    cen_hlad = "01"
ENDIF 
cen_hlad_davk = m.cen_hlad

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 * cena AS prachyprum, ;
	poradi, m.gcpgm_id AS id_man, m.id_kas AS id_kas, m.hladajuzaverka AS uzaverka, SPACE(13) 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 ;
	FROM buf_dav INTO CURSOR sk_pom READWRITE WHERE mnozstvi<>0
UPDATE sk_pom SET prachyprum=sk_pom.mnozstvi*menaz.cena_prum, kod=menaz.kod, dan=menaz.dan from menaz WHERE sk_pom.c_karty=menaz.c_karty
REPLACE FOR EMPTY(dan) dan WITH m.dph1
TRY 
	REPLACE FOR ABS(prachyprum)>0.1 marze WITH ( prachy/dan - prachyprum ) / prachyprum * 100
CATCH
ENDTRY 

IF prazdna()
	RETURN .F.
ENDIF
SELECT sk_pom
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 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)
		ENDIF 
	ENDIF 
ENDIF 
DO pozavirej WITH "volby2, typypohybov"
IF m.davka_tlacit_reporty
	SELECT sk_pom
	DO pr_report WITH "davk_odp"
    naklady = 0
	DO pr_report WITH "davk_od2"
ENDIF 
had = CREATEOBJECT("had", jaz("prg_odpocet_davky")+" " + m.hladajdavku )
had.SHOW()
small_log("CRLF", "odp_davky")

DO pozavirej WITH "aaa"
IF m.issql

	SELECT 0
	USE (m.sqlprefix+"rmaxcdokl") ALIAS aaa
	cdokldo=aaa.c_dokl
	USE IN aaa
	vysledok = sql_exec("SELECT food600.odpocet_davky('"+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.hladajdavkuold+"')")
	REQUERY("buffer")
	REQUERY("buf_dokl")
ELSE 
	SELECT food_dat
	LOCATE FOR ID = m.id_kas
	c_partner = food_dat.c_stredisk

	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

	IF !USED("obraty")
		SELECT 0
		USE (m.fn_obraty) ALIAS obraty
	ENDIF

	SELECT 0
	USE vmaxcdokl ALIAS aaa
	cdokldo=aaa.c_dokl
	USE IN aaa 

	SELECT mnozstvi * cena_prod AS prach_prod FROM buf_dav INTO CURSOR prach_prod
	SCATTER MEMVAR
	USE

	SELECT c_karty, SUM(mnozstvi)+0.0000 AS mnozstvi, 0000000.0000 AS odecteno ;
		GROUP BY c_karty FROM buf_dav INTO CURSOR sk1 READWRITE 

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

	SELECT BUFFER
	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.gic_stredisk 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
	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.gic_stredisk)
	lic_dokl = m.lic_dokl + 1

	DO pozavirej WITH "sk1, sk2, qqq, kalk, a_skl"
	SELECT BUFFER
	REPLACE ALL poradi WITH RECNO()
	WAIT CLEAR
	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 
SELECT buf_dokl
REPLACE typ_pohybu WITH m.typpohybu, provedl WITH m.gcuser_id
tux(.t.,.t.,"buf_dokl")
REQUERY("buf_dokl")
WAIT CLEAR
RELEASE had
small_log("pred zrc_crea " + m.hladajdavku, "odp_davky")
IF zrc_crea(.f.,.f.,m.cdokldo, m.menobuffera, m.lcoperace)
    small_log("po zrc_crea", "odp_davky")
	SELECT zrc
	SUM mnoz_ok * cena_prum TO nakl_celk
	naklady = m.nakl_celk
	SELECT buf_dokl
	REQUERY("buf_dokl")
	hladajcdokl=buf_dokl.c_dokl
    small_log("pred ins_sk", "odp_davky")
	DO ins_sk
    small_log("po ins_sk", "odp_davky")
	ret=.T.
ELSE
	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
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
	hladajidkas=m.id_kas
	SELECT strediska
	LOCATE FOR id=m.hladajidkas
	hladajckasy=strediska.c_stredisk
	SELECT 0
	USE (m.sqlprefix+"rskuzahladajcuzaverky") ALIAS maxuz 
	IF RECCOUNT()=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 
	hladajcuzaverka=maxuz.idriadok
	USE IN maxuz

	SELECT 0
	USE (m.sqlprefix+"rskpoluzaverka") ALIAS sk_pol
	=CURSORSETPROP("Buffering",5,"sk_pol")
	SELECT buf_dav.c_karty, SUM(buf_dav.mnozstvi) AS mnozstvi, SUM(buf_dav.mnozstvi * buf_dav.cena_prod) AS prachy, SUM(buf_dav.mnozstvi*buf_dav.cena_prod) AS prachyprum,  ;
		m.u_cis AS u_cis, m.gcpgm_id 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, 1.00 as dan, buf_dav.nazev, buf_dav.c_data, buf_dav.poradi, SUM(buf_dav.mnozstvi*buf_dav.cena_prod) AS cenadan ;
		GROUP BY buf_dav.c_karty;
		FROM buf_dav INTO CURSOR sk_pom READWRITE 
	UPDATE sk_pom SET prachyprum=sk_pom.mnozstvi*menaz.cena_prum, dan=menaz.dan from menaz WHERE sk_pom.c_karty=menaz.c_karty

	DELETE FOR mnozstvi = 0
    REPLACE FOR isnull(dan) dan WITH 0
	IF m.slovenskaverzia
		REPLACE FOR dan = 0 dan WITH 1
	ELSE
		REPLACE FOR dan = 0 dan WITH 1.21
	ENDIF
	REPLACE ALL cenadan WITH ROUND(prachy*(dan-1)/dan,2), poradi WITH RECNO()
	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, m.gic_stredisk AS 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 TRANSFORM(VAL(cen_hlad), "@L 99") FOR VAL(cen_hlad) > 0

	SELECT sk_pom
	tux(.t.,.t.,"sk_pol")
	REQUERY("sk_pol")
	SELECT sk_pol
ELSE 
	SELECT buf_dav.c_karty, SUM(buf_dav.mnozstvi) AS mnozstvi, SUM(buf_dav.mnozstvi * buf_dav.cena_prod) AS prachy, SUM(buf_dav.mnozstvi * buf_dav.cena_prod) AS prachyprum, ;
		m.u_cis AS u_cis, m.gcpgm_id 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, 1.00 as dan, buf_dav.nazev, buf_dav.c_karty as idriadok, buf_dav.poradi;
		GROUP BY buf_dav.c_karty;
		FROM buf_dav INTO CURSOR sk_pom READWRITE 
	UPDATE sk_pom SET prachyprum=sk_pom.mnozstvi*menaz.cena_prum, dan=menaz.dan from menaz WHERE sk_pom.c_karty=menaz.c_karty
	DELETE FOR mnozstvi=0
	TRY 
		idriadok=i_max("idriadok sk_pol",RECCOUNT("sk_pom"))
		REPLACE ALL idriadok WITH m.idriadok+1-RECNO()
	CATCH 
	ENDTRY 
	IF m.slovenskaverzia
		REPLACE FOR dan=0 dan WITH 1
	ELSE
		REPLACE FOR dan=0 dan WITH 1.21
	ENDIF
	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, prachy, hladajuzaverka, cen_hlad, k_odpoctu, dan, idriadok FROM sk_pom
	SELECT sk_pom
ENDIF 

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
	cena_celk=0
	dan_z=0
ENDIF
SCAN
	iii = STR(RECNO(),1)
	trzba_dph&iii = odp_dane.prachy
	odp_dph&iii   = odp_dane.dan
ENDSCAN
IF m.odp_dph1=9 AND m.trzba_dph1=0
	odp_dph1=0
ENDIF 
IF m.odp_dph2=9 AND m.trzba_dph2=0
	odp_dph2=0
ENDIF 
IF m.odp_dph3=9 AND m.trzba_dph3=0
	odp_dph3=0
ENDIF 
IF m.odp_dph4=9 AND m.trzba_dph4=0
	odp_dph4=0
ENDIF 
USE IN odp_dane
dan_z=ROUND(m.dan_z, en__meny(m.cmena,5))
cas_atrib = "Q"
pocet_h = 33

SELECT sk_pom
LOCATE FOR poradi # 0
IF FOUND()
	INDEX ON poradi TAG poradi
ENDIF
SELECT buffer
SUM -mnozstvi*cena TO naklady
SELECT sk_pom
SUM prachy/dan TO trzby
DO pr_report WITH "davk_od2"

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, "", m.cas_atrib, m.pocethostu, 0,  "     "+m.hladajdavku,  "     "+m.hladajdavku,"0001", 0, 0, m.cdata)
	tux(.t.,.t.,"sk_fin")
ELSE
	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.is_revenueman
	DO FORM revenueman WITH "ucet",m.u_cis, m.hladajuzaverka
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, fik, bkp) ;
		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, "DAVKA NEFISKALNI", "DAVKA NEFISKALNI")
	tux(.t.,.t.,"sk_plt")
ELSE
    INSERT INTO sk_plt (cena_pl, druh_pl, u_cis, uzaverka, mena, cena_mena, idriadok, fik, bkp) ;
		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"), "DAVKA NEFISKALNI", "DAVKA NEFISKALNI")
ENDIF 
IF m.issql
	SELECT 0
	USE (m.sqlprefix+"rsksezuzaverka") ALIAS sk_sez NODATA 
	=CURSORSETPROP("Buffering",5,"sk_sez")
	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) ;
		VALUE (m.gcpgm_id, m.id_kas, m.hladajuzaverka, m.gic_stredisk, m.hladajckasy, m.hladajcuzaverka, 0, m.gcuser_id, 1, 1, "D_" + m.hladajdavku,m.giakt_mena, m.cdata, m.datum_dokl, "")
	tux(.t.,.t.,"sk_sez")
ELSE
	INSERT INTO (m.fn_sk_sez) (id_man, id_kas, uzaverka, AUTOR, odecteno, lastuzav, c_mena, idriadok, uct_den) ;
		VALUE (m.gcpgm_id, m.id_kas, m.hladajuzaverka, m.gcuser_id, m.lcodecteno,  "D_" + m.hladajdavku,m.giakt_mena, i_max("idriadok sk_sez"), m.datum_dokl)
ENDIF 
DO pozavirej WITH "sk_fin,sk_pol,sk_plt,sk_sez,sk_pom"
