*;*******************************************************************************************
*;
*; Zapise event. Vraci Chyby jako character, jinak novy zustatek
*;
*;*******************************************************************************************
PARAMETERS p_retezec, p_ciskred, p_value, p_transactioncode, p_event
PRIVATE vysledek, dotazString, qq, json, klicKlienta, aktZustatek, deltaValue, jsonDateTime
  m.vysledek = 0
  IF VARTYPE(m.p_ciskred) = "C"
  	m.klicKlienta = json_getclientid(ALLTRIM(m.p_retezec)+ALLTRIM(m.p_ciskred))
  ELSE
  	m.klicKlienta = m.p_ciskred
  ENDIF 
  IF VARTYPE(m.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))
	  
      m.jsonDateTime = CHRTRAN(TTOC(DATETIME(), 3), 'T', ' ')

	  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":' + '"' + m.p_event +'"' + ;
		   ', "description":"' + LEFT(ALLTRIM(p_transactioncode),32)+'"'+;
		   '}'
	  ELSE 
		  m.lcJSON =;
		   '{'+;
		   '  "client_id":' + ALLTRIM(STR(m.klicKlienta,30)) + ;
		   ', "amount":' + STRTRAN(ALLTRIM(STR(m.p_value,12,2)), ",", ".") + ;
	   	   ', "kind":' + '"' + m.p_event +'"' + ;
		   ', "description":"' + LEFT(ALLTRIM(p_transactioncode),32)+'"'+;
		   ', "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"
		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 
	    m.dotazstring = lcJSON+"|POST|transactions"
	  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 ADDEVENT"
    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

