*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *
*
* program zapise producty na datastore
*
*  2022-12-07 - nazvy se posilaji z ktexty.meno1
*  2022-12-07 - nazvy produktu se posilaji v delce 64 zn. (protoze datastore vic nevezme), uvozovka nahrazena apostrofem 
*
*    *    *    *    *    *    *    *    *    *    *    *      #> ver. 2023-01-12 <#       *    *    *    *    *    *    *    *    *    *    *    *    *
#DEFINE PGM_VERZE                                               "ver. 2023-01-12"


#DEFINE CRLF         CHR(13)+CHR(10)
#DEFINE PAGESIZE     555
#DEFINE JMENOLOGU    "PRODS2DATASTORE"

small_log(PGM_VERZE, JMENOLOGU)
DO (LOWER(JUSTPATH(sys(16))) + "\ppgm_http")

SET REPROCESS TO AUTOMATIC
DO clos_data
ACTIVATE SCREEN 

vezmi_karty()

stahni_products()

DELETE FOR c_druhk = 0

REPLACE FOR name = name_web              co_s_tim WITH "nic"
REPLACE FOR name # name_web .and. id # 0 co_s_tim WITH "PATCH"

posli_products()

oznam_hotovo()
small_log("hotovo", JMENOLOGU)


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE vezmi_karty

small_log(PROGRAM(), JMENOLOGU)
IF m.issql

    DO open_database    
*    rv("ktexty", "ktexty")

    lsql =             "SELECT c_karty  , nazev      , c_druhk   FROM food600.karty WHERE c_data=?m.cdata AND nazev1<>?m.smazana_karta "
    sql_select(lsql, "karty", "c_karty I, nazev C(30), c_druhk I ")

    lsql =              "SELECT c_karty  , meno1      , meno2 FROM food600.ktexty WHERE c_data=?m.cdata "
    sql_select(lsql, "ktexty", "c_karty I, meno1 C(64), meno2 C(64) ")

    SELECT karty.*, ktexty.meno1 as name, ktexty.meno2, SPACE(64) as name_web, 00000000000 as id, "POST  " as co_s_tim  FROM karty LEFT JOIN ktexty ON ktexty.c_karty = karty.c_karty INTO TABLE products

    REPLACE FOR ISNULL(name) .or. EMPTY(name) name WITH nazev
    REPLACE ALL name WITH STRTRAN(ALLTRIM(name), '"', "'")

ELSE

    USE (m.fn_karty)   IN 0 NOUPDATE ALIAS karty   ORDER c_karty 

ENDIF 
small_log(PROGRAM() + " hotovo", JMENOLOGU)


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROC stahni_products

small_log(PROGRAM(), JMENOLOGU)

ACTIVATE SCREEN 
? "products (" + ALLTRIM(STR(PAGESIZE)) + ") "
FOR page_no = 1 TO 9999
    json = http_get("food/crud/products?page_size=" + ALLTRIM(STR(PAGESIZE)) + "&page=" + ALLTRIM(STR(m.page_no)))
    next_page = get_string_item (m.json, "next")
    ?? nx(m.page_no) + " "
    FOR ii = 1 TO PAGESIZE+3
       id       = get_integer_item (m.json, "id"                , ii)
       c_karty  = get_integer_item (m.json, "stock_card_number" , ii)
       name_web = get_string_item  (m.json, "name"              , ii)
       IF m.c_karty # 0
           LOCATE FOR c_karty = m.c_karty
           IF FOUND()
               GATHER FIELDS id, name_web MEMVAR 
           ELSE
               co_s_tim = "DELETE"
               INSERT INTO products FROM MEMVAR 
           ENDIF 
       ENDIF 
    ENDFOR 

    IF ISNULL(m.next_page)
        EXIT 
    ENDIF 
ENDFOR 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROC posli_products

small_log(PROGRAM(), JMENOLOGU)

ACTIVATE SCREEN 
? "products (" + ALLTRIM(STR(PAGESIZE)) + ") "

SCAN
    radek_productu = '{ "stock_card_number":' + nx(c_karty) + ', "name": "' + ALLTRIM(nazev) + '", "price": 99.9, "tax_rate": 21, "range":0 } '
    DO CASE
    CASE co_s_tim = "PATCH"
*        ?? "U" &&& + nazev
*        json = http_request("food/crud/products/" + nx(id), "PATCH", m.radek_productu)

    CASE co_s_tim = "POST"
        ?? "I" &&& + nazev
        json = http_request("food/crud/products/"          , "POST" , m.radek_productu)
        id = get_integer_item(m.json, "id")
        REPLACE id WITH m.id

    ENDCASE 
ENDSCAN 
small_log(PROGRAM() + " hotovo", JMENOLOGU)

