*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*
*    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 - ZRUSENO
*    2022-12-12  - zobrazuji se pocty zbyvajicich porci
*    2022-12-30  - zobrazuji se pocty zbyvajicich porci v kolonce description
*    2023-01-30  - omezeno spousteni jen na databaza THERESIA
*    
*    
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  

#DEFINE VERZE "ver. 2023-01-30"
#DEFINE JMENO_LOGU "pocitadlo_velim"


IF ATC("THERESIA", m.sqldatabaza) = 0
    WAIT "Pro data " + m.sqldatabaza + " se tohle nepousti" WINDOW TIMEOUT 3
ENDIF 


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 

get_meals_dnes ()
get_orders_dnes()

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"

        descript = "Zbývá: " + ALLTRIM(STR(cislopril - ordered)) + " porce"
        IF cislopril - ordered > 4
            descript = STRTR(m.descript, "porce", "porcí")
        ENDIF
        retez = '"show_on_web":true, "description":"<span style=\"display: block; text-align: center;\"><b>' + m.descript + '</b></span>"'

    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'

    ENDCASE 
    small_log (right(meals.date, 5) + " " + LEFT(strtr(meals.name, " ", ""), 9) + " " + 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
*    IF show_on_web_to_be # show_onweb       && takhle to nejde, neaktualizovalo by se pocitadlo
        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), descript C(250), 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)
       descript   = get_string_item (m.json, "description"                      , 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 
