*       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       
*   
*   2018-08-30 - get_orders pouziva filter na meals.date, takze precte jednim getem cely den
*   2020-07-14 - adresa datastore (prom. datastore_add) je definovana v menu_def_vyroba
*   2021-02-01 - cte se i field TYP z client_infos
*   2021-06-15 - get_clients_balances necte balances, jen clients+info, pak je nabidnuto menu firem a az potom se ctou z datastore balances
*   2022-04-21 - pokud je v clients isnul firstname nebo surnama, nahrazeno ""
*              - zkracena funkce dat_den_tydne
*              - get_orders nerequestuje jednotlive dny samostatne
*   2022-05-31 - get_transactions a get_transactions pouziva novou proceduru stahni() v ppgm_stravnici.prg
*   2022-12-14 - get_courses sama vola get_course_groups a get_canteens
*              - mealsy a ordersy jsou rozsireny o nazvy skupin chodu
*   2023-06-08 - stahni_vebovej_jidelak ma zvetsenu pagesize
*
*       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       



*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_data_stravy

#DEFINE PAGESIZE "555"
#DEFINE POCET_REQUESTU 9999      &&  jen pro zrychleni ladeni, jinak ma byt 9999

WAIT PROGRAM() + "  " + datsk1 + " - " + datsk2  WINDOW AT 22,22 NOWAIT 

DO clos_data
ACTIVATE SCREEN 
CLEAR 

DO ppgm_http 
SET PROCEDURE TO ppgm_stravnici ADDITIVE 


DO get_clients      IN stravovani_ppgm
DO get_client_infos IN stravovani_ppgm

DO get_courses      IN stravovani_ppgm
DO get_meals        IN stravovani_ppgm
DO get_orders       IN stravovani_ppgm
DO get_transactions IN stravovani_ppgm

SELECT 0
oznam_hotovo()


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_clients_balances

WAIT PROGRAM() WINDOW AT 22,22 NOWAIT 
DO clos_data
ACTIVATE SCREEN 

DO ppgm_http WITH m.datastore_add

DO get_clients      IN stravovani_ppgm
DO get_client_infos IN stravovani_ppgm

IF "AQUAPARK" $ upper(m.gchom_path)
    sele clients
    locate for typ = "2" .and. firma # "<<walet>>"
    DO WHILE FOUND()
        firstnamesurname = firstname + surname
        repl for firstname + surname = m.firstnamesurname firma with "<<walet>>"
        locate for typ = "2" .and. firma # "<<walet>>"
    ENDDO 
ENDIF

**DO get_balances     IN stravovani_ppgm

SELECT 0
oznam_hotovo()

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE otevri_databaze
DO clos_data
USE (m.gcpri_path + "clients")      EXCLUSIVE IN 0 ORDER id
USE (m.gcpri_path + "orders")       EXCLUSIVE IN 0 ORDER id
USE (m.gcpri_path + "meals")        EXCLUSIVE IN 0 ORDER id
USE (m.gcpri_path + "canteens")     EXCLUSIVE IN 0 ORDER id
USE (m.gcpri_path + "transactions") EXCLUSIVE IN 0
USE (m.gcpri_path + "courses")      EXCLUSIVE IN 0 ORDER name
SELECT 0


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_clients

? "Clients      (" + PAGESIZE + ") "

CREATE TABLE (m.gcpri_path + "clients") FREE  (id I, firstname C(22) NULL, surname C(22) NULL, client_id C(12), card_id C(32), firma C(25) NULL, address C(99) NULL, poc_naroku I, zb_naroku I, typ C(1), typ_slevy I, exp_date C(10) NULL)
INDEX ON id TAG id

IF VARTYPE(netahat_z_datastore) = "C" .and. ATC("clients_z_hotkred", m.netahat_z_datastore) > 0
    ?? " z hot_kred.dbf"
    SELECT idklienta as id, prijmeni as surname, krestjm as firstname, firma, cis_kred as client_id, cis_kred as card_id FROM (m.gchom_path + "hot_kred") INTO TABLE (m.gcpri_path + "zroutiii")
    SELECT clients
    APPEND FROM (m.gcpri_path + "zroutiii")
    ?? "."
