*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*
*    program stahne z datastore meals a orders a pro course = "dinner" udela nasledujici:
*    jidlum na dalsi dny nastavi show_on_we = false
*    jidlum na dnes nastavi show_on_we = true, po prekroceni poctu ordered pres cislo v cislopril opet false
*    datum "dnes" se otaci ve 13:00
*    
*    2022-12-06
*    2022-12-08  - show_on_web se v 10:30 nastavi na true, aby kasa nabizela vecere k odberu
*    2022-12-12  - zobrazuji se pocty zbyvajicich porci
*    
*    
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  

#DEFINE VERZE "ver. 2022-12-12"
#DEFINE JMENO_LOGU "pocitadlo_velim"

DO clos_data
ACTIVATE SCREEN 
CLEAR 
small_log (VERZE, JMENO_LOGU)

DO ppgm_http 
SET PROCEDURE TO ppgm_stravnici ADDITIVE 

IF TIME() < "13:00"
    dnes = LEFT(TTOC(DATE()  , 3), 10)
ELSE
    dnes = LEFT(TTOC(DATE()+1, 3), 10)        && odpo se uz resi zobrazeni zitrejsiho datumu
ENDIF 

DO get_courses      IN stravovani_ppgm
DO get_meals_dnes
DO get_orders_dnes
small_log ("getnuto", JMENO_LOGU)

SELECT meal, SUM(ordered) as ordered, SUM(eaten) as eaten GROUP BY meal FROM orders INTO CURSOR orders_sum
SET RELATION TO meal INTO meals
REPLACE ALL meals.ordered WITH ordered, meals.eaten WITH eaten
USE 

SELECT meals
SCAN 
    DO CASE 
    CASE date = m.dnes .and. ordered < cislopril
        show_on_web_to_be = .T.
        hlaska = "  SHOW"
        retez = '"show_on_web":true, "name":"' + left(name, 77)  + ' (' + allt(str(cislopril - ordered)) + ')" }'

    CASE date = LEFT(TTOC(DATE(), 3), 10) .and. TIME() > "10:30"
        show_on_web_to_be = .T.
        hlaska = "  SHOW JQ"
        retez = '"show_on_web":true, "name":"' + left(name, 77) + ' (' + allt(str(cislopril - ordered)) + ')" }'

    OTHERWISE                             &&& date > m.dnes nebo ordered dosahlo limitu, ktery je v cislopril
        show_on_web_to_be = .F.
        hlaska = "  hide"
        retez = '"show_on_web":false, "name":"' + name + '" }'

    ENDCASE 
    small_log (right(meals.date, 5) + " " + LEFT(strtr(meals.name, " ", ""), 11) + " " + allt(STR(meals.ordered, 3)) + "/" + allt(STR(meals.cislopril, 3)) + m.hlaska, JMENO_LOGU)
    
    IF show_on_web_to_be .or. show_onweb = .T.        && pokud se nemelo zobrazovat a dal se taky nema, patch se neposila
        small_log (SPACE(33) + m.retez, JMENO_LOGU)
        json = http_request("canteen/crud/meals/" + ALLT(STR(id)), "PATCH", '{"additional_course":null, ' + m.retez)
    ENDIF

ENDSCAN 
oznam_hotovo()


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_orders_dnes

CREATE TABLE (m.gcpri_path + "orders") FREE ;
       (id I, client I, meal I, ordered I, eaten I, created_at C(16), updated_at C(16), canteen I, course C(15), number I, name C(30), c_karty I, date C(10) )
INDEX ON id TAG id

stahni ( 'canteen/crud/orders?filter{meal.date.eq}=' + m.dnes + '&filter{meal.course.eq}=dinner&page_size=3333', "zpracuj_radek_orders()" )


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_meals_dnes

? "Meals "
CREATE TABLE (m.gcpri_path + "meals") FREE  (id I, date C(10), canteen I, course C(15), number I, c_karty I, name C(99), ordered I, eaten I, show_onweb L, cislopril N(2) )
INDEX ON id TAG id

json = http_get( 'canteen/crud/meals?filter{date.gte}=' + m.dnes + '&filter{course.eq}=dinner&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)
       cislopril  = get_integer_item (m.json, "additional_course_default_number", ii)

       IF m.id # 0
          INSERT INTO meals FROM MEMVAR 
       ENDIF 
    ENDFOR 
ENDIF 
