*      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      
*
*  v parlamentu se klapky z preddefinovane stranky posilaji jako meal do datastore, aby mohly byt zapsany jako vydana zavodka
*
* 2022-05-04
*
*      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      

IF ATC("D_SNEM", m.gchom_path) = 0 
    RETURN 
ENDIF 

IF .not. INLIST(m.gcuser_id, "SUP", "ADM")
    RETURN 
ENDIF 

WAIT "Čekejte, jídelníček se posílá do " + m.datastore_add WINDOW NOWAIT 

*#DEFINE STRANA_KLAPEK_ZAVSTR 17
#DEFINE STRANA_KLAPEK_ZAVSTR  10

#DEFINE CANTEEN1 "1"
#DEFINE CANTEEN2 "2"

#DEFINE KASA1   "06"
#DEFINE KASA2   "01"


* {"id":2012,"canteen":2,"course":"lunch_main","additional_course":null,"name":"vep.plec bratislavska 150 gpor","description":"","date":"2022-05-04","number":23,"show_on_web":false,"stock_card_number":3410,"additional_course_default_number":null},
*             ********     ***************                               *************                                            ********            *******                           ****************

json_datum = LEFT(TTOC(DATETIME(), 3), 10)

ACTIVATE SCREEN 

DO ppgm_http WITH m.datastore_add
DO get_meals_klap

SELECT MAX(number) AS cislo FROM Meals INTO CURSOR max_cislo WHERE course = "lunch_main" 
IF RECCOUNT() = 0
     cislo = 20
ELSE
     GO TOP 
     cislo = MAX(max_cislo.cislo, 20)        && cislovani od 21
ENDIF 

SELECT sqlklapky              &&  SELECT klapky

hladajid = KASA1
REQUERY("sqlklapky")
SCAN FOR strana = STRANA_KLAPEK_ZAVSTR .and. typ = "item"
    radek_jidla = '{"name": "' + ALLTRIM(caption1) + '", "stock_card_number":' + ALLTRIM(STR(c_karty)) + ', "canteen":' + CANTEEN1 
    zbytek()
ENDSCAN

hladajid = KASA2
REQUERY("sqlklapky")
SCAN FOR strana = STRANA_KLAPEK_ZAVSTR .and. typ = "item"
    radek_jidla = '{"name": "' + ALLTRIM(caption1) + '", "stock_card_number":' + ALLTRIM(STR(c_karty)) + ', "canteen":' + CANTEEN2
    zbytek()
ENDSCAN
WAIT CLEAR  


*!*                    SELECT klapky
*!*                    SCAN FOR klapky.strana = m.strana_klapek_zavstr AND klapky.typ = "item"
*!*                           radek_jidl = '{"name": "' + ALLTRIM(klapky.nazev) + '", "stock_card_number":' + ALLTRIM(STR(klapky.c_karty)) + ', "additional_course": '
*!*                           radek_jidl = radek_jidl +               'null, "additional_course_default_number": null'
*!*                        radek_jidl = radek_jidl +                ', "show_on_web":"FALSE"'
*!*                        SELECT myMeals
*!*                        LOCATE FOR myMeals.c_karty = klapky.c_karty AND !myMeals.ShowOnWeb    && Zmena 19.11.2018, kachna z jidelaku a mimo jidelak je treba mit v mealsech 2x
*!*                        IF !FOUND()
*!*                            * novy radek, musi se postnout
*!*                            m.cislo = m.cislo+1
*!*                            radek_jidl = radek_jidl + ', "number": "' + ALLTRIM(STR(m.cislo)) + '", "date": "' + m.json_datum + '", "course": "' + "lunch_main" + '", "canteen":' + ALLTRIM(STR(m.mygnIdCanteen)) + '}'
*!*                            json = http_request("canteen/crud/meals", "POST", m.radek_jidl)
*!*                        ENDIF 
*!*                    ENDSCAN



*      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      
PROCEDURE zbytek
SELECT Meals
LOCATE FOR c_karty = c_karty AND .not. show_onweb    && Zmena 19.11.2018, kachna z jidelaku a mimo jidelak je treba mit v mealsech 2x
IF .not. FOUND()
     cislo = m.cislo + 1
     radek_jidla = m.radek_jidla + ', "number":' + ALLTRIM(STR(m.cislo)) + ',"course":"lunch_main", "additional_course":null, "description":"", "show_on_web":false, "additional_course_default_number":null, "date": "' + m.json_datum + '" }'
     json = http_request("canteen/crud/meals", "POST", m.radek_jidla)
ENDIF 


*      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      
PROCEDURE get_meals_klap

? "Meals "
CREATE TABLE (m.gcpri_path + "meals") FREE  (id I, date C(10), den_tydne C(3), canteen I, course C(15), number I, c_karty I, name C(99), ordered I, eaten I, cour_name C(33), course_ord I, cant_name C(44), show_onweb L )
INDEX ON id TAG id

json = http_get( 'canteen/crud/meals?filter{date}=' + m.json_datum + '&filter{course}=lunch_main&page_size=999')

pocet_radku = json_rowcount(json)
IF pocet_radku > 0
    FOR ii = 1 TO m.pocet_radku
       id        = get_integer_item (m.json, "id"                , ii)
       canteen   = get_integer_item (m.json, "canteen"           , ii)
       number    = get_integer_item (m.json, "number"            , ii)
       c_karty   = get_integer_item (m.json, "stock_card_number" , ii)
       course     = get_string_item (m.json, "course"            , ii)
       date       = get_string_item (m.json, "date"              , ii)
       name       = get_string_item (m.json, "name"              , ii)
       show_onweb = get_boolean_item (m.json, "show_on_web"      , ii)
        IF m.id # 0
          INSERT INTO meals FROM MEMVAR 
       ENDIF 
    ENDFOR 
ENDIF 
