#DEFINE ODD "; "
#DEFINE UVOZOVKY ''
#DEFINE CRLF CHR(13)+CHR(10)

DO CASE 
CASE UPPER(ALIAS()) = "ZBOZI1"

    outfile = m.gcexportpath + "prodano.csv"
    WAIT m.outfile WINDOW NOWAIT 
    COPY all to (m.gcexportpath + "prodano")
    SELECT uct_den, id_kas, druh, c_karty, cen_hlad, dotacia, nazev, SUM(mnozstvi) as mnozstvi, SUM(naklady) as naklady, SUM(cena_slev) as cena_slev;
           GROUP BY 1,2,3,4,5,6 FROM (m.gcexportpath + "prodano") INTO TABLE (m.gcexportpath + "prodano_sum")
    STRTOFILE("datum   ; kasa;      c_karty;     nazev                  ; MJ ; skupina ; cen_hlad; mnozstvi;      naklady;        trzba;dotovano" + CRLF, m.outfile)

    SCAN
       txt = dx(uct_den) + tx(id_kas) + nx(c_karty) + tx(LEFT(nazev, 27)) + tx(RIGHT(nazev, 3)) + tx(druh) + tx(cen_hlad) + nx2(mnozstvi) + nx2(naklady) + nx2(cena_slev) + tx(dotacia)
       STRTOFILE(m.txt + CRLF, m.outfile, 1)
    ENDSCAN
    USE IN prodano
    USE IN prodano_sum

CASE UPPER(ALIAS()) = "NAKUPY"

    outfile = m.gcexportpath + "nakupy" + m.gcpgm_id
    WAIT m.outfile WINDOW NOWAIT 
    COPY all to (m.outfile)


    SELECT c_stredisk, c_karty, c_dokl, datum_dokl, druh, nazev, c_partner, partner, SUM(mnozstvi) AS mnozstvi, SUM(mnozstvi * cena) AS prachy, $0 as cena_prum, $0 as cena_vazen, " " as obdobi ;
           GROUP BY 1, 2, 3 FROM (m.outfile) INTO CURSOR nakupy2 WHERE operace = "PE" READWRITE 
    DELETE FOR mnozstvi = 0

    IF FILE(m.gccen_path + "prumerne_ceny.dbf")
       USE (m.gccen_path + "prumerne_ceny") ORDER c_karty IN 0
       SELECT nakupy2
       SET RELATION TO c_karty INTO prumerne_ceny
       REPLACE ALL cena_prum WITH prumerne_ceny.cena_prum, cena_vazen WITH prumerne_ceny.cena_vazen, obdobi WITH prumerne_ceny.obdobi
    ENDIF 

    STRTOFILE("     Sklad ;     c_karty ;       nazev                 ;MJ  ; Skupina   ;      Datum;    Mnozstvi ;  CenaCelkem ;       CenaMJ;    c_partner; partner                       ; cena_prumer ;cena_vazen   ;obdobi;" + CRLF, m.outfile + ".csv")
    SCAN
       txt = nx(-c_stredisk) + nx(c_karty) + tx(LEFT(nazev, 27)) + tx(RIGHT(nazev, 3)) + tx(druh) + dx(datum_dokl) + nx2(mnozstvi) + nx2(prachy) + nx2(prachy/mnozstvi) +  + nx(c_partner) + tx(partner) + nx2(cena_prum) + nx2(cena_vazen) + tx(obdobi)
       STRTOFILE(m.txt + CRLF, m.outfile + ".csv", 1)
    ENDSCAN

CASE UPPER(ALIAS()) = "XXXXX"

ENDCASE 
WAIT CLEAR 


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


********** vraci formatovane cislo na 2 des.mista **********
FUNCTION nx2 (cislo)
RETURN nx(cislo, 2)


********** vraci string + uvozovky  **********
FUNCTION tx (retez)
RETURN UVOZOVKY + STRTRAN(retez, UVOZOVKY, "'") + UVOZOVKY + ODD


********** vraci datum jako string YYYY-MM-DD + uvozovky  **********
FUNCTION dx (dat)
RETURN UVOZOVKY + LEFT(TTOC(dat, 3), 10) + UVOZOVKY + ODD
