
SELECT DISTI firma FROM transactions INTO CURSOR firmy
DEFINE POPUP firmy FROM 1,55 PROMPT FIELD firma TITLE "Vyber firmu, [Esc]=vsechny"
ON SELECTION POPUP firmy deacti POPUP 
ACTIVATE SCREEN 
ACTIVATE POPUP firmy
IF EMPTY(PROMPT())
    SELECT DISTI ALLTRIM(clientname) + " (" + ALLTRIM(client_id) + ")" as druh, .F. AS lbl, client                              FROM transactions INTO CURSOR cloveci READWRITE 
ELSE
    vybranafirma = firmy.firma
    SELECT DISTI ALLTRIM(clientname) + " (" + ALLTRIM(client_id) + ")" as druh, .F. AS lbl, client WHERE firma = m.vybranafirma FROM transactions INTO CURSOR cloveci READWRITE 
ENDIF 
DO FORM seldruhy WITH "cloveci", "Vyber strávníky"

SELECT *, 0000000.00 AS amount_poi ;
       WHERE amount # 0 AND client IN (SELECT client FROM cloveci WHERE lbl) FROM transactions INTO CURSOR trans2 READWRITE 

REPLACE ALL amount_poi WITH - amount_poi, amount WITH - amount, balance WITH - balance 
REPLACE FOR kind = "POI" amount_poi WITH amount, amount WITH 0, balance WITH 0
ALTER table trans2 ADD polozky MEMO 

IF .not. m.issql
    DO zpracuj_eskacka
ENDIF 

SELECT trans2 
INDEX on clientname + client_id + trans_at TAG cli_tra
DO pr_report WITH "stravovani_transakce"

IF ATC("d_dts", m.gchom_path) > 0
    COPY FIELD firma, client, client_id, clientname, u_cis, trans_at, cena_slev, cena_celk, cena_podpo to (m.gcexportpath + "stravovani") type XLS
    SELECT tr_pol
    INDEX on firma + clientname + uzaverka TAG fcu
    COPY to (m.gcexportpath + "stravovani_polozky") type XLS
ENDIF 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE zpracuj_eskacka
USE (m.gchom_path + "\data\karty")  AGAIN IN 0 NOUPDATE ORDER c_karty 
USE (m.gchom_path + "\data\sk_fin") AGAIN IN 0 NOUPDATE 
USE (m.gchom_path + "\data\sk_pol") AGAIN IN 0 NOUPDATE

uzav1 = SUBSTR(TTOC(s_trz_dat1 - 3, 1), 3,6)
uzav2 = SUBSTR(TTOC(s_trz_dat2 + 3, 1), 3,6)

WAIT "sk_fin" WINDOW NOWAIT 
SELECT uzaverka, u_cis, cena_slev, cena_celk, 001.000000000 as slev_koef, .F. as zpracovat ;
       WHERE BETWEEN(uzaverka, m.uzav1, m.uzav2) FROM sk_fin INTO CURSOR tr_fin READWRITE
INDEX on u_cis TAG u_cis

SELECT trans2
WAIT "scan trans2" WINDOW NOWAIT 
SCAN
    SCATTER FIELDS u_cis MEMVAR 
    SELECT tr_fin
    LOCATE FOR u_cis = m.u_cis
    IF FOUND()
        REPLACE zpracovat WITH .T.
        SELECT trans2
        REPLACE cena_slev WITH tr_fin.cena_slev, cena_celk WITH tr_fin.cena_celk
    ENDIF 
ENDSCAN 

WAIT "pol-koef" WINDOW NOWAIT 
SELECT tr_fin
DELETE FOR EMPTY(zpracovat)
REPLACE  FOR (cena_celk # 0) slev_koef WITH ROUND(cena_slev / cena_celk, 9)
INDEX on uzaverka + u_cis TAG uz_ucet

SELECT uzaverka, u_cis, c_karty, SPACE(30) AS nazev, SPACE(30) as kategorie, mnozstvi, prachy, prachy as prachy_old, prachy AS cena_podpo, dan, SPACE(22) as client_id, SPACE(66) as clientname, SPACE(25) as firma ;
       WHERE uzaverka + u_cis IN (SELECT uzaverka + u_cis FROM tr_fin) FROM sk_pol INTO CURSOR tr_pol READWRITE 
SET RELATION TO c_karty INTO karty
REPLACE ALL nazev WITH karty.nazev, kategorie WITH karty.nazev5
REPLACE FOR LOWER(LEFT(nazev, 25)) = LOWER(LEFT(kategorie, 25)) kategorie WITH ""

*      *     *      *     *      *     *      *     *      *     zatim pro DTS zacatek
REPLACE ALL cena_podpo WITH karty.cena11 * mnozstvi
*      *     *      *     *      *     *      *     *      *     zatim pro DTS konec




**************** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
REPLACE FOR c_karty = 1025  prachy WITH mnozstvi * 6.6
REPLACE FOR c_karty = 1058  prachy WITH mnozstvi * 10
REPLACE FOR c_karty = 1063  prachy WITH mnozstvi * 11
REPLACE FOR c_karty = 15221 prachy WITH mnozstvi * 24.4
REPLACE FOR c_karty = 15219 prachy WITH mnozstvi * 24.4
REPLACE FOR c_karty = 7769  prachy WITH mnozstvi * 29.64
REPLACE FOR c_karty = 7970  prachy WITH mnozstvi * 19




SET RELATION TO uzaverka + u_cis INTO tr_fin
REPLACE ALL prachy WITH ROUND(prachy * tr_fin.slev_koef, 2)
INDEX on u_cis TAG u_cis
o_u_c = "%^*&^/.,#*%"
polozky = ""
cena_podpo = 0
cena_xxxxx = 0
WAIT "scan pol-trans" WINDOW NOWAIT 
SCAN
    IF u_cis = m.o_u_c
        polozky = m.polozky + CHR(13) + formatuj_polozku()
        cena_xxxxx = m.cena_xxxxx + prachy
        cena_podpo = m.cena_podpo + tr_pol.cena_podpo
    ELSE
        o_u_c = u_cis
        cena_xxxxx = prachy
        cena_podpo = tr_pol.cena_podpo
        polozky =                       formatuj_polozku()
    ENDIF    
    SELECT trans2
    LOCATE FOR u_cis = m.o_u_c
    IF FOUND()
        REPLACE polozky WITH m.polozky, cena_podpo WITH m.cena_podpo


**************** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        REPLACE amount WITH m.cena_xxxxx, cena_slev WITH m.cena_xxxxx



        SCATTER FIELDS client_id, clientname, firma MEMVAR 
        SELECT tr_pol
        GATHER  FIELDS client_id, clientname, firma MEMVAR 
    ELSE
        SELECT tr_pol
    ENDIF 
ENDSCAN 

**************** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SELECT trans2
DELETE FOR u_cis = "F157711"
DELETE FOR u_cis = "E104441"



DO pozavirej WITH "sk_fin, sk_pol, karty"
WAIT CLEAR 

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION formatuj_polozku
DO CASE 
CASE mnozstvi = 1
    RETURN      "    "        + "    " + nazev + STR(prachy, 8,1)
CASE mnozstvi = INT(mnozstvi)
    RETURN STR(mnozstvi, 4,0) + "    " + nazev + STR(prachy, 8,1)
OTHERWISE 
    RETURN STR(mnozstvi, 7,2) + " "    + nazev + STR(prachy, 8,1)
ENDCASE 
