*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
*   
*   
*  2023-08-33 
*  2024-02-05   zmeny cen
*
*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   

had = CREATEOBJECT("had")
had.SHOW()
ACTIVATE SCREEN 

SELECT orders 
REPLACE FOR LEFT(created_at, 10) = date .and. SUBSTR(created_at, 12) >= "15" .and. ATC("lunch", course) > 0 course WITH "Dinner>15"


*SELECT disti clientname, client, SPACE(22) as client_id, canteen, cant_name, date, den_tydne, firma, 0000.00 as cena, 00000.00 as doplnkovy, 
SELECT disti clientname, client, SPACE(22) as client_id,                     date, den_tydne, firma, 0000.00 as cena, 00000.00 as doplnkovy, ;
       00 as  no_pre, 00 as  no_main, 00 as  no_side, 00 as  no_dinner, ;
       00 as ord_pre, 00 as ord_main, 00 as ord_side, 00 as ord_dinner, 000 as ord_lunch, ;
       00 as eat_pre, 00 as eat_main, 00 as eat_side, 00 as eat_dinner, 000 as eat_lunch, SPACE(7) as cant_lun, SPACE(7) as cant_din  ;
       FROM orders INTO CURSOR orders_sum READWRITE 

had.set_had( 44 )
SELECT orders
SCAN FOR ordered + eaten # 0
    SCATTER MEMVAR 
    kantyna_txt = ALLTRIM(STR(m.canteen))
    SELECT orders_sum
    DO CASE 
    CASE ATC("lunch", m.course) > 0
        REPLACE FOR date = m.date .and. client = m.client ord_lunch  WITH ord_lunch  + m.ordered, eat_lunch  WITH eat_lunch  + m.eaten, cant_lun WITH IIF( AT(m.kantyna_txt, cant_lun) = 0, m.kantyna_txt + cant_lun, cant_lun)
        
    OTHERWISE
        REPLACE FOR date = m.date .and. client = m.client ord_dinner WITH ord_dinner + m.ordered, eat_dinner WITH eat_dinner + m.eaten, cant_din WITH IIF( AT(m.kantyna_txt, cant_din) = 0, m.kantyna_txt + cant_din, cant_din)
    ENDCASE 
ENDSCAN 

SELECT orders_sum
REPLACE FOR ord_lunch  > 0 ord_lunch  WITH 1
REPLACE FOR ord_dinner > 0 ord_dinner WITH 1
REPLACE FOR eat_lunch  > 0 eat_lunch  WITH 1
REPLACE FOR eat_dinner > 0 eat_dinner WITH 1

had.set_had( 77 )
SELECT transactions
REPLACE FOR ISNULL(displ_desc) displ_desc WITH "..."

SCAN FOR displ_desc # "Zavodka" .and. amount # 0
    SCATTER MEMVAR 
    SELECT orders_sum
    LOCATE FOR client = m.client .and. date = LEFT(m.trans_at, 10)
    IF FOUND()
        REPLACE doplnkovy WITH doplnkovy + m.amount 
    ELSE
        APPEND BLANK 
        REPLACE clientname WITH m.clientname, client WITH m.client, client_id WITH m.client_id, date WITH m.trans_at, den_tydne WITH dat_den_tydne(m.trans_at), doplnkovy WITH m.amount, firma WITH m.firma
    ENDIF 
ENDSCAN

SELECT orders_sum

****************************                            NASTAVENI CEN PRO PARLAMENT                   **************************** zacatek
SET RELATION TO client INTO clients
REPLACE ALL cena WITH  54.0 ,                            clientname WITH ALLTRIM(clientname) + " (" + ALLTRIM(clients.client_id) + ")"
REPLACE ALL cena WITH  77.0 FOR ATC("dohoda"  , firma) > 0
REPLACE ALL cena WITH  85.0 FOR ATC("UVN"     , firma) > 0
REPLACE ALL cena WITH 112.0 FOR ATC("polic"   , firma) > 0
REPLACE ALL cena WITH  85.0 FOR ATC("drago"   , firma) > 0
REPLACE ALL cena WITH   0.0 FOR ATC("poslanc" , firma) > 0

**         to samy nastavit v autoexec\zavodka do transakci
****************************                            NASTAVENI CEN PRO PARLAMENT                   **************************** konec

INDEX on clientname + STR(client) + date TAG cli_da
RELEASE had

DO pr_report WITH "stravovani_vyuctovani_parlament"

