*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
*    program je volan z formulare nulovani_limitu
*
*    2019-03-04 - nuluji se jen klienti, koncici dane obdobi zapornym balancem
*    2019-04-14 - nuluji se zvlast vsechny hodnoty sloupce KIND (tedy pravdepodobne KC a POI)
*    2020-07-14 - adresa datastore je definovana v menu_def_vyroba
*
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2020-07-14 <#       *    *    *    *    *    *    *    *    *    *    *    *    *

PARAMETERS hranice_konec_date

ACTIVATE SCREEN 
DO ppgm_http 

SET REPROCESS TO AUTOMATIC
DO clos_data
o_set_point = SET("POINT")
SET POINT TO "."

DO nulovani_limitu2

SET POINT TO (m.o_set_point)
oznam_hotovo()


*       *       *       *       *       *       *       *      *       *       *       *       *       *       *       *     
PROCEDURE nulovani_limitu2

hranice_zacatek = TTOC(m.hranice_konec_date-77, 3)
hranice_konec   = TTOC(m.hranice_konec_date   , 3)

CREATE TABLE (m.gcpri_path + "last_transaction") FREE ( client I, kind C(11), trans_at C(19), balance N(11,2) ) 
INDEX ON client TAG client

* nalezeni casu predchoziho vyuctovani
filtr = '&filter{transacted_at.range}=' + hranice_zacatek + '&filter{transacted_at.range}=' + hranice_konec + '&filter{description}=VYUCT'
DO stahni_transakce
SELECT transactions
IF RECCOUNT() > 0
    SET ORDER TO trans_at 
    GO TOP 
    hranice_zacatek = LEFT(transactions.trans_at, 18) + "1Z"    && vyuctovani se dela v cele minute, takhle se pricte jedna vterina
ENDIF 

filtr = '&filter{transacted_at.range}=' + hranice_zacatek + '&filter{transacted_at.range}=' + hranice_konec
DO stahni_transakce
*                          nalezeni posledni transakce pro kazdy kind zvlast
SELECT transactions
SCAN 
    SCATTER MEMVAR 
    SELECT last_transaction
    LOCATE FOR (client = m.client) .AND. (kind = m.kind)
    IF FOUND()
        IF m.trans_at > trans_at
            GATHER FIELDS trans_at, balance
        ENDIF 
    ELSE 
        INSERT INTO last_transaction FROM MEMVAR 
    ENDIF 
ENDSCAN 

? "POSTing VYUCTOVANI "
SELECT last_transaction
SCAN FOR balance < 0
    pocitej(33)
    rq = '{ "client":' + ALLTRIM(STR(client,11)) + ', "kind":"' + ALLTRIM(kind) + '", "transacted_at":"' + hranice_konec + '", "balance":0, "description":"VYUCT", "display_description":"Vyuctovani ' + ALLTRIM(kind) + '"}'
    json = http_request("kredit/crud/historic_transactions", "POST", m.rq)
ENDSCAN 


*       *       *       *       *       *       *       *      *       *       *       *       *       *       *       *     
PROCEDURE stahni_transakce
#DEFINE PAGESIZE 777
? "transactions " + ALLTRIM(STR(pagesize)) + " " + m.filtr
? ""
CREATE TABLE (m.gcpri_path + "transactions") ;
             (id I, client I, kind C(9), amount N(9,2), balance N(9,2), descript C(7), trans_at C(19), disp_descr C(22) NULL , priznak C(14))
INDEX on trans_at TAG trans_at DESCENDING 


FOR page_no = 1 TO 9999
    json = http_get("kredit/crud/transactions?page_size=" + ALLTRIM(STR(PAGESIZE))+ "&page=" + ALLTRIM(STR(m.page_no)) + m.filtr)
    next_page = get_string_item (m.json, "next")
    pocet_radku = json_rowcount(json)
    ?? nx(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)
       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))
       disp_descr =    get_string_item  (m.json, "display_description" , ii)
       
       IF m.id # 0
          INSERT INTO transactions FROM MEMVAR 
       ENDIF 
    ENDFOR 

    IF ISNULL(m.next_page)
        EXIT 
    ENDIF 
ENDFOR 
