*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
* protoze nenabijeji karty, je mozno nulovat podle tabulky clients
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2019-05-24 <#       *    *    *    *    *    *    *    *    *    *    *    *    *

SET REPROCESS TO AUTOMATIC
DO clos_data

*HTTP_READ_ONLY_MODE = .T.


IF VARTYPE(vysledek) = "C"
    vysledek = m.vysledek + "  v.2019-05-24"
ELSE 
    vysledek = "  v.2019-05-24"
ENDIF 


o_set_point = SET("POINT")
SET POINT TO "."

DO (LOWER(JUSTPATH(sys(16))) + "\http_ppgm") WITH "https://alto:1991.Altopraha@ceskatelevize-backend.alto-production.mine.nu/"

hranice_konec = "2019-06-01T00:00:00"

DO get_clients

? "POSTing VYUCTOVANI "
SELECT clients
SCAN
    IF RECNO() % 11 = 0
        ?? "."
    ENDIF 

    rq = '{ "client":' + ALLTRIM(STR(id,11)) + ', "kind":"KC", "transacted_at":"' + hranice_konec + '", "balance":0, "description":"VYUCT", "display_description":"Vyuctovani"}'
    json = http_request("kredit/crud/historic_transactions", "POST", m.rq)
ENDSCAN 

vysledek = m.vysledek + "    vynulovano do " + m.hranice_konec
SET POINT TO (m.o_set_point)
oznam_hotovo()


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_clients

#DEFINE PAGESIZE 666
? "Clients "

CREATE TABLE (m.gcpri_path + "clients") FREE  (id I, firstname C(22), surname C(22), client_id C(11), expiration C(10) ) 
INDEX on id TAG id

FOR page_no = 1 TO 9999
    json = http_get( 'kredit/crud/clients?page_size=' + ALLTRIM(STR(PAGESIZE))+ '&page=' + ALLTRIM(STR(m.page_no)) )
    ?? ALLTRIM(STR(m.page_no)) + " "
    FOR ii = 1 TO PAGESIZE+3
       id          = get_integer_item (m.json, "id"             , ii)
       firstname   = get_string_item  (m.json, "firstname"      , ii)
       surname     = get_string_item  (m.json, "surname"        , ii)
       client_id   = get_string_item  (m.json, "client_id"      , ii)
       expiration  = get_string_item  (m.json, "expiration_date", ii)
       IF m.id # 0
          INSERT INTO clients FROM MEMVAR 
       ENDIF 
    ENDFOR 

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