PARAMETERS generovat_sk, minuz, old_minuz, maxuz, old_maxuz, crd_path, m.uctden1, m.uctden2, filter_priznakov

PRIVATE str, oldprc
filt_kas = ""
pokec = SPACE(12)
ostatni = "__ostatni_"

fn_sk_n_plt = "sk__plt"
fn_sk_n_pol = "sk__pol"
fn_sk_n_fin = "sk__fin"
fn_sk_n_sez = "sk__sez"
fn_sk_n_txt = "sk__txt"

fn_sk_o_plt = "sk_plt_"
fn_sk_o_pol = "sk_pol_"
fn_sk_o_fin = "sk_fin_"
fn_sk_o_sez = "sk_sez_"
fn_sk_o_txt = "sk_txt_"
IF VARTYPE(m.reporty_volane_z_formu)=="U"
	m.reporty_volane_z_formu=.t.
ENDIF 
IF generovat_sk
	DO pozavirej WITH "buf_ucet, pri_buff, dph1, dph2, dph3, obr_sum, namarkovano, o_dokl1, odecteno"

	IF m.issql
		DO sk_new3 IN sk_new
	ELSE
		DO sk_new1 IN sk_new
		DO sk_new2 IN sk_new
	ENDIF
	SET EXACT OFF
	IF USED("sk__sez") AND USED("sk__pol") AND USED("sk__plt") AND USED("sk__fin") AND USED("sk__txt")
		DO priprav_tm_sk
	ELSE
		=MESSAGEBOX(jaz("form_v_obdobi_nie_su_data"),32,"FoodMan")
		RETURN
	ENDIF
ELSE
	IF !USED("sk__sez") OR !USED("sk__pol") OR !USED("sk__plt") OR !USED("sk__fin") OR !USED("sk__txt")
		=MESSAGEBOX(jaz("form_v_obdobi_nie_su_data"),32,"FoodMan")
		RETURN
	ENDIF
ENDIF
had = CREATEOBJECT("had")
IF USED("sestavy")
	SELECT sestavy
	SCAN FOR ! EMPTY(lbl)
		SCATTER FIELD cmd, dily MEMVAR
		&cmd
	ENDSCAN
ENDIF
IF USED("zostavy")
	SELECT zostavy
	SCAN FOR ! EMPTY(lbl)
		SCATTER FIELD cmd, dily MEMVAR
		&cmd
	ENDSCAN
ENDIF

RELEASE had
SET EXACT ON
**** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
**** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****


*!*****************************************************************************
*!      Procedure: S_KAS_DPH
*!*****************************************************************************
PROCEDURE s_kas_dph

TRY 
    had.SHOW()
CATCH
	had = CREATEOBJECT("had")
ENDTRY 
had.set_had( 11 )
SELE uzaverka, id_kas, SUM(cena_pl) AS cena_pl, SUM(tip) AS tip GROUP BY uzaverka, id_kas ;
     FROM tm_sk_plt INTO CURSOR tm_sk_plt_sum READWRITE
INDEX ON uzaverka + id_kas TAG uz_kas

had.set_had( 33 )
IF "D_TGAS" $ UPPER(m.gchom_path)
    SELECT disti druh_pl, PADR(i_drupla(druh_pl), 30) as druh, .T. AS lbl FROM tm_sk_plt INTO CURSOR vybery READWRITE 
    INDEX ON druh_pl TAG druh_pl
    INDEX ON druh TAG druh
    DO FORM seldruhy WITH "vybery","druhpl",150 TO void

    SELECT uzaverka, id_kas, u_cis WHERE druh_pl in (SELECT druh_pl FROM vybery WHERE lbl) ;
           FROM tm_sk_plt INTO CURSOR filtr_plateb READWRITE 

    SELECT id_kas, uzaverka, SUM(cena_celk) AS cena_celk, SUM(sleva_abs) AS sleva_abs, SUM(trzba1+trzba2+trzba3+trzba4+round50) AS cena_slev, ;
        $0 AS tip, SUM(pocet_h) AS pocet_h, ;
        SUM(trzba1) AS trzba_dph1, SUM(trzba2) AS trzba_dph2, SUM(trzba3) AS trzba_dph3, SUM(trzba4) AS trzba_dph4, ;
        SUM(dan1) AS sumdph1, SUM(dan2) AS sumdph2, SUM(dan3) AS sumdph3, SUM(dan4) AS sumdph4, ;
        SPACE(5) AS uz_cislo, PADR(id_kas, 30) AS id_zkratka, SUM(round50) AS round50, {^1111-11-11} as uct_den ;
        GROUP BY id_kas, uzaverka ;
        WHERE uzaverka + id_kas + u_cis IN (SELECT uzaverka + id_kas + u_cis FROM filtr_plateb) ;
        FROM tm_sk_fin INTO CURSOR sk_fi3 READWRITE
ELSE 
    SELECT id_kas, uzaverka, SUM(cena_celk) AS cena_celk, SUM(sleva_abs) AS sleva_abs, SUM(trzba1+trzba2+trzba3+trzba4+round50) AS cena_slev, ;
        $0 AS tip, SUM(pocet_h) AS pocet_h, ;
        SUM(trzba1) AS trzba_dph1, SUM(trzba2) AS trzba_dph2, SUM(trzba3) AS trzba_dph3, SUM(trzba4) AS trzba_dph4, ;
        SUM(dan1) AS sumdph1, SUM(dan2) AS sumdph2, SUM(dan3) AS sumdph3, SUM(dan4) AS sumdph4, ;
        SPACE(5) AS uz_cislo, PADR(id_kas, 30) AS id_zkratka, SUM(round50) AS round50, {^1111-11-11} as uct_den ;
        GROUP BY id_kas, uzaverka FROM tm_sk_fin INTO CURSOR sk_fi3 READWRITE
ENDIF

DELETE FOR (trzba_dph1 + trzba_dph2 + trzba_dph3 + trzba_dph4 + round50 = 0) .AND. "*" $ uzaverka
had.set_had( 77 )

SET RELATION TO uzaverka + id_kas INTO tm_sk_plt_sum
REPLACE ALL tip WITH tm_sk_plt_sum.tip
SET RELATION TO

SET ORDER TO uz_kas IN tm_sk_sez_kas
SELECT sk_fi3
SET RELATION TO uzaverka + id_kas INTO tm_sk_sez_kas
REPLACE FOR tm_sk_sez_kas.uz_cislo # 0 uz_cislo WITH STR(tm_sk_sez_kas.uz_cislo, 5)
REPLACE ALL uct_den WITH tm_sk_sez_kas.uct_den, id_zkratka WITH tm_sk_sez_kas.zkr_kas
SET RELATION TO
had.HIDE()

IF "1" $ m.dily

    DO pr_report WITH IIF(m.s_dph4 > 9, "trz_kas_dph@1", "trz_kas_dph_4sl@1"), !m.reporty_volane_z_formu     && pokud jsou pouzity 4 sazby dph, pouzije se dvouradkovy report

	IF m.slovenskaverzia
		DO pr_report WITH "trz_kas_dph_slov@1", !m.reporty_volane_z_formu
	ENDIF

ENDIF

IF "2" $ m.dily
	SELECT " " AS id_kas, LEFT(id_zkratka, 19) AS uzaverka, SUM(cena_celk) AS cena_celk, SUM(sleva_abs) AS sleva_abs, SUM(cena_slev) AS cena_slev, SUM(tip) AS tip, SUM(pocet_h) AS pocet_h, ;
		SUM(trzba_dph1) AS trzba_dph1, SUM(trzba_dph2) AS trzba_dph2, SUM(trzba_dph3) AS trzba_dph3, SUM(trzba_dph4) AS trzba_dph4, ;
		SUM(sumdph1) AS sumdph1, SUM(sumdph2) AS sumdph2, SUM(sumdph3) AS sumdph3, SUM(sumdph4) AS sumdph4, ;
		id_kas AS uz_cislo, SPACE(3) AS id_zkratka, SUM(round50) AS round50  ;
		GROUP BY 1, 2 FROM sk_fi3 INTO CURSOR sk_fi4

    DO pr_report WITH IIF(m.s_dph4 > 9, "trz_kas_dph@2", "trz_kas_dph_4sl@2")     && pokud jsou pouzity 4 sazby dph, pouzije se dvouradkovy report

	IF m.slovenskaverzia
		DO pr_report WITH "trz_kas_dph_slov@2", !m.reporty_volane_z_formu
	ENDIF
ENDIF


IF "3" $ m.dily
	DO s_druhy_a_platby
ENDIF

IF "4" $ m.dily    && po uct_den misto uzaverka
	SELECT id_kas, DTOS(uct_den) + "  " AS uzaverka, SUM(cena_celk) AS cena_celk, SUM(sleva_abs) AS sleva_abs, SUM(cena_slev) AS cena_slev, SUM(tip) AS tip, SUM(pocet_h) AS pocet_h, ;
		SUM(trzba_dph1) AS trzba_dph1, SUM(trzba_dph2) AS trzba_dph2, SUM(trzba_dph3) AS trzba_dph3, SUM(trzba_dph4) AS trzba_dph4, ;
		SUM(sumdph1) AS sumdph1, SUM(sumdph2) AS sumdph2, SUM(sumdph3) AS sumdph3, SUM(sumdph4) AS sumdph4, ;
		0 AS uz_cislo, id_zkratka, SUM(round50) AS round50  ;
		GROUP BY 1, 2 FROM sk_fi3 INTO CURSOR sk_fi4 READWRITE
	REPLACE ALL uzaverka WITH LEFT(uzaverka, 4) + "-" + SUBSTR(uzaverka, 5, 2) + "-" + SUBSTR(uzaverka, 7)
    DO pr_report WITH IIF(m.s_dph4 > 9, "trz_kas_dph@4", "trz_kas_dph_4sl@4"), !m.reporty_volane_z_formu     && pokud jsou pouzity 4 sazby dph, pouzije se dvouradkovy report
ENDIF


IF "5" $ m.dily     &&          ceske kontrolni hlaseni DPH
	TRY 
	    had.SHOW()
    CATCH
		had = CREATEOBJECT("had")
    ENDTRY 
    SELECT id_kas, uzaverka, TRANSFORM(u_cis, "@L 99999") as u_cis, SPACE(12) as dic, SPACE(33) as jmeno, {} as uct_den, SPACE(44) as platba, ;
           trzba1, trzba2, trzba3, trzba4, dan1, dan2, dan3, dan4 ;
           FROM tm_sk_fin INTO CURSOR sk_fi_3 READWRITE
    INDEX on uzaverka TAG uzaverka
    INDEX on id_kas   TAG id_kas
    INDEX on u_cis    TAG u_cis

    had.set_had( 22 )
    had.set_txt2("hlaseni DPH - txt")
    SELECT tm_sk_txt
    SCAN FOR .not. EMPTY(dic)
        SCATTER FIELDS id_kas, uzaverka, ucet, dic, jmeno MEMVAR 
        u_cis = TRANSFORM(ucet, "@L 99999")
        SELECT sk_fi_3
        REPLACE FOR id_kas=m.id_kas .and. uzaverka=m.uzaverka .and. u_cis=m.u_cis dic WITH m.dic, jmeno WITH m.jmeno
    ENDSCAN 

    had.set_had( 33 )
    had.set_txt2("hlaseni DPH - sez")
    SELECT tm_sk_sez
    SCAN 
        SCATTER FIELDS id_kas, uzaverka, uct_den MEMVAR 
        SELECT sk_fi_3
        REPLACE FOR id_kas=m.id_kas .and. uzaverka=m.uzaverka uct_den WITH m.uct_den
    ENDSCAN 

    had.set_had( 77 )
    had.set_txt2("hlaseni DPH - platby")
    SELECT uzaverka, id_kas, u_cis, druh_pl, cena_pl ;
           FROM tm_sk_plt INTO CURSOR platby_pro_hlaseni READWRITE 
    INDEX on ABS(cena_pl) TAG cena_pl DESCENDING 

    uzaverka_k_vypisu = "%^&*#$%"
    SCAN
        SCATTER memvar
        IF m.uzaverka # m.uzaverka_k_vypisu
            uzaverka_k_vypisu = m.uzaverka
            had.set_txt2("hlaseni DPH - platby " + m.uzaverka_k_vypisu)
        ENDIF 
        SELECT sk_fi_3
        IF FSIZE("u_cis") = 5
            REPLACE FOR uzaverka=m.uzaverka .and. id_kas=m.id_kas .and. u_cis = RIGHT(m.u_cis, 5) platba WITH ALLTRIM(platba) + " " + m.druh_pl
        ELSE
            REPLACE FOR uzaverka=m.uzaverka .and. id_kas=m.id_kas .and. u_cis = m.u_cis           platba WITH ALLTRIM(platba) + " " + m.druh_pl
        ENDIF 
    ENDSCAN 

    #DEFINE JM_A5 "__ OSTATNI __"
    IF ATC("BUDVAR", m.wi_zakaz) > 0  && pro budvar zrusena kumulace levnejsich uctu, protoze chteji hlasit i prekroceny soucet z RUZNYCH uctu
        had.set_txt2(m.gcexportpath + "hlaseni DPH ")
        SELECT dic, jmeno, id_kas+u_cis as u_cis, uct_den, platba, ;
               trzba1-dan1 as dz1 , trzba2-dan2 as dz2 , trzba3-dan3 as dz3 , trzba4-dan4 as dz4, ;
               dan1        as dph1, dan2        as dph2, dan3        as dph3, dan4        as dph4 ;
               FROM sk_fi_3 INTO CURSOR sk_fi_4 READWRITE
        DO generuj_hlaseni_budvar

    ELSE
        SELECT sk_fi_3
        REPLACE FOR ABS(trzba1+trzba2+trzba3+trzba4) < 10000 .or. EMPTY(dic+jmeno) ;
                dic WITH "pod 10000", jmeno WITH JM_A5, id_kas WITH "", u_cis WITH "", uct_den WITH {}, platba WITH ""

        SELECT dic, jmeno, id_kas+u_cis as u_cis, uct_den, platba, ;
               SUM(trzba1-dan1) as dz1 , SUM(trzba2-dan2) as dz2 , SUM(trzba3-dan3) as dz3 , SUM(trzba4-dan4) as dz4, ;
               SUM(dan1)        as dph1, SUM(dan2)        as dph2, SUM(dan3)        as dph3, SUM(dan4)        as dph4 ;
               GROUP BY 1 FROM sk_fi_3 INTO CURSOR sk_fi_4 READWRITE

    ENDIF 
    had.HIDE()
    DO pr_report WITH "trz_dic_dph", !m.reporty_volane_z_formu

ENDIF 

DO pozavirej WITH "sk_fi2, sk_fi3, sk_fi4, sk_fi5, tm_sk_plt_sum"


*!*****************************************************************************
PROCEDURE generuj_hlaseni_budvar
TRY 
    had.SHOW()
CATCH
	had = CREATEOBJECT("had")
ENDTRY 
had.set_txt2(m.gcexportpath + "hlaseni DPH - CSV")
STRTOFILE( '"tabulka", "dic", "jmeno", "ucet",  "duzp", "platba", "dz1",  "dz2",  "dz3",  "dz4",  "dph1",  "dph2",  "dph3", "dph4", "sazba1", "sazba2", "sazba3", "sazba4" ' + CHR(13)+CHR(10), m.gcexportpath + "hlaseni_dph.csv")
SCAN
    dz_txt    = nx(dz1 , 2)             + nx(dz2,  2)             + nx(dz3,  2)             + nx(dz4,  2)
    dph_txt   = nx(dph1, 2)             + nx(dph2, 2)             + nx(dph3, 2)             + nx(dph4, 2)
    sazby_txt = nx(100*(m.s_dph1-1), 0) + nx(100*(m.s_dph2-1), 0) + nx(100*(m.s_dph3-1), 0) + nx(100*(m.s_dph4-1), 0)
    STRTOFILE( IIF(jmeno = JM_A5, '"A5", ', '"A4", ') + '"' + ALLTRIM(dic) + '", "' + ALLTRIM(jmeno) + '", "' + ALLTRIM(u_cis) + '", "' + LEFT(TTOC(uct_den, 3), 10) + '", "' + ALLTRIM(platba) +  + '"' + m.dz_txt + m.dph_txt + m.sazby_txt + CHR(13)+CHR(10), m.gcexportpath + "hlaseni_dph.csv", 1)
ENDSCAN 
had.HIDE()

********** vraci oddelovac + formatovane cislo  **********
FUNCTION nx (cislo, des_mist)
IF EMPTY(m.des_mist)
    des_mist = 0
ENDIF 
RETURN ', ' + STRTRAN(ALLTRIM(STR(cislo, 12, m.des_mist)), ",", ".")



*!*****************************************************************************
*!      Procedure: S_KAS_MAN
*!*****************************************************************************
PROCEDURE s_kas_man

had.set_had( 33 )
SELECT DISTINCT id_man FROM tm_sk_pol INTO CURSOR man2
STORE "" TO man1, man2, man3, man4, man5, man6
FOR iii = 1 TO MIN(RECC(), 9)
	GO (m.iii)
	iiii = STR(iii, 1)
	m.man&iiii = man2.id_man
ENDFOR

SELECT id_kas, uzaverka, id_man, SUM(prachy) AS cena_prod, SUM(prachy-dan) AS dan_zakl, ;
	$0 AS tr1, $0 AS tr2, $0 AS tr3, $0 AS tr4, $0 AS tr5, ;
	$0 AS dz1, $0 AS dz2, $0 AS dz3, $0 AS dz4, $0 AS dz5  ;
	GROUP BY 1, 2, 3 FROM tm_sk_pol INTO CURSOR kas_man1 READWRITE

SCAN
	DO CASE
		CASE id_man = m.man1
			REPLACE tr1 WITH cena_prod, dz1 WITH dan_zakl
		CASE id_man = m.man2
			REPLACE tr2 WITH cena_prod, dz2 WITH dan_zakl
		CASE id_man = m.man3
			REPLACE tr3 WITH cena_prod, dz3 WITH dan_zakl
		CASE id_man = m.man4
			REPLACE tr4 WITH cena_prod, dz4 WITH dan_zakl
		OTHER
			REPLACE tr5 WITH cena_prod, dz5 WITH dan_zakl
	ENDCASE
ENDSCAN

had.set_had( 55 )

SELECT id_kas, uzaverka, ;
	SUM(tr1) AS tr1, SUM(tr2) AS tr2, SUM(tr3) AS tr3, SUM(tr4) AS tr4, SUM(tr5) AS tr5, ;
	SUM(dz1) AS dz1, SUM(dz2) AS dz2, SUM(dz3) AS dz3, SUM(dz4) AS dz4, SUM(dz5) AS dz5, 00.00 as round50  ;
	GROUP BY 1, 2 FROM kas_man1 INTO CURSOR kas_man READWRITE
INDEX ON id_kas + uzaverka TAG ku
SELECT id_kas, uzaverka,SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas, uzaverka INTO CURSOR roundy
UPDATE kas_man SET round50=roundy.round50 from roundy WHERE kas_man.id_kas=roundy.id_kas AND kas_man.uzaverka=roundy.uzaverka
USE IN roundy
SELECT kas_man
had.HIDE()
IF "1" $ m.dily
	m.ttl_trkasman = jaz("rep_ttl_trby_man")
	DO pr_report WITH "trz_5man@1", !m.reporty_volane_z_formu
ENDIF

IF "2" $ m.dily
	m.ttl_trkasman = jaz("rep_ttl_vynos_man")
	REPLACE ALL tr1 WITH dz1, tr2 WITH dz2, tr3 WITH dz3, tr4 WITH dz4, tr5 WITH dz5
	DO pr_report WITH "trz_5man@2", !m.reporty_volane_z_formu
ENDIF
DO pozavirej WITH "man2, kas_man1, kas_man"


*!*****************************************************************************
PROCEDURE spocti_procenta
SELECT ID, SUM(cena_prod2) AS cena_prod2, SUM(dan_zakl2) AS dan_zakl2 GROUP BY ID FROM pom INTO CURSOR id_druh
INDEX ON ID TAG ID
SELECT pom
SET RELATION TO ID INTO id_druh
REPLACE ALL proc_vynos WITH IIF(EMPTY(id_druh.dan_zakl2),  0, 100*dan_zakl2 /id_druh.dan_zakl2), ;
    proc_trzby WITH IIF(EMPTY(id_druh.cena_prod2), 0, 100*cena_prod2/id_druh.cena_prod2)
*SET RELATION TO
USE IN id_druh


*!*****************************************************************************
*!      Procedure: S_DR_STR
*!*****************************************************************************
PROCEDURE s_dr_str
PARAMETERS id_xxx

TRY 
    had.SHOW()
CATCH
	had = CREATEOBJECT("had")
ENDTRY 

IF "MAN" $ UPPER(id_xxx)
    m.ttl_report = jaz("rep_ttl_trzby_man_druh")
	SELECT c_karty, id_man AS ID, c_stredisk AS c_str, ;
		SUM(prachy_puv) AS cena_prod, SUM(prachy)     AS cena_prod2, SPACE(m.sirkastlpcadruh) as druh, SPACE(3) as spart, ;
		SUM(prachy-dan) AS dan_zakl,  SUM(prachy-dan) AS dan_zakl2, SUM(mnozstvi) AS mnozstvi, $0 as cena_prum ;
		GROUP BY c_karty, ID FROM tm_sk_pol WHERE .NOT. EMPTY(k_odpoctu) AND ALLTRIM(cen_hlad) $ m.cen_hladiny INTO CURSOR pom1 READWRITE
ELSE
    m.ttl_report = jaz("rep_ttl_trzby_kas_druh")
    SELECT c_karty, id_kas AS ID, c_kas AS c_str, ;
        SUM(prachy_puv) AS cena_prod, SUM(prachy)     AS cena_prod2, SPACE(m.sirkastlpcadruh) as druh, SPACE(3) as spart, ;
        SUM(prachy-dan) AS dan_zakl,  SUM(prachy-dan) AS dan_zakl2, SUM(mnozstvi) AS mnozstvi, $0 as cena_prum ;
        GROUP BY c_karty, ID FROM tm_sk_pol WHERE .NOT. EMPTY(k_odpoctu) AND ALLTRIM(cen_hlad) $ m.cen_hladiny INTO CURSOR pom1 READWRITE
   	SELECT id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas INTO CURSOR roundy
   	SELECT pom1
ENDIF

DO dopln_nazev
REPLACE FOR cena_prod2 # 0 .and. cena_prod # cena_prod2 ;
            dan_zakl WITH dan_zakl2 * cena_prod / cena_prod2

SELECT druh, id, c_str, spart, PADR(id, 30) AS id_zkratka, $0 AS proc_vynos, $0 AS proc_trzby, $0 AS food_cost, ;
       SUM(cena_prod) AS cena_prod, SUM(cena_prod2) AS cena_prod2, ;
       SUM(dan_zakl)  AS dan_zakl,  SUM(dan_zakl2) AS dan_zakl2, SUM(cena_prum) AS cena_prum, 00.00 as round50 ;
       GROUP BY druh, id FROM pom1 INTO CURSOR pom READWRITE ORDER BY id
IF USED("roundy")
   	UPDATE pom SET round50=roundy.round50 from roundy WHERE pom.id=roundy.id_kas
   	LOCAL m.idxx
   	m.idxx="/\"
   	SELECT pom
   	SCAN ALL 
   		IF m.idxx=pom.id
   			REPLACE round50 WITH 0
   		ELSE
   			m.idxx=pom.id
   		ENDIF 
   	ENDSCAN
   	GO TOP 
   	RELEASE m.idxx
	USE IN roundy
	SELECT pom
ENDIF 

SET RELATION TO c_str INTO fp_str
REPLACE ALL id_zkratka WITH fp_str.id_zkratka
DELETE FOR EMPTY(cena_prod) .AND. EMPTY(cena_prod2) .AND. EMPTY(cena_prum)    && vyhozeni radku bez penez

had.set_had( 55 )
DO spocti_procenta
DO spocti_food_cost

INDEX ON ID + spart + druh TAG pompom
IF "1" $ m.dily
    had.HIDE()
	DO pr_report WITH "trz_kas_druh@1", !m.reporty_volane_z_formu
    had.show()
ENDIF
USE IN pom

* * * *     soucet po druzich za vybrana strediska bez jejich subtotalovani    * * * *
SELECT druh, " " AS ID, "za vsechna strediska" AS id_zkratka, spart, $0 AS proc_vynos, $0 AS proc_trzby, $0 AS food_cost, ;
       SUM(cena_prod) AS cena_prod, SUM(cena_prod2) AS cena_prod2, ;
       SUM(dan_zakl)  AS dan_zakl,  SUM(dan_zakl2) AS dan_zakl2, SUM(cena_prum) AS cena_prum, 00.00  as round50 ;
       GROUP BY druh FROM pom1 INTO CURSOR pom READWRITE
DELETE FOR EMPTY(cena_prod) .AND. EMPTY(cena_prod2) .AND. EMPTY(cena_prum)
LOCAL m.round50celkom
SELECT tm_sk_fin
SUM round50 TO m.round50celkom
SELECT pom
GO BOTTOM 
REPLACE round50 WITH m.round50celkom
RELEASE m.round50celkom
DO spocti_procenta
DO spocti_food_cost

INDEX ON ID + spart + druh TAG pompom
IF "2" $ m.dily
    had.HIDE()
	m.ttl_report = jaz("rep_ttl_trzby_druh")
	DO pr_report WITH "trz_kas_druh", !m.reporty_volane_z_formu
    had.show()
ENDIF
USE IN pom


