*;*******************************************************************************************
*;
*; Vrati objednavku klienta 
*; 2023/05/02 pridan parametr datetime objednavky
*;
*;*******************************************************************************************
PARAMETERS p_soubor, p_aliasmeals, p_clientid, _dtobjednavky
PRIVATE vysledek, dotazString, qq, json, klicKlienta, m_jsonOK, myHkUrl, ltDatetimeObjednavky
  myHkUrl = m.hk_url
  IF EMPTY(_dtobjednavky)
  	m.ltDatetimeObjednavky = sertimek()
  ELSE
  	m.ltDatetimeObjednavky = _dtobjednavky
  ENDIF 
	  IF !USED(m.p_aliasmeals)
	    WAIT "Chyba" WINDOW TIMEOUT 2
	    RETURN "Chyba"
	  ENDIF 
	  IF USED("myOrders")
	  	USE IN myOrders
	  ENDIF 
	  SELECT 0
	  IF !EMPTY(m.p_soubor)
	    CREATE TABLE (m.p_soubor);
	     (id I, nazev C(30), canteen I, course C(16), group C(16), cislo N (5,0), c_karty I, objednano N(2,0), sezrano N(2,0), meal I, showonweb L)
	    USE (m.p_soubor) ALIAS myOrders EXCLUSIVE 
	  ELSE
	    CREATE CURSOR myOrders;
	     (id I, nazev C(30), canteen I, course C(16), group C(16), cislo N (5,0), c_karty I, objednano N(2,0), sezrano N(2,0), meal I, showonweb L)
	  ENDIF 
	  SELECT 0
	  CREATE CURSOR mylcMeals;
	   (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)
	    
		  IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
	      		m.dotazstring = "canteen/crud/orders?filter{client}="+ALLTRIM(STR(m.p_clientid, 10))+"&filter{meal.date}="+LEFT(TTOC(m.ltDatetimeObjednavky, 3), 10) +"&include[]=meal.*"
				json = http_get(m.dotazstring)		
	      ELSE
			    json = "Not in old datastore"
	      ENDIF       
		  IF myjsonok(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
		    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 
*!*		        IF VARTYPE(lzHTTP) = "O"
*!*		        	DO  zurnaljson WITH m.dotazstring, "Err:"+ALLTRIM(STR(lzHTTP.nError,10))+" ErrMssg:"+lzHTTP.cErrorMSg+" Vysl:"+m.vysledek
*!*		        ELSE
*!*		    	  	DO  zurnaljson WITH m.dotazstring, "ResultCode: " + STR(get_integer_item (m.json, '"http_status":'),6)+" Vysl:"+m.vysledek
*!*		        ENDIF 
		    EXIT 
		  ENDIF 
	  IF VARTYPE(m.vysledek) # 'C'
		  m.vysledek = 0
	  ENDIF 
	  USE IN mylcMeals   
RETURN m.vysledek

**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
PROCEDURE parsuj_json (json)
RETURN 

PROCEDURE parsuj_json_qdf (json)
PRIVATE zavrit, nazev, canteen, course, pocet, c_karty, kk, cislo
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
	TRY 
		m.id	      = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].id) # 'U', loVysledek.Results.Meals[m.kk].id, 0)
		m.nazev       = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].name) # 'U', loVysledek.Results.Meals[m.kk].name, "")
		m.canteen_id  = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].canteen) # 'U', loVysledek.Results.Meals[m.kk].canteen, 0)
		m.course      = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].course) # 'U', loVysledek.Results.Meals[m.kk].course, "")
		m.cislo       = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].number) # 'U', loVysledek.Results.Meals[m.kk].number, 0)
		m.c_karty     = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].stock_card_number) # 'U', loVysledek.Results.Meals[m.kk].stock_card_number, 0)
		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   = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].show_on_web) # 'U', loVysledek.Results.Meals[m.kk].show_on_web, .F.)
		IF !EMPTY(m.nazev)
		  INSERT INTO mylcMeals FROM MEMVAR
		ENDIF
	CATCH
	ENDTRY  
ENDFOR


FOR m.kk = 1 TO loVysledek.Count
	m.id       = loVysledek.Results.Orders[m.kk].id
	m.objednano= loVysledek.Results.Orders[m.kk].ordered
	m.sezrano  = loVysledek.Results.Orders[m.kk].eaten
	m.meal     = loVysledek.Results.Orders[m.kk].meal
*!*		TRY 
*!*			m.nazev    = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].name) # 'U', loVysledek.Results.Meals[m.kk].name, "")
*!*			m.canteen  = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].canteen) # 'U', loVysledek.Results.Meals[m.kk].canteen, 0)
*!*			m.course   = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].course) # 'U', loVysledek.Results.Meals[m.kk].course, "")
*!*			*m.group   = ""
*!*			m.cislo    = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].number) # 'U', loVysledek.Results.Meals[m.kk].number, 0)
*!*			m.c_karty  = IIF(VARTYPE(loVysledek.Results.Meals[m.kk].stock_card_number) # 'U', loVysledek.Results.Meals[m.kk].stock_card_number, 0)
*!*		CATCH
*!*			m.c_karty = 0
*!*		ENDTRY 
	SELECT mylcMeals
	LOCATE FOR mylcMeals.id = m.meal
	IF FOUND()
		m.nazev    = mylcMeals.nazev
		m.canteen  = mylcMeals.canteen_id
		m.course   = mylcMeals.course
		*m.group   = ""
		m.cislo    = mylcMeals.cislo
		m.c_karty  = mylcMeals.c_karty
		m.showonweb  = mylcMeals.showonweb
	ELSE
		m.c_karty  = 0
	ENDIF 
	IF !EMPTY(m.id) AND !EMPTY(m.c_karty) && vyhozeno kvuli duplicitam klient+jidlo v jednom dni AND !EMPTY(m.objednano)
	  INSERT INTO myOrders FROM MEMVAR
	ENDIF 
ENDFOR
IF TYPE("loJSON") == 'O' AND m.zavrit
  RELEASE loJSON
ENDIF     
RETURN
