*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
*   program z transakci z kavovaru vyrobi 2 ucty, ktere zapise do SK s datumem posledniho dne minuleho mesice
*   transakce se ctou s filtrem jen kavovarovych, takze to trva jen chvilku
*   pres zapisem do SK se kontroluje, zda uzaverka neexistuje v sk_sez, ale pri appendu do ostatnich se radky mazou, takze pokud je treba importovat znovu, staci smazat jen sk_sez
*
*  2020-08-22 - predelano jako specialita
*  2020-12-15 - opraven sklad hl.jidelny z AC na 4A
*  2022-02-02 - do fin se vyplni i u_cas
*  2022-09-30 - pridany nove volby
*  2022-11-03 - pridan kavovar K5 se stejnymi volbami jako K4, ale na stredisko D (zpravodajstvi 24)
*  2022-11-03 - pridana platba bankovni kartou na kavovaru (client Kartovnik)
*  2024-04-01 - zmenena sazba dph na 21%
*
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2024-04-01 <#       *    *    *    *    *    *    *    *    *    *    *    *    *

IF DAY(DATE()) > 11
    WAIT "tohle se ma poustet jen prvni dny v mesici, holenku" WINDOW TIMEOUT 3
    RETURN 
ENDIF 

#DEFINE CLIENT_MINCOVNIK 4237    && karta Mincovniku
#DEFINE CLIENT_KARTOVNIK 7044    && karta Kartovniku

uct_den = DATE(YEAR(DATE()), MONTH(DATE()), 1) - 1

uzaverka = SUBSTR(DTOS(m.uct_den), 3) + " 23:99"

u_cas    = DTOS(m.uct_den) + "239999"
? m.uzaverka, m.uct_den

get_transactions_z_kavovaru()

SELECT id, SPACE(7) as u_cis, client, kind, amount, descript, ;
       00000 as volba, "  " as id_kas, "  " as id_man, 0000000 as c_karty, 1 as pocet, "kavovar " + descript as nazev ;
       FROM transactions_kafe WHERE "Z:" $ descript .and. "V:" $ descript INTO TABLE (m.gcpri_path + "kavovary")

DO vypln_c_karty

REPLACE FOR client = CLIENT_MINCOVNIK u_cis WITH id_kas + "00001"
REPLACE FOR client = CLIENT_KARTOVNIK u_cis WITH id_kas + "00003"
REPLACE FOR EMPTY(u_cis)              u_cis WITH id_kas + "00002"

SELECT id_kas, id_man, c_karty, u_cis, SUM(pocet) as mnozstvi, SUM(amount) as prachy, 1.21 as dan, "1" as cen_hlad, "*" as k_odpoctu, m.uzaverka as uzaverka, nazev ;
       GROUP BY 1,2,3,4 FROM kavovary INTO TABLE (m.gcpri_path + "kavovary_do_sk_pol")
REPLACE FOR atc(",P:" , nazev) > 8 nazev with left(nazev, atc(",P:" , nazev)-1)

SELECT uzaverka, u_cis, id_kas, "CZK" as mena, "K_HOTEL2  " as druh_pl, SUM(prachy) as cena_pl, SUM(prachy) as cena_mena, SUM(prachy) as cena_celk, SUM(prachy) as cena_slev, ;
       9977.7 as stul, "*K*" as autor, "_PLATBA_NEFISKALNI_" as fik, "_PLATBA_NEFISKALNI_" as bkp, m.u_cas as u_cas, "Kavovary" as text_sleva ;
       GROUP BY 1,2 FROM kavovary_do_sk_pol INTO TABLE (m.gcpri_path + "kavovary_do_sk_plt")
REPLACE FOR "00001" $ u_cis druh_pl WITH "Hotove"
REPLACE FOR "00003" $ u_cis druh_pl WITH "KAR_KAFE"