ELSE 

    FOR page_no = 1 TO POCET_REQUESTU
        json = http_get( 'kredit/crud/clients?page_size=' + PAGESIZE + "&page=" + ALLTRIM(STR(m.page_no)) )
        pocitej(1, m.page_no)
        FOR ii = 1 TO VAL(PAGESIZE) + 3
           id        = get_integer_item (m.json, "id"             , ii)
           firstname = get_string_item  (m.json, "firstname"      , ii)
           surname   = get_string_item  (m.json, "surname"        , ii)
           client_id = get_string_item  (m.json, "client_id"      , ii)
           card_id   = get_string_item  (m.json, "card_id"        , ii)
           firma     = get_string_item  (m.json, "name"           , ii)
           address   = get_object_item  (m.json, "address"        , ii)
           exp_date  = get_string_item  (m.json, "expiration_date", ii)

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

        next_page = get_string_item (m.json, "next")
        IF ISNULL(m.next_page)
            EXIT 
        ENDIF 
    ENDFOR 
ENDIF 

SELECT clients
REPLACE FOR ISNULL(firstname) firstname WITH ""
REPLACE FOR ISNULL(surname)   surname   WITH ""
REPLACE FOR ISNULL(firma) .or. EMPTY(firma) firma WITH "<< nevyplnena >>"
REPLACE FOR ISNULL(exp_date) .or. EMPTY(exp_date) exp_date WITH "2222-22-22"
REPLACE FOR exp_date < TTOC(DATE() - 444, 3) firstname WITH LOWER(ALLTRIM(firstname)) + " ZRUSEN"

TRY 
    EXPORT FIELDS firstname, surname, client_id, card_id, firma TYPE xls TO (m.gcexportpath + "klienti")
CATCH
ENDTRY 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_client_infos

? "Client_infos (" + PAGESIZE + ") "
FOR page_no = 1 TO POCET_REQUESTU
    json = http_get("kredit/crud/client_infos?page_size=" + PAGESIZE + "&page=" + ALLTRIM(STR(m.page_no)))
    pocitej(1, m.page_no)
    FOR ii = 1 TO VAL(PAGESIZE) + 3
       id_infos  = get_integer_item (m.json, "id"        , ii)
       client    = get_integer_item (m.json, "client"    , ii)
       typ       = get_string_item  (m.json, "typ"       , ii)
       typ_slevy = get_integer_item (m.json, "typ_slevy" , ii)

       IF m.client # 0
          SELECT clients
          LOCATE FOR id = m.client 
          IF FOUND()
             GATHER FIELDS typ, typ_slevy MEMVAR 
          ENDIF 
       ENDIF 
    ENDFOR 

    next_page = get_string_item (m.json, "next")
    IF ISNULL(m.next_page)
        EXIT 
    ENDIF 
ENDFOR 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_orders

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, cant_name C(33), course C(33), group_name C(44), number I, name C(30), c_karty I, name_long C(99), clientname C(66), client_id C(12), date C(10), u_cis C(7), u_cas C(4), den_tydne C(3), firma C(33) NULL, cena Y NULL )
INDEX ON id TAG id

IF VARTYPE(netahat_z_datastore) = "C" .and. ATC("orders", m.netahat_z_datastore) > 0
    * nestahuje se