IF "3" $ m.dily

    SET ORDER to uzucetkas IN tm_sk_fin
    SELECT uzaverka, u_cis, id_kas, id_man, "1" as cas_atrib, c_stredisk, c_kas, ;
           SUM(prachy_puv) AS prachy_puv, SUM(prachy) AS prachy, SUM(prachy-dan) AS dan_zakl ;
           WHERE .NOT. EMPTY(k_odpoctu) AND (ALLTRIM(cen_hlad )$ m.cen_hladiny) ;
           GROUP BY 1, 2, 3, 4 FROM tm_sk_pol INTO CURSOR pol_cas_attrib READWRITE 
    SET RELATION TO uzaverka + u_cis + id_kas INTO tm_sk_fin
    REPLACE ALL cas_atrib WITH tm_sk_fin.cas_atrib

	IF "MAN" $ UPPER(id_xxx)
		SELECT id_man AS ID, cas_atrib, c_stredisk AS c_str, PADR(id_man,30) AS id_zkratka, ;
			SUM(prachy_puv) AS cena_prod, SUM(prachy) AS cena_prod2, ;
			SUM(dan_zakl)   AS dan_zakl,  SUM(dan_zakl) AS dan_zakl2, 00.00 as round50 ;
			GROUP BY 1, 2 FROM pol_cas_attrib INTO CURSOR tr_ka_ca READWRITE

	ELSE
		SELECT id_kas AS ID, cas_atrib, c_kas AS c_str, PADR(id_kas,30) AS id_zkratka, ;
            SUM(prachy_puv) AS cena_prod, SUM(prachy) AS cena_prod2, ;
            SUM(dan_zakl)   AS dan_zakl,  SUM(dan_zakl) AS dan_zakl2, 00.00 as round50 ;
            GROUP BY 1, 2 FROM pol_cas_attrib INTO CURSOR tr_ka_ca READWRITE ORDER BY id
	   	SELECT id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas INTO CURSOR roundy
	   	UPDATE tr_ka_ca SET round50=roundy.round50 from roundy WHERE tr_ka_ca.id=roundy.id_kas
		LOCAL m.idxx
	   	m.idxx="sdfs"
	   	SELECT tr_ka_ca
	   	SCAN ALL 
	   		IF m.idxx=tr_ka_ca.id
	   			REPLACE round50 WITH 0
	   		ELSE
	   			m.idxx=tr_ka_ca.id
	   		ENDIF 
	   	ENDSCAN
	   	GO TOP 
	   	RELEASE m.idxx
		USE IN roundy
	   	SELECT tr_ka_ca
	ENDIF

    REPLACE FOR cena_prod2 # 0 .and. cena_prod # cena_prod2 ;
                dan_zakl WITH dan_zakl2 * cena_prod / cena_prod2

	SET RELATION TO c_str INTO fp_str
	REPLACE ALL id_zkratka WITH fp_str.id_zkratka

    had.hide()
	DO pr_report WITH "trz_kas_catrib", !m.reporty_volane_z_formu
ENDIF
DO pozavirej WITH "pom, tr_ka_ca, id_druh"
had.hide()


*!*****************************************************************************
*!      Procedure: PLATBYKASY
*!*****************************************************************************
PROCEDURE platbykasy
TRY 
    had.SHOW()
CATCH
	had = CREATEOBJECT("had")
ENDTRY 

SELE id_kas, u_cis, PADR(druh_pl, 30) AS druh_pl, uzaverka, mena, SUM(cena_pl) AS cena_pl, SUM(tip) AS tip, SUM(cena_mena) AS cena_mena, ;
     COUNT(u_cis+druh_pl) AS pocetslev, c_mena, PADR(id_kas, 30) as zkr_kas, ;
     $0 AS vynos1, $0 AS vynos2, $0 AS vynos3, $0 AS vynos4, ;
     $0 AS dan__1, $0 AS dan__2, $0 AS dan__3, $0 AS dan__4, ;
     $0 AS trzba1, $0 AS trzba2, $0 AS trzba3, $0 AS trzba4, $0 AS trzba ;
     GROUP BY 1, 2, 3, 4 FROM tm_sk_plt INTO CURSOR platby2 READWRITE
REPLACE FOR ("@" $ druh_pl) .OR. ("^"   $ druh_pl) druh_pl WITH ""
REPLACE FOR EMPTY(druh_pl)      druh_pl WITH jaz("rep_beze_slevy")
REPLACE FOR "7_VIP"   $ druh_pl druh_pl WITH "7_VIP"
REPLACE FOR "#7_PERS" $ druh_pl druh_pl WITH "#7_PERS"
REPLACE ALL druh_pl WITH i_drupla(druh_pl)
*REPLACE ALL druh_pl WITH jaz2(druh_pl)
REPLACE FOR cena_pl # 0 pocetslev WITH pocetslev * SIGN(cena_pl)
had.set_had( 55 )
UPDATE platby2 SET zkr_kas=fp_str.id_zkratka FROM fp_str WHERE platby2.id_kas=fp_str.id

SELE id_kas, druh_pl, SUM(cena_pl) AS cena_pl, SUM(tip) AS tip, SUM(cena_mena) AS cena_mena, SUM(pocetslev) AS pocetslev, ;
	zkr_kas AS id_zkratka, SPACE(28) AS field1, SPACE(28) AS field2, $0 AS procentick ;
	GROUP BY 1, 2 FROM platby2 INTO CURSOR platby READWRITE
REPLACE ALL field1 WITH id_kas + " " + id_zkratka, field2 WITH druh_pl
INDEX ON field1 + field2 TAG f1f2

*!*****************************************************************************
*!      Procedure: S_PLAT_KAS
*!*****************************************************************************
PROCEDURE s_plat_kas
DO platbykasy
SELECT field1, SUM(cena_pl) AS cena_pl GROUP BY 1 FROM platby INTO CURSOR sumplatby
INDEX ON field1 TAG field1
SELECT platby
SET RELATION TO field1 INTO sumplatby
REPLACE FOR sumplatby.cena_pl#0 procentick WITH ROUND(cena_pl / sumplatby.cena_pl * 100, 0)
REPLACE FOR procentick >99.9 procentick WITH 0
had.HIDE()

IF "1" $ m.dily
	DO pr_report WITH "trz_kas_platba", !m.reporty_volane_z_formu

    IF ATC("d_hajek", m.gchom_path) > 0

        SET FILTER TO "prodej voucheru " $ druh_pl
        DO pr_report WITH "trz_kas_platba@3", !m.reporty_volane_z_formu
        SET FILTER TO 
    ENDIF 

ENDIF

REPLACE ALL field2 WITH id_kas + " " + id_zkratka, field1 WITH druh_pl
SELECT field1, SUM(cena_pl) AS cena_pl GROUP BY 1 FROM platby INTO CURSOR sumplatby
INDEX ON field1 TAG field1
SELECT platby
SET RELATION TO field1 INTO sumplatby
REPLACE FOR sumplatby.cena_pl#0 procentick WITH ROUND(cena_pl / sumplatby.cena_pl * 100, 0)
REPLACE FOR procentick >99.9 procentick WITH 0
IF "2" $ m.dily
	DO pr_report WITH "trz_kas_platba@2", !m.reporty_volane_z_formu
ENDIF
DO pozavirej WITH "sumplatby, platby, platby2"


*!*****************************************************************************
*!      Procedure: S_PLAT_DPH
*!*****************************************************************************
PROCEDURE s_plat_dph
DO platbykasy

SELECT tm_sk_fin
INDEX ON uzaverka + u_cis + id_kas TAG uu
SELECT platby2
SET RELATION TO uzaverka + u_cis + id_kas INTO tm_sk_fin
REPLACE ALL trzba1 WITH tm_sk_fin.trzba1, trzba2 WITH tm_sk_fin.trzba2, trzba3 WITH tm_sk_fin.trzba3, trzba4 WITH tm_sk_fin.trzba4, ;
	trzba WITH trzba1 + trzba2 + trzba3 + trzba4

IF m.slovenskaverzia
	*** Nakopirujem si DPH z uctov
	REPLACE ALL dan__1 WITH tm_sk_fin.dan1, dan__2 WITH tm_sk_fin.dan2, dan__3 WITH tm_sk_fin.dan3, dan__4 WITH tm_sk_fin.dan4
	*** Rozpocitam DPH pomerne medzi druhy platby
	REPLACE FOR !EMPTY(trzba) dan__1 WITH ground(dan__1*cena_pl/trzba, m.girep_round),  dan__2 WITH ground(dan__2*cena_pl/trzba, m.girep_round), dan__3 WITH ground(dan__3*cena_pl/trzba, m.girep_round), dan__4 WITH ground(dan__4*cena_pl/trzba, m.girep_round)
	REPLACE FOR !EMPTY(trzba) trzba1 WITH GROUND(trzba1 * cena_pl/trzba, m.girep_round), trzba2 WITH GROUND(trzba2 * cena_pl/trzba, m.girep_round), trzba3 WITH GROUND(trzba3 * cena_pl/trzba, m.girep_round), trzba4 WITH GROUND(trzba4 * cena_pl/trzba, m.girep_round)
ELSE
	TRY 
		REPLACE FOR !EMPTY(trzba) ;
			trzba1 WITH ROUND(trzba1 * cena_pl/trzba ,4), ;
			trzba2 WITH ROUND(trzba2 * cena_pl/trzba ,4), ;
			trzba3 WITH ROUND(trzba3 * cena_pl/trzba ,4), ;
			trzba4 WITH ROUND(trzba4 * cena_pl/trzba ,4)
	CATCH
	ENDTRY 

	TRY 
		REPLACE ALL dan__1 WITH ROUND(trzba1 * (m.s_dph1-1) / m.s_dph1, 2), ;
		            dan__2 WITH ROUND(trzba2 * (m.s_dph2-1) / m.s_dph2, 2), ;
		            dan__3 WITH ROUND(trzba3 * (m.s_dph3-1) / m.s_dph3, 2), ;
		            dan__4 WITH ROUND(trzba4 * (m.s_dph4-1) / m.s_dph4, 2)
	CATCH
	ENDTRY 
ENDIF
REPLACE ALL vynos1 WITH trzba1 - dan__1, ;
            vynos2 WITH trzba2 - dan__2, ;
            vynos3 WITH trzba3 - dan__3, ;
            vynos4 WITH trzba4 - dan__4


IF ATC("frontier", m.gchom_path) > 0
    TRY 
        WAIT "Generating FRONTIERE export" WINDOW NOWAIT 
        SELECT platby2
        COPY all to (m.gcexportpath + "frontiere")
        DO frontiere_export
    CATCH 
    ENDTRY 
    WAIT CLEAR 
ENDIF 


SELECT id_kas, uzaverka, druh_pl, mena, SUM(tip) AS tip, SUM(cena_mena) AS cena_mena, ;
	ROUND(SUM(trzba1),2) AS trzba1, ROUND(SUM(trzba2),2) AS trzba2, ROUND(SUM(trzba3),2) AS trzba3, ROUND(SUM(trzba4),2) AS trzba4, ;
	ROUND(SUM(vynos1),2) AS vynos1, ROUND(SUM(vynos2),2) AS vynos2, ROUND(SUM(vynos3),2) AS vynos3, ROUND(SUM(vynos4),2) AS vynos4  ;
	GROUP BY 1, 2, 3 FROM platby2 INTO CURSOR platby3

had.HIDE()
IF "1" $ m.dily
	DO pr_report WITH "trz_platba_dph", !m.reporty_volane_z_formu
ENDIF

SELECT druh_pl, id_kas, uzaverka, mena, zkr_kas AS id_zkratka, ;
	SUM(tip) AS tip, SUM(cena_mena) AS cena_mena, SUM(cena_pl) AS cena_pl, SUM(trzba1+trzba2+trzba3+trzba4) AS trzba, ;
	SUM(trzba1) AS trzba1, SUM(trzba2) AS trzba2, SUM(trzba3) AS trzba3, SUM(trzba4) AS trzba4, ;
	ROUND(SUM(vynos1),2) AS vynos1, ROUND(SUM(vynos2),2) AS vynos2, ROUND(SUM(vynos3),2) AS vynos3, ROUND(SUM(vynos4),2) AS vynos4 ;
	GROUP BY 1, 2, 3 FROM platby2 INTO CURSOR platby4 READWRITE
DELETE FOR EMPTY(trzba1) .AND. EMPTY(trzba2) .AND. EMPTY(trzba3) .AND. EMPTY(trzba4) .AND. EMPTY(tip)

IF "2" $ m.dily
	INDEX ON id_kas + druh_pl + uzaverka TAG kpu
    DO CASE 
    CASE m.s_dph4 > 9
        DO pr_report WITH "trz_dph_plat", !m.reporty_volane_z_formu

    CASE ATC("d_hajek", m.gchom_path) > 0
        DO pr_report WITH "trz_dph_plat_4sl_a", !m.reporty_volane_z_formu    && ctyrsloupcovy report, jen trzba a DZ, bez dph

    OTHERWISE 
        DO pr_report WITH "trz_dph_plat_4sl"  , !m.reporty_volane_z_formu    && ctyrsloupcovy report, dvouradkovy

    ENDCASE 
*        DO pr_report WITH IIF(m.s_dph4 > 9, "trz_dph_plat", "trz_dph_plat_4sl"), !m.reporty_volane_z_formu     && pokud jsou pouzity 4 sazby dph, pouzije se dvouradkovy report
	SET ORDER TO

ENDIF

IF "3" $ m.dily
	SELECT druh_pl, id_zkratka AS uzaverka, "  " AS id_kas, mena, "  " AS id_zkratka, ;
		SUM(tip) AS tip, SUM(cena_mena) AS cena_mena, SUM(cena_pl) AS cena_pl, SUM(trzba) AS trzba, ;
		SUM(trzba1) AS trzba1, SUM(trzba2) AS trzba2, SUM(trzba3) AS trzba3, SUM(trzba4) AS trzba4, ;
		SUM(vynos1) AS vynos1, SUM(vynos2) AS vynos2, SUM(vynos3) AS vynos3, SUM(vynos4) AS vynos4  ;
		GROUP BY 1, 2 FROM platby4 INTO CURSOR platby4xx

    DO CASE 
    CASE m.s_dph4 > 9
        DO pr_report WITH "trz_dph_plat@3", !m.reporty_volane_z_formu

    CASE ATC("d_hajek", m.gchom_path) > 0
        DO pr_report WITH "trz_dph_plat_4sl_a@3", !m.reporty_volane_z_formu    && ctyrsloupcovy report, jen trzba a DZ, bez dph

    OTHERWISE 
        DO pr_report WITH "trz_dph_plat_4sl@3"  , !m.reporty_volane_z_formu    && ctyrsloupcovy report, dvouradkovy

    ENDCASE 
*    DO pr_report WITH IIF(m.s_dph4 > 9, "trz_dph_plat@3", "trz_dph_plat_4sl@3"), !m.reporty_volane_z_formu     && pokud jsou pouzity 4 sazby dph, pouzije se dvouradkovy report

ENDIF

IF "4" $ m.dily
    SELECT platby4
	REPLACE FOR trzba # 0 ;
		trzba1 WITH ROUND(trzba1 * cena_mena/trzba, m.girep_round), ;
		trzba2 WITH ROUND(trzba2 * cena_mena/trzba, m.girep_round), ;
		trzba4 WITH ROUND(trzba4 * cena_mena/trzba, m.girep_round)
	REPLACE ALL trzba3 WITH cena_mena - trzba1 - trzba2 - trzba4, trzba WITH cena_pl
	DO pr_report WITH "trz_dph_platba", !m.reporty_volane_z_formu
ENDIF
DO pozavirej WITH "platby1, platby2, platby3, platby4, platby4xx"


*!*****************************************************************************
*!      Procedure: s_cas_atr
*!*****************************************************************************
PROCEDURE s_cas_atr

s_cas_atr_had = CREATEOBJECT("had", PROGRAM())
STORE "-" TO atrib1, atrib2, atrib3, atrib4, atrib5, atrib6, atrib7

IF "1" $ m.dily
    SELECT id_kas, uzaverka, cas_atrib, uzaverka as field2, SUM(pocet_h) as pocet_h, SUM(cena_celk - sleva_abs + round50) AS cena_slev ;
           GROUP BY 1, 2, 3 FROM tm_sk_fin INTO CURSOR fi_atrib00
    ukazovat_hosty_v_uzav = .T.
    s_cas_atr_had.SHOW()
    SELECT id_kas, uzaverka, cas_atrib, SUM(cena_slev) AS cena_slev, SUM(pocet_h) AS pocet_h, ;
        000000 AS hostu1, 000000 AS hostu2, 000000 AS hostu3, 000000 AS hostu4, 000000 AS hostu5, 000000 AS hostu6, 000000 AS hostu7, ;
        $0     AS trzba1,   $0   AS trzba2,   $0   AS trzba3,   $0   AS trzba4,   $0   AS trzba5,   $0   AS trzba6,   $0   AS trzba7  ;
        GROUP BY 1,2,3 FROM fi_atrib00 INTO CURSOR fi_atrib READWRITE

    DO dopln_catrib

    SELECT id_kas, uzaverka, SUM(cena_slev) AS cena_slev, SUM(pocet_h) AS pocet_h, SPACE(30) AS id_zkratka, ;
        SUM(hostu1) AS hostu1, SUM(hostu2) AS hostu2, SUM(hostu3) AS hostu3, SUM(hostu4) AS hostu4, SUM(hostu5) AS hostu5, SUM(hostu6) AS hostu6, SUM(hostu7) AS hostu7, ;
        SUM(trzba1) AS trzba1, SUM(trzba2) AS trzba2, SUM(trzba3) AS trzba3, SUM(trzba4) AS trzba4, SUM(trzba5) AS trzba5, SUM(trzba6) AS trzba6, SUM(trzba7) AS trzba7  ;
        GROUP BY 1,2 FROM fi_atrib INTO CURSOR fi_atrib1 READWRITE
    DELETE FOR EMPTY(cena_slev) .AND. EMPTY(pocet_h)

    SET ORDER TO uz_kas IN tm_sk_sez_kas
    SET RELATION TO uzaverka + id_kas INTO tm_sk_sez_kas
    REPLACE ALL id_zkratka WITH tm_sk_sez_kas.zkr_kas

    SET RELATION TO id_kas INTO fi_kas1
    s_cas_atr_had.HIDE()
    DO pr_report WITH "trz_catrib", !m.reporty_volane_z_formu
ENDIF 

IF "2" $ m.dily
    SELECT uzaverka, u_cis, id_kas, SPACE(7) + id_man  as field2, "1" as cas_atrib, 000000 AS pocet_h, SUM(prachy) AS cena_slev, SPACE(30) AS id_zkratka ;
           GROUP BY 1, 2, 3, 4 FROM tm_sk_pol INTO CURSOR fi_atrib00 READWRITE 
    DO s_cas_atr_pokrac2
    ukazovat_hosty_v_uzav = .T.
    DO pr_report WITH "trz_catrib", !m.reporty_volane_z_formu
ENDIF 

IF "3" $ m.dily
    SELECT uzaverka, u_cis, id_kas, c_karty, SPACE(10) as field2, "1" as cas_atrib, 000000 AS pocet_h, SUM(prachy) AS cena_slev, SPACE(30) AS id_zkratka ;
           GROUP BY 1, 2, 3, 4 FROM tm_sk_pol INTO CURSOR fi_atrib00 READWRITE 
    SET RELATION TO c_karty INTO prodane_karty
    REPL ALL field2 WITH prodane_karty.druh_xx
    DO s_cas_atr_pokrac2
*    ukazovat_hosty_v_uzav = .F.
    DO pr_report WITH "trz_catrib_bez_hostu", !m.reporty_volane_z_formu
ENDIF 

RELEASE s_cas_atr_had
DO pozavirej WITH "fi_atrib, fi_atrib1, fi_atrib00, fi_atrib00_b, cas_atr"


*!*****************************************************************************
*!      Procedure: s_cas_atr_pokrac2
*!*****************************************************************************
PROCEDURE s_cas_atr_pokrac2
s_cas_atr_had.SHOW()
SET ORDER to uzucetkas IN tm_sk_fin
SELECT fi_atrib00
SET RELATION TO uzaverka + u_cis + id_kas INTO tm_sk_fin
REPLACE ALL cas_atrib WITH tm_sk_fin.cas_atrib, pocet_h WITH tm_sk_fin.pocet_h
LOCAL m.uzavxx, m.ucxx, m.ikxx
m.uzavxx="asdfasdf"
m.ucxx="asdsaf"
m.ikxx="/\"
SCAN ALL 
	IF fi_atrib00.uzaverka=m.uzavxx AND fi_atrib00.u_cis=m.ucxx AND fi_atrib00.id_kas=m.ikxx
	ELSE
		REPLACE cena_slev WITH cena_slev+tm_sk_fin.round50
		m.uzavxx=fi_atrib00.uzaverka
		m.ucxx=fi_atrib00.u_cis
		m.ikxx=fi_atrib00.id_kas
	ENDIF 
ENDSCAN 
GO TOP 
RELEASE m.uzavxx, m.ucxx, m.ikxx
SET ORDER TO uz_kas IN tm_sk_sez_kas
SELECT fi_atrib00
SET RELATION TO uzaverka + id_kas INTO tm_sk_sez_kas
REPLACE ALL id_zkratka WITH tm_sk_sez_kas.zkr_kas

SELECT id_kas, field2 as uzaverka, cas_atrib, id_zkratka, SUM(cena_slev) AS cena_slev, SUM(pocet_h) AS pocet_h, ;
       000000 AS hostu1, 000000 AS hostu2, 000000 AS hostu3, 000000 AS hostu4, 000000 AS hostu5, 000000 AS hostu6, 000000 AS hostu7, ;
       $0     AS trzba1, $0     AS trzba2, $0     AS trzba3,     $0 AS trzba4, $0     AS trzba5, $0     AS trzba6, $0     AS trzba7  ;
       GROUP BY 1, 2, 3 FROM fi_atrib00 INTO CURSOR fi_atrib READWRITE

DO dopln_catrib

SELECT id_kas, uzaverka, SUM(cena_slev) AS cena_slev, id_zkratka, SUM(pocet_h) AS pocet_h, ;
       SUM(trzba1) AS trzba1, SUM(trzba2) AS trzba2, SUM(trzba3) AS trzba3, SUM(trzba4) AS trzba4, SUM(trzba5) AS trzba5, SUM(trzba6) AS trzba6, SUM(trzba7) AS trzba7, ;
       SUM(hostu1) AS hostu1, SUM(hostu2) AS hostu2, SUM(hostu3) AS hostu3, SUM(hostu4) AS hostu4, SUM(hostu5) AS hostu5, SUM(hostu6) AS hostu6, SUM(hostu7) AS hostu7  ;
       GROUP BY 1, 2 FROM fi_atrib INTO CURSOR fi_atrib1 READWRITE
DELETE FOR EMPTY(cena_slev)

SET RELATION TO id_kas INTO fi_kas1
s_cas_atr_had.HIDE()


*!*****************************************************************************
PROCEDURE dopln_catrib
s_cas_atr_had.set_had(33)
SELECT DISTINCT cas_atrib FROM fi_atrib INTO CURSOR cas_atr READWRITE
iii = 0
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
REPLACE FOR cas_atrib = m.atrib6 hostu6 WITH pocet_h, trzba6 WITH cena_slev
REPLACE FOR cas_atrib = m.atrib7 hostu7 WITH pocet_h, trzba7 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
s_cas_atr_had.set_had(88)


*!*****************************************************************************
*!      Procedure: S_ZBOZI
*!*****************************************************************************
PROCEDURE s_zbozi
PARAMETERS jakesestavy

TRY 
    had.SHOW()
CATCH
	had = CREATEOBJECT("had")
ENDTRY 
SET ORDER TO uzucetkas IN tm_sk_fin

IF "D_HAJEK" $ UPPER(m.gchom_path)
    SELECT disti druh_pl, PADR(i_drupla(druh_pl), 30) as druh, .T. AS lbl FROM tm_sk_plt INTO CURSOR vybery READWRITE 
    INDEX ON druh_pl TAG druh_pl
    INDEX ON druh TAG druh
    DO FORM seldruhy WITH "vybery","druhpl",150 TO void

    SELECT uzaverka, id_kas, u_cis WHERE druh_pl in (SELECT druh_pl FROM vybery WHERE lbl) ;
           FROM tm_sk_plt INTO CURSOR filtr_plateb READWRITE 

    SELECT id_kas, id_man, uzaverka, u_cis, c_karty, dan_sazba, dil_porce, cen_hlad, dotacia, {} as uct_den, ;
           sum(mnozstvi) as mnozstvi , sum(prachy_puv) as prachy, SUM(dan) as dan, ;
           sum(prachy) as cena_slev, SUM(prachy - dan) as dan_zakl2, $0 as prachymenu, $0 as cena_menu, k_odpoctu, SPACE(2) as id_slave, 00.00 as round50 ;
           WHERE ALLTRIM(cen_hlad) $ m.cen_hladiny AND uzaverka + u_cis IN (SELECT uzaverka + u_cis FROM filtr_plateb) ;
           GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9  ORDER BY id_kas;
           FROM tm_sk_pol INTO CURSOR zbozi1 READWRITE 

ELSE

    SELECT id_kas, id_man, uzaverka, u_cis, c_karty, dan_sazba, dil_porce, cen_hlad, dotacia, {} as uct_den, SPACE(30) as nazev, SPACE(m.sirkastlpcadruh) as druh, $0 as naklady, ;
           sum(mnozstvi) as mnozstvi , sum(prachy_puv) as prachy, SUM(dan) as dan, ;
           sum(prachy) as cena_slev, SUM(prachy - dan) as dan_zakl2, $0 as prachymenu, $0 as cena_menu, k_odpoctu, SPACE(2) as id_slave, 00.00 as round50 ;
           WHERE ALLTRIM(cen_hlad) $ m.cen_hladiny ;
           GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9 ;
           FROM tm_sk_pol INTO CURSOR zbozi1 READWRITE ORDER BY id_kas
