*;*******************************************************************************************
*;
*;*******************************************************************************************

DO (LOWER(JUSTPATH(sys(16))) + "\http_ppgm") WITH "https://admin:admin@canteen-backend.alto-testing.mine.nu/"

json = http_get('kredit/crud/clients')
json = http_get('kredit/crud/transactions')

klicKlienta = 1
p_value = 12.34
p_transactioncode = "popisek"
p_displaydescription = "displejpopisek"
jsonDateTime = ""


lcJSON =;
           '{'+;
           '  "client":' + ALLTRIM(STR(m.klicKlienta,30)) + ;
           ', "amount":' + STRTRAN(ALLTRIM(STR(m.p_value,12,2)), ",", ".") + ;
           ', "kind":"KC"' + ;
           ', "description":"' + ALLTRIM(p_transactioncode)+'"'+;
           IIF(!EMPTY(m.p_displaydescription), ', "display_description":"' + ALLTRIM(m.p_displaydescription)+'"', "")+;
           IIF(!EMPTY(m.jsonDateTime), ', "transacted_at":"' + ALLTRIM(m.jsonDateTime)+'"', "")+;
           '}'

  json = http_request("kredit/crud/transactions", "POST", m.lcJSON)



RETURN 

PROCEDURE xxx
PARAMETERS p_retezec, p_ciskred, p_value, p_transactioncode, p_idklienta, p_displaydescription, p_trzbadatetime
PRIVATE vysledek, dotazString, qq, json, klicKlienta, aktZustatek, deltaValue, jsonDateTime
  m.vysledek = 0
  IF EMPTY(m.p_idklienta)
    m.klicKlienta = json_getclientid(ALLTRIM(m.p_retezec)+ALLTRIM(m.p_ciskred))
  ELSE
    m.klicKlienta = m.p_idklienta
  ENDIF 
  IF TYPE("klicKlienta") # 'C'
    IF m.klicKlienta > 0
	  IF USED("myTransakce")
	    USE IN myTransakce
	  ENDIF 
	  SELECT 0
	  CREATE CURSOR myTransakce;
	   (id I, client_key I, value Y, new_value Y, kind C(22), tr_code C(33), tr_date C(33))
	  
      IF !EMPTY(m.p_trzbadatetime)
      	m.jsonDateTime = lcgetJSONISODatetime(m.p_trzbadatetime, m.gcTimeZoneOffset, m.gcLetniCasOffset)
      ELSE 
      	m.jsonDateTime = ""
      ENDIF 

	  IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
		  m.lcJSON =;
		   '{'+;
		   '  "client":' + ALLTRIM(STR(m.klicKlienta,30)) + ;
		   ', "amount":' + STRTRAN(ALLTRIM(STR(m.p_value,12,2)), ",", ".") + ;
		   ', "kind":"KC"' + ;
		   ', "description":"' + ALLTRIM(p_transactioncode)+'"'+;
		   IIF(!EMPTY(m.p_displaydescription), ', "display_description":"' + ALLTRIM(m.p_displaydescription)+'"', "")+;
		   IIF(!EMPTY(m.jsonDateTime), ', "transacted_at":"' + ALLTRIM(m.jsonDateTime)+'"', "")+;
		   '}'
	  ELSE 
		  m.lcJSON =;
		   '{'+;
		   '  "client_id":' + ALLTRIM(STR(m.klicKlienta,30)) + ;
		   ', "amount":' + STRTRAN(ALLTRIM(STR(m.p_value,12,2)), ",", ".") + ;
		   ', "kind":"KC"' + ;
		   ', "description":"' + ALLTRIM(p_transactioncode)+'"'+;
		   ', "canteen_id":' + ALLTRIM(STR(m.hk_defidkantyny,30)) + ;
		   '}'
	  ENDIF 
	   *', "transacted_at":' + '"' + m.jsonDateTime + '"' +;

	  IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
		m.dotazstring = "kredit/create_transaction"
		* bude kredit/crud/transactions
		json = http_request(m.dotazstring, "POST", m.lcJSON)		
	  ELSE 
	    lzHTTP.nHttpPostMode = 8
	    lzHTTP.cContentType = "application/json; charset=UTF-8"
	    lzHTTP.AddPostKey()    && to reset post buffer
	    lzHTTP.AddPostKey(STRCONV(STRCONV(lcJSON,1),9))
	    lzHTTP.cHttpVerb = "POST"
	    json = STRCONV(STRCONV(lzHTTP.HttpGet(m.hk_url + "transactions"),11),2)
	  ENDIF 

	  IF myjsonok(m.json)	&& lzHTTP.nError = 0 AND EMPTY(lzHTTP.cErrorMSg)	&&json_ok (m.json)
		IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
	    	DO parsuj_json_qdf WITH m.json
	    ELSE
	    	DO parsuj_json WITH m.json
	    ENDIF 
	  ELSE 
	    * Web nedostupny nebo jina chyba
	    DO CASE
	    CASE EMPTY(m.json)
	      m.vysledek = "9999 NO WEB CONNECTION"
	    OTHERWISE 
	      m.vysledek = m.json
	    ENDCASE 
        IF VARTYPE(lzHTTP) = "O"
	    	m.dotazstring = lcJSON+"|POST|transactions"
          DO  zurnaljson WITH m.dotazstring, "Err:"+ALLTRIM(STR(lzHTTP.nError,10))+" ErrMssg:"+lzHTTP.cErrorMSg+" Vysl:"+m.vysledek
	    ELSE
	    	m.dotazstring = lcJSON+"|POST|create_transaction"
    	  DO  zurnaljson WITH m.dotazstring, "ResultCode: " + STR(get_integer_item (m.json, '"http_status":'),6)+" Vysl:"+m.vysledek
	    ENDIF 
	  ENDIF 
	  IF TYPE("m.vysledek") # 'C'
     	SELECT myTransakce
		GO BOTTOM 
		m.vysledek = myTransakce.new_value
      ENDIF
      *IF VARTYPE(lzHTTP) == 'O'
      *  RELEASE lzHTTP 
      *ENDIF 
    ELSE
      m.vysledek = "100 ERROR GETCLIENTID IN ADDCREDIT"
    ENDIF 
  ELSE
    m.vysledek = m.klicKlienta
  ENDIF  
  *IF TYPE("lzHTTP") == 'O'
  *  RELEASE lzHTTP
  *ENDIF     