ELSE

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

    stahni ( 'canteen/crud/orders?filter{meal.date.gte}=' + m.datsk1 + '&filter{meal.date.lte}=' + m.datsk2 + '&page_size=' + PAGESIZE, "zpracuj_radek_orders()" )

    SELECT orders
    SET RELATION TO meal INTO meals, client INTO clients
    REPLACE ALL canteen WITH meals.canteen, cant_name WITH meals.cant_name, course WITH meals.course, number WITH meals.number, date WITH meals.date, name WITH meals.name, name_long with meals.name, c_karty WITH meals.c_karty, group_name WITH meals.group_name, cena WITH meals.cena, ;
                firma WITH clients.firma, clientname WITH ALLTRIM(clients.surname) + " " + ALLTRIM((clients.firstname)), client_id WITH ALLTRIM(clients.client_id)
    SET RELATION TO 
    REPLACE ALL den_tydne WITH dat_den_tydne(date)

    IF  FILE(m.gchom_path + "hk_dspolozky.dbf") .and. FILE(m.gccen_path + "sk_fin.dbf")
        ??                  "hk_dspolozky "
        sele 0
        USE (m.gchom_path + "hk_dspolozky.dbf")
        inde on dsordersid to (m.gcpri_path + "dsordersid")

        USE (m.gccen_path + "sk_fin") in 0
        sele u_cis, u_cas from sk_fin where uzaverka >= subst(ttoc(m.s_trz_dat1-1, 1), 3, 6) into table sk_fin2
        inde on u_cis tag "u_cis"
        use in sk_fin

        SELECT orders 
        SCAN
            SCATTER FIELDS id MEMVAR 
            SELECT hk_dspolozky
            LOCATE FOR dsordersid = m.id
            SELECT orders 
            REPLACE u_cis WITH hk_dspolozky.u_cis
            IF .not. EMPTY(orders.u_cis)
                SELECT sk_fin2
                LOCATE FOR u_cis = orders.u_cis
                SELECT orders 
                REPLACE u_cas WITH SUBSTR(sk_fin2.u_cas, 9)      && 20181116121053
            ENDIF 

        ENDSCAN 
        DO pozavirej WITH "hk_dspolozky, sk_fin2"
        ?? "hk_dspolozky hotovy"
    ENDIF 
ENDIF 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION dat_den_tydne(ddd)

TRY 
    IF VARTYPE(ddd) = "D"
        downum = DOW(m.ddd, 2)
    ELSE
        downum = DOW(DATE(VAL(LEFT(m.ddd,4)), VAL(SUBST(m.ddd,6,2)), VAL(SUBST(m.ddd,9,2))), 2)
    ENDIF 
CATCH
    downum = 9
ENDTRY 
*                  1234567890123456789
#DEFINE DNY_TYDNE "-PoÚtStČtPáSoNe--------"
RETURN SUBSTR(DNY_TYDNE, m.downum * 2, 2)


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_meals

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

json = http_get( 'canteen/crud/meals?filter{date.range}=' + m.datsk1 + '&filter{date.range}=' + m.datsk2 + '&page_size=9999')

pocet_radku = json_rowcount(json)
IF pocet_radku > 0

    json = STRTRAN(m.json, '\"', "'")

    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)

       IF AT('<b>', m.descript) > 0
           descript = STRTRAN(SUBSTR(m.descript, AT('<b>', m.descript) + 3), ",", ".")
           cena     = VAL(m.descript)
       ELSE 
           cena     = 0
       ENDIF 

       IF m.id # 0
          INSERT INTO meals FROM MEMVAR 
       ENDIF 
    ENDFOR 
ENDIF 
SET RELATION TO course INTO courses
REPLACE ALL course_ord WITH courses.order_no, cour_name WITH courses.disp_name, group WITH courses.group, group_name WITH courses.group_name
SET RELATION TO canteen INTO canteens
REPLACE ALL cant_name WITH canteens.cant_name
REPLACE ALL den_tydne WITH dat_den_tydne(date)


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_canteens

CREATE TABLE (m.gcpri_path + "canteens") FREE (id I, cant_name C(33), registerno I)
INDEX ON id TAG id

json = http_get('canteen/crud/canteens')
pocet_radku = json_rowcount(json)

seznam_kantyn = "Kantyna "
*{"id":1,"name":"Jidelna 1","register_number":-101,"discriminator_html":"","discriminator_height":null},

FOR ii = 1 TO m.pocet_radku
   id         = get_integer_item  (m.json, "id"             , ii)
   cant_name  = get_string_item   (m.json, "name"           , ii)
   registerno = get_integer_item  (m.json, "register_number", ii)
   IF m.id # 0
      INSERT INTO canteens FROM MEMVAR 
      seznam_kantyn = seznam_kantyn + ALLTRIM(cant_name) + " "
   ENDIF 
ENDFOR 

publikuj("seznam_kantyn_txt", m.seznam_kantyn)


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_course_groups 