ENDIF 
SELECT id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas INTO CURSOR roundy
IF USED("roundy")
   	UPDATE zbozi1 SET round50=roundy.round50 from roundy WHERE zbozi1.id_kas=roundy.id_kas
   	LOCAL m.idxx
   	idxx="/\"
   	SELECT zbozi1
   	SCAN ALL 
   		IF m.idxx=zbozi1.id_kas
   			REPLACE round50 WITH 0
   		ELSE
   			idxx=zbozi1.id_kas
   		ENDIF 
   	ENDSCAN
   	GO TOP 
   	RELEASE m.idxx
	USE IN roundy
	SELECT zbozi1
ENDIF 


SET RELATION TO uzaverka + u_cis + id_kas INTO tm_sk_fin
REPLACE ALL id_slave WITH tm_sk_fin.id_slave
SET RELATION TO 

IF prazdna()
	INSERT INTO zbozi1 (c_karty, dan) VALUES (-654321, 1)    && ---- nic se neprodalo ---
ENDIF

REPLACE FOR EMPTY(k_odpoctu) cena_menu WITH cena_slev, prachymenu WITH prachy, prachy WITH 0, cena_slev WITH 0, id_man WITH "Mn"


IF  "D_SNEM" $ UPPER(m.gchom_path)       && generovani CSV pro Perinku v parlamentu
    SELECT zbozi1
    SET RELATION TO uzaverka + id_kas INTO tm_sk_sez_kas
    REPLACE ALL uct_den WITH tm_sk_sez_kas.uct_den
    SET ORDER TO c_karty IN prodane_karty
    SET RELATION TO c_karty INTO prodane_karty
*    REPLACE ALL nazev WITH prodane_karty.nazev, druh WITH prodane_karty.druh_kalk
    REPLACE ALL naklady WITH mnozstvi * prodane_karty.cena_prum
    SET RELATION TO 
    DO dopln_nazev
    DO to_csv
ENDIF 

SELECT id_kas, id_man, c_karty, dil_porce, SPACE(30) as nazev, str(dan_sazba*100 - 100, 2) as dan, "  " as id_pom, sum(mnozstvi) as mnozstvi, SPACE(m.sirkastlpcadruh) as druh, SPACE(30) as id_zkratka, SPACE(30) as zkr_man, SPACE(3) as spart, ;
       sum(prachy) as cena, sum(cena_slev) as cena_slev, sum(prachymenu) as prachymenu, sum(cena_menu) as cena_menu, sum(dan_zakl2) as dan_zakl2, ;
       $0 as cena_prum, $0 as mn1, $0 as mn2, $0 as mn3, $0 as mn4, $0 as mn5, $0 as mn6, $0 as mn7, $0 as mn8, $0 as mn9, "    " as stredskup, k_odpoctu, $0 as food_cost, 00.00 as round50 ;
       GROUP BY 1, 2, 3, 4 FROM zbozi1 INTO CURSOR zbozi READWRITE
DO dopln_nazev
had.set_had( 33 )

INDEX ON id_kas + id_man + nazev TAG kasmannaz

SELECT id_kas, id_man, c_karty, cen_hlad, SPACE(30) AS nazev, SUM(mnozstvi) AS mnozstvi , SUM(prachy) AS prachy ;
       WHERE ! EMPTY(k_odpoctu) GROUP BY 1, 2, 3, 4 ;
       FROM tm_sk_pol INTO CURSOR zbozi_cenhl READWRITE

REPLACE FOR VAL(cen_hlad)=0 OR VAL(cen_hlad)>9     cen_hlad WITH "9" 
REPLACE FOR ! BETWEEN(ALLTRIM(cen_hlad), "1", "9") cen_hlad WITH "1"

DO dopln_nazev

SET RELATION TO id_kas + id_man + nazev INTO zbozi
SCAN
	SCATT FIELD cen_hlad MEMVAR
	REPLACE zbozi.mn&cen_hlad WITH zbozi.mn&cen_hlad + mnozstvi
	SELECT zbozi_cenhl
ENDSCAN
USE

had.set_had( 88 )

SELECT zbozi
UPDATE zbozi SET stredskup=fd.stredskup, id_zkratka=fd.id_zkratka FROM fd WHERE fd.ID=zbozi.id_kas

INDEX ON id_kas + id_man + druh + nazev TAG kmdn
SELECT id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas INTO CURSOR roundy
IF USED("roundy")
   	UPDATE zbozi SET round50=roundy.round50 from roundy WHERE zbozi.id_kas=roundy.id_kas
   	LOCAL m.idxx
   	idxx="/\"
   	SELECT zbozi
   	SCAN ALL 
   		IF m.idxx=zbozi.id_kas
   			REPLACE round50 WITH 0
   		ELSE
   			idxx=zbozi.id_kas
   		ENDIF 
   	ENDSCAN
   	GO TOP 
   	RELEASE m.idxx
	USE IN roundy
	SELECT zbozi
ENDIF 

DO spocti_food_cost
had.HIDE()

IF "KASMAN" $ m.jakesestavy
    IF "1" $ m.dily
        view_mssg = jaz("s_sk_po_kasach")
        IF "D_RADLIC" $ UPPER(m.gchom_path)
            DO pr_report WITH "trz_prodzboz_tjr", !m.reporty_volane_z_formu
        ELSE
            DO pr_report WITH "trz_prodzboz", !m.reporty_volane_z_formu
        ENDIF
    ENDIF

    REPLACE ALL id_pom WITH id_kas, id_kas WITH id_man, id_man WITH id_pom, id_pom WITH ""
    IF "2" $ m.dily
        view_mssg = "Po Managerech"
        DO pr_report WITH "trz_prodzboz@km2", !m.reporty_volane_z_formu
    ENDIF

    IF "3" $ m.dily
        DO zbozi_po_sloupcich
        DO zbozi_po_hodne_sloupcich
    ENDIF

    IF "4" $ m.dily
        DO zbozi_po_hodinach
    ENDIF

    IF "5" $ m.dily
        SELECT id_slave as id_kas, id_man, c_karty, dil_porce, SPACE(30) as nazev, str(dan_sazba*100 - 100, 2) as dan, "  " as id_pom, sum(mnozstvi) as mnozstvi, SPACE(m.sirkastlpcadruh) as druh, SPACE(30) as id_zkratka, SPACE(30) as zkr_man, SPACE(3) as spart, ;
           sum(prachy) as cena, sum(cena_slev) as cena_slev, sum(prachymenu) as prachymenu, sum(cena_menu) as cena_menu, sum(dan_zakl2) as dan_zakl2, ;
           $0 as cena_prum, $0 as mn1, $0 as mn2, $0 as mn3, $0 as mn4, $0 as mn5, $0 as mn6, $0 as mn7, $0 as mn8, $0 as mn9, "    " as stredskup, k_odpoctu, $0 as food_cost, 00.00 as round50, id_kas as ik ;
           GROUP BY 1, 2, 3, 4 FROM zbozi1 INTO CURSOR zb_slave READWRITE ORDER BY ik
		SELECT id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas INTO CURSOR roundy
		IF USED("roundy")
		   	UPDATE zb_slave SET round50=roundy.round50 from roundy WHERE zb_slave.ik=roundy.id_kas
		   	LOCAL m.idxx
		   	m.idxx="/\"
		   	SELECT zb_slave
		   	SCAN ALL 
		   		IF m.idxx=zb_slave.ik
		   			REPLACE round50 WITH 0
		   		ELSE
		   			m.idxx=zb_slave.ik
		   		ENDIF 
		   	ENDSCAN
		   	GO TOP 
		   	RELEASE m.idxx
			USE IN roundy
			SELECT zb_slave
		ENDIF 
        DO dopln_nazev
        UPDATE zb_slave SET stredskup=fd.stredskup, id_zkratka=fd.id_zkratka FROM fd WHERE fd.ID=zb_slave.id_kas
        INDEX ON id_kas + id_man + druh + nazev TAG kmdn
        view_mssg = "Po podrizenych pokladnach"
        DO pr_report WITH "trz_prodzboz@slave", !m.reporty_volane_z_formu
    ENDIF
ENDIF

IF "CELK" $ m.jakesestavy
	IF "1" $ m.dily
		view_mssg = "Celkem"
		SELECT nazev, dil_porce, druh, spart, SUM(mnozstvi) AS mnozstvi, SUM(mn1) AS mn1, SUM(mn2) AS mn2, SUM(mn3) AS mn3, SUM(mn4) AS mn4, SUM(mn5) AS mn5, ;
			SUM(cena) AS cena, SUM(cena_slev) AS cena_slev, SUM(cena_prum) AS cena_prum, SUM(dan_zakl2) AS dan_zakl2, ;
			"  " AS id_kas, "  " AS id_man, " " AS id_pom, " " AS dan, " " AS id_zkratka, $0 as food_cost, SUM(round50) as round50 ;
			FROM zbozi INTO CURSOR zb2 READWRITE GROUP BY 1,2 ORDER BY druh, nazev, dil_porce
        DO spocti_food_cost
		DO pr_report WITH "trz_prodzboz@c1", !m.reporty_volane_z_formu
		USE
	ENDIF

	IF "2" $ m.dily
		view_mssg = "Po STREDSKUP"
		SELECT stredskup AS id_kas, druh, nazev, dil_porce, SUM(mnozstvi) AS mnozstvi, SUM(mn1) AS mn1, SUM(mn2) AS mn2, SUM(mn3) AS mn3, SUM(mn4) AS mn4, SUM(mn5) AS mn5, ;
			SUM(cena) AS cena, SUM(cena_slev) AS cena_slev, SUM(cena_prum) AS cena_prum, SUM(dan_zakl2) AS dan_zakl2, ;
			spart, "  " AS id_man, " " AS id_pom, " " AS dan, " " AS id_zkratka, $0 as food_cost, SUM(round50) as round50 ;
			FROM zbozi INTO CURSOR zb2 READWRITE GROUP BY 1,2,3,4 ORDER BY 1,2,3,4
        DO spocti_food_cost
		DO pr_report WITH "trz_prodzboz@c2", !m.reporty_volane_z_formu
		USE
	ENDIF

	IF "3" $ m.dily
		view_mssg = jaz("form_s_kalkulaciami")
		SELECT id_kas, id_man, druh, nazev, dil_porce, SUM(mnozstvi) AS mnozstvi, SUM(mn1) AS mn1, SUM(mn2) AS mn2, SUM(mn3) AS mn3, SUM(mn4) AS mn4, SUM(mn5) AS mn5, ;
			SUM(cena) AS cena, SUM(cena_slev) AS cena_slev, SUM(cena_prum) AS cena_prum, SUM(dan_zakl2) AS dan_zakl2, ;
			c_karty, k_odpoctu, "A" as hlavicka, space(30) as polozka,$0 as hruba,$0 as cista, $0 as cena_pol, SPACE(10) as poznamka, 00 as poradie, SPACE(30) as id_zkratka, dan, $0 as pocet, $0 as cen_pr_net, $0 as cena01, SUM(round50) as round50;
			FROM zbozi INTO CURSOR zb2 GROUP BY 1,2,3,4 ORDER BY 1,2,3,4,5 READWRITE 
		IF m.slovenskaverzia
			REPLACE k_odpoctu WITH "" FOR k_odpoctu=="F"
		ENDIF
		PRIVATE m.hladajckarty, m.hladajnazev, m.hladajidkas, m.hladajdruh, m.hladajidman
		IF !m.issql AND !USED("polozky")
			USE (m.gccen_path+"polozky") IN 0
		ENDIF 
		SELECT c_karty, nazev, id_kas, id_man, druh, dan FROM zb2 INTO CURSOR zb3 WHERE !EMPTY(k_odpoctu) READWRITE 
		LOCAL lokalcena, lokalpocet, lokalccena, lokaldan
		SELECT karty
		SET ORDER TO c_karty
		SELECT zb3
		SCAN ALL 
			lokaldan    = VAL(zb3.dan)
			hladajckarty= zb3.c_karty
			hladajnazev = zb3.nazev
			hladajidkas = zb3.id_kas
			hladajdruh  = zb3.druh
			hladajidman = zb3.id_man
			SELECT karty
			IF SEEK(m.hladajckarty)
				lokalcena=karty.cena01*en__meny(karty.c_mena,m.girep_mena)
				lokalpocet=karty.pocet
			ELSE
				lokalcena=0
				lokalpocet=1
			ENDIF 
			IF USED("polozky1")
				REQUERY("polozky1")
			ELSE
				IF m.issql
					USE (m.sqlprefix+"rpolozkyscenou") IN 0 ALIAS polozky1
				ELSE
					USE vpolozkyscenou IN 0 ALIAS polozky1
				ENDIF 
			ENDIF 
			IF RECCOUNT("polozky1")>0
				INSERT INTO zb2 (hlavicka, id_kas, id_man, druh, nazev, c_karty, cena01, cen_pr_net, pocet, cena_pol) SELECT "B" as hlavicka, m.hladajidkas as id_kas, m.hladajidman as id_man, m.hladajdruh as druh, m.hladajnazev as nazev, m.hladajckarty as c_karty, m.lokalcena, m.lokalcena-ROUND(m.lokalcena*m.lokaldan/(100+m.lokaldan),2), m.lokalpocet, SUM(NTOM(MTON(hruba*cena_prum)*en__meny(c_mena_n,m.girep_mena))) as cen_pol FROM polozky1
				INSERT INTO zb2 (hlavicka, id_kas, id_man, druh, nazev, c_karty, polozka, hruba, cista, cena_pol, poznamka, poradie) SELECT "C" as hlavicka, m.hladajidkas as id_kas, m.hladajidman as id_man, m.hladajdruh as druh, m.hladajnazev as nazev, m.hladajckarty as c_karty, nazev, hruba, cista, NTOM(MTON(cena_prum)*en__meny(c_mena_n,m.girep_mena)) as cen_pol, poznamka, RECNO() as poradie FROM polozky1
				INSERT INTO zb2 (hlavicka, id_kas, id_man, druh, nazev, c_karty, hruba, cista, cena_pol) SELECT "D" as hlavicka, m.hladajidkas as id_kas, m.hladajidman as id_man, m.hladajdruh as druh, m.hladajnazev as nazev, m.hladajckarty as c_karty, SUM(hruba), SUM(cista), SUM(NTOM(MTON(hruba*cena_prum)*en__meny(c_mena_n,m.girep_mena))) as cen_pol FROM polozky1
			ENDIF 
			SELECT zb3
		ENDSCAN 
		DO pozavirej WITH "polozky1, zb3, polozky"
		SELECT zb2
		UPDATE zb2 SET id_zkratka=fd.id_zkratka FROM fd WHERE fd.ID=zb2.id_man
		INDEX on id_kas+id_man+druh+nazev+hlavicka TAG aaa
		DO pr_report WITH "trz_prodzboz_kalk", !m.reporty_volane_z_formu
		USE

        SELECT c_kas, c_stredisk, uzaverka, c_karty, dan_sazba, PADR(cen_hlad,2) AS cen_hlad, id_kas, id_man, ;
            SUM(prachy)     AS prachy,   SUM(dan) AS dan, $0 AS cena_prum, ;
            SUM(prachy_puv) AS prachy_puv, $0     AS dan_puv, SUM(mnozstvi) as mnozstvi ;
            WHERE ALLTRIM(cen_hlad) $ m.cen_hladiny GROUP BY 1,2,3,4,5,6 ;
            FROM tm_sk_pol INTO CURSOR namarkovano1_a READWRITE

        DO dopln_nazev
        REPLACE FOR prachy # 0 dan_puv WITH dan * prachy_puv / prachy

		SELECT c_kas, c_stredisk, uzaverka, dan_sazba, cen_hlad, id_kas, id_man, ;
			SUM(prachy)     AS prachy,     SUM(prachy-dan)         AS vynos, SUM(cena_prum) AS cena_prum, ;
			SUM(prachy_puv) AS prachy_puv, SUM(prachy_puv-dan_puv) AS vynos_puv, ;
			$0 AS dan1, $0 AS dan2, $0 AS dan3, $0 AS dz1, $0 AS dz2, $0 AS dz3 ;
			GROUP BY 1,2,3,4,5 ;
			FROM namarkovano1_a INTO CURSOR namarkovano1 READWRITE

		SELECT c_kas, c_stredisk, uzaverka, cen_hlad, id_kas, id_man, SPACE(30) AS zkr_kas, SPACE(30) AS zkr_man, $0 AS naklady, ;
			SUM(prachy) AS prachy, SUM(vynos) AS vynos, SUM(prachy_puv) AS prachy_puv, SUM(vynos_puv) AS vynos_puv, SUM(cena_prum) AS cena_prum ;
			GROUP BY 1,2,3,4 FROM namarkovano1 INTO CURSOR namarkovano READWRITE

		UPDATE namarkovano SET zkr_kas=fp_str.id_zkratka FROM fp_str WHERE namarkovano.c_kas=fp_str.c_stredisk
		UPDATE namarkovano SET zkr_man=fp_str.id_zkratka FROM fp_str WHERE namarkovano.c_stredisk=fp_str.c_stredisk
		INDEX ON STR(c_kas) + STR(c_stredisk) + uzaverka + cen_hlad TAG ksuc
		DO priprav_odectene
		SELECT c_kas, c_stredisk, nazev, cen_hlad, c_karty, druh, SUM(mnozstvi) as mnozstvi, SUM(mnozstvi * cena) AS naklady, SPACE(2) as id_kas, SPACE(30) as menokasy, SPACE(30) as id_zkratka, kod ;
			GROUP BY 1,2,3,4 FROM odecteno INTO CURSOR odecteno2 READWRITE
		UPDATE odecteno2 SET id_zkratka=fd.id_zkratka FROM fd WHERE fd.c_stredisk=odecteno2.c_stredisk
		UPDATE odecteno2 SET menokasy=fd.id_zkratka FROM fd WHERE fd.c_stredisk=odecteno2.c_kas
		UPDATE odecteno2 SET id_kas=fd.id FROM fd WHERE fd.c_stredisk=odecteno2.c_kas
		view_mssg = "Odpis zo skladov"
		DO pr_report WITH "trz_prodzboz_spotreba", !m.reporty_volane_z_formu
		DO pozavirej WITH "namarkovano,namarkovano1, odecteno, odecteno2"
	ENDIF
	
ENDIF

if "DPH" $ m.jakesestavy

    select id_man as id_kas, c_karty, str(dan_sazba*100 - 100, 2) as id_man, "%" as id_pom, sum(mnozstvi) as mnozstvi, ;
           SPACE(30) as nazev, SPACE(m.sirkastlpcadruh) as druh, SPACE(30) as id_zkratka, SPACE(30) as zkr_man, ;
           sum(prachy) as cena, sum(cena_slev) as cena_slev, sum(prachymenu) as prachymenu, sum(cena_menu) as cena_menu, sum(dan_zakl2) as dan_zakl2, ;
           $0 as cena_prum, "    " as stredskup, SUM(round50) as round50 ;
           from zbozi1 group by id_man, id_kas, c_karty, dan_sazba INTO CURSOR zbozi_dph READWRITE
 
    DO dopln_nazev
    UPDATE zbozi_dph SET id_zkratka=fd.id_zkratka from fd WHERE zbozi_dph.id_kas=fd.id

    index on id_kas + id_man + nazev tag kasmannaz

    repl for empty(id_zkratka) id_zkratka with "STRED  >" + id_kas + "<"
    view_mssg = "Po DPH" 
    DO pr_report WITH "trz_prodzboz_dph", !m.reporty_volane_z_formu
    use
endif

DO pozavirej WITH "zbozi, zbozi1, zbozi_cenhl"


*!*****************************************************************************
*!      Procedure: ZBOZI_PO_HODINACH
*!*****************************************************************************
PROCEDURE zbozi_po_hodinach
TRY 
    had.SHOW()
CATCH
	had = CREATEOBJECT("had")
ENDTRY 

SELECT id_kas, uzaverka, u_cis, c_karty, SPACE(2) as hodina, ;
       sum(mnozstvi) as mnozstvi , sum(prachy) as prachy, k_odpoctu ;
       WHERE ALLTRIM(cen_hlad) $ m.cen_hladiny group by 1, 2, 3, 4 ;
       FROM tm_sk_pol INTO CURSOR zbozi_hod1 READWRITE
 
SET ORDER to uzucetkas IN tm_sk_fin
SET RELATION TO uzaverka + u_cis + id_kas INTO tm_sk_fin
REPLACE ALL hodina WITH SUBSTR(tm_sk_fin.u_cas, 4)
REPLACE FOR VAL(hodina) =  0 hodina WITH "00"
REPLACE FOR VAL(hodina) = 24 hodina WITH "00"
REPLACE FOR VAL(hodina) > 23 hodina WITH "23"
REPLACE ALL hodina WITH TRANSFORM(VAL(hodina), "@L 99")
SELECT id_kas, c_karty, hodina, ;
       SUM(mnozstvi) as mnozstvi , SUM(prachy) as prachy ;
       GROUP BY 1, 2, 3 ;
       FROM zbozi_hod1 INTO CURSOR zbozi_hod2 READWRITE

SELECT id_kas, c_karty, ;
       SUM(mnozstvi) as mnozstvi , SUM(prachy) as prachy, ;
       SPACE(30) as nazev, SPACE(m.sirkastlpcadruh) as druh, SPACE(3) as id_zkratka, ;
       $0 AS trzba00, $0 AS trzba01, $0 AS trzba02, $0 AS trzba03, $0 AS trzba04, $0 AS trzba05, $0 AS trzba06, $0 AS trzba07, $0 AS trzba08, $0 AS trzba09, $0 AS trzba10, $0 AS trzba11, ;
       $0 AS trzba12, $0 AS trzba13, $0 AS trzba14, $0 AS trzba15, $0 AS trzba16, $0 AS trzba17, $0 AS trzba18, $0 AS trzba19, $0 AS trzba20, $0 AS trzba21, $0 AS trzba22, $0 AS trzba23, ;
       $0 AS mnoz00, $0 AS mnoz01, $0 AS mnoz02, $0 AS mnoz03, $0 AS mnoz04, $0 AS mnoz05, $0 AS mnoz06, $0 AS mnoz07, $0 AS mnoz08, $0 AS mnoz09, $0 AS mnoz10, $0 AS mnoz11, ;
       $0 AS mnoz12, $0 AS mnoz13, $0 AS mnoz14, $0 AS mnoz15, $0 AS mnoz16, $0 AS mnoz17, $0 AS mnoz18, $0 AS mnoz19, $0 AS mnoz20, $0 AS mnoz21, $0 AS mnoz22, $0 AS mnoz23  ;
       GROUP BY 1, 2 ;
       FROM zbozi_hod2 INTO CURSOR zbozi_hod3 READWRITE
DO dopln_nazev
INDEX on id_kas  TAG id_kas
INDEX on c_karty TAG c_karty

SELECT zbozi_hod2
SCAN
    SCATTER MEMVAR
    SELECT zbozi_hod3
    LOCATE FOR id_kas = m.id_kas .and. c_karty = m.c_karty
    cmd = 'REPLACE mnoz' + m.hodina + ' WITH m.mnozstvi, trzba' + m.hodina + ' WITH m.prachy'
    &cmd
ENDSCAN

had.hide()
SELECT zbozi_hod3
INDEX on id_kas + druh + nazev TAG k_d_n

o_gctxt_summary = m.gctxt_summary
gctxt_summary = "SUMMARY"
DO pr_report WITH "trz_prodzboz_po_hodinach", !m.reporty_volane_z_formu
gctxt_summary = m.o_gctxt_summary

DO pozavirej WITH "zbozi_hod1, zbozi_hod2, zbozi_hod3"


*!*****************************************************************************
*!      Procedure: ZBOZI_PO_SLOUPCICH
*!*****************************************************************************
PROCEDURE zbozi_po_sloupcich
TRY 
    had.SHOW()
CATCH
	had = CREATEOBJECT("had")
ENDTRY 
had.set_had(11)
SELECT DISTINCT id_kas, 00 AS dil, 0 AS sloupec FROM zbozi INTO CURSOR sloupce_kas READWRITE
REPLACE ALL dil WITH INT((RECNO()-1) / 5), sloupec WITH ((RECNO()-1) % 5) + 1
INDEX ON id_kas TAG id_kas

had.set_had(33)
SELECT DISTINCT druh, nazev FROM zbozi INTO CURSOR prodany_sortiment1 READWRITE
SELECT       druh, nazev, 00 AS dil, ;
	$0 AS mnozstvi1, $0 AS mnozstvi2, $0 AS mnozstvi3, $0 AS mnozstvi4, $0 AS mnozstvi5, ;
	$0 AS   prachy1, $0 AS   prachy2, $0 AS   prachy3, $0 AS   prachy4, $0 AS   prachy5, ;
	SPACE(15) AS nadpis1, SPACE(15) AS nadpis2, SPACE(15) AS nadpis3, SPACE(15) AS nadpis4, SPACE(15) AS nadpis5 ;
	FROM prodany_sortiment1 INTO CURSOR prodany_sortiment READWRITE

SELECT DISTINCT dil WHERE dil > 0 FROM sloupce_kas INTO CURSOR dily
SCAN
	SCATTER MEMVAR
	INSERT INTO prodany_sortiment (druh, nazev, dil) SELECT druh, nazev, m.dil AS dil FROM prodany_sortiment1
	SELECT dily
ENDSCAN
SELECT prodany_sortiment
INDEX ON nazev TAG nazev
INDEX ON dil TAG dil

had.set_had(66)
SELECT id_kas, druh, nazev, SUM(mnozstvi) AS mnozstvi, SUM(cena) AS prachy, 00 AS dil, 0 AS sloupec ;
	GROUP BY 1,2,3 FROM zbozi INTO CURSOR zbozi_po_sloupcich READWRITE