SELECT DISTI id_kas, id_man, "1" as c_mena, m.uzaverka as uzaverka, "*K*" as autor, m.uct_den as uct_den ;
       GROUP BY 1,2 FROM kavovary_do_sk_pol INTO TABLE (m.gcpri_path + "kavovary_do_sk_sez")

*****DO kavovary_do_sk


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
PROCEDURE kavovary_do_sk

USE (m.gccen_path + "sk_sez")  AGAIN
LOCATE FOR uzaverka = m.uzaverka
IF FOUND()
    WAIT "Uz byly kavovary naimportovany" WINDOW TIMEOUT 5
ELSE 
    DELETE FOR uzaverka = m.uzaverka
    APPEND FROM (m.gcpri_path + "kavovary_do_sk_sez")

    USE (m.gccen_path + "sk_pol")  AGAIN 
    DELETE FOR uzaverka = m.uzaverka
    APPEND FROM (m.gcpri_path + "kavovary_do_sk_pol")

    USE (m.gccen_path + "sk_fin")  AGAIN
    DELETE FOR uzaverka = m.uzaverka
    APPEND FROM (m.gcpri_path + "kavovary_do_sk_plt")

    USE (m.gccen_path + "sk_plt")  AGAIN
    DELETE FOR uzaverka = m.uzaverka
    APPEND FROM (m.gcpri_path + "kavovary_do_sk_plt")
ENDIF 


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
PROCEDURE  vypln_c_karty

REPLACE ALL volba WITH VAL(SUBSTR(descript, ATC("V:", descript)+2))
SET FILTER TO "Z:K1" $ descript
REPLACE ALL id_kas WITH "B1", id_man WITH "4B"
REPLACE FOR volba = 3 c_karty WITH 1063    && 31.-

SET FILTER TO "Z:K4" $ descript
REPLACE ALL id_kas WITH "C1", id_man WITH "4C"
REPLACE FOR volba = 1 c_karty WITH 1063    && 31.-
REPLACE FOR volba = 2 c_karty WITH 1058    && 28.-
REPLACE FOR volba = 3 c_karty WITH 1025    && 21.-
REPLACE FOR volba = 4 c_karty WITH 1056    && 30.-
REPLACE FOR volba = 5 c_karty WITH 1025    && 21.-
REPLACE FOR volba = 6 c_karty WITH 0       && jen horka voda
REPLACE FOR volba = 7 c_karty WITH 1052    && 21.-
REPLACE FOR volba = 8 c_karty WITH 1025    && 21.-

SET FILTER TO "Z:K5" $ descript
REPLACE ALL id_kas WITH "D1", id_man WITH "4D"
REPLACE FOR volba = 1 c_karty WITH 1063    && 31.-
REPLACE FOR volba = 2 c_karty WITH 1058    && 28.-
REPLACE FOR volba = 3 c_karty WITH 1025    && 21.-
REPLACE FOR volba = 4 c_karty WITH 1056    && 30.-
REPLACE FOR volba = 5 c_karty WITH 1025    && 21.-
REPLACE FOR volba = 6 c_karty WITH 0       && jen horka voda
REPLACE FOR volba = 7 c_karty WITH 1052    && 21.-
REPLACE FOR volba = 8 c_karty WITH 1025    && 21.-