json = http_get('canteen/crud/course_groups')
pocet_radku = json_rowcount(json)

*{"name":"breakfast","display_name":"Snídaně","served_at":"08:00:00","lock_before":"00:00:00","order_number":1},

FOR ii = 1 TO m.pocet_radku
   group      = get_string_item  (m.json, "name"         , ii)
   disp_name  = get_string_item  (m.json, "display_name" , ii)
   order_no   = get_integer_item (m.json, "order_number", ii)
   served_at  = get_string_item  (m.json, "served_at"   , ii)
   lock_befor = get_string_item  (m.json, "lock_before" , ii)
   IF .not. EMPTY(m.group)
      INSERT INTO courses FROM MEMVAR 
      REPLACE FOR group = m.group group_name WITH m.disp_name, served_at WITH m.served_at, lock_befor WITH m.lock_befor 
   ENDIF 
ENDFOR 

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_courses

CREATE TABLE (m.gcpri_path + "courses")  FREE (name C(33), group C(33), disp_name C(44), group_name C(44), order_no I, is_addit L, served_at C(8), lock_befor C(8) )
INDEX on name TAG name

json = http_get('canteen/crud/courses')
pocet_radku = json_rowcount(json)

*{"name":"breakfast" ,"group":"breakfast","display_name":"Snídaně"         ,"order_number":1,"is_additional":false},
*{"name":"dinner"    ,"group":"dinner"   ,"display_name":"Večeře"          ,"order_number":9,"is_additional":false},
*{"name":"lunch_main","group":"lunch"    ,"display_name":"Hlavní jídla"    ,"order_number":6,"is_additional":false},
*{"name":"lunch_pre" ,"group":"lunch"    ,"display_name":"Polévky a saláty","order_number":5,"is_additional":false},
*{"name":"lunch_side","group":"lunch"    ,"display_name":"Přílohy"         ,"order_number":7,"is_additional":true}

FOR ii = 1 TO m.pocet_radku
   name      = get_string_item  (m.json, "name"         , ii)
   group     = get_string_item  (m.json, "group"        , ii)
   disp_name = get_string_item  (m.json, "display_name" , ii)
   is_addit  = get_boolean_item (m.json, "is_additional", ii)
   order_no  = get_integer_item (m.json, "order_number" , ii)
   IF .not. EMPTY(m.name)
      INSERT INTO courses FROM MEMVAR 
   ENDIF 
ENDFOR 

get_course_groups()
get_canteens()


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_transactions

CREATE TABLE (m.gcpri_path + "transactions") FREE ;
       (id I, client I, kind C(9), amount N(9,2), balance N(9,2), u_cis C(7), descript C(44) null, displ_desc C(44) null, trans_at C(16), client_id C(12), clientname C(66), text_sleva C(22), cena_slev N(9,2), cena_celk N(9,2), cena_podpo N(9,2), u_cas C(14), platba C(33), firma C(25) NULL, priznak C(14))

stahni ( "kredit/crud/transactions?filter{transacted_at.range}=" + m.datsk1 + "T00:00:00Z&filter{transacted_at.range}=" + m.datsk2 + "T23:59:59Z" + "&page_size=" + PAGESIZE, "zpracuj_radek_transactions()" )

SELECT transactions
SET RELATION TO client INTO clients
REPLACE ALL client_id WITH clients.client_id, clientname WITH ALLTRIM(clients.surname) + " " + ALLTRIM((clients.firstname)), firma WITH clients.firma
INDEX ON u_cis TAG u_cis
INDEX ON id    TAG id

SELECT transactions
TRY 
EXPORT FIELDS kind, amount, balance, u_cis, descript, trans_at, client_id, clientname, firma ;
       TYPE xls TO (m.gcexportpath + "transakce")
CATCH
ENDTRY 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_balances

ACTI SCREE 
? "Zustatky "
o_set_point = SET("point")
SET POINT TO "."
CREATE TABLE (m.gcpri_path + "balances") FREE ;
       (id I, firstname C(22), surname C(22), client_id C(12), firma C(25) NULL, balances C(222), balanc_kc Y, balanc_poi Y, typ C(1)) 