SET RELATION TO id_kas INTO sloupce_kas
REPLACE ALL dil WITH sloupce_kas.dil, sloupec WITH sloupce_kas.sloupec
SCAN
	SCATTER MEMVAR
	SELECT prodany_sortiment
	LOCATE FOR dil = m.dil .AND. nazev = m.nazev
	DO CASE
		CASE m.sloupec = 1
			REPLACE mnozstvi1 WITH mnozstvi, prachy1 WITH prachy
		CASE m.sloupec = 2
			REPLACE mnozstvi2 WITH mnozstvi, prachy2 WITH prachy
		CASE m.sloupec = 3
			REPLACE mnozstvi3 WITH mnozstvi, prachy3 WITH prachy
		CASE m.sloupec = 4
			REPLACE mnozstvi4 WITH mnozstvi, prachy4 WITH prachy
		CASE m.sloupec = 5
			REPLACE mnozstvi5 WITH mnozstvi, prachy5 WITH prachy
	ENDCASE
ENDSCAN

had.set_had(88)
SELECT sloupce_kas
SCAN
	SCATTER MEMVAR
	SELECT prodany_sortiment
	DO CASE
		CASE m.sloupec = 1
			REPLACE FOR dil = m.dil nadpis1 WITH m.id_kas
		CASE m.sloupec = 2
			REPLACE FOR dil = m.dil nadpis2 WITH m.id_kas
		CASE m.sloupec = 3
			REPLACE FOR dil = m.dil nadpis3 WITH m.id_kas
		CASE m.sloupec = 4
			REPLACE FOR dil = m.dil nadpis4 WITH m.id_kas
		CASE m.sloupec = 5
			REPLACE FOR dil = m.dil nadpis5 WITH m.id_kas
	ENDCASE
ENDSCAN
SELECT prodany_sortiment
INDEX ON STR(dil, 2) + druh + nazev TAG d_d_n
had.HIDE()
DO pr_report WITH "trz_prodzboz_po_sloupcich", !m.reporty_volane_z_formu
DO pozavirej WITH "prodany_sortiment, prodany_sortiment1, zbozi_po_sloupcich, sloupce_kas"


*!*****************************************************************************
*!      Procedure: ZBOZI_PO_HODNE_SLOUPCICH
*!*****************************************************************************
PROCEDURE zbozi_po_hodne_sloupcich
TRY 
    had.SHOW()
CATCH
	had = CREATEOBJECT("had")
ENDTRY 
had.set_had(11)
had.set_txt1("zbozi_po_hodne_sloupcich")
SELECT DISTI id_kas, 00 AS sloupec FROM zbozi INTO CURSOR sloupce_kas READWRITE
REPLACE ALL sloupec WITH MIN(RECNO(), 40)    && muze byt maximalne 40 sloupcu
INDEX ON id_kas TAG id_kas

had.set_had(33)
SELECT id_kas, druh, nazev, SUM(mnozstvi) AS mnozstvi, SUM(cena) AS prachy, 00 AS sloupec ;
	GROUP BY 1,2,3 FROM zbozi INTO CURSOR zbozi_po_sloupcich READWRITE
had.set_had(66)
SELECT DISTI druh, nazev, ;
	$0 AS m01, $0 AS p01, $0 AS m02, $0 AS p02, $0 AS m03, $0 AS p03, $0 AS m04, $0 AS p04, $0 AS m05, $0 AS p05, ;
	$0 AS m06, $0 AS p06, $0 AS m07, $0 AS p07, $0 AS m08, $0 AS p08, $0 AS m09, $0 AS p09, $0 AS m10, $0 AS p10, ;
	$0 AS m11, $0 AS p11, $0 AS m12, $0 AS p12, $0 AS m13, $0 AS p13, $0 AS m14, $0 AS p14, $0 AS m15, $0 AS p15, ;
	$0 AS m16, $0 AS p16, $0 AS m17, $0 AS p17, $0 AS m18, $0 AS p18, $0 AS m19, $0 AS p19, $0 AS m20, $0 AS p20, ;
	$0 AS m21, $0 AS p21, $0 AS m22, $0 AS p22, $0 AS m23, $0 AS p23, $0 AS m24, $0 AS p24, $0 AS m25, $0 AS p25, ;
	$0 AS m26, $0 AS p26, $0 AS m27, $0 AS p27, $0 AS m28, $0 AS p28, $0 AS m29, $0 AS p29, $0 AS m30, $0 AS p30, ;
	$0 AS m31, $0 AS p31, $0 AS m32, $0 AS p32, $0 AS m33, $0 AS p33, $0 AS m34, $0 AS p34, $0 AS m35, $0 AS p35, ;
	$0 AS m36, $0 AS p36, $0 AS m37, $0 AS p37, $0 AS m38, $0 AS p38, $0 AS m39, $0 AS p39, $0 AS m40, $0 AS p40  ;
	FROM zbozi_po_sloupcich INTO CURSOR prodany_sortiment READWRITE
INDEX ON nazev TAG nazev

SELECT zbozi_po_sloupcich

SET RELATION TO id_kas INTO sloupce_kas
REPLACE ALL sloupec WITH sloupce_kas.sloupec
SCAN
	SCATTER MEMVAR
	SELECT prodany_sortiment
	LOCATE FOR nazev = m.nazev
	cmd = 'REPLACE m' + TRANSFORM(sloupec, "@L 99") + ' WITH mnozstvi, p' + TRANSFORM(sloupec, "@L 99") + ' WITH prachy'
	&cmd
*        REPLACE mnozstvi1 WITH mnozstvi, prachy1 WITH prachy
ENDSCAN

had.set_had(88)
SELECT sloupce_kas
SCAN
	SCATTER MEMVAR
	cmd = 'ALTER TABLE prodany_sortiment RENAME m' + TRANSFORM(m.sloupec, "@L 99") + ' TO   mnoz_' + m.id_kas
	&cmd
	cmd = 'ALTER TABLE prodany_sortiment RENAME p' + TRANSFORM(m.sloupec, "@L 99") + ' TO prachy_' + m.id_kas
	&cmd
ENDSCAN
SELECT prodany_sortiment
INDEX ON druh + nazev TAG d_n

had.HIDE()
o_gctxt_summary = m.gctxt_summary
gctxt_summary = "SUMMARY"
DO pr_report WITH "trz_prodzboz_po_hodne_sloupcich", !m.reporty_volane_z_formu
gctxt_summary = m.o_gctxt_summary
DO pozavirej WITH "prodany_sortiment, zbozi_po_sloupcich, sloupce_kas"

*!*****************************************************************************
*!      Procedure: S_PROFIT
*!*****************************************************************************
PROCEDURE s_profit
PARAMETERS jakesestavy
PRIVATE profit_txt
cenhl = ""
last_ceny = ""

TRY 
    had.SHOW()
CATCH
	had = CREATEOBJECT("had")
ENDTRY 

IF "KAS" $ m.jakesestavy
	profit_txt = "A N A L Y Z A  P R O F I T U  P O  K A S A C H "
	SELECT id_kas,         c_karty, cen_hlad, 1 AS c_mena, c_kas, SPACE(30) as zkr_kas, ;
		SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, SUM(prachy_puv) AS prachy_puv, SUM(dan) AS dan, $0 AS dan_puv, ;
		$0 AS mnoz1, $0 AS mnoz2, $0 AS mnoz3, $0 AS mnoz4, $0 AS mnoz5, $0 AS mnoz6, $0 AS mnoz7, $0 AS mnoz8, $0 AS mnoz9 ;
		WHERE (.NOT. EMPTY(k_odpoctu)) .AND. (ALLTRIM(cen_hlad) $ m.cen_hladiny) ;
		GROUP BY id_kas, c_karty, cen_hlad ORDER BY c_karty FROM tm_sk_pol INTO CURSOR prof1 READWRITE
	SET RELATION TO c_kas INTO fp_str
	REPLACE ALL zkr_kas WITH fp_str.id_zkratka
	SET RELATION TO
ELSE
	profit_txt = "A N A L Y Z A  P R O F I T U  Z A  H O T E L "
    SELECT "  " AS id_kas, c_karty, cen_hlad,  1 as c_mena,    "cely hotel" AS zkr_kas, ;
        SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, SUM(prachy_puv) AS prachy_puv, SUM(dan) AS dan, $0 AS dan_puv, ;
		$0 AS mnoz1, $0 AS mnoz2, $0 AS mnoz3, $0 AS mnoz4, $0 AS mnoz5, $0 AS mnoz6, $0 AS mnoz7, $0 AS mnoz8, $0 AS mnoz9 ;
		WHERE (.NOT. EMPTY(k_odpoctu)) .AND. (ALLTRIM(cen_hlad) $ m.cen_hladiny) ;
		GROUP BY id_kas, c_karty, cen_hlad ORDER BY c_karty FROM tm_sk_pol INTO CURSOR prof1 READWRITE
ENDIF
REPLACE FOR prachy # 0 dan_puv WITH dan * prachy_puv / prachy

SELECT DISTINCT cen_hlad ORDER BY cen_hlad FROM prof1 INTO CURSOR cen_hl READWRITE
hlavicka = ""
SCAN
	SCATTER MEMVAR
	IF RECNO() < 10
		hlavicka = ALLTRIM(hlavicka + SPACE(12) + m.cen_hlad)
		cmdp = 'repl for cen_hlad = "' + m.cen_hlad + '" mnoz' + ALLTRIM(STR(RECNO())) + ' with mnozstvi'
		SELECT prof1
		&cmdp
	ELSE
		hlavicka = ALLTRIM(hlavicka) + m.cen_hlad
		SELECT prof1
		REPL FOR cen_hlad = m.cen_hlad mnoz9 WITH mnoz9 + mnozstvi
	ENDIF
	SELECT cen_hl
ENDSCAN

* berou se sloupce PRACHY_PUV a DAN_PUV, takze ceny PRED SLEVOU, pokud by se chtely ceny po sleve, staci vzit PRACHY a DAN
SELECT id_kas, c_karty, SPACE(30) AS nazev, zkr_kas AS id_zkratka, SPACE(m.sirkastlpcadruh) AS druh, SPACE(3) AS spart, ;
       SUM(mnozstvi) AS mnozstvi, SUM(prachy_puv) AS cena_brtto, SUM(prachy_puv - dan_puv) AS cena_netto, $0 AS cena_prum, ;
       SUM(mnoz1) AS mnoz1, SUM(mnoz2) AS mnoz2, SUM(mnoz3) AS mnoz3, SUM(mnoz4) AS mnoz4, SUM(mnoz5) AS mnoz5, SUM(mnoz6) AS mnoz6, SUM(mnoz7) AS mnoz7, SUM(mnoz8) AS mnoz8, SUM(mnoz9) AS mnoz9, ;
       $0 AS marze_net, $0 AS mrz_netsum, $0 AS cen_netsum, $0 AS profit ;
       GROUP BY id_kas, c_karty ORDER BY nazev FROM prof1 INTO CURSOR pom READWRITE

had.set_had( 55 )
DO dopln_nazev
SET FILTER TO mnozstvi # 0

REPLACE ALL marze_net WITH cena_netto - cena_prum
INDEX ON id_kas + spart + druh TAG ksd
INDEX ON id_kas + spart + druh + nazev TAG ksdn

had.set_had( 88 )

SELECT id_kas + spart + druh AS k_s_d, SUM(marze_net) AS mrz_netsum, SUM(cena_netto) AS cen_netsum FROM pom GROUP BY 1 INTO CURSOR marz_sum
INDEX ON k_s_d TAG k_s_d
SELECT pom
SET RELATION TO id_kas + spart + druh INTO marz_sum
REPLACE ALL mrz_netsum WITH marz_sum.mrz_netsum, cen_netsum WITH marz_sum.cen_netsum
SET RELATION OFF INTO marz_sum

had.HIDE()

IF "1" $ m.dily
	REPLACE FOR cena_prum # 0 profit WITH ROUND(100 * marze_net / cena_prum, m.girep_round)
	DO pr_report WITH "trz_profit1", !m.reporty_volane_z_formu
ENDIF

IF "2" $ m.dily
	REPLACE FOR mrz_netsum # 0 profit WITH MIN(ROUND(100 * marze_net / mrz_netsum, m.girep_round), 9999)
	DO pr_report WITH "trz_profit2", !m.reporty_volane_z_formu
ENDIF

IF "3" $ m.dily
	REPLACE ALL marze_net WITH 0
	REPLACE FOR cena_netto # 0 marze_net WITH 100 * cena_prum / cena_netto
	DO pr_report WITH "trz_profit3", !m.reporty_volane_z_formu
ENDIF
USE
DO pozavirej WITH "pom, prof1, marz_sum, cen_hl"

*:*****************************************************************************
*:        Procedure: ROZPETI
*:*****************************************************************************
PROCEDURE rozpeti
PARAMETERS jakesestavy
had.SHOW()

SELECT c_kas, c_stredisk, uzaverka, c_karty, dan_sazba, cen_hlad, id_kas, id_man, ;
       SUM(prachy)     AS prachy,     SUM(dan) AS dan, SUM(mnozstvi) as mnozstvi, ;
       SUM(prachy_puv) AS prachy_puv,    $0    AS dan_puv, $0 AS cena_prum, ;
       $0 AS dan1, $0 AS dan2, $0 AS dan3, $0 AS dan4, $0 AS dz1, $0 AS dz2, $0 AS dz3, $0 AS dz4 ;
       WHERE ALLTRIM(cen_hlad) $ m.cen_hladiny GROUP BY 1,2,3,4,5,6 ;
       FROM tm_sk_pol INTO CURSOR namarkovano1 READWRITE

REPLACE FOR "*" $ uzaverka  cen_hlad WITH "1 "

*REPLACE FOR EMPTY(cen_hlad) cen_hlad WITH "1 "
*REPLACE FOR VAL(cen_hlad) # 0 cen_hlad WITH TRANSFORM(VAL(cen_hlad), "@L 99")

DO dopln_nazev
REPLACE FOR prachy # 0 dan_puv WITH dan * prachy_puv / prachy

SELECT c_kas, c_stredisk, uzaverka, cen_hlad, id_kas, id_man, SPACE(30) AS zkr_kas, SPACE(30) AS zkr_man, $0 AS naklady, ;
       SUM(prachy) AS prachy, SUM(prachy-dan) AS vynos, SUM(prachy_puv) AS prachy_puv, SUM(prachy_puv - dan_puv) AS vynos_puv, SUM(cena_prum) AS cena_prum, 00.00 as round50 ;
       GROUP BY 1,2,3,4 FROM namarkovano1 INTO CURSOR namarkovano READWRITE
      

UPDATE namarkovano SET zkr_kas=fp_str.id_zkratka FROM fp_str WHERE namarkovano.c_kas=fp_str.c_stredisk
UPDATE namarkovano SET zkr_man=fp_str.id_zkratka FROM fp_str WHERE namarkovano.c_stredisk=fp_str.c_stredisk
INDEX ON STR(c_kas) + STR(c_stredisk) + uzaverka + cen_hlad TAG ksuc

DO priprav_odectene

SELECT c_kas, c_stredisk, uzaverka, cen_hlad, SUM(mnozstvi * cena) AS naklady ;
	GROUP BY 1,2,3,4 FROM odecteno INTO CURSOR odecteno2 READWRITE

REPLACE FOR "*" $ uzaverka  cen_hlad WITH "1 "
*REPLACE FOR EMPTY(cen_hlad) cen_hlad WITH "1 "
*REPLACE FOR VAL(cen_hlad) # 0 cen_hlad WITH TRANSFORM(VAL(cen_hlad), "@L 99")

IF m.slovenskaverzia OR m.issql
	UPDATE namarkovano SET naklady=odecteno2.naklady from odecteno2 ;
	       WHERE namarkovano.c_kas=odecteno2.c_kas AND namarkovano.c_stredisk=odecteno2.c_stredisk AND LEFT(namarkovano.uzaverka,14)=LEFT(odecteno2.uzaverka,14) AND namarkovano.cen_hlad=odecteno2.cen_hlad
ELSE
	UPDATE namarkovano SET naklady=odecteno2.naklady from odecteno2 ;
	       WHERE namarkovano.c_kas=odecteno2.c_kas AND namarkovano.c_stredisk=odecteno2.c_stredisk AND namarkovano.uzaverka=odecteno2.uzaverka AND namarkovano.cen_hlad=odecteno2.cen_hlad
ENDIF 
* * * * * * * * * * * * * *

SELECT id_kas, id_man, uzaverka, SUM(cena_prum) AS cena_prum, SUM(prachy) AS cena_prod, SUM(vynos) AS vynos, SUM(naklady) AS naklady, ;
	"  " AS pom, $0 AS rozpeti, $0 AS sumrozpeti, $0 AS rozpeti2, $0 AS sumrozp2, zkr_kas AS id_zkratka, 00.00 as round50 ;
	GROUP BY id_kas, id_man, uzaverka FROM namarkovano INTO CURSOR rozpeti READWRITE ORDER BY id_kas
SELECT id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas INTO CURSOR roundy
UPDATE rozpeti SET round50=roundy.round50 from roundy WHERE rozpeti.id_kas=roundy.id_kas
LOCAL m.idxx
m.idxx="/\"
SELECT rozpeti
SCAN ALL 
	IF m.idxx=rozpeti.id_kas
		REPLACE round50 WITH 0
	ELSE
		m.idxx=rozpeti.id_kas
	ENDIF 
ENDSCAN
GO TOP 
RELEASE m.idxx
USE IN roundy
SELECT rozpeti

IF !m.rozpetiesnulami     
	DELETE FOR (vynos=0) .AND. (naklady=0)
ENDIF 

SUM vynos, naklady, cena_prum TO celk_vynos, celk_naklady, celk_cena_prum
STORE 0 TO celk_rozpeti, celk_rozpeti2

had.HIDE()
*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *
IF "UZAV" $ m.jakesestavy
	DO spocti_celk_rozpeti
	m.report_ttl = jaz("rep_ttl_roz_kas_uzav")
	DO pr_report WITH "trz_rozpeti_uzav", !m.reporty_volane_z_formu
ENDIF

IF "KASMAN" $ m.jakesestavy
	SELECT id_kas, id_man, SUM(prachy) AS cena_prod, SUM(vynos) AS vynos, SUM(naklady) AS naklady, SUM(cena_prum) AS cena_prum, ;
		$0 AS rozpeti, $0 AS sumrozpeti, $0 AS rozpeti2, $0 AS sumrozp2, SPACE(30) AS pom, zkr_kas AS kas_zkrat, zkr_man AS man_zkrat, 00.00 as round50 ;
		GROUP BY id_kas, id_man FROM namarkovano INTO CURSOR rozpeti READWRITE
	SELECT id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas INTO CURSOR roundy
	UPDATE rozpeti SET round50=roundy.round50 from roundy WHERE rozpeti.id_kas=roundy.id_kas
	LOCAL m.idxx
	m.idxx="/\"
	SELECT rozpeti
	SCAN ALL 
		IF m.idxx=rozpeti.id_kas
			REPLACE round50 WITH 0
		ELSE
			m.idxx=rozpeti.id_kas
		ENDIF 
	ENDSCAN
	GO TOP 
	RELEASE m.idxx
	USE IN roundy
	SELECT rozpeti
	IF !m.rozpetiesnulami     
		DELETE FOR (vynos=0) .AND. (naklady=0)
	ENDIF 

	SUM vynos, naklady, cena_prum TO celk_vynos, celk_naklady, celk_cena_prum

	IF "1" $ m.dily
		DO spocti_celk_rozpeti
		m.report_ttl = jaz("rep_ttl_roz_kas_man")
		DO pr_report WITH "trz_rozpeti", !m.reporty_volane_z_formu
	ENDIF

	IF "2" $ m.dily
		REPLACE ALL pom WITH id_kas, id_kas WITH id_man, id_man WITH pom ;
			pom WITH kas_zkrat, kas_zkrat WITH man_zkrat, man_zkrat WITH pom
		INDEX ON id_kas+id_man TAG kas_man

		DO spocti_celk_rozpeti

		m.report_ttl = jaz("rep_ttl_roz_man_kas")
		DO pr_report WITH "trz_rozpeti@2", !m.reporty_volane_z_formu
	ENDIF

ENDIF

IF "DPH" $ m.jakesestavy
	SELECT DISTINCT dan_sazba ORDE BY dan_sazba FROM namarkovano1 INTO CURSOR dane READWRITE
	APPEND BLANK
	APPEND BLANK
	APPEND BLANK

	GO TOP
	_dan1 = dane.dan_sazba
	SKIP
	_dan2 = dane.dan_sazba
	SKIP
	_dan3 = dane.dan_sazba
	SKIP
	_dan4 = dane.dan_sazba
	USE
	IF _dan1 > 1.0
        _dan4 = _dan3
		_dan3 = _dan2
		_dan2 = _dan1
		_dan1 = 1.0
	ENDIF

*!*    	IF ! EMPTY(_dan4)
*!*    		= MESSAGEBOX("Pøíliš mnoho rùzných sazeb DPH, tisknou se jen prvni 3" , 0+64+256, "", 7777)
*!*    	ENDIF

	SELECT namarkovano1
	REPLACE FOR dan_sazba = m._dan1 dz1 WITH prachy-dan, dan1 WITH dan
	REPLACE FOR dan_sazba = m._dan2 dz2 WITH prachy-dan, dan2 WITH dan
    REPLACE FOR dan_sazba = m._dan3 dz3 WITH prachy-dan, dan3 WITH dan
	REPLACE FOR dan_sazba = m._dan4 dz4 WITH prachy-dan, dan4 WITH dan

	SELECT c_kas, c_stredisk, uzaverka, cen_hlad, id_kas, id_man, ;
		SUM(prachy) AS cena_prod , SUM(dan) AS dan_zakl, $0 AS naklady, ;
		SUM(dz1) AS dz1, SUM(dan1) AS dan1, SUM(dz2) AS dz2, SUM(dan2) AS dan2, SUM(dz3) AS dz3, SUM(dan3) AS dan3, SUM(dz4) AS dz4, SUM(dan4) AS dan4 ;
		GROUP BY 1, 2, 3, 4 FROM namarkovano1 INTO CURSOR rozpeti5 READWRITE
	IF m.slovenskaverzia OR m.issql
		INDEX ON STR(c_kas) + STR(c_stredisk) + LEFT(uzaverka,14) + cen_hlad TAG ksuc
		SELECT odecteno2
		SET RELATION TO STR(c_kas) + STR(c_stredisk) + LEFT(uzaverka,14) + cen_hlad INTO rozpeti5
	ELSE
		INDEX ON STR(c_kas) + STR(c_stredisk) + uzaverka + cen_hlad TAG ksuc
		SELECT odecteno2
		SET RELATION TO STR(c_kas) + STR(c_stredisk) + uzaverka + cen_hlad INTO rozpeti5
	ENDIF 
	REPLACE ALL rozpeti5.naklady WITH naklady
	SET RELATION TO

	SELECT id_kas, id_man, cen_hlad, c_kas, SUM(cena_prod) AS cena_prod, SUM(dan_zakl) AS vynos1, ;
		SUM(dz1) AS dz1, SUM(dan1) AS dan1, SUM(dz2) AS dz2, SUM(dan2) AS dan2, SUM(dz3) AS dz3, SUM(dan3) AS dan3, SUM(dz4) AS dz4, SUM(dan4) AS dan4, ;
		SUM(naklady) AS naklady, $0 AS rozpeti, $0 AS sumrozpeti, SPACE(30) AS zkr_kas, 00.00 as round50 ;
		GROUP BY 1, 2, 3 FROM rozpeti5 INTO CURSOR rozpeti6 READWRITE ORDER BY id_kas

	IF isfoodcost
		REPLACE FOR dz1+dz2+dz3+dz4 # 0   rozpeti WITH 100 * naklady / (dz1+dz2+dz3+dz4)
	ELSE
		REPLACE FOR naklady # 0 rozpeti WITH 100 * (dz1+dz2+dz3+dz4 - naklady) / naklady
	ENDIF

	SELECT id_kas, SUM(vynos1) AS vynos, SUM(naklady) AS naklady, $0 AS rozpeti ;
		GROUP BY id_kas FROM rozpeti6 INTO CURSOR sumrozpeti READWRITE
	SUM vynos, naklady TO celk_vynos, celk_naklady

	IF m.isfoodcost
		REPLACE FOR vynos # 0   rozpeti WITH 100 * naklady / vynos
		celk_rozpeti = IIF(celk_vynos = 0, 0, 100 * celk_naklady / celk_vynos)
	ELSE
		REPLACE FOR naklady # 0 rozpeti WITH 100 * (vynos - naklady) / naklady
		celk_rozpeti = IIF(celk_naklady = 0, 0, 100 * (celk_vynos - celk_naklady) / celk_naklady)
	ENDIF
	INDEX ON id_kas TAG id_kas

	SELECT rozpeti6
	SELECT id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas INTO CURSOR roundy
	UPDATE rozpeti6 SET round50=roundy.round50 from roundy WHERE rozpeti6.id_kas=roundy.id_kas
	LOCAL m.idxx
	m.idxx="/\"
	SELECT rozpeti6
	SCAN ALL 
		IF m.idxx=rozpeti6.id_kas
			REPLACE round50 WITH 0
		ELSE
			m.idxx=rozpeti6.id_kas
		ENDIF 
	ENDSCAN
	GO TOP 
	RELEASE m.idxx
	USE IN roundy
	SELECT rozpeti6
	
	SET RELATION TO c_kas INTO fp_str
	REPLACE ALL zkr_kas WITH fp_str.id_zkratka
	SET RELATION TO id_kas INTO sumrozpeti
	REPLACE ALL sumrozpeti WITH sumrozpeti.rozpeti
	USE IN sumrozpeti

*	DO pr_report WITH "trz_rozpeti2"
    DO pr_report WITH IIF(m.s_dph4 > 9, "trz_rozpeti2", "trz_rozpeti2_4sl"), !m.reporty_volane_z_formu     && pokud jsou pouzity 4 sazby dph, pouzije se dvouradkovy report
ENDIF

DO pozavirej WITH "odect, mans, rozpeti, rozpeti1, naklady, naklady5, rozpeti5, rozpeti6, sumrozpeti, namarkovano, namarkovano1, odecteno2"
DO pozavirej WITH "vynosy_naklady, vynosy_naklady1, hhhosti, buff_bb, buff_cc, buff_aa, naklady_1"

