*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
*  podprogramy pro zapis do datastore tabulek clients a client_infos a users
*  typicky po importu nejakeho csv se porovnaji tabulky novych klientu se stavem na webu, rozhodne se, co je treba aktualizovat a to se pak udela
*
* 2018-10-31 - prozatim nemazu klienty
* 2018-11-02 - protoze parsovani dlouhych stringu trva dlouho, stahuje se po strankach
* 2019-01-10 - mazani klientu je deaktivuje a maze jim chip a email
* 2019-01-17 - do mazani se berou jen stravnici s "IMPORTED" v commentu, aby se nemazaly rucne zadane karty
* 2019-01-24 - pokud je v datastore karta se statusem "N" a je nalezena v importnim souboru, je nastavena na "A"
* 2019-02-13 - chybejici mail je fejkovan, protoze v users byt musi
* 2021-08-25 - obsluhuje se i pole address a v company name a ico
* 2022-05-31 - parsovani je zrychleno tim, ze se z json vykousne cely record a parsuje se jen v nem
*
*
*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
PARAMETERS stahnout_data_do_logu
SET PROCEDURE TO (SYS(16)) ADDITIVE 

#DEFINE PAGESIZE  "666"
#DEFINE PAGECNT   9999
#DEFINE ZACATEK   '{"id":'

SET REPROCESS TO AUTOMATIC
DO clos_data

CREATE TABLE (m.gcpri_path + "stravnici_karty_web") FREE ( ;
       id I, client_id C(12), surname C(22) NULL, firstname C(22) NULL, email C(55) NULL, chip_id C(22), card_id C(22), firma C(25) NULL, ico C(10) NULL, heslo C(33) NULL, status C(1), expiration C(10), ;
       id_infos I, c_hlad N(2), pl_c_hlad N(2), limit I, limit_var I, max_denne I null, typ C(2) NULL, typ_slevy N(2) NULL, pl_typ_sl N(2) NULL, sl_koef N(4,2) NULL, pl_sl_koef N(4,2) NULL,  ;
       usr_id I, usr_2name C(22), usr_1name C(22), username C(22), usr_email C(55), has_passwd L, comment C(22) NULL, address C(99) NULL, co_s_tim C(22), response C(99))
INDEX ON id TAG id

* tabulka stravnici_karty se pouziva pri importech klientu z ruznych formatu
CREATE TABLE (m.gcpri_path + "stravnici_karty")     FREE ( ;
       client_id C(12), surname C(22), firstname C(22) , email C(55) NULL, chip_id C(22), card_id C(22), ;
       c_hlad N(2), pl_c_hlad N(2), limit I, limit_var I, max_denne I null, typ C(1) NULL, typ_slevy N(1) NULL, pl_typ_sl N(1) NULL, sl_koef N(4,2) NULL, pl_sl_koef N(4,2) NULL, ;
       firma C(25) NULL, ico C(10) NULL, heslo C(33) NULL, comment C(22) NULL, address C(99) NULL )
INDEX on client_id TAG client_id
SET POINT TO "."

IF .not. EMPTY(m.stahnout_data_do_logu)
    DO stahni_data_do_logu
ENDIF 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
PROCEDURE posli_cloveky

SELECT stravnici_karty_web
SET ORDER TO 

ACTIVATE SCREEN 
? "poslani:"

SCAN FOR ATC("DELETE", co_s_tim) > 0 .and. status # "N" .and. ATC("IMPORTED", comment) > 0
    ?? "D"
    id_txt    = nx(id)
    json = http_request("kredit/crud/clients/" + m.id_txt, "PATCH", '{"status":"N", "email":null, "chip_id":null, "expiration_date":"' + LEFT(TTOC(DATE()-1, 3), 10) + '"}')
    json = http_request("kredit/crud/clients/" + m.id_txt, "DELETE", '')  && kdo mel nejake transakce ci objednavky, smazani se nepovede, coz neva
    *    json = http_request("kredit/crud/users/" + m.usr_id_txt, "DELETE", "")
