*************************************************************************************
*   program zapise v elfetexu do transakci neodberanou zavodni stravu podle orders
*
*   2022-01-26 - 
*
*************************************************************************************

IF n_slock ("ZAPIS_TRANSAKCI")

    IF m.datastore_add = "neniii"
        datastore_add = get_ini("datastores", "*"   , "neniii", m.gchom_path + "datastores_adresses.ini")
    ENDIF 

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

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

    DO get_data_stravy IN stravovani_ppgm

    SELECT orders
    DELETE FOR c_karty = 538        && bez polevky
    DELETE FOR eaten # 0
    DELETE FOR ordered = 0
    SELECT client, date, SUM(cena) as cena GROUP BY 1,2 FROM orders INTO TABLE (m.gcpri_path + "orders_sum")

    DO zapis_transakce
ENDIF 
= n_sunlock ("ZAPIS_TRANSAKCI")


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

SELECT orders_sum
SCAN 
    SCATTER MEMVAR 
    popis = 'NEODEBRAN ' + SUBSTR(m.date, 6)
    SELECT transactions
    LOCATE FOR client = m.client .and. kind = "KC" .and. m.popis $ descript
    IF .not. FOUND()
        WAIT m.popis WINDOW NOWAIT 
        rq_trans =            '{' + '"client":' + nx(m.client) + ',"kind":"KC", "transacted_at":"' + m.date + 'T12:00:00"' 
        rq_trans = rq_trans + ', "description":"' + m.popis + '", "display_description":"Zavodka"' + ', "amount":' + STRTRAN(ALLTRIM(STR(-cena,7,2)), ",", ".") + ' }'
        http_request('kredit/crud/historic_transactions', "POST", rq_trans)
    ENDIF 
ENDSCAN 

WAIT CLEAR 
