*;*******************************************************************************************
*;
*; Vrati zustatek penez. Chyby typ character, jinak zustatek
*;
*;*******************************************************************************************
PARAMETERS p_retezec, p_ciskred, p_soubor, p_idklienta, p_datetime
PRIVATE vysledek, dotazString, qq, json, klicKlienta, m_jsonOK, myHkUrl
  myHkUrl = m.hk_url
  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
	  IF !EMPTY(m.p_soubor)
	    CREATE TABLE (m.p_soubor);
	     (id I, client_key I, value Y, new_value Y, kind C(22), tr_code C(33), tr_date C(33))
	    USE (m.p_soubor) ALIAS myTransakce EXCLUSIVE 
	  ELSE
	    CREATE CURSOR myTransakce;
	     (id I, client_key I, value Y, new_value Y, kind C(22), tr_code C(33), tr_date C(33))
	  ENDIF 
	    
	  IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
      	IF !EMPTY(m.p_datetime)
      		m.jsonDateTime = TTOC(m.p_datetime, 3)
      	ELSE
      		m.jsonDateTime = ""
      	ENDIF 
      	m.dotazstring = "kredit/crud/transactions?filter{client}="+ALLTRIM(STR(m.klicKlienta, 25, 0))+IIF(!EMPTY(m.jsonDateTime), "&filter{transacted_at.lt}="+m.jsonDateTime, "")+"&filter{kind}=KC&sort[]=-transacted_at&page=1&page_size=1"
		json = http_get(m.dotazstring)		
      ELSE
        lzHTTP.cContentType = "application/json"
      	m.dotazString = "clients/"+ALLTRIM(STR(m.klicKlienta, 25, 0))+"/transactions/last?kind=KC"
        json = STRCONV(STRCONV(lzHTTP.HttpGet(m.myHkUrl + IIF(RIGHT(m.myHkUrl,1) = "/","","/")+m.dotazString),11),2)
      ENDIF       
	  IF myjsonok(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"
	    CASE "99 NOT FOUND" $ m.json
	      * Transakce nenalezena
	      m.vysledek = 0
	    OTHERWISE 
	      m.vysledek = m.json
	    ENDCASE 
*!*	        IF VARTYPE(lzHTTP) = "O"
*!*	        	DO  zurnaljson WITH m.dotazstring, "Err:"+ALLTRIM(STR(lzHTTP.nError,10))+" ErrMssg:"+lzHTTP.cErrorMSg+" Vysl:"+m.vysledek
*!*	        ELSE
*!*	    	  	DO  zurnaljson WITH m.dotazstring, "ResultCode: " + STR(get_integer_item (m.json, '"http_status":'),6)+" Vysl:"+m.vysledek
*!*	        ENDIF 
	  ENDIF 
	  IF VARTYPE(m.vysledek) # 'C'
		  SELECT myTransakce
		  IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
		  	GO TOP 
		  ELSE 
		  	GO BOTTOM 
		  ENDIF 
		  m.vysledek = myTransakce.new_value
	  ENDIF    
    ELSE
      m.vysledek = "100 ERROR GETCLIENTID IN GETCREDIT"
    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          = IIF(!ISNULL(loVysledek.Rows[m.kk, 1].description), 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 1
 TRY 
	m.id               = loVysledek.Results.Transactions[1].id
	m.client_key       = loVysledek.Results.Transactions[1].client
	m.value            = IIF(VARTYPE(loVysledek.Results.Transactions[1].amount)='C', VAL(loVysledek.Results.Transactions[1].amount), loVysledek.Results.Transactions[1].amount) 
	m.new_value        = IIF(VARTYPE(loVysledek.Results.Transactions[1].balance)='C', VAL(loVysledek.Results.Transactions[1].balance), loVysledek.Results.Transactions[1].balance) 
	m.kind             = loVysledek.Results.Transactions[1].kind
	m.tr_code          = IIF(!ISNULL(loVysledek.Results.Transactions[1].description), loVysledek.Results.Transactions[1].description, "")
	m.tr_date          = CHRTRAN(loVysledek.Results.Transactions[1].transacted_at, "-", "")
	m.tr_date          = CHRTRAN(m.tr_date, "T", " ")

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