ENDSCAN 

SCAN 
    client_id = ALLTRIM(client_id)
    id_txt    = nx(id)

    rq_cli =             '{"firstname":' + tx2(firstname) + ', "surname":' + tx2(surname) + ', "email":' + LOWER(tx2(email))
    rq_cli = m.rq_cli + ', "client_id":"' + m.client_id + '", "card_id":"' + m.client_id + '", "chip_id":' + tx2(chip_id) + ', "comment":' + tx2(comment)
    rq_cli = m.rq_cli + ', "expiration_date":"2055-05-05", "status":"A", "company":{"dic":"", "ico":"' + ALLT(ico) + '", "name":' + tx2(firma) + "}"
    IF .not. ISNULL(address) .and. .not. EMPTY(address)
        rq_cli = m.rq_cli + ', "address":' + ALLTRIM(address)
    ENDIF 
    rq_cli = m.rq_cli + ' }'

    rq_inf = ', "typ":' + typ + ', "typ_slevy":' + nx(typ_slevy) + ', "pl_typ_slevy":' + nx(pl_typ_sl) + ', "pl_c_hlad":' + nx(pl_c_hlad) + ', "c_hlad":' + nx(c_hlad) + ', "limit":' + nx(limit) + ', "max_den_odber":' + nx(max_denne) + '}'

    DO CASE 
    CASE ATC("PATCH", co_s_tim) > 0
        ?? "p"
        http_request("kredit/crud/clients/" + m.id_txt, "PATCH", m.rq_cli)
        http_request("kredit/crud/client_infos/" + ALLTRIM(STR(id_infos)), "PATCH", '{"client":' + m.id_txt + m.rq_inf)

        IF ISNULL(email) .or. EMPTY(email)
             REPLACE email WITH ALLT(client_id) + "@fake.cz"
        ENDIF 

        rq_user = '{"first_name":' + tx2(firstname) + ', "last_name":' + tx2(surname) + ', "email":' + LOWER(tx2(email))
        IF .not. EMPTY(heslo)
            rq_user = m.rq_user + ', "username":' + tx(client_id) + ', "password":' + tx(heslo)
        ENDIF 
         rq_user = m.rq_user + '}'
        http_request("auth/crud/users/" + ALLTRIM(STR(usr_id))    , "PATCH", m.rq_user)

    CASE ATC("POST", co_s_tim) > 0
        ?? "P"
        json = http_request("kredit/crud/clients", "POST", m.rq_cli)
        id_txt = ALLTRIM(STR(get_integer_item(m.json, "id")))
        REPLACE id WITH VAL(m.id_txt) 

        IF VAL(m.id_txt) # 0           && zapis do client_infos - nema cenu delat get+post, proste zkusim post, pokud record uz existuje, vrati chybu a nic se nestane
            http_request("kredit/crud/client_infos", "POST", '{"client":' + m.id_txt + m.rq_inf)
        ENDIF 

    ENDCASE 

ENDSCAN 

SCAN FOR usr_id = 0 .AND. ATC("DELETE", co_s_tim) = 0    && stravnikum ke smazani se nepostuje user
    ?? "u"
    IF ISNULL(email) .or. EMPTY(email)
        REPLACE email WITH ALLT(client_id) + "@fake.cz"
    ENDIF 
    rq_user = '{"first_name":' + tx2(firstname) + ', "last_name":' + tx2(surname) + ', "email":' + tx(LOWER(email))
    rq_user = m.rq_user + ', "username":' + tx(client_id) + ', "password":' + tx(heslo) + ', "client":' + nx(id) + ', "is_active":true, "is_staff":false, "is_superuser":false}'
    http_request("auth/crud/users", "POST", m.rq_user)