SELECT firma, client, clientname, "" as date, "" as den_tydne, cena, SUM(doplnkovy) as doplnkovy, ;
       SUM(ord_lunch) as ord_lunch, SUM(ord_dinner) as ord_dinner,;
       SUM(eat_lunch) as eat_lunch, SUM(eat_dinner) as eat_dinner ;
       GROUP BY firma, client ORDER BY firma, clientname ;
       FROM orders_sum INTO TABLE orders_sumsum READWRITE 
*           GROUP BY firma, clientname FROM orders_sum INTO CURSOR orders_sumsum READWRITE 

dele for cena = 0 .and. doplnkovy = 0 .and. ord_lunch = 0 .and. ord_dinner = 0 .and. eat_lunch = 0 .and. eat_dinner = 0

DO pr_report WITH "stravovani_odber_parlament_sum"

DO parlament_xml


*     *     *     *     *     *     *     *     *     *     *     *     *     *     
PROCEDURE parlament_xml

* 2019-03-28 - za kazdou firmu je generovan extra soubor
* 2019-04-29 - generovan jeste kontrolni soubor s intervalem generovani
* 2019-04-30 - soubory jsou generovany do (m.exportpath)

#DEFINE CRLF  CHR(13)+CHR(10)
ACTIVATE SCREEN 
SET POINT TO "."

SELECT orders_sumsum
INDEX on clientname TO qwer


xml_exportpath = m.gcexportpath 
xml_exportpath = GETDIR(xml_exportpath, "Vyber adresář, do nej budou exportovány soubory XML", "Výběr adresáře")


log_file = m.xml_exportpath + "stravovani" + ".txt"
STRTOFILE(''                                         , m.log_file)
STRTOFILE("Od data: " + datsk1                 + CRLF, m.log_file, 1)
STRTOFILE("Do data: " + datsk2                 + CRLF, m.log_file, 1)
STRTOFILE("Generovano: " + TTOC(DATETIME(), 3) + CRLF, m.log_file, 1)


SELECT disti firma FROM orders_sumsum INTO CURSOR firmy 
SCAN
    exportfile = m.xml_exportpath + "stravovani_" + ALLTRIM(firmy.firma) + ".xml"
    STRTOFILE('<?xml version="1.0" encoding="UTF-8"?>' + CRLF, m.exportfile)
    ex( "<Stravovani>")
    DO xml_jedna_firma WITH firmy.firma
    ex(  "</Stravovani>" )

    STRTOFILE("Firma: " + ALLTRIM(firmy.firma) + CRLF, m.log_file, 1)
ENDSCAN 


*     *     *     *     *     *     *     *     *     *     *     *     *     *     
PROCEDURE xml_jedna_firma
PARAMETERS firma
ACTIVATE SCREEN 
? m.firma
ex( '<Firma NazevFirmy="' + ALLTRIM(firma) + '">' )
SELECT orders_sumsum
SCAN FOR firma = m.firma
    pozice_zavorky = AT("(", clientname)
    jmeno = ALLTRIM(LEFT(clientname, pozice_zavorky-1))

    jmeno = STRCONV(m.jmeno, 1, 1029)
    jmeno = STRCONV(m.jmeno, 9)

    client_id = ALLTRIM(STRTRAN(SUBSTR(clientname, pozice_zavorky+1), ")", ""))
    ex( '<Stravnik ID="' + ALLTRIM(client_id) + '">' )
    ex( "    " + str2item(m.jmeno, "Jmeno") + str2item(STR(cena,9,2), "Cena") + str2item(STR(doplnkovy,9,2), "Doplnkovy_Prodej") )
    ex( "    " + str2item(STR(ord_lunch,9), "Objednano_obedu") +  + str2item(STR(eat_lunch,9), "Snedeno_obedu") )
    ex( "    " + str2item(STR(ord_dinner,9), "Objednano_veceri") +  + str2item(STR(eat_dinner,9), "Snedeno_Veceri") )
    ex( "</Stravnik>" )
ENDSCAN 
ex( "</Firma>" )


*     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION ex (strng)
STRTOFILE(m.strng + CRLF, m.exportfile, 1)


*     *     *     *     *     *     *     *     *     *     *     *     *     *     
FUNCTION str2item (hodnota, nazev)
RETURN "<" + m.nazev + ">" + ALLTRIM(m.hodnota) + "</" + m.nazev + ">"