RETURN m.vysledek


**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
PROCEDURE parsuj_json (json)

*m.lcClasslibWas = SET("Classlib")
*SET CLASSLIB TO "JSON.VCX" ADDITIVE 
*m.loJSON = CREATEOBJECT("JSON")
loVysledek=lojson.parse(m.json)
*RELEASE CLASSLIB JSON.VCX
*SET CLASSLIB TO &lcClasslibWas
FOR m.kk = 1 TO loVysledek.RowCount
	m.id               = loVysledek.Rows[m.kk, 1].id
	m.client_key       = loVysledek.Rows[m.kk, 1].client_id
	m.value            = IIF(VARTYPE(loVysledek.Rows[m.kk, 1].amount)='C', VAL(loVysledek.Rows[m.kk, 1].amount), loVysledek.Rows[m.kk, 1].amount) 
	m.new_value        = IIF(VARTYPE(loVysledek.Rows[m.kk, 1].balance)='C', VAL(loVysledek.Rows[m.kk, 1].balance), loVysledek.Rows[m.kk, 1].balance) 
	m.kind             = loVysledek.Rows[m.kk, 1].kind
	m.tr_code          = loVysledek.Rows[m.kk, 1].description
	m.tr_date          = TTOC(loVysledek.Rows[m.kk, 1].transacted_at, 1)
	IF !EMPTY(m.id)
	  INSERT INTO myTransakce FROM MEMVAR
	ENDIF 
ENDFOR
*IF TYPE("loJSON") == 'O'
*  RELEASE loJSON
*ENDIF     
RETURN 

PROCEDURE parsuj_json_qdf (json)
PRIVATE zavrit
m.zavrit = .F.
IF VARTYPE(loJSON) # 'O'
	m.lcProcLibWas = SET("procedure")
	IF !("QDFOXJSON" $ UPPER(m.lcProcLibWas))
	  SET PROCEDURE TO "QDFOXJSON" ADDITIVE 
	ENDIF 
    m.loJSON = CREATEOBJECT("jsonHelper")
	loVysledek=lojson.parse(m.json)
	m.zavrit = .T.
ELSE
	loVysledek=lojson.parse(m.json)
ENDIF 

*FOR m.kk = 1 TO loVysledek.Count

	m.id               = loVysledek.Transaction.id
	m.client_key       = loVysledek.Transaction.client
	m.value            = IIF(VARTYPE(loVysledek.Transaction.amount)='C', VAL(loVysledek.Transaction.amount), loVysledek.Transaction.amount) 
	m.new_value        = IIF(VARTYPE(loVysledek.Transaction.balance)='C', VAL(loVysledek.Transaction.balance), loVysledek.Transaction.balance) 
	m.kind             = loVysledek.Transaction.kind
	m.tr_code          = loVysledek.Transaction.description
	m.tr_date          = CHRTRAN(loVysledek.Transaction.transacted_at, "-", "")
	m.tr_date          = CHRTRAN(m.tr_date, "T", " ")

	IF !EMPTY(m.id)
	  INSERT INTO myTransakce FROM MEMVAR
	ENDIF 
*ENDFOR
IF TYPE("loJSON") == 'O' AND m.zavrit
  RELEASE loJSON
ENDIF     
RETURN