ENDSCAN 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
PROCEDURE priprav_co_delat
ACTIVATE SCREEN 
? " "
? "priprava: "
SELECT stravnici_karty
REPLACE  FOR AT(".", email) = 0 .or. AT("@", email) = 0 .or. ISNULL(email) email WITH LOWER(ALLTRIM(client_id)) + m.mail_domain
SCAN
   SCATTER MEMVAR 
   SELECT stravnici_karty_web
   LOCATE FOR client_id = m.client_id
   IF FOUND()
        IF ALLTRIM(surname) # ALLTRIM(m.surname) .or. ALLTRIM(firstname) # ALLTRIM(m.firstname)
              REPLACE co_s_tim WITH "PATCH", response WITH "jmeno:" + ALLTRIM(surname) + " " + ALLTRIM(m.firstname), surname WITH m.surname, firstname WITH m.firstname
        ENDIF 

        IF ALLTRIM(chip_id) # ALLTRIM(m.chip_id)
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " chip:" + ALLTRIM(chip_id), chip_id WITH m.chip_id
        ENDIF 

        IF ALLTRIM(email) # ALLTRIM(m.email)
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " email:" + ALLTRIM(email), email WITH m.email
        ENDIF 

        IF ALLTRIM(firma) # ALLTRIM(m.firma)
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " firma:" + ALLTRIM(firma), firma WITH m.firma
        ENDIF 

        IF ALLTRIM(ico) # ALLTRIM(m.ico)
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " ico", firma WITH m.firma, ico WITH m.ico
        ENDIF 

        IF ISNULL(comment) .or. (ALLTRIM(comment) # ALLTRIM(m.comment))
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " comment", comment WITH m.comment
        ENDIF 

        IF (limit # m.limit) .or. (limit_var # m.limit_var)
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " limit ", limit WITH m.limit, limit_var WITH m.limit_var
        ENDIF 

        IF (c_hlad # m.c_hlad) .or. (pl_c_hlad # m.pl_c_hlad)
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " c_hlad", c_hlad WITH m.c_hlad, pl_c_hlad WITH m.pl_c_hlad
        ENDIF 

        IF (sl_koef # m.sl_koef) .or. (pl_sl_koef # m.pl_sl_koef)
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " sl_koef", sl_koef WITH m.sl_koef, pl_sl_koef WITH m.pl_sl_koef 
        ENDIF 

        IF (typ_slevy # m.typ_slevy) .or. (pl_typ_sl # m.pl_typ_sl)
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " typ_slevy", typ_slevy WITH m.typ_slevy, pl_typ_sl WITH m.pl_typ_sl 
        ENDIF 

        IF (max_denne # m.max_denne)
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " max_denne", max_denne WITH m.max_denne
        ENDIF 

        IF (typ # m.typ)
*              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " typ:" + STR(typ,1) + STR(m.typ,2), typ WITH m.typ
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " typ:" + ALLT(typ)                 , typ WITH m.typ
        ENDIF 

        IF status # "A"
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " status"
        ENDIF 

        IF .not. EMPTY(heslo)
              REPLACE co_s_tim WITH "PATCH", response WITH ALLTRIM(response) + " heslo"
        ENDIF 

        REPLACE address WITH m.address

        IF co_s_tim = "PATCH"
              ?? "U"  && musi se patchovat
        ELSE
              REPLACE co_s_tim WITH "nic"
        ENDIF 
        IF ALLTRIM(usr_2name) # ALLTRIM(m.surname) .or. ALLTRIM(usr_1name) # ALLTRIM(m.firstname) .or. ALLTRIM(usr_email) # ALLTRIM(m.email)
             REPLACE co_s_tim WITH ALLTRIM(co_s_tim) + " USERS"
        ENDIF 
   ELSE 
        ?? "I"  && novy radek, musi se postnout
        co_s_tim = "POST"
        INSERT INTO stravnici_karty_web FROM MEMVAR 
   ENDIF 
ENDSCAN 
*    SELECT stravnici_karty_web
*    REPLACE  FOR AT(".", email) = 0 .or. AT("@", email) = 0 .or. ISNULL(email) email WITH nx(id) + m.mail_domain


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
PROCEDURE stahni_webovy_klienty

json = http_get("settings/crud/settings")

stahni ("kredit/crud/clients"      + "?page_size=" + PAGESIZE, "zpracuj_radek_clients()")
stahni ("kredit/crud/client_infos" + "?page_size=" + PAGESIZE, "zpracuj_radek_infos  ()")
stahni ("auth/crud/users"          + "?page_size=" + PAGESIZE, "zpracuj_radek_users  ()")


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE stahni(co_stahnout, co_volat)
zacatek_prace = SECONDS()
ACTIVATE SCREEN 
krok_pocitadla = 1

? PADR(STRTRAN(m.co_volat, "zpracuj_radek_", ""), 12) + "(" + PAGESIZE + ") "
FOR page_no = 1 TO PAGECNT
    json = http_get( m.co_stahnout + "&page=" + ALLTRIM(STR(m.page_no))) + ZACATEK

    IF page_no = 1
        pocet_recordu = get_integer_item (m.json, "count")
        ?? nx(m.pocet_recordu) + " "
        krok_pocitadla = CEILING(pocet_recordu / VAL(PAGESIZE) / 30)
    ENDIF 

    at_nasledujici = ATC(ZACATEK, m.json, 1)
    FOR ii = 2 TO VAL(PAGESIZE) + 3
        at_tenhle      = at_nasledujici
        at_nasledujici = ATC(ZACATEK, m.json, m.ii)
        delka          = at_nasledujici - at_tenhle
        IF m.delka > 0
            radek = SUBSTR(m.json, m.at_tenhle, m.delka)
            &co_volat
        ENDIF 
    ENDFOR 

    pocitej(m.krok_pocitadla, m.page_no)
*    ?? nx(m.page_no) + " "
    next_page = get_string_item (m.json, "next")
    IF ISNULL(m.next_page)
        EXIT 
    ENDIF 
ENDFOR 
?? TRANS(SECONDS() - m.zacatek_prace, "99999") + "sec  "


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION zpracuj_radek_clients
id              = get_integer_item (m.radek, "id"             )
IF m.id # 0
    client_id  = get_string_item  (m.radek, "client_id"       )
    card_id    = get_string_item  (m.radek, "card_id"         )
    chip_id    = get_string_item  (m.radek, "chip_id"         )
    firstname  = get_string_item  (m.radek, "firstname"       )
    surname    = get_string_item  (m.radek, "surname"         )
    email      = get_string_item  (m.radek, "email"           )
    firma      = get_string_item  (m.radek, "name"            )
    ico        = get_string_item  (m.radek, "ico"             )
    status     = get_string_item  (m.radek, "status"          )
    expiration = get_string_item  (m.radek, "expiration_date" )
    comment    = get_string_item  (m.radek, "comment"         )
    address    = get_object_item  (m.radek, "address"         )

    chip_id    = IIF(ISNULL(m.chip_id)   , "", m.chip_id)
    expiration = IIF(ISNULL(m.expiration), "", m.expiration)
    co_s_tim   = "DELETE - neni v CSV"

    INSERT INTO stravnici_karty_web FROM MEMVAR 
ENDIF 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION zpracuj_radek_infos
client         = get_integer_item (m.radek, "client"          )
IF m.client # 0
    id_infos   = get_integer_item   (m.radek, "id"            )
    c_hlad     = get_integer_item   (m.radek, "c_hlad"        )
    pl_c_hlad  = get_integer_item   (m.radek, "pl_c_hlad"     )
    limit      = get_num_item       (m.radek, "limit"         )
    limit_var  = get_num_item       (m.radek, "limit_var"     )
    max_denne  = get_integer_item   (m.radek, "max_den_odber" )
    sl_koef    = VAL(get_string_item(m.radek, "sl_koef"      ))
    pl_sl_koef = VAL(get_string_item(m.radek, "pl_sl_koef"   ))
    typ_slevy  = get_num_item       (m.radek, "typ_slevy"     )
    pl_typ_sl  = get_num_item       (m.radek, "pl_typ_slevy"  )
    typ        = get_string_item    (m.radek, "typ"           )

    SELECT stravnici_karty_web
    LOCATE FOR id = m.client 
    IF FOUND()
        GATHER FIELDS id_infos, limit, limit_var, max_denne, c_hlad, pl_c_hlad, typ_slevy, pl_typ_sl, sl_koef, pl_sl_koef, typ MEMVAR 
    ENDIF 
ENDIF 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION zpracuj_radek_users

client         = get_integer_item (m.radek, "client"      )
IF m.client # 0
    usr_id     = get_integer_item (m.radek, "id"          )
    usr_email  = get_string_item  (m.radek, "email"       )
    usr_1name  = get_string_item  (m.radek, "first_name"  )
    usr_2name  = get_string_item  (m.radek, "last_name"   )
    username   = get_string_item  (m.radek, "username"    )
    has_passwd = get_boolean_item (m.radek, "has_password") 
    SELECT stravnici_karty_web
    LOCATE FOR id = m.client
    IF FOUND()
        GATHER FIELDS usr_id, usr_email, usr_1name, usr_2name, username, has_passwd MEMVAR 
    ELSE 
**    PROZATIM NEMAZU          ? "deactivating user " + m.usr_2name + m.usr_1name, m.usr_id
**    PROZATIM NEMAZU          http_request("auth/crud/users/" + nx(usr_id), "PATCH", '{"is_active":false}')
    ENDIF 
ENDIF 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION zpracuj_radek_transactions

id         =    get_integer_item (m.radek, "id"                        )
descript   =    get_string_item  (m.radek, "description"               )
u_cis      =    iif(isnull(m.descript), "", m.descript)

IF m.id # 0 .and. ATC("VYUCT", m.u_cis) = 0
    client     =       get_integer_item (m.radek, "client"             )
    displ_desc =       get_string_item  (m.radek, "display_description")
    trans_at   =       get_string_item  (m.radek, "transacted_at"      )
    kind       =       get_string_item  (m.radek, "kind"               )
    amount     = - VAL(get_string_item  (m.radek, "amount"            ))
    balance    = - VAL(get_string_item  (m.radek, "balance"           ))
    INSERT INTO transactions FROM MEMVAR 
ENDIF 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION zpracuj_radek_orders

id              = get_integer_item (m.radek, "id"         )
IF m.id # 0
     client     = get_integer_item (m.radek, "client"     )
     meal       = get_integer_item (m.radek, "meal"       )
     ordered    = get_integer_item (m.radek, "ordered"    )
     eaten      = get_integer_item (m.radek, "eaten"      )
     created_at = get_string_item  (m.radek, "created_at" )
     updated_at = get_string_item  (m.radek, "updated_at" )

     ordered = MAX(m.ordered, m.eaten)                       &&&&***** oprava pepinovy chyby
     INSERT INTO orders FROM MEMVAR 
ENDIF 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
PROCEDURE smaz_klienty
FOR ii = 999999624 TO 626
    ii_txt = ALLTRIM(STR(m.ii))
    = http_request("auth/crud/users/"          + ii_txt, "DELETE", '')
    = http_request("kredit/crud/client_infos/" + ii_txt, "DELETE", '')
    = http_request("kredit/crud/clients/"      + ii_txt, "DELETE", '')
ENDFOR 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
PROCEDURE  stahni_data_do_logu
json = http_get("auth/crud/users?page_size=9999"          )
json = http_get("kredit/crud/clients?page_size=9999" )
json = http_get("kredit/crud/client_infos?page_size=9999")
json = http_get("canteen/crud/orders?page_size=9999&filter{created_at.gt}=2018-12-10T00:00")


