#DEFINE ODD          ";"
#DEFINE CRLF         CHR(13)+CHR(10)
#DEFINE PAGESIZE 333
 
DO (LOWER(JUSTPATH(sys(16))) + "\ppgm_http") WITH  "https://ovocnysvetozor.cz/wi/api/public/"

auth_HTTP=CREATEOBJECT("wwHttp")

http_auth_wi()

*vyzkousej_http()

DO vypln_c_karty_v_wi

DO oprav_ceny_v_wi


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE http_auth_wi

auth_request_string = m.http_adresa + "login"

DO set_http_parms WITH auth_HTTP, "AUTH"
auth_HTTP.AddPostKey()    && to reset post buffer

auth_HTTP.AddPostKey("email", "alto_api@wi.cz"    )
auth_HTTP.AddPostKey("password", "57Kqy0rft0#I"   )

auth_HTTP.cHttpVerb = "POST"

auth_retval_string = sloz_response_string ( auth_HTTP.HttpGet(m.auth_request_string), auth_HTTP.cResultCode, auth_HTTP.cResultCodeMessage, auth_HTTP.nError, auth_HTTP.cErrorMSg )

DO http_log WITH m.auth_retval_string, "AUTH " + m.http_adresa + CRLF + chr(9) + retez_programu() + " from " + UPPER(m.machine)
auth_HTTP.httpclose()

http_auth_token = "-DENIED-"
IF json_ok(m.auth_retval_string)
    http_auth_token = get_string_item (m.auth_retval_string, "access_token")
    IF VARTYPE(m.http_auth_token) = "C"
        http_auth_token = "Bearer " + m.http_auth_token 
    ENDIF 
ENDIF 
RETURN m.auth_retval_string 


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE vypln_c_karty_v_wi

get_products()

DO clos_data 
DO FORM alto_wi


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE oprav_ceny_v_wi

DO clos_data 
chyba_pri_otevirani = .F.
TRY  
        otevira_se = m.fn_skupiny
        USE (m.fn_skupiny) ALIAS skupiny    IN 0 SHARED
        otevira_se = m.fn_karty
        USE (m.fn_karty)   ALIAS karty        IN 0 SHARED 
CATCH TO oErr 
    otevira_se = m.otevira_se + CHR(13) + a_s(oErr.ErrorNo) + " " + oErr.Message 
        chyba_pri_otevirani = .T.
ENDTRY 
IF chyba_pri_otevirani
    MESSAGEBOX("Nelze otevrit databaze" + CHR(13) + m.otevira_se,16+1, PROGRAM(), 2222)
ELSE
    get_products("stock_card_number|eq|notnull")
    SELECT products
    SCAN
        SCATTER MEMVAR 
        SELECT karty
        LOCATE FOR c_karty = m.c_karty
        IF FOUND() .and. cena01 # m.price
            json = http_request( 'products/' + nx(m.id), "PATCH", '{ "product_price": "' + nx(karty.cena01,2) + '",  "product_tax_id": "2" }' )
            IF AT("200 OK", m.json) = 0
                MESSAGEBOX("Nepodarilo se zapsat do e-shopu" + m.json,1+16, products.name_cs + "  " + products.nazev, 3333)
            ENDIF 
        ENDIF 
    ENDSCAN 
ENDIF 



*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE get_products (txt_filter)
IF EMPTY(m.txt_filter)
    txt_filter = ''
ELSE 
    txt_filter = '&filter=' + m.txt_filter       &&&& '&filter=stock_card_number|eq|null'
ENDIF 

? "Products  (" + nx(PAGESIZE) + ") "

CREATE TABLE (m.gcpri_path + "products") FREE ;
       (id I, c_karty I NULL, name_cs C(99) NULL, nazev C(30) NULL, price N(9,2) NULL, cena N(9,2) NULL, tax_id N(1) NULL, tax_value N(6,3) NULL, tax_name C(22) NULL)
INDEX ON id TAG id


