*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
*
*     generator clientu + users do datastore pro objednavani ve Lvim dvore
*     na zacatku stahne clients, nahodne generuje nova ID a pokud neexistuji, zapise je do clients, client_infos (velmi usporne) a users
*
*  2022-04-20 - 
*
*
*
*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    


DO ppgm_http 

*HTTP_READ_ONLY_MODE = .T.

#DEFINE COUNTRYCODE "77"
#DEFINE POCET_KARET 22

stahni_klienty()
rand(-99)
ACTIVATE SCREEN 
? "Generating .."
FOR iii = 1 TO POCET_KARET
    cislo_cloveka = COUNTRYCODE + TRANSFORM(rand() * 999999, "@L 999999")
    LOCATE FOR client_id = m.cislo_cloveka
    IF .not. FOUND()
        IF vyrob_cloveka()
            ?? "."
            INSERT INTO stravnici_web (client_id, poznamka) VALUES (m.cislo_cloveka, "PRIDAN TED")
        ENDIF 
    ELSE 
        ?? "-"
    ENDIF 
ENDFOR 
SELECT stravnici_web 
SET FILTER TO poznamka = "PRIDAN TED"
DO pr_report WITH "generovani_stravnici"

CLOSE DATABASES 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
FUNCTION vyrob_cloveka ()

*client: {"id":1,"info":1,"user":2,"status":"A","client_id":"1565","card_id":"1565","chip_id":"00001312BAFF","email":"1565@fejk.cz","firstname":"AntonUn","surname":"Admek","comment":"IMPORTED (Elfetex)","news":false,"daily_menu":false,"expiration_date":"2055-05-05","personal":{},"company":{"dic":"","ico":"2","name":"05- Poboka PlzeÚ"},"address":{},"address2":{},"created_at":"2021-05-27T11:55:48.506756Z","updated_at":"2022-03-24T14:37:20.768761Z"},
*clinfo: {"id":1,"client":1,"typ":"1","limit":0,"limit_var":0,"limit_den":0,"sl_limit":0,"sl_limit_mes":0,"c_hlad":0,"pl_c_hlad":0,"typ_slevy":0,"pl_typ_slevy":2,"sl_koef":"0.00","pl_sl_koef":"0.00","omez_platba":false,"k_vyplate":false,"rod_cis":null,"id_vlastnik":null,"pr_pozice":null,"st_autor":null,"rezerva":null,"profil":null,"max_den_odber":0,"akt_den_odber":null,"id_omezeni":null,"posl_poh_nul":null,"posl_poh_arc":null,"id_firmy":null,"id_zuct":null,"sl_min_cast":null,"d_zm_status":null,"d_konec_kred":null,"d_posl_odber":null,"created_at":"2021-05-27T11:55:48.679577Z","updated_at":"2022-03-24T14:37:20.831467Z"},
*user:   {"id":2,"username":"1565","first_name":"AntonUn","last_name":"Admek","email":"1565@fejk.cz","is_active":true,"is_staff":false,"is_superuser":false,,"has_password":true,"groups":["clients","diners"],"client":1,"pin":null},

vysledek_vyroby = .T.
STORE m.cislo_cloveka TO client_id, card_id, chip_id, surname, password, m.username
email = m.cislo_cloveka + "@fake.cz"

rq_cli =            '{ "card_id":"' + m.card_id + '", "client_id":"' + m.client_id + '", "chip_id":"' + m.chip_id + '", "surname":"' + m.surname + '"'
rq_cli = m.rq_cli + ', "status":"A", "expiration_date":"2055-05-05", "comment":"GENERATED (Lvi dvur)" }'

json = http_request("kredit/crud/clients", "POST", m.rq_cli)
IF .not. json_ok(m.json)
    MESSAGEBOX(m.json, 1+16, "Nepodarilo se zapsat stravnika - clients")
    vysledek_vyroby = .F.
ELSE 
    cl_id = get_integer_item(m.json, "id")
    rq_info =             '{ "client":' + nx(m.cl_id) + ' }'
    json = http_request("kredit/crud/client_infos", "POST" , m.rq_info)
    IF .not. json_ok(m.json)
        MESSAGEBOX(m.json, 1+16, "Nepodarilo se zapsat stravnika - client_infos")
        vysledek_vyroby = .F.
    ENDIF 

    rq_user = '{ "username":"' + m.username + '", "last_name":"' + m.surname + '", "email":"' + m.email + '", "password":"' + m.password +  '", "is_active":true, "is_staff":false, "is_superuser":false, "client":' + nx(m.cl_id) + ' }'
    json = http_request("auth/crud/users", "POST" , m.rq_user)
    IF .not. json_ok(m.json)
        MESSAGEBOX(m.json, 1+16, "Nepodarilo se zapsat stravnika - users")
    vysledek_vyroby = .F.
    ENDIF 
ENDIF 
RETURN m.vysledek_vyroby



*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
PROCEDURE stahni_klienty

#DEFINE PAGESIZE  "250"

CREATE TABLE (m.gcpri_path + "stravnici_web") FREE ( id I, client_id C(22), poznamka C(11) )
INDEX ON client_id TAG client_id

zacatek_prace = SECONDS()
? "clients (" + PAGESIZE + ") "
FOR page_no = 1 TO 9999
    json = http_get( "kredit/crud/clients?page_size=" + PAGESIZE + "&page=" + ALLTRIM(STR(m.page_no)))
    next_page = get_string_item (m.json, "next")
    ?? nx(m.page_no) + " "
    FOR ii = 1 TO VAL(PAGESIZE) + 3
       id          = get_integer_item (m.json, "id"              , ii)
       client_id   = get_string_item  (m.json, "client_id"       , ii)

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

    IF ISNULL(m.next_page)
        EXIT 
    ENDIF 
ENDFOR 
?? STR(SECONDS() - m.zacatek_prace) + "sec"



