*;*******************************************************************************************
*;
*; Vrati soubor s kantynami + id podle food_datu. Chyby typ character, jinak 0
*;
*;*******************************************************************************************
PARAMETERS p_soubor
PRIVATE vysledek, dotazString, qq, json, klicKlienta, m_jsonOK, myHkUrl
  myHkUrl = m.hk_url
	  IF USED("myCanteens")
	    USE IN myCanteens
	  ENDIF 
	  SELECT 0
	  * Pro novy datastore maji smysl jen prvni 2 sloupce
	  IF !EMPTY(m.p_soubor)
	    CREATE TABLE (m.p_soubor);
	     (id_kas C(2), id_zkratka C(32), id I, c_stredisk I)
	    USE (m.p_soubor) ALIAS myCanteens EXCLUSIVE 
	  ELSE
	    CREATE CURSOR myCanteens;
	     (id_kas C(2), id_zkratka C(32), id I, comment C(25), c_stredisk I)
	  ENDIF 
	    
	  IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
      		m.dotazstring = "canteen/crud/canteens"
			json = http_get(m.dotazstring)		
      ELSE
		  m.dotazString = "canteens"

		  loHttp.cContentType = "application/json"
		  
		  qq = STRCONV(STRCONV(loHttp.HttpGet(m.hk_url + IIF(RIGHT(m.hk_url,1) = "/","","/")+m.dotazString),11),2)

		  json = strip_whitespaces (m.qq)
      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
	      * Cantena nenalezena
	      m.vysledek = m.json
	    OTHERWISE 
	      m.vysledek = m.json
	    ENDCASE 
	  ENDIF 
	  IF VARTYPE(m.vysledek) # 'C'
		  m.vysledek = 0
	  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.comment       	  = loVysledek.Rows[m.kk, 1].comment
*!*		m.firma_id            = loVysledek.Rows[m.kk, 1].firma_id
*!*		m.chain_id            = loVysledek.Rows[m.kk, 1].chain_id
	IF !EMPTY(m.id)
	  INSERT INTO myCanteens FROM MEMVAR
	ENDIF 
ENDFOR
*IF TYPE("loJSON") == 'O'
*  RELEASE loJSON
*ENDIF     

PROCEDURE parsuj_json_qdf (json)
PRIVATE zavrit, id_kas, id_zkratka, c_stredisk
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 

m.id_kas = ""
FOR m.kk = 1 TO loVysledek.Count
	m.id		   = loVysledek.Results.Canteens[m.kk].id
	m.id_zkratka   = loVysledek.Results.Canteens[m.kk].name
	m.c_stredisk   = IIF(VARTYPE(loVysledek.Results.Canteens[m.kk].register_number) # "U", loVysledek.Results.Canteens[m.kk].register_number, 0)
	m.c_stredisk   = IIF(ISNULL(m.c_stredisk), 0, m.c_stredisk)
	IF !EMPTY(m.id_zkratka)
	  INSERT INTO myCanteens FROM MEMVAR
	ENDIF 
ENDFOR
IF TYPE("loJSON") == 'O' AND m.zavrit
  RELEASE loJSON
ENDIF     
RETURN
