*;*******************************************************************************************
*;
*; Vytvori objednavku v datastore. Vraci Chyby jako character, jinak id objednavky
*; BACHA! Crudova routa nema kontroly na opakovanou objednavku!
*;*******************************************************************************************
PARAMETERS p_idklienta, p_mealid, p_value
PRIVATE vysledek, dotazString, qq, json, klicKlienta, aktZustatek, deltaValue, jsonDateTime
  m.vysledek = 0
	  IF USED("myNewOrder")
	    USE IN myNewOrder
	  ENDIF 
	  SELECT 0
	  CREATE CURSOR myNewOrder;
	   (id I, client_id I, ordered I, eaten I, meal_id I)
	  
      m.jsonDateTime = CHRTRAN(TTOC(DATETIME(), 3), 'T', ' ')

	  IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
		  m.lcJSON =;
		   '{'+;
		   '  "client":'  + ALLTRIM(STR(m.p_idklienta,30)) + ;
		   ', "meal":'    + ALLTRIM(STR(m.p_mealid,12)) + ;
		   ', "ordered":' + STRTRAN(ALLTRIM(STR(m.p_value,5,0)), ",", ".") +; 
		   ', "eaten":'   + STRTRAN(ALLTRIM(STR(m.p_value,5,0)), ",", ".") +;
		   '}'
	  ELSE 
		  m.lcJSON ='{}'
	  ENDIF 

	  IF VARTYPE(m.http_logovat) # "U" AND VARTYPE(m.http_auth_token) # "U"
		m.dotazstring = "canteen/crud/orders"
		json = http_request(m.dotazstring, "POST", m.lcJSON)		
	  ELSE 
	    json = "{Old datastore}"
	  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"
	    	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"
	    OTHERWISE 
	      m.vysledek = m.json
	    ENDCASE 
	  ENDIF 
	  IF TYPE("m.vysledek") # 'C'
     	SELECT myNewOrder
		GO BOTTOM 
		m.vysledek = myNewOrder.id
      ENDIF
RETURN m.vysledek


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

RETURN 

PROCEDURE parsuj_json_qdf (json)
PRIVATE zavrit
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.Order.id
	m.client_id        = loVysledek.Order.client
	m.ordered          = loVysledek.Order.ordered
	m.eaten        	   = loVysledek.Order.eaten
	m.meal_id          = loVysledek.Order.meal

	IF !EMPTY(m.id)
	  INSERT INTO myNewOrder FROM MEMVAR
	ENDIF 
*ENDFOR
IF TYPE("loJSON") == 'O' AND m.zavrit
  RELEASE loJSON
ENDIF     
RETURN

