*;*******************************************************************************************
*;
*; Odesle penezni transakci. Vraci Chyby jako character, jinak novy zustatek
*;
*;*******************************************************************************************
* Vyhozeno transacted_at!!!!
PARAMETERS p_retezec, p_ciskred, p_value, p_transactioncode, p_idklienta, p_displaydescription, p_datetime
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_datetime)
      	m.jsonDateTime = lcgetJSONISODatetime(m.p_datetime, 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"
		m.dotazstring = "kredit/crud/transactions"
		json = http_request(m.dotazstring, "POST", m.lcJSON)		
	  	IF "400 Bad Request" $ m.json AND !EMPTY(m.jsonDateTime)
			m.dotazstring = "kredit/crud/historic_transactions"
			json = http_request(m.dotazstring, "POST", m.lcJSON)		
		ENDIF 
	  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
IF "historic_transaction" $ m.json
	m.id               = loVysledek.historic_transaction.id
	m.client_key       = loVysledek.historic_transaction.client
	m.value            = IIF(VARTYPE(loVysledek.historic_transaction.amount)='C', VAL(loVysledek.historic_transaction.amount), loVysledek.historic_transaction.amount) 
	m.new_value        = IIF(VARTYPE(loVysledek.historic_transaction.balance)='C', VAL(loVysledek.historic_transaction.balance), loVysledek.historic_transaction.balance) 
	m.kind             = loVysledek.historic_transaction.kind
	m.tr_code          = loVysledek.historic_transaction.description
	m.tr_date          = CHRTRAN(loVysledek.historic_transaction.transacted_at, "-", "")
	m.tr_date          = CHRTRAN(m.tr_date, "T", " ")
ELSE 
	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", " ")
ENDIF 
	IF !EMPTY(m.id)
	  INSERT INTO myTransakce FROM MEMVAR
	ENDIF 
*ENDFOR
IF TYPE("loJSON") == 'O' AND m.zavrit
  RELEASE loJSON
ENDIF     
RETURN


*!*	IF AT('"rows":', m.json) # 0
*!*	  retval = SUBSTR(json, AT('"rows":', m.json)+5)
*!*	  retval = SUBSTR(m.retval, AT('[', m.retval))
*!*	ELSE
*!*	  m.retval = json
*!*	ENDIF

*!*	DO WHILE "{" $ retval .and. "}" $ retval
*!*	   retval = SUBSTR(m.retval, AT('{', m.retval)+1)
*!*	   kousicek = LEFT(m.retval, AT('}', m.retval)-1)
*!*	   retval = SUBSTR(m.retval, AT('}', m.retval)+1)
*!*	   DO parsuj_kousicek WITH m.kousicek
*!*	   *? "- - - - - - - - - - - - - - - - - - - - - - -"
*!*	ENDDO 

**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
*!*	PROCEDURE parsuj_kousicek (kousicek)
*!*	id               = VAL(najdi_item (m.kousicek, "id"))
*!*	client_key       = VAL(najdi_item (m.kousicek, "client_key"))
*!*	value            = VAL(najdi_item (m.kousicek, "value"))
*!*	new_value        = VAL(najdi_item (m.kousicek, "new_value"))
*!*	kind             = najdi_item (m.kousicek, "kind")
*!*	tr_code          = najdi_item (m.kousicek, "transaction_code")
*!*	tr_date          = najdi_item (m.kousicek, "transaction_date")
*!*	INSERT INTO myTransakce FROM MEMVAR
