*;*******************************************************************************************
*;
*; Vrati id klienta (client_key). Chyba spojeni = -9999999999, -1 nenalezen, -2 jina chyba
*; p_vyznam: empty = hledani podle cis_kred, !empty podle kodu karty
*;*******************************************************************************************
PARAMETERS p_ciskred, p_vyznam, p_prescardid
PRIVATE vysledek, dotazString, qq, json, p_ciskredURLEncoded
  m.vysledek = 0
  IF USED("myClients")
    USE IN myClients
  ENDIF 
  SELECT 0
  CREATE CURSOR myClients;
   (id I, client_id C(25), card_id C(25), zamest_key I, firstname C(30), surname C(40), email C(50), exp_date C(10), chipcode C(22),;
    adresa C(30), ico C(15), dic C(15), nazevfirmy C(30), storno L, dat_naroz D)
  SCATTER FIELDS dat_naroz MEMVAR 
  m.edate = m.dat_naroz

  *m.adresa2 = "http://transakce.mine.nu:9989/"

  m.p_ciskredURLEncoded = ALLTRIM(m.p_ciskred)
  m.p_ciskredURLEncoded = STRTRAN(m.p_ciskredURLEncoded, '\', '\\')
  m.p_ciskredURLEncoded = STRTRAN(m.p_ciskredURLEncoded, '%', '%25')
  m.p_ciskredURLEncoded = STRTRAN(m.p_ciskredURLEncoded, '&', '%26')
  m.p_ciskredURLEncoded = STRTRAN(m.p_ciskredURLEncoded, '?', '%3f')
  m.p_ciskredURLEncoded = STRTRAN(m.p_ciskredURLEncoded, '=', '%3d')

  *qq = dotaz(m.dotazString)
  *lzHTTP = CREATEOBJECT("wwHttp")
	IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
  		IF EMPTY(m.p_vyznam)
  			m.dotazString = "kredit/crud/clients?filter{client_id}=" + ALLTRIM(m.p_ciskredURLEncoded)
  		ELSE
  			IF EMPTY(m.p_prescardid)
  				m.dotazString = "kredit/crud/clients?filter{chip_id.istartswith}=" + ALLTRIM(m.p_ciskredURLEncoded)
  			ELSE
  				m.dotazString = "kredit/crud/clients?filter{card_id.istartswith}=" + ALLTRIM(m.p_ciskredURLEncoded)
  			ENDIF 
  		ENDIF 
		json = http_get(m.dotazstring)		
	ELSE 
  		m.dotazString = "clients?client_id=" + ALLTRIM(m.p_ciskredURLEncoded)
	  
		lzHTTP.cContentType = "application/json"
	  
		json = STRCONV(STRCONV(lzHTTP.HttpGet(m.hk_url + IIF(RIGHT(m.hk_url,1) = "/","","/")+m.dotazString),11),2)

		*json = strip_whitespaces (m.qq)
	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"
		m.json = CHRTRAN(m.json, "'", "")	&& Vyhodit apostrofy
    	DO parsuj_json_qdf WITH m.json
    ELSE
    	DO parsuj_json WITH m.json
    ENDIF 
  ELSE 
    * Web nedostupny nebo jina chyba
    IF EMPTY(m.json)
      m.vysledek = "9999 NO WEB CONNECTION"
    ELSE
      m.vysledek = m.json
    ENDIF
  ENDIF 
  IF TYPE("m.vysledek") # 'C'
	  SELECT myClients
	  m.pzaznamu = RECCOUNT()
	  DO CASE	  
	  CASE m.pzaznamu = 0
	    * Nenalezen klient
	    m.vysledek = "101 CLIENT NOT FOUND"
	  CASE m.pzaznamu > 1
	    * Duplicitni klient
		SELECT myClients
  		IF EMPTY(m.p_vyznam)
    		DELETE FOR !(ALLTRIM(myClients.client_id) == ALLTRIM(m.p_ciskred))
  			LOCATE FOR ALLTRIM(myClients.client_id) == ALLTRIM(m.p_ciskred)
  		ELSE
  			IF EMPTY(m.p_prescardid)
    			DELETE FOR !(ALLTRIM(myClients.chipcode) == ALLTRIM(m.p_ciskred))
  				LOCATE FOR ALLTRIM(myClients.chipcode) == ALLTRIM(m.p_ciskred)
  			ELSE
    			DELETE FOR !(ALLTRIM(myClients.card_id) == ALLTRIM(m.p_ciskred))
  				LOCATE FOR ALLTRIM(myClients.card_id) == ALLTRIM(m.p_ciskred)
  			ENDIF 
  		ENDIF 
	    IF FOUND()
	    	m.vysledek = myClients.id
	    ELSE 
	    	m.vysledek = "101 CLIENT NOT FOUND"
	    ENDIF 
	  OTHERWISE
	    * OK
	    m.vysledek = myClients.id
	  ENDCASE 
  ENDIF    
  *IF TYPE("lzHTTP") == 'O'
  *  RELEASE lzHTTP
  *ENDIF     
RETURN m.vysledek


**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
PROCEDURE parsuj_json (json)
PRIVATE zavrit
*!*	*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

m.zavrit = .F.
IF VARTYPE(loJSON) # 'O'
	m.lcClasslibWas = SET("Classlib")
	IF !("JSON.VCX" $ UPPER(m.lcClasslibWas))
	  SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	ENDIF 
	m.loJSON = CREATEOBJECT("JSON")
	loVysledek=lojson.parse(m.json)
	RELEASE CLASSLIB JSON.VCX
	SET CLASSLIB TO &lcClasslibWas
	m.zavrit = .T.
ELSE
	loVysledek=lojson.parse(m.json)
ENDIF 

FOR m.kk = 1 TO loVysledek.RowCount
	m.id               = loVysledek.Rows[m.kk, 1].id
	m.client_id       = loVysledek.Rows[m.kk, 1].client_id
	m.card_id            = loVysledek.Rows[m.kk, 1].card_id
	*tohle nejak vypadlo najednou (22.4.2015)
	*m.zamest_key        = IIF(EMPTY(loVysledek.Rows[m.kk, 1].employer_id), 0, loVysledek.Rows[m.kk, 1].employer_id)
	m.firstname             = IIF(!EMPTY(loVysledek.Rows[m.kk, 1].firstname), loVysledek.Rows[m.kk, 1].firstname, "")
	m.surname          = IIF(!EMPTY(loVysledek.Rows[m.kk, 1].surname), loVysledek.Rows[m.kk, 1].surname, "")
	m.email          = IIF(!EMPTY(loVysledek.Rows[m.kk, 1].email),loVysledek.Rows[m.kk, 1].email,"")
	m.email				= IIF(ISNULL(m.email), "", m.email)
	m.exp_date = CHRTRAN(loVysledek.Rows[m.kk, 1].expiration_date, "-", "/")
	m.dt_updated = CHRTRAN(TTOC(loVysledek.Rows[m.kk, 1].created_at, 3), "-", "")
	m.dt_updated = CHRTRAN(m.dt_updated, "T", " ") 
	m.dt_changed = CHRTRAN(TTOC(loVysledek.Rows[m.kk, 1].updated_at, 3), "-", "")
	m.dt_changed = CHRTRAN(m.dt_changed, "T", " ") 
	IF !EMPTY(m.id)
	  INSERT INTO myClients FROM MEMVAR
	ENDIF 
ENDFOR
IF TYPE("loJSON") == 'O' AND m.zavrit
  RELEASE loJSON
ENDIF     
RETURN


PROCEDURE parsuj_json_qdf (json)
PRIVATE zavrit, nCount, dic
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 
IF VARTYPE(loVysledek) = "O"
	m.nCount = IIF(VARTYPE(loVysledek.Count) # "U", loVysledek.Count, 0)
ELSE 
	m.nCount = 0
ENDIF 

FOR m.kk = 1 TO m.nCount
	m.id               = loVysledek.Results.Clients[m.kk].id
	m.client_id       = loVysledek.Results.Clients[m.kk].client_id
	m.card_id            = UPPER(loVysledek.Results.Clients[m.kk].card_id)
	m.chipcode 			= IIF(!ISNULL(loVysledek.Results.Clients[m.kk].chip_id), UPPER(loVysledek.Results.Clients[m.kk].chip_id), "")
	*tohle nejak vypadlo najednou (22.4.2015)
	*m.zamest_key        = IIF(EMPTY(loVysledek.Results.Clients[m.kk, 1].employer_id), 0, loVysledek.Results.Clients[m.kk, 1].employer_id)
	m.firstname             = IIF(!ISNULL(loVysledek.Results.Clients[m.kk].firstname), loVysledek.Results.Clients[m.kk].firstname, "")
	m.surname          = IIF(!ISNULL(loVysledek.Results.Clients[m.kk].surname), loVysledek.Results.Clients[m.kk].surname, "")
	m.email          = IIF(!ISNULL(loVysledek.Results.Clients[m.kk].email),loVysledek.Results.Clients[m.kk].email,"")
	m.exp_date = IIF(!ISNULL(loVysledek.Results.Clients[m.kk].expiration_date), CHRTRAN(loVysledek.Results.Clients[m.kk].expiration_date, "-", "/"), "")
	m.dt_updated = CHRTRAN(loVysledek.Results.Clients[m.kk].created_at, "-", "")
	m.dt_updated = CHRTRAN(m.dt_updated, "T", " ") 
	m.dt_changed = CHRTRAN(loVysledek.Results.Clients[m.kk].updated_at, "-", "")
	m.dt_changed = CHRTRAN(m.dt_changed, "T", " ") 
	IF VARTYPE(loVysledek.Results.Clients[m.kk].address.street) # 'U' AND VARTYPE(loVysledek.Results.Clients[m.kk].address.city) # 'U'  
	  m.adresa	= IIF(!EMPTY(loVysledek.Results.Clients[m.kk].address.street) and !ISNULL(loVysledek.Results.Clients[m.kk].address.street), loVysledek.Results.Clients[m.kk].address.street, "")+" "+IIF(!EMPTY(loVysledek.Results.Clients[m.kk].address.city) and !ISNULL(loVysledek.Results.Clients[m.kk].address.city), loVysledek.Results.Clients[m.kk].address.city, "")
	ELSE
	  m.adresa = ""
	ENDIF 
	IF VARTYPE(loVysledek.Results.Clients[m.kk].company.ico) # 'U'
	  m.ico = IIF(!EMPTY(loVysledek.Results.Clients[m.kk].company.ico) and !ISNULL(loVysledek.Results.Clients[m.kk].company.ico), loVysledek.Results.Clients[m.kk].company.ico, "")
	ELSE
	  m.ico = ""
	ENDIF 
	IF VARTYPE(loVysledek.Results.Clients[m.kk].company.dic) # 'U'
	  m.dic = IIF(!EMPTY(loVysledek.Results.Clients[m.kk].company.dic) and !ISNULL(loVysledek.Results.Clients[m.kk].company.dic), loVysledek.Results.Clients[m.kk].company.dic, "")
	ELSE
	  m.dic = ""
	ENDIF 
	IF VARTYPE(loVysledek.Results.Clients[m.kk].company.name) # 'U'
	  m.nazevfirmy = IIF(!EMPTY(loVysledek.Results.Clients[m.kk].company.name) and !ISNULL(loVysledek.Results.Clients[m.kk].company.name), loVysledek.Results.Clients[m.kk].company.name, "")
	ELSE
	  m.nazevfirmy = ""
	ENDIF 
	m.storno = IIF(INLIST(loVysledek.Results.Clients[m.kk].status, "A", "1"), .F., .T.)
	IF VARTYPE(loVysledek.Results.Clients[m.kk].personal.birth_date) # 'U'
	  m.dat_naroz = jtod(loVysledek.Results.Clients[m.kk].personal.birth_date, m.edate)
	ENDIF 
	IF !EMPTY(m.id)
	  INSERT INTO myClients FROM MEMVAR
	ENDIF 
ENDFOR
IF TYPE("loJSON") == 'O' AND m.zavrit
  RELEASE loJSON
ENDIF     
RETURN