*!*****************************************************************************
PROCEDURE spocti_celk_rozpeti
SELECT id_kas, SUM(vynos) AS vynos, SUM(naklady) AS naklady, SUM(cena_prum) AS cena_prum, $0 AS rozpeti, $0 AS rozpeti2 ;
	GROUP BY id_kas FROM rozpeti INTO CURSOR sumrozpeti READWRITE

IF m.isfoodcost
	REPL FOR vynos # 0 rozpeti  WITH 100 * naklady   / vynos
	REPL FOR vynos # 0 rozpeti2 WITH 100 * cena_prum / vynos
	celk_rozpeti  = IIF(celk_vynos = 0, 0, 100 * celk_naklady   / celk_vynos)
	celk_rozpeti2 = IIF(celk_vynos = 0, 0, 100 * celk_cena_prum / celk_vynos)
ELSE
	REPL FOR naklady   # 0 rozpeti  WITH 100 * (vynos - naklady)   / naklady
	REPL FOR cena_prum # 0 rozpeti2 WITH 100 * (vynos - cena_prum) / cena_prum
	celk_rozpeti  = IIF(celk_naklady   = 0, 0, 100 * (celk_vynos - celk_naklady)   / celk_naklady)
	celk_rozpeti2 = IIF(celk_cena_prum = 0, 0, 100 * (celk_vynos - celk_cena_prum) / celk_cena_prum)
ENDIF
INDEX ON id_kas TAG id_kas

SELECT rozpeti
SET RELATION TO id_kas INTO sumrozpeti
REPLACE ALL sumrozpeti WITH sumrozpeti.rozpeti, sumrozp2 WITH sumrozpeti.rozpeti2
USE IN sumrozpeti

IF m.isfoodcost
	REPLACE FOR vynos # 0 rozpeti  WITH 100 * naklady   / vynos, ;
		rozpeti2 WITH 100 * cena_prum / vynos
ELSE
	REPLACE FOR naklady   # 0 rozpeti  WITH 100 * (vynos - naklady)   / naklady
	REPLACE FOR cena_prum # 0 rozpeti2 WITH 100 * (vynos - cena_prum) / cena_prum
ENDIF

*:*****************************************************************************
*:        Procedure: priprav_odectene
*:*****************************************************************************
PROCEDURE priprav_odectene
IF USED("o_dokl1") AND USED("odecteno")
	RETURN
ENDIF
SET ORDER TO c_karty IN karty
TRY 
    had.SHOW()
CATCH
	had = CREATEOBJECT("had")
ENDTRY 
had.set_had( 33 )
IF m.issql
	rv("zosttrzdoklady_vk","o_dokl1x")
	rv("zosttrzdoklady_vk_a","o_dokl1_a")
	SELECT c_dokl, c_stredisk, doklad, datum_dokl, c_partner, pz1, pz2, c_mena, provedl+" "+TTOC(kdy) AS kdo_kdy, cen_hlad, SPACE(19) AS uzaverka FROM o_dokl1x INTO CURSOR o_dokl1 READWRITE ;
		UNION SELECT c_dokl, c_stredisk, doklad, datum_dokl, c_partner, pz1, pz2, c_mena, provedl+" "+TTOC(kdy) AS kdo_kdy, cen_hlad, SPACE(19) AS uzaverka FROM o_dokl1_a
	DO pozavirej WITH "o_dokl1x, o_dokl1_a"
ELSE
	USE (m.fn_doklady) ALIAS doklady IN 0
	USE (m.fn_dokl_a) ALIAS dokl_a IN 0
	SELECT c_dokl, c_stredisk, doklad, datum_dokl, c_partner, pz1, pz2, c_mena, provedl + " " + TTOC(kdy) AS kdo_kdy, cen_hlad ;
		WHERE operace = "VK" FROM dokl_a INTO CURSOR o_dokl1_a
	SELECT c_dokl, c_stredisk, doklad, datum_dokl, c_partner, pz1, pz2, c_mena, provedl + " " + TTOC(kdy) AS kdo_kdy, cen_hlad, PADL(pz2, 19) AS uzaverka ;
		WHERE operace = "VK" FROM doklady INTO CURSOR o_dokl1 READWRITE
	INSERT INTO o_dokl1 (c_dokl, c_stredisk, doklad, datum_dokl, c_partner, pz1, pz2, c_mena, kdo_kdy, cen_hlad);
		SELECT c_dokl, c_stredisk, doklad, datum_dokl, c_partner, pz1, pz2, c_mena, kdo_kdy, cen_hlad FROM o_dokl1_a
	DO pozavirej WITH "doklady, dokl_a, o_dokl1_a"
ENDIF
had.set_had( 44 )
SELECT o_dokl1

REPLACE FOR "*" $    pz2    cen_hlad WITH "1 "
REPLACE FOR EMPTY(cen_hlad) cen_hlad WITH "1 "
REPLACE FOR VAL(cen_hlad) # 0 cen_hlad WITH ALLTRIM(STR(VAL(cen_hlad)))

SET PROCEDURE TO sk_new ADDI
REPLACE FOR "/" $ pz2 uzaverka WITH uzav_old2new (LEFT(pz2, 12))
RELEASE PROCEDURE sk_new


STORE 12 TO uz_sk_len, uz_dok_len 

SELECT AVG(LEN(ALLTRIM(uzaverka))) as uz_sk_len FROM namarkovano INTO CURSOR uz_sk_len
IF RECCOUNT() = 1
    GO TOP 
    SCATTER MEMVAR 
ENDIF 

SELECT AVG(LEN(ALLTRIM(uzaverka))) as uz_dok_len FROM o_dokl1    INTO CURSOR uz_dok_len
IF RECCOUNT() = 1
    GO TOP 
    SCATTER MEMVAR 
ENDIF 

IF m.uz_dok_len > m.uz_sk_len
    SELECT o_dokl1
    REPLACE ALL uzaverka WITH SUBSTR(uzaverka, 3)
ENDIF 

had.set_had( 55 )
SELECT * WHERE LEFT(uzaverka,14) + STR(c_partner) IN (SELECT LEFT(uzaverka,14) + STR(c_kas) FROM namarkovano) ;
	FROM o_dokl1 INTO CURSOR o_dokl1b READWRITE 

*        *        *        *        *        *        *        prepsani jmena davky u odpoctu dluhu ZACATEK
SELECT o_dokl1b
INDEX on pz2 TO qweqwe
pz2 = "#$%^&)(*%^&"
c_stredisk = 987654213
c_partner  = 987654651
cen_hlad   = "QQ"
pz1        = "XXXXXXXXX"
SCAN
    IF pz2 = m.pz2 .and. c_stredisk = m.c_stredisk .and. c_partner = m.c_partner .and. cen_hlad = m.cen_hlad
        REPLACE pz1 WITH m.pz1
    ELSE
        SCATTER FIELDS pz2, c_stredisk, c_partner, cen_hlad, pz1 MEMVAR 
    ENDIF 
ENDSCAN
*        *        *        *        *        *        *        prepsani jmena davky u odpoctu dluhu KONEC

INDEX ON c_dokl TAG c_dokl

IF m.issql
	PRIVATE m.mincdokl, m.pocet, m.id_zoz
	rv("mincdokl","mincdokl")
	SELECT mincdokl
	mincdokl = IIF(RECCOUNT()=1, mincdokl.c_dokl, 0)
	USE 
	SELECT o_dokl1b
	IF RECCOUNT()>0
		pocet=0
		id_zoz=""
		SCAN FOR c_dokl>=m.mincdokl
			id_zoz=m.id_zoz+","+ALLTRIM(STR(o_dokl1b.c_dokl,15,0))
			pocet=m.pocet+1
			IF m.pocet>20
				m.id_zoz=SUBSTR(m.id_zoz,2)
				lsql="SELECT c_dokl, c_stredisk, c_karty, mnozstvi, cena from food600.obraty"
				lsql=lsql+" WHERE obraty.operace = 'VK' and obraty.c_dokl in ("+m.id_zoz+")  AND obraty.c_data= ?m.cdata AND obraty.c_karty>0"
				sql_select(lsql,'o_obr2_a','c_dokl i,c_stredisk i,c_karty i, mnozstvi Y,cena Y')
				IF RECCOUNT("o_obr2_a")>0
					IF USED("odecteno")
						INSERT INTO odecteno (c_dokl, c_stredisk, c_karty, mnozstvi, cena) SELECT c_dokl, c_stredisk, c_karty, -mnozstvi, cena FROM o_obr2_a
					ELSE
						SELECT c_dokl, c_stredisk, c_karty, - mnozstvi AS mnozstvi, cena, SPACE(30) AS nazev, 00000000 AS doklad, SPACE(19) AS uzaverka, ;
							{} AS datum_dokl, 00000000 AS c_kas, 1 AS c_mena, SPACE(20) AS kdo_kdy, "1 " AS cen_hlad, SPACE(15) AS pz1, SPACE(15) AS pz2, SPACE(13) AS kod, ;
                            0000000000 AS c_druh, SPACE(3) AS spart, SPACE(m.sirkastlpcadruh) AS druh, 0.000 as dan ;
							FROM o_obr2_a INTO CURSOR odecteno READWRITE
					ENDIF
				ENDIF
				USE IN o_obr2_a
				pocet=0
				id_zoz=""
			ENDIF
			SELECT o_dokl1b
		ENDSCAN
		IF m.pocet>0
			id_zoz=SUBSTR(m.id_zoz,2)
			lsql="SELECT c_dokl, c_stredisk, c_karty, mnozstvi, cena from food600.obraty"
			lsql=lsql+" WHERE obraty.operace = 'VK' and obraty.c_dokl in ("+m.id_zoz+")  AND obraty.c_data= ?m.cdata AND obraty.c_karty>0"
			sql_select(lsql,'o_obr2_a','c_dokl i,c_stredisk i,c_karty i, mnozstvi Y,cena Y')
			IF RECCOUNT("o_obr2_a")>0
				IF USED("odecteno")
					INSERT INTO odecteno (c_dokl, c_stredisk, c_karty, mnozstvi, cena) SELECT c_dokl, c_stredisk, c_karty, -mnozstvi, cena FROM o_obr2_a
				ELSE
					SELECT c_dokl, c_stredisk, c_karty, - mnozstvi AS mnozstvi, cena, SPACE(30) AS nazev, 00000000 AS doklad, SPACE(19) AS uzaverka, ;
						{} AS datum_dokl, 00000000 AS c_kas, 1 AS c_mena, SPACE(20) AS kdo_kdy, "1 " AS cen_hlad, SPACE(15) AS pz1, SPACE(15) AS pz2, SPACE(13) AS kod, ;
                        0000000000 AS c_druh, SPACE(3) AS spart, SPACE(m.sirkastlpcadruh) AS druh, 0.000 as dan ;
						FROM o_obr2_a INTO CURSOR odecteno READWRITE
				ENDIF
			ENDIF
			USE IN o_obr2_a
			pocet=0
			id_zoz=""
		ENDIF
		SELECT o_dokl1b
		pocet=0
		id_zoz=""
		SCAN FOR c_dokl<m.mincdokl
			id_zoz=m.id_zoz+","+ALLTRIM(STR(o_dokl1b.c_dokl,15,0))
			pocet=m.pocet+1
			IF m.pocet>20
				m.id_zoz=SUBSTR(m.id_zoz,2)
				lsql="SELECT c_dokl, c_stredisk, c_karty, mnozstvi, cena from food600.obraty_a"
				lsql=lsql+" WHERE obraty_a.operace = 'VK' and obraty_a.c_dokl in ("+m.id_zoz+")  AND obraty_a.c_data= ?m.cdata AND obraty_a.c_karty>0"
				sql_select(lsql,'o_obr2_a','c_dokl i,c_stredisk i,c_karty i, mnozstvi Y,cena Y')
				IF RECCOUNT("o_obr2_a")>0
					IF USED("odecteno")
						INSERT INTO odecteno (c_dokl, c_stredisk, c_karty, mnozstvi, cena) SELECT c_dokl, c_stredisk, c_karty, -mnozstvi, cena FROM o_obr2_a
					ELSE
						SELECT c_dokl, c_stredisk, c_karty, - mnozstvi AS mnozstvi, cena, SPACE(30) AS nazev, 00000000 AS doklad, SPACE(19) AS uzaverka, ;
							{} AS datum_dokl, 00000000 AS c_kas, 1 AS c_mena, SPACE(20) AS kdo_kdy, "1 " AS cen_hlad, SPACE(15) AS pz1, SPACE(15) AS pz2, SPACE(13) AS kod, ;
                            0000000000 AS c_druh, SPACE(3) AS spart, SPACE(m.sirkastlpcadruh) AS druh, 0.000 as dan ;
							FROM o_obr2_a INTO CURSOR odecteno READWRITE
					ENDIF
				ENDIF
				USE IN o_obr2_a
				pocet=0
				id_zoz=""
			ENDIF
			SELECT o_dokl1b
		ENDSCAN
		IF m.pocet>0
			m.id_zoz=SUBSTR(m.id_zoz,2)
			lsql="SELECT c_dokl, c_stredisk, c_karty, mnozstvi, cena from food600.obraty_a"
			lsql=lsql+" WHERE obraty_a.operace = 'VK' and obraty_a.c_dokl in ("+m.id_zoz+")  AND obraty_a.c_data= ?m.cdata AND obraty_a.c_karty>0"
			sql_select(lsql,'o_obr2_a','c_dokl i,c_stredisk i,c_karty i, mnozstvi Y,cena Y')
			IF RECCOUNT("o_obr2_a")>0
				IF USED("odecteno")
					INSERT INTO odecteno (c_dokl, c_stredisk, c_karty, mnozstvi, cena) SELECT c_dokl, c_stredisk, c_karty, -mnozstvi, cena FROM o_obr2_a
				ELSE
					SELECT c_dokl, c_stredisk, c_karty, - mnozstvi AS mnozstvi, cena, SPACE(30) AS nazev, 00000000 AS doklad, SPACE(19) AS uzaverka, ;
						{} AS datum_dokl, 00000000 AS c_kas, 1 AS c_mena, SPACE(20) AS kdo_kdy, "1 " AS cen_hlad, SPACE(15) AS pz1, SPACE(15) AS pz2, SPACE(13) AS kod, ;
                        0000000000 AS c_druh, SPACE(3) AS spart, SPACE(m.sirkastlpcadruh) AS druh, 0.000 as dan ;
						FROM o_obr2_a INTO CURSOR odecteno READWRITE
				ENDIF
			ENDIF
			USE IN o_obr2_a
			pocet=0
			id_zoz=""
		ENDIF
*	ELSE
*		CREATE CURSOR odecteno (c_dokl i, c_stredisk i, c_karty i, mnozstvi Y, cena Y, nazev c(30), doklad i, uzaverka c(19), ;
*			datum_dokl d, c_kas i, c_mena i, kdo_kdy c(20), cen_hlad c(2), pz1 c(15), pz2 c(15), kod C(13), c_druh i, spart c(3), druh c(m.sirkastlpcadruh), dan N(5,3))
	ENDIF
	IF !USED("odecteno")
		CREATE CURSOR odecteno (c_dokl i, c_stredisk i, c_karty i, mnozstvi Y, cena Y, nazev c(30), doklad i, uzaverka c(19), ;
			datum_dokl d, c_kas i, c_mena i, kdo_kdy c(20), cen_hlad c(2), pz1 c(15), pz2 c(15), kod C(13), c_druh i, spart c(3), druh c(m.sirkastlpcadruh), dan N(5,3))
	ENDIF 
ELSE
	USE (m.fn_obraty) IN 0 ALIAS obraty
	USE (m.fn_obra_a) IN 0 ALIAS obra_a
	SELECT c_dokl, c_stredisk, c_karty, - mnozstvi AS mnozstvi, cena ;
		WHERE operace="VK" AND c_dokl IN (SELECT c_dokl FROM o_dokl1b) ;
		FROM obra_a INTO CURSOR o_obr2_a

	had.set_had( 66 )
	SELECT c_dokl, c_stredisk, c_karty, - mnozstvi AS mnozstvi, cena, SPACE(30) AS nazev, 00000000 AS doklad, SPACE(19) AS uzaverka, ;
		{} AS datum_dokl, 00000000 AS c_kas, 1 AS c_mena, SPACE(20) AS kdo_kdy, "1 " AS cen_hlad, SPACE(15) AS pz1, SPACE(15) AS pz2, ;
        0000000000 AS c_druh, SPACE(3) AS spart, SPACE(m.sirkastlpcadruh) AS druh, 0.000 as dan, SPACE(13) as kod ;
		WHERE operace="VK" AND c_dokl IN (SELECT c_dokl FROM o_dokl1b) ;
		FROM obraty INTO CURSOR odecteno READWRITE
	INSERT INTO odecteno (c_dokl, c_stredisk, c_karty, mnozstvi, cena) SELECT c_dokl, c_stredisk, c_karty, mnozstvi, cena FROM o_obr2_a
	DO pozavirej WITH "o_obr2_a, obraty, obra_a"
ENDIF

SELECT odecteno
SET RELATION TO c_karty INTO karty, c_dokl INTO o_dokl1b
IF m.issql
    REPLACE ALL c_druh WITH karty.c_druh, nazev WITH karty.nazev, doklad WITH o_dokl1b.doklad, uzaverka WITH o_dokl1b.uzaverka, datum_dokl WITH o_dokl1b.datum_dokl, pz2 WITH o_dokl1b.pz2, ;
	            c_kas WITH o_dokl1b.c_partner, c_mena WITH o_dokl1b.c_mena, kdo_kdy WITH o_dokl1b.kdo_kdy, cen_hlad WITH o_dokl1b.cen_hlad, pz1 WITH o_dokl1b.pz1, kod WITH karty.kod
ELSE
    REPLACE ALL c_druh WITH karty.c_druh, nazev WITH karty.nazev, doklad WITH o_dokl1b.doklad, uzaverka WITH o_dokl1b.uzaverka, datum_dokl WITH o_dokl1b.datum_dokl, pz2 WITH o_dokl1b.pz2, ;
                c_kas WITH o_dokl1b.c_partner, c_mena WITH o_dokl1b.c_mena, kdo_kdy WITH o_dokl1b.kdo_kdy, cen_hlad WITH o_dokl1b.cen_hlad, pz1 WITH o_dokl1b.pz1, kod WITH ""
ENDIF 
REPLACE c_druh WITH karty.c_druhs FOR EMPTY(odecteno.c_druh)
REPLACE c_druh WITH karty.c_druhk FOR EMPTY(odecteno.c_druh)
SET RELATION TO
UPDATE odecteno SET druh=skupiny.druh, spart=skupiny.spart, dan=skupiny.dan01 FROM skupiny WHERE odecteno.c_druh=skupiny.c_druh

*UPDATE odecteno SET cen_hlad=SUBSTR(cen_hlad,2) WHERE LEN(ALLTRIM(cen_hlad))=2 AND LEFT(ALLTRIM(cen_hlad),1)="0"

had.set_had( 77 )
REPLACE ALL cena WITH NTOM(MTON(cena)*en__meny(c_mena,m.girep_mena))
REPLACE FOR EMPTY(nazev) nazev WITH "-- karta " + ALLTRIM(STR(c_karty)) + " --"


*!*****************************************************************************
*!      Procedure: S_UCTY
*!*****************************************************************************
PROCEDURE s_ucty
PRIVATE pom_sel
had.SHOW()
had.set_had( 33 )
SELECT DISTINCT uzaverka, u_cis, cen_hlad     FROM tm_sk_pol INTO CURSOR qq  READWRITE
SELECT DISTINCT uzaverka, u_cis, "     " AS cen_hlad FROM qq INTO CURSOR qqq READWRITE
INDEX ON uzaverka + u_cis TAG uz_u_cis
SELECT qq
SET RELATION TO uzaverka + u_cis INTO qqq
SCAN
	SELECT qq
	REPLACE qqq.cen_hlad WITH ALLTRIM(qqq.cen_hlad) + qq.cen_hlad
ENDSCAN
USE IN qq
SELECT qqq

had.set_had( 44 )

IF m.issql
    rv("users","Users")
ELSE
	IF !USED("users")
	    TRY 
    	    USE (m.gchom_path+"users") IN 0 SHARED AGAIN 
	    CATCH 
    	    =MESSAGEBOX(jaz("form_nedostupny_users"),64,"FoodMan")
	    ENDTRY 
	ENDIF 
ENDIF

IF USED("users")
    SELECT uzaverka, u_cis, id_kas, PADR(id_kas, 30) AS id_zkratka, autor, users.username, SPACE(8) AS u_cas, "     " AS u_open, mena, cena_mena, tip, druh_pl, cena_pl, ;
        SPACE(16) AS text_sleva, $0 AS sleva_abs, $0 AS cena_slev, 000000 AS pocet_h, 0000.0 as stul, "     " AS cen_hlad, 00.00 AS round50, poradi, TEXT, SPACE(14) as uzcis, SPACE(44) as jmeno ;
        FROM tm_sk_plt LEFT JOIN USERS ON USERS.user_id = tm_sk_plt.autor INTO CURSOR platby2 READWRITE
    REPLACE FOR ISNULL(username) username WITH "## " + AUTOR + " ##"
ELSE
    SELECT uzaverka, u_cis, id_kas, PADR(id_kas, 30) AS id_zkratka, autor, autor as username, SPACE(8) AS u_cas, "     " AS u_open, mena, cena_mena, tip, druh_pl, cena_pl, ;
        SPACE(16) AS text_sleva, $0 AS sleva_abs, $0 AS cena_slev, 000000 AS pocet_h, 0000.0 as stul, "     " AS cen_hlad, 00.00 AS round50, poradi, TEXT, SPACE(14) as uzcis, SPACE(44) as jmeno ;
        FROM tm_sk_plt INTO CURSOR platby2 READWRITE
ENDIF 

SET RELATION TO uzaverka + u_cis INTO qqq
REPLACE ALL cen_hlad WITH qqq.cen_hlad
REPLACE FOR !EMPTY(text_sleva) text_sleva WITH jaz2(text_sleva)

SET ORDER TO uz_kas IN tm_sk_sez_kas
SET RELATION TO uzaverka + id_kas INTO tm_sk_sez_kas
REPLACE ALL id_zkratka WITH tm_sk_sez_kas.zkr_kas

SET RELATION TO uzaverka + u_cis + STR(poradi,2) INTO tm_sk_txt
REPLACE ALL              jmeno WITH tm_sk_txt.jmeno
REPLACE FOR EMPTY(jmeno) jmeno WITH tm_sk_txt.hjmeno

SET RELATION TO
INDEX ON uzaverka + u_cis TAG uz_u_cis

had.set_had(66)
had.set_txt2("scan")
SELECT tm_sk_fin
SCAN
	SCATTER FIELDS uzaverka, u_cis, cena_celk, sleva_abs, u_cas, u_minut, text_sleva, pocet_h, stul, uzcis, round50 MEMVAR
*       u_open = TRANS(m.u_minut, "@Z 99999")
	u_open = IIF(m.u_minut>1, TRANSFORM(INT(m.u_minut/60)) + ":" + TRANSFORM(MOD(m.u_minut, 60), "@L 99"), "")

	cena_slev = m.cena_celk - m.sleva_abs
	SELECT platby2
	LOCATE FOR uzaverka + u_cis = m.uzaverka + m.u_cis
	IF FOUND()
		GATHER FIELDS cena_slev, sleva_abs, u_cas, u_open, text_sleva, pocet_h, stul, uzcis, round50 MEMVAR
	ELSE
		WAIT jaz("form_nenajdene_cislo_uctu")+ " " + m.u_cis WINDOW AT 22,22 NOWAIT
	ENDIF
ENDSCAN
WAIT CLEAR
had.set_had(77)

SELECT platby2
had.HIDE()

IF "1" $ m.dily
    INDEX ON id_kas + uzaverka + u_cis + STR(poradi,1) TAG kuup
	DO pr_report WITH IIF(m.slovenskaverzia, "trz_s_ucty1", "trz_ucty1"), !m.reporty_volane_z_formu
ENDIF

IF "2" $ m.dily
    INDEX ON id_kas + druh_pl + STR(poradi,1)  + u_cis TAG kupu
	DO pr_report WITH IIF(m.slovenskaverzia, "trz_s_ucty1@2", "trz_ucty1@2"), !m.reporty_volane_z_formu
ENDIF