SET FILTER TO "Z:L1" $ descript
REPLACE ALL id_kas WITH "A1", id_man WITH "4A"
REPLACE FOR volba =  1 c_karty WITH 7907
REPLACE FOR volba =  2 c_karty WITH 7908
REPLACE FOR volba =  3 c_karty WITH 7909
REPLACE FOR volba =  4 c_karty WITH 7910
REPLACE FOR volba =  5 c_karty WITH 7911
REPLACE FOR volba =  6 c_karty WITH 7912
REPLACE FOR volba =  7 c_karty WITH 7913
REPLACE FOR volba =  8 c_karty WITH 7914
REPLACE FOR volba =  9 c_karty WITH 7924
REPLACE FOR volba = 10 c_karty WITH 7925
REPLACE FOR volba = 11 c_karty WITH 7926
REPLACE FOR volba = 12 c_karty WITH 7927
REPLACE FOR volba = 13 c_karty WITH 7928
REPLACE FOR volba = 14 c_karty WITH 7929
REPLACE FOR volba = 15 c_karty WITH 7930
REPLACE FOR volba = 16 c_karty WITH 7931
REPLACE FOR volba = 17 c_karty WITH 7918
REPLACE FOR volba = 18 c_karty WITH 7919
REPLACE FOR volba = 19 c_karty WITH 7921
REPLACE FOR volba = 20 c_karty WITH 7920
REPLACE FOR volba = 21 c_karty WITH 7917
REPLACE FOR volba = 22 c_karty WITH 7915
REPLACE FOR volba = 23 c_karty WITH 7922
REPLACE FOR volba = 24 c_karty WITH 7923

SET FILTER TO 
REPLACE FOR atc(",P:" , nazev) > 8 nazev with left(nazev, atc(",P:" , nazev)-1)

SET FILTER TO c_karty = 0 
COUNT to pocet_nul
IF m.pocet_nul > 0
    SELECT DISTINCT nazev, amount FROM kavovary WHERE c_karty = 0 INTO CURSOR nulovenazvy
    msg_txt = "Tyto volby na automatech nemaji nadefinovan prevod do Foodu, informujte Evzena: "
    SCAN
        msg_txt = m.msg_txt + CHR(13) + ALLTRIM(nazev) + STR(amount, 7,1)
    ENDSCAN 
    MESSAGEBOX(m.msg_txt, 1+32,"import kavovaru")
    vysledek = IIF( VARTYPE(vysledek) = "C", m.vysledek, "" ) + msg_txt 

    SELECT kavovary    
    REPLACE FOR c_karty = 0 c_karty WITH 100000 + volba
ENDIF 


*     *     *     *     *     *     *     *     *     *     *     *     *     *     *     *     
PROCEDURE get_transactions_z_kavovaru
#DEFINE PAGESIZE 555

DO clos_data
ACTIVATE SCREEN 
DO ppgm_http WITH "https://alto:1991.Altopraha@ceskatelevize-backend.production-c.asrv.cz/"

mesicek = LEFT(TTOC(m.uct_den, 3), 8)  && 2018-11-

CREATE TABLE (m.gcpri_path + "transactions_kafe") FREE ;
       (id I, client I, kind C(9), amount N(9,2), balance N(9,2), u_cis C(7), descript C(44) null, trans_at C(16))

? "Transactions (" + ALLTRIM(STR(pagesize)) + ") "
FOR page_no = 1 TO 9999

    konec_requestu = '&filter{description.icontains}=Z&filter{description.icontains}=V'
    json = http_get("kredit/crud/transactions?page_size=" + ALLTRIM(STR(PAGESIZE))+ "&page=" + ALLTRIM(STR(m.page_no)) + "&filter{transacted_at.startswith}=" + m.mesicek + '&filter{description.icontains}=Z&filter{description.icontains}=V')

    pocitej(1, m.page_no)
    FOR ii = 1 TO PAGESIZE+3
       id         =    get_integer_item (m.json, "id"                 , ii)
       client     =    get_integer_item (m.json, "client"             , ii)
       descript   =    get_string_item  (m.json, "description"        , ii)
       u_cis      =    m.descript
       trans_at   =    get_string_item  (m.json, "transacted_at"      , ii)
       kind       =    get_string_item  (m.json, "kind"               , ii)
       amount     = - VAL(get_string_item (m.json, "amount"           , ii))
       balance    = - VAL(get_string_item (m.json, "balance"          , ii))
       IF m.id # 0 .and. ATC("VYUCT", m.u_cis) = 0
          INSERT INTO transactions_kafe FROM MEMVAR 
       ENDIF 
    ENDFOR 

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

oznam_hotovo()
