*************************************************************************************
*
*   program zapise v parlamentu do transakci odber zavodni stravy podle orders
*
*   2019-10-17 - vydeje po 15. hod se berou jako vecere
*   2023-09-18 - zmeneny ceny pro HPP a dohody
*              - pousti se i v SQL verzi, zadna foodovska data to stejne nezpracovava
*   2024-01-08 - pri nenastavene adrese datastoru nic nedela
*              - prom. m.datastore_add je uz nastavena drive
*   2024-02-06 - zmeneny ceny
*
*************************************************************************************

IF m.datastore_add = "neniii"
    WAIT "Neni nastavena adresa datastoru pro zapis zavodky do transakci" WINDOW TIMEOUT 3
    RETURN 
ENDIF 

IF n_slock ("ZAPIS_TRANSAKCI")

    s_trz_dat2 = DATE() - 1
    s_trz_dat1 = s_trz_dat2 - 9

    datsk1 = LEFT(TTOC(s_trz_dat1, 3), 10)          && &&     "2019-09-25T15:53"
    datsk2 = LEFT(TTOC(s_trz_dat2, 3), 10)

    DO get_data_stravy IN stravovani_ppgm

    SELECT orders
    REPLACE FOR LEFT(updated_at, 10) = LEFT(created_at, 10) .and. SUBSTR(created_at, 12) >= "15" .and. ATC("lunch", course) > 0 course WITH "Dinner>15"

    SELECT disti clientname, client, date, den_tydne, firma, 0000.00 as cena, ;
           00 as  no_pre, 00 as  no_main, 00 as  no_side, 00 as  no_dinner, ;
           00 as ord_pre, 00 as ord_main, 00 as ord_side, 00 as ord_dinner, 000 as ord_lunch, ;
           00 as eat_pre, 00 as eat_main, 00 as eat_side, 00 as eat_dinner, 000 as eat_lunch  ;
           FROM orders INTO CURSOR orders_sum READWRITE 

    SELECT orders
    SCAN
        SCATTER MEMVAR 
        SELECT orders_sum
        DO CASE 
        CASE ATC("lunch", m.course) > 0
            REPLACE FOR date = m.date .and. clientname = m.clientname ord_lunch  WITH ord_lunch  + m.ordered, eat_lunch  WITH eat_lunch  + m.eaten
        OTHERWISE
            REPLACE FOR date = m.date .and. clientname = m.clientname ord_dinner WITH ord_dinner + m.ordered, eat_dinner WITH eat_dinner + m.eaten
        ENDCASE 
    ENDSCAN 

    SELECT orders_sum
    REPLACE FOR ord_lunch  > 0 ord_lunch  WITH 1
    REPLACE FOR ord_dinner > 0 ord_dinner WITH 1
    REPLACE FOR eat_lunch  > 0 eat_lunch  WITH 1
    REPLACE FOR eat_dinner > 0 eat_dinner WITH 1

    REPLACE ALL cena WITH  54.0
    REPLACE ALL cena WITH  77.0 FOR ATC("dohoda"  , firma) > 0
    REPLACE ALL cena WITH  85.0 FOR ATC("UVN"     , firma) > 0
    REPLACE ALL cena WITH 112.0 FOR ATC("polic"   , firma) > 0
    REPLACE ALL cena WITH  85.0 FOR ATC("drago"   , firma) > 0
    INDEX on clientname + date TAG cli_da

    DO zapis_transakce
ENDIF 
= n_sunlock ("ZAPIS_TRANSAKCI")


*************************************************************************************
PROCEDURE zapis_transakce

SELECT orders_sum
SCAN FOR ord_lunch > 0             && obedy
    SCATTER MEMVAR 
    popis = 'OBED ' + SUBSTR(m.date, 6)
    SELECT transactions
    LOCATE FOR client = m.client .and. kind = "POI" .and. m.popis $ descript
    IF .not. FOUND()
        WAIT ALLT(m.clientname) +  '  OBED ' + m.date WINDOW NOWAIT 
        bylo_odebrano = IIF(ord_lunch > eat_lunch, "neodebran", "")
        rq_trans =            '{' + '"client":' + nx(m.client) + ',"kind":"POI", "transacted_at":"' + m.date + 'T12:00:00"' 
        rq_trans = rq_trans + ', "description":"' + m.popis + m.bylo_odebrano + '", "display_description":"Zavodka"' + ', "amount":' + nx(-cena) + ' }'
        http_request('kredit/crud/historic_transactions', "POST", rq_trans)
    ENDIF 
ENDSCAN 

SELECT orders_sum
SCAN FOR ord_dinner > 0           && vecere
    SCATTER MEMVAR 
    popis = 'VECERE ' + SUBSTR(m.date, 6)
    SELECT transactions
    LOCATE FOR client = m.client .and. kind = "POI" .and. m.popis $ descript
    IF .not. FOUND()
        WAIT ALLT(m.clientname) +  '  VECERE ' + m.date WINDOW NOWAIT 
        bylo_odebrano = IIF(ord_dinner > eat_dinner, "neodebran", "")
        rq_trans =            '{' + '"client":' + nx(m.client) + ',"kind":"POI", "transacted_at":"' + m.date + 'T19:00:00"' 
        rq_trans = rq_trans + ', "description":"' + m.popis + m.bylo_odebrano + '", "display_description":"Zavodka"' + ', "amount":' + nx(-cena) + ' }'
        http_request('kredit/crud/historic_transactions', "POST", rq_trans)
    ENDIF 
ENDSCAN 
SELECT orders_sum
WAIT CLEAR 