SELECT clients
SCAN
    pocitej(10, RECNO())
    SCATTER MEMVAR 
    client_id = ALLTRIM(STR(clients.id))

* { "http_status":"200 OK", "balances":{"KC":"89.00"}}
    json = http_get ("kredit/get_balances?client_id=" + m.client_id + "&at=" + m.datsk2 + "T23:59:59Z")
    balances = get_object_item (m.json, "balances")

    balanc_kc  = get_1_balance("KC" )
    balanc_poi = get_1_balance("POI")
    
    IF m.balanc_poi # 0 .or. balanc_kc  # 0
        INSERT INTO (m.gcpri_path + "balances") FROM MEMVAR     
    ENDIF 

ENDSCAN 
SET POINT TO (m.o_set_point)


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION get_1_balance (kind)
balance_txt  = get_string_item (m.json, m.kind)
IF ISNULL( m.balance_txt )
    RETURN 0
ELSE
    RETURN VAL( balance_txt ) + 0.0001
ENDIF 


*:*****************************************************************************
FUNCTION s_o_v2course (s_o_v)
DO CASE
CASE m.s_o_v = "S"
         RETURN PADR(m.course_S, 15)
CASE m.s_o_v = "V"
         RETURN PADR(m.course_V, 15)
CASE m.s_o_v = "P"
         RETURN PADR(m.course_P, 15)
OTHERWISE 
         RETURN PADR(m.course_O, 15)
ENDCASE 


*:*****************************************************************************
PROCEDURE napln_name_a_description

otevreno_ok = .T.
TRY  
    SELECT 0
    IF m.issql
        otevira_se = "rktexty"
        rv("ktexty", "ktexty")
    ELSE
        otevira_se = m.fn_ktexty
        USE (m.fn_ktexty) ALIAS ktexty
    ENDIF 
CATCH 
    otevreno_ok = .F.
ENDTRY 

IF m.otevreno_ok

    SELECT jidelak
    SCAN FOR datum > DATE()-3
        SCATTER FIELDS c_karty MEMVAR 
        SELECT ktexty
        LOCATE FOR c_karty = m.c_karty
        IF FOUND()
            IF .not. ISNULL(meno1)
                SELECT jidelak
                REPLACE name WITH STRTRAN(ALLTRIM(ktexty.meno1), '"', "'")
            ENDIF 
            SELECT ktexty
            IF .not. ISNULL(meno2)
                IF meno2 # meno1
                    SELECT jidelak
                    REPLACE descript WITH ktexty.meno2
                ELSE 
                    SELECT jidelak
                    REPLACE descript WITH ""
                ENDIF 
            ENDIF 
        ELSE
            WAIT jidelak.nazev + "  neni v textech" WINDOW TIMEOUT 1 NOWAIT  
        ENDIF 
    ENDSCAN
    USE IN ktexty
ENDIF 

REPLACE FOR EMPTY(canteen) canteen WITH 1
REPLACE FOR EMPTY(name)    name    WITH nazev
REPLACE ALL name     WITH LEFT(name    , 123)
REPLACE ALL descript WITH LEFT(descript, 123)

*:*****************************************************************************
PROCEDURE stahni_webovej_jidelak

DO stahni_chody_a_kantyny

CREATE TABLE (m.gchom_path + "jidelak_web") FREE ;
       (id I, date C(10), canteen I, course C(15), add_course C(15) NULL, number I, c_karty I, name C(250), descript C(250), cislopril I NULL, show_onweb L, co_s_tim C(25), radek_jidl C(254), response C(254) )
json        = http_get( 'canteen/crud/meals?filter{date.gte}=' + LEFT(TTOC(DATE()-2, 3), 10) + '&page_size=3333')
pocet_radku = json_rowcount(json)
? m.pocet_radku
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)
   cislopril = get_integer_item  (m.json, "additional_course_default_number", ii)

   course     = get_string_item  (m.json, "course"                          , ii)
   add_course = get_string_item  (m.json, "additional_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)
   co_s_tim   = "DELETE - neni v jidelaku"
   IF m.id # 0
      INSERT INTO jidelak_web FROM MEMVAR 
   ENDIF 
ENDFOR 