*GET https://ovocnysvetozor.alfabetatestserver.cz/wi/api/public/products?limit=99&page=1&filter=stock_card_number|eq|null 
*{ "http_status":"200 OK", "limit":99,"start":0,"count":99,"all_products_count":683,"products":[
*{"product_id":247,"stock_card_number":null, "name_cs":"Termo ta\u0161ka", "price":"79.000","tax_id":1,"tax_value":"21.0000","tax_name":"DPH&nbsp;21%"

FOR page_no = 1 TO 9999
    ACTIVATE SCREEN 
    json = http_get( 'products?limit=' + nx(PAGESIZE) + '&page=' + nx(page_no) + m.txt_filter )

    pocet = get_integer_item (m.json, "count", 1)
    IF ISNULL(m.pocet) .or. m.pocet = 0
        EXIT 
    ENDIF 

    ?? "."
    FOR ii = 1 TO m.pocet
       id        =     get_integer_item (m.json, "product_id"       , ii)
       c_karty   =     get_integer_item (m.json, "stock_card_number", ii)
       name_cs   = unicode( get_string_item  (m.json, "name_cs"          , ii) )
       tax_id    =     get_integer_item (m.json, "tax_id"           , ii)
       tax_name  =     get_string_item  (m.json, "tax_name"         , ii)
       price     = VAL(get_string_item  (m.json, "price"            , ii))
       tax_value = VAL(get_string_item  (m.json, "tax_value"        , ii))

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

ENDFOR 

SELECT name_cs, CNT(*) as pocet GROUP BY 1 FROM products INTO CURSOR duplicity
INDEX on name_cs TAG name_cs
SELECT products
SET RELATION TO name_cs INTO duplicity
REPLACE FOR duplicity.pocet > 1 name_cs WITH ALLTRIM(name_cs) + "  [" + nx(id) + "]"
USE IN duplicity



*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION unicode(lStringIn)
LOCAL lStringOut, lStringTmp, lcToConvert, lcChrToConverPosition
 
lStringTmp = m.lStringIn
lStringOut = ""
 
DO WHILE .T.
    lcChrToConverPosition = AT("\u", m.lStringTmp)
    IF  m.lcChrToConverPosition # 0    &&ak je este znak unicode
       lStringOut = m.lStringOut + LEFT(m.lStringTmp ,  m.lcChrToConverPosition-1) &&najskor cas retazca po \u
       lStringOut = m.lStringOut + CON_PYCHR_TO_UNICODE(SUBSTR(m.lStringTmp, m.lcChrToConverPosition,6))
       lStringTmp = SUBSTR(m.lStringTmp, m.lcChrToConverPosition+6)
    ELSE
        lStringOut = m.lStringOut + m.lStringTmp
        EXIT
    ENDIF    
ENDDO 
RETURN m.lStringOut


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*** znak pride vo formate \u017d = é, vysledok = STRCONV(CHR(0x7d)+CHR(0x01),6)
FUNCTION CON_PYCHR_TO_UNICODE  (ChrToCnovert)
LOCAL lCmd, lcReturn
lcReturn = m.ChrToCnovert
TRY 
    lCmd = "STRCONV(CHR(0x"+SUBSTR(m.ChrToCnovert,5,2)+")+CHR(0x"+SUBSTR(m.ChrToCnovert,3,2)+"),6)"
    lcReturn = &lCmd
CATCH
ENDTRY
RETURN m.lcReturn



*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PROCEDURE vyzkousej_http
http_get('orders')
http_get('products/720')
http_get('products/byAltoId/5500')
*http_request( 'products/720', "PATCH", '{  "product_price": "500.0",  "product_tax_id": "2", "stock_card_number" : "123456" }' )
*http_request( 'products/246', "PATCH", '{  "product_price": "555.5",  "product_tax_id": "2", "stock_card_number" : "123455" }' )
*http_request( 'products/517', "PATCH", '{  "product_price": "555.5",  "product_tax_id": "2", "stock_card_number" : "123455" }' )
http_get('products/720')
http_get('products?limit=10&filter=product_id|eq|720')
http_get('products?limit=99&start=0&filter=stock_card_number|eq|notnull')
http_get('products?limit=40&start=0&filter=stock_card_number|eq|null')


