*:*****************************************************************************
*: VYROBA_PARLAMENT.PRG
*:*****************************************************************************



*:*****************************************************************************
PROCEDURE def_menu_vyroba

IF VARTYPE(omezeny_uzivatel) = "U"
    PUBLIC omezeny_uzivatel
ENDIF

#DEFINE NEVYVOLENI_UZIVATELE  "__XXA__XXB__XXC__XXD__"
omezeny_uzivatel = ATC(m.gcuser_id, NEVYVOLENI_UZIVATELE ) > 0


DEFINE BAR 11 OF vyroba PROMPT "Jídelní lístek"        SKIP FOR m.omezeny_uzivatel
DEFINE BAR 22 OF vyroba PROMPT "Sestavy"               SKIP FOR m.omezeny_uzivatel
DEFINE BAR 33 OF vyroba PROMPT "Odpuštění objednávky"  SKIP FOR m.omezeny_uzivatel
DEFINE BAR 44 OF vyroba PROMPT "Strávníci"
DEFINE BAR 55 OF vyroba PROMPT "Nulovaní kreditu"      SKIP FOR m.omezeny_uzivatel

ON SELECTION BAR 11 OF vyroba DO zavolej_jidelak IN vyroba_parlament

IF m.issql
*    ON SELECTION BAR 11 OF vyroba DO FORM jidelak_parlament_sql
    ON SELECTION BAR 22 OF vyroba DO FORM stravovani_sestavy
    ON SELECTION BAR 33 OF vyroba DO      odpusteni_objednavky
    ON SELECTION BAR 44 OF vyroba DO FORM stravnici
    ON SELECTION BAR 55 OF vyroba DO FORM nulovani_limitu

ELSE
*    ON SELECTION BAR 11 OF vyroba DO FORM jidelak_parlament
    ON SELECTION BAR 22 OF vyroba DO FORM stravovani_sestavy
    ON SELECTION BAR 33 OF vyroba DO      odpusteni_objednavky
    ON SELECTION BAR 44 OF vyroba DO FORM stravnici
    ON SELECTION BAR 55 OF vyroba DO FORM nulovani_limitu

ENDIF

SET PROC TO stravovani_ppgm ADDI
DO stahni_chody_a_kantyny


*:*****************************************************************************
PROCEDURE zavolej_jidelak


IF .not. FILE(m.gchom_path + "jidelak.dbf")

                                                 ***********************   jidelak_import()   ***********************
    DO stahni_webovej_jidelak

    SELECT *, " " as s_o_v, {} as datum, number as cislojidla, LEFT(name, 30) as nazev ;
           FROM (m.gchom_path + "jidelak_web") INTO TABLE (m.gcpri_path + "jidelak_web_2")
    SCAN
        TRY  
            REPLACE datum WITH DATE(VAL(LEFT(date, 4)), VAL(SUBSTR(date,6,2)), VAL(SUBSTR(date,9,2)))
        CATCH
        ENDTRY 
    ENDSCAN 

    REPLACE s_o_v WITH "S" FOR course = m.course_S
    REPLACE s_o_v WITH "O" FOR course = m.course_O
    REPLACE s_o_v WITH "P" FOR course = m.course_P
    REPLACE s_o_v WITH "V" FOR course = m.course_V

    MESSAGEBOX("zakladam novy lokalni soubor" + CHR(13) + (m.gchom_path + "jidelak.dbf"), 1+64, "Jidelak parlament", 5522)
    CREATE TABLE (m.gchom_path + "jidelak") FREE ;
           (datum D, s_o_v C(1), cislojidla N(2), c_karty I, nazev C(30), spart C(3), canteen I, cislopril N(2), name C(250), descript C(250))
    APPEND FROM (m.gcpri_path + "jidelak_web_2")
ENDIF 

clos_data()
DO FORM jidelak_parlament


*:*****************************************************************************
PROCEDURE odpusteni_objednavky

DO clos_data
s_trz_dat1 = DATE() - 35
s_trz_dat2 = DATE() + 14

datsk1 = LEFT(TTOC(s_trz_dat1, 3), 10)
datsk2 = LEFT(TTOC(s_trz_dat2, 3), 10)

DO get_data_stravy IN stravovani_ppgm


SELECT disti clientname, client FROM orders WHERE ordered > eaten INTO CURSOR cloveci
DEFINE POPUP cloveci FROM 3,55 PROMPT FIELD clientname
ON SELECTION POPUP cloveci deacti POPUP 
ACTIVATE POPUP cloveci
IF .not. EMPTY(PROMPT())
    clovek_id = cloveci.client

    SELECT disti date as druh, .f. as lbl FROM orders WHERE client = m.clovek_id AND ordered > eaten INTO CURSOR seldruhy READWRITE 
    DO FORM sel_date
    SCAN FOR lbl
        SCATTER MEMVAR 

        SELECT orders
        SCAN FOR client = m.clovek_id .and. date = m.druh
            ? id, ALLTRIM(clientname), date, ALLTRIM(STR(ordered)) + " / " + ALLTRIM(STR(eaten))
            json = http_request("canteen/crud/orders/" + ALLTRIM(STR(orders.id)), "PATCH", '{"ordered":0}')

            *v transactions.dbf musim najit, zda se odpousti jidlo, ktere je nauctovano transakci, v tom pripade by se muselo do transakci zapsat storno


cena=22.22
            rq_trans =            '{' + '"client":' + nx(m.clovek_id) + ',"kind":"KC", "transacted_at":"' + m.druh + 'T22:22:22"' 
            rq_trans = rq_trans + ', "description":"STORNO ' + m.druh + '", "display_description":"Zavodka"' + ', "amount":' + nx(cena) + ' }'
*            http_request('kredit/crud/historic_transactions', "POST", rq_trans)

            DO zurnal_log WITH "OPERA", "ODPUSTENI OBJEDNAVKY", STR(id) + " " + ALLTRIM(clientname) + " " + m.druh
        ENDSCAN 
    ENDSCAN 
ENDIF 
DO clos_data



********** vraci string s uvozovkami nahrazenymi apostrofy + uvozovky  **********
FUNCTION tx2 (retez)
RETURN '"' + ALLTRIM(STRTRAN(IIF(ISNULL(m.retez), "", m.retez), '"', "'")) + '"'

********** vraci string bez mezers uvozovkami nahrazenymi apostrofy + uvozovky  **********
FUNCTION tx (retez)
retval = STRTRAN(m.retez, " ", "")
RETURN '"' + ALLTRIM(STRTRAN(retval, '"', "'")) + '"'

********** vraci string s uvozovkami nahrazenymi apostrofy  **********
FUNCTION xx (retez)
RETURN ALLTRIM(STRTRAN(retez, '"', "'"))