IF "3" $ m.dily
	DO pozavirej WITH "u_plt"
	SELECT uzaverka, id_kas, u_cis, "zzz " + ALLTRIM(text_sleva) + " " + i_drupla(druh_pl) AS nazev, cena_pl, text, jmeno ;
		FROM platby2 INTO CURSOR u_plt READWRITE

	had.set_had( 88 )

	SELECT id_kas, uzaverka, u_cis, c_karty, SPACE(30) AS nazev, mnozstvi, prachy, prachy_puv, cen_hlad, 100*(dan_sazba-1) AS dph, ;
		00.00 AS round50, $0 AS cena_pl, SPACE(25) AS text, SPACE(10) AS u_cas, 000000 as pocet_h, SPACE(3) as spart, SPACE(9) as uzcis, "   " as autor, $0 as sleva_abs, SPACE(44) as jmeno ;
		WHERE ! EMPTY(k_odpoctu) FROM tm_sk_pol INTO CURSOR pol2 READWRITE
	REPLACE ALL sleva_abs WITH prachy_puv-prachy
	REPLACE FOR .NOT. (ALLTRIM(cen_hlad) $ m.cen_hladiny) c_karty WITH -99

   	SELECT id_kas, uzaverka, u_cis, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas, uzaverka, u_cis INTO CURSOR roundy
   	UPDATE pol2 SET round50=roundy.round50 from roundy WHERE pol2.id_kas=roundy.id_kas AND pol2.uzaverka=roundy.uzaverka AND pol2.u_cis=roundy.u_cis
   	LOCAL m.idxx, m.uzxx, m.ucxx
    STORE "/\" TO m.idxx, m.uzxx, m.ucxx
   	
   	SELECT pol2
   	SCAN ALL 
   		IF m.idxx=pol2.id_kas AND m.uzxx=pol2.uzaverka AND m.ucxx=pol2.u_cis
   			REPLACE round50 WITH 0
   		ELSE
   			idxx=pol2.id_kas
   			uzxx=pol2.uzaverka
   			ucxx=pol2.u_cis
   		ENDIF 
   	ENDSCAN
   	GO TOP 
   	RELEASE m.idxx, m.uzxx, m.ucxx
	USE IN roundy
	SELECT pol2

	DO dopln_nazev
	had.set_had( 96 )
	SELECT pol2
	INSERT INTO pol2 (uzaverka, id_kas, u_cis, nazev, cena_pl, TEXT, jmeno) SELECT uzaverka, id_kas, u_cis, nazev, cena_pl, TEXT, jmeno FROM u_plt
	INDEX ON id_kas + uzaverka + u_cis TAG k_uz_uc
	had.HIDE()

	IF m.slovenskaverzia
		SELECT pol2
		DO pr_report WITH "trz_s_ucty2", !m.reporty_volane_z_formu
	ELSE
		SET FILTER TO          IN tm_sk_fin
        SET ORDER to uzucetkas IN tm_sk_fin

		SET FILTER TO         IN platby2
		SET ORDER TO uz_u_cis IN platby2

		SELECT pol2
        SET RELATION TO uzaverka + u_cis + id_kas INTO tm_sk_fin
		REPLACE ALL u_cas WITH tm_sk_fin.u_cas, pocet_h WITH tm_sk_fin.pocet_h, autor WITH tm_sk_fin.autor

		SET RELATION TO uzaverka + u_cis INTO platby2
        REPLACE ALL uzcis WITH platby2.uzcis, autor WITH platby2.autor
*        REPLACE FOR EMPTY(autor) autor WITH platby2.autor

		DO pr_report WITH "trz_ucty2", !m.reporty_volane_z_formu
	ENDIF
ENDIF
DO pozavirej WITH "platby1, platby2, u_plt, u_fin, pol2, qq, qqq"


*!*****************************************************************************
*!      Procedure: s_1karta_po_dnech
*!*****************************************************************************
PROCEDURE s_1karta_po_dnech
SELECT PADR(id_kas,30) AS zkr_kas, id_kas, uzaverka, c_karty, cen_hlad, SPACE(30) AS nazev, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, ;
	"  " AS c_hl1, 000000.0 AS mnozstvi1, 000000000.00 AS prachy1, ;
	"  " AS c_hl2, 000000.0 AS mnozstvi2, 000000000.00 AS prachy2, ;
	"  " AS c_hl3, 000000.0 AS mnozstvi3, 000000000.00 AS prachy3, ;
	"  " AS c_hl4, 000000.0 AS mnozstvi4, 000000000.00 AS prachy4, ;
	"  " AS c_hl5, 000000.0 AS mnozstvi5, 000000000.00 AS prachy5, ;
	"  " AS c_hl6, 000000.0 AS mnozstvi6, 000000000.00 AS prachy6, ;
	"  " AS c_hl7, 000000.0 AS mnozstvi7, 000000000.00 AS prachy7, ;
	"  " AS c_hl8, 000000.0 AS mnozstvi8, 000000000.00 AS prachy8, ;
	"  " AS c_hl9, 000000.0 AS mnozstvi9, 000000000.00 AS prachy9  ;
	GROUP BY 1,2,3,4,5 FROM tm_sk_pol INTO CURSOR uzav_karty READWRITE

DO dopln_nazev
SELECT id_kas, cen_hlad, 1 AS poradi, SUM(prachy) AS prachy GROUP BY 1,2 FROM uzav_karty INTO CURSOR cen_hladiny READWRITE
REPLACE FOR prachy < 0 prachy WITH 0
*INDEX ON id_kas + STR(prachy, 12) TAG kkk DESCENDING
INDEX ON id_kas + cen_hlad TAG kkk
o_kas = "$#"
poradi = 1
SCAN
	SCATTER FIELDS id_kas, cen_hlad MEMVAR
	IF id_kas = m.o_kas
		poradi = MIN(m.poradi + 1, 9)    && je 9 sloupcu pro cenove hladiny
		GATHER FIELDS poradi MEMVAR
	ELSE
		o_kas = m.id_kas
		poradi = 1
	ENDIF
	SELECT uzav_karty
	IF m.poradi < 9    && je 9 sloupcu pro cenove hladiny
		policko = "c_hl" + STR(m.poradi, 1)
		REPLACE (policko) WITH m.cen_hlad FOR id_kas = m.id_kas
	ELSE
		REPLACE c_hl9 WITH ALLTRIM(c_hl9) + m.cen_hlad FOR id_kas = m.id_kas   && je 9 sloupcu pro cenove hladiny
	ENDIF
	SELECT cen_hladiny
ENDSCAN

INDEX ON id_kas + cen_hlad TAG kas_chl

SELECT uzav_karty
SET RELATION TO id_kas + cen_hlad INTO cen_hladiny
SCAN
	policko = "mnozstvi" + STR(cen_hladiny.poradi, 1)
	REPLACE (policko) WITH mnozstvi
	policko = "prachy" + STR(cen_hladiny.poradi, 1)
	REPLACE (policko) WITH prachy
ENDSCAN

SELECT zkr_kas, id_kas, uzaverka, c_karty, nazev, c_hl1, c_hl2, c_hl3, c_hl4, c_hl5, c_hl6, c_hl7, c_hl8, c_hl9, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, ;
	SUM(mnozstvi1) AS mnozstvi1, SUM(prachy1) AS prachy1, SUM(mnozstvi2) AS mnozstvi2, SUM(prachy2) AS prachy2, SUM(mnozstvi3) AS mnozstvi3, SUM(prachy3) AS prachy3, ;
	SUM(mnozstvi4) AS mnozstvi4, SUM(prachy4) AS prachy4, SUM(mnozstvi5) AS mnozstvi5, SUM(prachy5) AS prachy5, SUM(mnozstvi6) AS mnozstvi6, SUM(prachy6) AS prachy6, ;
	SUM(mnozstvi7) AS mnozstvi7, SUM(prachy7) AS prachy7, SUM(mnozstvi8) AS mnozstvi8, SUM(prachy8) AS prachy8, SUM(mnozstvi9) AS mnozstvi9, SUM(prachy9) AS prachy9  ;
	GROUP BY 1,2,3,4,5 FROM uzav_karty INTO CURSOR uzav_karty2 READWRITE

SELECT DISTINCT nazev FROM uzav_karty2 INTO CURSOR nazev_menu
DO WHILE .T.
	m.nazev=""
	IF m.reporty_volane_z_formu
		DO FORM dajdruh WITH "","nazev_menu","nazev",250 TO m.nazev
	ENDIF 
	IF EMPTY(m.nazev)
		EXIT
	ELSE
		SELECT uzav_karty2
		SET FILTER TO nazev = m.nazev
		DO pr_report WITH "trz_prod_1karta", !m.reporty_volane_z_formu
		SELECT uzav_karty2
		SET FILTER TO
	ENDIF
ENDDO
DO pozavirej WITH "uzav_karty2, uzav_karty, cen_hladiny, nazev_menu"

*!*****************************************************************************
*!      Procedure: MARK_CIS
*!*****************************************************************************
PROCEDURE mark_cis
PRIVATE m.cisnik2
IF m.issql
	rv("users","Users")
ELSE
	TRY 
		USE (m.gchom_path+"users") IN 0
	CATCH 
		=MESSAGEBOX(jaz("form_nedostupny_users"),64,"FoodMan")
	ENDTRY 
ENDIF
IF !USED("users")
	RETURN
ENDIF 
txt_hlavicka = ""
SELECT uzaverka, id_kas, u_cis, AUTOR, SUM(u_minut) AS doba_uctu ;
	GROUP BY 1, 2, 3 FROM tm_sk_fin INTO CURSOR sl_cis READWRITE
INDEX ON uzaverka + id_kas + u_cis TAG uz_ucet

SELECT sl_cis.AUTOR, USERS.username, COUNT(AUTOR) AS poc_uctu GROUP BY 1 ;
	FROM sl_cis LEFT JOIN USERS ON USERS.user_id = sl_cis.AUTOR INTO CURSOR cisnici READWRITE
REPLACE FOR ISNULL(username) username WITH "## " + AUTOR + " ##"
INSERT INTO cisnici (AUTOR, username) VALUE ("%%%", "-- vsichni - soucty --")
INDEX ON AUTOR TAG AUTOR
*   *   *   *   *   *   *   *   *   *   *   *

SELECT uzaverka, id_kas, u_cis, SUM(tip) as tip, "       --- TIP ---" AS nazev ;
       GROUP BY 1,2,3 FROM tm_sk_plt INTO CURSOR tipy READWRITE
DELETE FOR EMPTY(tip)

had.set_had( 44 )
SELECT uzaverka, id_kas, u_cis, SPACE(30) AS nazev, c_karty, mnozstvi, prachy_puv AS prachy, prachy AS prachy_po, $0 AS tip, SPACE(m.sirkastlpcadruh) as druh, SPACE(3) as autor ;
       WHERE .NOT. EMPTY(k_odpoctu) FROM tm_sk_pol INTO CURSOR polozky READWRITE

DO dopln_nazev
had.set_had( 66 )
INSERT INTO polozky (uzaverka, id_kas, u_cis, tip, nazev) SELECT uzaverka, id_kas, u_cis, tip, nazev FROM tipy
SET RELATION TO uzaverka + id_kas + u_cis INTO sl_cis
REPLACE ALL AUTOR WITH sl_cis.AUTOR
SET RELATION TO

IF m.issql
	PRIVATE m.hladajhladinu
	m.hladajhladinu=39
	rv("predceny","predceny")
	SELECT polozky.AUTOR, polozky.druh, polozky.nazev, polozky.c_karty, SUM(polozky.mnozstvi) AS mnozstvi, SUM(polozky.prachy) AS prachy, SUM(polozky.prachy_po) AS prachy_po, SUM(polozky.tip) AS tip, SPACE(15) AS username, predceny.cena AS cena_body, 00.00 as round50 ;
		GROUP BY 1,2,3,4 FROM polozky LEFT JOIN predceny ON polozky.c_karty = predceny.c_karty INTO CURSOR ppp2 READWRITE
	USE IN predceny
ELSE
	SELECT polozky.AUTOR, polozky.druh, polozky.nazev, polozky.c_karty, SUM(polozky.mnozstvi) AS mnozstvi, SUM(polozky.prachy) AS prachy, SUM(polozky.prachy_po) AS prachy_po, SUM(polozky.tip) AS tip, SPACE(15) AS username, karty.cena39 AS cena_body, 00.00 as round50 ;
		GROUP BY 1,2,3,4 FROM polozky LEFT JOIN karty ON karty.c_karty = polozky.c_karty INTO CURSOR ppp2 READWRITE
ENDIF
SET RELATION TO AUTOR INTO cisnici
REPLACE ALL username WITH cisnici.username
SET RELATION TO
SELECT autor, SUM(round50) as round50 FROM tm_sk_fin GROUP BY autor INTO CURSOR roundy
IF USED("roundy")
   	UPDATE ppp2 SET round50=roundy.round50 from roundy WHERE ppp2.autor=roundy.autor
   	LOCAL m.idxx
   	m.idxx="/\"
   	SELECT ppp2
   	SCAN ALL 
   		IF m.idxx=ppp2.autor
   			REPLACE round50 WITH 0
   		ELSE
   			m.idxx=ppp2.autor
   		ENDIF 
   	ENDSCAN
   	GO TOP 
   	RELEASE m.idxx
	USE IN roundy
	SELECT ppp2
ENDIF 


IF ATC( "BAKARE", m.wi_zakaz) + ATC("D_VIKTORIA", m.gchom_path) > 0
    REPLACE ALL tip WITH IIF(ISNULL(cena_body), 0, cena_body * mnozstvi)
ENDIF

IF "1" $ m.dily
	SELECT cisnici
	SET CONFIRM ON
	DO WHILE .T.
		m.cisnik=""
		IF m.reporty_volane_z_formu
			DO FORM dajdruh WITH "","cisnici","autor",80, "username",200 TO m.cisnik
		ENDIF 
		IF EMPTY(m.cisnik)
			EXIT
		ELSE
			SELECT cisnici
			LOCATE FOR AUTOR=m.cisnik
			m.cisnik2=cisnici.username
			SELECT ppp2
			IF m.cisnik = "%%%"
				SET FILTER TO
			ELSE
				SET FILTER TO AUTOR = m.cisnik
			ENDIF
			DO pr_report WITH "trz_cisnici", !m.reporty_volane_z_formu
			SELECT cisnici
		ENDIF
	ENDDO
ENDIF

IF "2" $ m.dily
	cisnik2="Spolu"
	SELE ppp2
	SET FILTER TO
	gctxt_summary = "SUMMARY"
	DO pr_report WITH "trz_cisnici@2", !m.reporty_volane_z_formu
	gctxt_summary = ""
ENDIF

    IF m.issql
        SELECT id_kas, uzaverka, left(dic, 3) as autor, padr("## " + left(dic, 3) + " ##", 30) as username, ;
               val(hpokoj) as stul, hpref as cas, val(cislo) as prachy, schvalil ;
               WHERE ucet = 0 .AND. .not. EMPTY(dic) AND ico LIKE "SPOKO" ;
               ORDER BY autor, uzaverka, cas ;
               FROM tm_sk_txt INTO CURSOR spokojenost READWRITE 
    ELSE
        SELECT id_kas, uzaverka, left(dic, 3) as autor, padr("## " + left(dic, 3) + " ##", 30) as username, ;
               val(hpokoj) as stul, hpref as cas, val(cislo) as prachy, schvalil ;
               WHERE u_cis LIKE "__SPOKO" ;
               ORDER BY autor, uzaverka, cas ;
               FROM (m.fn_sk_o_txt) INTO CURSOR spokojenost READWRITE 
    ENDIF

	IF !prazdna()
		SET RELATION TO AUTOR INTO cisnici
		REPLACE FOR .NOT. EMPTY(cisnici.username) username WITH cisnici.username
		DO pr_report WITH "trz_spokojenost", !m.reporty_volane_z_formu

		***********************
		SELECT DISTI STR(stul,9,1) AS stul_txt FROM spokojenost INTO CURSOR stoly
		stul=""
		IF m.reporty_volane_z_formu
			DEFINE POPUP stoly PROMPT FIELD stul_txt FROM 1,33
			ON SELECTION POPUP stoly DEACTI POPUP
			ACTIVATE SCREEN
			ACTIVATE POPUP stoly
			stul = PROMPT()
		ENDIF 
		DO WHILE .NOT. EMPTY(m.stul)
			SELECT spokojenost
			SET FILTER TO stul = VAL(m.stul)
			DO pr_report WITH "trz_spokojenost@2", !m.reporty_volane_z_formu
			SELECT stoly
			ACTIVATE SCREEN
			ACTIVATE POPUP stoly
			stul = PROMPT()
		ENDDO
		RELEASE POPUPS stoly
		***********************

		SELECT AUTOR AS uzaverka, username AS schvalil, SUM(prachy) AS prachy, ;
			" " AS AUTOR, " " AS id_kas, 0 AS stul, " " AS username, " " AS cas ;
			GROUP BY AUTOR ;
			FROM spokojenost INTO CURSOR spokojenost2 READWRITE 
		DO pr_report WITH "trz_spokojenost@3", !m.reporty_volane_z_formu
		USE IN spokojenost2
		SELECT uzaverka, " " AS schvalil, SUM(prachy) AS prachy, ;
			" " AS AUTOR, " " AS id_kas, 0 AS stul, " " AS username, " " AS cas ;
			GROUP BY uzaverka ;
			FROM spokojenost INTO CURSOR spokojenost2 READWRITE 
		DO pr_report WITH "trz_spokojenost@4", !m.reporty_volane_z_formu
	ENDIF
	DO pozavirej WITH "spokojenost, spokojenost2"

IF "3" $ m.dily
	SELECT uzaverka, u_cis, SUM(prachy - dan) AS vynos, SPACE(3) AS AUTOR, 000000.00 AS doba_uctu ;
		GROUP BY 1,2 FROM tm_sk_pol INTO CURSOR cis_xx1 READWRITE
	SET RELATION TO uzaverka + u_cis INTO sl_cis
	REPLACE ALL AUTOR WITH sl_cis.AUTOR, doba_uctu WITH sl_cis.doba_uctu

	SELECT DISTINCT AUTOR, uzaverka FROM cis_xx1 INTO CURSOR cis_uzaverky1 READWRITE
	SELECT AUTOR, CNT(AUTOR) AS cnt_uzaver GROUP BY 1 FROM cis_uzaverky1 INTO CURSOR cis_uzaverky READWRITE
	INDEX ON AUTOR TAG AUTOR

	SELECT AUTOR, PADR("## " + AUTOR + " ##", 30) AS username, SUM(doba_uctu) AS doba_uctu, "00:00:00" AS du_txt, COUNT(AUTOR) AS mnozstvi, SUM(vynos) AS vynos, ;
		000000 AS cnt_uzaver ;
		GROUP BY 1 FROM cis_xx1 INTO CURSOR cis_xx READWRITE
	SET RELATION TO AUTOR INTO cisnici
	REPLACE FOR .NOT. EMPTY(cisnici.username) username WITH cisnici.username
	SET RELATION TO AUTOR INTO cis_uzaverky
	REPLACE ALL cnt_uzaver WITH cis_uzaverky.cnt_uzaver
	SET RELATION TO

	REPLACE ALL doba_uctu WITH ROUND(doba_uctu / mnozstvi, 2), du_txt WITH TRANSFORM(INT(doba_uctu/60), "@l 99") + ":"
	REPLACE ALL doba_uctu WITH MOD(doba_uctu, 60), du_txt WITH ALLTRIM(du_txt) + TRANSFORM(INT(doba_uctu), "@l 99") + ":"
	REPLACE ALL doba_uctu WITH ROUND(MOD(doba_uctu*60, 60), 0), du_txt WITH ALLTRIM(du_txt) + TRANSFORM(INT(doba_uctu), "@l 99")
	REPLACE FOR LEFT(du_txt, 4) = "00:0" du_txt WITH "    " + SUBSTR(du_txt, 5)
	REPLACE FOR LEFT(du_txt, 3) = "00:"  du_txt WITH "   "  + SUBSTR(du_txt, 4)

	INDEX ON username TAG username
	DO pr_report WITH "trz_cisnici2", !m.reporty_volane_z_formu
ENDIF

IF "4" $ m.dily .and. !m.issql .and. FILE(m.gccen_path + "zurobj.dbf") && body v bakare i jinde

    SELECT autor, zurobj.c_karty, zurobj.nazev, SUM(kolik) as mnozstvi, SUM(prachy) as prachy, $0 AS prachy_po, cisnik as username, karty.druh_kalk as druh, SUM(kolik * karty.cena39) AS tip ;
           GROUP BY 1,2 ;
           WHERE uzaverka + id_kas IN (SELECT uzaverka + id_kas FROM uzaverky WHERE ukazat .and. lbl) ;
           FROM (m.gccen_path + "zurobj.dbf") LEFT JOIN karty ON karty.c_karty = zurobj.c_karty INTO CURSOR bonusycisniku READWRITE 
    REPLACE FOR ISNULL(druh) druh WITH "__________"
    REPLACE FOR  EMPTY(druh) druh WITH "__________"

    SELECT disti druh, .t. as lbl FROM bonusycisniku INTO CURSOR druhy_z_bonusu READWRITE
    INDEX on druh TAG druh
	IF m.reporty_volane_z_formu
	    DO FORM seldruhy WITH "druhy_z_bonusu","druh", 155 TO m.druhx
	ENDIF 
    SELECT bonusycisniku 
    SET RELATION TO druh INTO druhy_z_bonusu
    SET FILTER TO druhy_z_bonusu.lbl = .T.

    INDEX on autor + druh + nazev TAG a_d_n
    m.cisnik2=""
    txt_hlavicka = " z OBJEDNAVEK"
    DO pr_report WITH "trz_cisnici", !m.reporty_volane_z_formu
ENDIF 

DO pozavirej WITH "cisnici, polozky, users, spokojenost, spokojenost2, cis_xx, tipy, cis_xx1, cis_uzaverky, cis_uzaverky1, ppp2, sl_cis"


*!*****************************************************************************
*!      Procedure: SLEVY
*!*****************************************************************************
PROCEDURE slevy
had.SHOW()
IF m.slovenskaverzia
	SELECT c_kas, uzaverka, u_cis, SUM(trzba1+trzba2+trzba3+trzba4) AS cena_slev, SUM(cena_celk) AS cena_celk, $0 AS sleva_abs, SUM(dan1+dan2+dan3+dan4) AS dz, ;
		cas_atrib, id_kas, PADR(id_kas, 30) as zkr_kas, AUTOR, u_minut, u_cas, pocet_h, stul, round50, ;
		$0 AS koef_slev, SPACE(7) AS platba, text_sleva, text_link ;
		GROUP BY uzaverka, u_cis FROM tm_sk_fin INTO CURSOR slevy READWRITE
ELSE
	SELECT c_kas, uzaverka, u_cis, SUM(trzba1+trzba2+trzba3+trzba4) AS cena_slev, SUM(cena_celk) AS cena_celk, $0 AS sleva_abs, SUM(dan1+dan2+dan3+dan4) AS dz, ;
		cas_atrib, id_kas, PADR(id_kas, 30) as zkr_kas, AUTOR, u_minut, u_cas, pocet_h, stul, round50, ;
		$0 AS koef_slev, SPACE(7) AS platba, text_sleva, text_link ;
		WHERE  INLIST(LEFT(text_sleva, 1), "#", "^", "@") ;
		GROUP BY uzaverka, u_cis FROM tm_sk_fin INTO CURSOR slevy READWRITE
ENDIF
REPLACE ALL sleva_abs WITH cena_celk - cena_slev, dz WITH cena_slev - dz

SET ORDER TO uz_kas IN tm_sk_sez_kas
SET RELATION TO uzaverka + id_kas INTO tm_sk_sez_kas
REPLACE ALL zkr_kas WITH tm_sk_sez_kas.zkr_kas
SET RELATION TO 

IF m.slovenskaverzia
	IF m.hotel_krajina="CZE"
		DELETE FOR ABS(sleva_abs) <= 0.5
	ELSE 
		DELETE FOR ABS(sleva_abs) <= 0.01
	ENDIF 
ELSE
	DELETE FOR ABS(sleva_abs) <= 0.5
ENDIF

SELECT uzaverka, u_cis, druh_pl, ABS(cena_pl) AS cena_pl, TEXT FROM tm_sk_plt INTO CURSOR sl_platby READWRITE
INDEX ON uzaverka + u_cis + STR(cena_pl) DESCENDING TAG uup
oooo = "&^$(*&"
SCAN        && tohle najde u viceplatboveho uctu radek s nejvyssi castkou, ostatni zrusi
	IF uzaverka + u_cis = m.oooo
		REPLACE u_cis WITH "SMAZAT"
	ENDIF
	m.oooo = uzaverka + u_cis
ENDSCAN
INDEX ON uzaverka + u_cis TAG uu

had.set_had( 33 )
SELECT slevy
SET RELATION TO uzaverka + u_cis INTO sl_platby
REPLACE ALL platba WITH sl_platby.druh_pl
USE IN sl_platby

had.set_had( 88 )
REPL ALL koef_slev WITH IIF(cena_celk=0, 100, 100 * sleva_abs / cena_celk)

** aby se zvlast subtotalovaly slevy a zvlast prirazky
REPLACE FOR sleva_abs * cena_celk < 0 .AND. LEFT(text_sleva, 1) # "@" uzaverka WITH "-" + SUBSTR(uzaverka, 2)

INDEX ON id_kas + uzaverka + u_cis TAG kuu

had.HIDE()
IF "1" $ m.dily
	DO pr_report WITH "trz_slevy", !m.reporty_volane_z_formu
ENDIF

IF "2" $ m.dily .AND. ! prazdna()
	IF m.slovenskaverzia
		SELECT id_kas, uzaverka, zkr_kas, ;
			" " AS u_cis, " " AS AUTOR, " " AS text_sleva, 0 AS stul, " " AS u_cas, " " AS text_link, ;
			SUM(cena_celk) AS cena_celk, SUM(cena_slev+round50) AS cena_slev, SUM(sleva_abs) AS sleva_abs, SUM(dz) AS dz, $0 AS koef_slev, " " AS platba ;
			GROUP BY id_kas, uzaverka ;
			FROM slevy INTO CURSOR slevy2 READWRITE
	ELSE
		SELECT id_kas, uzaverka, zkr_kas, ;
			" " AS u_cis, " " AS AUTOR, " " AS text_sleva, 0 AS stul, " " AS u_cas, " " AS text_link, ;
			SUM(cena_celk) AS cena_celk, SUM(cena_slev) AS cena_slev, SUM(sleva_abs) AS sleva_abs, SUM(dz) AS dz, $0 AS koef_slev, " " AS platba ;
			GROUP BY id_kas, uzaverka ;
			FROM slevy INTO CURSOR slevy2 READWRITE
	ENDIF
	DO pr_report WITH "trz_slevy@2", !m.reporty_volane_z_formu
ENDIF

IF m.slovenskaverzia
	SELECT id_kas, text_sleva AS uzaverka, u_cis, AUTOR, " " AS text_sleva, text_link, u_cas, cena_celk, cena_slev, sleva_abs, koef_slev, dz, round50, platba, stul, zkr_kas ;
		WHERE LEFT(text_sleva, 1) = "#" ;
		FROM slevy ORDER BY 1,2,3 INTO CURSOR slevy_s READWRITE
