*;*******************************************************************************************
*;
*; 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, p_alias
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), status C(1), storno L, ico C(15), dic C(15), nazevfirmy C(30), id_firmy N(10,0), dat_naroz D)
  SCATTER FIELDS dat_naroz MEMVAR 
  m.edate = m.dat_naroz
  IF USED("myClientInfos")
    USE IN myClientInfos
  ENDIF 
  IF !EMPTY(p_alias) AND USED(m.p_alias)
  	SELECT &p_alias
  	AFIELDS(aff)
  	SELECT 0
  	CREATE CURSOR myClientInfos FROM ARRAY aff
  ELSE 
  	SELECT 0
  	CREATE CURSOR myClientInfos;
   	 (idclinfo I, typ C(1), typ_slevy N(2,0), pltyp_slev N(2,0), idklienta I, sl_koef N(6,2), plsl_koef N(6,2), c_hlad C(1), plc_hlad C(1), dkoneckred D, limit N(7,0), max_d_odb N(3,0))
  ENDIF 


  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')

	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)+"&include[]=info.*"
  		ELSE
  			IF EMPTY(m.p_prescardid)
  				m.dotazString = "kredit/crud/clients?filter{chip_id.istartswith}=" + ALLTRIM(m.p_ciskredURLEncoded)+"&include[]=info.*"
  			ELSE
  				m.dotazString = "kredit/crud/clients?filter{card_id.istartswith}=" + ALLTRIM(m.p_ciskredURLEncoded)+"&include[]=info.*"
  			ENDIF 
  		ENDIF 
		json = http_get(m.dotazstring)		
	ELSE 
		* Nic
		json = '{}'
	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
    	*Nic
    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 VARTYPE(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
	    	SELECT myClientInfos
	    	DELETE FOR myClientInfos.idklienta # myClients.id
	    	GO TOP 
	    ELSE 
	    	m.vysledek = "101 CLIENT NOT FOUND"
	    ENDIF 
    	SELECT myclients
	  OTHERWISE
	    * OK
	    m.vysledek = myClients.id
	  ENDCASE 
  ENDIF    
RETURN m.vysledek



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 

SELECT myClientInfos
SCATTER FIELDS dkoneckred BLANK MEMVAR 
m.edate = m.dkoneckred

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)
	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.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", " ") 
	m.status = ALLTRIM(loVysledek.Results.Clients[m.kk].status)
	m.storno = IIF(INLIST(m.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 
	*
	TRY 
		IF VARTYPE(loVysledek.Results.client_infos[m.kk]) = 'O'
			m.typ = loVysledek.Results.client_infos[m.kk].typ
			m.idclinfo        = loVysledek.Results.client_infos[m.kk].id
			m.idklienta       = loVysledek.Results.client_infos[m.kk].client
			m.typ_slevy = loVysledek.Results.client_infos[m.kk].typ_slevy
			m.pltyp_slev = loVysledek.Results.client_infos[m.kk].pl_typ_slevy
			m.sl_koef = VAL(loVysledek.Results.client_infos[m.kk].sl_koef)
			m.plsl_koef = VAL(loVysledek.Results.client_infos[m.kk].pl_sl_koef)
			m.limit = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].limit), loVysledek.Results.client_infos[m.kk].limit, 0)
			m.limit_var = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].limit_var), loVysledek.Results.client_infos[m.kk].limit_var, 0)
			m.limit_den = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].limit_den), loVysledek.Results.client_infos[m.kk].limit_den, 0)
			m.sl_limit = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].sl_limit), loVysledek.Results.client_infos[m.kk].sl_limit, 0)
			m.sl_lim_mes = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].sl_limit_mes), loVysledek.Results.client_infos[m.kk].sl_limit_mes, 0)
			m.c_hlad = ALLTRIM(STR(loVysledek.Results.client_infos[m.kk].c_hlad, 3, 0))
			m.plc_hlad = ALLTRIM(STR(loVysledek.Results.client_infos[m.kk].pl_c_hlad, 3, 0))
			m.rezerva = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].rezerva), loVysledek.Results.client_infos[m.kk].rezerva, "")
			m.omezplatba = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].omez_platba), loVysledek.Results.client_infos[m.kk].omez_platba, .F.)
			m.omezeniid = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].id_omezeni), loVysledek.Results.client_infos[m.kk].id_omezeni, 0)
			m.poslpohnul = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].posl_poh_nul), loVysledek.Results.client_infos[m.kk].posl_poh_nul, 0)
			m.poslpoharc = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].posl_poh_arc), loVysledek.Results.client_infos[m.kk].posl_poh_arc, 0)
			m.sl_mincast = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].sl_min_cast), loVysledek.Results.client_infos[m.kk].sl_min_cast, 0)
			m.profil = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].profil), loVysledek.Results.client_infos[m.kk].profil, "")
			m.dkoneckred = jtod(loVysledek.Results.client_infos[m.kk].d_konec_kred, m.edate)
			m.max_d_odb = IIF(!ISNULL(loVysledek.Results.client_infos[m.kk].max_den_odber), loVysledek.Results.client_infos[m.kk].max_den_odber, 0)
			IF !EMPTY(m.idclinfo)
			  *INSERT INTO myClientInfos FROM MEMVAR
			  SELECT myClientInfos
			  APPEND BLANK 
			  GATHER FIELDS typ, idclinfo, idklienta, typ_slevy, pltyp_slev, sl_koef, plsl_koef, limit, limit_var, limit_den, sl_limit, sl_lim_mes,;
			   c_hlad, plc_hlad, rezerva, omezplatba, omezeniid, poslpohnul, poslpoharc, sl_mincast, profil, dkoneckred, max_d_odb;
			   MEMVAR 
			ENDIF 
		ENDIF 
	CATCH
	ENDTRY 
ENDFOR
IF VARTYPE(loJSON) == 'O' AND m.zavrit
  RELEASE loJSON
ENDIF     
RETURN
