*;*******************************************************************************************
*;
*; Vrati soubor s Jidelaku za dany den a kantynu
*;
*;*******************************************************************************************
PARAMETERS p_soubor, p_tdate, p_idcanteen, p_ckarty
PRIVATE vysledek, dotazString, qq, json, klicKlienta, m_jsonOK, myHkUrl, p_tdate_from
  myHkUrl = m.hk_url
	  IF USED("myMeals")
	    USE IN myMeals
	  ENDIF 
	  SELECT 0
	  IF !EMPTY(m.p_soubor)
	    CREATE TABLE (m.p_soubor);
	     (ddatum D, id I, nazev C(30), canteen_id I, course C(16), group C(16), cislo N (5,0), c_karty I, showonweb L)
	    USE (m.p_soubor) ALIAS myMeals EXCLUSIVE 
	  ELSE
	    CREATE CURSOR myMeals;
	     (ddatum D, id I, nazev C(30), canteen_id I, course C(16), group C(16), cislo N (5,0), c_karty I, showonweb L)
	  ENDIF 
	  
	  m.p_tdate_from =  m.p_tdate - 86400 * 3	&& 3 dny dozadu
	  IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
      		m.dotazstring = "canteen/crud/meals?filter{date}="+LEFT(TTOC(p_tdate, 3), 10)+"&filter{canteen}="+ALLTRIM(STR(m.p_idcanteen, 10))+;
      		 IIF(!EMPTY(m.p_ckarty),"&filter{stock_card_number}="+ALLTRIM(STR(m.p_ckarty, 10)),"")+"&page_size=200"
      		*m.dotazstring = "canteen/crud/meals?filter{date.range}="+LEFT(TTOC(p_tdate_from, 3), 10)+"&filter{date.range}="+LEFT(TTOC(p_tdate, 3), 10)+"&filter{canteen}="+ALLTRIM(STR(m.p_idcanteen, 10))
			json = http_get(m.dotazstring)		
      ELSE
		    json = "Not in new datastore"
      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
	      * SOV 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)


PROCEDURE parsuj_json_qdf (json)
PRIVATE zavrit, nazev, canteen, course, pocet, c_karty, kk
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.Results.Meals[m.kk].id
	m.nazev       = loVysledek.Results.Meals[m.kk].name
	m.canteen_id  = loVysledek.Results.Meals[m.kk].Canteen
	m.course      = loVysledek.Results.Meals[m.kk].Course
	m.cislo       = loVysledek.Results.Meals[m.kk].number
	m.c_karty     = loVysledek.Results.Meals[m.kk].stock_card_number
	m.ddatum	  = DATE(VAL(LEFT(loVysledek.Results.Meals[m.kk].date, 4)), VAL(SUBSTR(loVysledek.Results.Meals[m.kk].date, 6, 2)), VAL(SUBSTR(loVysledek.Results.Meals[m.kk].date, 9, 2)))
	m.showonweb   = loVysledek.Results.Meals[m.kk].show_on_web
	IF !EMPTY(m.nazev)
	  INSERT INTO myMeals FROM MEMVAR
	ENDIF 
ENDFOR
IF TYPE("loJSON") == 'O' AND m.zavrit
  RELEASE loJSON
ENDIF     
RETURN