ELSE
	SELECT id_kas, text_sleva AS uzaverka, u_cis, AUTOR, " " AS text_sleva, text_link, u_cas, cena_celk, cena_slev, sleva_abs, koef_slev, dz, round50, platba, stul, zkr_kas ;
		WHERE LEFT(text_sleva, 1) = "#" ;
		FROM slevy ORDER BY 1,2,3 INTO CURSOR slevy_s READWRITE
ENDIF
REPLACE ALL text_link WITH STRTRAN(text_link, "   ", " ")
REPLACE ALL text_link WITH STRTRAN(text_link, "  ", " ")
REPLACE FOR .NOT. EMPTY(text_link) uzaverka WITH text_link
INDEX ON id_kas + uzaverka + u_cis TAG kuu
o_gctxt_summary = gctxt_summary
gctxt_summary = "summary"
DO pr_report WITH "trz_slevy@3", !m.reporty_volane_z_formu
gctxt_summary = o_gctxt_summary

SELECT slevy
SET FILTER TO text_sleva = "#8_CLUB        " .AND. .NOT. ("=" $ text_link) .AND. ABS(sleva_abs) >= 0.5
IF ! prazdna()
	DO pr_report WITH "trz_slevy@8", !m.reporty_volane_z_formu
ENDIF

IF ATC("international", m.sqldatabaza) > 0
    
    SELECT tm_sk_sez
    INDEX on uzaverka + id_kas TO (m.gcpri_path + "uz_kass")


    SELECT uzaverka, {^1111-11-11} as uct_den, id_kas, u_cis, zkr_kas, cena_celk, sleva_abs, text_sleva, text_link, 1 as pocet_slev, "0" as sloupec, ;
           00000000.00 AS sleva_1, 00000000.00 AS sleva_2, 00000000.00 AS sleva_3, 00000000.00 AS sleva_4 ;
           FROM slevy INTO TABLE (m.gcpri_path + "slevy_intr")
    DELETE FOR sleva_abs = 0
    
    SET RELATION TO uzaverka + id_kas INTO tm_sk_sez
    REPLACE ALL uct_den WITH tm_sk_sez.uct_den
    SET RELATION TO 
    
    REPLACE FOR LEFT(text_sleva,3) = "#7_" text_link WITH "#7_" + text_link

*    dohledani slevy puvodniho uctu u zmeny platby
    DO WHILE .t.
        LOCATE FOR LEFT(text_sleva,1) = "^" .and. EMPTY(text_link)
        IF FOUND()
            kasa = SUBSTR(text_sleva, 2, 2)
            ucet = SUBSTR(text_sleva, 4, 5)
            ? m.kasa, m.ucet
            radek = RECNO()
            REPLACE text_link WITH "   xxxxx"
            LOCATE FOR u_cis = m.ucet .and. id_kas = m.kasa
            IF FOUND()
                SCATTER FIELDS text_link MEMVAR 
                GO (m.radek)
                GATHER FIELDS text_link MEMVAR 
            ENDIF 
        ELSE
            EXIT 
        ENDIF 
    ENDDO 

    REPLACE FOR sloupec = "0" .and. LEFT(text_link,3)  = "#7_" sloupec WITH "1"
    REPLACE FOR sloupec = "0" .and. LEFT(text_sleva,1) = "@"   sloupec WITH "4"
    REPLACE FOR sloupec = "0" .and. LEFT(text_link,3)  = "1 _" sloupec WITH "2"
    REPLACE FOR sloupec = "0" .and. LEFT(text_link,3)  = "2 _" sloupec WITH "2"
    REPLACE FOR sloupec = "0" .and. LEFT(text_link,3)  = "3 _" sloupec WITH "2"
    REPLACE FOR sloupec = "0" .and. LEFT(text_link,3)  = "4 _" sloupec WITH "2"
    REPLACE FOR sloupec = "0" .and. LEFT(text_link,3)  = "5 _" sloupec WITH "3"
    REPLACE FOR sloupec = "0" .and. LEFT(text_link,3)  = "6 _" sloupec WITH "3"
    REPLACE FOR sloupec = "0" .and. LEFT(text_link,3)  = "7 _" sloupec WITH "1"
    REPLACE FOR sloupec = "0" .and. LEFT(text_link,3)  = "8 _" sloupec WITH "3"
    REPLACE FOR sloupec = "0" .and. LEFT(text_link,3)  = "9 _" sloupec WITH "3"
    
    REPLACE FOR sloupec = "0"                                  sloupec WITH "1"

    REPLACE FOR sloupec = "1" sleva_1 WITH sleva_abs 
    REPLACE FOR sloupec = "2" sleva_2 WITH sleva_abs 
    REPLACE FOR sloupec = "3" sleva_3 WITH sleva_abs 
    REPLACE FOR sloupec = "4" sleva_4 WITH sleva_abs 

    SELECT id_kas, uct_den, zkr_kas, SUM(cena_celk) as cena_celk, SUM(sleva_abs) as sleva_abs, SUM(pocet_slev) as pocet_slev, ;
           SUM(sleva_1) AS sleva_1, SUM(sleva_2) AS sleva_2, SUM(sleva_3)  AS sleva_3, SUM(sleva_4) AS sleva_4 ;
           GROUP BY 1, 2 FROM slevy_intr INTO TABLE (m.gcpri_path + "slevy_intr_sum")

    SELECT disti id_kas FROM slevy_intr_sum INTO CURSOR kasy_slev
    SCAN
        SCATTER MEMVAR 
        SELECT slevy_intr_sum
        dat_slevy = m.uctden1
        DO WHILE m.dat_slevy <= m.uctden2
            LOCATE FOR id_kas = m.id_kas .and. uct_den = m.dat_slevy
            IF .not. FOUND()
                INSERT INTO (m.gcpri_path + "slevy_intr_sum") (id_kas, uct_den) VALUES (m.id_kas, m.dat_slevy)
            ENDIF 
            dat_slevy = m.dat_slevy + 1
        ENDDO 
        
    ENDSCAN  

    SELECT slevy_intr_sum
    INDEX on id_kas + TTOC(uct_den,3) TAG kas_dat
    DO pr_report WITH "trz_slevy_intr", !m.reporty_volane_z_formu
    
ENDIF 

DO pozavirej WITH "slevy, slevy2, sl_platby, slevy_intr, slevy_intr_sum"


*!*****************************************************************************
*!      Procedure: po_stolech
*!*****************************************************************************
PROCEDURE po_stolech

#DEFINE NAZEV_POCUCTU "_______       pocet uctu:"

SELECT stul, COUNT(stul) AS pocet_uctu, NAZEV_POCUCTU AS nazev, SPACE(50) as menostola, SUM(round50) as round50  ;
	GROUP BY stul FROM tm_sk_fin INTO CURSOR pocet_uctu READWRITE

SELECT stul, COUNT(stul) AS pocet_uctu, NAZEV_POCUCTU AS nazev, SPACE(50) as menostola, uzaverka, SUM(round50) as round50 ;
	GROUP BY stul, uzaverka FROM tm_sk_fin INTO CURSOR pocet_uctu_u READWRITE

SELECT uzaverka, u_cis, id_kas, id_man, c_karty, SPACE(30) nazev, SPACE(m.sirkastlpcadruh) as druh, SUM(mnozstvi) AS mnozstvi, SUM(prachy_puv) AS cena_prod, SUM(prachy) AS cena_prod2, ;
       $0 AS cena_prum, 0000.0 as stul, 0000000 AS pocet_uctu, SPACE(50) as menostola, 00.00 as round50 ;
       WHERE .NOT. EMPTY(k_odpoctu) GROUP BY uzaverka, u_cis, id_kas, id_man, c_karty FROM tm_sk_pol INTO CURSOR po_stolech_1 READWRITE

DO dopln_nazev

REPLACE FOR nazev = NAZEV_POCUCTU druh WITH "_pocetuctu"

SET ORDER to uzucetkas IN tm_sk_fin
SET RELATION TO uzaverka + u_cis + id_kas INTO tm_sk_fin
REPLACE ALL stul WITH tm_sk_fin.stul
INDEX on STR(stul) + uzaverka TAG stul_uz

IF m.issql
	TRY 
		USE (m.sqlprefix+"rfoodie_stoly") ALIAS stoly IN 0
		=RECCOUNT("stoly")
		UPDATE po_stolech_1 SET menostola=ALLTRIM(stoly.miestnost)+"/"+ALLTRIM(stoly.stol) from stoly WHERE po_stolech_1.stul=stoly.id
		UPDATE pocet_uctu   SET menostola=ALLTRIM(stoly.miestnost)+"/"+ALLTRIM(stoly.stol) from stoly WHERE pocet_uctu.stul  =stoly.id
		UPDATE pocet_uctu_u SET menostola=ALLTRIM(stoly.miestnost)+"/"+ALLTRIM(stoly.stol) from stoly WHERE pocet_uctu_u.stul=stoly.id
		USE IN stoly 
	CATCH 
	ENDTRY 
ENDIF 
SELECT pocet_uctu
REPLACE menostola WITH IIF(stul = INT(stul), STR(stul,7), STR(stul,9,1)) FOR EMPTY(menostola)
SELECT pocet_uctu_u
REPLACE menostola WITH IIF(stul = INT(stul), STR(stul,7), STR(stul,9,1)) FOR EMPTY(menostola)
SELECT po_stolech_1
REPLACE menostola WITH IIF(stul = INT(stul), STR(stul,7), STR(stul,9,1)) FOR EMPTY(menostola)

SELECT stul, c_karty, nazev, druh, SUM(mnozstvi) AS mnozstvi, SUM(cena_prod) AS cena_prod, SUM(cena_prod2) AS cena_prod2, SUM(cena_prum) as cena_prum, pocet_uctu, menostola, round50 ;
       GROUP BY stul, c_karty FROM po_stolech_1 INTO CURSOR po_stolech READWRITE

INSERT INTO po_stolech (stul, pocet_uctu, nazev, druh, menostola, round50) SELECT stul, pocet_uctu, nazev, "_pocetuctu", menostola, round50 FROM pocet_uctu
INDEX ON menostola + nazev TAG stul_nazev
INSERT INTO po_stolech_1 (stul, pocet_uctu, nazev, druh, menostola, uzaverka, round50) SELECT stul, pocet_uctu, nazev, "_pocetuctu", menostola, uzaverka, round50 FROM pocet_uctu_u

SELECT DISTINCT menostola FROM po_stolech INTO CURSOR stoly READWRITE
SET CONFIRM ON
stul=""
IF m.reporty_volane_z_formu
	DEFINE POPUP stul FROM 1,50 PROMPT FIELD menostola TITLE "od stolu"
	ON SELE POPUP stul DEACTIVATE POPUP
	ACTIVATE SCREE
	ACTIVATE POPUP stul
	stul=PROMPT()
ENDIF 
IF ! EMPTY(m.stul)
    stul1 = stoly.menostola
    SET FILTER TO menostola >= m.stul1
    DEFINE POPUP stul FROM 1,50 PROMPT FIELD menostola TITLE "do stolu"
    ON SELE POPUP stul DEACTIVATE POPUP
    ACTIVATE SCREE
    ACTIVATE POPUP stul
    IF ! EMPTY(PROMPT())
        stul2 = stoly.menostola

        IF "1" $ m.dily
            SELECT po_stolech
            SET FILTER TO BETWEEN(menostola, m.stul1, m.stul2)
            DO pr_report WITH "trz_stul", !m.reporty_volane_z_formu
        ENDIF 

        IF "2" $ m.dily
            SELECT po_stolech_1
            SET FILTER TO BETWEEN(menostola, m.stul1, m.stul2)
            DO pr_report WITH "trz_stul_uzav", !m.reporty_volane_z_formu
        ENDIF 

        IF "3" $ m.dily
            SELECT stul, c_karty, druh as nazev, druh, SUM(mnozstvi) AS mnozstvi, SUM(cena_prod) AS cena_prod, SUM(cena_prod2) AS cena_prod2, SUM(cena_prum) as cena_prum, pocet_uctu, menostola, round50 ;
                   WHERE BETWEEN(menostola, m.stul1, m.stul2) GROUP BY stul, druh FROM po_stolech INTO CURSOR po_stolech_druh READWRITE
            DO pr_report WITH "trz_stul", !m.reporty_volane_z_formu
        ENDIF 

        IF ATC("internationa", m.sqldatabaza) > 0
            SELECT menostola, "stred. " + id_man AS uzaverka, 1 AS c_karty, druh AS nazev, 0 AS mnozstvi, SUM(cena_prod) AS cena_prod, SUM(cena_prod2) AS cena_prod2, ;
                   SUM(cena_prum) AS cena_prum, stul, 0 AS pocet_uctu, 0 as round50 ;
                   WHERE BETWEEN(menostola, m.stul1, m.stul2) and nazev # NAZEV_POCUCTU ;
                   GROUP BY menostola, id_man, druh FROM po_stolech_1 INTO CURSOR po_stolech_managerech READWRITE 
            DO pr_report WITH "trz_stul_uzav", !m.reporty_volane_z_formu
        ENDIF 

    ENDIF
ENDIF

DO pozavirej WITH "po_stolech, po_stolech_1, stoly, pocet_uctu, pocet_uctu_u, po_stolech_managerech "


*!*****************************************************************************
*!      Procedure: S_SLEV_KAS
*!*****************************************************************************
PROCEDURE s_slev_kas

had.SHOW()
had.set_had( 11 )

SELECT uzaverka, u_cis, id_kas, text_sleva + "  " AS text_sleva, trzba1+trzba2+trzba3+trzba4+round50 AS cena_slev, cena_celk, PADR(id_kas, 30) as zkr_kas ;
       FROM tm_sk_fin INTO CURSOR platby99 READWRITE
REPLACE FOR "*" $ uzaverka text_sleva WITH "___ davka:" + ALLTRIM(text_sleva)

REPLACE FOR "@"   $ text_sleva     text_sleva WITH ""
REPLACE FOR "^"   $ text_sleva     text_sleva WITH ""
REPLACE FOR "7_VIP"   $ text_sleva text_sleva WITH "7_VIP"
REPLACE FOR "#7_PERS" $ text_sleva text_sleva WITH "#7_PERS"

txt_beze_slevy = jaz("rep_beze_slevy")
REPLACE FOR EMPTY(text_sleva) text_sleva WITH m.txt_beze_slevy
IF m.slovenskaverzia
	REPLACE FOR ABS(cena_slev - cena_celk) = 0    text_sleva WITH m.txt_beze_slevy
ELSE
	REPLACE FOR ABS(cena_slev - cena_celk) < 0.5  text_sleva WITH m.txt_beze_slevy
ENDIF

SET ORDER TO uz_kas IN tm_sk_sez_kas
SET RELATION TO uzaverka + id_kas INTO tm_sk_sez_kas
REPLACE ALL zkr_kas WITH tm_sk_sez_kas.zkr_kas

SELECT id_kas, text_sleva AS druh_pl, zkr_kas, SUM(cena_slev) AS cena_pl, SUM(cena_celk) AS cena_celk, COUNT(id_kas + text_sleva) AS pocetslev ;
	GROUP BY 1, 2 FROM platby99 INTO CURSOR platby2 READWRITE
had.set_had( 55 )

REPLACE ALL druh_pl WITH i_drupla(druh_pl)
*REPLACE ALL druh_pl WITH jaz2(druh_pl)

SELECT id_kas, druh_pl + SPACE(10) AS field2, SUM(cena_pl) AS cena_pl, SUM(cena_celk) AS cena_celk, ;
	zkr_kas AS field1, $0 AS procentick, 0 AS tip, SUM(pocetslev) AS pocetslev ;
	GROUP BY 1, 2 FROM platby2 INTO CURSOR platby READWRITE
INDEX ON field1 + field2 TAG f1f2

had.set_had( 88 )

SELECT field1, SUM(cena_pl) AS cena_pl GROUP BY 1 FROM platby INTO CURSOR sumplatby READWRITE
INDEX ON field1 TAG field1

SELECT platby
SET RELATION TO field1 INTO sumplatby
REPLACE FOR sumplatby.cena_pl#0 procentick WITH ROUND(cena_pl / sumplatby.cena_pl * 100, 1)
had.HIDE()
DO pr_report WITH "trz_kas_slev", !m.reporty_volane_z_formu


IF "SENAT" $ UPPER(m.gchom_path)
    DO sestava_pro_senat
ENDIF


*!*****************************************************************************
*!      Procedure: ODP_DAVK
*!*****************************************************************************
PROCEDURE odp_davk
had.SHOW()
had.set_had( 11 )

DO pozavirej WITH "namarkovano, odecteno, o_dokl1"
SELECT c_kas, c_stredisk, uzaverka, c_karty, SPACE(30) AS nazev, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, id_kas, id_man, PADR(id_kas,30) AS zkr_kas, PADR(id_man,30) AS zkr_man ;
	WHERE uzaverka LIKE "%*%" GROUP BY 1,2,3,4 FROM tm_sk_pol INTO CURSOR namarkovano READWRITE
DO dopln_nazev

DO priprav_odectene
*      *      *      *      *      *      *      *      *      *      *      *
had.set_had( 99 )
SELECT pz1 AS davka, pz2, nazev, SUM(mnozstvi) AS mnozstvi, SUM(mnozstvi*cena) AS prachy, druh, spart ;
	GROUP BY davka, pz2, nazev ORDER BY davka, pz2, nazev FROM odecteno INTO CURSOR o_d4 READWRITE

had.HIDE()
IF "1" $ m.dily
	DO pr_report WITH "trz_odp_davk", !m.reporty_volane_z_formu
ENDIF

SELECT davka, " " AS nazev, 0 AS mnozstvi, SUM(prachy) AS prachy ;
	GROUP BY davka FROM o_d4 INTO CURSOR o_d5 READWRITE
SELECT o_d4
DELETE ALL
INSERT INTO o_d4 (davka, nazev, mnozstvi, prachy) SELECT davka, nazev, mnozstvi, prachy FROM o_d5
IF "2" $ m.dily
	DO pr_report WITH "trz_odp_davk@2", !m.reporty_volane_z_formu
ENDIF


SELECT pz1 as davka, druh AS nazev, 0 AS mnozstvi, SUM(mnozstvi*cena) AS prachy ;
	GROUP BY davka, druh FROM odecteno INTO CURSOR o_d5 READWRITE
SELECT o_d4
DELETE ALL
INSERT INTO o_d4 (davka, nazev, mnozstvi, prachy) SELECT davka, nazev, mnozstvi, prachy FROM o_d5
IF "3" $ m.dily
	DO pr_report WITH "trz_odp_davk@3", !m.reporty_volane_z_formu
ENDIF

SELECT pz1 as davka, spart AS nazev, 0 AS mnozstvi, SUM(mnozstvi*cena) AS prachy ;
	GROUP BY pz1, spart FROM odecteno INTO CURSOR o_d5 READWRITE
SELECT o_d4
DELETE ALL
INSERT INTO o_d4 (davka, nazev, mnozstvi, prachy) SELECT davka, nazev, mnozstvi, prachy FROM o_d5
IF "4" $ m.dily
	DO pr_report WITH "trz_odp_davk@4", !m.reporty_volane_z_formu
ENDIF
DO pozavirej WITH "o_d1, o_d2, o_d3, o_d4, o_d5, namarkovano, odecteno, o_dokl1"


*!*****************************************************************************
*!      Procedure: transakce
*!*****************************************************************************
PROCEDURE transakce
IF m.issql
	SELECT id_kas, uzaverka, VAL(cislo) AS pocet WHERE "TRANS" $ ico ;
		ORDER BY 1,2 FROM tm_sk_txt INTO CURSOR transakc
ELSE 
	SELECT id_kas, uzaverka, VAL(cislo) AS pocet WHERE "TRANS" $ u_cis ;
		ORDER BY 1,2 FROM sk_txt_ INTO CURSOR transakc
ENDIF 
DO pr_report WITH "trz_transakce", !m.reporty_volane_z_formu
DO pozavirej WITH "transakc"

*!*****************************************************************************
*!      Procedure: DPH_DRUH
*!*****************************************************************************
PROCEDURE dph_druh

SELECT id_kas, c_karty, dan_sazba, SPACE(3) as spart, SPACE(m.sirkastlpcadruh) as druh, SUM(prachy) AS cena_prod2, SUM(prachy - dan) AS dan_zakl2, SUM(prachy) AS cena_slev ;
       WHERE (.NOT. EMPTY(k_odpoctu)) .AND. (ALLTRIM(cen_hlad) $ m.cen_hladiny) GROUP BY id_kas, c_karty, dan_sazba ;
       FROM tm_sk_pol INTO CURSOR dph_druh_1 READWRITE

DO dopln_nazev

SELECT id_kas, PADR(id_kas, 30) AS id_zkratka, dan_sazba AS dan, spart, druh, SUM(cena_prod2) AS cena_prod2, SUM(dan_zakl2) AS dan_zakl2, SUM(cena_slev) AS cena_slev, ;
    $0 AS trzba_dph1, $0 AS trzba_dph2, $0 AS trzba_dph3, $0 AS trzba_dph4, ;
    $0 AS sumdph1,    $0 AS sumdph2,    $0 AS sumdph3,    $0 AS sumdph4, 0 AS round50 ;
    GROUP BY id_kas, spart, druh, dan_sazba ;
    FROM dph_druh_1 INTO CURSOR dph_druh READWRITE

SELECT dph_druh
REPLACE FOR dan = m.s_dph1 trzba_dph1 WITH cena_prod2, sumdph1 WITH cena_prod2 - dan_zakl2
REPLACE FOR dan = m.s_dph2 trzba_dph2 WITH cena_prod2, sumdph2 WITH cena_prod2 - dan_zakl2
REPLACE FOR dan = m.s_dph3 trzba_dph3 WITH cena_prod2, sumdph3 WITH cena_prod2 - dan_zakl2
REPLACE FOR dan = m.s_dph4 trzba_dph4 WITH cena_prod2, sumdph4 WITH cena_prod2 - dan_zakl2

SELECT id_kas, id_zkratka, dan, spart, druh, SUM(cena_prod2) AS cena_prod2, SUM(dan_zakl2) AS dan_zakl2, SUM(cena_slev) AS cena_slev, ;
	SUM(trzba_dph1) AS trzba_dph1, SUM(trzba_dph2) AS trzba_dph2, SUM(trzba_dph3) AS trzba_dph3, SUM(trzba_dph4) AS trzba_dph4, ;
	SUM(sumdph1)    AS sumdph1,    SUM(sumdph2)    AS sumdph2,    SUM(sumdph3)    AS sumdph3,    SUM(sumdph4)    AS sumdph4, 00.00 AS round50 ;
	GROUP BY id_kas, spart, druh ;
	FROM dph_druh INTO CURSOR dph_druh_sum READWRITE 

SELECT id_kas, SUM(round50) as round50 FROM tm_sk_fin GROUP BY id_kas INTO CURSOR roundy
IF USED("roundy")
   	UPDATE dph_druh_sum SET round50=roundy.round50 from roundy WHERE dph_druh_sum.id_kas=roundy.id_kas
   	LOCAL m.idxx
   	m.idxx="/\"
   	SELECT dph_druh_sum
   	SCAN ALL 
   		IF m.idxx=dph_druh_sum.id_kas
   			REPLACE round50 WITH 0
   		ELSE
   			m.idxx=dph_druh_sum.id_kas
   		ENDIF 
   	ENDSCAN
   	GO TOP 
   	RELEASE m.idxx
	USE IN roundy
	SELECT dph_druh_sum
ENDIF 


DO pr_report WITH IIF(m.s_dph4 > 9, "trz_kasdp3", "trz_kasdp3_4sl"), !m.reporty_volane_z_formu     && pokud jsou pouzity 4 sazby dph, pouzije se dvouradkovy report

DO pozavirej WITH "dane, dph_druh, dph_druh_sum "


*:*****************************************************************************
*:        Procedure: ROZPDAVK
*:*****************************************************************************
PROCEDURE rozpdavk
PARAMETERS rozpdavk, diely
had.SHOW()
DO pozavirej WITH "odecteno, o_dokl1, namarkovano"
IF m.rozpdavk
	SELECT c_kas, c_stredisk, uzaverka, c_karty, SPACE(30) AS nazev, SUM(mnozstvi) AS prodano, SUM(prachy) AS trzba, SUM(prachy-dan) AS vynos, id_kas, id_man, PADR(id_kas, 30) AS zkr_kas, PADR(id_man, 30) AS zkr_man, ;
		SPACE(15) AS pz1, SPACE(20) AS kdo_kdy, $0 AS mnozstvi, $0 AS naklady, 000000000 AS c_dokl, 000000000 AS c_druh, SPACE(m.sirkastlpcadruh) as druh, SPACE(3) as spart ;
		WHERE uzaverka LIKE "%*%" GROUP BY 1,2,3,4 FROM tm_sk_pol INTO CURSOR namarkovano READWRITE
ELSE 
	SELECT c_kas, c_stredisk, uzaverka, c_karty, SPACE(30) AS nazev, SUM(mnozstvi) AS prodano, SUM(prachy) AS trzba, SUM(prachy-dan) AS vynos, id_kas, id_man, PADR(id_kas, 30) AS zkr_kas, PADR(id_man, 30) AS zkr_man, ;
		SPACE(15) AS pz1, SPACE(20) AS kdo_kdy, $0 AS mnozstvi, $0 AS naklady, 000000000 AS c_dokl, 000000000 AS c_druh, SPACE(m.sirkastlpcadruh) as druh, SPACE(3) as spart ;
		GROUP BY 1,2,3,4 FROM tm_sk_pol INTO CURSOR namarkovano READWRITE
ENDIF 
DO dopln_nazev

DO priprav_odectene

had.set_had( 44 )

