
*:*****************************************************************************
PROCEDURE tvor_obrsum
IF !USED("obr_sum")
	IF !USED("odecteno")
		DO pozavirej WITH "namarkovano"
		SELECT DISTINCT c_kas, uzaverka FROM tm_sk_pol INTO CURSOR namarkovano READWRITE 
		DO priprav_odectene IN s_trzby_new
		DO pozavirej WITH "namarkovano"
	ENDIF 
	SELECT odecteno.nazev, odecteno.druh, odecteno.spart, odecteno.cena, -odecteno.mnozstvi as mnozstvi, odecteno.doklad, 'VK' as operace, fp_str.id as id_man,;
		odecteno.c_stredisk, odecteno.pz2, odecteno.cen_hlad, odecteno.dan FROM odecteno LEFT JOIN fp_str ON odecteno.c_stredisk=fp_str.c_stredisk INTO CURSOR obr_sum READWRITE 
ENDIF 


*:*****************************************************************************
PROCEDURE pripravdata
PUBLIC m.roundx
LOCAL m.oblast, m.cena_akt, m.prikaz
IF USED("pri_buff") AND USED("buf_ucet")
	RETURN 
ENDIF 
priprav_had = CREATEOBJECT("had", PROGRAM())
priprav_had.SHOW()
*****  *****  *****  *****  *****  *****  *****  *****  *****  *****  *****  *****  *****  *****  *****  
SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, SPACE(30) as nazev, u_cis, dan_sazba as dan, SPACE(2) as id_prev, SPACE(30) as prevadzka, ;
	SUM(prachy) AS sumpc, SPACE(5) AS pl_cas, $0 AS summc, SUM(mnozstvi) AS mnozstvi, SUM(dan) AS sum_dph,;
	0000000.00 AS sumpc_dan, 00.00 AS round50, dan_sazba AS dan1, SUM($0+prachy_puv-prachy) AS zlava, 000000000.00 AS celkom, 0000000.00 AS sumdanzakl, SPACE(m.sirkastlpcadruh) as druh, SPACE(8) AS druh_pl, ;
	SPACE(50) AS TEXT, SPACE(3) as autor, SPACE(3) as spart, c_mena, 000.0000 as dan01, " " as cas_atrib, id_fmenu, .f. as minusy, SPACE(8) as den, c_druh, 000000000.00 as teorpc, 0000 as pocet_h, SPACE(50) as miestnost, SPACE(50) as menostola, 000000.0 as stul , k_odpoctu;
    FROM tm_sk_pol WHERE !EMPTY(k_odpoctu) AND LEFT(ALLTRIM(cen_hlad),1)$m.cen_hladiny INTO CURSOR pri_buff READWRITE ;
    GROUP BY id_kas, id_man, uzaverka, cen_hlad, c_karty, u_cis, dan, id_fmenu ;
    ORDER BY id_kas, id_man, uzaverka, cen_hlad
REPLACE ALL teorpc WITH sumpc
UPDATE pri_buff SET id_prev=fd.id_prev, prevadzka=fd.prevadzka from fd WHERE pri_buff.id_kas=fd.id
SELECT disti id_kas FROM pri_buff WHERE VAL(cen_hlad)<>1 INTO CURSOR prihlad
IF RECCOUNT("prihlad")>0
	SELECT prihlad
	m.oblast="a1"
	IF DATE()>m.oblast_a
		m.oblast="a1"
		IF DATE()>m.oblast_b
			m.oblast="b1"
			IF DATE()>m.oblast_c
				m.oblast="c1"
			ENDIF 
		ELSE
			IF DATE()>m.oblast_c
				m.oblast="c1"
			ENDIF 
		ENDIF 
	ELSE
		IF DATE()>m.oblast_b
			m.oblast="b1"
			IF DATE()>m.oblast_c
				m.oblast="c1"
			ENDIF 
		ELSE
			IF DATE()>m.oblast_c
				m.oblast="c1"
			ENDIF 
		ENDIF 
	ENDIF 
	SCAN ALL 
		SELECT fd
		LOCATE FOR id=prihlad.id_kas
		IF FOUND()
			m.cena_akt=EVALUATE("fd."+m.oblast)
			IF BETWEEN(m.cena_akt,"01","40")
				UPDATE pri_buff SET teorpc=pri_buff.mnozstvi*karty.cena&cena_akt from karty WHERE pri_buff.c_karty=karty.c_karty
			ELSE 
				IF BETWEEN(m.cena_akt,"41","90")
					UPDATE pri_buff SET teorpc=pri_buff.mnozstvi*kceny.cena&cena_akt from kceny WHERE pri_buff.c_karty=kceny.c_karty
				ENDIF 
			ENDIF 
		ENDIF 
	ENDSCAN 
	USE IN prihlad
ENDIF 
SELECT pri_buff
DO dopln_nazev IN s_trzby_new
UPDATE pri_buff SET dan01=skupiny.dan01 from skupiny WHERE pri_buff.c_druh=skupiny.c_druh

REPLACE minusy WITH .t. FOR sumpc<0
REPLACE dan01 WITH 1.00 FOR dan01=0 OR ISNULL(dan01)
priprav_had.set_had(50)
SET ORDER TO uz_ucet IN tm_sk_fin
SET RELATION TO uzaverka + u_cis INTO tm_sk_fin
REPLACE ALL pri_buff.cas_atrib WITH tm_sk_fin.cas_atrib, pri_buff.round50 WITH tm_sk_fin.round50, pri_buff.pl_cas WITH RIGHT(tm_sk_fin.u_cas,5),pri_buff.den WITH LEFT(tm_sk_fin.uzaverka,6)+LEFT(tm_sk_fin.u_cas,2), text with tm_sk_fin.text_sleva, pri_buff.autor WITH tm_sk_fin.autor, pri_buff.stul WITH tm_sk_fin.stul && nebo text_LINK?
REPLACE FOR ! EMPTY(tm_sk_fin.id_limit) TEXT WITH "Limit_" + ALLTRIM(STR(tm_sk_fin.id_limit))
IF m.issql
	TRY 
		USE (m.sqlprefix+"rfoodie_stoly") ALIAS stoly IN 0
		=RECCOUNT("stoly")
		UPDATE pri_buff SET menostola=ALLTRIM(stoly.stol), miestnost=ALLTRIM(stoly.miestnost) from stoly WHERE pri_buff.stul=stoly.id
		USE IN stoly 
	CATCH 
	ENDTRY 
	SELECT pri_buff
ENDIF 
UPDATE pri_buff SET miestnost=i_zkrat(id_kas) WHERE EMPTY(miestnost)
UPDATE pri_buff SET menostola=ALLTRIM(STR(stul,6,1)) WHERE EMPTY(menostola)

SET RELATION TO 
UPDATE pri_buff SET nazev=karty.nazev from karty WHERE pri_buff.c_karty=karty.c_karty
INDEX ON uzaverka+u_cis TAG uzuc
LOCAL m.uzaverka, m.u_cis
m.uzaverka="sdfsdfsd"
m.u_cis="asdfa"
SCAN FOR !EMPTY(round50)
	IF pri_buff.uzaverka=m.uzaverka AND pri_buff.u_cis=m.u_cis
		REPLACE round50 WITH 0
	ELSE
		m.uzaverka=pri_buff.uzaverka
		m.u_cis=pri_buff.u_cis
	ENDIF 
ENDSCAN 
REPLACE ALL pri_buff.sumpc_dan WITH pri_buff.sumpc*(pri_buff.dan01-1)
UPDATE pri_buff SET text=tm_sk_plt.text from tm_sk_plt WHERE pri_buff.uzaverka=tm_sk_plt.uzaverka AND pri_buff.u_cis=tm_sk_plt.u_cis AND EMPTY(pri_buff.text)

*!*	IF m.girep_mena=2
*!*		REPLACE ALL round50 WITH 0
*!*	ENDIF

priprav_had.set_had(88)
SELECT id_kas, id_man, uzaverka, u_cis, dan, cen_hlad, pl_cas, text, SUM(sumpc) AS sumpc, SUM(round50) as round50, ;
	SUM(summc) AS summc, SUM(sum_dph) as sum_dph, SUM(sumpc_dan) AS sumpc_dan, $0 as sumdanzakl, SPACE(17) as davka, SPACE(2) as id_prev, SPACE(30) as prevadzka ;
    FROM pri_buff INTO CURSOR buf_ucet READWRITE ;
	GROUP BY uzaverka, u_cis, id_man, id_kas, cen_hlad, dan ;
	ORDER BY uzaverka, u_cis, id_man, id_kas, cen_hlad
REPLACE ALL sumdanzakl WITH sumpc - sum_dph
UPDATE buf_ucet SET davka=SUBSTR(tm_sk_sez.lastuzav,3) from tm_sk_sez WHERE buf_ucet.uzaverka=tm_sk_sez.uzaverka AND LEFT(tm_sk_sez.lastuzav,2)="D_"
UPDATE buf_ucet SET id_prev=fd.id_prev, prevadzka=fd.prevadzka from fd WHERE buf_ucet.id_kas=fd.id
priprav_had.set_had(99)
RELEASE priprav_had


*!*****************************************************************************
*!      Procedure: STOR_CAS
*!      Storna casnikov
*!*****************************************************************************
PROCEDURE stor_cas
PARAMETERS repriadok
DO pozavirej WITH "bufff, bufffx"
SELECT autor, uzaverka, u_cis, SUM(cena_celk-sleva_abs + round50) AS prachy, id_kas, LEFT(text_sleva,1) AS text_sleva, text_link, c_mena ;
       FROM tm_sk_fin INTO CURSOR bufffx WHERE (cena_celk-sleva_abs < 0) AND (LEFT(text_sleva,1)=="@" OR LEFT(text_sleva,1)=="^" OR LEFT(text_link,1)=="^") ;
       GROUP BY id_kas, uzaverka, u_cis, autor, text_sleva

IF "1" $ m.repriadok
	SELECT id_kas, autor, text_sleva, SUM(prachy) AS prachy ;
		FROM bufffx INTO CURSOR bufff GROUP BY id_kas, autor, text_sleva
	DO pr_report WITH "trz_i_storna", !m.reporty_volane_z_formu
ENDIF 

IF "2" $ m.repriadok
	SELECT * FROM bufffx INTO CURSOR bufff
	DO pr_report WITH "trz_i_storuc", !m.reporty_volane_z_formu
ENDIF
IF "3" $ m.repriadok
	SELECT c_karty, id_kas, mnozstvi, prachy, SPACE(3) as autor, id_man, uzaverka, u_cis  FROM tm_sk_pol INTO CURSOR bufffxx;
		WHERE uzaverka+u_cis in (SELECT uzaverka+u_cis FROM tm_sk_fin WHERE (cena_celk-sleva_abs < 0) AND LEFT(text_sleva,1)=="@") READWRITE
	UPDATE bufffxx SET autor=tm_sk_fin.autor from tm_sk_fin WHERE bufffxx.uzaverka=tm_sk_fin.uzaverka AND bufffxx.u_cis=tm_sk_fin.u_cis
	SELECT SPACE(30) as nazev, c_karty, id_kas, SUM(mnozstvi) as mnozstvi, SUM(prachy) as prachy, autor, id_man, uzaverka  FROM bufffxx INTO CURSOR bufff;
		GROUP BY id_kas, uzaverka, autor, c_karty ORDER BY id_kas, uzaverka, c_karty, autor READWRITE
	DO dopln_nazev IN s_trzby_new
	DO pr_report WITH "trz_i_storuc_sumar", !m.reporty_volane_z_formu
ENDIF 
DO pozavirej WITH "bufff, bufffx, bufffxx"



*!*****************************************************************************
*!      Procedure: TRZ_PLAT
*!      Trzby podla druhov platieb
*!*****************************************************************************
PROCEDURE trz_plat
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
DO pozavirej WITH "vybery"
trzplat_had = CREATEOBJECT("had", PROGRAM())
trzplat_had.SHOW()
IF !USED("druhy_pl")
	SELECT 0
	IF m.issql 
		rv("druhy_pl","druhy_pl")
	ELSE 
		USE (m.gchom_path+"druhy_pl")
	ENDIF 
ELSE
	SELECT druhy_pl
ENDIF 
IF "1" $ m.repriadok
	DO pozavirej WITH "buff"
	trzplat_had.set_had(5)
	DO tvor_dph1 IN sk_stati
	trzplat_had.set_had(15)
	SELECT druh_pl AS druh, SPACE(30) as menoplatby, 00000 as poradie, SUM(cena_pl) AS prachy, LEFT(u_cis,2) AS id_kas, SUM(round50) as round50, ;
		SUM(cena_dan) AS sum_dph, SUM(cena_pl - cena_dan) AS dan_zakl ;
        FROM dph1 INTO CURSOR buff GROUP BY druh READWRITE 
	INDEX on STR(poradie)+druh TAG poradie
    UPDATE buff SET menoplatby=druhy_pl.j0, poradie=druhy_pl.zaokr from druhy_pl WHERE ALLTRIM(buff.druh)==ALLTRIM(druhy_pl.nazev)    
    SELECT buff
    TRY 
	    REPLACE menoplatby WITH i_drupla(buff.druh) FOR EMPTY(buff.menoplatby) OR ISNULL(buff.menoplatby)
	CATCH
		SCAN FOR EMPTY(buff.menoplatby) OR ISNULL(buff.menoplatby)
			IF !ISNULL(i_drupla(buff.druh))
				REPLACE menoplatby WITH i_drupla(buff.druh)
			ENDIF 
		ENDSCAN   
	ENDTRY 
	trzplat_had.HIDE()
	SELECT DISTINCT druh AS druh_pl,menoplatby AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	INDEX ON druh_pl TAG druh_pl
	INDEX ON druh TAG druh
*!*		REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",150, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_pl
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO druh INTO vybery
			DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT BUFF
			DO pr_report WITH "trz_i_platba2", !m.reporty_volane_z_formu
		ENDIF
	ENDIF
	DO pozavirej WITH "buff"
	trzplat_had.SHOW()
ENDIF
IF "2"$m.repriadok
	DO pozavirej WITH "buff"
	DO tvor_dph1 IN sk_stati
	trzplat_had.set_had(30)
	SELECT druh_pl AS druh, SPACE(30) as menoplatby, 00000 as poradie, SUM(cena_pl) AS prachy, id_kas_o AS id_kas, SUM(cena_dan) AS sum_dph,;
		(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl, dan, SUM(round50) as round50 FROM dph1 INTO CURSOR buff READWRITE GROUP BY id_kas, druh, dan;
		UNION ;
		SELECT "Spolu" AS druh, "Spolu" as menoplatby, 99 as poradie, SUM(cena_pl) AS prachy, id_kas_o AS id_kas, SUM(cena_dan) AS sum_dph,;
		(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl, dan, SUM(round50) as round50 FROM dph1  GROUP BY id_kas, dan;
		UNION ;
		SELECT druh_pl AS druh, SPACE(30) as menoplatby, 00000 as poradie, SUM(cena_pl) AS prachy, "XX" AS id_kas, SUM(cena_dan) AS sum_dph,;
		(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl, dan, SUM(round50) as round50 FROM dph1  GROUP BY druh, dan;
		UNION ;
		SELECT "Spolu" AS druh, "Spolu" as menoplatby, 99 as poradie, SUM(cena_pl) AS prachy, "XZ" AS id_kas, SUM(cena_dan) AS sum_dph,;
		(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl, dan, SUM(round50) as round50 FROM dph1 GROUP BY dan
	SELECT buff
	trzplat_had.set_had(40)
	INDEX ON id_kas+STR(poradie)+druh+STR(dan,4,2) TAG aaa
    SELECT buff 
    UPDATE buff SET menoplatby=druhy_pl.j0, poradie=druhy_pl.zaokr from druhy_pl WHERE ALLTRIM(buff.druh)==ALLTRIM(druhy_pl.nazev) AND buff.druh<>"Spolu"
    REPLACE menoplatby WITH i_drupla(buff.druh) FOR EMPTY(buff.menoplatby) OR ISNULL(buff.menoplatby)
	trzplat_had.HIDE()
	IF USED("vybery")
		SELECT vybery
		SET ORDER TO druh_pl
	ELSE
		SELECT DISTINCT druh AS druh_pl,menoplatby AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
		UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
		SELECT vybery
		INDEX ON druh_pl TAG druh_pl
		INDEX ON druh TAG druh
*!*			REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
		IF RECCOUNT()>0
			IF m.reporty_volane_z_formu
				DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
			ENDIF 
			SELECT vybery
			SET ORDER TO druh_pl
		ENDIF
	ENDIF
	LOCATE FOR !EMPTY(lbl)
	IF FOUND()
		SELECT BUFF
		SET RELATION TO druh INTO vybery
		DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
		SET RELATION OFF INTO vybery
		SELECT vybery
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			DELETE FOR druh="Spolu"
		ENDIF
		SELECT BUFF
		DO pr_report WITH "trz_i_platba", !m.reporty_volane_z_formu
	ENDIF
	trzplat_had.SHOW()
	DO pozavirej WITH "buff"
ENDIF
IF "3"$m.repriadok
	DO pozavirej WITH "buff"
	DO tvor_dph2 IN sk_stati
	trzplat_had.HIDE()
	IF USED("vybery")
		SELECT vybery
		SET ORDER TO druh_pl
	ELSE
		SELECT DISTINCT druh AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
		UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
		SELECT vybery
		INDEX ON druh_pl TAG druh_pl
		INDEX ON druh TAG druh
		REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
		IF RECCOUNT()>0
			IF m.reporty_volane_z_formu
				DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
			ENDIF 
			SELECT vybery
			SET ORDER TO druh_pl
		ENDIF
	ENDIF
	LOCATE FOR !EMPTY(lbl)
	IF FOUND()
		SELECT BUFF
		SET RELATION TO druh INTO vybery
		DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
		SET RELATION OFF INTO vybery
		SELECT BUFF
		DO pr_report WITH "trz_i_druh", !m.reporty_volane_z_formu
	ENDIF
	trzplat_had.SHOW()
	DO pozavirej WITH "buff"
ENDIF
IF "4"$m.repriadok
	IF !USED("buf_ucet")
		DO pripravdata
	ENDIF
	DO pozavirej WITH "buff"
	DO tvor_dph1 IN sk_stati
	SELECT * , 1.0000000000 AS koef FROM dph1 INTO CURSOR dph11 READWRITE 
	SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_pl, dan FROM dph1 INTO CURSOR dph111 READWRITE GROUP BY uzaverka, u_cis, dan
	INDEX ON uzaverka+u_cis+STR(dan,5,2) TAG uzuc
	SELECT dan as pr, druh_pl, cena_pl, cena_dan, u_cis, uzaverka, $0 as storna, $0 as zlavy, $0 as minus, id_kas_o, round50 FROM dph1 INTO CURSOR dphx READWRITE 
	USE IN dph1
	SELECT dph11
	SET RELATION TO uzaverka+u_cis+STR(dan,5,2) INTO dph111
*	REPLACE koef WITH 1 FOR dph111.cena_pl=0
	REPLACE koef WITH ROUND(dph11.cena_pl/dph111.cena_pl, 9) FOR dph111.cena_pl<>0
	USE IN dph111
	SELECT pri_buff.*, dph11.koef, dph11.druh_pl AS druh_plat FROM pri_buff, dph11 WHERE pri_buff.uzaverka=dph11.uzaverka .AND. pri_buff.u_cis=dph11.u_cis .AND. pri_buff.dan=dph11.dan;
		INTO CURSOR pribuff READWRITE 
	SELECT * FROM pribuff WHERE koef<>1 INTO CURSOR pribuff1 READWRITE 
	REPLACE ALL mnozstvi WITH ROUND(mnozstvi*koef, 4)
	REPLACE sumpc WITH ground(sumpc*(koef),m.girep_round) ALL
	REPLACE sum_dph WITH ground(sum_dph*(koef),m.girep_round) ALL
	SELECT pribuff
	DELETE FOR koef<>1
	INSERT INTO pribuff (id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
		sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat) ;
		SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
		sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat FROM pribuff1
	USE IN pribuff1

	SELECT SUM(sumpc) as cena, uzaverka,u_cis,druh_plat as druh_pl, dan as pr FROM pribuff GROUP BY uzaverka, u_cis, druh_pl,pr WHERE pribuff.mnozstvi<0 .AND. pribuff.sumpc<0 INTO CURSOR aaaaaa
	INDEX on uzaverka+u_cis+druh_pl+STR(pr,10,3) TAG aaa
	SELECT dphx
	SET RELATION TO uzaverka+u_cis+druh_pl+STR(pr,10,3) INTO aaaaaa
	REPLACE ALL storna WITH aaaaaa.cena
	SET RELATION OFF INTO aaaaaa
	SELECT SUM(sumpc) as cena, uzaverka,u_cis,druh_plat as druh_pl, dan as pr FROM pribuff GROUP BY uzaverka, u_cis, druh_pl,pr WHERE (pribuff.mnozstvi<0 .AND. pribuff.sumpc>0) .or. (pribuff.mnozstvi>0 .AND. pribuff.sumpc<0) INTO CURSOR aaaaaa
	INDEX on uzaverka+u_cis+druh_pl+STR(pr,10,3) TAG aaa
	SELECT dphx
	SET RELATION TO uzaverka+u_cis+druh_pl+STR(pr,10,3) INTO aaaaaa
	REPLACE ALL minus WITH aaaaaa.cena
	SET RELATION OFF INTO aaaaaa

*	SELECT SUM(IIF(zlava=0,1,sumpc/zlava)*(celkom-zlava)) as cena, uzaverka,u_cis,druh_plat as druh_pl, dan as pr FROM pribuff GROUP BY uzaverka, u_cis, druh_pl,pr  INTO CURSOR aaaaaa
	SELECT SUM(zlava) as cena, uzaverka,u_cis,druh_plat as druh_pl, dan as pr FROM pribuff GROUP BY uzaverka, u_cis, druh_pl,pr  INTO CURSOR aaaaaa
	INDEX on uzaverka+u_cis+druh_pl+STR(pr,10,3) TAG aaa
	SELECT dphx
	SET RELATION TO uzaverka+u_cis+druh_pl+STR(pr,10,3) INTO aaaaaa
	REPLACE ALL zlavy WITH aaaaaa.cena
	SET RELATION OFF INTO aaaaaa
	USE IN aaaaaa

	USE IN pribuff
	USE IN dph11
	trzplat_had.set_had(75)
	SELECT druh_pl AS druh, SUM(cena_pl) AS prachy, id_kas_o AS id_kas, SUM(cena_dan) AS sum_dph,SUM(storna) AS storna, SUM(zlavy) AS zlavy, SUM(MINUS) AS minusy,;
		(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl,pr AS dan, SUM(round50) as round50 FROM dphx INTO CURSOR buff READWRITE GROUP BY id_kas, druh,dan;
		UNION ;
		SELECT "Spolu" AS druh, SUM(cena_pl) AS prachy, id_kas_o AS id_kas, SUM(cena_dan) AS sum_dph,SUM(storna) AS storna, SUM(zlavy) AS zlavy, SUM(MINUS) AS minusy,;
		(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl,pr AS dan, SUM(round50) as round50 FROM dphx GROUP BY id_kas, dan;
		UNION ;
		SELECT druh_pl AS druh, SUM(cena_pl) AS prachy, "XX" AS id_kas, SUM(cena_dan) AS sum_dph,SUM(storna) AS storna, SUM(zlavy) AS zlavy, SUM(MINUS) AS minusy,;
		(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl,pr AS dan, SUM(round50) as round50 FROM dphx GROUP BY druh,dan;
		UNION ;
		SELECT "Spolu" AS druh, SUM(cena_pl) AS prachy, "XZ" AS id_kas, SUM(cena_dan) AS sum_dph,SUM(storna) AS storna, SUM(zlavy) AS zlavy, SUM(MINUS) AS minusy,;
		(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl,pr AS dan, SUM(round50) as round50 FROM dphx  GROUP BY dan
	USE IN dphx
	INDEX ON id_kas+druh+STR(dan,4,2) TAG aaa
	trzplat_had.set_had(80)
	trzplat_had.HIDE()
	IF USED("vybery")
		SELECT vybery
		SET ORDER TO druh_pl
	ELSE
		SELECT DISTINCT druh AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
		UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
		SELECT vybery
		INDEX ON druh_pl TAG druh_pl
		INDEX ON druh TAG druh
		REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
		IF RECCOUNT()>0
			IF m.reporty_volane_z_formu
				DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
			ENDIF 
			SELECT vybery
			SET ORDER TO druh_pl
		ENDIF
	ENDIF
	LOCATE FOR !EMPTY(lbl)
	IF FOUND()
		SELECT BUFF
		SET RELATION TO druh INTO vybery
		DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
		SET RELATION OFF INTO vybery
		SELECT vybery
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			DELETE FOR druh="Spolu"
		ENDIF
		SELECT BUFF
		DO pr_report WITH "trz_i_strdru", !m.reporty_volane_z_formu
	ENDIF
	trzplat_had.SHOW()
	DO pozavirej WITH "buff,dphx"
ENDIF
IF "5"$m.repriadok
	DO pozavirej WITH "buff"
	DO tvor_dph1
	LOCAL cotlacit
	IF m.reporty_volane_z_formu
		SELECT " " AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl FROM dph1 INTO CURSOR vybery READWRITE WHERE .f.
		INSERT INTO vybery (druh_pl, druh, lbl) VALUES ("1",jaz("form_po_kas_uzav_plat"),.t.)
		INSERT INTO vybery (druh_pl, druh, lbl) VALUES ("2",jaz("form_po_kas_plat_uzav"),.t.)
		INSERT INTO vybery (druh_pl, druh, lbl) VALUES ("3",jaz("form_po_dnoch_plat_kas"),.t.)
		SELECT vybery
		INDEX ON druh_pl TAG druh_pl
		IF RECCOUNT()>0
			DO FORM seldruhy WITH "vybery","druhpl",250, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		m.cotlacit=""
		SCAN FOR lbl
			m.cotlacit=m.cotlacit+ALLTRIM(vybery.druh_pl)
		ENDSCAN 
		DO pozavirej WITH "vybery"
	ELSE
		m.cotlacit="123"
	ENDIF
	IF !EMPTY(m.cotlacit)
		IF "1"$m.cotlacit
			SELECT druh_pl AS druh, SUM(cena_pl) AS prachy, id_kas_o AS id_kas, SUM(cena_dan) AS sum_dph,;
				(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl, uzaverka, dan, SUM(round50) as round50 FROM dph1 INTO CURSOR buff READWRITE GROUP BY id_kas, uzaverka, druh, dan;
				ORDER BY id_kas, uzaverka, druh
			trzplat_had.set_had(90)
			trzplat_had.HIDE()
			IF USED("vybery")
				SELECT vybery
				SET ORDER TO druh_pl
			ELSE
				SELECT DISTINCT druh AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
				UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
				SELECT vybery
				INDEX ON druh_pl TAG druh_pl
				INDEX ON druh TAG druh
				REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
				IF RECCOUNT()>0
					IF m.reporty_volane_z_formu
						DO FORM seldruhy WITH "vybery","druhpl",200,.t. TO m.druhxx
					ENDIF 
					SELECT vybery
					SET ORDER TO druh_pl
				ENDIF
			ENDIF
			LOCATE FOR !EMPTY(lbl)
			IF FOUND()
				SELECT BUFF
				SET RELATION TO druh INTO vybery
				DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
				SET RELATION OFF INTO vybery
				DO pr_report WITH "trz_i_uz1", !m.reporty_volane_z_formu
			ENDIF
		ENDIF 
		IF "2"$m.cotlacit
			trzplat_had.SHOW()
			IF !USED("dph1")
				DO tvor_dph1
			ENDIF 
			SELECT druh_pl AS druh, SUM(cena_pl) AS prachy, id_kas_o AS id_kas, SUM(cena_dan) AS sum_dph,;
				(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl, uzaverka, dan, SUM(round50) as round50 FROM dph1 INTO CURSOR buff READWRITE GROUP BY id_kas, uzaverka, druh, dan;
				ORDER BY id_kas, druh, uzaverka
			trzplat_had.set_had(100)
			trzplat_had.HIDE()
			IF USED("vybery")
				SELECT vybery
				SET ORDER TO druh_pl
			ELSE
				SELECT DISTINCT druh AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
				UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
				SELECT vybery
				INDEX ON druh_pl TAG druh_pl
				INDEX ON druh TAG druh
				REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
				IF RECCOUNT()>0
					IF m.reporty_volane_z_formu
						DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
					ENDIF 
					SELECT vybery
					SET ORDER TO druh_pl
				ENDIF
			ENDIF
			SELECT vybery 
			LOCATE FOR !EMPTY(lbl)
			IF FOUND()
				SELECT BUFF
				SET RELATION TO druh INTO vybery
				DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
				SET RELATION OFF INTO vybery
				DO pr_report WITH "trz_i_uz2", !m.reporty_volane_z_formu
			ENDIF
		ENDIF 
		IF "3"$m.cotlacit
			trzplat_had.SHOW()
			IF !USED("dph1")
				DO tvor_dph1
			ENDIF 
			SELECT druh_pl AS druh, SUM(cena_pl) AS prachy, id_kas_o AS id_kas, SUM(cena_dan) AS sum_dph,;
				(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl, uzaverka, dan, SUM(round50) as round50, SPACE(10) as den FROM dph1 INTO CURSOR buff READWRITE GROUP BY id_kas, uzaverka, druh, dan;
				ORDER BY id_kas, druh, uzaverka
			UPDATE buff SET den=DTOC(tm_sk_sez.uct_den) FROM tm_sk_sez WHERE buff.uzaverka=tm_sk_sez.uzaverka AND buff.id_kas=tm_sk_sez.id_kas
			trzplat_had.set_had(100)
			trzplat_had.HIDE()
			IF USED("vybery")
				SELECT vybery
				SET ORDER TO druh_pl
			ELSE
				SELECT DISTINCT druh AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
				UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
				SELECT vybery
				INDEX ON druh_pl TAG druh_pl
				INDEX ON druh TAG druh
				REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
				IF RECCOUNT()>0
					IF m.reporty_volane_z_formu
						DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
					ENDIF 
					SELECT vybery
					SET ORDER TO druh_pl
				ENDIF
			ENDIF
			LOCATE FOR !EMPTY(lbl)
			IF FOUND()
				SELECT BUFF
				SET RELATION TO druh INTO vybery
				DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
				SET RELATION OFF INTO vybery
				INDEX ON den+druh+id_kas+uzaverka TAG aaa
				DO pr_report WITH "trz_i_uz3", !m.reporty_volane_z_formu
			ENDIF
		ENDIF 
		trzplat_had.SHOW()
	ENDIF 
	DO pozavirej WITH "buff"
ENDIF
DO pozavirej WITH "vybery"
RELEASE trzplat_had


*!*****************************************************************************
*!      Procedure: GR_TRZCIS
*!      Trzby podla casnikov
*!*****************************************************************************
PROCEDURE gr_trzcis
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
trzcis_had = CREATEOBJECT("had", PROGRAM())
trzcis_had.SHOW()
DO pozavirej WITH "buff"
IF !USED("users")
	IF m.issql
		rv("users","users")
	ELSE 
		IF FILE(m.gchom_path + "users.dbf")
			USE (m.gchom_path + "users") AGAIN IN 0
		ENDIF 
	ENDIF 
ENDIF 
IF "1" $ m.repriadok
	IF !USED("pri_buff")
		DO pripravdata
	ENDIF
	SELECT id_kas, uzaverka, AUTOR, SUM(cena_celk-sleva_abs+round50) AS prachy, c_mena, $0 as summc ;
		GROUP BY  id_kas, uzaverka, autor FROM tm_sk_fin ;
		INTO CURSOR buffx READWRITE  ORDER BY id_kas, uzaverka, autor
	SELECT  SUM(summc) as summc, uzaverka, autor, id_kas FROM pri_buff GROUP BY id_kas, uzaverka, autor INTO CURSOR spotreby
	UPDATE buffx SET summc=spotreby.summc from spotreby WHERE buffx.id_kas=spotreby.id_kas AND buffx.uzaverka=spotreby.uzaverka AND buffx.autor=spotreby.autor
	USE IN spotreby
	SELECT id_kas, AUTOR, SUM(prachy) AS prachy, SUM(summc) as summc;
		GROUP BY  id_kas, autor FROM buffx ;
		INTO CURSOR buff READWRITE ORDER BY id_kas, autor
	
	trzcis_had.HIDE()
	DO pr_report WITH "trz_i_cisnik", !m.reporty_volane_z_formu
	trzcis_had.SHOW()
	USE IN BUFF
	USE IN buffx
ENDIF
IF "2" $ m.repriadok
	PRIVATE m.str, m.oldprc
	DO tvor_dph1
	trzcis_had.set_had(75)
	SELECT id_kas_o AS id_kas, autor, SUM(cena_pl) AS prachy, SUM(cena_dan) AS sum_dph, SUM(round50) as round50,;
		(SUM(cena_pl)-SUM(cena_dan)) AS dan_zakl;
		GROUP BY autor, id_kas FROM dph1 INTO CURSOR buff READWRITE ;
		ORDER BY id_kas, autor
	trzcis_had.HIDE()
	DO pr_report WITH "trz_i_cisnik2", !m.reporty_volane_z_formu
	trzcis_had.SHOW()
	USE IN BUFF
ENDIF
IF "3"$m.repriadok
	SELECT  tm_sk_fin.AUTOR, SUM(tm_sk_fin.cena_celk-tm_sk_fin.sleva_abs+tm_sk_fin.round50) AS prachy, USERS.mzda, tm_sk_fin.uzaverka, tm_sk_fin.c_mena, DATE() AS datum;
		GROUP BY tm_sk_fin.uzaverka, tm_sk_fin.AUTOR FROM tm_sk_fin LEFT JOIN users ON tm_sk_fin.autor=users.user_id;
		INTO CURSOR buffx READWRITE ORDER BY tm_sk_fin.uzaverka,tm_sk_fin.AUTOR
	REPLACE ALL datum WITH CTOD(SUBSTR(uzaverka,7,2)+"."+SUBSTR(uzaverka,5,2)+"."+LEFT(uzaverka,4))
	REPLACE datum WITH datum-1 FOR SUBSTR(uzaverka,10,2)<"07"
	SELECT  AUTOR, mzda, datum, SUM(prachy) AS prachy ;
		GROUP BY datum, AUTOR ORDER BY datum, AUTOR ;
		FROM buffx INTO CURSOR buff READWRITE 
	trzcis_had.HIDE()
	DELETE FOR prachy=0
	DO pr_report WITH "trz_i_cisnik3", !m.reporty_volane_z_formu
	trzcis_had.SHOW()
	DO pozavirej WITH "buffx, BUFF"
ENDIF
IF "4"$m.repriadok
	SELECT tm_sk_fin.id_kas, tm_sk_fin.autor, SUM(tm_sk_plt.tip) AS tip, tm_sk_plt.druh_pl, tm_sk_fin.c_mena, tm_sk_fin.uzaverka ;
		GROUP BY tm_sk_fin.AUTOR, tm_sk_fin.id_kas, tm_sk_fin.uzaverka, tm_sk_plt.druh_pl FROM tm_sk_fin, tm_sk_plt;
		WHERE (tm_sk_fin.uzaverka=tm_sk_plt.uzaverka) .and. ;
		(tm_sk_fin.u_cis=tm_sk_plt.u_cis)	INTO CURSOR buffx READWRITE ORDER BY tm_sk_fin.AUTOR, tm_sk_fin.id_kas

	DELETE FOR tip=0
	SELECT id_kas, AUTOR, SUM(tip) AS tip, druh_pl;
		GROUP BY AUTOR, id_kas,druh_pl FROM buffx ;
		INTO CURSOR buff READWRITE ORDER BY AUTOR, id_kas
	trzcis_had.set_had(50)
	IF !EMPTY(m.tip_kod)
		m.tipkalk=0
		IF m.issql
			PRIVATE m.hladajkod
			m.hladajkod=m.tip_kod
			SELECT 0
			USE (m.sqlprefix+"rkartykukodu") ALIAS kkod
			IF RECCOUNT()>0
				GO TOP 
				m.tipkalk=kkod.c_karty
			ENDIF 
			USE IN kkod
		ELSE 
			SELECT karty
			LOCATE FOR ALLTRIM(LOWER(kod))==ALLTRIM(LOWER(m.tip_kod))
			IF FOUND()
				m.tipkalk=karty.c_karty
			ENDIF 
		ENDIF 
		IF !EMPTY(m.tipkalk)
			SELECT id_kas, uzaverka, u_cis, prachy as tip, c_mena, "XYZ" as autor, SPACE(8) as druh_pl ;
                   WHERE c_karty = m.tipkalk FROM tm_sk_pol INTO CURSOR tipkalk1 READWRITE 
			IF RECCOUNT()>0
				SELECT tm_sk_plt
				INDEX ON uzaverka+u_cis TAG uzuc
*				SET ORDER TO uzuc
				SELECT tipkalk1
				SET  RELATION TO u_cis + uzaverka INTO tm_sk_plt
				REPLACE ALL druh_pl WITH tm_sk_plt.druh_pl
				SET RELATION TO
				SELECT id_kas,autor, druh_pl, SUM(tip) as tip ;
                       GROUP BY autor, id_kas, druh_pl FROM tipkalk1 INTO CURSOR tipkalk READWRITE 
				USE IN tipkalk1
			    SELECT tm_sk_plt
				DELE TAG uzuc
            ENDIF 
			SELECT buff 
			IF USED("tipkalk")
				INSERT INTO buff (id_kas, tip, autor, druh_pl) SELECT id_kas, tip, autor, druh_pl FROM tipkalk
				USE IN tipkalk
			ENDIF 
			RELEASE m.tipkalk
		ENDIF 
		SELECT buff
	ENDIF 
	trzcis_had.HIDE()
	DO pr_report WITH "trz_i_tip", !m.reporty_volane_z_formu
	trzcis_had.SHOW()
	DO pozavirej WITH "buffx, BUFF"
ENDIF
IF "5"$m.repriadok
	IF FSIZE("u_datetime","tm_sk_fin")>0
		SELECT tm_sk_fin.c_mena, tm_sk_fin.id_kas, tm_sk_fin.AUTOR, tm_sk_fin.uzaverka, tm_sk_fin.u_cis, SUM(tm_sk_plt.tip) AS tip, tm_sk_plt.druh_pl, SPACE(6) AS hpokoj,SPACE(15) AS hjmeno, PADR(ALLTRIM(tm_sk_fin.u_cas),19) as u_cas, tm_sk_fin.u_datetime ;
			GROUP BY tm_sk_fin.AUTOR, tm_sk_fin.id_kas, tm_sk_fin.uzaverka, tm_sk_fin.u_cis, tm_sk_plt.druh_pl ;
        	FROM tm_sk_fin LEFT JOIN tm_sk_plt ON (tm_sk_fin.uzaverka=tm_sk_plt.uzaverka) .and. (tm_sk_fin.u_cis=tm_sk_plt.u_cis);
			INTO CURSOR buff READWRITE ORDER BY tm_sk_fin.AUTOR, tm_sk_fin.id_kas
		UPDATE buff SET u_cas=STRTRAN(TTOC(u_datetime,3),"T"," ") WHERE id_kas in (select id from fd WHERE LOWER(pgm)='kas')
	ELSE
		SELECT tm_sk_fin.c_mena, tm_sk_fin.id_kas, tm_sk_fin.AUTOR, tm_sk_fin.uzaverka, tm_sk_fin.u_cis, SUM(tm_sk_plt.tip) AS tip, tm_sk_plt.druh_pl, SPACE(6) AS hpokoj,SPACE(15) AS hjmeno,tm_sk_fin.u_cas ;
			GROUP BY tm_sk_fin.AUTOR, tm_sk_fin.id_kas, tm_sk_fin.uzaverka, tm_sk_fin.u_cis, tm_sk_plt.druh_pl ;
        	FROM tm_sk_fin LEFT JOIN tm_sk_plt ON (tm_sk_fin.uzaverka=tm_sk_plt.uzaverka) .and. (tm_sk_fin.u_cis=tm_sk_plt.u_cis);
			INTO CURSOR buff READWRITE ORDER BY tm_sk_fin.AUTOR, tm_sk_fin.id_kas
	ENDIF 
	DELETE FOR tip=0
	UPDATE buff SET hpokoj=tm_sk_txt.hpokoj, hjmeno=tm_sk_txt.hjmeno from tm_sk_txt WHERE buff.uzaverka=tm_sk_txt.uzaverka AND buff.u_cis=tm_sk_txt.u_cis	

	IF !EMPTY(m.tip_kod)
		m.tipkalk=0
		IF m.issql
			PRIVATE m.hladajkod
			m.hladajkod=m.tip_kod
			SELECT 0
			USE (m.sqlprefix+"rkartykukodu") ALIAS kkod
			IF RECCOUNT()>0
				GO TOP 
				m.tipkalk=kkod.c_karty
			ENDIF 
			USE IN kkod
		ELSE 
			SELECT karty
			LOCATE FOR ALLTRIM(LOWER(kod))==ALLTRIM(LOWER(m.tip_kod))
			IF FOUND()
				m.tipkalk=karty.c_karty
			ENDIF 
		ENDIF 
		IF !EMPTY(m.tipkalk)
			SELECT id_kas, uzaverka, u_cis, prachy as  tip, c_mena, SPACE(3) as autor, SPACE(8) as druh_pl ;
                   FROM tm_sk_pol WHERE c_karty = m.tipkalk INTO CURSOR tipkalk1 READWRITE 
			IF RECCOUNT()>0
				UPDATE tipkalk1 SET autor=tm_sk_fin.autor from tm_sk_fin WHERE tipkalk1.uzaverka=tm_sk_fin.uzaverka AND tipkalk1.u_cis=tm_sk_fin.u_cis
				SELECT tm_sk_plt
                INDEX ON uzaverka+u_cis TAG uzuc
				SELECT tipkalk1
				SET  RELATION TO u_cis+uzaverka INTO tm_sk_plt
				REPLACE ALL druh_pl WITH tm_sk_plt.druh_pl

**                REPLACE ALL hjmeno WITH tm_sk_plt.text    &&&&& doresit data z puvodniho sk_txt !!!!!!!!!!!!!!!!!!!

				SET RELATION TO
				SELECT uzaverka, u_cis, id_kas, SUM(tip) as tip, autor, druh_pl FROM tipkalk1 INTO CURSOR tipkalk READWRITE ;
					GROUP BY autor, id_kas, uzaverka, u_cis, druh_pl
				USE IN tipkalk1
                SELECT tm_sk_plt
                DELE TAG uzuc
			ENDIF 
			SELECT buff 
			IF USED("tipkalk")
				INSERT INTO buff (id_kas, uzaverka, u_cis, tip, autor, druh_pl) SELECT id_kas, uzaverka, u_cis, tip, autor, druh_pl FROM tipkalk
				USE IN tipkalk
			ENDIF 
		ENDIF 
		SELECT buff
	ENDIF 

	trzcis_had.set_had(50)
	SELECT BUFF
	trzcis_had.HIDE()
	DO pr_report WITH "trz_i_tit", !m.reporty_volane_z_formu
	trzcis_had.SHOW()
	USE IN BUFF
ENDIF
DO pozavirej WITH "users"
RELEASE trzcis_had


*!*****************************************************************************
*!      Procedure: GR_TRZKAS
*!      Trzby podla pokladni
*!*****************************************************************************
PROCEDURE gr_trzkas
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
DO pozavirej WITH "buff"
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
SET assert on
ASSERT .f.
IF "1"$m.repriadok
	SELECT id_kas, dan, SUM(sumpc) AS prachy, SUM(sum_dph) AS sum_dph, id_prev, prevadzka, SUM(round50) as round50, ;
		SUM(sum_dph) AS pc_dan, SUM(sumdanzakl) AS dan_zakl ;
	    FROM buf_ucet INTO CURSOR buff ;
		GROUP BY id_kas, dan ORDER BY id_prev, id_kas, dan
	DO pr_report WITH "trz_i_kas_dph", !m.reporty_volane_z_formu
	USE IN BUFF
ENDIF 
IF "2"$m.repriadok
	DO pozavirej WITH "buff"
	IF !USED("users")
		IF m.issql
			rv("users","users")
		ELSE 
			IF FILE(m.gchom_path + "users.dbf")
				USE (m.gchom_path + "users") AGAIN IN 0
			ENDIF 
		ENDIF 
	ENDIF 

	SELECT id_kas, id_man, autor, druh, SUM(sumpc) as prachy, SUM(summc) as summc, SUM(sum_dph) as sum_dph, SUM(sumpc-sum_dph) as dan_zakl, id_prev, prevadzka, SUM(round50) as round50 FROM pri_buff GROUP BY id_kas, autor, id_man, druh ORDER BY id_prev, id_kas, autor, id_man, druh INTO CURSOR buff READWRITE 
	DO pr_report WITH "trz_i_kas_cas", !m.reporty_volane_z_formu

	USE IN buff 


ENDIF 

*!*****************************************************************************
*!      Procedure: M_DRK_PLAT
*!      Trzby podla druhov kalkulacii a druhov platieb
*!*****************************************************************************
PROCEDURE m_drk_plat
PARAMETERS repriadok
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
IF !USED("druhy_pl")
	SELECT 0
	IF m.issql 
		rv("druhy_pl","druhy_pl")
	ELSE 
		USE (m.gchom_path+"druhy_pl")
	ENDIF 
ELSE
	SELECT druhy_pl
ENDIF 
DO tvor_dph1
SELECT * , 0.00000 AS koef FROM dph1 INTO CURSOR dph11 READWRITE 
SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_pl, dan FROM dph1 INTO CURSOR dph111 READWRITE GROUP BY uzaverka, u_cis, dan
INDEX ON uzaverka+u_cis+STR(dan,5,2) TAG uzuc
SELECT dph11
SET RELATION TO uzaverka+u_cis+STR(dan,5,2) INTO dph111
REPLACE koef WITH 1 FOR dph111.cena_pl=0
REPLACE koef WITH dph11.cena_pl/dph111.cena_pl FOR dph111.cena_pl<>0
USE IN dph111
SELECT pri_buff.*,dph11.koef,dph11.druh_pl AS druh_plat FROM pri_buff, dph11 WHERE pri_buff.uzaverka=dph11.uzaverka .AND. pri_buff.u_cis=dph11.u_cis .AND. pri_buff.dan=dph11.dan;
	INTO CURSOR pribuff READWRITE 
SELECT * FROM pribuff WHERE koef<>1 INTO CURSOR pribuff1 READWRITE 
REPLACE ALL mnozstvi WITH mnozstvi*(koef)
REPLACE sumpc WITH sumpc*(koef) ALL
REPLACE sum_dph WITH sum_dph*(koef) ALL
SELECT pribuff
DELETE FOR koef<>1
INSERT INTO pribuff (id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
	sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat) ;
	SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
	sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat FROM pribuff1
SELECT id_kas, sumpc as prachy, druh_plat as druh_k, druh as druh, u_cis, uzaverka, sum_dph as sum_dph, round50,;
	(sumpc-sum_dph) as dan_zakl,dan FROM pribuff INTO CURSOR dph3 READWRITE 
DO pozavirej WITH "pribuff, pribuff1, dph11"
IF "1"$m.repriadok
	SELECT id_kas,SUM(prachy) AS prachy,druh_k,druh,u_cis,uzaverka,SUM(sum_dph) AS sum_dph,SUM(dan_zakl) AS dan_zakl, dan, SUM(round50) as round50 ;
		FROM dph3 GROUP BY id_kas,druh_k,uzaverka,druh,dan INTO CURSOR buff READWRITE ORDER BY druh_k
	SELECT BUFF
	REPLACE ALL prachy WITH ground(prachy,2)
	SELECT BUFF
	DO pozavirej WITH "vybery"
	SELECT DISTINCT druh_k, SPACE(m.sirkastlpcadruh) AS druh,.T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
	INDEX ON ALLTRIM(druh_k) TAG druh_k
	UPDATE vybery SET fiskal=.t. WHERE druh_k in (select nazev from druhy_pl WHERE cisfplat>0)
	REPLACE ALL druh WITH i_drupla(druh_k)
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF	
			DELETE FROM buff WHERE druh_k in (select druh_k FROM vybery WHERE EMPTY(lbl))
			DO pr_report WITH "trz_i_drkpl2", !m.reporty_volane_z_formu
		ENDIF
	ENDIF
	USE IN vybery
	USE IN BUFF
ENDIF
IF "2"$m.repriadok
	SELECT id_kas,SUM(prachy) AS prachy,druh_k,druh,u_cis,uzaverka,SUM(sum_dph) AS sum_dph,SUM(dan_zakl) AS dan_zakl, dan, SUM(round50) as round50 ;
		FROM dph3 GROUP BY druh_k,druh,dan INTO CURSOR buff READWRITE ORDER BY druh_k
	SELECT BUFF
	REPLACE ALL prachy WITH ground(prachy,2)
	SELECT BUFF
	SELECT DISTINCT druh_k, SPACE(m.sirkastlpcadruh) AS druh,.T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
	INDEX ON ALLTRIM(druh_k) TAG druh_k
	UPDATE vybery SET fiskal=.t. WHERE druh_k in (select nazev from druhy_pl WHERE cisfplat>0)
	REPLACE ALL druh WITH i_drupla(druh_k)
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			DELETE FROM buff WHERE druh_k in (select druh_k FROM vybery WHERE EMPTY(lbl))
			DO pr_report WITH "trz_i_drkplt", !m.reporty_volane_z_formu
		ENDIF
	ENDIF
	USE IN vybery
	USE IN BUFF
ENDIF
USE IN dph3


*!*****************************************************************************
*!      Procedure: GR_TRZMAN
*!      Trzby podla managerov
*!*****************************************************************************
PROCEDURE gr_trzman
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
*CASE barr = 1                    && Jednoduche
IF !USED("druhy_pl")
	SELECT 0
	IF m.issql 
		rv("druhy_pl","druhy_pl")
	ELSE 
		USE (m.gchom_path+"druhy_pl")
	ENDIF 
ELSE
	SELECT druhy_pl
ENDIF 
IF "1"$m.repriadok
	DO tvor_dph3 WITH .F.
	SELECT DISTINCT druh AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO  CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	INDEX ON druh_pl TAG druh_pl
	INDEX ON druh TAG druh
	REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_pl
		m.pl_txt="Platby: "
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			SCAN FOR !EMPTY(lbl)
				m.pl_txt=m.pl_txt+ALLTRIM(vybery.druh)+", "
			ENDSCAN 
		ELSE
			m.pl_txt=m.pl_txt+"Všetky"
		ENDIF 
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO druh INTO vybery
			DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT BUFF
			DO pr_report WITH "trz_i_man", !m.reporty_volane_z_formu
		ENDIF 
	ENDIF 
	SELECT SUM(prachy) AS prachy, id_man, dan, SUM(dan_zakl) AS dan_zakl, SUM(sum_dph) AS sum_dph, '' AS uzaverka, SUM(round50) as round50 FROM BUFF INTO CURSOR asddf GROUP BY id_man,dan
	DO pr_report WITH "trz_i_man3", !m.reporty_volane_z_formu
	DO pozavirej WITH "buff, asddf, dph3"
ENDIF
*CASE barr = 2                    && Podrobne (podla dni a druhov platby)
IF "2"$m.repriadok 
	PRIVATE m.str, m.oldprc
	DO tvor_dph3 WITH .T.
	SELECT DISTINCT druh AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	INDEX ON druh_pl TAG druh_pl
	INDEX ON druh TAG druh
	REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_pl
		m.pl_txt="Platby: "
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			SCAN FOR !EMPTY(lbl)
				m.pl_txt=m.pl_txt+ALLTRIM(vybery.druh)+", "
			ENDSCAN 
		ELSE
			m.pl_txt=m.pl_txt+"Všetky"
		ENDIF 
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO druh INTO vybery
			DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT BUFF
			DO pr_report WITH "trz_i_man2", !m.reporty_volane_z_formu
		ENDIF 
	ENDIF 
	SELECT SUM(prachy) AS prachy, id_man, dan, SUM(dan_zakl) AS dan_zakl, SUM(sum_dph) AS sum_dph, uzaverka, SUM(round50) as round50 FROM BUFF INTO CURSOR asddf GROUP BY id_man,uzaverka,dan
	DO pr_report WITH "trz_i_man3@2", !m.reporty_volane_z_formu
	m.view_mssg=''
	DO pozavirej WITH "buff, asddf, dph3"
ENDIF
IF "3"$m.repriadok
	PRIVATE m.str, m.oldprc
	DO tvor_dph3 WITH .T.
	SELECT buff.*, DATE() as datum FROM buff INTO CURSOR buffx READWRITE 
	UPDATE buffx SET datum=tm_sk_sez_kas.uct_den from tm_sk_sez_kas WHERE buffx.uzaverka=tm_sk_sez_kas.uzaverka
	USE IN buff
	SELECT "" as id_kas,SUM(prachy) AS prachy,id_man,druh,u_cis,DTOC(datum) as uzaverka, datum, SUM(sum_dph) AS sum_dph,SUM(dan_zakl) AS dan_zakl,dan, SUM(round50) as round50 ;
		FROM buffx GROUP BY id_man,druh,dan ORDER BY id_man, datum, druh INTO CURSOR buff READWRITE 
	SELECT DISTINCT druh AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl, .f. as fiskal FROM buffx INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	INDEX ON druh_pl TAG druh_pl
	INDEX ON druh TAG druh
	REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_pl
		m.pl_txt="Platby: "
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			SCAN FOR !EMPTY(lbl)
				m.pl_txt=m.pl_txt+ALLTRIM(vybery.druh)+", "
			ENDSCAN 
		ELSE
			m.pl_txt=m.pl_txt+"Všetky"
		ENDIF 
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO druh INTO vybery
			DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT BUFF
			DO pr_report WITH "trz_i_man2", !m.reporty_volane_z_formu
		ENDIF 
	ENDIF 
	SELECT SUM(prachy) AS prachy, id_man, dan, SUM(dan_zakl) AS dan_zakl, SUM(sum_dph) AS sum_dph, uzaverka, datum, SUM(round50) as round50 FROM BUFF INTO CURSOR asddf GROUP BY id_man,uzaverka,dan ORDER BY id_man, datum
	DO pr_report WITH "trz_i_man3", !m.reporty_volane_z_formu
	m.view_mssg=''
	DO pozavirej WITH "buff, asddf, dph3"
ENDIF 
IF "4"$m.repriadok 
	PRIVATE m.str, m.oldprc
	DO tvor_dph3 WITH .T.
	SELECT DISTINCT druh AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	INDEX ON druh_pl TAG druh_pl
	INDEX ON druh TAG druh
	REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_pl
		m.pl_txt="Platby: "
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			SCAN FOR !EMPTY(lbl)
				m.pl_txt=m.pl_txt+ALLTRIM(vybery.druh)+", "
			ENDSCAN 
		ELSE
			m.pl_txt=m.pl_txt+"Všetky"
		ENDIF 
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO druh INTO vybery
			DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT BUFF
			DO pr_report WITH "trz_i_man24", !m.reporty_volane_z_formu
		ENDIF 
	ENDIF 
	SELECT SUM(prachy) AS prachy, id_man, SUM(dan_zakl) AS dan_zakl, SUM(sum_dph) AS sum_dph, uzaverka, SUM(round50) as round50 FROM BUFF INTO CURSOR asddf GROUP BY id_man,uzaverka
	DO pr_report WITH "trz_i_man4", !m.reporty_volane_z_formu
	m.view_mssg=''
	DO pozavirej WITH "buff, asddf, dph3"
ENDIF
IF "5"$m.repriadok 
	PRIVATE m.str, m.oldprc
	DO tvor_dph3 WITH .T.
	SELECT *, {..} as datum FROM buff INTO CURSOR buffx READWRITE 
	UPDATE buffx SET datum=tm_sk_sez_kas.uct_den from tm_sk_sez_kas WHERE buffx.uzaverka=tm_sk_sez_kas.uzaverka
	UPDATE buffx SET uzaverka=DTOC(datum) WHERE .t.
	USE IN buff
	SELECT SUM(prachy) AS prachy, id_man, SUM(dan_zakl) AS dan_zakl, SUM(sum_dph) AS sum_dph, uzaverka, datum, druh, SUM(round50) as round50 FROM BUFFx INTO CURSOR buff GROUP BY id_man, uzaverka, druh READWRITE 
	INDEX on id_man+DTOS(datum)+druh TAG aaa
	USE IN buffx
	SELECT DISTINCT druh AS druh_pl,SPACE(m.sirkastlpcadruh) AS druh, .T. AS lbl, .f. as fiskal FROM BUFF INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	INDEX ON druh_pl TAG druh_pl
	INDEX ON druh TAG druh
	REPLACE ALL vybery.druh WITH i_drupla(vybery.druh_pl)
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_pl
		m.pl_txt="Platby: "
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			SCAN FOR !EMPTY(lbl)
				m.pl_txt=m.pl_txt+ALLTRIM(vybery.druh)+", "
			ENDSCAN 
		ELSE
			m.pl_txt=m.pl_txt+"Všetky"
		ENDIF 
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO druh INTO vybery
			DELETE FOR druh=vybery.druh_pl .AND. EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT BUFF
			DO pr_report WITH "trz_i_man24", !m.reporty_volane_z_formu
		ENDIF 
	ENDIF 
	SELECT SUM(prachy) AS prachy, id_man, SUM(dan_zakl) AS dan_zakl, SUM(sum_dph) AS sum_dph, uzaverka, datum, SUM(round50) as round50 FROM BUFF INTO CURSOR asddf GROUP BY id_man,uzaverka
	INDEX on id_man+DTOS(datum) TAG aaa
	DO pr_report WITH "trz_i_man4", !m.reporty_volane_z_formu
	m.view_mssg=''
	DO pozavirej WITH "buff, asddf, dph3"
ENDIF

*!*****************************************************************************
*!      Procedure: GR_TRZHLA
*!      Trzby podla cenovych hladin
*!*****************************************************************************
PROCEDURE gr_trzhla
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
DO pozavirej WITH "buff"
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
IF "1"$repriadok
	SELECT cen_hlad, uzaverka, SUM(sumpc) AS prachy, id_kas, u_cis, c_mena, SUM(round50) as round50 ;
		FROM pri_buff INTO CURSOR bufff GROUP BY id_kas, u_cis, cen_hlad, uzaverka ORDER BY id_kas, cen_hlad
* prachy jsou uz po sleve, takze odpada korekce z fin.cena_slev/cena_celk
	SELECT id_kas, cen_hlad, SUM(prachy) AS prachy, SUM(round50) as round50 FROM bufff ;
		INTO CURSOR buff READWRITE GROUP BY id_kas, cen_hlad
	USE IN bufff
	SELECT BUFF
	DO pr_report WITH "trz_i_kas_hlad", !m.reporty_volane_z_formu
	USE IN BUFF
ENDIF
IF "2"$repriadok
	DO s_mes
ENDIF
IF "3"$m.repriadok
	DO pozavirej WITH "buff"
	DO tvor_dph2 IN sk_stati
	SELECT BUFF
	SELECT SUM(prachy) as prachy, cen_hlad,id_kas,"" as uzaverka,dan,SUM(dan_zakl) as dan_zakl,SUM(sum_dph) as sum_dph, SUM(round50) as round50 ;
		FROM buff INTO CURSOR buffx READWRITE GROUP BY id_kas,uzaverka,cen_hlad,dan ORDER BY id_kas, uzaverka,cen_hlad,dan
	DO pr_report WITH "trz_i_hlad_dph", !m.reporty_volane_z_formu
	DO pozavirej WITH "buff,buffx"
ENDIF
IF "4"$repriadok
*!*		SELECT tm_sk_pol.cen_hlad, tm_sk_pol.uzaverka, SUM(tm_sk_pol.prachy) AS prachy, tm_sk_pol.id_kas, tm_sk_pol.u_cis, tm_sk_fin.c_mena, tm_sk_fin.autor ;
*!*			FROM tm_sk_pol LEFT JOIN tm_sk_fin ON tm_sk_pol.uzaverka=tm_sk_fin.uzaverka AND tm_sk_pol.u_cis=tm_sk_fin.u_cis ;
*!*	        INTO CURSOR bufff WHERE !EMPTY(k_odpoctu);
*!*			GROUP BY tm_sk_pol.id_kas, tm_sk_fin.autor, tm_sk_pol.cen_hlad, tm_sk_pol.uzaverka;
*!*			ORDER BY tm_sk_pol.id_kas, tm_sk_pol.uzaverka, tm_sk_pol.cen_hlad, tm_sk_fin.autor
	SELECT cen_hlad, uzaverka, SUM(sumpc) AS prachy, id_kas, u_cis, c_mena, autor, SUM(round50) as round50 ;
		FROM pri_buff INTO CURSOR bufff GROUP BY id_kas, autor, cen_hlad, uzaverka ORDER BY id_kas, uzaverka, cen_hlad, autor
	SELECT * FROM bufff INTO CURSOR buff
	DO pr_report WITH "trz_i_kas_hlad_cas" , !m.reporty_volane_z_formu
* prachy jsou uz po sleve, takze odpada korekce z fin.cena_slev/cena_celk
	SELECT id_kas, cen_hlad, SUM(prachy) AS prachy, autor, "" as uzaverka, SUM(round50) as round50 FROM bufff ;
		INTO CURSOR buff READWRITE GROUP BY id_kas, autor, cen_hlad
	USE IN bufff
	SELECT BUFF
	DO pr_report WITH "trz_i_kas_hlad_cas@b" , !m.reporty_volane_z_formu
	USE IN BUFF
ENDIF

*!*****************************************************************************
*!      Procedure: TRZ_UZAV
*!      Trzby podla uzavierok
*!*****************************************************************************
PROCEDURE trz_uzav
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
DO pozavirej WITH "buff"
IF "1"$m.repriadok
	SELECT uzaverka, SUM(sumpc) AS prachy, id_kas, dan, SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
		SUM(sumdanzakl) AS dan_zakl FROM "buf_ucet" INTO CURSOR BUFF ;
		GROUP BY uzaverka, id_kas, dan;
		ORDER BY id_kas, uzaverka, dan
	DO pr_report WITH "trz_i_uzav", !m.reporty_volane_z_formu
	DO pozavirej WITH "buff"
ENDIF 
IF "2"$m.repriadok
	PUBLIC pocetdni
	SELECT uzaverka, SUM(sumpc) AS prachy, id_kas, dan, SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
		SUM(sumdanzakl) AS dan_zakl, DATE() as datum FROM "buf_ucet" INTO CURSOR buffx ;
		GROUP BY uzaverka, id_kas, dan;
		ORDER BY id_kas, uzaverka, dan READWRITE 
		UPDATE buffx SET datum=tm_sk_sez_kas.uct_den from tm_sk_sez_kas WHERE buffx.uzaverka=tm_sk_sez_kas.uzaverka
	SELECT SUM(prachy) AS prachy, id_kas, dan, SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
		SUM(dan_zakl) AS dan_zakl, datum FROM buffx INTO CURSOR buff;
		GROUP BY datum, id_kas, dan;
		ORDER BY datum, id_kas, dan	
	SELECT DISTINCT datum FROM buff INTO CURSOR pocdni
	m.pocetdni=RECCOUNT("pocdni")
	USE IN pocdni
	SELECT buff 
	DO pr_report WITH "trz_i_uzav_dni", !m.reporty_volane_z_formu
	DO pozavirej WITH "buff,buffx"
	RELEASE m.pocetdni
ENDIF 
IF "3"$m.repriadok
	SELECT distinct STR(dan*100-100,10,0)+"%" as dan FROM buf_ucet INTO CURSOR vyberdane READWRITE 
	INSERT INTO vyberdane (dan) VALUES (jaz("rep_spolu"))
	m.druh=""
	DO FORM dajdruh WITH "","vyberdane","dan",200 TO m.druh
	IF EMPTY(m.druh) or m.druh=jaz("rep_spolu")
		SELECT uzaverka, SUM(sumpc) AS prachy, id_kas, -1 as dan , SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
			SUM(sumdanzakl) AS dan_zakl FROM "buf_ucet" INTO CURSOR BUFF ;
			GROUP BY uzaverka, id_kas;
			ORDER BY id_kas, uzaverka
	ELSE 
		SELECT uzaverka, SUM(sumpc) AS prachy, id_kas, dan , SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
			SUM(sumdanzakl) AS dan_zakl FROM "buf_ucet" INTO CURSOR BUFF WHERE STR(dan*100-100,10,0)+"%"=m.druh;
			GROUP BY uzaverka, id_kas;
			ORDER BY id_kas, uzaverka
	ENDIF 
	DO pr_report WITH "trz_i_uzav", !m.reporty_volane_z_formu
	DO pozavirej WITH "buff"
ENDIF 



*!*****************************************************************************
*!      Procedure: GR_POCKALK
*!      Pocet predanych jedal
*!*****************************************************************************
PROCEDURE gr_pockalk
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
DO pozavirej WITH "buff, vybery"
m.cen_hlad="ZZ"           &&&& co se do toho soupne?
IF "1"$m.repriadok .OR. "2"$m.repriadok .OR. "3"$m.repriadok
	IF !m.reporty_volane_z_formu OR MESSAGEBOX(jaz("s_sk_po_kasach")+"?",4+32,"FoodMan")=6
		SELECT c_karty, nazev AS zkratka, SUM(summc) AS summc, SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
			SUM(sumpc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, id_kas, id_man, druh, cen_hlad, spart, k_odpoctu;
			FROM pri_buff INTO CURSOR buff READWRITE ;
			GROUP BY id_kas, id_man, zkratka, dan, cen_hlad;
			ORDER BY id_kas, druh, zkratka, id_man, dan WHERE c_karty<>-99
	ELSE 
		SELECT c_karty, nazev AS zkratka, SUM(summc) AS summc, SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
			SUM(sumpc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, id_man as id_kas, "  " as id_man, druh, cen_hlad, spart, k_odpoctu;
			FROM pri_buff INTO CURSOR buff READWRITE ;
			GROUP BY id_kas, id_man, zkratka, dan, cen_hlad;
			ORDER BY id_kas, druh, zkratka, id_man, dan WHERE c_karty<>-99
	ENDIF 
	SELECT DISTINCT druh, .T. AS lbl,.f. as kalkulacia FROM BUFF INTO CURSOR vybery READWRITE
	UPDATE vybery SET kalkulacia=!EMPTY(skupiny.typ_kalk) from skupiny WHERE vybery.druh=skupiny.druh 
	INDEX ON druh TAG druh
	
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery" TO m.druhxx
			filtrujpriznaky("buff")
		ENDIF 
		RELEASE m.gcfilter_triedenia
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO druh INTO vybery
			DELETE FOR druh=vybery.druh .AND. EMPTY(vybery.lbl)
			SELECT DISTINCT zkratka as druh, .T. AS lbl FROM BUFF INTO CURSOR vybery READWRITE 
			INDEX ON druh TAG druh
			IF RECCOUNT()>0
				IF m.reporty_volane_z_formu
					DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
				ENDIF 
				SELECT vybery
				LOCATE FOR !EMPTY(lbl)
				IF FOUND()
					SELECT BUFF
					SET RELATION TO zkratka INTO vybery
					DELETE FOR zkratka=vybery.druh .AND. EMPTY(vybery.lbl)
					IF "1"$m.repriadok
						m.txthlavicka="Po kasách"
						DO pr_report WITH "trz_i_Pockal@kas", !m.reporty_volane_z_formu
						SELECT * FROM buff INTO CURSOR buffx
						SELECT zkratka, SUM(summc) AS summc,SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
							SUM(pc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, "" as id_kas,id_man, druh, cen_hlad, k_odpoctu;
							FROM buff INTO CURSOR buff1;
							GROUP BY id_man, zkratka, dan, cen_hlad;
							ORDER BY druh, zkratka, id_man, cen_hlad
						SELECT * FROM buff1 INTO CURSOR buff READWRITE 
						USE IN buff1
						INDEX on druh+zkratka+ STR(dan,5,2) TAG aaa
						m.txthlavicka="Všetky kasy spolu"
						DO pr_report WITH "trz_i_Pockal", !m.reporty_volane_z_formu
						USE IN buff
						SELECT * FROM buffx INTO CURSOR buff READWRITE 
						USE IN buffx
						SELECT buff
					ENDIF
					IF "2"$m.repriadok
						m.txthlavicka="Po kasách a predajných cenách"
						INDEX ON id_kas+STR(pc,13,3) TAG aaa
						DO pr_report WITH "trz_i_Pockak", !m.reporty_volane_z_formu
					ENDIF
					IF "3"$m.repriadok
						SELECT zkratka, SUM(summc) AS summc, SUM(sum_dph) AS sum_dph,;
							SUM(pc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, id_kas, id_man, druh, cen_hlad, spart, SUM(round50) as round50;
							FROM buff INTO CURSOR buffx READWRITE ;
							GROUP BY id_kas, id_man, zkratka, dan;
							ORDER BY id_kas, druh, zkratka, id_man, dan 
						USE IN buff
						SELECT * FROM buffx INTO CURSOR buff READWRITE 
						USE IN buffx
						m.txthlavicka="Po kasách a množstvách"
						INDEX ON id_kas+STR(mnozstvi,13,3) TAG aaa
						DO pr_report WITH "trz_i_Pockak@mnoz", !m.reporty_volane_z_formu
					ENDIF
				ENDIF 
			ENDIF 
		ENDIF 
	ENDIF 
	USE IN vybery
ENDIF 

IF "4"$m.repriadok
	DO pozavirej WITH "buff"
	SELECT nazev AS zkratka, SUM(summc) AS summc,SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
			SUM(sumpc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, id_kas, AUTOR, druh, cen_hlad, spart;
			FROM pri_buff INTO CURSOR buffx READWRITE;
			GROUP BY AUTOR, zkratka, dan;
			ORDER BY AUTOR, druh, zkratka, dan WHERE c_karty<>-99 &&cen_hlad=m.cen_hlad
	SELECT distinct autor, SPACE(25) as druh, .t. as lbl FROM buffx INTO CURSOR casnici READWRITE 
	IF RECCOUNT()>0
		REPLACE ALL druh WITH i_mencis("01",autor)
		DO WHILE .t. 
			IF m.reporty_volane_z_formu
				DO FORM seldruhy WITH "casnici","meno",200 TO m.druhxx
			ENDIF 
			SELECT casnici
			LOCATE FOR !EMPTY(lbl)
			IF FOUND()
				SELECT buffx.* FROM buffx LEFT JOIN casnici ON buffx.autor=casnici.autor INTO CURSOR buff WHERE !EMPTY(casnici.lbl) READWRITE 
				INDEX ON AUTOR+druh+zkratka+STR(dan,4,2) TAG aaa			
				DO pr_report WITH "trz_i_Pockac", !m.reporty_volane_z_formu
				USE IN buff
				IF m.reporty_volane_z_formu
					EXIT 
				ENDIF 
			ELSE
				EXIT 
			ENDIF 
		ENDDO 
	ENDIF
	DO pozavirej WITH "buffx, casnici"
ENDIF

IF "5"$m.repriadok
	SELECT nazev AS zkratka, uzaverka, SUM(summc) AS summc,SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
			SUM(sumpc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, id_kas, id_man, druh, cen_hlad;
			FROM pri_buff INTO CURSOR buff READWRITE;
			GROUP BY uzaverka, id_kas, id_man,zkratka, dan, cen_hlad;
			ORDER BY id_kas,uzaverka,druh,zkratka,id_man,dan WHERE c_karty<>-99
	SELECT DISTINCT druh, .T. AS lbl,.f. as kalkulacia FROM BUFF INTO CURSOR vybery READWRITE
	UPDATE vybery SET kalkulacia=!EMPTY(skupiny.typ_kalk) from skupiny WHERE vybery.druh=skupiny.druh 
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery" TO m.druhxx
		ENDIF 
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO druh INTO vybery
			DELETE FOR druh=vybery.druh .AND. EMPTY(vybery.lbl)
		ENDIF 
		filtrujpriznaky("buff")
		RELEASE m.gcfilter_triedenia
	ENDIF 
	SELECT buff
	DO pr_report WITH "trz_i_Pockau", !m.reporty_volane_z_formu
ENDIF 
DO pozavirej WITH "buff"

*!*****************************************************************************
*!      Procedure: PR_ROZPE
*!*****************************************************************************
PROCEDURE pr_rozpe
PARAMETER barr, repriadok
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
IF !USED("obr_sum")
	DO tvor_obrsum
ENDIF 
DO CASE
	CASE barr = 5              && podla pokladni
		SELECT SUM(mnozstvi*cena) AS spotreba, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas,;
			id_man FROM obr_sum INTO CURSOR obr_ssum READWRITE GROUP BY id_kas
        INDEX ON id_kas TAG id_kas
	CASE barr = 6              && podla pokladni a managerov
		SELECT SUM(mnozstvi*cena) AS spotreba, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas,;
			id_man FROM obr_sum INTO CURSOR obr_ssum READWRITE GROUP BY id_kas, id_man
        INDEX ON id_man+id_kas TAG mankas
	CASE barr = 7              && podla cenovych hladin a pokladni
		IF "1"$repriadok
			SELECT SUM(mnozstvi*cena) AS spotreba, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas, cen_hlad, id_man;
				FROM obr_sum INTO CURSOR obr_ssum READWRITE GROUP BY id_kas, cen_hlad
			INDEX on id_kas+cen_hlad TAG kashl
		ENDIF 
	CASE barr = 8              && limitov
		IF "1"$repriadok
			SELECT SUM(mnozstvi*cena) AS spotreba, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas,;
				id_man,cen_hlad FROM obr_sum INTO CURSOR obr_ssum READWRITE GROUP BY id_kas, id_man,cen_hlad,uzaverka
			INDEX ON id_man+id_kas+uzaverka+ALLTRIM(cen_hlad) TAG mankashluz
		ENDIF 
	CASE barr = 1
		IF "1"$repriadok
			SELECT SUM(mnozstvi*cena) AS spotreba, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas,;
				id_man FROM obr_sum INTO CURSOR obr_ssum READWRITE GROUP BY id_man,id_kas, uzaverka
	        INDEX ON id_man+id_kas+uzaverka TAG mankasuz
		ENDIF 
		IF "2"$repriadok
			SELECT SUM(mnozstvi*cena) AS spotreba, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas,;
				id_man, cen_hlad FROM obr_sum INTO CURSOR obr_ssum12 READWRITE GROUP BY id_man,id_kas,cen_hlad, uzaverka
		ENDIF 
	OTHERWISE
		SELECT SUM(mnozstvi*cena) AS spotreba, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas,;
			id_man FROM obr_sum INTO CURSOR obr_ssum READWRITE GROUP BY id_man,id_kas, uzaverka
        INDEX ON id_man+id_kas+uzaverka TAG mankasuz
ENDCASE
*SELECT obr_ssum
*!*	IF m.issql
*!*		REPLACE ALL spotreba WITH -spotreba
*!*	ENDIF 
* metoda 2 (select z dvoch databazi a relaciou doplnena sadzba DPH)
DO pozavirej WITH "buff"
DO CASE
	CASE barr = 1 
		IF "1"$repriadok
			SELECT id_kas, id_man, uzaverka, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
				SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl, davka, SUM(round50) as round50;
				FROM buf_ucet INTO CURSOR buff READWRITE;
				GROUP BY id_man, id_kas, uzaverka ORDER BY id_man,id_kas,uzaverka
		ENDIF 
		IF "2"$repriadok
			SELECT id_kas, id_man, uzaverka, cen_hlad, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
				SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl, davka, $0 as spotreba, 0000 as pocet_h, SUM(round50) as round50;
				FROM buf_ucet INTO CURSOR buff12 READWRITE;
				GROUP BY id_man, id_kas, cen_hlad, uzaverka ORDER BY id_man,id_kas,cen_hlad,uzaverka
			SELECT distinct id_kas, uzaverka, u_cis, cen_hlad FROM tm_sk_pol INTO CURSOR chl
			SELECT tm_sk_fin.id_kas, tm_sk_fin.uzaverka, SUM(tm_sk_fin.pocet_h) as pocet_h, chl.cen_hlad FROM tm_sk_fin INNER JOIN chl ON tm_sk_fin.id_kas=chl.id_kas AND tm_sk_fin.uzaverka=chl.uzaverka AND tm_sk_fin.u_cis=chl.u_cis INTO CURSOR pocethchl GROUP BY tm_sk_fin.id_kas, tm_sk_fin.uzaverka, chl.cen_hlad
			SELECT id_kas, uzaverka, cen_hlad, SUM(pocet_h) as pocet_h FROM pocethchl GROUP BY id_kas, uzaverka, cen_hlad INTO CURSOR pocchl
			UPDATE buff12 SET pocet_h=pocchl.pocet_h from pocchl WHERE buff12.id_kas=pocchl.id_kas AND buff12.uzaverka=pocchl.uzaverka AND buff12.cen_hlad=pocchl.cen_hlad
			UPDATE buff12 SET pocet_h=1 WHERE pocet_h=0
			IF USED("buff12")
				SELECT buff12
				REPLACE ALL sumpc WITH ROUND(sumpc,2), summc WITH ROUND(summc,2), sum_dph WITH ROUND(sum_dph,2), sumpc_dan WITH ROUND(sumpc_dan,2)
				UPDATE buff12 SET spotreba=obr_ssum12.spotreba from obr_ssum12 WHERE buff12.id_kas=obr_ssum12.id_kas AND buff12.id_man=obr_ssum12.id_man AND buff12.cen_hlad=obr_ssum12.cen_hlad AND SUBSTR(buff12.uzaverka,3,12)=obr_ssum12.uzaverka
			ENDIF 
			DO pozavirej WITH "pocchl, pocethchl, chl"
		ENDIF 
	CASE barr = 2
		SELECT id_kas, id_man, uzaverka, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
			SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl, davka, SUM(round50) as round50;
			FROM buf_ucet INTO CURSOR buff READWRITE;
			GROUP BY id_man, id_kas, uzaverka ORDER BY id_man,id_kas,uzaverka
	CASE barr = 9
		IF "1"$repriadok
			SELECT id_kas,  uzaverka, u_cis, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
				SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl,SUM(sumpc) AS cena_pl, "  " as id_man, SUM(round50) as round50;
				FROM buf_ucet INTO CURSOR buff READWRITE;
				GROUP BY id_kas, uzaverka, u_cis ORDER BY id_kas,uzaverka, u_cis
			m.druh=""
			SELECT DISTINCT druh_pl, SPACE(20) AS druhpl FROM tm_sk_plt INTO CURSOR druhy READWRITE 
			REPLACE ALL druhpl WITH i_drupla(druh_pl)
			IF m.reporty_volane_z_formu
				DO FORM dajdruh WITH "","druhy","druhpl",200 TO m.druh
			ENDIF 
			IF !EMPTY(m.druh)
				SELECT druhy
				LOCATE FOR druhpl=m.druh
				m.druh=druh_pl

				SELECT tm_sk_plt 
				INDEX ON u_cis+uzaverka TAG uzuc FOR druh_pl = m.druh
				SELECT BUFF
				SET RELATION TO u_cis+uzaverka INTO tm_sk_plt
				REPLACE cena_pl WITH tm_sk_plt.cena_pl FOR !EMPTY(tm_sk_plt.u_cis)
				DELETE FOR EMPTY(tm_sk_plt.u_cis)
				SET RELATION TO

	            SELECT tm_sk_plt
				SET ORDER TO

				SELECT BUFF
				REPLACE summc WITH summc*cena_pl/sumpc,sum_dph WITH sum_dph*cena_pl/sumpc,;
					sumpc_dan WITH sumpc_dan*cena_pl/sumpc, dan_zakl WITH dan_zakl*cena_pl/sumpc FOR sumpc<>cena_pl .AND. sumpc<>0
				REPLACE sumpc WITH cena_pl ALL
			ENDIF
	        SELECT buff
		ENDIF 
		IF "2"$repriadok
			SELECT id_kas,  uzaverka, u_cis, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
				SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl,SUM(sumpc) AS cena_pl, "  " as id_man, "A" as hlavicka, SPACE(30) as platba, SPACE(30) as nazev, $0 as mnozstvi, $0 as prachy,0.00 as dan, 000000000000 as c_karty, SUM(round50) as round50;
				FROM buf_ucet INTO CURSOR buffdet READWRITE;
				GROUP BY id_kas, uzaverka, u_cis ORDER BY id_kas,uzaverka, u_cis
			SELECT id_kas, uzaverka, u_cis FROM buffdet INTO CURSOR aaa
			INSERT INTO buffdet (id_kas, uzaverka, u_cis, c_karty, mnozstvi, prachy, dan, hlavicka, id_man) SELECT aaa.id_kas, aaa.uzaverka, aaa.u_cis, tm_sk_pol.c_karty, tm_sk_pol.mnozstvi, tm_sk_pol.prachy, tm_sk_pol.dan_sazba,"B" as hlavicka, tm_sk_pol.id_man FROM aaa INNER JOIN tm_sk_pol on aaa.uzaverka=tm_sk_pol.uzaverka AND aaa.u_cis=tm_sk_pol.u_cis
			UPDATE buffdet SET nazev=karty.nazev from karty WHERE buffdet.c_karty=karty.c_karty AND buffdet.hlavicka="B"
			INSERT INTO buffdet (id_kas, uzaverka, u_cis, cena_pl, platba, hlavicka) SELECT aaa.id_kas, aaa.uzaverka, aaa.u_cis, tm_sk_plt.cena_pl, tm_sk_plt.druh_pl, "C" as hlavicka FROM aaa INNER JOIN tm_sk_plt on aaa.uzaverka=tm_sk_plt.uzaverka AND aaa.u_cis=tm_sk_plt.u_cis 
			USE IN aaa
			SELECT buffdet
			REPLACE platba WITH i_drupla(ALLTRIM(platba)) FOR hlavicka="C"
		ENDIF 
		IF "3"$repriadok
			m.hlavicka_reportu=""
			DO pozavirej WITH "hodiny"
			SELECT distinct LEFT(pl_cas,2) as hodina, .t. as lbl FROM buf_ucet INTO CURSOR hodiny READWRITE WHERE !ISNULL(pl_cas) AND !EMPTY(pl_cas) 
			IF RECCOUNT("hodiny")>0
				DO FORM selpartn WITH "hodiny","hodina"
				SELECT hodiny
				LOCATE FOR EMPTY(lbl)
				IF FOUND()
					GO TOP 
					SCAN FOR lbl
						IF EMPTY(m.hlavicka_reportu)
							m.hlavicka_reportu=hodiny.hodina
						ELSE
							m.hlavicka_reportu=m.hlavicka_reportu+","+hodiny.hodina
						ENDIF 
					ENDSCAN 
				ELSE 
					m.hlavicka_reportu=""
				ENDIF 
			ELSE 
				m.hlavicka_reportu=""
			ENDIF 
			IF USED("hodiny")
				SELECT id_kas,  uzaverka, u_cis, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
					SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl,SUM(sumpc) AS cena_pl, "  " as id_man, SUM(round50) as round50;
					FROM buf_ucet INTO CURSOR buff READWRITE WHERE LEFT(pl_cas,2) IN (SELECT hodina FROM hodiny WHERE lbl);
					GROUP BY id_kas, uzaverka, u_cis ORDER BY id_kas,uzaverka, u_cis
			ELSE
				SELECT id_kas,  uzaverka, u_cis, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
					SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl,SUM(sumpc) AS cena_pl, "  " as id_man, SUM(round50) as round50;
					FROM buf_ucet INTO CURSOR buff READWRITE;
					GROUP BY id_kas, uzaverka, u_cis ORDER BY id_kas,uzaverka, u_cis
			ENDIF 
			DO pozavirej WITH "hodiny"
			m.druh=""
			SELECT DISTINCT druh_pl, SPACE(20) AS druhpl FROM tm_sk_plt INTO CURSOR druhy READWRITE 
			REPLACE ALL druhpl WITH i_drupla(druh_pl)
			IF m.reporty_volane_z_formu
				DO FORM dajdruh WITH "","druhy","druhpl",200 TO m.druh
			ENDIF 
			IF !EMPTY(m.druh)
				SELECT druhy
				LOCATE FOR druhpl=m.druh
				m.druh=druh_pl

				SELECT tm_sk_plt 
				INDEX ON u_cis+uzaverka TAG uzuc FOR druh_pl = m.druh
				SELECT BUFF
				SET RELATION TO u_cis+uzaverka INTO tm_sk_plt
				REPLACE cena_pl WITH tm_sk_plt.cena_pl FOR !EMPTY(tm_sk_plt.u_cis)
				DELETE FOR EMPTY(tm_sk_plt.u_cis)
				SET RELATION TO

	            SELECT tm_sk_plt
				SET ORDER TO

				SELECT BUFF
				REPLACE summc WITH summc*cena_pl/sumpc,sum_dph WITH sum_dph*cena_pl/sumpc,;
					sumpc_dan WITH sumpc_dan*cena_pl/sumpc, dan_zakl WITH dan_zakl*cena_pl/sumpc FOR sumpc<>cena_pl .AND. sumpc<>0
				REPLACE sumpc WITH cena_pl ALL
			ENDIF
	        SELECT buff
		ENDIF 
		IF "4"$repriadok
			m.hlavicka_reportu=""
			DO pozavirej WITH "hodiny"
			SELECT distinct LEFT(pl_cas,2) as hodina, .t. as lbl FROM buf_ucet INTO CURSOR hodiny READWRITE WHERE !ISNULL(pl_cas) AND !EMPTY(pl_cas) 
			IF RECCOUNT("hodiny")>0
				DO FORM selpartn WITH "hodiny","hodina"
				SELECT hodiny
				LOCATE FOR EMPTY(lbl)
				IF FOUND()
					GO TOP 
					SCAN FOR lbl
						IF EMPTY(m.hlavicka_reportu)
							m.hlavicka_reportu=hodiny.hodina
						ELSE
							m.hlavicka_reportu=m.hlavicka_reportu+","+hodiny.hodina
						ENDIF 
					ENDSCAN 
				ELSE 
					m.hlavicka_reportu=""
				ENDIF 
			ELSE 
				m.hlavicka_reportu=""
			ENDIF 
			IF USED("hodiny")
				SELECT id_kas,  uzaverka, u_cis, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
					SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl,SUM(sumpc) AS cena_pl, "  " as id_man, "A" as hlavicka, SPACE(30) as platba, SPACE(30) as nazev, $0 as mnozstvi, $0 as prachy,0.00 as dan, 000000000000 as c_karty, SUM(round50) as round50;
					FROM buf_ucet INTO CURSOR buffdet READWRITE WHERE LEFT(pl_cas,2) IN (SELECT hodina FROM hodiny WHERE lbl);
					GROUP BY id_kas, uzaverka, u_cis ORDER BY id_kas,uzaverka, u_cis
			ELSE
				SELECT id_kas,  uzaverka, u_cis, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
					SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl,SUM(sumpc) AS cena_pl, "  " as id_man, "A" as hlavicka, SPACE(30) as platba, SPACE(30) as nazev, $0 as mnozstvi, $0 as prachy,0.00 as dan, 000000000000 as c_karty, SUM(round50) as round50;
					FROM buf_ucet INTO CURSOR buffdet READWRITE;
					GROUP BY id_kas, uzaverka, u_cis ORDER BY id_kas,uzaverka, u_cis
			ENDIF 
			SELECT id_kas, uzaverka, u_cis FROM buffdet INTO CURSOR aaa
			INSERT INTO buffdet (id_kas, uzaverka, u_cis, c_karty, mnozstvi, prachy, dan, hlavicka, id_man) SELECT aaa.id_kas, aaa.uzaverka, aaa.u_cis, tm_sk_pol.c_karty, tm_sk_pol.mnozstvi, tm_sk_pol.prachy, tm_sk_pol.dan_sazba,"B" as hlavicka, tm_sk_pol.id_man FROM aaa INNER JOIN tm_sk_pol on aaa.uzaverka=tm_sk_pol.uzaverka AND aaa.u_cis=tm_sk_pol.u_cis
			UPDATE buffdet SET nazev=karty.nazev from karty WHERE buffdet.c_karty=karty.c_karty AND buffdet.hlavicka="B"
			INSERT INTO buffdet (id_kas, uzaverka, u_cis, cena_pl, platba, hlavicka) SELECT aaa.id_kas, aaa.uzaverka, aaa.u_cis, tm_sk_plt.cena_pl, tm_sk_plt.druh_pl, "C" as hlavicka FROM aaa INNER JOIN tm_sk_plt on aaa.uzaverka=tm_sk_plt.uzaverka AND aaa.u_cis=tm_sk_plt.u_cis 
			USE IN aaa
			SELECT buffdet
			REPLACE platba WITH i_drupla(ALLTRIM(platba)) FOR hlavicka="C"
		ENDIF 
*stop
	CASE barr = 8
		IF "1"$repriadok
			SELECT id_kas, id_man, u_cis, cen_hlad, uzaverka, VAL(SUBSTR(TEXT,7)) AS id_limit, TEXT, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
				SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl,00000000.00 AS limit, 00000 as osob ;
				FROM buf_ucet WHERE LEFT(TEXT,5) == "Limit" INTO CURSOR Buff READWRITE  ;
				GROUP BY id_man, id_kas, cen_hlad, TEXT, u_cis, uzaverka ORDER BY TEXT, uzaverka, id_kas, id_man
			IF m.issql
				SELECT buff
				SELECT DISTINCT id_limit FROM buff INTO CURSOR novelimity
				SCAN ALL 
					m.hladajidlimit=novelimity.id_limit
					DO pozavirej WITH "limden,limrov,limhl"
					IF USED("limden")
						=REQUERY("limden")
					ELSE 
						USE (m.sqlprefix+"rskstati_limden") IN 0 ALIAS limden
					ENDIF 
					IF USED("limrov")
						=REQUERY("limrov")
					ELSE
						USE (m.sqlprefix+"rskstati_limrovsumy") IN 0 ALIAS limrov
					ENDIF 
					IF USED("limhl")
						=REQUERY("limhl")
					ELSE 
						USE (m.sqlprefix+"rskstati_limhl") IN 0 ALIAS limhl
					ENDIF 
					SELECT limrov.pocet, limrov.limit, limrov.uzaverka, limden.c_mena FROM limrov LEFT JOIN limden ON LEFT(limrov.uzaverka,8)=DTOS(limden.datum) INTO CURSOR limit 
					UPDATE buff SET text=limhl.menolimit from limhl WHERE buff.id_limit=limhl.id_limit AND buff.id_limit =m.hladajidlimit
					UPDATE buff SET osob=IIF(ISNULL(limit.pocet),0,limit.pocet), limit=IIF(ISNULL(limit.limit),0,NTOM(MTON(limit.limit)*en__meny(limit.c_mena,m.girep_mena))) from limit WHERE buff.id_limit=m.hladajidlimit AND buff.uzaverka==limit.uzaverka
					USE IN limit
				ENDSCAN 
				DO pozavirej WITH "novelimity, limden, limrov, limhl"
			ELSE 
				IF FILE(m.gccen_path+"limitdni.dbf")
					SELECT 0
					IF f_acc(m.gccen_path+"limitdni") .AND. f_acc('(m.gccen_path+"limit_hl") order id_limit') .AND. !prazdna()
						SELECT BUFF
						SET RELATION TO id_limit INTO limit_hl
						REPLACE ALL TEXT WITH limit_hl.limit
						SET RELATION OFF INTO limit_hl
						SELECT id_limit, hladina, u_cis, SPACE(50) AS akcia, LEFT(uzaverka,14) as uzaverka,(limit1*pocet1+limit12*pocet12;
							+limit2*pocet2+limit22*pocet22+limit3*pocet3+limit32*pocet32) AS limit,(pocet1+pocet12+pocet2+pocet22+pocet3+pocet32) as osob,c_mena ;
							FROM limitdni INTO CURSOR limit READWRITE 
						REPLACE ALL u_cis WITH TRANS(VAL(SUBSTR(u_cis,3)), "@L 99999")
						REPLACE hladina WITH "1" FOR EMPTY(hladina)
						USE IN limitdni
						SET RELATION TO id_limit INTO limit_hl
						REPLACE ALL akcia WITH limit_hl.limit
						SET RELATION OFF INTO limit_hl
						USE IN limit_hl
						SELECT BUFF
						UPDATE buff SET osob=limit.osob, limit=NTOM(MTON(limit.limit)*en__meny(limit.c_mena,m.girep_mena)) from limit WHERE ALLTRIM(buff.text)==ALLTRIM(limit.akcia) AND ALLTRIM(buff.uzaverka)==ALLTRIM(limit.uzaverka) AND ALLTRIM(buff.u_cis)==ALLTRIM(limit.u_cis)
						USE IN limit
					ENDIF
					DO pozavirej WITH "limit, limit_hl, limitdni"
					IF m.is_newlim
						SELECT buff
						SELECT DISTINCT id_limit, uzaverka, u_cis FROM buff WHERE osob=0 OR ISNULL(osob) INTO CURSOR novelimity
						IF RECCOUNT("novelimity")>0 AND FILE(m.gccen_path+"nlimitdni.dbf") AND FILE(m.gccen_path+"nlimit_hl.dbf") AND FILE(m.gccen_path+"nlimitrov.dbf")
							TRY 
								USE (m.gccen_path+"nlimit_hl") IN 0
								USE (m.gccen_path+"nlimitdni") IN 0
								USE (m.gccen_path+"nlimitrov") IN 0
								USE (m.gccen_path+"nlimitrov_hladiny") IN 0
								m.pokracuj=.t.
							CATCH
								m.pokracuj=.f.
							ENDTRY 
							IF m.pokracuj
								UPDATE buff SET text=nlimit_hl.menolimit from nlimit_hl WHERE buff.id_limit=nlimit_hl.id_limit AND (buff.id_limit IN (SELECT id_limit FROM novelimity))
								SELECT SUM(hodnota*pocet) as hodnota, SUM(pocet) as pocet, id_rov FROM nlimitrov_hladiny GROUP BY id_rov INTO CURSOR rovsumy
								SELECT novelimity.id_limit, m.girep_mena as c_mena,SUM(rovsumy.hodnota) as limit, SUM(rovsumy.pocet) as osob, novelimity.uzaverka, novelimity.u_cis FROM nlimitrov INNER JOIN rovsumy ON nlimitrov.id_rov=rovsumy.id_rov INNER JOIN novelimity ON LEFT(nlimitrov.uzaverka,14)=LEFT(novelimity.uzaverka,14) AND nlimitrov.id_limit=novelimity.id_limit  INTO CURSOR limit GROUP BY novelimity.id_limit, novelimity.uzaverka READWRITE 
								UPDATE limit SET c_mena=nlimitdni.c_mena from nlimitdni WHERE limit.id_limit=nlimitdni.id_limit AND LEFT(limit.uzaverka,8)=DTOS(nlimitdni.datum)
								SELECT buff
								UPDATE buff SET osob=limit.osob, limit=NTOM(MTON(limit.limit)*en__meny(limit.c_mena,m.girep_mena)) from limit WHERE buff.id_limit=limit.id_limit AND buff.uzaverka=limit.uzaverka AND buff.u_cis=limit.u_cis
							ENDIF 
						ENDIF 
						DO pozavirej WITH "novelimity, limit, nlimit_hl, nlimitdni, nlimitrov, rovsumy, nlimitrov_hladiny"
					ENDIF 
				ENDIF 
			ENDIF 
			SELECT BUFF
			INDEX ON TEXT+uzaverka+u_cis TAG texuz
			m.uzaverka=''
			m.text=''
			m.u_cis=''
			SCAN ALL
				IF m.text=BUFF.TEXT .AND. m.uzaverka=BUFF.uzaverka &&.AND. m.u_cis=BUFF.u_cis
					REPLACE BUFF.limit WITH 0, buff.osob WITH 0
				ELSE
					SCATTER MEMVAR FIELDS TEXT,uzaverka,u_cis
				ENDIF
			ENDSCAN
			DELETE TAG ALL
			INDEX ON TEXT+uzaverka+id_man TAG aaa
		ENDIF 
	CASE barr = 11
		SELECT id_kas, id_man, uzaverka, TEXT, SUM(sumpc) AS sumpc, SUM(summc) AS summc, SUM(round50) as round50,;
			SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(sumdanzakl) AS dan_zakl,00000000.00 AS limit ;
			FROM buf_ucet WHERE SUBSTR(uzaverka,12,1) = "*" INTO CURSOR Buff READWRITE;
			GROUP BY id_man, id_kas, TEXT, uzaverka ORDER BY TEXT, uzaverka, id_kas, id_man
	CASE barr = 3
		SELECT id_kas, id_man, uzaverka, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
			SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, druh, SUM(round50) as round50;
			FROM pri_buff INTO CURSOR buff READWRITE;
			GROUP BY druh, id_man ORDER BY druh, id_man
	CASE barr = 4
		SELECT id_kas, id_man, uzaverka, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
			SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, druh, SUM(round50) as round50;
			FROM pri_buff INTO CURSOR buff READWRITE;
			GROUP BY id_kas, druh ORDER BY id_kas, druh
	CASE barr = 5
		SELECT id_kas, id_man, uzaverka, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
			SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, cen_hlad, id_prev, prevadzka, SUM(round50) as round50;
			FROM buf_ucet INTO CURSOR buff READWRITE;
			GROUP BY id_kas ORDER BY id_prev, id_kas
	CASE barr = 7
		IF "1"$repriadok
			SELECT id_kas, id_man, uzaverka, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
				SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, cen_hlad, 0000 as pocet_h, SUM(round50) as round50;
				FROM buf_ucet INTO CURSOR buff READWRITE;
				GROUP BY id_kas, cen_hlad ORDER BY id_kas, cen_hlad
			SELECT distinct id_kas, uzaverka, u_cis, cen_hlad FROM tm_sk_pol INTO CURSOR chl
			SELECT tm_sk_fin.id_kas, tm_sk_fin.uzaverka, SUM(tm_sk_fin.pocet_h) as pocet_h, chl.cen_hlad FROM tm_sk_fin INNER JOIN chl ON tm_sk_fin.id_kas=chl.id_kas AND tm_sk_fin.uzaverka=chl.uzaverka AND tm_sk_fin.u_cis=chl.u_cis INTO CURSOR pocethchl GROUP BY tm_sk_fin.id_kas, tm_sk_fin.uzaverka,chl.cen_hlad
			SELECT id_kas, uzaverka, cen_hlad, SUM(pocet_h) as pocet_h FROM pocethchl GROUP BY id_kas, uzaverka, cen_hlad INTO CURSOR pocchl
			UPDATE buff SET pocet_h=pocchl.pocet_h from pocchl WHERE buff.id_kas=pocchl.id_kas AND buff.uzaverka=pocchl.uzaverka AND buff.cen_hlad=pocchl.cen_hlad
			UPDATE buff SET pocet_h=1 WHERE pocet_h=0
			DO pozavirej WITH "pocchl, pocethchl, chl"
		ENDIF 
	CASE barr = 6
		SELECT id_kas, id_man, uzaverka, SUM(sumpc) AS sumpc, SUM(summc) AS summc,;
			SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, SUM(round50) as round50;
			FROM buf_ucet INTO CURSOR buff READWRITE;
			GROUP BY id_kas, id_man ORDER BY id_kas, id_man
ENDCASE
IF USED("obr_ssum")
	SELECT obr_ssum
	REPLACE ALL spotreba WITH ROUND(spotreba,2)
	IF USED("buffdet")
		SELECT buffdet
		REPLACE ALL sumpc WITH ROUND(sumpc,2), summc WITH ROUND(summc,2), sum_dph WITH ROUND(sum_dph,2), sumpc_dan WITH ROUND(sumpc_dan,2)
		SET RELATION TO id_man+id_kas+SUBSTR(uzaverka,3,12) INTO obr_ssum
	ENDIF 
	IF USED("buff")
		SELECT buff
		REPLACE ALL sumpc WITH ROUND(sumpc,2), summc WITH ROUND(summc,2), sum_dph WITH ROUND(sum_dph,2), sumpc_dan WITH ROUND(sumpc_dan,2)
		SET RELATION TO id_man+id_kas+SUBSTR(uzaverka,3,12) INTO obr_ssum
	ENDIF 
ENDIF 

DO CASE
	CASE barr = 1
		IF "1"$repriadok	
			DO pr_report WITH "trz_i_rozpeti", !m.reporty_volane_z_formu
		ENDIF 
		IF "2"$repriadok
			SELECT buff12
			DO pr_report WITH "trz_i_rozpeti_hlad", !m.reporty_volane_z_formu
		ENDIF 
		DO CASE 
			CASE "3"$repriadok AND "4"$repriadok
				DO rozpis_odpoctu WITH "1,2"
			CASE "3"$repriadok
				DO rozpis_odpoctu WITH "1"
			CASE "4"$repriadok
				DO rozpis_odpoctu WITH "2"
		ENDCASE 
	CASE barr = 2
		DO pr_report WITH "trz_i_roz_odpocty", !m.reporty_volane_z_formu
	CASE barr = 3
		IF "1"$repriadok
			DO pr_report WITH "trz_i_roz_druh", !m.reporty_volane_z_formu
		ENDIF 
		IF "2"$repriadok
			SELECT id_kas, id_man, uzaverka, SUM(sumpc) AS sumpc, SUM(summc) AS summc, SUM(sumpc+zlava) as sumpcpuv,;
				SUM(sum_dph) AS sum_dph, SUM(sumpc_dan) AS sumpc_dan, druh, SUM(round50) as round50;
				FROM pri_buff INTO CURSOR buff READWRITE;
				GROUP BY id_kas, id_man, druh ORDER BY id_kas, id_man, druh
			REPLACE ALL sumpc WITH ROUND(sumpc,2), summc WITH ROUND(summc,2), sum_dph WITH ROUND(sum_dph,2), sumpc_dan WITH ROUND(sumpc_dan,2)
			DO pr_report WITH "trz_i_roz_druh_2", !m.reporty_volane_z_formu
		ENDIF 
	CASE barr = 4
		DO pr_report WITH "trz_i_roz_kas_druh", !m.reporty_volane_z_formu
	CASE barr = 5
        SET RELATION TO id_kas INTO obr_ssum
		DO pr_report WITH "trz_i_roz_kas", !m.reporty_volane_z_formu         && rozpatie podla pokladni
	CASE barr = 6
        SET RELATION TO id_man+id_kas INTO obr_ssum
		DO pr_report WITH "trz_i_roz_kas_man", !m.reporty_volane_z_formu         && rozpatie podla pokladni a managerov
	CASE barr = 7
		IF "1"$repriadok
			SELECT BUFF
	        SET RELATION TO id_kas+cen_hlad INTO obr_ssum
			DO pr_report WITH "trz_i_roz_hlad", !m.reporty_volane_z_formu         && rozpatie podla cenovych hladin a pokladni a managerov
		ENDIF 
		IF "2"$repriadok

			SELECT SUM(mnozstvi*cena) AS spotreba, druh, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas, cen_hlad, id_man, dan;
				FROM obr_sum INTO CURSOR obr_ssum_2 READWRITE GROUP BY id_kas, cen_hlad, druh
			INDEX on id_kas+cen_hlad TAG kashl

			SELECT id_kas, druh, uzaverka, SUM(sumpc) AS sumpc, SUM(teorpc) AS summc, SUM(round50) as round50,;
				SUM(sum_dph) AS sum_dph, SUM(sumdanzakl) AS sumpc_dan, cen_hlad, $0 as spotreba, $0 as spot_dph;
				FROM pri_buff INTO CURSOR buff_2 READWRITE;
				GROUP BY id_kas, cen_hlad, druh ORDER BY id_kas, cen_hlad, druh

			SELECT obr_ssum_2
			REPLACE ALL spotreba WITH ROUND(spotreba,2)
			SELECT buff_2
			UPDATE buff_2 SET spotreba=-obr_ssum_2.spotreba, spot_dph=-obr_ssum_2.spotreba*(obr_ssum_2.dan-1) from obr_ssum_2 WHERE buff_2.id_kas=obr_ssum_2.id_kas AND buff_2.cen_hlad=obr_ssum_2.cen_hlad AND ;
				buff_2.druh=obr_ssum_2.druh
			UPDATE obr_ssum_2 SET spotreba=0 from buff_2 WHERE buff_2.id_kas=obr_ssum_2.id_kas AND buff_2.cen_hlad=obr_ssum_2.cen_hlad AND ;
				buff_2.druh=obr_ssum_2.druh
			INSERT INTO buff_2 (id_kas, cen_hlad, druh, spotreba, spot_dph) SELECT id_kas, cen_hlad, druh, -spotreba, -spotreba*(dan-1) FROM obr_ssum_2 WHERE spotreba<>0

			SELECT BUFF_2
			INDEX on id_kas+cen_hlad+druh TAG aaa
	        DO pr_report WITH "trz_i_roz_hlad_2", !m.reporty_volane_z_formu         && rozpatie podla cenovych hladin a pokladni a managerov
			DO pozavirej WITH "buff_2, obr_ssum_2"
		ENDIF 

		IF "3"$repriadok

			SELECT SUM(mnozstvi*cena) AS spotreba, nazev as druh, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas, cen_hlad, id_man, dan;
				FROM obr_sum INTO CURSOR obr_ssum_2 READWRITE GROUP BY id_kas, cen_hlad, nazev
			INDEX on id_kas+cen_hlad TAG kashl

			SELECT id_kas, nazev as druh, uzaverka, SUM(sumpc) AS sumpc, SUM(teorpc) AS summc, SUM(round50) as round50,;
				SUM(sum_dph) AS sum_dph, SUM(sumdanzakl) AS sumpc_dan, cen_hlad, $0 as spotreba, $0 as spot_dph;
				FROM pri_buff INTO CURSOR buff_2 READWRITE;
				GROUP BY id_kas, cen_hlad, nazev ORDER BY id_kas, cen_hlad, druh

			SELECT obr_ssum_2
			REPLACE ALL spotreba WITH ROUND(spotreba,2)
			SELECT buff_2
			UPDATE buff_2 SET spotreba=-obr_ssum_2.spotreba, spot_dph=-obr_ssum_2.spotreba*(obr_ssum_2.dan-1) from obr_ssum_2 WHERE buff_2.id_kas=obr_ssum_2.id_kas AND buff_2.cen_hlad=obr_ssum_2.cen_hlad AND ;
				buff_2.druh=obr_ssum_2.druh
			UPDATE obr_ssum_2 SET spotreba=0 from buff_2 WHERE buff_2.id_kas=obr_ssum_2.id_kas AND buff_2.cen_hlad=obr_ssum_2.cen_hlad AND ;
				buff_2.druh=obr_ssum_2.druh
			INSERT INTO buff_2 (id_kas, cen_hlad, druh, spotreba, spot_dph) SELECT id_kas, cen_hlad, druh, -spotreba, -spotreba*(dan-1) FROM obr_ssum_2 WHERE spotreba<>0

			SELECT BUFF_2
			INDEX on id_kas+cen_hlad+druh TAG aaa
	        DO pr_report WITH "trz_i_roz_hlad_2", !m.reporty_volane_z_formu         && rozpatie podla cenovych hladin a pokladni a managerov
			DO pozavirej WITH "buff_2, obr_ssum_2"
		ENDIF 

		IF "4"$repriadok

			SELECT SUM(mnozstvi*cena) AS spotreba, spart as druh, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas, cen_hlad, id_man, dan;
				FROM obr_sum INTO CURSOR obr_ssum_2 READWRITE GROUP BY id_kas, cen_hlad, spart
			INDEX on id_kas+cen_hlad TAG kashl

			SELECT id_kas, spart as druh, uzaverka, SUM(sumpc) AS sumpc, SUM(teorpc) AS summc, SUM(round50) as round50,;
				SUM(sum_dph) AS sum_dph, SUM(sumdanzakl) AS sumpc_dan, cen_hlad, $0 as spotreba, $0 as spot_dph;
				FROM pri_buff INTO CURSOR buff_2 READWRITE;
				GROUP BY id_kas, cen_hlad, spart ORDER BY id_kas, cen_hlad, druh

			SELECT obr_ssum_2
			REPLACE ALL spotreba WITH ROUND(spotreba,2)
			SELECT buff_2
			UPDATE buff_2 SET spotreba=-obr_ssum_2.spotreba, spot_dph=-obr_ssum_2.spotreba*(obr_ssum_2.dan-1) from obr_ssum_2 WHERE buff_2.id_kas=obr_ssum_2.id_kas AND buff_2.cen_hlad=obr_ssum_2.cen_hlad AND ;
				buff_2.druh=obr_ssum_2.druh
			UPDATE obr_ssum_2 SET spotreba=0 from buff_2 WHERE buff_2.id_kas=obr_ssum_2.id_kas AND buff_2.cen_hlad=obr_ssum_2.cen_hlad AND ;
				buff_2.druh=obr_ssum_2.druh
			INSERT INTO buff_2 (id_kas, cen_hlad, druh, spotreba, spot_dph) SELECT id_kas, cen_hlad, druh, -spotreba, -spotreba*(dan-1) FROM obr_ssum_2 WHERE spotreba<>0

			SELECT BUFF_2
			INDEX on id_kas+cen_hlad+druh TAG aaa
	        DO pr_report WITH "trz_i_roz_hlad_2", !m.reporty_volane_z_formu         && rozpatie podla cenovych hladin a pokladni a managerov
			DO pozavirej WITH "buff_2, obr_ssum_2"
		ENDIF 

		IF "5"$repriadok

			SELECT SUM(mnozstvi*cena) AS spotreba, STR(dan*100-100,3,0)+" %" as druh, LEFT(pz2,12) AS uzaverka, RIGHT(pz2,2) AS id_kas, cen_hlad, id_man, dan;
				FROM obr_sum INTO CURSOR obr_ssum_2 READWRITE GROUP BY id_kas, cen_hlad, dan
			INDEX on id_kas+cen_hlad TAG kashl

			SELECT id_kas, STR(dan*100-100,3,0)+" %" as druh, uzaverka, SUM(sumpc) AS sumpc, SUM(teorpc) AS summc,;
				SUM(sum_dph) AS sum_dph, SUM(sumdanzakl) AS sumpc_dan, cen_hlad, $0 as spotreba, $0 as spot_dph, SUM(round50) as round50;
				FROM pri_buff INTO CURSOR buff_2 READWRITE;
				GROUP BY id_kas, cen_hlad, dan ORDER BY id_kas, cen_hlad, druh

			SELECT obr_ssum_2
			REPLACE ALL spotreba WITH ROUND(spotreba,2)
			SELECT buff_2
			UPDATE buff_2 SET spotreba=-obr_ssum_2.spotreba, spot_dph=-obr_ssum_2.spotreba*(obr_ssum_2.dan-1) from obr_ssum_2 WHERE buff_2.id_kas=obr_ssum_2.id_kas AND buff_2.cen_hlad=obr_ssum_2.cen_hlad AND ;
				buff_2.druh=obr_ssum_2.druh
			UPDATE obr_ssum_2 SET spotreba=0 from buff_2 WHERE buff_2.id_kas=obr_ssum_2.id_kas AND buff_2.cen_hlad=obr_ssum_2.cen_hlad AND ;
				buff_2.druh=obr_ssum_2.druh
			INSERT INTO buff_2 (id_kas, cen_hlad, druh, spotreba, spot_dph) SELECT id_kas, cen_hlad, druh, -spotreba, -spotreba*(dan-1) FROM obr_ssum_2 WHERE spotreba<>0

			SELECT BUFF_2
			INDEX on id_kas+cen_hlad+druh TAG aaa
	        DO pr_report WITH "trz_i_roz_hlad_2", !m.reporty_volane_z_formu         && rozpatie podla cenovych hladin a pokladni a managerov
			DO pozavirej WITH "buff_2, obr_ssum_2"
		ENDIF 


	CASE barr = 8
		IF "1"$repriadok
	        SET RELATION TO id_man+id_kas+SUBSTR(uzaverka,3,12)+cen_hlad INTO obr_ssum
			DO pr_report WITH "trz_i_roz_limit", !m.reporty_volane_z_formu         && rozpatie podla textov (limitov)
		ENDIF 
		IF "2"$repriadok
			DO rozpis_odpoctu WITH "3"
		ENDIF 
	CASE barr = 9
		IF "1"$repriadok OR "3"$repriadok
			SELECT BUFF
			DO pr_report WITH "trz_i_roz_ucty", !m.reporty_volane_z_formu         && rozpatie podla cenovych hladin a pokladni a managerov
		ENDIF 
		IF "2"$repriadok OR "4"$repriadok
			SELECT buffdet
			INDEX on id_kas+uzaverka+u_cis+hlavicka TAG aaa
			DO pr_report WITH "trz_i_roz_ucty_detail", !m.reporty_volane_z_formu         && rozpatie podla cenovych hladin a pokladni a managerov
			USE IN buffdet
		ENDIF 	
	CASE barr=11
		DO pr_report WITH "trz_i_roz_davky", !m.reporty_volane_z_formu         && rozpatie podla textov (limitov)
ENDCASE
DO pozavirej WITH "buff,obr_ssum,limitdni,buff12, obr_ssum12"


*!*****************************************************************************
*!      Procedure: TRZ_UCTY
*!*****************************************************************************
PROCEDURE trz_ucty     && Prehlad uctov
PARAMETERS repriadok
IF !USED("druhy_pl")
	SELECT 0
	IF m.issql 
		rv("druhy_pl","druhy_pl")
	ELSE 
		USE (m.gchom_path+"druhy_pl")
	ENDIF 
ELSE
	SELECT druhy_pl
ENDIF 
SELECT nazev, id_kas, uzaverka, sumpc, mnozstvi, cen_hlad, dan,;
	u_cis, druh_pl, SPACE(25) AS platba FROM pri_buff;
	INTO CURSOR buff READWRITE ORDER BY id_kas, uzaverka, u_cis
pom_sel = SELECT()
UPDATE buff SET platba=druhy_pl.nazev from druhy_pl WHERE ALLTRIM(buff.druh_pl)==ALLTRIM(druhy_pl.druh)
REPLACE platba WITH i_drupla(buff.druh) FOR EMPTY(buff.platba) OR ISNULL(buff.platba)
DO pr_report WITH "trz_i_ucty", !m.reporty_volane_z_formu
USE IN BUFF

*!*****************************************************************************
*!      Procedure: TRZ_CASY
*!*****************************************************************************
PROCEDURE trz_casy     && Trzby v case
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
DO pozavirej WITH "pom_buff, buff"
IF "1"$m.repriadok
	SELECT SUM(sumpc) AS prachy, id_kas, dan, LEFT(pl_cas,2) as pl_hodina, SUM(sum_dph) AS sum_dph, u_cis, uzaverka, SUM(round50) as round50,;
		SUM(sumdanzakl) AS dan_zakl FROM buf_ucet INTO CURSOR pom_buff READWRITE GROUP BY id_kas, uzaverka, u_cis;
		ORDER BY id_kas, u_cis
	SELECT SUM(prachy) AS prachy, id_kas, dan, pl_hodina, COUNT(pl_hodina) as pocet, SUM(sum_dph) AS sum_dph, SUM(round50) as round50,;
		SUM(dan_zakl) AS dan_zakl FROM pom_buff INTO CURSOR BUFF GROUP BY id_kas, pl_hodina;
		ORDER BY id_kas, pl_hodina
	DO pr_report WITH "trz_i_kas_hod", !m.reporty_volane_z_formu
	DO pozavirej WITH "pom_buff,buff"
ENDIF
IF "2"$m.repriadok
	SELECT nazev,SUM(sumpc) AS prachy, SUM(mnozstvi) AS mnozstvi,id_kas, dan, pl_cas, druh FROM pri_Buff INTO CURSOR pom_buff GROUP BY nazev,pl_cas;
		ORDER BY id_kas, pl_cas READWRITE 
	DO pozavirej WITH "vybery"
	SELECT DISTINCT druh AS druh, .T. AS lbl,.t. as kalkulacia FROM pom_buff INTO CURSOR vybery READWRITE 
	UPDATE vybery SET kalkulacia=!EMPTY(skupiny.typ_kalk) from skupiny WHERE vybery.druh=skupiny.druh 
	SELECT vybery
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",150 TO m.druhxx
			filtrujpriznaky("pom_buff")
		ENDIF 
		RELEASE m.gcfilter_triedenia
		SELECT vybery
		SET ORDER TO druh
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT pom_buff
			SET RELATION TO druh INTO vybery
			DELETE FOR druh=vybery.druh .AND. EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			DO pozavirej WITH "vybery"
			SELECT DISTINCT nazev AS druh, .T. AS lbl FROM pom_buff INTO CURSOR vybery READWRITE 
			SELECT vybery
			INDEX ON druh TAG druh
			IF RECCOUNT()>0
				IF m.reporty_volane_z_formu
					DO FORM seldruhy WITH "vybery","druhpl",150 TO m.druhxx
				ENDIF 
				SELECT vybery
				SET ORDER TO druh
				LOCATE FOR !EMPTY(lbl)
				IF FOUND()
					SELECT pom_buff
					SET RELATION TO nazev INTO vybery
					DELETE FOR nazev=vybery.druh .AND. EMPTY(vybery.lbl)
					DO pozavirej WITH "cas_kalk"
					SELECT druh, nazev,SUM(prachy) AS prachy, SUM(mnozstvi) AS mnozstvi,id_kas, dan, LEFT(pl_cas,2) AS pl_hodina;
						FROM pom_buff INTO CURSOR cas_kalk GROUP BY nazev, pl_hodina;
						ORDER BY druh, nazev, pl_hodina
					DO pr_report WITH "trz_i_nazev_hod", !m.reporty_volane_z_formu
					USE IN cas_kalk
				ENDIF 
			ENDIF 
		ENDIF
	ENDIF
	DO pozavirej WITH "pom_buff,cas_kalk,caskalk, vybery"
ENDIF
IF "3"$m.repriadok
	SELECT druh,SUM(sumpc) AS prachy, SUM(mnozstvi) AS mnozstvi,id_kas, dan, LEFT(pl_cas,2) AS pl_hodina, SUM(round50) as round50 ;
           FROM pri_Buff INTO CURSOR BUFF GROUP BY id_kas,druh,pl_hodina;
		ORDER BY id_kas, druh, pl_hodina
	DO pr_report WITH "trz_i_kas_dr_hod", !m.reporty_volane_z_formu
	DO pozavirej WITH "buff"
ENDIF
IF "4"$m.repriadok
	SELECT id_kas, uzaverka, DATE() as datum, spart,SUM(sumpc) AS sumpc, SUM(sum_dph) AS sumpc_dph, cas_atrib, SUM(round50) as round50 FROM pri_buff INTO CURSOR BUFF1 GROUP BY id_kas, uzaverka, cas_atrib, spart READWRITE 
	UPDATE buff1 SET datum=tm_sk_sez.uct_den from tm_sk_sez WHERE buff1.uzaverka=tm_sk_sez.uzaverka
	SELECT id_kas, datum, spart,SUM(sumpc) AS sumpc, SUM(sumpc_dph) AS sumpc_dph, cas_atrib, SUM(round50) as round50 FROM Buff1 INTO CURSOR BUFF GROUP BY id_kas, datum, cas_atrib, spart;
		ORDER BY id_kas, datum, cas_atrib, spart
	DO pr_report WITH "trz_i_caa", !m.reporty_volane_z_formu
	SELECT id_kas, DOW(datum,2) as den, spart,SUM(sumpc) AS sumpc, SUM(sumpc_dph) AS sumpc_dph,cas_atrib, SUM(round50) as round50 FROM buff1 INTO CURSOR BUFF GROUP BY id_kas, den, cas_atrib, spart;
		ORDER BY id_kas, den, cas_atrib,spart
	DO pr_report WITH "trz_i_caw", !m.reporty_volane_z_formu
	DO pozavirej WITH "buff,buff1"
ENDIF 
IF "5"$m.repriadok
	SELECT id_kas, uzaverka, DATE() as datum, 00000 as hosti, 00000 as ucty, cas_atrib FROM pri_buff INTO CURSOR BUFF1 GROUP BY id_kas, uzaverka, cas_atrib READWRITE 
	UPDATE buff1 SET datum=tm_sk_sez.uct_den from tm_sk_sez WHERE buff1.uzaverka=tm_sk_sez.uzaverka
	SELECT SUM(cena_pl) as cena_slev, uzaverka, u_cis FROM tm_sk_plt GROUP BY uzaverka, u_cis INTO CURSOR cenaslev
	SELECT SUM(tm_sk_fin.pocet_h) as hosti, COUNT(tm_sk_fin.u_cis) as ucty, tm_sk_fin.uzaverka, tm_sk_fin.cas_atrib FROM tm_sk_fin LEFT JOIN cenaslev ON tm_sk_fin.uzaverka=cenaslev.uzaverka AND tm_sk_fin.u_cis=cenaslev.u_cis INTO CURSOR fin1 WHERE !tm_sk_fin.pohladavka AND tm_sk_fin.uzaverka in (SELECT DISTINCT uzaverka FROM buff1) AND cenaslev.cena_slev>=0 GROUP BY tm_sk_fin.uzaverka, tm_sk_fin.cas_atrib
	UPDATE buff1 SET hosti=fin1.hosti, ucty=fin1.ucty from fin1 WHERE buff1.uzaverka=fin1.uzaverka AND buff1.cas_atrib=fin1.cas_atrib
	SELECT SUM(tm_sk_fin.pocet_h) as hosti, COUNT(tm_sk_fin.u_cis) as ucty, tm_sk_fin.uzaverka FROM tm_sk_fin LEFT JOIN cenaslev ON tm_sk_fin.uzaverka=cenaslev.uzaverka AND tm_sk_fin.u_cis=cenaslev.u_cis INTO CURSOR fin1 WHERE !tm_sk_fin.pohladavka AND tm_sk_fin.uzaverka in (SELECT DISTINCT uzaverka FROM buff1) AND cenaslev.cena_slev<0 GROUP BY tm_sk_fin.uzaverka
	UPDATE buff1 SET hosti=buff1.hosti-fin1.hosti, ucty=buff1.ucty-fin1.ucty from fin1 WHERE buff1.uzaverka=fin1.uzaverka
	USE IN fin1	
	SELECT id_kas, datum, SUM(ucty) AS ucty, SUM(hosti) AS hosti, cas_atrib FROM Buff1 INTO CURSOR BUFF GROUP BY id_kas, datum, cas_atrib;
		ORDER BY id_kas, datum, cas_atrib
	DO pr_report WITH "trz_i_cah", !m.reporty_volane_z_formu
	SELECT id_kas, DOW(datum,2) as den, SUM(ucty) AS ucty, SUM(hosti) AS hosti, cas_atrib FROM Buff1 INTO CURSOR BUFF GROUP BY id_kas, den, cas_atrib;
		ORDER BY id_kas, den, cas_atrib
	DO pr_report WITH "trz_i_cax", !m.reporty_volane_z_formu
	DO pozavirej WITH "buff,buff1"
ENDIF 

RETURN


*!*****************************************************************************
*!      Procedure: TR_BUFFPREP
*!*****************************************************************************
PROCEDURE tr_buffprep
IF ! m.buff_prepared
	SELECT DISTINCT nazev,u_cis, id_kas, id_man, SPACE(m.sirkastlpcadruh) AS druh, cen_hlad,dan, ;
		SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy,SUM((IIF(dan = 0, 0, ground((prachy / dan) * (dan - 1),1)))) AS cena_dan,uzaverka ;
		WHERE !EMPTY(k_odpoctu);
		GROUP BY nazev, u_cis,uzaverka,id_kas, id_man, cen_hlad FROM tm_sk_pol INTO CURSOR aaa READWRITE 
	SELECT tm_sk_fin
	SET ORDER TO uzuc
	SELECT aaa
	SET RELATION TO u_cis+uzaverka INTO tm_sk_fin
	REPLACE ALL aaa.prachy WITH IIF(tm_sk_fin.cena_celk=0,tm_sk_fin.cena_slev,aaa.prachy*(tm_sk_fin.cena_slev+tm_sk_fin.round50)/tm_sk_fin.cena_celk)
	REPLACE ALL aaa.cena_dan WITH IIF(tm_sk_fin.cena_celk=0,0,aaa.cena_dan*tm_sk_fin.cena_slev/tm_sk_fin.cena_celk)
	SET RELATION OFF INTO tm_sk_fin
	SELECT DISTINCT nazev, id_kas, id_man, SPACE(m.sirkastlpcadruh) AS druh, cen_hlad,dan, ;
		SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy,SUM(cena_dan) AS cena_dan ;
		GROUP BY nazev,id_kas, id_man, cen_hlad FROM aaa INTO CURSOR trbuff READWRITE 
	REPLACE ALL prachy WITH ground(prachy,1)
	USE IN aaa
	buff_sel = SELECT()
	m.buff_prepared = .F.
ENDIF


*!*****************************************************************************
*!      Procedure: S_DR_STR
*!*****************************************************************************
PROCEDURE s_dr_str
PARAMETERS repriadok
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
DO pozavirej WITH "buff, vybery"
DO tvor_dph1 IN sk_stati
IF !USED("druhy_pl")
	SELECT 0
	IF m.issql 
		rv("druhy_pl","druhy_pl")
	ELSE 
		USE (m.gchom_path+"druhy_pl")
	ENDIF 
ELSE
	SELECT druhy_pl
ENDIF 
SELECT * , 0.000000000 AS koef FROM dph1 INTO CURSOR dph11 READWRITE 
SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_pl, dan FROM dph1 INTO CURSOR dph111 READWRITE  GROUP BY uzaverka, u_cis, dan
INDEX ON uzaverka+u_cis+STR(dan,5,2) TAG uzuc
SELECT dph11
SET RELATION TO uzaverka+u_cis+STR(dan,5,2) INTO dph111
REPLACE koef WITH 1 FOR dph111.cena_pl=0
REPLACE koef WITH dph11.cena_pl/dph111.cena_pl FOR dph111.cena_pl<>0
USE IN dph111
SELECT pri_buff.*,dph11.koef,dph11.druh_pl AS druh_plat FROM pri_buff, dph11 WHERE pri_buff.uzaverka=dph11.uzaverka .AND. pri_buff.u_cis=dph11.u_cis .AND. pri_buff.dan=dph11.dan;
	INTO CURSOR pribuff READWRITE 
SELECT * FROM pribuff WHERE koef<>1 INTO CURSOR pribuff1 READWRITE 
REPLACE ALL mnozstvi WITH mnozstvi*(koef)
REPLACE sumpc WITH sumpc*(koef) ALL
REPLACE sum_dph WITH sum_dph*(koef) ALL
SELECT pribuff
DELETE FOR koef<>1
INSERT INTO pribuff (id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
	sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat) ;
	SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
	sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat FROM pribuff1
SELECT id_kas, sumpc as prachy, druh_plat as druh_k, druh as druh, u_cis, uzaverka, sum_dph as sum_dph,;
	(sumpc-sum_dph) as dan_zakl, dan, cen_hlad, round50 FROM pribuff INTO CURSOR dph3 READWRITE 
DO pozavirej WITH "pribuff, pribuff1, dph11"
IF "1"$m.repriadok
	SELECT DISTINCT druh, id_kas AS ID, druh_k,;
		SUM(prachy) AS prachy, SUM(sum_dph) AS cena_dan2, 0000000.0000 AS cena_dan1, SUM(round50) as round50 ;
		GROUP BY druh_k, druh, ID FROM dph3 INTO CURSOR pomkk READWRITE 
	SELECT DISTINCT druh_k, SPACE(m.sirkastlpcadruh) AS druh,.T. AS lbl, .f. as fiskal FROM pomkk INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_k in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	REPLACE ALL druh WITH i_drupla(druh_k)
	INDEX on druh_k TAG druh_k
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
			filtrujpriznaky("pomkk")
		ENDIF 
		RELEASE m.gcfilter_triedenia
		SELECT vybery
		SET ORDER TO druh_k
		IF FOUND()
			m.txthlavicka="Iba vybrané platby"
		ELSE
			m.txthlavicka=""
		ENDIF
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT pomkk
			SET RELATION TO druh_k INTO vybery
			DELETE FOR ALLTRIM(druh_k)=ALLTRIM(vybery.druh_k).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT DISTINCT druh, ID, SPACE(25) AS id_zkratka , "   " AS spart, SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2, 0000000.0000 AS cena_dan1, 0000000.0000 AS cena_netto, SUM(round50) as round50 ;
				GROUP BY druh, ID FROM pomkk INTO CURSOR pom READWRITE 

  **DUO**
            REPLACE ALL cena_netto WITH prachy - cena_dan1 - cena_dan2
  **DUO**
			REPLACE FOR EMPTY(druh) druh WITH m.ostatni
			UPDATE pom SET id_zkratka=fp_str.id_zkratka from fp_str WHERE pom.id=fp_str.id
			UPDATE pom SET spart=skupiny.spart from skupiny WHERE LEFT(pom.druh,10)=skupiny.druh
			REPLACE FOR EMPTY(spart) spart WITH "___"
			REPLACE FOR EMPTY(id_zkratka) id_zkratka WITH m.ostatni
			INDEX ON spart + druh + ID TAG pom
			DO pr_report WITH "trz_i_dr_kas", !m.reporty_volane_z_formu
		ENDIF
	ENDIF
	DO pozavirej WITH "vybery, pomkk, pom"
ENDIF
IF "2"$m.repriadok
	SELECT DISTINCT druh, id_kas AS ID, druh_k,;
		SUM(prachy) AS prachy, SUM(sum_dph) AS cena_dan2, 0000000.0000 AS cena_dan1, SUM(round50) as round50, dan ;
		GROUP BY druh_k, druh, ID, dan FROM dph3 INTO CURSOR pomkk READWRITE 
	SELECT DISTINCT druh_k, SPACE(m.sirkastlpcadruh) AS druh,.T. AS lbl, .f. as fiskal FROM pomkk INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_k in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	REPLACE ALL druh WITH i_drupla(druh_k)
	INDEX on druh_k TAG druh_k
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_k
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			m.txthlavicka="Iba vybrané platby"
		ELSE
			m.txthlavicka=""
		ENDIF
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT pomkk
			SET RELATION TO druh_k INTO vybery
			DELETE FOR ALLTRIM(druh_k)=ALLTRIM(vybery.druh_k).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT DISTINCT druh, ID, SPACE(25) AS id_zkratka , "   " AS spart, "" as druh_k, dan,;
				SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2, 0000000.0000 AS cena_dan1, 0000000.0000 AS cena_netto, SUM(round50) as round50 ;
				GROUP BY druh, ID, dan FROM pomkk INTO CURSOR pom READWRITE 
  **DUO**
            REPLACE ALL cena_netto WITH prachy - cena_dan1 - cena_dan2
  **DUO**
			REPLACE FOR EMPTY(druh) druh WITH m.ostatni
			UPDATE pom SET id_zkratka=fp_str.id_zkratka from fp_str WHERE pom.id=fp_str.id
			UPDATE pom SET spart=skupiny.spart from skupiny WHERE LEFT(pom.druh,10)=skupiny.druh
			REPLACE FOR EMPTY(spart) spart WITH "___"
			REPLACE FOR EMPTY(id_zkratka) id_zkratka WITH m.ostatni
			INDEX ON ID + spart + druh + druh_k TAG pom
			DO pr_report WITH "trz_i_kas_dr", !m.reporty_volane_z_formu
		ENDIF
	ENDIF
	DO pozavirej WITH "vybery, pomkk, pom"
ENDIF

IF "3"$m.repriadok
	SELECT id_kas,"   " AS spart, dan,druh, ;
		SUM(prachy) AS prachy, SUM(sum_dph) AS cena_dan2, druh_k, SUM(round50) as round50 ;
		GROUP BY druh_k,druh,dan,id_kas FROM dph3 INTO CURSOR pomkk READWRITE 
	UPDATE pomkk SET spart=skupiny.spart from skupiny WHERE LEFT(pomkk.druh,10)=skupiny.druh
	REPLACE FOR EMPTY(spart) spart WITH "___"
	SELECT DISTINCT druh_k, SPACE(m.sirkastlpcadruh) AS druh,.T. AS lbl, .f. as fiskal FROM pomkk INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_k in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	REPLACE ALL druh WITH i_drupla(druh_k)
	INDEX on druh_k TAG druh_k
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_k
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			m.txthlavicka="Iba vybrané platby"
		ELSE
			m.txthlavicka=""
		ENDIF
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT pomkk
			SET RELATION TO druh_k INTO vybery
			DELETE FOR ALLTRIM(druh_k)=ALLTRIM(vybery.druh_k).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT id_kas,SPACE(25) AS id_zkratka,spart, dan, ;
				SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2, SUM(round50) as round50;
				GROUP BY spart,dan,id_kas FROM pomkk INTO CURSOR i_tr_sp READWRITE 
			UPDATE i_tr_sp SET id_zkratka=fp_str.id_zkratka from fp_str WHERE i_tr_sp.id_kas=fp_str.id
			REPLACE FOR EMPTY(id_zkratka) id_zkratka WITH m.ostatni
			SELECT 'XX' AS id_kas,'Spolu' AS id_zkratka, spart, dan, ;
				SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2, SUM(round50) as round50 ;
				GROUP BY spart,dan FROM i_tr_sp INTO CURSOR i_tr_ READWRITE 
			SELECT i_tr_sp
			APPEND FROM DBF("i_tr_")
*			INSERT INTO i_tr_sp (id_kas, id_zkratka, spart, dan, prachy, cena_dan2) SELECT id_kas, id_zkratka, spart, dan, prachy, cena_dan2 FROM i_tr_
			INDEX ON id_kas + STR(dan,4,2) + spart TAG aaa 
			DO pr_report WITH "trz_i_dr_spart", !m.reporty_volane_z_formu
			DO pozavirej WITH "i_tr_,i_tr_sp"
		ENDIF
	ENDIF
	USE IN vybery
	USE IN pomkk
ENDIF
IF "4"$m.repriadok
	SELECT id_kas, "   " AS spart, dan, druh, cen_hlad, ;
		SUM(prachy) AS prachy, SUM(sum_dph) AS cena_dan2, druh_k, SUM(round50) as round50;
		GROUP BY druh_k, druh, dan, id_kas FROM dph3 INTO CURSOR pomxx READWRITE 
	UPDATE pomxx SET spart=skupiny.spart from skupiny WHERE LEFT(pomxx.druh,10)=skupiny.druh
	REPLACE FOR EMPTY(spart) spart WITH "___"
	SELECT id_kas, spart, dan, SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2, druh_k, SPACE(25) as id_zkratka, SPACE(30) as menoplatby, cen_hlad, 0000000.0000 as cena_netto, SUM(round50) as round50 ;
		GROUP BY druh_k, spart, dan, id_kas FROM pomxx INTO CURSOR i_tr_sp READWRITE 
	UPDATE i_tr_sp SET id_zkratka=fp_str.id_zkratka from fp_str WHERE i_tr_sp.id_kas=fp_str.id
	REPLACE FOR EMPTY(id_zkratka) id_zkratka WITH m.ostatni
	SELECT 'XX' AS id_kas,'Spolu' AS id_zkratka, spart, dan, ;
		SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2, druh_k, SPACE(30) as menoplatby, cen_hlad, SUM(round50) as round50 ;
		GROUP BY druh_k,spart,dan FROM i_tr_sp INTO CURSOR i_tr_ READWRITE 
	SELECT i_tr_sp
	APPEND FROM DBF("i_tr_")
	IF !USED("druhy_pl")
		SELECT 0
		IF m.issql 
			rv("druhy_pl","druhy_pl")
		ELSE 
			USE (m.gchom_path+"druhy_pl")
		ENDIF 
	ELSE
		SELECT druhy_pl
	ENDIF 
    UPDATE i_tr_sp SET menoplatby=druhy_pl.j0 from druhy_pl WHERE ALLTRIM(i_tr_sp.druh_k)==ALLTRIM(druhy_pl.nazev)    
    SELECT i_tr_sp
    REPLACE menoplatby WITH i_drupla(i_tr_sp.druh_k) FOR EMPTY(i_tr_sp.menoplatby) OR ISNULL(i_tr_sp.menoplatby)

  **DUO**
    REPLACE ALL cena_netto WITH prachy - cena_dan2
  **DUO**

	INDEX ON id_kas + menoplatby + STR(dan,4,2) + spart TAG aaa 
	DO pr_report WITH "trz_i_pl_spart", !m.reporty_volane_z_formu
	DO pozavirej WITH "i_tr_,i_tr_sp, pomxx"
ENDIF
IF "5"$m.repriadok
	SELECT DISTINCT druh, id_kas AS ID, druh_k,;
		SUM(prachy) AS prachy, SUM(sum_dph) AS cena_dan2, 0000000.0000 AS cena_dan1, SUM(round50) as round50, dan ;
		GROUP BY druh_k, druh, ID, dan FROM dph3 INTO CURSOR pomkk READWRITE 
	SELECT DISTINCT druh_k, SPACE(m.sirkastlpcadruh) AS druh,.T. AS lbl, .f. as fiskal FROM pomkk INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_k in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	REPLACE ALL druh WITH i_drupla(druh_k)
	INDEX on druh_k TAG druh_k
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_k
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			m.txthlavicka="Iba vybrané platby"
		ELSE
			m.txthlavicka=""
		ENDIF
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT pomkk
			SET RELATION TO druh_k INTO vybery
			DELETE FOR ALLTRIM(druh_k)=ALLTRIM(vybery.druh_k).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT DISTINCT druh, ID, SPACE(25) AS id_zkratka , "   " AS spart, druh_k, dan,;
				SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2, 0000000.0000 AS cena_dan1, SUM(round50) as round50 ;
				GROUP BY druh, druh_k, ID, dan FROM pomkk INTO CURSOR pom READWRITE 
			REPLACE FOR EMPTY(druh) druh WITH m.ostatni
			UPDATE pom SET id_zkratka=fp_str.id_zkratka from fp_str WHERE pom.id=fp_str.id
			UPDATE pom SET spart=skupiny.spart from skupiny WHERE LEFT(pom.druh,10)=skupiny.druh
			REPLACE FOR EMPTY(spart) spart WITH "___"
			REPLACE FOR EMPTY(id_zkratka) id_zkratka WITH m.ostatni
			INDEX ON ID + spart + druh + druh_k TAG pom
			DO pr_report WITH "trz_i_kas_dr@5", !m.reporty_volane_z_formu
		ENDIF
	ENDIF
	DO pozavirej WITH "vybery, pomkk, pom"
ENDIF
DO pozavirej WITH "pom, dph3"


*!*****************************************************************************
*!      Procedure: TVOR_DPH1
*!*****************************************************************************
PROCEDURE tvor_dph1
IF USED("dph1")
	RETURN
ENDIF
SELECT uzaverka, id_kas, u_cis, dan_sazba, SUM(prachy) as prachy, SUM(dan) as dan, SPACE(3) as autor ;
       GROUP BY uzaverka, id_kas, u_cis, dan_sazba FROM tm_sk_pol INTO CURSOR dph1_pol READWRITE 
INDEX on uzaverka + id_kas + u_cis TAG uku
UPDATE dph1_pol SET autor=tm_sk_fin.autor FROM tm_sk_fin WHERE dph1_pol.uzaverka=tm_sk_fin.uzaverka AND dph1_pol.u_cis=tm_sk_fin.u_cis

SELECT id_kas, SPACE(30) as zkr_kas, uzaverka, u_cis, druh_pl, SUM(cena_pl) as cena_pl, 000000000.00 as prachy_sum ;
       GROUP BY uzaverka, id_kas, u_cis, druh_pl FROM tm_sk_plt INTO CURSOR dph1_plt READWRITE 
UPDATE dph1_plt SET zkr_kas=tm_sk_sez.zkr_kas FROM tm_sk_sez WHERE dph1_plt.id_kas=tm_sk_sez.id_kas

SELECT uzaverka, id_kas, u_cis, SUM(cena_pl) as cena_pl GROUP BY uzaverka, id_kas, u_cis ;
       FROM dph1_plt INTO CURSOR dph1_plt_sum READWRITE 
INDEX on uzaverka + id_kas + u_cis TAG uku

SELECT dph1_plt
SET RELATION TO uzaverka + id_kas + u_cis INTO dph1_plt_sum
REPLACE ALL prachy_sum WITH dph1_plt_sum.cena_pl
USE IN dph1_plt_sum

SELECT pol.id_kas as id_kas_o, pol.autor, pol.dan_sazba as dan, pol.uzaverka, pol.u_cis, pol.prachy, pol.dan as cena_dan, plt.druh_pl, plt.cena_pl, plt.prachy_sum, .F. as hotovo, 00.00 as round50 ;
       FROM dph1_pol pol LEFT JOIN dph1_plt plt ON plt.uzaverka+plt.id_kas+plt.u_cis = pol.uzaverka+pol.id_kas+pol.u_cis ;
       INTO CURSOR dph1 READWRITE 
UPDATE dph1 SET round50=tm_sk_fin.round50 FROM tm_sk_fin WHERE dph1.uzaverka=tm_sk_fin.uzaverka AND dph1.u_cis=tm_sk_fin.u_cis
SELECT dph1
REPLACE round50 WITH 0 FOR !("hoto"$LOWER(druh_pl))
SELECT dph1
INDEX on uzaverka+u_cis TAG uzuc
LOCAL m.uzaverka, m.u_cis
m.uzaverka="sdfsdfsd"
m.u_cis="asdfa"
SCAN FOR !EMPTY(round50)
	IF dph1.uzaverka=m.uzaverka AND dph1.u_cis=m.u_cis
		REPLACE round50 WITH 0
	ELSE
		m.uzaverka=dph1.uzaverka
		m.u_cis=dph1.u_cis
	ENDIF 
ENDSCAN 

REPLACE FOR prachy_sum # 0 cena_dan WITH ground(cena_dan * cena_pl / prachy_sum,2), cena_pl WITH ground(prachy * cena_pl / prachy_sum,2), hotovo WITH .T.
REPLACE FOR ISNULL(druh_pl) druh_pl WITH "...null..."
INDEX ON STR(dan,5,3) + ALLTR(druh_pl) TAG pr_druh_pl


*!*****************************************************************************
*!      Procedure: TVOR_DPH3
*!*****************************************************************************
PROCEDURE tvor_dph3
PARAMETERS aj_uzav
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
DO pozavirej WITH "buff"
DO tvor_dph1 IN sk_stati
SELECT * , 0.0000000 AS koef FROM dph1 INTO CURSOR dph11 READWRITE 
SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_pl, dan FROM dph1 INTO CURSOR dph111 READWRITE GROUP BY uzaverka, u_cis, dan
INDEX ON uzaverka+u_cis+STR(dan,5,2) TAG uzuc
SELECT dph11
SET RELATION TO uzaverka+u_cis+STR(dan,5,2) INTO dph111
REPLACE koef WITH 1 FOR dph111.cena_pl=0
REPLACE koef WITH MTON(dph11.cena_pl)/MTON(dph111.cena_pl) FOR dph111.cena_pl<>0
USE IN dph111
SELECT pri_buff.*,dph11.koef,dph11.druh_pl AS druh_plat FROM pri_buff, dph11 WHERE pri_buff.uzaverka=dph11.uzaverka .AND. pri_buff.u_cis=dph11.u_cis .AND. pri_buff.dan=dph11.dan;
	INTO CURSOR pribuff READWRITE  
SELECT * FROM pribuff WHERE koef<>1 INTO CURSOR pribuff1 READWRITE 
REPLACE ALL mnozstvi WITH ground(mnozstvi*(koef),3)
REPLACE sumpc WITH ground(sumpc*(koef),m.girep_round) ALL
REPLACE sum_dph WITH ground(sum_dph*(koef),m.girep_round) ALL
SELECT pribuff
DELETE FOR koef<>1
INSERT INTO pribuff (id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
	sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat) ;
	SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
	sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat FROM pribuff1
USE IN pribuff1
SELECT uzaverka, u_cis, SUM(sumpc) as sumpc, druh_plat, dan  FROM pribuff INTO CURSOR pooprave GROUP BY uzaverka, u_cis, druh_plat, dan
SELECT pooprave.uzaverka, pooprave.u_cis, pooprave.sumpc, pooprave.druh_plat, pooprave.dan, dph1.cena_pl FROM pooprave INNER JOIN dph1 ON pooprave.uzaverka=dph1.uzaverka AND pooprave.u_cis=dph1.u_cis;
	AND pooprave.druh_plat=dph1.druh_pl AND pooprave.dan=dph1.dan INTO CURSOR asdasd READWRITE 

SCAN FOR sumpc<>cena_pl
	SELECT pribuff
	LOCATE FOR uzaverka=asdasd.uzaverka AND u_cis=asdasd.u_cis AND dan=asdasd.dan AND druh_plat=asdasd.druh_plat
	IF FOUND()
		REPLACE sumpc WITH sumpc-(asdasd.sumpc-asdasd.cena_pl)
	ENDIF 
ENDSCAN 
USE IN asdasd
USE IN pooprave
SELECT id_kas, sumpc as prachy, id_man, druh_plat as druh, u_cis, uzaverka, sum_dph as sum_dph, round50,;
	(sumpc-sum_dph) as dan_zakl,dan FROM pribuff INTO CURSOR dph3
USE IN pribuff
USE IN dph11
IF m.aj_uzav
	SELECT id_kas, SUM(prachy) AS prachy, id_man, druh, u_cis, uzaverka, SUM(sum_dph) AS sum_dph, SUM(dan_zakl) AS dan_zakl, dan, SUM(round50) as round50 ;
		FROM dph3 GROUP BY id_kas,id_man,uzaverka,druh,dan INTO CURSOR buff READWRITE ORDER BY id_man
ELSE
	SELECT id_kas, SUM(prachy) AS prachy, id_man, druh, u_cis, uzaverka, SUM(sum_dph) AS sum_dph, SUM(dan_zakl) AS dan_zakl, dan, SUM(round50) as round50 ;
		FROM dph3 GROUP BY id_man,druh,dan INTO CURSOR buff READWRITE ORDER BY id_man
ENDIF
USE IN dph3
SELECT BUFF
REPLACE ALL prachy WITH ground(prachy,m.girep_round)


*!*****************************************************************************
*!      Procedure: TVOR_DPH2
*!*****************************************************************************
PROCEDURE tvor_dph2
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
DO pozavirej WITH "buff"
DO tvor_dph1 IN sk_stati
SELECT * , 0.0000000000 AS koef FROM dph1 INTO CURSOR dph11 READWRITE 
SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_pl, dan FROM dph1 INTO CURSOR dph111 READWRITE GROUP BY uzaverka, u_cis, dan
INDEX ON uzaverka+u_cis+STR(dan,5,2) TAG uzuc
USE IN dph1
SELECT dph11
SET RELATION TO uzaverka+u_cis+STR(dan,5,2) INTO dph111
REPLACE koef WITH 1 FOR dph111.cena_pl=0
REPLACE koef WITH dph11.cena_pl/dph111.cena_pl FOR dph111.cena_pl<>0
USE IN dph111
SELECT pri_buff.*,dph11.koef,dph11.druh_pl AS druh_plat FROM pri_buff, dph11 WHERE pri_buff.uzaverka=dph11.uzaverka .AND. pri_buff.u_cis=dph11.u_cis .AND. pri_buff.dan=dph11.dan;
	INTO CURSOR pribuff READWRITE 
SELECT * FROM pribuff WHERE koef<>1 INTO TABLE (m.gcpri_path +"pribuff1")
REPLACE ALL mnozstvi WITH mnozstvi*(koef)
REPLACE sumpc WITH ground(sumpc*(koef),m.girep_round) ALL
REPLACE sum_dph WITH ground(sum_dph*(koef),m.girep_round) ALL

SELECT pribuff
DELETE FOR koef<>1
INSERT INTO pribuff (id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
	sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat) ;
	SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
	sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat FROM pribuff1
USE IN pribuff1
SELECT id_kas, sumpc as prachy, cen_hlad, druh_plat as druh, u_cis, uzaverka, sum_dph as sum_dph,round50,;
	(sumpc-sum_dph) as dan_zakl,dan FROM pribuff INTO CURSOR dph2 
SELECT dph2.id_kas,SUM(prachy) AS prachy,cen_hlad,druh,u_cis,uzaverka,SUM(sum_dph) AS sum_dph,SUM(dan_zakl) AS dan_zakl,dan, SUM(round50) as round50 ;
	FROM dph2 GROUP BY dph2.id_kas,cen_hlad,druh,dan INTO CURSOR buff READWRITE ORDER BY cen_hlad
DO pozavirej WITH "dph11, dph2, pribuff1, pribuff"


*!*****************************************************************************
*!      Procedure: ZSTRAVA
*!*****************************************************************************
PROCEDURE zstrava
PARAMETERS cislo
PRIVATE karta, plusy, minusy, listkov,uskarty,usskup
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
IF !USED("obr_sum")
	DO tvor_obrsum
ENDIF 
IF "1"$m.cislo
	SELECT obr_sum.nazev, obr_sum.druh,ROUND(obr_sum.mnozstvi*obr_sum.cena,4) AS suma, 00.000 AS dan_1, obr_sum.pz2 as doklad FROM obr_sum ;
		INTO CURSOR sumar1 READWRITE  
	UPDATE sumar1 SET dan_1=skupiny.dan01 from skupiny WHERE sumar1.druh=skupiny.druh
	REPLACE dan_1 WITH 1 FOR EMPTY(dan_1)
	SELECT SUM(suma) AS suma, dan_1,doklad, SPACE(20) AS priznak FROM sumar1 INTO CURSOR sumar READWRITE GROUP BY doklad, dan_1
	USE IN sumar1
	SELECT DISTINCT SUBSTR(uzaverka,3,12)+"/"+id_kas AS doklad,priznak FROM tm_sk_sez INTO CURSOR sez 
	INDEX ON doklad TAG doklad
	SELECT sumar
	SET RELATION TO doklad INTO sez
	REPLACE ALL sumar.priznak WITH sez.priznak
	SET RELATION OFF INTO sez
	REPLACE priznak WITH "Bežná spotreba" FOR EMPTY(priznak)
	SELECT SUM(suma) AS suma, 1.000 AS dan_1, priznak FROM sumar INTO CURSOR delspot WHERE !("repre"$LOWER(priznak)) GROUP BY priznak;
		UNION ;
		SELECT SUM(suma) AS suma, dan_1, priznak FROM sumar WHERE ("repre"$LOWER(priznak)) GROUP BY dan_1, priznak
	DO pr_report WITH "trz_del_spot", !m.reporty_volane_z_formu
	DO pozavirej WITH "delspot, sez, sumar"
ENDIF 
IF "2"$m.cislo .or. "3"$m.cislo .or. "4"$m.cislo
	IF m.issql
		RETURN
	ENDIF 
	SELECT SUM(ROUND(mnozstvi*cena,4)) AS suma, stravnik.meno, stravnik.karta,0000 AS pocet_l,orgstr.nazov_org, 000000.00 AS pc, obr_sum.cen_hlad FROM obr_sum,(m.gccen_path+"stravnik"),(m.gccen_path+"orgstr") WHERE ;
		!EMPTY(obr_sum.cen_hlad) .AND. (obr_sum.cen_hlad)=stravnik.cen_hlad .AND. stravnik.cislo_org=orgstr.cislo_org GROUP BY karta INTO CURSOR sumar READWRITE 
	INDEX ON karta TAG karta
	INDEX ON nazov_org+meno TAG ktokde
	SELECT DISTINCT cen_hlad FROM sumar INTO CURSOR kartky
	SCAN ALL
		m.karta=kartky.cen_hlad
		SELECT DISTINCT u_cis, uzaverka, SUM(mnozstvi) AS mnozstvi, SUM(sumpc) AS pc FROM pri_buff INTO CURSOR ucty WHERE pri_buff.cen_hlad=m.karta GROUP BY uzaverka, u_cis
		COUNT TO m.plusy FOR mnozstvi>0
		COUNT TO m.minusy FOR mnozstvi<0
		SUM pc TO m.pc
		m.listkov=m.plusy-m.minusy
		SELECT sumar
		REPLACE pocet_l WITH m.listkov FOR cen_hlad=m.karta
		REPLACE pc WITH m.pc FOR cen_hlad=m.karta
		SELECT kartky
		USE IN ucty
	ENDSCAN
	USE IN kartky
	IF "2"$m.cislo
		SELECT sumar
		DO pr_report WITH "trz_del_strv", !m.reporty_volane_z_formu
	ENDIF 
	IF "3"$m.cislo
		SELECT DISTINCT stravnik.karta,stravnik.meno, .T. AS LBL, obr_sum.cen_hlad FROM obr_sum,(m.gccen_path+"stravnik") WHERE;
			!EMPTY(obr_sum.cen_hlad) .AND. obr_sum.cen_hlad=stravnik.cen_hlad INTO CURSOR sumar1 READWRITE ORDER BY meno
		DO FORM selpartn WITH "sumar1","meno"
		SELECT pri_buff.nazev, pri_buff.uzaverka,SUM(pri_buff.sumpc) AS cena,SUM(pri_buff.mnozstvi) AS mnozstvi, pri_buff.u_cis,;
			pri_buff.id_kas,pri_buff.pl_cas, sumar1.karta, sumar1.meno , 000000.00 as spotreba, 0000 as pocet_l FROM pri_buff, sumar1  WHERE pri_buff.cen_hlad=sumar1.cen_hlad .AND. !EMPTY(sumar1.LBL) GROUP BY karta,id_kas,uzaverka,u_cis, nazev INTO CURSOR ucty READWRITE 
		INDEX ON karta TAG karta
		SELECT sumar
		SET RELATION TO karta INTO ucty
		REPLACE ALL ucty.spotreba WITH ABS(sumar.suma), ucty.pocet_l WITH sumar.pocet_l
		SET RELATION OFF INTO ucty
		SELECT ucty
		DO pr_report WITH "trz_strv_uct", !m.reporty_volane_z_formu
		USE IN sumar1
		USE IN ucty
	ENDIF
	IF "4"$m.cislo
		SELECT DISTINCT stravnik.karta,stravnik.meno, .T. AS LBL, stravnik.cen_hlad FROM obr_sum,(m.gccen_path+"stravnik") WHERE;
			!EMPTY(obr_sum.cen_hlad) .AND. obr_sum.cen_hlad=stravnik.cen_hlad INTO CURSOR sumar1 READWRITE ORDER BY meno
		DO FORM selpartn WITH "sumar1","meno"
		SELECT pri_buff.uzaverka,SUM(pri_buff.sumpc) AS suma, pri_buff.u_cis,;
			pri_buff.id_kas,pri_buff.pl_cas, sumar1.karta, sumar1.meno, 000000.00 as spotreba, 0000 as pocet_l FROM pri_buff, sumar1  WHERE pri_buff.cen_hlad=sumar1.cen_hlad .AND. !EMPTY(sumar1.LBL) GROUP BY karta,id_kas,uzaverka,u_cis INTO CURSOR ucty READWRITE 
		INDEX ON karta TAG karta
		SELECT sumar
		SET RELATION TO karta INTO ucty
		REPLACE ALL ucty.spotreba WITH ABS(sumar.suma), ucty.pocet_l WITH sumar.pocet_l
		SET RELATION OFF INTO ucty
		SELECT ucty
		SET RELATION OFF INTO sumar
		DO pr_report WITH "trz_strv_uc1", !m.reporty_volane_z_formu
		USE IN sumar1
		USE IN ucty
	ENDIF 
	USE IN sumar
ENDIF
RELEASE m.karta, m.plusy, m.minusy, m.listkov



*!*****************************************************************************
*!      Procedure: TRZ_MENY
*!      Trzby v cudzej mene
*!*****************************************************************************
PROCEDURE trz_meny
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
IF USED('buff')
	USE IN BUFF
ENDIF
SELECT uzaverka, SUM(cena_pl) AS cena_pl, SUM(cena_mena) AS cena_mena, mena, id_kas, c_mena ;
	FROM tm_sk_plt INTO CURSOR buff READWRITE GROUP BY id_kas, uzaverka, mena
REPLACE cena_mena WITH cena_pl FOR EMPTY(mena)
REPLACE mena WITH en__meny(m.girep_mena,3) FOR EMPTY(mena)
IF "1"$m.repriadok
	DO pr_report WITH "trz_i_meny", !m.reporty_volane_z_formu
ENDIF
IF "2"$m.repriadok
	SELECT "" AS uzaverka, SUM(cena_pl) AS cena_pl, SUM(cena_mena) AS cena_mena, mena, id_kas ;
		FROM BUFF INTO CURSOR buff1 GROUP BY id_kas, mena
    DO pr_report WITH "trz_i_meny@2", !m.reporty_volane_z_formu
	USE IN buff1
ENDIF
IF "3"$m.repriadok
	SELECT "" AS uzaverka, SUM(cena_pl) AS cena_pl, SUM(cena_mena) AS cena_mena, mena, "" AS id_kas ;
		FROM BUFF INTO CURSOR buff1 GROUP BY mena
    DO pr_report WITH "trz_i_meny@3", !m.reporty_volane_z_formu
	USE IN buff1
ENDIF
USE IN BUFF



*!*****************************************************************************
*!      Function: GROUND
*!*****************************************************************************
* Pre spravne zaokruhlovanie cisel ako 0.25,...
FUNCTION ground
PARAMETERS xx,xy
IF ABS(ROUND(m.xx, m.xy)-m.xx)*10^(m.xy+1)=5
	IF INT(ROUND(m.xx, m.xy)*10^m.xy/2)*2=ROUND(m.xx, m.xy)*10^m.xy
		RETURN ROUND(m.xx,m.xy)
	ELSE
		RETURN (ROUND(m.xx,m.xy)*10^m.xy-1)/10^m.xy
	ENDIF
ELSE
	RETURN ROUND(m.xx,m.xy)
ENDIF





*!*****************************************************************************
*!      Procedure: UVERY
*!*****************************************************************************
PROCEDURE uvery
PARAMETERS BAR
PRIVATE ucis,uzav,por,druh,obedov
DO pozavirej WITH "uvery"
IF m.issql
	rv("uvery","uvery")
	SELECT uvery
	INDEX on hjmeno TAG hjmeno
ELSE
	IF FILE(m.gcdat_path+"uvery.dbf")
		TRY 
			USE (m.gccen_path + "uvery") ORDER hjmeno IN 0
		CATCH
			USE (m.gccen_path + "uvery") IN 0
		ENDTRY 
	ENDIF 
ENDIF 
IF USED("uvery")
	uvery_had = CREATEOBJECT("had", PROGRAM())
	uvery_had.SHOW()
	SELECT tm_sk_txt
	SET ORDER TO tm_skxx
	SELECT DISTINCT druh_pl, SPACE(m.sirkastlpcadruh) as druh, .T. as lbl, .f. as fiskal FROM tm_sk_plt INTO CURSOR vybery READWRITE WHERE LOWER(LEFT(druh_pl,6))="n_uver"
	IF !USED("druhy_pl")
		SELECT 0
		IF m.issql 
			rv("druhy_pl","druhy_pl")
		ELSE 
			USE (m.gchom_path+"druhy_pl")
		ENDIF 
	ELSE
		SELECT druhy_pl
	ENDIF 
    UPDATE vybery SET druh=druhy_pl.j0 from druhy_pl WHERE ALLTRIM(vybery.druh_pl)==ALLTRIM(druhy_pl.nazev)    
	UPDATE vybery SET fiskal=.t. WHERE druh_pl in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	INDEX ON druh_pl TAG druh_pl
	INDEX ON druh TAG druh
	IF RECCOUNT()>1
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druh",150,.t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_pl
		LOCATE FOR !EMPTY(lbl)
		IF !FOUND()
			USE IN vybery
			RETURN  
		ENDIF
	ENDIF
	SELECT DISTINCT hjmeno, SPACE(35) AS akce FROM tm_sk_txt INTO CURSOR druhy READWRITE ;
		WHERE uzaverka + u_cis IN (SELECT DISTINCT uzaverka + u_cis FROM tm_sk_plt WHERE druh_pl IN (SELECT druh_pl FROM vybery WHERE lbl))
	uvery_had.set_had(90)
	DELETE FOR EMPTY(hjmeno)
	uvery_had.set_had(100)
	RELEASE uvery_had
	IF prazdna()
		=MESSAGEBOX(jaz("form_neboli_uvery"),64,"FoodMan")
		DO pozavirej WITH "uvery,druhy"
		RETURN
	ENDIF
	IF "1"$m.bar
		DO WHILE .T.
			m.hjmeno=""
			IF m.reporty_volane_z_formu
				DO FORM dajdruh WITH "","druhy","hjmeno",200 TO m.hjmeno
			ENDIF 
			IF EMPTY(m.hjmeno)
				EXIT
			ENDIF
			SELECT druhy
			LOCATE FOR ALLTRIM(druhy.hjmeno)==ALLTRIM(m.hjmeno)
			m.hjmeno=druhy.hjmeno
			m.akce=druhy.akce
			SELECT uvery
			LOCATE FOR ALLTRIM(hjmeno)==ALLTRIM(m.hjmeno)
			IF FOUND()
				SCATTER MEMVAR
			ELSE
				SCATTER MEMVAR BLANK FIELDS adresa1, adresa2, adresa3, dic, txt, akce
			ENDIF
			uvery_had = CREATEOBJECT("had", PROGRAM())
			uvery_had.SHOW()
			SELECT id_kas,u_cis,uzaverka,cena_pl,00 AS poradie,druh_pl, SPACE(15) AS hjmeno, 000000000.00 AS trzba_dph1, 000000000.00 AS trzba_dph2,;
				000000000.00 AS trzba_dph3, 000000000.00 AS trzba_dph4, 000000000.00 AS dph1, 000000000.00 AS dph2, 000000000.00 AS dph3, 000000000.00 AS dph4, 00000.00 AS obedov;
					FROM tm_sk_plt INTO CURSOR zplt READWRITE ORDER BY uzaverka,u_cis
			INDEX ON id_kas+uzaverka+u_cis TAG kasuzuc
			uvery_had.set_had(50)
			GO TOP
			m.ucis=SPACE(6)
			m.uzav=SPACE(12)
			m.por=1
			SCAN ALL
				IF u_cis=m.ucis .AND. uzaverka=m.uzav
					m.por=m.por+1
				ELSE
					m.por=1
				ENDIF
				REPLACE poradie WITH m.por
				m.ucis=u_cis
				m.uzav=uzaverka
			ENDSCAN
			uvery_had.set_had(90)
			DELETE FOR LEFT(zplt.druh_pl,6)<>UPPER("n_uver")
			UPDATE zplt SET trzba_dph1=tm_sk_fin.trzba1, trzba_dph2=tm_sk_fin.trzba2, trzba_dph3=tm_sk_fin.trzba3, trzba_dph4=tm_sk_fin.trzba4, dph1=tm_sk_fin.dan1, dph2=tm_sk_fin.dan2, dph3=tm_sk_fin.dan3, dph4=tm_sk_fin.dan4;
				FROM tm_sk_fin WHERE zplt.uzaverka=tm_sk_fin.uzaverka AND zplt.u_cis=tm_sk_fin.u_cis
			SET RELATION TO uzaverka+u_cis+STR(poradie,2,0) INTO tm_sk_txt
			UPDATE zplt SET hjmeno=tm_sk_txt.hjmeno
			uvery_had.set_had(100)
			DELETE FOR ALLTRIM(tm_sk_txt.hjmeno)<>ALLTRIM(m.hjmeno)
			RELEASE uvery_had
			DO pr_report WITH 'trz_uvery3', !m.reporty_volane_z_formu
			SET RELATION OFF INTO tm_sk_txt
			SET FILTER TO
			USE IN zplt
		ENDDO
	ENDIF
	IF "2"$m.bar
		DO WHILE .T.
			m.hjmeno=""
			IF m.reporty_volane_z_formu
				DO FORM dajdruh WITH "","druhy","hjmeno",200 TO m.hjmeno
			ENDIF 
			IF EMPTY(m.hjmeno)
				EXIT
			ENDIF
			SELECT druhy
			LOCATE FOR ALLTRIM(druhy.hjmeno)==ALLTRIM(m.hjmeno)
			m.hjmeno=druhy.hjmeno
			m.akce=druhy.akce
			SELECT uvery
			LOCATE FOR ALLTRIM(hjmeno)==ALLTRIM(m.hjmeno)
			IF FOUND()
				SCATTER MEMVAR
			ELSE
				SCATTER MEMVAR BLANK FIELDS adresa1, adresa2, adresa3, dic, txt, akce
			ENDIF
			uvery_had = CREATEOBJECT("had", PROGRAM())
			uvery_had.SHOW()
			SELECT 00 AS poradie,tm_sk_pol.c_karty, SPACE(30) as nazev,tm_sk_pol.mnozstvi,tm_sk_pol.id_kas,tm_sk_pol.id_man,tm_sk_pol.u_cis,tm_sk_pol.uzaverka,tm_sk_pol.prachy,tm_sk_pol.prachy_puv,tm_sk_pol.dan_sazba, tm_sk_pol.dan, $0 AS jc,$0 AS cena_pl,$0 AS cena_plt,$0 as spdan, $0 as objem, $0 as koeficient, $0 as nc, SPACE(50) as stol ;
				FROM tm_sk_pol INTO CURSOR zpol READWRITE ORDER BY tm_sk_pol.uzaverka,tm_sk_pol.u_cis ;
				WHERE !EMPTY(tm_sk_pol.k_odpoctu) AND (tm_sk_pol.uzaverka+tm_sk_pol.u_cis) IN ;
				(SELECT (uzaverka+u_cis) AS uzcis FROM tm_sk_plt WHERE druh_pl IN (SELECT druh_pl FROM vybery WHERE lbl))
			UPDATE zpol SET nazev=karty.nazev, objem=karty.objem, nc=karty.cena_prum from karty WHERE zpol.c_karty=karty.c_karty 
			UPDATE zpol SET nc=0 WHERE (c_karty IN (select c_karty from karty WHERE c_druhk in (select c_druh from skupiny WHERE spart_kas="FST")))
			IF m.spotrebnadan>0
				IF m.issql
					rv("spdan","spdane")
					SELECT spdane
					INDEX on c_karty TAG c_karty
				ELSE 
					IF FILE(m.fn_spotrebnadan+".dbf")
						USE (m.fn_spotrebnadan) ALIAS spdane ORDER c_karty IN 0
					ENDIF 
				ENDIF 
				IF USED("spdane")
					UPDATE zpol SET koeficient=spdane.koeficient from spdane WHERE zpol.c_karty=spdane.c_karty
					USE IN spdane
				ENDIF 
			ENDIF 
			uvery_had.set_had(10)
			SELECT u_cis,uzaverka,SUM(cena_pl) AS cena, 0000.0 as stul, SPACE(30) as  stol, SPACE(30) as miestnost FROM tm_sk_plt INTO CURSOR pplt READWRITE GROUP BY uzaverka,u_cis ;
				WHERE   (uzaverka + id_kas + u_cis) IN ;
				(SELECT (uzaverka + id_kas + u_cis) AS uzcis FROM tm_sk_plt WHERE druh_pl IN (SELECT druh_pl FROM vybery WHERE lbl))
			INDEX ON uzaverka+u_cis TAG aaa
			UPDATE pplt SET stul=tm_sk_fin.stul from tm_sk_fin WHERE pplt.uzaverka=tm_sk_fin.uzaverka AND pplt.u_cis=tm_sk_fin.u_cis
			uvery_had.set_had(20)
			IF m.issql
				TRY 
					IF !USED("stoly")
						USE (m.sqlprefix+"rfoodie_stoly") ALIAS stoly IN 0
					ENDIF 
					=RECCOUNT("stoly")
				CATCH
				ENDTRY 
				IF USED("stoly")
					UPDATE pplt SET miestnost=stoly.miestnost, stol=stoly.stol from stoly WHERE pplt.stul=stoly.id
				ENDIF 
				DO pozavirej WITH "stoly"
			ENDIF 
			REPLACE stol WITH ALLTRIM(STR(stul)) FOR EMPTY(stol)
			SELECT id_kas,u_cis,uzaverka,cena_pl,00 AS poradie,druh_pl FROM tm_sk_plt INTO CURSOR pplt1 READWRITE ORDER BY uzaverka,u_cis ;
                WHERE   (uzaverka + id_kas + u_cis) IN ;
				(SELECT (uzaverka + id_kas + u_cis) AS uzcis FROM tm_sk_plt WHERE druh_pl IN (SELECT druh_pl FROM vybery WHERE lbl))
			INDEX ON uzaverka+u_cis TAG aaa
			INDEX ON id_kas+uzaverka+u_cis TAG kasuzuc
			uvery_had.set_had(30)
			GO TOP
			m.ucis=SPACE(6)
			m.uzav=SPACE(12)
			m.por=1
			SCAN ALL
				IF u_cis=m.ucis .AND. uzaverka=m.uzav
					m.por=m.por+1
				ELSE
					m.por=1
				ENDIF
				REPLACE poradie WITH m.por
				m.ucis=u_cis
				m.uzav=uzaverka
			ENDSCAN
			uvery_had.set_had(40)
			DELETE FOR LEFT(pplt1.druh_pl,6)<>UPPER("n_uver")
			SET RELATION TO uzaverka+u_cis+STR(poradie,2,0) INTO tm_sk_txt
			DELETE FOR ALLTRIM(tm_sk_txt.hjmeno)<>ALLTRIM(m.hjmeno)
			SET RELATION TO
			uvery_had.set_had(50)
			SET ORDER TO aaa
			SELECT zpol
			SET RELATION TO uzaverka+u_cis INTO pplt
			REPLACE ALL cena_plt WITH pplt.cena
			REPLACE stol WITH pplt.stol FOR EMPTY(pplt.miestnost)
			REPLACE stol WITH ALLTRIM(pplt.miestnost)+" / "+ALLTRIM(pplt.stol) FOR !EMPTY(pplt.miestnost)
			SET RELATION TO
			uvery_had.set_had(60)
			SET RELATION TO uzaverka+u_cis INTO pplt1
			REPLACE ALL cena_pl WITH pplt1.cena_pl, poradie WITH pplt1.poradie
			DELETE FOR EMPTY(pplt1.u_cis)
			SET RELATION TO
			uvery_had.set_had(70)
			REPLACE ALL jc WITH IIF(mnozstvi=0,0,prachy/mnozstvi)
			uvery_had.set_had(80)
*			REPLACE ALL jc WITH IIF(prachy_puv=0,0,ROUND(jc*prachy/prachy_puv,2))
			uvery_had.set_had(90)
			REPLACE ALL mnozstvi WITH IIF(cena_plt=0,mnozstvi,mnozstvi*cena_pl/cena_plt)
			REPLACE ALL prachy WITH ROUND(mnozstvi*jc,2)
			uvery_had.set_had(100)
			SELECT zpol
			IF MESSAGEBOX(jaz("form_tlacit_report_po_man"),4+32+256,"FoodMan")<>6
				INDEX ON id_kas+uzaverka+u_cis TAG kasuzuc
				RELEASE uvery_had
				SET RELATION TO uzaverka+u_cis+STR(poradie,2,0) INTO tm_sk_txt
				DO pr_report WITH 'trz_uvery5', !m.reporty_volane_z_formu
				SET RELATION TO
			ELSE
				INDEX ON id_kas+id_man+uzaverka+u_cis TAG kasuzuc
				RELEASE uvery_had
				SET RELATION TO uzaverka+u_cis+STR(poradie,2,0) INTO tm_sk_txt
				DO pr_report WITH 'trz_uvery5_1', !m.reporty_volane_z_formu
				SET RELATION TO
			ENDIF 
			DO pozavirej WITH "zpol, pplt1, pplt"
		ENDDO
	ENDIF
	IF "3"$m.bar
		DO WHILE .T.
			m.hjmeno=""
			IF m.reporty_volane_z_formu
				DO FORM dajdruh WITH "","druhy","hjmeno",200 TO m.hjmeno
			ENDIF 
			IF EMPTY(m.hjmeno)
				EXIT
			ENDIF
			SELECT druhy
			LOCATE FOR ALLTRIM(druhy.hjmeno)==ALLTRIM(m.hjmeno)
			m.hjmeno=druhy.hjmeno
			m.akce=druhy.akce
			SELECT uvery
			LOCATE FOR ALLTRIM(hjmeno)==ALLTRIM(m.hjmeno)
			IF FOUND()
				SCATTER MEMVAR
			ELSE
				SCATTER MEMVAR BLANK FIELDS adresa1, adresa2, adresa3, dic, txt, akce
			ENDIF
			uvery_had = CREATEOBJECT("had", PROGRAM())
			uvery_had.SHOW()
			SELECT tm_sk_pol.c_karty, SPACE(30) as nazev,tm_sk_pol.mnozstvi,tm_sk_pol.id_kas,tm_sk_pol.id_man,tm_sk_pol.u_cis,tm_sk_pol.uzaverka,tm_sk_pol.prachy,tm_sk_pol.prachy_puv, $0 AS jc,$0 AS cena_pl,$0 AS cena_plt, $0 as nc FROM tm_sk_pol INTO CURSOR zpol READWRITE ORDER BY tm_sk_pol.uzaverka,tm_sk_pol.u_cis ;
				HAVING !EMPTY(tm_sk_pol.k_odpoctu) WHERE (tm_sk_pol.uzaverka+tm_sk_pol.u_cis) IN ;
				(SELECT (uzaverka+u_cis) AS uzcis FROM tm_sk_plt WHERE druh_pl IN (SELECT druh_pl FROM vybery WHERE lbl))
			UPDATE zpol SET nazev=karty.nazev, nc=karty.cena_prum from karty WHERE zpol.c_karty=karty.c_karty			
			uvery_had.set_had(10)
			SELECT u_cis,uzaverka,SUM(cena_pl) AS cena FROM tm_sk_plt INTO CURSOR pplt READWRITE GROUP BY uzaverka,u_cis ;
				WHERE (tm_sk_plt.uzaverka+tm_sk_plt.u_cis) IN ;
				(SELECT (uzaverka+u_cis) AS uzcis FROM tm_sk_plt WHERE druh_pl IN (SELECT druh_pl FROM vybery WHERE lbl))
			INDEX ON uzaverka+u_cis TAG aaa
			uvery_had.set_had(20)
			SELECT LEFT(u_cis,2) AS id_kas,u_cis,uzaverka,cena_pl,00 AS poradie,druh_pl FROM tm_sk_plt INTO CURSOR pplt1 READWRITE ORDER BY uzaverka,u_cis ;
				WHERE (tm_sk_plt.uzaverka+tm_sk_plt.u_cis) IN ;
				(SELECT (uzaverka+u_cis) AS uzcis FROM tm_sk_plt WHERE druh_pl IN (SELECT druh_pl FROM vybery WHERE lbl))
			INDEX ON uzaverka+u_cis TAG aaa
			INDEX ON id_kas+uzaverka+u_cis TAG kasuzuc
			uvery_had.set_had(30)
			GO TOP
			m.ucis=SPACE(6)
			m.uzav=SPACE(12)
			m.por=1
			SCAN ALL
				IF u_cis=m.ucis .AND. uzaverka=m.uzav
					m.por=m.por+1
				ELSE
					m.por=1
				ENDIF
				REPLACE poradie WITH m.por
				m.ucis=u_cis
				m.uzav=uzaverka
			ENDSCAN
			uvery_had.set_had(40)
			DELETE FOR LEFT(pplt1.druh_pl,6)<>UPPER("n_uver")
			SET RELATION TO uzaverka+u_cis+STR(poradie,2,0) INTO tm_sk_txt
			DELETE FOR ALLTRIM(tm_sk_txt.hjmeno)<>ALLTRIM(m.hjmeno)
			SET RELATION TO
			uvery_had.set_had(50)
			SET ORDER TO aaa
			SELECT zpol
			SET RELATION TO uzaverka+u_cis INTO pplt
			REPLACE ALL cena_plt WITH pplt.cena
			SET RELATION OFF INTO pplt
			uvery_had.set_had(60)
			SET RELATION TO uzaverka+u_cis INTO pplt1
			REPLACE ALL cena_pl WITH pplt1.cena_pl
			DELETE FOR EMPTY(pplt1.u_cis)
			SET RELATION OFF INTO pplt1
			uvery_had.set_had(70)
			USE IN pplt1
			USE IN pplt
			REPLACE ALL jc WITH IIF(mnozstvi=0,0,prachy/mnozstvi)
			REPLACE ALL jc WITH IIF(prachy_puv=0,0,ROUND(jc*prachy/prachy_puv,2))
			uvery_had.set_had(80)
			REPLACE ALL mnozstvi WITH IIF(cena_plt=0,mnozstvi,mnozstvi*cena_pl/cena_plt)
			REPLACE ALL prachy WITH ROUND(mnozstvi*jc,2)
			REPLACE ALL nc WITH ROUND(mnozstvi*nc,2)
			uvery_had.set_had(90)
			SELECT nazev, SUM(mnozstvi) AS kolko, SUM(prachy) AS prachy, jc, id_kas, id_man, SUM(nc) as nc FROM zpol INTO CURSOR asdf READWRITE GROUP BY id_kas,id_man,jc,nazev ORDER BY id_kas, id_man, nazev
			uvery_had.set_had(100)
			DELETE FOR kolko=0
			RELEASE uvery_had
			DO pr_report WITH "trz_spotr_u", !m.reporty_volane_z_formu
			USE IN asdf
			SELECT nazev, SUM(mnozstvi) AS kolko, SUM(prachy) AS prachy, jc, "" as id_kas, "" AS id_man, SUM(nc) as nc FROM zpol INTO CURSOR asdf READWRITE GROUP BY id_kas,id_man,jc,nazev ORDER BY id_kas, id_man, nazev
			DELETE FOR kolko=0
			DO pr_report WITH "trz_spotr_u@b", !m.reporty_volane_z_formu
			USE IN asdf
			USE IN zpol
		ENDDO
	ENDIF
	IF "4"$m.bar
		uvery_had = CREATEOBJECT("had", PROGRAM())
		uvery_had.SHOW()
		SELECT LEFT(u_cis,2) AS id_kas,u_cis,uzaverka,cena_pl,00 AS poradie,druh_pl, SPACE(15) AS hjmeno, 000000000.00 AS trzba_dph1, 000000000.00 AS trzba_dph2,;
			000000000.00 AS trzba_dph3, 000000000.00 AS trzba_dph4, 00.00 AS dph1, 00.00 AS dph2, 00.00 AS dph3, 00.00 AS dph4, 00000.00 AS obedov;
			FROM tm_sk_plt INTO CURSOR zplt READWRITE ORDER BY uzaverka,u_cis WHERE druh_pl IN (SELECT druh_pl FROM vybery WHERE lbl)
		INDEX ON id_kas+uzaverka+u_cis TAG kasuzuc
		uvery_had.set_had(10)
		GO TOP
		m.ucis=SPACE(6)
		m.uzav=SPACE(12)
		m.por=1
		SCAN ALL
			IF u_cis=m.ucis .AND. uzaverka=m.uzav
				m.por=m.por+1
			ELSE
				m.por=1
			ENDIF
			REPLACE poradie WITH m.por
			m.ucis=u_cis
			m.uzav=uzaverka
		ENDSCAN
		DELETE FOR LEFT(zplt.druh_pl,6)<>UPPER("n_uver")
		uvery_had.set_had(20)
		SET RELATION TO uzaverka+u_cis+STR(poradie,2,0) INTO tm_sk_txt
		REPLACE ALL zplt.hjmeno WITH tm_sk_txt.hjmeno
		SELECT 0
		CREATE CURSOR dph (dph N(5,2))
		SELECT zplt
		SCAN ALL
			SELECT tm_sk_pol
			SUM(mnozstvi) FOR u_cis=zplt.u_cis .AND. uzaverka=zplt.uzaverka TO m.obedov
			SELECT zplt
			REPLACE obedov WITH m.obedov
			SELECT tm_sk_fin
			SET ORDER TO uz_ucet
			IF SEEK(zplt.uzaverka+zplt.u_cis)
				SELECT zplt
				IF (tm_sk_fin.cena_celk-tm_sk_fin.sleva_abs)<>0
					IF tm_sk_fin.trzba1<>0
						*m.xxx=ktory(tm_sk_fin.dan1)
						REPLACE zplt.trzba_dph1 WITH tm_sk_fin.trzba1*zplt.cena_pl/(tm_sk_fin.cena_celk-tm_sk_fin.sleva_abs), zplt.dph1 WITH m.s_dph1
					ENDIF
					IF tm_sk_fin.trzba2<>0
						*m.xxx=ktory(tm_sk_fin.dan2)
						REPLACE zplt.trzba_dph2 WITH tm_sk_fin.trzba2*zplt.cena_pl/(tm_sk_fin.cena_celk-tm_sk_fin.sleva_abs), zplt.dph2 WITH m.s_dph2
					ENDIF
					IF tm_sk_fin.trzba3<>0
						*m.xxx=ktory(tm_sk_fin.dan3)
						REPLACE zplt.trzba_dph3 WITH tm_sk_fin.trzba3*zplt.cena_pl/(tm_sk_fin.cena_celk-tm_sk_fin.sleva_abs), zplt.dph3 WITH m.s_dph3
					ENDIF
					IF tm_sk_fin.trzba4<>0
						*m.xxx=ktory(tm_sk_fin.dan4)
						REPLACE zplt.trzba_dph4 WITH tm_sk_fin.trzba4*zplt.cena_pl/(tm_sk_fin.cena_celk-tm_sk_fin.sleva_abs), zplt.dph4 WITH m.s_dph4
					ENDIF
				ELSE
					IF tm_sk_fin.trzba1<>0
						*m.xxx=ktory(tm_sk_fin.dan1)
						REPLACE zplt.trzba_dph1 WITH tm_sk_fin.trzba1, zplt.dph1 WITH m.s_dph1
					ENDIF
					IF tm_sk_fin.trzba2<>0
						*m.xxx=ktory(tm_sk_fin.dan2)
						REPLACE zplt.trzba_dph2 WITH tm_sk_fin.trzba2, zplt.dph2 WITH m.s_dph2
					ENDIF
					IF tm_sk_fin.trzba3<>0
						*m.xxx=ktory(tm_sk_fin.dan3)
						REPLACE zplt.trzba_dph3 WITH tm_sk_fin.trzba3, zplt.dph3 WITH m.s_dph3
					ENDIF
					IF tm_sk_fin.trzba4<>0
						*m.xxx=ktory(tm_sk_fin.dan4)
						REPLACE zplt.trzba_dph4 WITH tm_sk_fin.trzba4, zplt.dph4 WITH m.s_dph4
					ENDIF
				ENDIF
			ENDIF
			SELECT tm_sk_fin
		ENDSCAN
		uvery_had.set_had(40)
		SELECT zplt
		LOCATE FOR !EMPTY(dph1)
		IF FOUND()
			m.xxx=zplt.dph1
			REPLACE ALL dph1 WITH m.xxx
		ENDIF
		LOCATE FOR !EMPTY(dph2)
		IF FOUND()
			m.xxx=zplt.dph2
			REPLACE ALL dph2 WITH m.xxx
		ENDIF
		LOCATE FOR !EMPTY(dph3)
		IF FOUND()
			m.xxx=zplt.dph3
			REPLACE ALL dph3 WITH m.xxx
		ENDIF
		LOCATE FOR !EMPTY(dph4)
		IF FOUND()
			m.xxx=zplt.dph4
			REPLACE ALL dph4 WITH m.xxx
		ENDIF
		LOCATE FOR !EMPTY(dph5)
		IF FOUND()
			m.txt="Nesuhlasia trzby podla DPH. Vo vybranom obdobi bolo prilis vela sadzieb."
		ELSE
			m.txt=""
		ENDIF
		uvery_had.set_had(50)
		SELECT SUM(cena_pl) AS suma, 0000 AS pocet_u, hjmeno, SUM(trzba_dph1) AS trzba_dph1, SUM(trzba_dph2) AS trzba_dph2,SUM(obedov) AS obedov,;
			SUM(trzba_dph3) AS trzba_dph3, SUM(trzba_dph4) AS trzba_dph4,dph1,dph2,dph3,dph4 FROM zplt INTO CURSOR odplt READWRITE;
			GROUP BY hjmeno
		uvery_had.set_had(60)
		PRIVATE hjmn, pocet_u
		SCAN ALL
			m.hjmn=odplt.hjmeno
			SELECT zplt
			COUNT TO m.pocet_u FOR hjmeno=m.hjmn
			SELECT odplt
			REPLACE pocet_u WITH m.pocet_u
		ENDSCAN
		uvery_had.set_had(100)
		RELEASE uvery_had
		DO pr_report WITH "trz_uvery4", !m.reporty_volane_z_formu
		DO pr_report WITH "trz_uvery6", !m.reporty_volane_z_formu
		USE IN odplt
		USE IN zplt
		RELEASE m.pocet_u, m.hjmn
		USE IN dph
	ENDIF
	USE IN druhy
	USE IN uvery
ELSE
	=MESSAGEBOX("UVERY.DBF ešte neexistuje",64,"FoodMan")
ENDIF


*!*****************************************************************************
*!      Function:KTORY
*!*****************************************************************************
FUNCTION ktory
PARAMETERS kt
PRIVATE ret
SELECT dph
LOCATE FOR dph=m.kt
IF FOUND()
	m.ret=RECNO()
ELSE
	INSERT INTO dph (dph) VALUES (m.kt)
	SELECT dph
	LOCATE FOR dph=m.kt
	m.ret=RECNO()
ENDIF
IF m.ret>4
	m.ret=5
ENDIF
SELECT zplt
RETURN STR(m.ret,1,0)


*!*****************************************************************************
*!      Procedure: S_MES
*!*****************************************************************************
PROCEDURE s_mes
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
SELECT id_kas, cen_hlad, spart, SUM(sumpc) AS prachy, ROUND(SUM(sumpc-sum_dph), m.girep_round) AS vynos, ;
	$0 AS prachy1, $0 AS prachy2, $0 AS prachy3, $0 AS prachy4, $0 AS prachy5, $0 AS prachy6, $0 AS prachy7, $0 AS prachy8, $0 AS prachy9, ;
	$0 AS vynos1,  $0 AS vynos2,  $0 AS vynos3,  $0 AS vynos4,  $0 AS vynos5,  $0 AS vynos6,  $0 AS vynos7,  $0 AS vynos8,  $0 AS vynos9, SUM(round50) AS round50 ;
	GROUP BY id_kas, cen_hlad, spart ;
	FROM pri_buff INTO CURSOR sk_hl2 READWRITE 

REPLACE ALL prachy1 WITH prachy, vynos1 WITH vynos
REPLACE prachy1 WITH 0, vynos1 WITH 0,prachy9 WITH prachy, vynos9 WITH vynos FOR cen_hlad="9"
REPLACE prachy1 WITH 0, vynos1 WITH 0,prachy8 WITH prachy, vynos8 WITH vynos FOR cen_hlad="8"
REPLACE prachy1 WITH 0, vynos1 WITH 0,prachy7 WITH prachy, vynos7 WITH vynos FOR cen_hlad="7"
REPLACE prachy1 WITH 0, vynos1 WITH 0,prachy6 WITH prachy, vynos6 WITH vynos FOR cen_hlad="6"
REPLACE prachy1 WITH 0, vynos1 WITH 0,prachy5 WITH prachy, vynos5 WITH vynos FOR cen_hlad="5"
REPLACE prachy1 WITH 0, vynos1 WITH 0,prachy4 WITH prachy, vynos4 WITH vynos FOR cen_hlad="4"
REPLACE prachy1 WITH 0, vynos1 WITH 0,prachy3 WITH prachy, vynos3 WITH vynos FOR cen_hlad="3"
REPLACE prachy1 WITH 0, vynos1 WITH 0,prachy2 WITH prachy, vynos2 WITH vynos FOR cen_hlad="2"
SELECT id_kas, spart, SUM(prachy) AS prachy, SUM(vynos) AS vynos, ;
	SUM(prachy1) AS prachy1, SUM(vynos1) AS vynos1, SUM(prachy2) AS prachy2, SUM(vynos2) AS vynos2, ;
	SUM(prachy3) AS prachy3, SUM(vynos3) AS vynos3, SUM(prachy4) AS prachy4, SUM(vynos4) AS vynos4, ;
	SUM(prachy5) AS prachy5, SUM(vynos5) AS vynos5, SUM(prachy6) AS prachy6, SUM(vynos6) AS vynos6, ;
	SUM(prachy7) AS prachy7, SUM(vynos7) AS vynos7, SUM(prachy8) AS prachy8, SUM(vynos8) AS vynos8, ;
	SUM(prachy9) AS prachy9, SUM(vynos9) AS vynos9 , SUM(round50) AS round50;
	GROUP BY id_kas, spart FROM sk_hl2 INTO CURSOR sk_hl3 READWRITE 
*Trzby
DO pr_report WITH "trz_hlad", !m.reporty_volane_z_formu
*Vynos
*REPL ALL prachy1 WITH vynos1, prachy2 WITH vynos2, prachy3 WITH vynos3, prachy4 WITH vynos4, prachy5 WITH vynos5, prachy6 WITH vynos6, prachy7 WITH vynos7, prachy8 WITH vynos8, prachy9 WITH vynos9
*DO pr_report WITH "trz_hlad"
DO pozavirej WITH "sk_hl2, sk_hl3"


*!*****************************************************************************
*!      Procedure: S_PREDAJ
*!*****************************************************************************
PROCEDURE s_predaj
PARAMETERS repriadok
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
IF !USED("druhy_pl")
	SELECT 0
	IF m.issql 
		rv("druhy_pl","druhy_pl")
	ELSE 
		USE (m.gchom_path+"druhy_pl")
	ENDIF 
ENDIF 
IF "1"$m.repriadok
	DO pozavirej WITH "buff"
	DO tvor_dph1 IN sk_stati
	SELECT * , 0.00000 AS koef FROM dph1 INTO CURSOR dph11 READWRITE 
	SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_pl, dan FROM dph1 INTO CURSOR dph111 READWRITE GROUP BY uzaverka, u_cis, dan
	INDEX ON uzaverka+u_cis+STR(dan,5,2) TAG uzuc
	SELECT dph11
	SET RELATION TO uzaverka+u_cis+STR(dan,5,2) INTO dph111
	REPLACE koef WITH 1 FOR dph111.cena_pl=0
	REPLACE koef WITH dph11.cena_pl/dph111.cena_pl FOR dph111.cena_pl<>0
	USE IN dph111
	SELECT pri_buff.*,dph11.koef,dph11.druh_pl AS druh_plat FROM pri_buff, dph11 WHERE pri_buff.uzaverka=dph11.uzaverka .AND. pri_buff.u_cis=dph11.u_cis .AND. pri_buff.dan=dph11.dan;
		INTO CURSOR pribuff READWRITE 
	SELECT * FROM pribuff WHERE koef<>1 INTO CURSOR pribuff1 READWRITE 
	REPLACE ALL mnozstvi WITH mnozstvi*(koef)
	REPLACE sumpc WITH sumpc*(koef) ALL
	REPLACE sum_dph WITH sum_dph*(koef) ALL
	SELECT pribuff
	DELETE FOR koef<>1
	INSERT INTO pribuff (id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
		sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat, k_odpoctu) ;
		SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
		sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat, k_odpoctu FROM pribuff1
	USE IN pribuff1
	
	IF MESSAGEBOX(jaz("form_report_po_uzavierkach"),4+32+256,"")=6
		SELECT nazev AS zkratka, SUM(summc) AS summc,SUM(sum_dph) AS sum_dph,;
			SUM(sumpc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, id_kas, id_man, druh,druh_plat, uzaverka, SUM(round50) as round50, k_odpoctu;
			FROM pribuff INTO CURSOR buff READWRITE;
			GROUP BY id_kas, uzaverka, druh_plat, id_man, druh, zkratka;
			ORDER BY id_kas, uzaverka, druh_plat, id_man, druh, zkratka
	ELSE
		SELECT * FROM pribuff INTO CURSOR pribuffpom READWRITE 
		REPLACE ALL uzaverka WITH "Spolu"
		SELECT nazev AS zkratka, SUM(summc) AS summc,SUM(sum_dph) AS sum_dph,;
			SUM(sumpc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, id_kas, id_man, druh,druh_plat, uzaverka, SUM(round50) as round50, k_odpoctu;
			FROM pribuffpom INTO CURSOR buff READWRITE;
			GROUP BY id_kas, uzaverka, druh_plat, id_man, druh, zkratka;
			ORDER BY id_kas, uzaverka, druh_plat, id_man, druh, zkratka
		USE IN pribuffpom
	ENDIF 	
	IF MESSAGEBOX(jaz("form_report_po_druhoch"),4+32+256,"")=6
		SELECT DISTINCT druh,.T. AS lbl, .f. as kalkulacia FROM BUFF INTO CURSOR vybery READWRITE 
		UPDATE vybery SET kalkulacia=!EMPTY(skupiny.typ_kalk) from skupiny WHERE vybery.druh=skupiny.druh 
		INDEX ON druh TAG druh
		IF RECCOUNT()>0
			m.gcfilter_triedenia=""
			IF m.reporty_volane_z_formu
				DO FORM seldruhy WITH "vybery" TO m.druhxx
				filtrujpriznaky("buff")
			ENDIF 
			RELEASE m.gcfilter_triedenia
			SELECT vybery
			LOCATE FOR !EMPTY(lbl)
			IF FOUND()
				SELECT BUFF
				SET RELATION TO druh INTO vybery
				DELETE FOR ALLTRIM(druh)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
				IF !EMPTY(m.druhxx)
					DELETE FOR EMPTY(mnozstvi)
				ENDIF 
				SET RELATION OFF INTO vybery
				DO pr_report WITH "trz_m_kalplt", !m.reporty_volane_z_formu
			ENDIF
		ENDIF
	ELSE
		SELECT buff 
		DO pr_report WITH "trz_m_kalplt_bezdruhov", !m.reporty_volane_z_formu
	ENDIF 
	
	DO pozavirej WITH "pribuff, vybery, buff, dph11, dph111"
ENDIF

IF "2"$m.repriadok
	DO pozavirej WITH "buff, vybery"
	SELECT nazev AS zkratka, SUM(summc) AS summc,SUM(sum_dph) AS sum_dph,;
		SUM(sumpc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, id_kas, druh, AUTOR, SUM(round50) as round50;
		FROM pri_buff INTO CURSOR buff READWRITE;
		GROUP BY AUTOR, id_kas, zkratka
	SELECT DISTINCT zkratka AS druh,.T. AS lbl FROM BUFF INTO CURSOR vybery READWRITE 
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
		ENDIF 
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO zkratka INTO vybery
			DELETE FOR ALLTRIM(zkratka)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			DO pr_report WITH "trz_m_kalk_cisnik", !m.reporty_volane_z_formu
		ENDIF
	ENDIF
	DO pozavirej WITH "vybery, buff"
ENDIF

IF "3"$m.repriadok
	DO pozavirej WITH "buff"
	SELECT tm_sk_pol.id_kas, tm_sk_pol.uzaverka, karty.nazev,tm_sk_pol.c_karty, SUM(tm_sk_pol.prachy) as prachy, SUM(tm_sk_pol.mnozstvi) as mnozstvi, tm_sk_pol.cen_hlad ;
           FROM tm_sk_pol LEFT JOIN karty ON tm_sk_pol.c_karty=karty.c_karty WHERE EMPTY(tm_sk_pol.k_odpoctu) .or. tm_sk_pol.k_odpoctu="F" GROUP BY tm_sk_pol.id_kas, tm_sk_pol.uzaverka, tm_sk_pol.c_karty, tm_sk_pol.cen_hlad INTO CURSOR buff
	DO pr_report WITH "trz_predmenu", !m.reporty_volane_z_formu
	DO pozavirej WITH "buff"
ENDIF 

IF "4"$m.repriadok
	DO pozavirej WITH "pribuff, vybery, buff, dph11, dph111"
	DO tvor_dph1 IN sk_stati
	SELECT * , 0.00000 AS koef FROM dph1 INTO CURSOR dph11 READWRITE 
	SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_pl, dan FROM dph1 INTO CURSOR dph111 READWRITE GROUP BY uzaverka, u_cis, dan
	INDEX ON uzaverka+u_cis+STR(dan,5,2) TAG uzuc
	SELECT dph11
	SET RELATION TO uzaverka+u_cis+STR(dan,5,2) INTO dph111
	REPLACE koef WITH 1 FOR dph111.cena_pl=0
	REPLACE koef WITH dph11.cena_pl/dph111.cena_pl FOR dph111.cena_pl<>0
	USE IN dph111
	SELECT pri_buff.*,dph11.koef,dph11.druh_pl AS druh_plat FROM pri_buff, dph11 WHERE pri_buff.uzaverka=dph11.uzaverka .AND. pri_buff.u_cis=dph11.u_cis .AND. pri_buff.dan=dph11.dan;
		INTO CURSOR pribuff READWRITE 
	SELECT * FROM pribuff WHERE koef<>1 INTO CURSOR pribuff1 READWRITE 
	REPLACE ALL mnozstvi WITH mnozstvi*(koef)
	REPLACE sumpc WITH sumpc*(koef) ALL
	REPLACE sum_dph WITH sum_dph*(koef) ALL
	SELECT pribuff
	DELETE FOR koef<>1
	INSERT INTO pribuff (id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
		sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat) ;
		SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
		sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat FROM pribuff1
	USE IN pribuff1
	SELECT nazev AS zkratka, SUM(summc) AS summc,SUM(sum_dph) AS sum_dph,;
		SUM(sumpc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, id_kas, druh, druh_plat, SUM(round50) as round50;
		FROM pribuff INTO CURSOR buff READWRITE;
		GROUP BY id_kas, zkratka;
		ORDER BY zkratka, id_kas
	
	SELECT DISTINCT druh,.T. AS lbl, .f. as kalkulacia FROM BUFF INTO CURSOR vybery READWRITE 
	UPDATE vybery SET kalkulacia=!EMPTY(skupiny.typ_kalk) from skupiny WHERE vybery.druh=skupiny.druh 
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery" TO m.druhxx
			filtrujpriznaky("buff")
		ENDIF 
		RELEASE m.gcfilter_triedenia
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO druh INTO vybery
			DELETE FOR ALLTRIM(druh)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			DO pr_report WITH "trz_m_kalkas", !m.reporty_volane_z_formu
		ENDIF
	ENDIF
	DO pozavirej WITH "pribuff, vybery, buff, dph11, dph111"
ENDIF
IF "5"$m.repriadok
	IF MESSAGEBOX(jaz("s_sk_trz_po_dnoch")+"?",32+4+256)=6
		SELECT nazev, druh, SUM(sumpc) AS prachy, SUM(mnozstvi) AS mnozstvi,id_kas, dan, pl_cas, den, SUM(round50) as round50 FROM pri_Buff INTO CURSOR pom_buff GROUP BY den, nazev, id_kas, pl_cas;
			ORDER BY id_kas, den, pl_cas READWRITE 
	ELSE
		SELECT nazev, druh, SUM(sumpc) AS prachy, SUM(mnozstvi) AS mnozstvi,id_kas, dan, pl_cas, SPACE(8) as den, SUM(round50) as round50 FROM pri_Buff INTO CURSOR pom_buff GROUP BY den, nazev, id_kas, pl_cas;
			ORDER BY id_kas, den, pl_cas READWRITE 
	ENDIF 
	SELECT DISTINCT druh,.T. AS lbl, .f. as kalkulacia FROM pom_buff INTO CURSOR vybery READWRITE 
	UPDATE vybery SET kalkulacia=!EMPTY(skupiny.typ_kalk) from skupiny WHERE vybery.druh=skupiny.druh 
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery" TO m.druhxx
			filtrujpriznaky("pom_buff")
		ENDIF 
		RELEASE m.gcfilter_triedenia
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT pom_buff
			SET RELATION TO druh INTO vybery
			DELETE FOR ALLTRIM(druh)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			USE IN vybery
			SELECT DISTINCT nazev as druh, .T. AS lbl FROM pom_buff INTO CURSOR vybery READWRITE 
			INDEX ON druh TAG druh
			IF RECCOUNT()>0
				IF m.reporty_volane_z_formu
					DO FORM seldruhy WITH "vybery","druhpl",200,.t. TO m.druhxx
				ENDIF 
				SELECT vybery
				LOCATE FOR !EMPTY(lbl)
				IF FOUND()
					SELECT pom_buff
					SET RELATION TO nazev INTO vybery
					DELETE FOR ALLTRIM(nazev)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
					SET RELATION OFF INTO vybery
					SELECT nazev, druh, den, SUM(prachy) AS prachy, SUM(mnozstvi) AS mnozstvi, id_kas, dan, LEFT(pl_cas,2) AS pl_hodina, SUM(round50) as round50;
						FROM pom_buff INTO CURSOR cas_kalk1 GROUP BY nazev, den, id_kas, pl_hodina;
						ORDER BY nazev, den, id_kas, pl_hodina
					SELECT nazev, id_kas, den, druh, $0 as c00, $0 as s00, $0 as c01, $0 as s01, $0 as c02, $0 as s02, $0 as c03, $0 as s03, $0 as c04, $0 as s04, $0 as c05, $0 as s05, $0 as c06, $0 as s06, $0 as c07, $0 as s07, $0 as c08, $0 as s08, $0 as c09, $0 as s09, $0 as c10, $0 as s10, $0 as c11, $0 as s11, $0 as c12, $0 as s12,;
						 $0 as c13, $0 as s13, $0 as c14, $0 as s14, $0 as c15, $0 as s15, $0 as c16, $0 as s16, $0 as c17, $0 as s17, $0 as c18, $0 as s18, $0 as c19, $0 as s19, $0 as c20, $0 as s20, $0 as c21, $0 as s21, $0 as c22, $0 as s22, $0 as c23, $0 as s23, $0 as cp, $0 as sp FROM cas_kalk1 INTO CURSOR cas_kalk WHERE .f. READWRITE 
					INDEX on nazev+id_kas+den TAG aaa
					SELECT cas_kalk1
					SCAN ALL 
						SELECT cas_kalk
						IF !SEEK(cas_kalk1.nazev+cas_kalk1.id_kas+cas_kalk1.den)
							INSERT INTO cas_kalk (nazev, id_kas, druh, den) VALUES (cas_kalk1.nazev, cas_kalk1.id_kas, cas_kalk1.druh, cas_kalk1.den)
						ENDIF 
						m.plhodina=PADL(ALLTRIM(cas_kalk1.pl_hodina),2,"0")
						IF EMPTY(m.plhodina) OR ISNULL(m.plhodina) OR VAL(plhodina)>23
							m.plhodina="00"
						ENDIF 
						REPLACE cas_kalk.c&plhodina WITH cas_kalk.c&plhodina+cas_kalk1.mnozstvi, cas_kalk.s&plhodina WITH cas_kalk.s&plhodina+cas_kalk1.prachy, cas_kalk.cp WITH cas_kalk.cp+cas_kalk1.mnozstvi, cas_kalk.sp WITH cas_kalk.sp+cas_kalk1.prachy
						SELECT cas_kalk1						
					ENDSCAN 
					SELECT cas_kalk
					INDEX on den+druh+nazev+id_kas TAG aab
					DO pr_report WITH "trz_i_nazev_kascas", !m.reporty_volane_z_formu
					USE IN cas_kalk
				ENDIF 
			ENDIF 
		ENDIF
	ENDIF
	DO pozavirej WITH "pom_buff, cas_kalk, caskalk, cas_kalk1, vybery"
ENDIF


*!*****************************************************************************
PROCEDURE s_casnik
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
trzcis_had = CREATEOBJECT("had", PROGRAM())
trzcis_had.SHOW()
SELECT uzaverka, id_kas, autor, druh_pl, SUM(cena_pl) AS trzba, SUM(tip) AS tip ;
	GROUP BY autor, id_kas, uzaverka, druh_pl FROM tm_sk_plt INTO CURSOR buff1 READWRITE 
trzcis_had.HIDE()
IF "1"$m.repriadok
	SELECT AUTOR, SUM(trzba) AS trzba, SUM(tip) AS tip, druh_pl;
		GROUP BY druh_pl, AUTOR FROM buff1;
		INTO CURSOR buff READWRITE ORDER BY druh_pl, AUTOR
	DELETE FOR trzba=0
	DO pr_report WITH "trz_i_cisnik_platba1", !m.reporty_volane_z_formu
ENDIF
IF "2"$m.repriadok
	SELECT AUTOR, id_kas, SUM(trzba) AS trzba, SUM(tip) AS tip, druh_pl;
		GROUP BY AUTOR,druh_pl, id_kas FROM buff1;
		INTO CURSOR buff READWRITE ORDER BY AUTOR,id_kas,druh_pl
	DELETE FOR trzba=0
	DO pr_report WITH "trz_i_cisnik_platba", !m.reporty_volane_z_formu
ENDIF
trzcis_had.RELEASE()
USE IN BUFF


*!*****************************************************************************
*!      Procedure: UCTY1
*!*****************************************************************************
PROCEDURE ucty1
PRIVATE ucis,uzav,por,druh
SELECT DISTINCT druh_pl,SPACE(20) AS druh FROM tm_sk_plt INTO CURSOR druhy READWRITE 
REPLACE ALL druh WITH i_drupla(druh_pl)
INDEX ON druh TAG druh
m.txthlavicka=""
IF RECCOUNT()>0
	DO WHILE .T.
		m.druh=""
		IF m.reporty_volane_z_formu
			DO FORM dajdruh WITH "","druhy","druh",200 TO m.druh
		ENDIF 
		IF EMPTY(m.druh)
			EXIT
		ENDIF
		SELECT druhy
		LOCATE FOR druh=m.druh
		m.druh=druh_pl
		SELECT SPACE(30) AS zostava,.F. AS lbl INTO CURSOR selzost FROM tm_sk_plt WHERE .f. READWRITE 
		INSERT INTO selzost (zostava,lbl) VALUES (jaz("m_s5_sk_ucty_platby_prehlad"),.F.)
		INSERT INTO selzost (zostava,lbl) VALUES (jaz("m_s5_sk_ucty_platby_prehladsp"),.F.)
		INSERT INTO selzost (zostava,lbl) VALUES (jaz("m_s5_sk_ucty_platby_sumar"),.F.)
		INSERT INTO selzost (zostava,lbl) VALUES (jaz("m_s5_sk_ucty_platby_prehladho"),.F.)
		IF "N_UVER"$m.druh
			INSERT INTO selzost (zostava,lbl) VALUES (jaz("m_s5_sk_uvery_sparty_dph"),.F.)
		ENDIF 
		
		DO FORM selpartn WITH "selzost","zostava"
		SELECT selzost
		SCAN FOR !EMPTY(lbl)
			m.barlbl=RECNO()
			DO CASE
				CASE m.barlbl=1
					SELECT id_kas, u_cis, c_uzaverka, uzaverka, cena_pl, poradi AS poradie, druh_pl FROM tm_sk_plt INTO CURSOR zplt READWRITE ORDER BY id_kas, c_uzaverka, u_cis
*					UPDATE zplt SET uzaverka=tm_sk_sez_kas.uzaverka from tm_sk_sez_kas WHERE zplt.c_uzaverka=tm_sk_sez_kas.c_uzaverka
					INDEX ON id_kas + STR(c_uzaverka,12) + u_cis TAG kasuzuc
					SELECT tm_sk_txt
					SET ORDER TO tm_skxx
					SELECT zplt
					SET RELATION TO uzaverka + u_cis + STR(poradie,2) INTO tm_sk_txt
					DELETE FOR zplt.druh_pl <> m.druh
					DO pr_report WITH 'trz_ucty3', !m.reporty_volane_z_formu
					USE IN zplt

				CASE m.barlbl=2 .OR. m.barlbl=3 .OR. m.barlbl=4
					SELECT c_karty, SPACE(30) as nazev, mnozstvi, id_kas, id_man, u_cis, uzaverka, c_uzaverka, $0 as cena_prum, k_odpoctu, ;
                           prachy, prachy_puv, prachy AS jc, $0 AS cena_pl,$0 AS cena_plt,$0 AS cena_celk,$0 AS cena_slev,00.00 AS round50, c_mena, SPACE(2) as hodina ;
                           FROM tm_sk_pol INTO CURSOR zpol READWRITE ORDER BY c_uzaverka, u_cis
                    DO dopln_nazev IN s_trzby_new
					SELECT tm_sk_fin
                    INDEX on u_cis+STR(c_uzaverka,12) TAG uzuc
					SELECT zpol
					SET RELATION TO u_cis+STR(c_uzaverka,12) INTO tm_sk_fin
					REPLACE ALL cena_celk WITH tm_sk_fin.cena_celk, cena_slev WITH tm_sk_fin.trzba1+tm_sk_fin.trzba2+tm_sk_fin.trzba3+tm_sk_fin.trzba4, round50 WITH tm_sk_fin.round50
					IF FSIZE("u_datetime","tm_sk_fin")>0
						REPLACE ALL hodina WITH PADL(ALLTRIM(STR(HOUR(tm_sk_fin.u_datetime),2,0)),2,"0")
					ELSE
						REPLACE ALL hodina WITH SUBSTR(tm_sk_fin.u_cas,4,2)
					ENDIF 
					SET RELATION TO
					LOCAL m.cuz, m.cu
					m.cuz=-1213132
					m.cu="xxxxxx"
					SCAN ALL
						 IF zpol.c_uzaverka=m.cuz AND zpol.u_cis=m.cu
						 	REPLACE round50 WITH 0
						 ELSE
						 	m.cuz=zpol.c_uzaverka
						 	m.cu=zpol.u_cis
						 ENDIF 
					ENDSCAN 
					RELEASE m.cuz, m.cu
					SELECT tm_sk_fin
					DELETE TAG uzuc
					SELECT u_cis,c_uzaverka,SUM(cena_pl) AS cena FROM tm_sk_plt INTO TABLE(m.gcpri_path + 'pplt') GROUP BY c_uzaverka,u_cis
					INDEX ON STR(c_uzaverka,12)+u_cis TAG aaa
					SELECT u_cis,c_uzaverka,SUM(cena_pl) AS cena FROM tm_sk_plt INTO TABLE(m.gcpri_path + 'pplt1') GROUP BY c_uzaverka,u_cis ;
						WHERE druh_pl=m.druh
					INDEX ON STR(c_uzaverka,12)+u_cis TAG aaa
					SELECT zpol
					SET RELATION TO STR(c_uzaverka,12)+u_cis INTO pplt
					REPLACE ALL cena_plt WITH pplt.cena
					SET RELATION TO STR(c_uzaverka,12)+u_cis INTO pplt1
					REPLACE ALL cena_pl WITH pplt1.cena
					DELETE FOR EMPTY(pplt1.u_cis)
					SET RELATION TO
					USE IN pplt1
					USE IN pplt
					REPLACE ALL jc WITH IIF(mnozstvi=0,0,prachy/mnozstvi), cena_prum  WITH IIF(mnozstvi=0,0,cena_prum/mnozstvi)
					REPLACE ALL mnozstvi WITH IIF(cena_plt=0,mnozstvi,mnozstvi*cena_pl/cena_plt)    && jen za zvolenou platbu
					SELECT zpol
					REPLACE ALL prachy        WITH NTOM(MTON(prachy)    *en__meny(c_mena,m.girep_mena)),;
								    jc        WITH NTOM(MTON(jc)        *en__meny(c_mena,m.girep_mena)),;
								    cena_pl   WITH NTOM(MTON(cena_pl)   *en__meny(c_mena,m.girep_mena)),;
								    cena_plt  WITH NTOM(MTON(cena_plt)  *en__meny(c_mena,m.girep_mena)),;
									cena_celk WITH NTOM(MTON(cena_celk) *en__meny(c_mena,m.girep_mena)),;
									cena_slev WITH NTOM(MTON(cena_slev) *en__meny(c_mena,m.girep_mena))	
*!*						IF m.girep_mena=2
*!*							REPLACE ALL round50 WITH 0
*!*						ENDIF 
					PRIVATE hlavicka_reportu
					m.hlavicka_reportu=""
					DO CASE
						CASE m.barlbl=2
							SELECT zpol
*						DO had WITH "90","index"
							INDEX ON id_kas+STR(c_uzaverka,12)+u_cis+id_man TAG kasuzuc
							REPLACE cena_prum WITH 0 FOR EMPTY(id_man) OR (k_odpoctu="F" AND m.slovenskaverzia)
							SELECT zpol
							DO pr_report WITH 'trz_ucty5', !m.reporty_volane_z_formu
						CASE m.barlbl=3
*						DO had WITH "90","select"
							SELECT nazev, SUM(mnozstvi) AS kolko, SUM(prachy) AS prachy, jc, id_kas, id_man,SUM(round50) AS round50 FROM zpol INTO CURSOR asdf READWRITE  GROUP BY id_kas,id_man,jc,nazev ORDER BY id_kas,nazev
							DELETE FOR kolko=0
							DO pr_report WITH "trz_spotr_5", !m.reporty_volane_z_formu
							USE IN asdf
						CASE m.barlbl=4
							DO pozavirej WITH "hodiny"
							SELECT distinct hodina, .t. as lbl FROM zpol INTO CURSOR hodiny READWRITE WHERE !ISNULL(hodina) AND !EMPTY(hodina) 
							IF RECCOUNT("hodiny")>0
								DO FORM selpartn WITH "hodiny","hodina"
								SELECT hodiny
								LOCATE FOR EMPTY(lbl)
								IF FOUND()
									GO TOP 
									SCAN FOR lbl
										IF EMPTY(m.hlavicka_reportu)
											m.hlavicka_reportu=hodiny.hodina
										ELSE
											m.hlavicka_reportu=m.hlavicka_reportu+","+hodiny.hodina
										ENDIF 
									ENDSCAN 
									SELECT zpol
									DELETE from zpol WHERE hodina IN (select hodina from hodiny where !lbl)
								ENDIF 
							ENDIF 
							DO pozavirej WITH "hodiny"
							SELECT zpol
*						DO had WITH "90","index"
							INDEX ON id_kas+STR(c_uzaverka,12)+u_cis+id_man TAG kasuzuc
							REPLACE cena_prum WITH 0 FOR EMPTY(id_man) OR (k_odpoctu="F" AND m.slovenskaverzia)
							SELECT zpol
							DO pr_report WITH 'trz_ucty5', !m.reporty_volane_z_formu
					ENDCASE
*					DO had
					USE IN zpol
				CASE m.barlbl=5
					IF !USED("buf_ucet")
						DO pripravdata
					ENDIF
					DO tvor_dph1 IN sk_stati
					SELECT * , 0.00000 AS koef FROM dph1 INTO CURSOR dph11 READWRITE 
					SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_pl, dan FROM dph1 INTO CURSOR dph111 READWRITE  GROUP BY uzaverka, u_cis, dan
					INDEX ON uzaverka+u_cis+STR(dan,5,2) TAG uzuc
					SELECT dph11
					SET RELATION TO uzaverka+u_cis+STR(dan,5,2) INTO dph111
					REPLACE koef WITH 1 FOR dph111.cena_pl=0
					REPLACE koef WITH dph11.cena_pl/dph111.cena_pl FOR dph111.cena_pl<>0
					USE IN dph111
					SELECT pri_buff.*,dph11.koef,dph11.druh_pl AS druh_plat, SPACE(30) as hjmeno FROM pri_buff, dph11 WHERE pri_buff.uzaverka=dph11.uzaverka .AND. pri_buff.u_cis=dph11.u_cis .AND. pri_buff.dan=dph11.dan;
						INTO CURSOR pribuff READWRITE 
					SELECT * FROM pribuff WHERE koef<>1 INTO CURSOR pribuff1 READWRITE 
					REPLACE ALL mnozstvi WITH mnozstvi*(koef)
					REPLACE sumpc WITH sumpc*(koef) ALL
					REPLACE sum_dph WITH sum_dph*(koef) ALL
					SELECT pribuff
					DELETE FOR koef<>1
					INSERT INTO pribuff (id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
						sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat, hjmeno) ;
						SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
						sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat, hjmeno FROM pribuff1
					SELECT id_kas, sumpc as prachy, druh_plat as druh_k, druh as druh, u_cis, uzaverka, sum_dph as sum_dph,;
						(sumpc-sum_dph) as dan_zakl, dan, cen_hlad, hjmeno FROM pribuff INTO CURSOR dph3 READWRITE WHERE druh_plat=m.druh
					DO pozavirej WITH "pribuff, pribuff1, dph11"

					SELECT tm_sk_plt.id_kas, tm_sk_plt.u_cis, tm_sk_plt.uzaverka, tm_sk_txt.hjmeno FROM tm_sk_plt LEFT JOIN tm_sk_txt ;
						ON tm_sk_plt.uzaverka=tm_sk_txt.uzaverka AND tm_sk_plt.u_cis=tm_sk_txt.u_cis AND tm_sk_plt.poradi=tm_sk_txt.poradi INTO CURSOR zplt READWRITE ORDER BY tm_sk_plt.id_kas, tm_sk_plt.c_uzaverka, tm_sk_plt.u_cis WHERE tm_sk_plt.druh_pl=m.druh
					SELECT disti hjmeno, .T. as lbl FROM zplt INTO CURSOR selhjmeno READWRITE 
					IF RECCOUNT()>0
						UPDATE dph3 SET hjmeno=IIF(ISNULL(zplt.hjmeno),"",zplt.hjmeno) from zplt WHERE dph3.uzaverka=zplt.uzaverka AND dph3.u_cis=zplt.u_cis
						DO FORM selpartn WITH "selhjmeno","hjmeno"
						SELECT selhjmeno
						DELETE FROM dph3 WHERE hjmeno IN (SELECT hjmeno FROM selhjmeno WHERE EMPTY(lbl))
						SELECT hjmeno, id_kas,uzaverka, "   " AS spart, dan, druh, SUM(prachy) AS prachy, SUM(sum_dph) AS cena_dan2, druh_k ;
							GROUP BY hjmeno, druh_k,druh,dan,uzaverka, id_kas FROM dph3 INTO CURSOR pomkk READWRITE 
						UPDATE pomkk SET spart=skupiny.spart from skupiny WHERE LEFT(pomkk.druh,10)=skupiny.druh
						REPLACE FOR EMPTY(spart) spart WITH "___"
						SELECT hjmeno, spart, dan, ;
							SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2;
							GROUP BY hjmeno, spart, dan FROM pomkk INTO CURSOR i_tr_sp READWRITE 
						INDEX ON hjmeno + spart + STR(dan,4,2) TAG aaa 
						DO pr_report WITH "trz_i_dr_spart_uzav_uvery", !m.reporty_volane_z_formu
						DO pozavirej WITH "i_tr_,i_tr_sp"
						USE IN pomkk
					ENDIF 
					DO pozavirej WITH "zplt, selhjmeno, dph3, pomkk" 
			ENDCASE
		ENDSCAN
	ENDDO
ENDIF
USE IN druhy
RELEASE m.barlbl


*!*****************************************************************************
*!      Procedure: UCTY2
*!*****************************************************************************
PROCEDURE ucty2
PRIVATE ucis,uzav,por,druh,m.barlbl
SELECT DISTINCT cen_hlad FROM tm_sk_pol INTO CURSOR hladiny HAVING !EMPTY(cen_hlad) ORDER BY cen_hlad
IF RECCOUNT()>0
	DO WHILE .T.
		m.druh=""
		IF m.reporty_volane_z_formu
			DO FORM dajdruh WITH "","hladiny","cen_hlad",100 TO m.druh
		ENDIF 
		IF EMPTY(m.druh)
			EXIT
		ENDIF
*	DO had WITH "33","pol"
		SELECT c_karty, SPACE(30) as nazev, mnozstvi, id_kas, id_man, u_cis, uzaverka, c_uzaverka, prachy, ;
               prachy_puv, $0 AS jc,$0 AS cena_pl,$0 AS cena_plt,$0 AS cena_celk,$0 AS cena_slev, 000.0 AS round50, c_mena ;
			   FROM tm_sk_pol INTO CURSOR zpol READWRITE ORDER BY uzaverka, u_cis ;
 			   WHERE cen_hlad = m.druh .AND. !EMPTY(k_odpoctu)
*	DO had WITH "66","fin"
        DO dopln_nazev IN s_trzby_new

		SELECT tm_sk_fin
        INDEX on u_cis+STR(c_uzaverka,12) TAG uzuc
		SELECT zpol
        SET RELATION TO u_cis+STR(c_uzaverka,12) INTO tm_sk_fin
		REPLACE ALL zpol.cena_celk WITH tm_sk_fin.cena_celk, zpol.cena_slev WITH tm_sk_fin.trzba1+tm_sk_fin.trzba2+tm_sk_fin.trzba3+tm_sk_fin.trzba4

		INDEX ON u_cis+uzaverka TAG uzucq UNIQ
        SET RELATION TO u_cis+STR(c_uzaverka,12) INTO tm_sk_fin
		REPLACE ALL zpol.round50 WITH tm_sk_fin.round50
		SET RELATION TO
		INDEX ON u_cis+uzaverka TAG uzuc
		SELECT tm_sk_fin
		DELETE TAG uzuc
		SELECT zpol
*	DO had WITH "99","replace"
*		REPLACE ALL prachy WITH IIF(cena_celk=0,0,ROUND(prachy*cena_slev/cena_celk,1))
		REPLACE ALL jc WITH IIF(mnozstvi=0,0,prachy/mnozstvi)
		REPLACE ALL prachy WITH NTOM(MTON(prachy)   *en__meny(c_mena,m.girep_mena)), ;
                        jc WITH NTOM(MTON(jc)       *en__meny(c_mena,m.girep_mena)), ;
                   cena_pl WITH NTOM(MTON(cena_pl)  *en__meny(c_mena,m.girep_mena)), ;
                  cena_plt WITH NTOM(MTON(cena_plt) *en__meny(c_mena,m.girep_mena)),;
			     cena_celk WITH NTOM(MTON(cena_celk)*en__meny(c_mena,m.girep_mena)), ;
                 cena_slev WITH NTOM(MTON(cena_slev)*en__meny(c_mena,m.girep_mena))	
*!*			IF m.girep_mena=2
*!*				REPLACE ALL round50 WITH 0
*!*			ENDIF 

*	DO had
		SELECT SPACE(30) AS zostava,.F. AS lbl INTO CURSOR selzost FROM tm_sk_plt WHERE .f. READWRITE 
		INSERT INTO selzost (zostava,lbl) VALUES (jaz("m_s5_sk_ucty_platby_prehlad"),.F.)
		INSERT INTO selzost (zostava,lbl) VALUES (jaz("m_s5_sk_ucty_hladiny_sumar"),.F.)
		DO FORM selpartn WITH "selzost","zostava"
		SELECT selzost
		SCAN FOR !EMPTY(lbl)
			m.barlbl=RECNO()
			DO CASE
				CASE m.barlbl=1
					SELECT zpol
					INDEX ON id_kas+uzaverka+u_cis TAG kasuzuc
					DO pr_report WITH 'trz_ucty4', !m.reporty_volane_z_formu
				CASE m.barlbl=2
					SELECT nazev, SUM(mnozstvi) AS kolko, SUM(prachy) AS prachy, jc, id_kas, id_man,SUM(round50) AS round50 FROM zpol INTO CURSOR asdf READWRITE GROUP BY id_kas,id_man,jc,nazev ORDER BY id_kas,id_man,nazev
					DELETE FOR kolko=0
					DO pr_report WITH "trz_spotr_4", !m.reporty_volane_z_formu
					USE IN asdf
			ENDCASE
		ENDSCAN 
	ENDDO
ENDIF 
USE IN hladiny
IF USED("zpol")
	USE IN zpol
ENDIF 
RELEASE m.barlbl

*!*****************************************************************************
*!      Procedure: prehluctov
*!*****************************************************************************
PROCEDURE prehluctov
PARAMETERS m.repriadok
IF "1"$m.repriadok
	DO ucty1
ENDIF 
IF "2"$m.repriadok
	DO ucty2
ENDIF 

*!*****************************************************************************
*!      Procedure: SPDANPREDAJ
*!*****************************************************************************
PROCEDURE spdanpredaj
PARAMETERS repriadok
PRIVATE usedkarty
IF m.issql
	rv("spdan","spdane")
	SELECT spdane
	INDEX on c_karty TAG c_karty
ELSE 
	IF FILE(m.fn_spotrebnadan+".dbf")
		SELECT 0
		USE (m.fn_spotrebnadan) ALIAS spdane ORDER c_karty
	ENDIF 
ENDIF 
IF USED("spdane")

    SELECT tm_sk_pol.id_kas, tm_sk_pol.u_cis, tm_sk_pol.uzaverka,tm_sk_pol.c_karty, spdane.koeficient, tm_sk_pol.dan_sazba as dan, SUM(tm_sk_pol.mnozstvi) as mnozstvi,SUM(tm_sk_pol.prachy) as prachy, SPACE(30) as platby ;
           FROM tm_sk_pol INNER JOIN spdane ON tm_sk_pol.c_karty=spdane.c_karty ;
           GROUP BY tm_sk_pol.id_kas,tm_sk_pol.u_cis, tm_sk_pol.uzaverka, tm_sk_pol.c_karty, tm_sk_pol.dan ;
           ORDER BY tm_sk_pol.id_kas, tm_sk_pol.uzaverka, tm_sk_pol.c_karty, tm_sk_pol.dan INTO CURSOR spdanaa WHERE !EMPTY(tm_sk_pol.k_odpoctu) READWRITE 
	IF RECCOUNT()>0
		IF MESSAGEBOX(jaz("s_sk_po_platbach"),4+32+256,"FoodMan")=6
			SELECT uzaverka, u_cis, druh_pl FROM tm_sk_plt WHERE uzaverka+u_cis in (SELECT uzaverka+u_cis FROM spdanaa) INTO CURSOR platby
			SELECT uzaverka, u_cis, COUNT(uzaverka+u_cis) as pocet FROM platby INTO CURSOR pocty GROUP BY uzaverka, u_cis
			UPDATE spdanaa SET platby=platby.druh_pl from platby WHERE spdanaa.uzaverka=platby.uzaverka AND spdanaa.u_cis=platby.u_cis AND platby.uzaverka+platby.u_cis in (select uzaverka+u_cis from pocty WHERE pocet=1)
			SELECT pocty
			SCAN FOR pocet>1
				SCATTER MEMVAR fields uzaverka, u_cis
				SELECT platby
				SCAN FOR uzaverka=pocty.uzaverka AND u_cis=pocty.u_cis
					SELECT spdanaa
					REPLACE platby WITH ALLTRIM(platby)+","+ALLTRIM(platby.druh_pl) FOR uzaverka=platby.uzaverka AND u_cis=platby.u_cis
					SELECT platby
				ENDSCAN 
				SELECT pocty
			ENDSCAN 
			DO pozavirej WITH "pocty, platby"
			SELECT spdanaa
			REPLACE platby WITH SUBSTR(platby,2) FOR LEFT(platby,1)=","
		ELSE
			SELECT spdanaa
			REPLACE ALL platby WITH "Spolu"
		ENDIF 
		SELECT id_kas, uzaverka, c_karty, koeficient, dan, SUM(mnozstvi) as mnozstvi, SUM(prachy) as prachy, platby FROM spdanaa GROUP BY id_kas, uzaverka, platby, c_karty, dan INTO CURSOR spdanaaa
		SELECT spdanaaa.*, karty.objem, karty.nazev, m.spotrebnadan as plato FROM spdanaaa LEFT JOIN karty ON spdanaaa.c_karty=karty.c_karty INTO CURSOR spdanbbb ORDER BY spdanaaa.id_kas, spdanaaa.uzaverka, spdanaaa.platby, spdanaaa.c_karty, dan
		IF "1"$m.repriadok
			DO pr_report WITH "trz_prspdan", !m.reporty_volane_z_formu
		ENDIF 
		IF "2"$m.repriadok
			SELECT id_kas,uzaverka, koeficient, dan, platby, SUM(prachy) AS prachy, SUM(mnozstvi*objem) as mnozstvo, SUM(ROUND(mnozstvi*objem*koeficient*plato/100,2)) as spdan, plato FROM spdanbbb GROUP BY id_kas,uzaverka, platby, koeficient, dan ORDER BY id_kas, uzaverka, platby, koeficient INTO CURSOR spdanccc
			DO pr_report WITH "trz_prspdan2", !m.reporty_volane_z_formu
		ENDIF 
	ELSE
		=MESSAGEBOX(jaz("forn_no_kalk_so_spotreb_danou"),64,"FoodMan")
	ENDIF 
	DO pozavirej WITH "spdane, spdanaaa, spdanbbb, spdanccc"
ELSE
	=MESSAGEBOX(jaz("forn_nenastavena_spotrebna_dan"),64,"FoodMan")
ENDIF 


*!*****************************************************************************
*!      Procedure: PREDAJMENU
*!*****************************************************************************
PROCEDURE predajmenu
PARAMETERS m.repriadok
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
SELECT *, SPACE(6) as idmenu FROM pri_buff WHERE !EMPTY(id_fmenu) INTO CURSOR fmenu READWRITE
REPLACE summc WITH 0 FOR RIGHT(ALLTRIM(id_fmenu),1)="H"
SELECT DISTINCT nazev, SPACE(6) as idmenu FROM fmenu WHERE RIGHT(ALLTRIM(id_fmenu),1)="H" READWRITE INTO CURSOR fmenuhlv
REPLACE ALL idmenu WITH PADL(ALLTRIM(STR(RECNO(),5,0)),5,"0")+"H"
UPDATE fmenu SET idmenu=fmenuhlv.idmenu FROM fmenuhlv WHERE fmenu.nazev=fmenuhlv.nazev AND RIGHT(ALLTRIM(fmenu.id_fmenu),1)="H"
USE IN fmenuhlv
SELECT DISTINCT uzaverka, id_fmenu, id_fmenu as id_pol, idmenu FROM fmenu WHERE RIGHT(ALLTRIM(id_fmenu),1)="H" READWRITE INTO CURSOR fmenuhlv
REPLACE ALL id_pol WITH STRTRAN(id_pol,"H","P")
UPDATE fmenu SET idmenu=STRTRAN(fmenuhlv.idmenu,"H","P") FROM fmenuhlv WHERE fmenu.uzaverka=fmenuhlv.uzaverka AND fmenu.id_fmenu=fmenuhlv.id_pol AND RIGHT(ALLTRIM(fmenu.id_fmenu),1)="P"
USE IN fmenuhlv
IF "1"$m.dily
	SELECT id_kas, nazev, uzaverka, dan, SUM(sumpc) as sumpc, SUM(summc) as summc, SUM(mnozstvi) as mnozstvi, SUM(sum_dph) as sum_dph, SUM(sumpc_dan) as sumpc_dan, c_mena, idmenu FROM fmenu GROUP BY id_kas, uzaverka, idmenu, nazev ORDER BY id_kas, uzaverka, idmenu INTO CURSOR aaa
	GO TOP 
	DO pr_report WITH "trz_predajfast@kas", !m.reporty_volane_z_formu
ENDIF 
IF "2"$m.dily
	SELECT "" as id_kas, nazev, "" as uzaverka, dan, SUM(sumpc) as sumpc, SUM(summc) as summc, SUM(mnozstvi) as mnozstvi, SUM(sum_dph) as sum_dph, SUM(sumpc_dan) as sumpc_dan, c_mena, idmenu FROM fmenu GROUP BY idmenu, nazev ORDER BY idmenu INTO CURSOR aaa
	GO TOP 
	DO pr_report WITH "trz_predajfast", !m.reporty_volane_z_formu
ENDIF 

DO pozavirej WITH "fmenu, fmenuhlv, aaa"


*!*****************************************************************************
PROCEDURE s_cas_atr

s_cas_atr_had = createobject("had", program())

SELECT id_kas, uzaverka, cas_atrib, SUM(pocet_h) as pocet_h, SUM(prachy) as cena_slev, SPACE(30) as zkr_kas ;
       WHERE .not. EMPTY(k_odpoctu) GROUP BY 1, 2, 3 FROM tm_sk_pol INTO CURSOR fi_atrib00 READWRITE 
       
UPDATE fi_atrib00 SET zkr_kas=tm_sk_sez.zkr_kas FROM tm_sk_sez WHERE fi_atrib00.id_kas=tm_sk_sez.id_kas

m.ukazovat_hosty_v_uzav = .T.
DO s_cas_atr_pokrac

SELECT id_kas, zkr_man as uzaverka, cas_atrib, SUM(pocet_h) as pocet_h, SUM(prachy) as cena_slev, zkr_kas ;
       GROUP BY 1, 2, 3 FROM tm_sk_pol INTO CURSOR fi_atrib00 READWRITE 
m.ukazovat_hosty_v_uzav = .F.
DO s_cas_atr_pokrac

release s_cas_atr_had
do pozavirej with "fi_atrib, fi_atrib1, fi_atrib00, cas_atr"


*!*****************************************************************************
PROCEDURE s_cas_atr_pokrac
s_cas_atr_had.show()
s_cas_atr_had.set_had(22)
SELECT id_kas, uzaverka, cas_atrib, sum(cena_slev) as cena_slev, sum(pocet_h) as pocet_h, zkr_kas, ;
	000000 as hostu1, 000000 as hostu2, 000000 as hostu3, 000000 as hostu4, 000000 as hostu5,  ;
	$0  as trzba1,    $0  as trzba2,    $0  as trzba3,    $0  as trzba4,    $0  as trzba5   ;
	GROUP BY 1,2,3 FROM fi_atrib00 INTO CURSOR fi_atrib READWRITE 

s_cas_atr_had.set_had(33)
SELECT DISTINCT cas_atrib FROM fi_atrib INTO CURSOR cas_atr READWRITE 
iii = 0
STORE "-" TO atrib1, atrib2, atrib3, atrib4, atrib5
SCAN  
	iii = m.iii + 1
	atr_xx = "atrib" + STR(m.iii, 1)
	&atr_xx = cas_atr.cas_atrib
ENDSCAN 

SELECT fi_atrib
REPLACE FOR cas_atrib = m.atrib1 hostu1 WITH pocet_h, trzba1 WITH cena_slev
REPLACE FOR cas_atrib = m.atrib2 hostu2 WITH pocet_h, trzba2 WITH cena_slev
REPLACE FOR cas_atrib = m.atrib3 hostu3 WITH pocet_h, trzba3 WITH cena_slev
REPLACE FOR cas_atrib = m.atrib4 hostu4 WITH pocet_h, trzba4 WITH cena_slev
REPLACE FOR cas_atrib = m.atrib5 hostu5 WITH pocet_h, trzba5 WITH cena_slev

s_cas_atr_had.set_had(66)

SELECT id_kas, SUM(cena_slev) as cena_slev, SUM(pocet_h) as pocet_h ;
	GROUP BY 1 FROM fi_atrib INTO CURSOR fi_kas1 READWRITE 
REPLACE FOR pocet_h # 0 cena_slev WITH cena_slev / pocet_h
REPLACE FOR pocet_h = 0 cena_slev WITH 0
INDEX ON id_kas TAG id_kas

SELECT id_kas, uzaverka, SUM(cena_slev) as cena_slev, SUM(pocet_h) as pocet_h, zkr_kas as id_zkratka, ;
       SUM(hostu1) as hostu1, SUM(hostu2) as hostu2, SUM(hostu3) as hostu3, SUM(hostu4) as hostu4, SUM(hostu5) as hostu5, ;
       SUM(trzba1) as trzba1, SUM(trzba2) as trzba2, SUM(trzba3) as trzba3, SUM(trzba4) as trzba4, SUM(trzba5) as trzba5 ;
       GROUP BY 1,2 FROM fi_atrib INTO CURSOR fi_atrib1 READWRITE 
DELETE FOR EMPTY(cena_slev) .and. EMPTY(pocet_h)
SET RELATION TO id_kas INTO fi_kas1
s_cas_atr_had.hide()
DO pr_report WITH "trz_cas_atr", !m.reporty_volane_z_formu


PROCEDURE pohladavky
PARAMETERS repriadok
SELECT sk__sez.uzaverka, sk__sez.id_kas, sk__sez.c_mena, sk__fin.autor, sk__fin.c_uzaverka, sk__plt.cena_pl AS cena_celk, sk__fin.text_link, TRANSFORM(sk__fin.ucet, "@L 99999") AS u_cis, sk__plt.druh_pl, sk__plt.cena_pl FROM sk__fin LEFT JOIN  sk__plt;
	ON sk__fin.c_uzaverka=sk__plt.c_uzaverka AND sk__fin.ucet=sk__plt.ucet LEFT JOIN sk__sez ON sk__fin.c_uzaverka=sk__sez.c_uzaverka INTO CURSOR buff WHERE sk__fin.pohladavka;
	ORDER BY sk__sez.id_kas, sk__sez.uzaverka, sk__fin.ucet, sk__fin.text_link READWRITE
SELECT SPACE(17) as uzaverka, SPACE(2) as id_kas, m.giakt_mena as c_mena, sk__fin.autor, sk__fin.c_uzaverka, sk__plt.cena_pl AS cena_celk, sk__fin.text_link, TRANSFORM(sk__fin.ucet, "@L 99999") AS u_cis, sk__plt.druh_pl, sk__plt.cena_pl FROM sk__fin LEFT JOIN  sk__plt;
	ON sk__fin.c_uzaverka=sk__plt.c_uzaverka AND sk__fin.ucet=sk__plt.ucet INTO CURSOR buff WHERE sk__fin.pohladavka;
	ORDER BY id_kas, uzaverka, sk__fin.ucet, sk__fin.text_link READWRITE
UPDATE buff SET buff.uzaverka=sk__sez.uzaverka, buff.id_kas=sk__sez.id_kas, buff.c_mena=sk__sez.c_mena from sk__sez WHERE buff.c_uzaverka=sk__sez.c_uzaverka
REPLACE cena_celk WITH NTOM(MTON(cena_celk)*en__meny(c_mena,m.girep_mena)),  cena_pl WITH NTOM(MTON(cena_pl)*en__meny(c_mena,m.girep_mena)) FOR c_mena<>m.girep_mena
GO TOP 
DO pr_report WITH "trz_i_pohladavky", !m.reporty_volane_z_formu
USE IN buff


*!*****************************************************************************
*!      Procedure: OPISUCTU
*!*****************************************************************************
PROCEDURE opisuctu
PRIVATE m.hladajucis, m.hladajuzaverka, z_archivu, m.hladajidkas, m.k, m.sk, m.tip, m.pocetplatieb, m.ucetmena
PRIVATE m.cena_slev, m.cena_celk, m.hladajucet, m.hladajcuzaverka
m.hladajucis = ALLTRIM(UPPER(INPUTBOX(jaz("form_zadaj_cislo_uctu"), jaz("form_opis_uctu"))))
IF LEN(m.hladajucis)>7
	=MESSAGEBOX(jaz("form_ucty_su_iba_na_7_znakov"),64,"FoodMan")
	RETURN
ENDIF 
m.hladajuzaverka=""
m.hladajidkas=LEFT(m.hladajucis,2)
m.hladajucet=VAL(SUBSTR(m.hladajucis,3))
IF !EMPTY(m.hladajucis)
	IF m.issql
		rv("skfin1ucet","skf")
		rv("skfina1ucet","skfa")
		SELECT id_kas, id_kas+PADL(ALLTRIM(STR(ucet,5,0)),5,"0") as u_cis, autor, text_sleva, text_link, uzaverka, c_uzaverka, $0 as dph_celk, ;
			$0 as trzba_dph1, $0 as trzba_dph2, $0 as trzba_dph3, $0 as trzba_dph4, $0 as dan1, $0 as dan2, $0 as dan3, $0 as dan4, STR(u_minut,4,0)+"m" as u_open, $0 as dz1, $0 as dz2, $0 as dz3, $0 as dz4, $0 as dz5,;
			u_cas, pocet_h, stul, ducis, pohladavka, round50,.f. as archiv, SPACE(30) as username, SPACE(50) as text, $0 as tip FROM skf INTO CURSOR u_fin READWRITE WHERE ucet=m.hladajucet AND id_kas=m.hladajidkas UNION ;
			SELECT id_kas, id_kas+PADL(ALLTRIM(STR(ucet,5,0)),5,"0") as u_cis, autor, text_sleva, text_link, uzaverka, c_uzaverka, $0 as dph_celk, ;
				$0 as trzba_dph1, $0 as trzba_dph2, $0 as trzba_dph3, $0 as trzba_dph4, $0 as dan1, $0 as dan2, $0 as dan3, $0 as dan4, STR(u_minut,4,0)+"m" as u_open, $0 as dz1, $0 as dz2, $0 as dz3, $0 as dz4, $0 as dz5, ;
				u_cas, pocet_h, stul, ducis, pohladavka, round50,.t. as archiv, SPACE(30) as username, SPACE(50) as text, $0 as tip FROM skfa WHERE ucet=m.hladajucet AND id_kas=m.hladajidkas 
	ELSE
		USE (m.fn_sk_fin) IN 0 ALIAS skf again
		USE (m.fn_sk_fin_a) IN 0 ALIAS skfa AGAIN 
		IF m.slovenskaverzia
			SELECT id_kas, u_cis, autor, text_sleva, text_link, uzaverka, cena_celk, sleva_abs, cena_slev, $0 as dph_celk, ;
				$0 as trzba_dph1, $0 as trzba_dph2, $0 as trzba_dph3, $0 as trzba_dph4, $0 as dan1, $0 as dan2, $0 as dan3, $0 as dan4, $0 as dz1, $0 as dz2, $0 as dz3, $0 as dz4, ;
				u_cas, pocet_h, stul, u_open, ducis, pohladavka, round50,.f. as archiv, SPACE(30) as username, SPACE(50) as text, $0 as tip FROM skf INTO CURSOR u_fin READWRITE WHERE u_cis=m.hladajucis UNION ;
				SELECT id_kas, u_cis, autor, text_sleva, text_link, uzaverka, MTON(cena_celk) as cena_celk, MTON(sleva_abs) as sleva_abs, MTON(cena_slev) as cena_slev, $0 as dph_celk, ;
					$0 as trzba_dph1, $0 as trzba_dph2, $0 as trzba_dph3, $0 as trzba_dph4, $0 as dan1, $0 as dan2, $0 as dan3, $0 as dan4, $0 as dz1, $0 as dz2, $0 as dz3, $0 as dz4, ;
					u_cas, pocet_h, stul, u_open, ducis, pohladavka, round50, .t. as archiv, SPACE(30) as username, SPACE(50) as text, $0 as tip FROM skfa WHERE u_cis=m.hladajucis 
		ELSE
			SELECT id_kas, u_cis, autor, text_sleva, text_link, uzaverka, cena_celk, sleva_abs, cena_slev, $0 as dph_celk, ;
				$0 as trzba_dph1, $0 as trzba_dph2, $0 as trzba_dph3, $0 as trzba_dph4, $0 as dan1, $0 as dan2, $0 as dan3, $0 as dan4, $0 as dz1, $0 as dz2, $0 as dz3, $0 as dz4, ;
				u_cas, pocet_h, stul, u_open, SPACE(4) as ducis, .f. as pohladavka, 0.0 as round50,.f. as archiv, SPACE(30) as username, SPACE(50) as text, $0 as tip FROM skf INTO CURSOR u_fin READWRITE WHERE u_cis=m.hladajucis UNION ;
				SELECT id_kas, u_cis, autor, text_sleva, text_link, uzaverka, MTON(cena_celk) as cena_celk, MTON(sleva_abs) as sleva_abs, MTON(cena_slev) as cena_slev, $0 as dph_celk, ;
					$0 as trzba_dph1, $0 as trzba_dph2, $0 as trzba_dph3, $0 as trzba_dph4, $0 as dan1, $0 as dan2, $0 as dan3, $0 as dan4, $0 as dz1, $0 as dz2, $0 as dz3, $0 as dz4, ;
					u_cas, pocet_h, stul, u_open, SPACE(4) as ducis, .f. as pohladavka, 0.0 as round50, .t. as archiv, SPACE(30) as username, SPACE(50) as text, $0 as tip FROM skfa WHERE u_cis=m.hladajucis 
		ENDIF
	ENDIF 
	DO pozavirej WITH "skf, skfa"
	DO CASE 
		CASE RECCOUNT("u_fin")>1
			m.hladajuzaverka=""
			IF m.reporty_volane_z_formu
				DO FORM dajdruh WITH "","u_fin","uzaverka",200 TO m.hladajuzaverka
			ENDIF 
			IF EMPTY(m.hladajuzaverka)
				DO pozavirej WITH "skf, skfa, u_fin"
				RETURN
			ENDIF 
		CASE RECCOUNT("u_fin")=1
			m.hladajuzaverka=u_fin.uzaverka
		OTHERWISE 
			DO pozavirej WITH "skf, skfa, u_fin"
			=MESSAGEBOX(jaz("form_nenajdene_cislo_uctu"),32,jaz("form_tlac_kopie_uctu"))
			RETURN
	ENDCASE 
	m.hladajuzaverka=ALLTRIM(m.hladajuzaverka)
	IF m.issql
		SELECT u_fin
		LOCATE FOR uzaverka=m.hladajuzaverka
		m.hladajcuzaverka=u_fin.c_uzaverka
	ENDIF 
	had = createobject("had")
	had.show()
	had.set_had( 33 )
	SELECT u_fin
	DELETE FOR uzaverka<>m.hladajuzaverka
	GO TOP 
	REPLACE ALL username WITH i_mencis("",u_fin.autor)
	SELECT u_fin
	GO TOP 
	m.z_archivu=u_fin.archiv
   	IF m.z_archivu
		IF m.issql
			rv("skplta1ucet","u_plt")
			rv("skpola1ucet","u_polx")
			rv("sktxta1ucet","u_txt")
			rv("sksezauzaverka","u_sez")
		ELSE 
			USE (m.fn_sk_plt_a) IN 0 ALIAS splt again 
			SELECT * FROM splt INTO CURSOR u_plt READWRITE WHERE u_cis=m.hladajucis AND uzaverka=m.hladajuzaverka
			USE IN splt
			USE (m.fn_sk_pol_a) IN 0 ALIAS spol again
			SELECT * FROM spol INTO CURSOR u_polx READWRITE WHERE u_cis=m.hladajucis AND uzaverka=m.hladajuzaverka
			USE IN spol
			USE (m.fn_sk_txt_a) IN 0 ALIAS stxt again
			SELECT * FROM stxt INTO CURSOR u_txt READWRITE WHERE u_cis=m.hladajucis AND uzaverka=m.hladajuzaverka
			USE IN stxt
			USE (m.fn_sk_sez_a) IN 0 ALIAS ssez AGAIN 
			SELECT * FROM ssez INTO CURSOR u_sez READWRITE WHERE uzaverka=m.hladajuzaverka
			USE IN ssez
		ENDIF 
	ELSE 
		IF m.issql
			rv("skplt1ucet","u_plt")
			rv("skpol1ucet","u_polx")
			rv("sktxt1ucet","u_txt")
			rv("sksezuzaverka","u_sez")
		ELSE 
			USE (m.fn_sk_plt) IN 0 ALIAS splt again
			SELECT * FROM splt INTO CURSOR u_plt READWRITE WHERE u_cis=m.hladajucis AND uzaverka=m.hladajuzaverka
			USE IN splt
			USE (m.fn_sk_pol) IN 0 ALIAS spol again
			SELECT * FROM spol INTO CURSOR u_polx READWRITE WHERE u_cis=m.hladajucis AND uzaverka=m.hladajuzaverka
			USE IN spol
			USE (m.fn_sk_txt) IN 0 ALIAS stxt again
			SELECT * FROM stxt INTO CURSOR u_txt READWRITE WHERE u_cis=m.hladajucis AND uzaverka=m.hladajuzaverka
			USE IN stxt
			USE (m.fn_sk_sez) IN 0 ALIAS ssez AGAIN 
			SELECT * FROM ssez INTO CURSOR u_sez READWRITE WHERE uzaverka=m.hladajuzaverka
			USE IN ssez
		ENDIF 
	ENDIF 
	had.set_had( 66 )
	SELECT u_sez
	GO TOP 
	ucetmena=en__meny(u_sez.c_mena,3)
	USE IN u_sez

	IF m.issql
		SELECT u_polx.c_karty, karty.nazev, u_polx.prachy, u_polx.k_odpoctu,;
			u_polx.mnozstvi, u_polx.cen_hlad, u_polx.dan_sazba, ;
			u_polx.dan ORDER BY k_odpoctu ;
            FROM u_polx LEFT JOIN karty ON u_polx.c_karty=karty.c_karty INTO CURSOR u_pol READWRITE 

        * * * * poscitani a serazeni radku stejne, jak ma JQ v kase    ZACATEK
        FOR radek = 1 TO RECCOUNT()
            GO (m.radek)
            SCATTER MEMVAR 
            LOCATE FOR RECNO() < m.radek .and. c_karty = m.c_karty .and. cen_hlad = m.cen_hlad .and. dan_sazba = m.dan_sazba
            IF FOUND()
                REPLACE prachy WITH prachy + m.prachy, mnozstvi WITH mnozstvi + m.mnozstvi, dan WITH dan + m.dan
                GO (m.radek)
                REPLACE c_karty WITH -999
            ENDIF 
        ENDFOR 
        DELETE FOR c_karty < 0
        * * * * poscitani a serazeni radku stejne, jak ma JQ v kase     KONEC


		SELECT SUM(dan) as dan, dan_sazba, SUM(prachy) as prachy FROM u_pol GROUP BY dan_sazba INTO CURSOR dane READWRITE ORDER BY dan_sazba WHERE !EMPTY(k_odpoctu)
		SELECT dane 
		SCAN FOR RECNO()<=5 
			m.k=RECNO()
			m.sk=ALLTRIM(STR(m.k,1,0))
			SELECT u_fin
			REPLACE u_fin.trzba_dph&sk WITH dane.prachy, u_fin.dan&sk WITH dane.dan_sazba/100+1
			SELECT dane
		ENDSCAN 
		SUM ALL dan TO m.suma_dph
	ELSE
		SELECT u_fin
		GO TOP 
		SCATTER MEMVAR fields cena_celk, cena_slev	
		SELECT u_polx.c_karty, karty.nazev, u_polx.prachy, u_polx.k_odpoctu,;
			u_polx.mnozstvi, u_polx.cen_hlad, (u_polx.dan-1) * 100 as dan_sazba, u_polx.u_time, ;
			u_polx.dan FROM u_polx LEFT JOIN karty ON u_polx.c_karty=karty.c_karty INTO CURSOR  u_pol READWRITE 

        REPLACE FOR k_odpoctu = "f" prachy WITH 0

		SELECT SUM(prachy) as prachy, SUM(prachy) as prachy_puv, dan FROM u_pol GROUP BY dan INTO CURSOR dane READWRITE ORDER BY prachy WHERE !EMPTY(k_odpoctu)
		SELECT dane 
		REPLACE ALL prachy WITH IIF(m.cena_celk=0,prachy,ground(prachy*(m.cena_slev/m.cena_celk),2))
		REPLACE dan WITH 1 FOR dan=0
		SUM prachy TO k
		IF m.k<>m.cena_slev
			SELECT dane
			LOCATE FOR prachy<>0 AND dan<>1
			IF FOUND()
				REPLACE prachy WITH prachy-m.k+m.cena_slev
			ELSE 
				LOCATE FOR prachy<>0
				IF FOUND()
					REPLACE prachy WITH prachy-m.k+m.cena_slev
				ENDIF 
			ENDIF 
		ENDIF 
		m.suma_dph=0
		SCAN FOR RECNO()<=5 
			m.k=RECNO()
			m.sk=ALLTRIM(STR(m.k,1,0))
			m.suma_dph=m.suma_dph+dane.prachy*(dane.dan-1)/dane.dan
			SELECT u_fin
			REPLACE u_fin.trzba_dph&sk WITH dane.prachy, u_fin.dan&sk WITH dane.dan
			SELECT dane
		ENDSCAN 
	ENDIF 
	SELECT u_fin
	REPLACE dph_celk WITH ROUND(m.suma_dph,2)
	IF RECCOUNT("u_txt")>0
		REPLACE u_fin.text WITH alltrim(u_txt.hpokoj) + " " + u_txt.hjmeno
	ENDIF 
	had.set_had( 99 )
	SELECT u_plt
	SUM tip TO m.tip
	SELECT u_fin
	REPLACE tip WITH m.tip
    REPLACE FOR dan1 >= 1 dz1 WITH ROUND(trzba_dph1 / dan1, 2)
    REPLACE FOR dan2 >= 1 dz2 WITH ROUND(trzba_dph2 / dan2, 2)
    REPLACE FOR dan3 >= 1 dz3 WITH ROUND(trzba_dph3 / dan3, 2)
    REPLACE FOR dan4 >= 1 dz4 WITH ROUND(trzba_dph4 / dan4, 2)
    GO TOP 

	m.pocetplatieb=MAX(RECCOUNT("u_plt"), 1)
	DIMENSION uplt(4,8)

    FOR iii = 1 TO 4
        STORE "" TO uplt(iii, 1), uplt(iii, 5), uplt(iii, 6), uplt(iii, 7), uplt(iii, 8)
        STORE 0  TO uplt(iii, 2), uplt(iii, 3), uplt(iii, 4)
    ENDFOR 
	SELECT u_plt
    REPLACE FOR LEN(ALLTRIM(fik)) < 22 fik WITH ""
    REPLACE FOR LEN(ALLTRIM(bkp)) < 22 bkp WITH ""
	IF m.slovenskaverzia
		COPY to ARRAY uplt FIELDS druh_pl, cena_pl, tip, cena_mena, mena, fik, bkp, bkp ALL 
	ELSE
		COPY to ARRAY uplt FIELDS druh_pl, cena_pl, tip, cena_mena, mena, fik, bkp, cas_odesl ALL 
	ENDIF

	
	
	IF m.issql
		rv("kas_utxt", "kas_utxt")
	ELSE 
		IF FILE(m.gchom_path + "kas_utxt.dbf")
			USE (m.gchom_path + "kas_utxt.dbf") IN 0
		ENDIF 
	ENDIF 
	had.set_had( 100 )
	IF USED("kas_utxt") AND RECCOUNT("kas_utxt")>0
		SELECT kas_utxt
		LOCATE FOR id_kas = m.hladajidkas
		IF FOUND()
			SCATTER MEMVAR FIELDS userhead1, userhead2, userhead3, userhead4, userhead5, userhead6, userhead7, userhead8, usertail1, usertail2, usertail3, usertail4, usertail5
		ELSE 
			SCATTER MEMVAR BLANK FIELDS userhead1, userhead2, userhead3, userhead4, userhead5, userhead6, userhead7, userhead8, usertail1, usertail2, usertail3, usertail4, usertail5
		ENDIF 
	ELSE 
		STORE  "" TO userhead1, userhead2, userhead3, userhead4, userhead5, userhead6, userhead7, userhead8
		STORE  "" TO usertail1, usertail2, usertail3, usertail4, usertail5
	ENDIF 
    RELEASE had
    SELECT u_fin
    REPLACE ALL dan1 WITH MAX(dan1, 1)
    REPLACE ALL dan2 WITH MAX(dan2, 1)
    REPLACE ALL dan3 WITH MAX(dan3, 1)
    REPLACE ALL dan4 WITH MAX(dan4, 1)
    GO TOP 
	SELECT u_pol
	PRIVATE m.prachy_celk
	SUM prachy TO prachy_celk FOR !EMPTY(k_odpoctu)
	GO TOP 
	DO pr_report WITH "trz_ucet", !m.reporty_volane_z_formu
	RELEASE m.prachy_celk
ENDIF 
DO pozavirej WITH "u_fin, u_plt, u_pol, u_txt, dane, kas_utxt"


*!*****************************************************************************
PROCEDURE predaj_org
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
DO pozavirej WITH "buff, vybery"
SELECT .f. as kredit, nazev, summc, sum_dph, sumpc, dan, mnozstvi, id_kas, id_man, druh, cen_hlad, spart, uzaverka, u_cis, 000000000000 as id_ucet, 000000000000 as cislo_org, SPACE(30) as nazov_org, round50 FROM pri_buff INTO CURSOR buff READWRITE 
IF !EMPTY(m.crd_path)
	DO CASE 
		CASE FILE(m.crd_path+"hkredobr.dbf") AND FILE(m.crd_path+"orgstr.dbf") AND FILE(m.crd_path+"hot_kred.dbf") 
			TRY 
				USE (m.crd_path+"hkredobr") IN 0 SHARED 
				USE (m.crd_path+"orgstr") IN 0 SHARED 
				USE (m.crd_path+"hot_kred") IN 0 SHARED 
				IF FILE(m.crd_path+"_tmhkobr.dbf")
					USE (m.crd_path+"_tmhkobr") IN 0 SHARED 
				ENDIF 
			CATCH
				DO pozavirej WITH "hkredobr, orgstr, hot_kred, _tmhkobr"
			ENDTRY 
			IF USED("hkredobr")
				SELECT hkredobr
				DO CASE 
					CASE FSIZE("id_ucet")>0
						UPDATE buff SET kredit=.t.,id_ucet=hkredobr.id_ucet from hkredobr WHERE ALLTRIM(buff.uzaverka)=LEFT(hkredobr.uzaverka,14) AND buff.u_cis=RIGHT(ALLTRIM(hkredobr.u_cis),5)
						UPDATE buff SET kredit=.t.,id_ucet=hkredobr.id_ucet from hkredobr WHERE ALLTRIM(buff.uzaverka)=LEFT(hkredobr.uzaverka,14) AND PADL(ALLTRIM(STR(99999-VAL(RIGHT(buff.u_cis,5)),5,0)),5,"0")=RIGHT(ALLTRIM(hkredobr.u_cis),5) AND EMPTY(buff.id_ucet)
						UPDATE buff SET cislo_org=hot_kred.cislo_org from hot_kred WHERE buff.id_ucet=hot_kred.id_ucet
						UPDATE buff SET nazov_org=orgstr.nazov_org from orgstr WHERE buff.cislo_org=orgstr.cislo_org
					CASE FSIZE("id_karty")>0
						UPDATE buff SET kredit=.t.,id_ucet=hkredobr.id_karty from hkredobr WHERE ALLTRIM(buff.uzaverka)=LEFT(hkredobr.uzaverka,14) AND buff.u_cis=RIGHT(ALLTRIM(hkredobr.u_cis),5)
						UPDATE buff SET kredit=.t.,id_ucet=hkredobr.id_karty from hkredobr WHERE ALLTRIM(buff.uzaverka)=LEFT(hkredobr.uzaverka,14) AND PADL(ALLTRIM(STR(99999-VAL(RIGHT(buff.u_cis,5)),5,0)),5,"0")=RIGHT(ALLTRIM(hkredobr.u_cis),5) AND EMPTY(buff.id_ucet)
						UPDATE buff SET cislo_org=hot_kred.cislo_org from hot_kred WHERE buff.id_ucet=hot_kred.id_ucet
						UPDATE buff SET nazov_org=orgstr.nazov_org from orgstr WHERE buff.cislo_org=orgstr.cislo_org
				ENDCASE 
			ENDIF 
			IF USED("_tmhkobr")
				SELECT _tmhkobr
				DO CASE 
					CASE FSIZE("id_ucet")>0
						UPDATE buff SET kredit=.t.,id_ucet=_tmhkobr.id_ucet from _tmhkobr WHERE ALLTRIM(buff.uzaverka)=LEFT(_tmhkobr.uzaverka,14) AND buff.u_cis=RIGHT(ALLTRIM(_tmhkobr.u_cis),5)
						UPDATE buff SET kredit=.t.,id_ucet=_tmhkobr.id_ucet from _tmhkobr WHERE ALLTRIM(buff.uzaverka)=LEFT(_tmhkobr.uzaverka,14) AND PADL(ALLTRIM(STR(99999-VAL(RIGHT(buff.u_cis,5)),5,0)),5,"0")=RIGHT(ALLTRIM(_tmhkobr.u_cis),5) AND EMPTY(buff.id_ucet)
						UPDATE buff SET cislo_org=hot_kred.cislo_org from hot_kred WHERE buff.id_ucet=hot_kred.id_ucet
						UPDATE buff SET nazov_org=orgstr.nazov_org from orgstr WHERE buff.cislo_org=orgstr.cislo_org
					CASE FSIZE("id_karty")>0
						UPDATE buff SET kredit=.t.,id_ucet=_tmhkobr.id_karty from _tmhkobr WHERE ALLTRIM(buff.uzaverka)=LEFT(_tmhkobr.uzaverka,14) AND buff.u_cis=RIGHT(ALLTRIM(_tmhkobr.u_cis),5)
						UPDATE buff SET kredit=.t.,id_ucet=_tmhkobr.id_karty from _tmhkobr WHERE ALLTRIM(buff.uzaverka)=LEFT(_tmhkobr.uzaverka,14) AND PADL(ALLTRIM(STR(99999-VAL(RIGHT(buff.u_cis,5)),5,0)),5,"0")=RIGHT(ALLTRIM(_tmhkobr.u_cis),5) AND EMPTY(buff.id_ucet)
						UPDATE buff SET cislo_org=hot_kred.cislo_org from hot_kred WHERE buff.id_ucet=hot_kred.id_ucet
						UPDATE buff SET nazov_org=orgstr.nazov_org from orgstr WHERE buff.cislo_org=orgstr.cislo_org
				ENDCASE 
			ENDIF 
		CASE FILE(m.crd_path+"hkredobr.dbf") AND FILE(m.crd_path+"hot_kred.dbf") 
			TRY 
				USE (m.crd_path+"hkredobr") IN 0 SHARED 
				USE (m.crd_path+"hot_kred") IN 0 SHARED 
				IF FILE(m.crd_path+"_tmhkobr.dbf")
					USE (m.crd_path+"_tmhkobr") IN 0 SHARED 
				ENDIF 
			CATCH
				DO pozavirej WITH "hkredobr, hot_kred, _tmhkobr"
			ENDTRY 
			IF USED("hkredobr")
				SELECT hkredobr
				DO CASE 
					CASE FSIZE("id_karty")>0
						UPDATE buff SET kredit=.t.,id_ucet=hkredobr.id_karty from hkredobr WHERE ALLTRIM(buff.uzaverka)=LEFT(hkredobr.uzaverka,14) AND buff.u_cis=RIGHT(ALLTRIM(hkredobr.u_cis),5)
						UPDATE buff SET kredit=.t.,id_ucet=hkredobr.id_karty from hkredobr WHERE ALLTRIM(buff.uzaverka)=LEFT(hkredobr.uzaverka,14) AND PADL(ALLTRIM(STR(99999-VAL(RIGHT(buff.u_cis,5)),5,0)),5,"0")=RIGHT(ALLTRIM(hkredobr.u_cis),5) AND EMPTY(buff.id_ucet)
						UPDATE buff SET nazov_org=hot_kred.firma from hot_kred WHERE buff.id_ucet=hot_kred.id_karty
				ENDCASE 
			ENDIF 
			IF USED("_tmhkobr")
				SELECT _tmhkobr
				DO CASE 
					CASE FSIZE("id_karty")>0
						UPDATE buff SET kredit=.t.,id_ucet=_tmhkobr.id_karty from _tmhkobr WHERE ALLTRIM(buff.uzaverka)=LEFT(_tmhkobr.uzaverka,14) AND buff.u_cis=RIGHT(ALLTRIM(_tmhkobr.u_cis),5)
						UPDATE buff SET kredit=.t.,id_ucet=_tmhkobr.id_karty from _tmhkobr WHERE ALLTRIM(buff.uzaverka)=LEFT(_tmhkobr.uzaverka,14) AND PADL(ALLTRIM(STR(99999-VAL(RIGHT(buff.u_cis,5)),5,0)),5,"0")=RIGHT(ALLTRIM(_tmhkobr.u_cis),5) AND EMPTY(buff.id_ucet)
						UPDATE buff SET nazov_org=hot_kred.firma from hot_kred WHERE buff.id_ucet=hot_kred.id_karty
				ENDCASE 
			ENDIF 
	ENDCASE 
ENDIF 
DO pozavirej WITH "hkredobr, orgstr, hot_kred, _tmhkobr"
IF "1"$m.repriadok
	SELECT nazev AS zkratka, SUM(summc) AS summc, SUM(sum_dph) AS sum_dph,;
		SUM(sumpc) AS pc, dan, SUM(mnozstvi+0.000) AS mnozstvi, "" as id_kas, druh, spart, kredit, nazov_org, SUM(round50) as round50 ;
		FROM buff INTO CURSOR buff2 READWRITE ;
		GROUP BY kredit, nazov_org, zkratka, dan;
		ORDER BY kredit, nazov_org, id_kas, spart, druh, zkratka, dan 
	DO pr_report WITH "trz_m_org_predaj_kalk", !m.reporty_volane_z_formu
ENDIF 
IF "2"$m.repriadok
	SELECT druh AS zkratka, SUM(summc) AS summc, SUM(sum_dph) AS sum_dph,;
		SUM(sumpc) AS pc, dan, "" as id_kas, "" as druh, spart, kredit, nazov_org, SUM(round50) as round50 ;
		FROM buff INTO CURSOR buff2 READWRITE ;
		GROUP BY kredit, nazov_org, zkratka, dan;
		ORDER BY kredit, nazov_org, id_kas, spart, druh, zkratka, dan 
	DO pr_report WITH "trz_m_org_predaj_druh", !m.reporty_volane_z_formu
ENDIF 
IF "3"$m.repriadok
	SELECT spart AS zkratka, SUM(summc) AS summc, SUM(sum_dph) AS sum_dph,;
		SUM(sumpc) AS pc, dan, "" as id_kas, "" as druh, "" as spart, kredit, nazov_org, SUM(round50) as round50 ;
		FROM buff INTO CURSOR buff2 READWRITE ;
		GROUP BY kredit, nazov_org, zkratka, dan;
		ORDER BY kredit, nazov_org, id_kas, spart, druh, zkratka, dan
	DO pr_report WITH "trz_m_org_predaj_spart", !m.reporty_volane_z_formu
ENDIF 
DO pozavirej WITH "buff, buff2"
RETURN 


*!*****************************************************************************
*!      Procedure: GR_TRZCIS2
*!      Trzby podla casnikov 2
*!*****************************************************************************
PROCEDURE gr_trzcis2
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
trzcis2_had = CREATEOBJECT("had", PROGRAM())
trzcis2_had.SHOW()
DO pozavirej WITH "buff"
IF !USED("druhy_pl")
	SELECT 0
	IF m.issql 
		rv("druhy_pl","druhy_pl")
	ELSE 
		USE (m.gchom_path+"druhy_pl")
	ENDIF 
ELSE
	SELECT druhy_pl
ENDIF 
IF !USED("users")
	IF m.issql
		rv("users","users")
	ELSE 
		IF FILE(m.gchom_path + "users.dbf")
			USE (m.gchom_path + "users") AGAIN IN 0
		ENDIF 
	ENDIF 
ENDIF 
IF "1" $ m.repriadok
	IF !USED("pri_buff")
		DO pripravdata
	ENDIF
	SELECT tm_sk_fin.round50, tm_sk_fin.id_kas, tm_sk_fin.uzaverka, tm_sk_fin.AUTOR, SUM(tm_sk_pol.prachy) AS prachy, tm_sk_pol.c_druh, tm_sk_fin.c_mena, SPACE(m.sirkastlpcadruh) as druh, $0 as summc ;
		GROUP BY  tm_sk_fin.id_kas, tm_sk_fin.uzaverka, tm_sk_fin.autor, tm_sk_pol.c_druh FROM tm_sk_fin LEFT JOIN tm_sk_pol ON (tm_sk_fin.uzaverka=tm_sk_pol.uzaverka) .and. (tm_sk_fin.u_cis=tm_sk_pol.u_cis);
		INTO CURSOR buffx READWRITE  ORDER BY tm_sk_fin.id_kas, tm_sk_fin.uzaverka, tm_sk_fin.autor, tm_sk_pol.c_druh
	UPDATE buffx SET druh=skupiny.druh from skupiny WHERE buffx.c_druh=skupiny.c_druh
	SELECT buffx
	REPLACE prachy WITH NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena)) FOR c_mena<>m.girep_mena
	SELECT  SUM(summc) as summc, uzaverka, autor, id_kas, c_druh, SUM(round50) as round50 FROM pri_buff GROUP BY id_kas, uzaverka, autor INTO CURSOR spotreby
	UPDATE buffx SET summc=spotreby.summc, round50=spotreby.round50 from spotreby WHERE buffx.id_kas=spotreby.id_kas AND buffx.uzaverka=spotreby.uzaverka AND buffx.autor=spotreby.autor AND buffx.c_druh=spotreby.c_druh
	USE IN spotreby


	SELECT id_kas, AUTOR, SUM(prachy) AS prachy, SUM(summc) as summc, druh, SUM(round50) as round50;
		GROUP BY  id_kas, autor, druh FROM buffx ;
		INTO CURSOR buff READWRITE ORDER BY id_kas, autor, druh
	SELECT  autor, id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas, autor INTO CURSOR spotreby
	UPDATE buff SET round50=spotreby.round50 from spotreby WHERE buff.id_kas=spotreby.id_kas AND buff.autor=spotreby.autor
	USE IN spotreby
	SELECT buff
	LOCAL m.id_kas, m.autor
	m.id_kas="*/"
	m.autor="/*-"
	SCAN ALL 
		IF buff.id_kas=m.id_kas AND buff.autor=m.autor
			REPLACE round50 WITH 0
		ELSE
			m.id_kas=buff.id_kas
			m.autor=buff.autor
		ENDIF 
	ENDSCAN 
	SELECT buff
	trzcis2_had.HIDE()
	DO pr_report WITH "trz_i_cis_druh", !m.reporty_volane_z_formu
	trzcis2_had.SHOW()
	USE IN BUFF
	SELECT id_kas, AUTOR, SUM(prachy) AS prachy, SUM(summc) as summc, druh, round50;
		GROUP BY  id_kas, autor, druh FROM buffx ;
		INTO CURSOR buff READWRITE ORDER BY id_kas, druh, autor
	SELECT  autor, id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas, autor INTO CURSOR spotreby
	UPDATE buff SET round50=spotreby.round50 from spotreby WHERE buff.id_kas=spotreby.id_kas AND buff.autor=spotreby.autor
	USE IN spotreby
	SELECT buff
	m.id_kas="*/"
	m.autor="/*-"
	SCAN ALL 
		IF buff.id_kas=m.id_kas AND buff.autor=m.autor
			REPLACE round50 WITH 0
		ELSE
			m.id_kas=buff.id_kas
			m.autor=buff.autor
		ENDIF 
	ENDSCAN 
	trzcis2_had.HIDE()
	DO pr_report WITH "trz_i_cis_druh2", !m.reporty_volane_z_formu
	trzcis2_had.SHOW()
	USE IN BUFF
	USE IN buffx
ENDIF
IF "2" $ m.repriadok
	SELECT tm_sk_fin.id_kas, tm_sk_fin.uzaverka, tm_sk_fin.u_cis, tm_sk_fin.AUTOR, SUM(tm_sk_plt.cena_pl) AS cena_pl, tm_sk_plt.druh_pl, tm_sk_fin.c_mena, SPACE(30) as menoplatby, tm_sk_fin.cena_celk-tm_sk_fin.sleva_abs as cena_slev,;
		 tm_sk_fin.dan1+tm_sk_fin.dan2+tm_sk_fin.dan3+tm_sk_fin.dan4 as dph_celk ;
		GROUP BY  tm_sk_fin.id_kas, tm_sk_fin.uzaverka, tm_sk_fin.u_cis, tm_sk_fin.autor, tm_sk_plt.druh_pl FROM tm_sk_fin LEFT JOIN tm_sk_plt ON (tm_sk_fin.uzaverka=tm_sk_plt.uzaverka) .and. (tm_sk_fin.u_cis=tm_sk_plt.u_cis);
		INTO CURSOR buff READWRITE  ORDER BY tm_sk_fin.id_kas, tm_sk_fin.uzaverka, tm_sk_fin.autor, tm_sk_fin.u_cis, tm_sk_plt.druh_pl
	REPLACE cena_pl WITH 0 FOR ISNULL(cena_pl)
	REPLACE dph_celk WITH dph_celk*(cena_pl/cena_slev) FOR cena_slev<>0 AND cena_pl<>cena_slev
	REPLACE ALL cena_slev WITH cena_pl-dph_celk 
	REPLACE cena_pl WITH NTOM(MTON(cena_pl)*en__meny(c_mena,m.girep_mena)) FOR c_mena<>m.girep_mena
	IF !USED("druhy_pl")
		SELECT 0
		IF m.issql 
			rv("druhy_pl","druhy_pl")
		ELSE 
			USE (m.gchom_path+"druhy_pl")
		ENDIF 
	ELSE
		SELECT druhy_pl
	ENDIF 
    UPDATE buff SET menoplatby=druhy_pl.j0 from druhy_pl WHERE ALLTRIM(buff.druh_pl)==ALLTRIM(druhy_pl.nazev)    
    SELECT buff
    REPLACE menoplatby WITH i_drupla(buff.druh_pl) FOR EMPTY(buff.menoplatby) OR ISNULL(buff.menoplatby)
	trzcis2_had.HIDE()
	SELECT distinct autor, SPACE(25) as druh, .t. as lbl FROM buff INTO CURSOR vybery READWRITE 
	IF RECCOUNT()>0
		REPLACE ALL druh WITH i_mencis("01",autor)
		SELECT vybery
		INDEX on autor TAG autor
		INDEX ON druh TAG druh
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druh",150 TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO autor
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO autor INTO vybery
			DELETE FOR autor=vybery.autor .AND. EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT BUFF
			DO pr_report WITH "trz_i_cis_ucty", !m.reporty_volane_z_formu
		ENDIF
	ENDIF
	DO pozavirej WITH "buff"
	trzcis2_had.SHOW()
ENDIF
IF "3"$m.repriadok 
	IF !USED("pri_buff")
		DO pripravdata
	ENDIF
	SELECT SUM(sumpc) AS prachy, id_kas, LEFT(pl_cas,2) as pl_hodina, u_cis, uzaverka, SUM(round50) as round50,;
		autor FROM pri_buff INTO CURSOR pom_buff READWRITE GROUP BY id_kas, uzaverka, u_cis, autor;
		ORDER BY id_kas, u_cis
	SELECT SUM(prachy) AS prachy, id_kas, autor, pl_hodina, COUNT(pl_hodina) as pocet, SUM(round50) as round50 ;
		FROM pom_buff INTO CURSOR BUFF GROUP BY id_kas, autor, pl_hodina;
		ORDER BY id_kas, autor, pl_hodina
	DO pr_report WITH "trz_i_kas_hod_casnik", !m.reporty_volane_z_formu
	DO pozavirej WITH "buff, pom_buff"
ENDIF 
IF "4" $ m.repriadok
	IF !USED("buf_ucet")
		DO pripravdata
	ENDIF
	DO pozavirej WITH "buff, vybery"
	DO tvor_dph1 IN sk_stati
	SELECT * , 0.00000 AS koef FROM dph1 INTO CURSOR dph11 READWRITE 
	SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_pl, dan FROM dph1 INTO CURSOR dph111 READWRITE  GROUP BY uzaverka, u_cis, dan
	INDEX ON uzaverka+u_cis+STR(dan,5,2) TAG uzuc
	SELECT dph11
	SET RELATION TO uzaverka+u_cis+STR(dan,5,2) INTO dph111
	REPLACE koef WITH 1 FOR dph111.cena_pl=0
	REPLACE koef WITH dph11.cena_pl/dph111.cena_pl FOR dph111.cena_pl<>0
	USE IN dph111
	SELECT pri_buff.*,dph11.koef,dph11.druh_pl AS druh_plat FROM pri_buff, dph11 WHERE pri_buff.uzaverka=dph11.uzaverka .AND. pri_buff.u_cis=dph11.u_cis .AND. pri_buff.dan=dph11.dan;
		INTO CURSOR pribuff READWRITE 
	SELECT * FROM pribuff WHERE koef<>1 INTO CURSOR pribuff1 READWRITE 
	REPLACE ALL mnozstvi WITH mnozstvi*(koef)
	REPLACE sumpc WITH sumpc*(koef) ALL
	REPLACE sum_dph WITH sum_dph*(koef) ALL
	SELECT pribuff
	DELETE FOR koef<>1
	INSERT INTO pribuff (id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
		sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat) ;
		SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
		sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat FROM pribuff1
	SELECT id_kas, sumpc as prachy, druh_plat as druh_k, druh as druh, u_cis, uzaverka, sum_dph as sum_dph, autor, spart,round50,;
		(sumpc-sum_dph) as dan_zakl, dan, cen_hlad FROM pribuff INTO CURSOR dph3 READWRITE 
	DO pozavirej WITH "pribuff, pribuff1, dph11"
	SELECT DISTINCT autor, druh, spart, id_kas AS ID, druh_k,;
		SUM(prachy) AS prachy, SUM(sum_dph) AS cena_dan2, 0000000.0000 AS cena_dan1, SUM(round50) as round50 ;
		GROUP BY autor, druh_k, druh, ID FROM dph3 INTO CURSOR pomkk READWRITE 
	SELECT DISTINCT druh_k, SPACE(m.sirkastlpcadruh) AS druh,.T. AS lbl, .f. as fiskal FROM pomkk INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_k in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	REPLACE ALL druh WITH i_drupla(druh_k)
	INDEX on druh_k TAG druh_k
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_k
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			m.txthlavicka="Iba vybrané platby"
		ELSE
			m.txthlavicka=""
		ENDIF
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT pomkk
			SET RELATION TO druh_k INTO vybery
			DELETE FOR ALLTRIM(druh_k)=ALLTRIM(vybery.druh_k).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT DISTINCT autor, druh, ID, SPACE(25) AS id_zkratka , spart, "" as druh_k,;
				SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2, 0000000.0000 AS cena_dan1, 0000000.0000 AS cena_netto, SUM(round50) as round50 ;
				GROUP BY autor, druh, ID FROM pomkk INTO CURSOR pom READWRITE 
  **DUO**
            REPLACE ALL cena_netto WITH prachy - cena_dan1 - cena_dan2
  **DUO**
			REPLACE FOR EMPTY(druh) druh WITH m.ostatni
			UPDATE pom SET id_zkratka=fp_str.id_zkratka from fp_str WHERE pom.id=fp_str.id
			UPDATE pom SET spart=skupiny.spart from skupiny WHERE LEFT(pom.druh,10)=skupiny.druh
			REPLACE FOR EMPTY(spart) spart WITH "___"
			REPLACE FOR EMPTY(id_zkratka) id_zkratka WITH m.ostatni
			INDEX ON ID + autor + spart + druh TAG pom
			DO pr_report WITH "trz_i_kas_casnik_druh_druhpl", !m.reporty_volane_z_formu
		ENDIF
	ENDIF
	DO pozavirej WITH "vybery, pomkk, pom"
ENDIF

DO pozavirej WITH "users"
RELEASE trzcis2_had



*!*****************************************************************************
*!      Procedure: S_DR_STR2
*!*****************************************************************************
PROCEDURE s_dr_str2
PARAMETERS repriadok
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
DO pozavirej WITH "buff, vybery"
IF !USED("druhy_pl")
	SELECT 0
	IF m.issql 
		rv("druhy_pl","druhy_pl")
	ELSE 
		USE (m.gchom_path+"druhy_pl")
	ENDIF 
ELSE
	SELECT druhy_pl
ENDIF 
DO tvor_dph1 IN sk_stati
SELECT * , 0.00000 AS koef FROM dph1 INTO CURSOR dph11 READWRITE 
SELECT uzaverka, u_cis, SUM(cena_pl) AS cena_pl, dan FROM dph1 INTO CURSOR dph111 READWRITE  GROUP BY uzaverka, u_cis, dan
INDEX ON uzaverka+u_cis+STR(dan,5,2) TAG uzuc
SELECT dph11
SET RELATION TO uzaverka+u_cis+STR(dan,5,2) INTO dph111
REPLACE koef WITH 1 FOR dph111.cena_pl=0
REPLACE koef WITH dph11.cena_pl/dph111.cena_pl FOR dph111.cena_pl<>0
USE IN dph111
SELECT pri_buff.*,dph11.koef,dph11.druh_pl AS druh_plat FROM pri_buff, dph11 WHERE pri_buff.uzaverka=dph11.uzaverka .AND. pri_buff.u_cis=dph11.u_cis .AND. pri_buff.dan=dph11.dan;
	INTO CURSOR pribuff READWRITE 
SELECT * FROM pribuff WHERE koef<>1 INTO CURSOR pribuff1 READWRITE 
REPLACE ALL mnozstvi WITH mnozstvi*(koef)
REPLACE sumpc WITH sumpc*(koef) ALL
REPLACE sum_dph WITH sum_dph*(koef) ALL
SELECT pribuff
DELETE FOR koef<>1
*milan

INSERT INTO pribuff (id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
	sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat) ;
	SELECT id_kas, id_man, uzaverka, cen_hlad, c_karty, nazev, u_cis, dan, sumpc, pl_cas, summc, mnozstvi, sum_dph,;
	sumpc_dan, round50, dan1, zlava, celkom, sumdanzakl, druh, druh_pl, TEXT, autor, spart, c_mena, dan01, koef, druh_plat FROM pribuff1
SELECT id_kas, sumpc as prachy, druh_plat as druh_k, druh as druh, u_cis, uzaverka, sum_dph as sum_dph, round50,;
	(sumpc-sum_dph) as dan_zakl, dan, cen_hlad FROM pribuff INTO CURSOR dph3 READWRITE 
DO pozavirej WITH "pribuff, pribuff1, dph11"
IF "1"$m.repriadok
	SELECT id_kas,uzaverka, "   " AS spart, dan, druh, SUM(prachy) AS prachy, SUM(sum_dph) AS cena_dan2, druh_k, SUM(round50) as round50 ;
		GROUP BY druh_k,druh,dan,uzaverka, id_kas FROM dph3 INTO CURSOR pomkk READWRITE 
	UPDATE pomkk SET spart=skupiny.spart from skupiny WHERE LEFT(pomkk.druh,10)=skupiny.druh
	REPLACE FOR EMPTY(spart) spart WITH "___"
	SELECT DISTINCT druh_k, SPACE(m.sirkastlpcadruh) AS druh,.T. AS lbl, .f. as fiskal FROM pomkk INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_k in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	REPLACE ALL druh WITH i_drupla(druh_k)
	INDEX on druh_k TAG druh_k
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_k
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			m.txthlavicka="Iba vybrané platby"
		ELSE
			m.txthlavicka=""
		ENDIF
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT pomkk
			SET RELATION TO druh_k INTO vybery
			DELETE FOR ALLTRIM(druh_k)=ALLTRIM(vybery.druh_k).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT id_kas, SPACE(25) AS id_zkratka, spart, dan, uzaverka, SUM(round50) as round50, ;
				SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2;
				GROUP BY spart, dan, uzaverka, id_kas FROM pomkk INTO CURSOR i_tr_sp READWRITE 
			UPDATE i_tr_sp SET id_zkratka=fp_str.id_zkratka from fp_str WHERE i_tr_sp.id_kas=fp_str.id
			REPLACE FOR EMPTY(id_zkratka) id_zkratka WITH m.ostatni
			SELECT 'XX' AS id_kas,'Spolu' AS id_zkratka, spart, dan, SUM(round50) as round50, ;
				SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2 ;
				GROUP BY spart,dan FROM i_tr_sp INTO CURSOR i_tr_ READWRITE 
			SELECT i_tr_sp
			APPEND FROM DBF("i_tr_")
*			INSERT INTO i_tr_sp (id_kas, id_zkratka, spart, dan, prachy, cena_dan2) SELECT id_kas, id_zkratka, spart, dan, prachy, cena_dan2 FROM i_tr_
			INDEX ON id_kas + uzaverka+ spart + STR(dan,4,2) TAG aaa 
			DO pr_report WITH "trz_i_dr_spart_uzav", !m.reporty_volane_z_formu
			DO pozavirej WITH "i_tr_,i_tr_sp"
		ENDIF
	ENDIF
	USE IN vybery
	USE IN pomkk
ENDIF
IF "2"$m.repriadok
	SELECT id_kas,uzaverka, "   " AS spart, dan, druh, SUM(prachy) AS prachy, SUM(sum_dph) AS cena_dan2, druh_k, DATE() as datum, SUM(round50) as round50 ;
		GROUP BY druh_k, druh, dan, uzaverka, id_kas FROM dph3 INTO CURSOR pomkk1 READWRITE 
	UPDATE pomkk1 SET datum=tm_sk_sez_kas.uct_den from tm_sk_sez_kas WHERE pomkk1.uzaverka=tm_sk_sez_kas.uzaverka
	SELECT datum, "   " AS spart, dan, druh, SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2, druh_k, SUM(round50) as round50 ;
		GROUP BY druh_k, druh, dan, datum FROM pomkk1 INTO CURSOR pomkk READWRITE 
	USE IN pomkk1
	UPDATE pomkk SET spart=skupiny.spart from skupiny WHERE LEFT(pomkk.druh,10)=skupiny.druh
	REPLACE FOR EMPTY(spart) spart WITH "___"
	SELECT DISTINCT druh_k, SPACE(m.sirkastlpcadruh) AS druh,.T. AS lbl, .f. as fiskal FROM pomkk INTO CURSOR vybery READWRITE 
	UPDATE vybery SET fiskal=.t. WHERE druh_k in (select nazev from druhy_pl WHERE cisfplat>0)
	SELECT vybery
	REPLACE ALL druh WITH i_drupla(druh_k)
	INDEX on druh_k TAG druh_k
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery","druhpl",200, .t. TO m.druhxx
		ENDIF 
		SELECT vybery
		SET ORDER TO druh_k
		LOCATE FOR EMPTY(lbl)
		IF FOUND()
			m.txthlavicka="Iba vybrané platby"
		ELSE
			m.txthlavicka=""
		ENDIF
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT pomkk
			SET RELATION TO druh_k INTO vybery
			DELETE FOR ALLTRIM(druh_k)=ALLTRIM(vybery.druh_k).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			SELECT datum, spart, dan, SUM(prachy) AS prachy, SUM(cena_dan2) AS cena_dan2, SUM(round50) as round50;
				GROUP BY datum, spart, dan FROM pomkk INTO CURSOR i_tr_sp READWRITE 
			DELETE FOR prachy=0 AND cena_dan2=0
			INDEX ON DTOS(datum) + spart + STR(dan,4,2) TAG aaa 
			DO pr_report WITH "trz_i_dr_spart_dni", !m.reporty_volane_z_formu
			DO pozavirej WITH "i_tr_,i_tr_sp"
		ENDIF
	ENDIF
	USE IN vybery
	USE IN pomkk
ENDIF


*!*****************************************************************************
*!      Procedure: GR_PREDAJNOST
*!      Predajnost kalkulacii
*!      Oproti poctu predanych jdal tlaci aj tie kalkulacie, ktore neboli predane vobec
*!*****************************************************************************
PROCEDURE gr_predajnost
PARAMETERS repriadok
PRIVATE m.str, m.oldprc
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
DO pozavirej WITH "buff, vybery"
m.cen_hlad="ZZ"           &&&& co se do toho soupne?
IF "1"$m.repriadok .OR. "2"$m.repriadok
	SELECT nazev AS zkratka, SUM(summc) AS summc, SUM(sum_dph) AS sum_dph, SUM(round50) as round50,k_odpoctu,;
		SUM(sumpc) AS pc, dan, SUM(mnozstvi) AS mnozstvi, "  " as id_kas, "  " as id_man, druh, "  " as cen_hlad, spart, c_druh, c_karty;
		FROM pri_buff INTO CURSOR buff READWRITE ;
		GROUP BY zkratka, dan;
		ORDER BY druh, zkratka, dan WHERE c_karty<>-99
	INSERT INTO buff (zkratka, c_druh, c_karty) SELECT nazev as zkratka, c_druhk as c_druh, c_karty FROM karty WHERE !EMPTY(c_druhk) AND nazev1<>m.smazana_karta AND !(c_karty IN (SELECT c_karty FROM buff))
	UPDATE buff SET druh=skupiny.druh, spart=skupiny.spart, dan=skupiny.dan01 from skupiny WHERE buff.c_druh=skupiny.c_druh AND buff.c_druh>0 AND EMPTY(buff.druh)

	SELECT DISTINCT druh, .T. AS lbl,.f. as kalkulacia FROM BUFF INTO CURSOR vybery READWRITE
	UPDATE vybery SET kalkulacia=!EMPTY(skupiny.typ_kalk) from skupiny WHERE vybery.druh=skupiny.druh 
	INDEX ON druh TAG druh
	
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		IF m.reporty_volane_z_formu
			DO FORM seldruhy WITH "vybery" TO m.druhxx
			filtrujpriznaky("buff")
		ENDIF 
		RELEASE m.gcfilter_triedenia
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT BUFF
			SET RELATION TO druh INTO vybery
			DELETE FOR druh=vybery.druh .AND. EMPTY(vybery.lbl)
			SELECT DISTINCT zkratka as druh, .T. AS lbl FROM BUFF INTO CURSOR vybery READWRITE 
			INDEX ON druh TAG druh
			IF RECCOUNT()>0
				IF m.reporty_volane_z_formu
					DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
				ENDIF 
				SELECT vybery
				LOCATE FOR !EMPTY(lbl)
				IF FOUND()
					SELECT BUFF
					SET RELATION TO zkratka INTO vybery
					DELETE FOR zkratka=vybery.druh .AND. EMPTY(vybery.lbl)
					IF "1"$m.repriadok
						m.txthlavicka="Po druhoch"
						INDEX on druh+ STR(mnozstvi,10,3) TAG aaa
						DO pr_report WITH "trz_i_Pockal@preddruh", !m.reporty_volane_z_formu
						SELECT buff
					ENDIF
					IF "2"$m.repriadok
						m.txthlavicka="Spolu"
						INDEX ON STR(mnozstvi,10,3)+zkratka TAG aaa
						DO pr_report WITH "trz_i_Pockak@predspolu", !m.reporty_volane_z_formu
					ENDIF
				ENDIF 
			ENDIF 
		ENDIF 
	ENDIF 
	USE IN vybery
ENDIF 
IF "3"$m.repriadok AND m.reporty_volane_z_formu
	SELECT DISTINCT nazev, druh FROM  pri_buff INTO CURSOR vybery READWRITE ORDER BY nazev
		IF RECCOUNT()>0
		DO WHILE .t.
			DO pozavirej WITH "buff"
			m.nazev=""
			m.txthlavicka=""
			DO FORM dajdruh WITH "","vybery","nazev",300, "druh", 100 TO m.nazev
			IF EMPTY(m.nazev)
				EXIT
			ELSE
				m.txthlavicka=m.nazev
				SELECT id_kas, id_man, uzaverka, u_cis, dan, sumpc, mnozstvi, sum_dph, dan1, autor, cen_hlad FROM pri_buff INTO CURSOR buff WHERE nazev=m.nazev ORDER BY id_kas, uzaverka, u_cis
				DO pr_report WITH "trz_i_predkalk", !m.reporty_volane_z_formu
			ENDIF 
		ENDDO 
	ENDIF 
	DO pozavirej WITH "vybery, buff"
ENDIF 



PROCEDURE pocty_uctov
PARAMETERS repriadok
PRIVATE m.titulok
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
SELECT id_kas, cen_hlad, LEFT(text,1) as priznak, .f. as storno, SUM(sumpc) as sumpc, SUM(teorpc) as teorpc, SUM(summc) as summc, SUM(sumpc-sum_dph) as sumdanzakl, SUM(round50) as round50,;
	000000 as pocet_h, 01 as pocet_u, 000000000.000 as pocet_p, 000000000 as ph, 000000000 as pu, 000000000.000 as pp, $0 as pc, uzaverka, u_cis, LEFT(pl_cas,2) as pl_cas, cas_atrib FROM pri_buff ;
	GROUP BY uzaverka, u_cis, cen_hlad INTO CURSOR sumaructov READWRITE 
UPDATE sumaructov SET pocet_h=tm_sk_fin.pocet_h, pc=(tm_sk_fin.cena_celk-tm_sk_fin.sleva_abs) from tm_sk_fin WHERE sumaructov.uzaverka=tm_sk_fin.uzaverka AND sumaructov.u_cis=tm_sk_fin.u_cis
SELECT cen_hlad, uzaverka, u_cis, SUM(mnozstvi) as pocet_p FROM pri_buff GROUP BY uzaverka, u_cis, cen_hlad INTO CURSOR aaa 
UPDATE sumaructov SET pocet_p=aaa.pocet_p from aaa WHERE sumaructov.uzaverka=aaa.uzaverka AND sumaructov.u_cis=aaa.u_cis AND sumaructov.cen_hlad=aaa.cen_hlad
SELECT uzaverka, u_cis, SUM(mnozstvi) as pocet_p FROM pri_buff GROUP BY uzaverka, u_cis INTO CURSOR aaa 
UPDATE sumaructov SET pp=aaa.pocet_p from aaa WHERE sumaructov.uzaverka=aaa.uzaverka AND sumaructov.u_cis=aaa.u_cis
UPDATE sumaructov SET pocet_u=-pocet_u, pocet_p=-pocet_p WHERE INLIST(priznak,"^","@")
UPDATE sumaructov SET pu=pocet_u, ph=pocet_h WHERE .t.
SELECT sumaructov
INDEX on uzaverka+u_cis TAG aaa
m.ucis="XXXX"
m.uzav="XXXXX"
SCAN ALL
	IF u_cis=m.ucis .AND. uzaverka=m.uzav
		REPLACE pu WITH 0, ph WITH 0, pc WITH 0, pp WITH 0
	ELSE 
		m.ucis=u_cis
		m.uzav=uzaverka
	ENDIF
ENDSCAN
IF "1"$m.repriadok 
	SELECT id_kas, cen_hlad, SUM(pocet_p) as pocet_p, SUM(pocet_u) as pocet_u, SUM(pocet_h) as pocet_h, SUM(sumpc) as sumpc, SUM(teorpc) as teorpc, SUM(sumdanzakl) as sumdanzakl, SUM(summc) as summc, SUM(pc) as pc, SUM(pp) as pp, SUM(ph) as ph, SUM(pu) as pu, SUM(round50) as round50 FROM sumaructov;
		GROUP BY id_kas, cen_hlad INTO CURSOR buffx
	m.titulok="Sumár po kasách"
	DO pr_report WITH "trz_i_hlad_pocty_sum", !m.reporty_volane_z_formu
ENDIF 
IF "2"$m.repriadok 
	SELECT id_kas, uzaverka, cen_hlad, SUM(pocet_p) as pocet_p, SUM(pocet_u) as pocet_u, SUM(pocet_h) as pocet_h, SUM(sumpc) as sumpc, SUM(teorpc) as teorpc, SUM(sumdanzakl) as sumdanzakl, SUM(summc) as summc, SUM(pc) as pc, SUM(pp) as pp, SUM(ph) as ph, SUM(pu) as pu, SUM(round50) as round50 FROM sumaructov;
		GROUP BY id_kas, uzaverka, cen_hlad INTO CURSOR buffx
	m.titulok="Po uzávierkach"
	DO pr_report WITH "trz_i_hlad_pocty_uzav", !m.reporty_volane_z_formu
ENDIF 
IF "3"$m.repriadok 
	SELECT id_kas, pl_cas, cen_hlad, SUM(pocet_p) as pocet_p, SUM(pocet_u) as pocet_u, SUM(pocet_h) as pocet_h, SUM(sumpc) as sumpc, SUM(teorpc) as teorpc, SUM(sumdanzakl) as sumdanzakl, SUM(summc) as summc, SUM(pc) as pc, SUM(pp) as pp, SUM(ph) as ph, SUM(pu) as pu, SUM(round50) as round50 FROM sumaructov;
		GROUP BY id_kas, pl_cas, cen_hlad INTO CURSOR buffx
	m.titulok="Podľa času vystavenia účtu"
	DO pr_report WITH "trz_i_hlad_pocty_cas", !m.reporty_volane_z_formu
ENDIF 
RELEASE m.titulok


PROCEDURE vynosy_cen_hl
PARAMETERS repriadok
PRIVATE m.titulok
IF !USED("buf_ucet")
	DO pripravdata
ENDIF
SELECT id_kas, cen_hlad, LEFT(text,1) as priznak, .f. as storno, SUM(sumpc) as sumpc, SUM(teorpc) as teorpc, SUM(summc) as summc, SUM(sumpc-sum_dph) as sumdanzakl, SUM(round50) as round50, ;
	 $0 as pc, uzaverka, u_cis, LEFT(pl_cas,2) as pl_cas, cas_atrib FROM pri_buff ;
	GROUP BY uzaverka, u_cis, cen_hlad INTO CURSOR sumaructov READWRITE 
UPDATE sumaructov SET pc=(tm_sk_fin.cena_celk-tm_sk_fin.sleva_abs) from tm_sk_fin WHERE sumaructov.uzaverka=tm_sk_fin.uzaverka AND sumaructov.u_cis=tm_sk_fin.u_cis
SELECT sumaructov
INDEX on uzaverka+u_cis TAG aaa
m.ucis="XXXX"
m.uzav="XXXXX"
SCAN ALL
	IF u_cis=m.ucis .AND. uzaverka=m.uzav
		REPLACE pc WITH 0
	ELSE 
		m.ucis=u_cis
		m.uzav=uzaverka
	ENDIF
ENDSCAN
IF "1"$m.repriadok 
	SELECT id_kas, cen_hlad, SUM(sumpc) as sumpc, SUM(teorpc) as teorpc, SUM(sumdanzakl) as sumdanzakl, SUM(summc) as summc, SUM(pc) as pc, 0000000.00 as marza,  0000000.00 as fc, SUM(round50) as round50 FROM sumaructov;
		GROUP BY id_kas, cen_hlad INTO CURSOR buffx READWRITE 
	REPLACE marza WITH (sumdanzakl-summc)/summc*100 FOR summc<>0
	REPLACE marza WITH 9999 FOR summc=0
	REPLACE fc WITH summc/sumdanzakl*100 FOR sumdanzakl<>0
	REPLACE fc WITH 9999 FOR sumdanzakl=0
	m.titulok="Sumár po kasách"
	DO pr_report WITH "trz_i_hlad_vynos_sum", !m.reporty_volane_z_formu
ENDIF 
IF "2"$m.repriadok 
	SELECT id_kas, uzaverka, cen_hlad, SUM(sumpc) as sumpc, SUM(teorpc) as teorpc, SUM(sumdanzakl) as sumdanzakl, SUM(summc) as summc, SUM(pc) as pc, 0000000.00 as marza,  0000000.00 as fc, SUM(round50) as round50 FROM sumaructov;
		GROUP BY id_kas, uzaverka, cen_hlad INTO CURSOR buffx READWRITE 
	REPLACE marza WITH (sumdanzakl-summc)/summc*100 FOR summc<>0
	REPLACE marza WITH 9999 FOR summc=0
	REPLACE fc WITH summc/sumdanzakl*100 FOR sumdanzakl<>0
	REPLACE fc WITH 9999 FOR sumdanzakl=0
	m.titulok="Po uzávierkach"
	DO pr_report WITH "trz_i_hlad_vynos_uzav", !m.reporty_volane_z_formu
ENDIF 
IF "3"$m.repriadok 
	SELECT id_kas, pl_cas, cen_hlad, SUM(sumpc) as sumpc, SUM(teorpc) as teorpc, SUM(sumdanzakl) as sumdanzakl, SUM(summc) as summc, SUM(pc) as pc, 0000000.00 as marza,  0000000.00 as fc, SUM(round50) as round50 FROM sumaructov;
		GROUP BY id_kas, pl_cas, cen_hlad INTO CURSOR buffx READWRITE 
	REPLACE marza WITH (sumdanzakl-summc)/summc*100 FOR summc<>0
	REPLACE marza WITH 9999 FOR summc=0
	REPLACE fc WITH summc/sumdanzakl*100 FOR sumdanzakl<>0
	REPLACE fc WITH 9999 FOR sumdanzakl=0
	m.titulok="Podľa času vystavenia účtu"
	DO pr_report WITH "trz_i_hlad_vynos_cas", !m.reporty_volane_z_formu
ENDIF 
RELEASE m.titulok


*!*****************************************************************************
*!      Procedure: ROZPIS_ODPOCTU
*!      Sumár predaných jedál spolu so sumárom kariet, ktoré boli k nim odpočítané
*!*****************************************************************************
PROCEDURE rozpis_odpoctu
PARAMETERS repriadok
PRIVATE m.xxkas, m.xxuzaverka, m.xxman, m.xxhlad, m.xxporadie

IF !USED("buf_ucet")
	DO pripravdata
ENDIF
IF !USED("obr_sum") OR !USED("odecteno")
	DO pozavirej WITH "obr_sum, odecteno"
	DO tvor_obrsum
ENDIF 
DO CASE 
	CASE "1"$m.repriadok OR "2"$m.repriadok 
		SELECT 0 AS id_limit, "" as text, 0 AS limit, 0 as osob, id_kas, uzaverka, cen_hlad, id_man, nazev, SUM(mnozstvi) as mnozstvi, SUM(sumpc) as prachy, SUM(summc) as nakup, SUBSTR(uzaverka,3,12)+"/"+id_kas as pz2, 0000000000 as doklad, SPACE(30) as kdo_kdy, SPACE(30) as meno, 000000000.0000 as pocet, 0000000000.0000 as hodnota, " " as poradi, .f. as sumdoklad, 0000000000 as porx, SUM(round50) as round50 FROM pri_buff INTO CURSOR xbuff GROUP BY id_kas, uzaverka, cen_hlad, id_man, nazev READWRITE  ORDER BY id_kas, uzaverka, cen_hlad, id_man
		m.xxkas="++"
		m.xxuzaverka="++"
		m.xxman="++"
		m.xxhlad="++"
		m.xxporadie=1
		SCAN ALL 
			IF xbuff.id_kas=m.xxkas AND xbuff.uzaverka=m.xxuzaverka AND xbuff.id_man=m.xxman AND xbuff.cen_hlad=m.xxhlad
				m.xxporadie=m.xxporadie+1
			ELSE
				m.xxkas=xbuff.id_kas		
				m.xxuzaverka=xbuff.uzaverka
				m.xxman=xbuff.id_man
				m.xxhlad=xbuff.cen_hlad
				m.xxporadie=1
			ENDIF 
			REPLACE porx WITH m.xxporadie
		ENDSCAN 

		IF "1"$m.repriadok 
			SELECT odecteno.pz2, odecteno.cen_hlad, odecteno.c_stredisk, fp_str.id as id_man, odecteno.nazev, -1 as doklad, SUM(odecteno.mnozstvi) as mnozstvi, SUM(odecteno.mnozstvi*odecteno.cena) as hodnota, .f. as sumdoklad, "A" as poradi, "" as kdo_kdy FROM odecteno LEFT JOIN fp_str ON odecteno.c_stredisk=fp_str.c_stredisk INTO CURSOR sumspot GROUP BY odecteno.pz2, odecteno.cen_hlad, odecteno.c_stredisk, odecteno.nazev 
			SELECT id_kas, uzaverka, id_man, cen_hlad, COUNT(id_kas) as pocet FROM xbuff INTO CURSOR aaxxss GROUP BY id_kas, uzaverka, id_man, cen_hlad
			SCAN ALL 
				m.xxkas=aaxxss.id_kas		
				m.xxuzaverka=aaxxss.uzaverka
				m.xxman=aaxxss.id_man
				m.xxhlad=aaxxss.cen_hlad
				m.xxpocet=aaxxss.pocet
				SELECT *, 000000 as porx FROM sumspot INTO CURSOR ssumx ORDER BY poradi WHERE pz2=SUBSTR(m.xxuzaverka,3,12)+"/"+m.xxkas AND id_man=m.xxman AND cen_hlad=m.xxhlad READWRITE 
				REPLACE ALL porx WITH RECNO()
				UPDATE xbuff SET meno=ssumx.nazev, pocet=ssumx.mnozstvi, hodnota=ssumx.hodnota, sumdoklad=ssumx.sumdoklad, poradi=ssumx.poradi from ssumx WHERE xbuff.pz2=ssumx.pz2 AND xbuff.cen_hlad=ssumx.cen_hlad AND xbuff.id_man=ssumx.id_man AND xbuff.porx=ssumx.porx
				IF USED("pridanie")
					INSERT INTO pridanie(id_kas, uzaverka, id_man, cen_hlad, meno, pocet, hodnota, doklad, sumdoklad, poradi, porx, pz2) SELECT m.xxkas as id_kas, m.xxuzaverka as uzaverka, id_man, cen_hlad, nazev as meno, mnozstvi as pocet, hodnota, doklad, sumdoklad, poradi, porx, pz2 FROM ssumx WHERE porx>m.xxpocet 
				ELSE
					SELECT m.xxkas as id_kas, m.xxuzaverka as uzaverka, id_man, cen_hlad, nazev as meno, mnozstvi as pocet, hodnota, doklad, sumdoklad, poradi, porx, pz2 FROM ssumx WHERE porx>m.xxpocet INTO CURSOR pridanie READWRITE 
				ENDIF 
			ENDSCAN 	
			SELECT xbuff
			IF USED("pridanie")
				APPEND FROM DBF("pridanie")
			ENDIF 
			INDEX on id_kas+uzaverka+cen_hlad+id_man+STR(porx,10,0) TAG aaa
			m.txthlavicka="Sumár"
			DO pr_report WITH "rozpis_odpoctu" 
			DO pozavirej WITH "aaxxss, pridanie, sumspot, ssumx"
		ENDIF 

		IF "2"$m.repriadok 
			SELECT xbuff
			REPLACE ALL meno WITH "", pocet WITH 0, hodnota WITH 0, doklad WITH -1, sumdoklad WITH .f., poradi WITH "A"
			SELECT odecteno.pz2, odecteno.cen_hlad, odecteno.c_stredisk, fp_str.id as id_man, odecteno.doklad, odecteno.nazev, SUM(odecteno.mnozstvi) as mnozstvi, SUM(odecteno.mnozstvi*odecteno.cena) as hodnota, .f. as sumdoklad, "A" as poradi, odecteno.kdo_kdy FROM odecteno LEFT JOIN fp_str ON odecteno.c_stredisk=fp_str.c_stredisk INTO CURSOR sumspot GROUP BY odecteno.pz2, odecteno.cen_hlad, odecteno.c_stredisk, odecteno.doklad, odecteno.nazev READWRITE 
			INSERT INTO sumspot (pz2, cen_hlad, id_man, doklad, nazev, mnozstvi, hodnota, sumdoklad, poradi, kdo_kdy) SELECT pz2, cen_hlad, id_man, doklad, "", SUM(mnozstvi) as mnozstvi, SUM(hodnota) as hodnota, .t. as sumdoklad, "B" as poradi, kdo_kdy FROM sumspot group by pz2, cen_hlad, id_man, doklad
			SELECT id_kas, uzaverka, id_man, cen_hlad, COUNT(id_kas) as pocet FROM xbuff INTO CURSOR aaxxss GROUP BY id_kas, uzaverka, id_man, cen_hlad
			SCAN ALL 
				m.xxkas=aaxxss.id_kas		
				m.xxuzaverka=aaxxss.uzaverka
				m.xxman=aaxxss.id_man
				m.xxhlad=aaxxss.cen_hlad
				m.xxpocet=aaxxss.pocet
				SELECT *, 000000 as porx FROM sumspot INTO CURSOR ssumx ORDER BY poradi WHERE pz2=SUBSTR(m.xxuzaverka,3,12)+"/"+m.xxkas AND id_man=m.xxman AND cen_hlad=m.xxhlad READWRITE 
				REPLACE ALL porx WITH RECNO()
				UPDATE xbuff SET meno=ssumx.nazev, pocet=ssumx.mnozstvi, hodnota=ssumx.hodnota, sumdoklad=ssumx.sumdoklad, poradi=ssumx.poradi, kdo_kdy=ssumx.kdo_kdy, doklad=ssumx.doklad from ssumx WHERE xbuff.pz2=ssumx.pz2 AND xbuff.cen_hlad=ssumx.cen_hlad AND xbuff.id_man=ssumx.id_man AND xbuff.porx=ssumx.porx
				IF USED("pridanie")
					INSERT INTO pridanie(id_kas, uzaverka, id_man, cen_hlad, meno, pocet, hodnota, doklad, sumdoklad, poradi, porx, kdo_kdy, pz2) SELECT m.xxkas as id_kas, m.xxuzaverka as uzaverka, id_man, cen_hlad, nazev as meno, mnozstvi as pocet, hodnota, doklad, sumdoklad, poradi, porx, kdo_kdy, pz2 FROM ssumx WHERE porx>m.xxpocet 
				ELSE
					SELECT m.xxkas as id_kas, m.xxuzaverka as uzaverka, id_man, cen_hlad, nazev as meno, mnozstvi as pocet, hodnota, doklad, sumdoklad, poradi, porx, kdo_kdy, pz2 FROM ssumx WHERE porx>m.xxpocet INTO CURSOR pridanie READWRITE 
				ENDIF 
			ENDSCAN 	
			SELECT xbuff
			IF USED("pridanie")
				APPEND FROM DBF("pridanie")
			ENDIF 
			INDEX on id_kas+uzaverka+cen_hlad+id_man+STR(porx,10,0) TAG aaa
			m.txthlavicka="Detail"
			DO pr_report WITH "rozpis_odpoctu" 
			DO pozavirej WITH "aaxxss, pridanie, sumspot, ssumx"
		ENDIF 
		DO pozavirej WITH "xbuff, aaxxss, pridanie, sumspot, ssumx"
	CASE "3"$m.repriadok
		SELECT VAL(SUBSTR(TEXT,7)) AS id_limit, text, 00000000.00 AS limit, 00000 as osob, id_kas, uzaverka, u_cis, cen_hlad, id_man, nazev, SUM(mnozstvi) as mnozstvi, SUM(sumpc) as prachy, SUM(summc) as nakup, SUBSTR(uzaverka,3,12)+"/"+id_kas as pz2, 0000000000 as doklad, SPACE(30) as kdo_kdy, SPACE(30) as meno, 000000000.0000 as pocet, 0000000000.0000 as hodnota, " " as poradi, .f. as sumdoklad, 0000000000 as porx, SUM(round50) as round50 FROM pri_buff INTO CURSOR xbuff GROUP BY id_kas, uzaverka, u_cis, cen_hlad, id_man, nazev READWRITE  ORDER BY id_kas, uzaverka, cen_hlad WHERE LEFT(TEXT,5) == "Limit"
		IF m.issql
			SELECT xbuff
			SELECT DISTINCT id_limit FROM xbuff INTO CURSOR novelimity
			SCAN ALL 
				m.hladajidlimit=novelimity.id_limit
				IF USED("limden")
					=REQUERY("limden")
				ELSE 
					USE (m.sqlprefix+"rskstati_limden") IN 0 ALIAS limden
				ENDIF 
				IF USED("limrov")
					=REQUERY("limrov")
				ELSE
					USE (m.sqlprefix+"rskstati_limrovsumy") IN 0 ALIAS limrov
				ENDIF 
				IF USED("limhl")
					=REQUERY("limhl")
				ELSE 
					USE (m.sqlprefix+"rskstati_limhl") IN 0 ALIAS limhl
				ENDIF 
				SELECT limrov.pocet, limrov.limit, limrov.uzaverka, limden.c_mena FROM limrov LEFT JOIN limden ON LEFT(limrov.uzaverka,8)=DTOS(limden.datum) INTO CURSOR limit 
				UPDATE xbuff SET text=limhl.menolimit from limhl WHERE xbuff.id_limit=limhl.id_limit AND xbuff.id_limit =m.hladajidlimit
				UPDATE xbuff SET osob=IIF(ISNULL(limit.pocet),0,limit.pocet), limit=NTOM(MTON(limit.limit)*en__meny(limit.c_mena,m.girep_mena)) from limit WHERE xbuff.id_limit=m.hladajidlimit AND xbuff.uzaverka==limit.uzaverka
				USE IN limit
			ENDSCAN 
			DO pozavirej WITH "novelimity, limden, limrov, limhl"
		ELSE 
			IF FILE(m.gccen_path+"limitdni.dbf")
				SELECT 0
				IF f_acc(m.gccen_path+"limitdni") .AND. f_acc('(m.gccen_path+"limit_hl") order id_limit') .AND. !prazdna()
					SELECT xbuff
					SET RELATION TO id_limit INTO limit_hl
					REPLACE ALL TEXT WITH limit_hl.limit
					SET RELATION OFF INTO limit_hl
					SELECT id_limit, hladina, u_cis, SPACE(50) AS akcia, LEFT(uzaverka,14) as uzaverka,(limit1*pocet1+limit12*pocet12;
						+limit2*pocet2+limit22*pocet22+limit3*pocet3+limit32*pocet32) AS limit,(pocet1+pocet12+pocet2+pocet22+pocet3+pocet32) as osob,c_mena ;
						FROM limitdni INTO CURSOR limit READWRITE 
					REPLACE ALL u_cis WITH TRANS(VAL(SUBSTR(u_cis,3)), "@L 99999")
					REPLACE hladina WITH "1" FOR EMPTY(hladina)
					USE IN limitdni
					SET RELATION TO id_limit INTO limit_hl
					REPLACE ALL akcia WITH limit_hl.limit
					SET RELATION OFF INTO limit_hl
					USE IN limit_hl
					SELECT xbuff
					UPDATE xbuff SET osob=limit.osob, limit=NTOM(MTON(limit.limit)*en__meny(limit.c_mena,m.girep_mena)) from limit WHERE ALLTRIM(xbuff.text)==ALLTRIM(limit.akcia) AND ALLTRIM(xbuff.uzaverka)==ALLTRIM(limit.uzaverka) AND ALLTRIM(xbuff.u_cis)==ALLTRIM(limit.u_cis)
					USE IN limit
				ENDIF
				DO pozavirej WITH "limit, limit_hl, limitdni"
				IF m.is_newlim
					SELECT xbuff
					SELECT DISTINCT id_limit, uzaverka, u_cis FROM xbuff WHERE osob=0 OR ISNULL(osob) INTO CURSOR novelimity
					IF RECCOUNT("novelimity")>0 AND FILE(m.gccen_path+"nlimitdni.dbf") AND FILE(m.gccen_path+"nlimit_hl.dbf") AND FILE(m.gccen_path+"nlimitrov.dbf")
						TRY 
							USE (m.gccen_path+"nlimit_hl") IN 0
							USE (m.gccen_path+"nlimitdni") IN 0
							USE (m.gccen_path+"nlimitrov") IN 0
							USE (m.gccen_path+"nlimitrov_hladiny") IN 0
							m.pokracuj=.t.
						CATCH
							m.pokracuj=.f.
						ENDTRY 
						IF m.pokracuj
							UPDATE xbuff SET text=nlimit_hl.menolimit from nlimit_hl WHERE xbuff.id_limit=nlimit_hl.id_limit AND (xbuff.id_limit IN (SELECT id_limit FROM novelimity))
							SELECT SUM(hodnota*pocet) as hodnota, SUM(pocet) as pocet, id_rov FROM nlimitrov_hladiny GROUP BY id_rov INTO CURSOR rovsumy
							SELECT novelimity.id_limit, m.girep_mena as c_mena,SUM(rovsumy.hodnota) as limit, SUM(rovsumy.pocet) as osob, novelimity.uzaverka, novelimity.u_cis FROM nlimitrov INNER JOIN rovsumy ON nlimitrov.id_rov=rovsumy.id_rov INNER JOIN novelimity ON LEFT(nlimitrov.uzaverka,14)=LEFT(novelimity.uzaverka,14) AND nlimitrov.id_limit=novelimity.id_limit  INTO CURSOR limit GROUP BY novelimity.id_limit, novelimity.uzaverka READWRITE 
							UPDATE limit SET c_mena=nlimitdni.c_mena from nlimitdni WHERE limit.id_limit=nlimitdni.id_limit AND LEFT(limit.uzaverka,8)=DTOS(nlimitdni.datum)
							SELECT xbuff
							UPDATE xbuff SET osob=limit.osob, limit=NTOM(MTON(limit.limit)*en__meny(limit.c_mena,m.girep_mena)) from limit WHERE xbuff.id_limit=limit.id_limit AND xbuff.uzaverka=limit.uzaverka AND xbuff.u_cis=limit.u_cis
						ENDIF 
					ENDIF 
					DO pozavirej WITH "novelimity, limit, nlimit_hl, nlimitdni, nlimitrov, nlimitrov_hladiny, rovsumy"
				ENDIF 
			ENDIF 
		ENDIF 
		SELECT xbuff
		m.xxkas="++"
		m.xxuzaverka="++"
		m.xxman="++"
		m.xxhlad="++"
		m.xxporadie=1
		SCAN ALL 
			IF xbuff.id_kas=m.xxkas AND xbuff.uzaverka=m.xxuzaverka AND xbuff.id_man=m.xxman AND xbuff.cen_hlad=m.xxhlad
				m.xxporadie=m.xxporadie+1
			ELSE
				m.xxkas=xbuff.id_kas		
				m.xxuzaverka=xbuff.uzaverka
				m.xxman=xbuff.id_man
				m.xxhlad=xbuff.cen_hlad
				m.xxporadie=1
			ENDIF 
			REPLACE porx WITH m.xxporadie
		ENDSCAN 

		SELECT odecteno.pz2, odecteno.cen_hlad, odecteno.c_stredisk, fp_str.id as id_man, odecteno.nazev, -1 as doklad, SUM(odecteno.mnozstvi) as mnozstvi, SUM(odecteno.mnozstvi*odecteno.cena) as hodnota, .f. as sumdoklad, "A" as poradi, "" as kdo_kdy FROM odecteno LEFT JOIN fp_str ON odecteno.c_stredisk=fp_str.c_stredisk INTO CURSOR sumspot GROUP BY odecteno.pz2, odecteno.cen_hlad, odecteno.c_stredisk, odecteno.nazev 
		SELECT id_kas, uzaverka, id_man, cen_hlad, COUNT(id_kas) as pocet FROM xbuff INTO CURSOR aaxxss GROUP BY id_kas, uzaverka, id_man, cen_hlad
		SCAN ALL 
			m.xxkas=aaxxss.id_kas		
			m.xxuzaverka=aaxxss.uzaverka
			m.xxman=aaxxss.id_man
			m.xxhlad=aaxxss.cen_hlad
			m.xxpocet=aaxxss.pocet
			SELECT *, 000000 as porx FROM sumspot INTO CURSOR ssumx ORDER BY poradi WHERE pz2=SUBSTR(m.xxuzaverka,3,12)+"/"+m.xxkas AND id_man=m.xxman AND cen_hlad=m.xxhlad READWRITE 
			REPLACE ALL porx WITH RECNO()
			UPDATE xbuff SET meno=ssumx.nazev, pocet=ssumx.mnozstvi, hodnota=ssumx.hodnota, sumdoklad=ssumx.sumdoklad, poradi=ssumx.poradi from ssumx WHERE xbuff.pz2=ssumx.pz2 AND xbuff.cen_hlad=ssumx.cen_hlad AND xbuff.id_man=ssumx.id_man AND xbuff.porx=ssumx.porx
			IF USED("pridanie")
				INSERT INTO pridanie(id_kas, uzaverka, id_man, cen_hlad, meno, pocet, hodnota, doklad, sumdoklad, poradi, porx, pz2) SELECT m.xxkas as id_kas, m.xxuzaverka as uzaverka, id_man, cen_hlad, nazev as meno, mnozstvi as pocet, hodnota, doklad, sumdoklad, poradi, porx, pz2 FROM ssumx WHERE porx>m.xxpocet 
			ELSE
				SELECT m.xxkas as id_kas, m.xxuzaverka as uzaverka, id_man, cen_hlad, nazev as meno, mnozstvi as pocet, hodnota, doklad, sumdoklad, poradi, porx, pz2 FROM ssumx WHERE porx>m.xxpocet INTO CURSOR pridanie READWRITE 
			ENDIF 
		ENDSCAN 	
		SELECT xbuff
		IF USED("pridanie")
			APPEND FROM DBF("pridanie")
		ENDIF 
		INDEX on id_kas+uzaverka+cen_hlad+id_man+STR(porx,10,0) TAG aaa
		m.txthlavicka="Limity"
		DO pr_report WITH "rozpis_odpoctu" 
		DO pozavirej WITH "xbuff,aaxxss, pridanie, sumspot, ssumx"
ENDCASE 



PROCEDURE hores_budovy
PARAMETERS repriadok
LOCAL id_hotel
IF m.typ_hotel=17
	SELECT tm_sk_plt.id_kas, tm_sk_plt.u_cis, tm_sk_plt.c_uzaverka, tm_sk_plt.uzaverka, tm_sk_plt.cena_pl, tm_sk_plt.poradi AS poradie, tm_sk_plt.druh_pl, tm_sk_txt.hpokoj, tm_sk_txt.hpref, SPACE(30) as budova, SPACE(30) as hotel FROM tm_sk_plt LEFT JOIN tm_sk_txt ON tm_sk_plt.uzaverka=tm_sk_txt.uzaverka AND tm_sk_plt.u_cis=tm_sk_txt.u_cis INTO CURSOR zplt READWRITE ORDER BY tm_sk_plt.id_kas, tm_sk_plt.uzaverka, tm_sk_plt.u_cis WHERE LEFT(tm_sk_plt.druh_pl,7)="N_HOTEL"
	SELECT disti hpref FROM zplt INTO CURSOR hotely
	IF m.issql
		USE (m.sqlprefix+"rhores_d") IN 0 ALIAS hores_d
	ELSE
		USE (m.gchom_path+"hores_d") IN 0 ALIAS hores_d
	ENDIF 
	UPDATE zplt SET hotel=hores_d.hor_hotel from hores_d WHERE VAL(zplt.hpref)=hores_d.idriadok
	
	SELECT hotely
	SCAN ALL 
		IF !EMPTY(hotely.hpref)
			m.id_hotel=hotely.hpref
			SELECT hores_d
			LOCATE FOR idriadok=VAL(m.id_hotel)
			IF FOUND() AND !EMPTY(hores_d.api_meno) AND !EMPTY(hores_d.api_heslo) AND !EMPTY(hores_d.hor_ip) AND !EMPTY(hores_d.hor_port)
				=citajapirooms(ALLTRIM(hores_d.hor_ip),hores_d.hor_port, ALLTRIM(hores_d.api_meno), ALLTRIM(hores_d.api_heslo))
				IF USED("rooms")
					UPDATE zplt SET budova=rooms.building from rooms WHERE zplt.hpref=m.id_hotel AND ALLTRIM(zplt.hpokoj)=ALLTRIM(rooms.room_code)
					USE IN rooms
				ENDIF 
			ENDIF
		ENDIF 
	ENDSCAN 
	IF "1"$m.repriadok
		SELECT id_kas, "" as uzaverka, SUM(cena_pl) as cena_pl, hotel, budova FROM zplt GROUP BY id_kas, hotel, budova INTO CURSOR buff
		DO pr_report WITH 'hotel_budovy', !m.reporty_volane_z_formu
	ENDIF 
	IF "2"$m.repriadok
		SELECT id_kas, uzaverka, SUM(cena_pl) as cena_pl, hotel, budova FROM zplt GROUP BY id_kas, uzaverka, hotel, budova INTO CURSOR buff
		DO pr_report WITH 'hotel_budovy', !m.reporty_volane_z_formu
	ENDIF 
	DO pozavirej WITH "zplt, hores_d, buff, hotely"
ENDIF 





PROCEDURE terminaly
PARAMETERS repriadok
USE (m.sqlprefix+"rterminaly") IN 0 ALIAS terminaly
SELECT distinct c_uzaverka, uzaverka, id_kas FROM tm_sk_fin INTO CURSOR uzaverka 

IF "1"$m.repriadok
	SELECT *, SPACE(17) as uzaverka, SPACE(2) as id_kas FROM terminaly WHERE c_uzaverka in (SELECT c_uzaverka FROM uzaverka) READWRITE INTO CURSOR buff
	REPLACE prn_name WITH "??" FOR ISNULL(prn_name) OR EMPTY(prn_name)
	UPDATE buff SET uzaverka=uzaverka.uzaverka, id_kas=uzaverka.id_kas from uzaverka WHERE buff.c_uzaverka=uzaverka.c_uzaverka
	INDEX on id_kas + uzaverka TAG uzaverka
	DO pr_report WITH "trz_i_terminaly"
	DO pozavirej WITH "buff"
ENDIF 
IF "2"$m.repriadok
	SELECT *, SPACE(17) as uzaverka, SPACE(2) as id_kas FROM terminaly WHERE c_uzaverka in (SELECT c_uzaverka FROM uzaverka) READWRITE INTO CURSOR buff0
	UPDATE buff0 SET uzaverka=uzaverka.uzaverka, id_kas=uzaverka.id_kas from uzaverka WHERE buff0.c_uzaverka=uzaverka.c_uzaverka
	SELECT id_kas, j0, prn_name, "" as uzaverka, sum(suma) as suma FROM buff0 INTO CURSOR buff READWRITE GROUP BY id_kas, prn_name ORDER BY id_kas
	REPLACE prn_name WITH "??" FOR ISNULL(prn_name) OR EMPTY(prn_name)
	DO pr_report WITH "trz_i_terminaly"
ENDIF 
IF "3"$m.repriadok
	SELECT *, SPACE(17) as uzaverka, SPACE(2) as id_kas FROM terminaly WHERE c_uzaverka in (SELECT c_uzaverka FROM uzaverka) READWRITE INTO CURSOR buff0
	REPLACE prn_name WITH "??" FOR ISNULL(prn_name) OR EMPTY(prn_name)
	UPDATE buff0 SET uzaverka=uzaverka.uzaverka, id_kas=uzaverka.id_kas from uzaverka WHERE buff0.c_uzaverka=uzaverka.c_uzaverka
	SELECT id_kas, j0, prn_name, uzaverka, sum(suma) as suma FROM buff0 INTO CURSOR buff READWRITE GROUP BY prn_name, id_kas, uzaverka ORDER BY prn_name, id_kas, uzaverka
	DO pr_report WITH "trz_i_terminaly2"
ENDIF 
IF "4"$m.repriadok
	SELECT *, SPACE(17) as uzaverka, SPACE(2) as id_kas FROM terminaly WHERE c_uzaverka in (SELECT c_uzaverka FROM uzaverka) READWRITE INTO CURSOR buff0
	UPDATE buff0 SET uzaverka=uzaverka.uzaverka, id_kas=uzaverka.id_kas from uzaverka WHERE buff0.c_uzaverka=uzaverka.c_uzaverka
	REPLACE prn_name WITH "??" FOR ISNULL(prn_name) OR EMPTY(prn_name)
	SELECT id_kas, j0, prn_name, "" as uzaverka, sum(suma) as suma FROM buff0 INTO CURSOR buff READWRITE GROUP BY prn_name, id_kas ORDER BY prn_name, id_kas
	DO pr_report WITH "trz_i_terminaly2"
ENDIF 

DO pozavirej WITH "terminaly, uzaverka, buff, buff0"


PROCEDURE vklvyber
PARAMETERS repriadok
DO pozavirej WITH "buff"
SELECT *, SPACE(17) as uzaverka, SPACE(7) as u_cis, SPACE(2) as prn_no, SPACE(30) as textik,0 as poradie FROM sk__vklvy ORDER BY id_kas, uzaverka, ucet INTO CURSOR buff READWRITE 
REPLACE textik WITH "Prevod z minulého dňa", poradie WITH 1 FOR operacia=2 && prevod z minuleho dna
REPLACE textik WITH "Vklad", poradie WITH 2 FOR operacia=0 && vklady
REPLACE textik WITH "Výber", poradie WITH 7 FOR operacia=1 && vybery
REPLACE textik WITH "Odvod pri uzávierke", poradie WITH 8 FOR operacia=3 && odvod uzavierkou
REPLACE textik WITH "Prenos do ďalšieho dňa", poradie WITH 9 FOR operacia=4 && prenos do dalsieho dna

UPDATE buff SET uzaverka=sk__sez.uzaverka, id_kas=sk__sez.id_kas from sk__sez WHERE buff.c_uzaverka=sk__sez.c_uzaverka
UPDATE buff SET u_cis=id_kas+PADL(ALLTRIM(STR(ucet,5,0)),5,"0")
IF m.issql 
	USE (m.sqlprefix+"rskfis_prnno") IN 0 ALIAS skfis
	UPDATE buff SET prn_no=skfis.prn_no from skfis WHERE buff.ucet=skfis.ucet AND buff.c_uzaverka=skfis.c_uzaverka
ENDIF 
INDEX on id_kas+uzaverka+STR(poradie,1,0)+u_cis TAG aaa
GO TOP 
DO pr_report WITH "trz_i_vkladyvybery"
DO pozavirej WITH "buff, skfis"


PROCEDURE pomiestnostiach
PARAMETERS repriadok
DO pozavirej WITH "buff"
	IF !USED("pri_buff")
		DO pripravdata
	ENDIF

IF "1"$m.repriadok
	SELECT miestnost, SUM(sumpc) as sumpc, SUM(sum_dph) as sumpc_dph, SUM(round50) as round50,"" as id_kas, "" as uzaverka, "" as menostola, "" as u_cis  FROM pri_buff INTO CURSOR buff GROUP BY miestnost ORDER BY miestnost
	DO pr_report WITH "trz_miestnost_sumar"
	DO pozavirej WITH "buff"
	SELECT miestnost, id_kas, nazev, SUM(mnozstvi) as mnozstvi, uzaverka, SUM(sumpc) as sumpc FROM pri_buff INTO CURSOR buff GROUP BY id_kas, uzaverka, miestnost, nazev ORDER BY id_kas, uzaverka, miestnost, nazev
	DO pr_report WITH "trz_miestnost_detail"
	DO pozavirej WITH "buff"
ENDIF 
IF "2"$m.repriadok
	SELECT id_kas, uzaverka, miestnost, SUM(sumpc) as sumpc, SUM(sum_dph) as sumpc_dph, SUM(round50) as round50, "" as menostola, "" as u_cis FROM pri_buff INTO CURSOR buff GROUP BY id_kas, uzaverka, miestnost ORDER BY id_kas, uzaverka, miestnost
	DO pr_report WITH "trz_miestnost_sumar"
	DO pozavirej WITH "buff"
ENDIF 
IF "3"$m.repriadok
	SELECT id_kas, uzaverka, miestnost, menostola, SUM(sumpc) as sumpc, SUM(sum_dph) as sumpc_dph, SUM(round50) as round50, "" as u_cis FROM pri_buff INTO CURSOR buff GROUP BY id_kas, uzaverka, miestnost, menostola ORDER BY id_kas, uzaverka, miestnost, menostola
	DO pr_report WITH "trz_miestnost_sumar"
	DO pozavirej WITH "buff"
ENDIF 
IF "4"$m.repriadok
	SELECT id_kas, uzaverka, miestnost, menostola, u_cis, SUM(sumpc) as sumpc, SUM(sum_dph) as sumpc_dph, SUM(round50) as round50 FROM pri_buff INTO CURSOR buff GROUP BY id_kas, uzaverka, miestnost, menostola, u_cis ORDER BY id_kas, uzaverka, miestnost, menostola, u_cis
	DO pr_report WITH "trz_miestnost_sumar"
	DO pozavirej WITH "buff"
ENDIF 
IF "5"$m.repriadok
	SELECT id_kas, miestnost, menostola, u_cis, nazev, SUM(mnozstvi) as mnozstvi, SUM(sumpc) as sumpc FROM pri_buff INTO CURSOR buff GROUP BY id_kas, miestnost, menostola, u_cis, nazev
	DO pr_report WITH "trz_miestnost_detail"
	DO pozavirej WITH "buff"
ENDIF 



PROCEDURE fiskaly
PARAMETERS repriadok
DO pozavirej WITH "buff"
SELECT druh_pl, c_uzaverka, ucet, cena_pl, "  " as prn_no FROM sk__plt group by c_uzaverka, ucet, druh_pl INTO CURSOR aax READWRITE  
USE (m.sqlprefix+"rskfis_prnno") IN 0 ALIAS skfis
UPDATE aax SET prn_no=skfis.prn_no from skfis WHERE aax.ucet=skfis.ucet AND aax.c_uzaverka=skfis.c_uzaverka
SELECT DISTINCT prn_no AS druh_pl,SPACE(20)  AS druh, .T. AS lbl FROM aax INTO CURSOR vybery READWRITE 
USE (m.sqlprefix+"rprndef") IN 0 ALIAS prndef
UPDATE vybery SET druh=prndef.prn_name from prndef WHERE vybery.druh_pl=prndef.prn_no
SELECT vybery
INDEX ON druh_pl TAG druh_pl
INDEX ON druh TAG druh
IF RECCOUNT()>0
	IF m.reporty_volane_z_formu
		DO FORM seldruhy WITH "vybery","druhpl",150 TO m.druhxx
	ENDIF 
	SELECT vybery
	SET ORDER TO druh_pl
	LOCATE FOR !EMPTY(lbl)
	IF !FOUND()
		DO pozavirej WITH "vybery,aax, skfis, prndef"
		RETURN
	ENDIF 
ELSE 
	DO pozavirej WITH "vybery,aax, skfis, prndef"
	RETURN
ENDIF 

IF "1"$m.repriadok
	SELECT SPACE(30) as terminal, SPACE(50) as popis, nazev as druh_pl, datum,  c_uzaverka, ucet, ciastka, autor, 2 as typ, operacia, SPACE(17) as uzaverka, SPACE(7) as u_cis, SPACE(2) as prn_no,0 as poradie, SPACE(2) as id_kas FROM sk__vklvy ORDER BY id_kas, uzaverka, ucet INTO CURSOR buff READWRITE 
	SELECT buff
	REPLACE popis WITH "Prevod z minulého dňa", poradie WITH 1 FOR typ=2 AND operacia=2 && prevod z minuleho dna
	REPLACE popis WITH "Vklad", poradie WITH 2 FOR typ=2 AND operacia=0 && vklady
	REPLACE popis WITH "Výber", poradie WITH 8 FOR typ=2 AND operacia=1 && vybery
	DELETE FROM buff WHERE typ=2 AND operacia=3 && odvod uzavierkou
	REPLACE popis WITH "Prenos do ďalšieho dňa", poradie WITH 9 FOR typ=2 AND operacia=4 && prenos do dalsieho dna
**	typ=0 - ucty
	INSERT INTO buff (popis, druh_pl, c_uzaverka, ucet, ciastka, autor, typ, operacia, poradie, prn_no);
		SELECT "Tržba", druh_pl, c_uzaverka, 0, SUM(cena_pl), "", 0, 0, 3, prn_no FROM aax group by c_uzaverka, druh_pl, prn_no
	UPDATE buff SET uzaverka=sk__sez.uzaverka, id_kas=sk__sez.id_kas from sk__sez WHERE buff.c_uzaverka=sk__sez.c_uzaverka
	UPDATE buff SET u_cis=id_kas+PADL(ALLTRIM(STR(ucet,5,0)),5,"0")
**	typ=1 - pohladavky
	INSERT INTO buff (popis, druh_pl, c_uzaverka, ucet, ciastka, autor, typ, operacia, poradie);
		SELECT "Úhrada pohľadávky", sk__plt.druh_pl, sk__fin.c_uzaverka, sk__fin.ucet, sk__plt.cena_pl,sk__fin.autor, 1, 0, 4 FROM sk__fin LEFT JOIN sk__plt;
			ON sk__fin.c_uzaverka=sk__plt.c_uzaverka AND sk__fin.ucet=sk__plt.ucet WHERE sk__fin.pohladavka
	IF USED("skfis")
		UPDATE buff SET prn_no=skfis.prn_no from skfis WHERE buff.ucet=skfis.ucet AND buff.c_uzaverka=skfis.c_uzaverka
	ENDIF
	IF USED("prndef")
		UPDATE buff SET terminal=prndef.prn_name from prndef WHERE buff.prn_no=prndef.prn_no
	ELSE
		UPDATE buff SET terminal=prn_no
	ENDIF 	
	SELECT buff
	DELETE FROM buff WHERE !(prn_no in (select druh_pl FROM vybery WHERE lbl))
	INDEX on id_kas+uzaverka+druh_pl+ALLTRIM(STR(poradie,1,0)) TAG aaa
	GO TOP 
	DO pr_report WITH "trz_i_fiskaly"
	DO pozavirej WITH "buff, skfis"
ENDIF 

IF "2"$m.repriadok
	SELECT SPACE(30) as terminal, SPACE(50) as popis, nazev as druh_pl, datum,  c_uzaverka, ucet, ciastka, autor, 2 as typ, operacia, SPACE(17) as uzaverka, SPACE(7) as u_cis, SPACE(2) as prn_no,0 as poradie, SPACE(2) as id_kas FROM sk__vklvy ORDER BY id_kas, uzaverka, ucet INTO CURSOR buff READWRITE 
	SELECT buff
	REPLACE popis WITH "Prevod z minulého dňa", poradie WITH 1 FOR typ=2 AND operacia=2 && prevod z minuleho dna
	REPLACE popis WITH "Vklad", poradie WITH 2 FOR typ=2 AND operacia=0 && vklady
	REPLACE popis WITH "Výber", poradie WITH 8 FOR typ=2 AND operacia=1 && vybery
	DELETE FROM buff WHERE typ=2 AND operacia=3 && odvod uzavierkou
	REPLACE popis WITH "Prenos do ďalšieho dňa", poradie WITH 9 FOR typ=2 AND operacia=4 && prenos do dalsieho dna
**	typ=0 - ucty
	INSERT INTO buff (popis, druh_pl, c_uzaverka, ucet, ciastka, autor, typ, operacia, poradie);
		SELECT "Tržba", druh_pl, c_uzaverka, ucet, cena_pl, "", 0, 0, 3 FROM aax 
**	typ=1 - pohladavky
	INSERT INTO buff (popis, druh_pl, c_uzaverka, ucet, ciastka, autor, typ, operacia, poradie);
		SELECT "Úhrada pohľadávky", sk__plt.druh_pl, sk__fin.c_uzaverka, sk__fin.ucet, sk__plt.cena_pl,sk__fin.autor, 1, 0, 4 FROM sk__fin LEFT JOIN sk__plt;
			ON sk__fin.c_uzaverka=sk__plt.c_uzaverka AND sk__fin.ucet=sk__plt.ucet WHERE sk__fin.pohladavka
	UPDATE buff SET autor=sk__fin.autor from sk__fin WHERE buff.c_uzaverka=sk__fin.c_uzaverka AND buff.ucet=sk__fin.ucet AND buff.typ=0
	UPDATE buff SET uzaverka=sk__sez.uzaverka, id_kas=sk__sez.id_kas from sk__sez WHERE buff.c_uzaverka=sk__sez.c_uzaverka
	UPDATE buff SET u_cis=id_kas+PADL(ALLTRIM(STR(ucet,5,0)),5,"0")
	IF USED("skfis")
		UPDATE buff SET prn_no=skfis.prn_no from skfis WHERE buff.ucet=skfis.ucet AND buff.c_uzaverka=skfis.c_uzaverka
	ENDIF
	IF USED("prndef")
		UPDATE buff SET terminal=prndef.prn_name from prndef WHERE buff.prn_no=prndef.prn_no
	ELSE
		UPDATE buff SET terminal=prn_no
	ENDIF 	
	SELECT buff 
	DELETE FROM buff WHERE !(prn_no in (select druh_pl FROM vybery WHERE lbl))
	INDEX on id_kas+uzaverka+druh_pl+ALLTRIM(STR(poradie,1,0))+u_cis TAG aaa
	GO TOP 
	DO pr_report WITH "trz_i_fiskaly"
	DO pozavirej WITH "buff, skfis"
ENDIF 

IF "3"$m.repriadok
	SELECT SPACE(30) as terminal, SPACE(50) as popis, nazev as druh_pl, datum,  c_uzaverka, ucet, ciastka, autor, 2 as typ, operacia, SPACE(17) as uzaverka, SPACE(7) as u_cis, SPACE(2) as prn_no,0 as poradie, SPACE(2) as id_kas, SPACE(255) as karta, SPACE(30) as platba FROM sk__vklvy ORDER BY id_kas, uzaverka, ucet INTO CURSOR buff READWRITE 
	SELECT buff
	REPLACE popis WITH "Prevod z minulého dňa", poradie WITH 1 FOR typ=2 AND operacia=2 && prevod z minuleho dna
	REPLACE popis WITH "Vklad", poradie WITH 2 FOR typ=2 AND operacia=0 && vklady
	REPLACE popis WITH "Výber", poradie WITH 8 FOR typ=2 AND operacia=1 && vybery
	DELETE FROM buff WHERE typ=2 AND operacia=3 && odvod uzavierkou
	REPLACE popis WITH "Prenos do ďalšieho dňa", poradie WITH 9 FOR typ=2 AND operacia=4 && prenos do dalsieho dna
**	typ=0 - ucty
	INSERT INTO buff (popis, druh_pl, c_uzaverka, ucet, ciastka, autor, typ, operacia, poradie);
		SELECT "Tržba", druh_pl, c_uzaverka, ucet, cena_pl, "", 0, 0, 3 FROM aax 
**	typ=1 - pohladavky
	INSERT INTO buff (popis, druh_pl, c_uzaverka, ucet, ciastka, autor, typ, operacia, poradie);
		SELECT "Úhrada pohľadávky", sk__plt.druh_pl, sk__fin.c_uzaverka, sk__fin.ucet, sk__plt.cena_pl,sk__fin.autor, 1, 0, 4 FROM sk__fin LEFT JOIN sk__plt;
			ON sk__fin.c_uzaverka=sk__plt.c_uzaverka AND sk__fin.ucet=sk__plt.ucet WHERE sk__fin.pohladavka
	UPDATE buff SET autor=sk__fin.autor from sk__fin WHERE buff.c_uzaverka=sk__fin.c_uzaverka AND buff.ucet=sk__fin.ucet AND buff.typ=0
	UPDATE buff SET uzaverka=sk__sez.uzaverka, id_kas=sk__sez.id_kas from sk__sez WHERE buff.c_uzaverka=sk__sez.c_uzaverka
	UPDATE buff SET u_cis=id_kas+PADL(ALLTRIM(STR(ucet,5,0)),5,"0")
	UPDATE buff SET karta=sk__txt.karta from sk__txt WHERE buff.c_uzaverka=sk__txt.c_uzaverka AND buff.ucet=sk__txt.ucet AND !ISNULL(sk__txt.karta)
	IF USED("skfis")
		UPDATE buff SET prn_no=skfis.prn_no from skfis WHERE buff.ucet=skfis.ucet AND buff.c_uzaverka=skfis.c_uzaverka
	ENDIF
	IF USED("prndef")
		UPDATE buff SET terminal=prndef.prn_name from prndef WHERE buff.prn_no=prndef.prn_no
	ELSE
		UPDATE buff SET terminal=prn_no
	ENDIF 	
	SELECT buff 
	DELETE FROM buff WHERE !(prn_no in (select druh_pl FROM vybery WHERE lbl))
	INDEX on id_kas+uzaverka+druh_pl+ALLTRIM(STR(poradie,1,0))+u_cis TAG aaa
	GO TOP 
	DO pr_report WITH "trz_i_fiskaly"
	DO pozavirej WITH "buff, skfis"
ENDIF 

DO pozavirej WITH "buff, skfis, prndef, aax"