SELECT c_dokl, c_karty, c_stredisk, c_kas, uzaverka, pz1, kdo_kdy, "  " AS id_kas, "  " AS id_man, SPACE(30) AS zkr_kas, SPACE(30) AS zkr_man, ;
	SUM(mnozstvi) AS mnozstvi, SUM(mnozstvi * cena) AS naklady, nazev ;
	GROUP BY 1, 2 FROM odecteno INTO CURSOR naklady READWRITE
INDEX ON PADR(uzaverka,14) + STR(c_kas) TAG uz_c_kas  &&&XXXXXXXXX    PADR(uzaverka,19)

*SET RELATION TO c_stredisk INTO fp_str
*UPDATE naklady SET zkr_man=fp_str.id_zkratka, id_man=fp_str.ID FROM fp_str WHERE naklady.c_stredisk=fp_str.c_stredisk
*UPDATE naklady SET zkr_kas=fp_str.id_zkratka, id_kas=fp_str.ID FROM fp_str WHERE naklady.c_kas=fp_str.c_stredisk


*      *      *      *      *      *      *      *      *
IF m.rozpdavk
	SELECT c_kas, uzaverka, SUM(pocet_h) AS pocethostu, SPACE(15) AS pz1 WHERE uzaverka LIKE "%*%" ;
		GROUP BY 1,2 FROM tm_sk_fin INTO CURSOR pocet_hostu READWRITE
ELSE
	SELECT c_kas, uzaverka, SUM(pocet_h) AS pocethostu, SPACE(15) AS pz1 ;
		GROUP BY 1,2 FROM tm_sk_fin INTO CURSOR pocet_hostu READWRITE
ENDIF 
INDEX ON PADR(uzaverka,14) + STR(c_kas) TAG uz_c_kas   &&&XXXXXXX     PADR(uzaverka,19)

*      *      *      *      *      *      *      *      *
SELECT namarkovano
REPLACE ALL uzaverka WITH LEFT(uzaverka, 14)


SET RELATION TO PADR(uzaverka,14) + STR(c_kas) INTO naklady   &&&XXXXXXX    TO PADR(uzaverka,19)
REPLACE ALL pz1 WITH naklady.pz1

INSERT INTO namarkovano (c_dokl, c_karty, c_stredisk, c_kas, uzaverka, pz1, kdo_kdy, id_kas, id_man, zkr_kas, zkr_man, mnozstvi, naklady, nazev) ;
                  SELECT c_dokl, c_karty, c_stredisk, c_kas, uzaverka, pz1, kdo_kdy, id_kas, id_man, zkr_kas, zkr_man, mnozstvi, naklady, nazev FROM naklady

UPDATE namarkovano SET zkr_man=fp_str.id_zkratka, id_man=fp_str.id FROM fp_str WHERE namarkovano.c_stredisk = fp_str.c_stredisk
UPDATE namarkovano SET zkr_kas=fp_str.id_zkratka, id_kas=fp_str.id FROM fp_str WHERE namarkovano.c_kas      = fp_str.c_stredisk

had.set_had( 66 )
DO dopln_druhy

INDEX ON pz1 + PADR(uzaverka,14) + STR(c_dokl) + druh + nazev TAG pz1_uz    &&&XXXXX    pz1 + PADR(uzaverka,19)
SET RELATION TO PADR(uzaverka,14) + STR(c_kas) INTO pocet_hostu                 &&&XXXXXX    TO PADR(uzaverka,19)

REPLACE ALL pocet_hostu.pz1 WITH pz1

SELECT pz1, SUM(pocethostu) AS pocethostu GROUP BY 1 FROM pocet_hostu INTO CURSOR pocet_hostu_pz1 READWRITE
INDEX on pz1 TAG pz1

SELECT namarkovano
SET RELATION TO pz1 INTO pocet_hostu_pz1 ADDITIVE 

had.HIDE()
IF "1" $ m.diely
	DO pr_report WITH "trz_rozpdavk", !m.reporty_volane_z_formu
ENDIF 

IF "2" $ m.diely

	SELECT pz1, c_kas, c_stredisk, c_karty, druh as nazev, SUM(prodano) AS prodano, SUM(trzba) AS trzba, SUM(vynos) AS vynos, SUM(mnozstvi) AS mnozstvi, SUM(naklady) AS naklady, ;
		uzaverka, id_kas, id_man, zkr_kas, zkr_man, kdo_kdy, c_dokl, druh ;
		GROUP BY c_kas, c_stredisk, uzaverka, c_dokl, druh FROM namarkovano INTO CURSOR namarkovano2 READWRITE

*	SELECT namarkovano2
*	SET RELATION TO PADR(uzaverka,19) + STR(c_kas) INTO naklady
*	REPLACE ALL pz1 WITH naklady.pz1

	INDEX ON  pz1 + PADR(uzaverka,14) + STR(c_dokl) + druh + nazev TAG pz1_uz
	SET RELATION TO PADR(uzaverka,14) + STR(c_kas) INTO pocet_hostu
	had.HIDE()

	DO pr_report WITH "trz_rozpdavk@2", !m.reporty_volane_z_formu
	DO pozavirej WITH "namarkovano2"
ENDIF 

IF "3" $ m.diely
	SELECT namarkovano
	REPLACE ALL uzaverka WITH pz1
	SELECT pocet_hostu
	REPLACE ALL uzaverka WITH pz1
	SELECT c_kas, uzaverka, SUM(pocethostu) AS pocethostu GROUP BY 1,2 FROM pocet_hostu INTO CURSOR pocet_hostu_sum READWRITE
	SELECT pocet_hostu
	DELETE ALL
	INSERT INTO pocet_hostu (c_kas, uzaverka, pocethostu) SELECT c_kas, uzaverka, pocethostu FROM pocet_hostu_sum

	SELECT pz1, c_kas, c_stredisk, c_karty, nazev, SUM(prodano) AS prodano, SUM(trzba) AS trzba, SUM(vynos) AS vynos, SUM(mnozstvi) AS mnozstvi, SUM(naklady) AS naklady, ;
		uzaverka, id_kas, id_man, zkr_kas, zkr_man, SPACE(2) AS kdo_kdy, 0 AS c_dokl ;
		GROUP BY pz1, c_kas, nazev FROM namarkovano INTO CURSOR namarkovano2 READWRITE

	INDEX ON pz1 + PADR(uzaverka,14) + STR(c_dokl) + IIF(prodano # 0, "A", "B") + IIF(prodano = mnozstvi, "A", "B") + nazev TAG pz1_uz
	SET RELATION TO PADR(uzaverka,14) + STR(c_kas) INTO pocet_hostu

	DO pr_report WITH "trz_rozpdavk@3", !m.reporty_volane_z_formu
	DO pozavirej WITH "namarkovano2"
ENDIF 

IF "4" $ m.diely
	SELECT pz1, c_kas, c_stredisk, c_karty, druh as nazev, SUM(prodano) AS prodano, SUM(trzba) AS trzba, SUM(vynos) AS vynos, SUM(mnozstvi) AS mnozstvi, SUM(naklady) AS naklady, ;
		uzaverka, id_kas, id_man, zkr_kas, zkr_man, SPACE(2) AS kdo_kdy, 0 AS c_dokl, druh ;
		GROUP BY pz1, c_kas, druh FROM namarkovano INTO CURSOR namarkovano2 READWRITE

	INDEX ON pz1 + PADR(uzaverka,14) + STR(c_dokl) + IIF(prodano # 0, "A", "B") + IIF(prodano = mnozstvi, "A", "B") + nazev TAG pz1_uz
	SET RELATION TO PADR(uzaverka,14) + STR(c_kas) INTO pocet_hostu

	DO pr_report WITH "trz_rozpdavk@4", !m.reporty_volane_z_formu
ENDIF 

DO pozavirej WITH "namarkovano, namarkovano2, naklady, pocet_hostu, pocet_hostu_sum "
DO pozavirej WITH "odecteno, o_dokl1"
RELEASE m.rozpdavk


*!*****************************************************************************
*!      Procedure: s_hodiny
*!*****************************************************************************
PROCEDURE s_hodiny

SET ORDER TO uzucetkas IN tm_sk_fin

SELECT uzaverka, u_cis, id_kas, "  " AS hodina, SUM(prachy) AS cena_slev, 00000000 AS pocet_h, 00.00 as round50 ;
       GROUP BY id_kas, uzaverka, u_cis ;
       WHERE ALLTRIM(cen_hlad) $ m.cen_hladiny ;
       FROM tm_sk_pol INTO CURSOR pol_hlad READWRITE
SET RELATION TO uzaverka + u_cis + id_kas INTO tm_sk_fin
REPLACE ALL hodina WITH IIF("*" $ uzaverka, "DA", SUBSTR(tm_sk_fin.u_cas, 4,2)), pocet_h WITH tm_sk_fin.pocet_h, round50 WITH tm_sk_fin.round50

SELECT id_kas, hodina, PADR(id_kas, 30) AS zkr_kas, SUM(cena_slev+round50) AS cena_slev, SUM(pocet_h) AS pocet_h ;
    GROUP BY id_kas, hodina ;
    FROM pol_hlad INTO CURSOR hodiny READWRITE

SELECT DISTINCT id_kas, zkr_kas WHERE hodina # "DA" FROM hodiny INTO CURSOR hod_kas READWRITE
SCAN
	SCATTER FIELD id_kas, zkr_kas MEMVAR
	SELECT hodiny
	FOR iii = 0 TO 23
		LOCATE FOR id_kas = m.id_kas .AND. hodina = TRANSFORM(m.iii, "@L 99")
		IF ! FOUND()
			INSERT INTO hodiny (id_kas, zkr_kas, hodina) VALUES (m.id_kas, m.zkr_kas, TRANSFORM(m.iii, "@L 99"))
		ENDIF
	ENDFOR
	SELECT hod_kas
ENDSCAN
SELECT hodiny
INDEX ON id_kas + hodina TAG kas_hod
DO pr_report WITH "trz_hodiny", !m.reporty_volane_z_formu
DO pozavirej WITH "hodiny, hod_kas, pol_hlad"

*    *    *    *    *    *    *    *    *    *
SELECT id_kas, u_cis, SUM(trzba1+trzba2+trzba3+trzba4+round50) AS cena_slev, SUM(pocet_h) AS pocet_h, $0 AS trzba1, $0 AS trzba2, $0 AS trzba3, $0 AS trzba4, $0 AS trzba5, ;
	00000 AS hostu1, 00000 AS hostu2, 00000 AS hostu3, 00000 AS hostu4, 00000 AS hostu5 ;
	GROUP BY 1,2 FROM tm_sk_fin INTO CURSOR castky READWRITE

hranice1 = VAL(cen_pasma)
hranice2 = VAL(SUBSTR(cen_pasma, AT(" ", m.cen_pasma, 1)))
hranice3 = VAL(SUBSTR(cen_pasma, AT(" ", m.cen_pasma, 2)))
hranice4 = VAL(SUBSTR(cen_pasma, AT(" ", m.cen_pasma, 3)))

REPLACE FOR ABS(cena_slev) <  m.hranice1 trzba1 WITH cena_slev, hostu1 WITH pocet_h, cena_slev WITH 0, pocet_h WITH 0
REPLACE FOR ABS(cena_slev) <  m.hranice2 trzba2 WITH cena_slev, hostu2 WITH pocet_h, cena_slev WITH 0, pocet_h WITH 0
REPLACE FOR ABS(cena_slev) <  m.hranice3 trzba3 WITH cena_slev, hostu3 WITH pocet_h, cena_slev WITH 0, pocet_h WITH 0
REPLACE FOR ABS(cena_slev) <  m.hranice4 trzba4 WITH cena_slev, hostu4 WITH pocet_h, cena_slev WITH 0, pocet_h WITH 0
REPLACE FOR ABS(cena_slev) >= m.hranice4 trzba5 WITH cena_slev, hostu5 WITH pocet_h

SELECT id_kas, SUM(trzba1) AS trzba1, SUM(hostu1) AS hostu1, ;
	SUM(trzba2) AS trzba2, SUM(hostu2) AS hostu2, ;
	SUM(trzba3) AS trzba3, SUM(hostu3) AS hostu3, ;
	SUM(trzba4) AS trzba4, SUM(hostu4) AS hostu4, ;
	SUM(trzba5) AS trzba5, SUM(hostu5) AS hostu5, ;
	SUM(trzba1 + trzba2 + trzba3 + trzba4 + trzba5) AS cena_slev, ;
	SUM(hostu1 + hostu2 + hostu3 + hostu4 + hostu5) AS pocet_h ;
	GROUP BY id_kas FROM castky INTO CURSOR castky2 READWRITE
DO pr_report WITH "trz_cenpasma", !m.reporty_volane_z_formu
DO pozavirej WITH "castky, castky2"


*:*****************************************************************************
*:        Procedure: MUTACE
*:*****************************************************************************
PROCEDURE mutace
had.SHOW()
IF m.issql
	rv("narody","narody")
	IF USED("narody") AND RECCOUNT("narody")=0
		USE IN narody
	ENDIF
ELSE
	IF FILE(m.gccen_path+"narody.dbf")
		USE (m.gccen_path+"narody") IN 0
	ENDIF
ENDIF
IF USED("narody")
	SELECT tm_sk_fin.id_kas, tm_sk_fin.mutace, PADR(tm_sk_fin.id_kas, 30) as zkr_kas, SUM(tm_sk_fin.pocet_h) AS pocet_h, narody.narodnost, ;
		SUM(tm_sk_fin.cena_celk) AS cena_celk, SUM(trzba1+trzba2+trzba3+trzba4) AS cena_slev ;
		GROUP BY 1, 2 ;
		FROM tm_sk_fin LEFT JOIN narody ON narody.mutace == tm_sk_fin.mutace INTO CURSOR sk_mut READWRITE

    UPDATE sk_mut SET zkr_kas=fp_str.id_zkratka FROM fp_str WHERE sk_mut.id_kas=fp_str.id
	DELETE FOR cena_slev = 0
	REPLACE FOR ISNULL(narodnost) narodnost WITH "__ ostatni __"
	had.HIDE()
	DO pr_report WITH "trz_narodnosti", !m.reporty_volane_z_formu
	DO pozavirej WITH "narody, sk_mut"
ENDIF


*!*****************************************************************************
*!      Procedure: s_druhy_a_platby
*!*****************************************************************************
PROCEDURE s_druhy_a_platby

had.SHOW()
SELECT c_kas, PADR(i_drupla(druh_pl), 30) AS druh, PADR(id_kas, 30) AS id_zkratka, SUM(cena_pl) AS cena_prod2, SUM(tip) AS tip, 0 AS dil  ;
	GROUP BY 1, 2 FROM tm_sk_plt INTO CURSOR plt22 READWRITE

REPLACE ALL druh WITH jaz2(druh)
had.set_had( 22 )

*!*    SELECT id_kas, PADR(spart, 30) AS druh, c_kas, PADR(id_kas,30) AS id_zkratka, 1 AS dil, ;
*!*    	SUM(prachy_puv)         AS cena_prod, SUM(prachy)     AS cena_prod2, ;
*!*    	SUM(prachy_puv-dan_puv) AS dan_zakl,  SUM(prachy-dan) AS dan_zakl2 ;
*!*    	GROUP BY id_kas, spart FROM tm_sk_pol WHERE ALLTRIM(cen_hlad) $ m.cen_hladiny INTO CURSOR trz_skup_plt READWRITE

SELECT c_karty, id_kas, SPACE(3) as spart, c_kas, ;
       SUM(prachy_puv) AS cena_prod, SUM(prachy)     AS cena_prod2, ;
       $0              AS dan_zakl,  SUM(prachy-dan) AS dan_zakl2   ;
       GROUP BY c_karty, id_kas FROM tm_sk_pol WHERE ALLTRIM(cen_hlad) $ m.cen_hladiny INTO CURSOR trz_skup_plt_1 READWRITE
DELETE FOR EMPTY(cena_prod) .and. EMPTY(cena_prod2)

had.set_had(33)
DO dopln_nazev
had.set_had(44)
REPLACE FOR cena_prod2 # 0 dan_zakl WITH dan_zakl2 * cena_prod / cena_prod2

SELECT id_kas, PADR(spart, 30) as druh, c_kas, PADR(id_kas, 30) AS id_zkratka, 1 AS dil, ;
       SUM(cena_prod) AS cena_prod, SUM(cena_prod2) AS cena_prod2, ;
       SUM(dan_zakl2) AS dan_zakl,  SUM(dan_zakl2) AS dan_zakl2 ;
       GROUP BY id_kas, druh FROM trz_skup_plt_1 INTO CURSOR trz_skup_plt READWRITE

INSERT INTO trz_skup_plt (id_kas, c_kas, druh, id_zkratka, cena_prod2, dil) SELECT id_zkratka, c_kas, druh, id_zkratka, cena_prod2, dil FROM plt22
UPDATE trz_skup_plt SET id_zkratka=fp_str.id_zkratka FROM fp_str WHERE trz_skup_plt.c_kas=fp_str.c_stredisk

had.HIDE()
INDEX ON id_kas + STR(dil,1) + druh TAG kdsd
m.ttl_report = "TRZBY PO SPARTECH A PLATBACH"
DO pr_report WITH "trz_kas_druh_platba", !m.reporty_volane_z_formu


SELECT " " AS id_kas, druh, "za vsechna strediska" AS id_zkratka, dil,  ;
	SUM(cena_prod) AS cena_prod, SUM(cena_prod2) AS cena_prod2, ;
	SUM(dan_zakl)  AS dan_zakl,  SUM(dan_zakl2)  AS dan_zakl2 ;
	GROUP BY dil, druh FROM trz_skup_plt INTO CURSOR trz_skup_plt2

m.ttl_report = "TRZBY PO SPARTECH A PLATBACH"
DO pr_report WITH "trz_kas_druh_platba@2", !m.reporty_volane_z_formu

DO pozavirej WITH "trz_skup_plt, trz_skup_plt_1, trz_skup_plt2, plt22"


*:*****************************************************************************
*:        Procedure: DOPLN_NAZEV
*:*****************************************************************************
PROCEDURE dopln_nazev
IF .not. EMPTY(DBF())
    SET RELATION TO c_karty INTO prodane_karty
    cmd = "REPL ALL "
    IF FSIZE("nazev") > 0
        cmd = m.cmd + " nazev WITH prodane_karty.nazev,"
    ENDIF 
    IF FSIZE("druh") > 0
        cmd = m.cmd + " druh WITH prodane_karty.druh_xx,"
    ENDIF 
    IF FSIZE("spart") > 0
        cmd = m.cmd + " spart WITH prodane_karty.spart,"
    ENDIF 
    IF FSIZE("cena_prum") > 0 .and. FSIZE("mnozstvi") > 0
        cmd = m.cmd + " cena_prum WITH prodane_karty.cena_prum * mnozstvi,"
    ENDIF 
    IF FSIZE("summc") > 0
        cmd = m.cmd + " summc WITH prodane_karty.cena_prum * mnozstvi,"
    ENDIF 

    IF RIGHT(m.cmd, 1) = ","
        cmd = LEFT(m.cmd, LEN(m.cmd)-1)
    ENDIF 
    &cmd
ENDIF 

*:*****************************************************************************
*:        Procedure: DOPLN_DRUHY
*:*****************************************************************************
PROCEDURE dopln_druhy
SET ORDER TO c_karty IN karty
SET RELATION TO c_karty INTO karty
REPLACE ALL c_druh WITH karty.c_druhk
REPLACE c_druh WITH karty.c_druh FOR EMPTY(namarkovano.c_druh)
REPLACE c_druh WITH karty.c_druhs FOR EMPTY(namarkovano.c_druh)
UPDATE namarkovano SET druh=skupiny.druh, spart=skupiny.spart FROM skupiny WHERE namarkovano.c_druh=skupiny.c_druh


*:*****************************************************************************
PROCEDURE spocti_food_cost
IF m.isfoodcost
    REPLACE FOR dan_zakl2 # 0 food_cost WITH ROUND(100 * cena_prum / dan_zakl2, 1)   &&& FoodCost = WarenEinsatz
ELSE
    REPLACE FOR dan_zakl2 # 0 food_cost WITH ROUND(100 * (dan_zakl2 - cena_prum) / dan_zakl2, 1)    &&& Spanne
*    REPLACE FOR cena_prum # 0 food_cost WITH ROUND(100 * (dan_zakl2 - cena_prum) / cena_prum, 1)    &&& Marze
ENDIF


*!*****************************************************************************
PROCEDURE sestava_pro_senat
had.SHOW()
had.set_had( 11 )

SET ORDER TO c_karty IN karty

SELECT d.*, p.partner, p.naz6 WHERE operace = "VE" .AND. d.cen_hlad = "05" AND BETWEEN(d.datum_dokl, m.uctden1, m.uctden2) ;
    FROM (m.fn_doklady) d LEFT JOIN (m.fn_partneri) p ON p.c_partner = d.c_partner INTO TABLE (m.gcpri_path + "dokl05")

SELECT o.c_dokl, o.c_karty, o.mnozstvi, o.cena_part, d.doklad, d.datum_dokl, d.partner, d.naz6 ;
    WHERE o.c_dokl IN (SELECT c_dokl FROM dokl05) ;
    FROM (m.fn_obraty) o LEFT JOIN (m.gcpri_path + "dokl05") d ON d.c_dokl = o.c_dokl INTO TABLE (m.gcpri_path + "obra05")

SELECT SUBSTR(DTOS(datum_dokl), 3) + " SKLAD" AS uzaverka, STR(doklad, 7) AS u_cis, c_karty, ;
       SPACE(30) AS nazev, -mnozstvi * cena_part AS prachy, mnozstvi, partner AS text_link, naz6 ;
       FROM (m.gcpri_path + "obra05") INTO TABLE (m.gcpri_path + "obra05_2")
SET RELATION TO c_karty INTO karty
REPLACE ALL nazev WITH karty.nazev

had.set_had( 22 )

SET ORDER TO druh IN skupiny
SET ORDER TO c_karty IN karty

SELECT pol.uzaverka, pol.u_cis, c_karty, SPACE(30) AS nazev, $0 + pol.prachy AS prachy, 1.77 AS dan, pol.mnozstvi, pol.cen_hlad, ;
    fin.text_sleva, fin.text_link, SPACE(m.sirkastlpcadruh) AS druh, ;
    $0 AS trzba1, $0 AS trzba2, $0 AS trzba3, $0 AS trzba4 ;
    WHERE pol.uzaverka + pol.u_cis IN (SELECT uzaverka + u_cis FROM (m.fn_sk_o_fin) WHERE LEFT(text_sleva, 3) = "#R_") AND ! EMPTY(k_odpoctu) ;
    FROM (m.fn_sk_o_pol) pol LEFT JOIN (m.fn_sk_o_fin) fin ON fin.uzaverka = pol.uzaverka AND fin.u_cis = pol.u_cis ;
    INTO TABLE (m.gcpri_path + "repre1")

SELECT DISTINCT F.uzaverka, F.u_cis, F.text_sleva, r1.text_link ;
    WHERE INLIST(LEFT(F.text_sleva, 1), "@", "^") AND SUBSTR(F.text_sleva, 2, 7) IN (SELECT u_cis FROM (m.gcpri_path + "repre1")) ;
    FROM (m.fn_sk_o_fin) F LEFT JOIN (m.gcpri_path + "repre1") r1 ON r1.u_cis == SUBSTR(F.text_sleva, 2, 7) ;
    INTO TABLE (m.gcpri_path + "repre_storna_fin")

SELECT pol.uzaverka, pol.u_cis, c_karty, SPACE(30) AS nazev, $0 + pol.prachy AS prachy, 1.77 AS dan, pol.mnozstvi, pol.cen_hlad, ;
    fin.text_sleva, fin.text_link ;
    WHERE pol.uzaverka + pol.u_cis IN (SELECT uzaverka + u_cis FROM (m.gcpri_path + "repre_storna_fin") ) AND ! EMPTY(k_odpoctu) ;
    FROM (m.fn_sk_o_pol) pol LEFT JOIN (m.gcpri_path + "repre_storna_fin") fin ON fin.uzaverka = pol.uzaverka AND fin.u_cis = pol.u_cis ;
    INTO TABLE (m.gcpri_path + "repre1_storna")

SELECT repre1
APPEND FROM (m.gcpri_path + "obra05_2")
APPEND FROM (m.gcpri_path + "repre1_storna")

had.set_had( 44 )
SET RELATION TO c_karty INTO karty
REPLACE ALL druh WITH karty.druh_kalk, nazev WITH karty.nazev

SET RELATION TO druh INTO skupiny
REPLACE ALL dan WITH skupiny.dan22    && ktera dan se pouzije !!!!!!!!!!!!!!!!!!!!!!!!!
SET RELATION TO

REPLACE FOR dan < 1 dan WITH 1.20
had.set_had( 88 )

SELECT repre1
REPL FOR dan = m.s_dph1 trzba1 WITH prachy
REPL FOR dan = m.s_dph2 trzba2 WITH prachy
REPL FOR dan = m.s_dph3 trzba3 WITH prachy
REPL FOR dan = m.s_dph4 trzba4 WITH prachy

SELECT text_link, uzaverka, u_cis, ;
    SUM(trzba1) AS trzba1, SUM(trzba2) AS trzba2, SUM(trzba3) AS trzba3, SUM(trzba4) AS trzba4 ;
    GROUP BY text_link, uzaverka, u_cis ;
    FROM (m.gcpri_path + "repre1") INTO TABLE (m.gcpri_path + "repre2")
had.HIDE()


txt_hlavicka = "KOMPLET"
DO pr_report WITH "trz_repre", !m.reporty_volane_z_formu

SET FILTER TO "SKLAD" $ uzaverka
txt_hlavicka = "SKLAD"
DO pr_report WITH "trz_repre@skl", !m.reporty_volane_z_formu

SET FILTER TO .NOT. ("SKLAD" $ uzaverka)
txt_hlavicka = "POKLADNY"
DO pr_report WITH "trz_repre@kas", !m.reporty_volane_z_formu

