PARAMETERS p_czradelny, p_idklienta, p_sov, p_jsondatum, p_sezrat, p_soubor
PRIVATE vysledek, dotazString, qq, json
  m.vysledek = 0
  IF USED("myObjednan")
    USE IN myObjednan
  ENDIF 
  SELECT 0
  CREATE TABLE (m.p_soubor);
   (id I, firma_key I, s_o_v C(1), datum D, c_karty I, nazev C(30), client_key I, objednano I, sezrano I)
  USE (m.p_soubor) ALIAS myObjednan EXCLUSIVE 
  m.dotazString = "canteens/" + m.p_czradelny + "/orders?date=" + p_jsondatum + "&client_key="+m.p_idklienta	&& canteens/4/orders?date=2014-07-09&client_key=3
  qq = dotaz(m.dotazString)
  json = strip_whitespaces (m.qq)

  IF json_ok (m.json)
    DO parsuj_json WITH m.json
  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 myObjednan
	  DELETE FOR myObjednan.s_o_v # m.p_sov
	  PACK 
	  IF RECCOUNT() > 0
	    IF myObjednan.objednano > myObjednan.sezrano
	      m.vysledek = 0
	      IF m.p_sezrat
	      	SELECT myObjednan
		    SCAN FOR sezrano < objednano
		      id_objednavky = ALLTRIM(STR(id))
		      pocet = ALLTRIM(STR(objednano - sezrano))
	          oHTTP.AddPostKey()    && to reset post buffer
		      oHTTP.AddPostKey("qwe", "qwe")
		      qq = dotaz("orders/" + m.id_objednavky + "/consume?" + "count=" + m.pocet)
		    ENDSCAN 
	      ENDIF 
	    ELSE
	      * Uz bylo vydano
	      m.vysledek = "8888 Jídlo již vydáno!"
	    ENDIF 
	  ELSE 
	    * Zadna objednavka
	    m.vysledek = "8889 Žádná objednávka!"
	  ENDIF
  ENDIF    
RETURN m.vysledek


PROCEDURE parsuj_json (json)
retval = SUBSTR(json, AT('"rows":', m.json)+5)
retval = SUBSTR(m.retval, AT('[', m.retval))

DO WHILE "{" $ retval .and. "}" $ retval
   retval = SUBSTR(m.retval, AT('{', m.retval)+1)
   kousicek = LEFT(m.retval, AT('}', m.retval)-1)
   retval = SUBSTR(m.retval, AT('}', m.retval)+1)
   DO parsuj_kousicek WITH m.kousicek
   ? "- - - - - - - - - - - - - - - - - - - - - - -"
ENDDO 

**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  
PROCEDURE parsuj_kousicek (kousicek)
id         = VAL(najdi_item (m.kousicek, "id"))
dddd       = najdi_item (m.kousicek, "datum")
datum      = DATE(VAL(LEFT(m.dddd,4)), VAL(SUBSTR(m.dddd,6,2)), VAL(SUBSTR(m.dddd,9,2)) )
s_o_v      = najdi_item (m.kousicek, "s_o_v")
client_key = VAL(najdi_item (m.kousicek, "client_key"))
firma_key  = VAL(najdi_item (m.kousicek, "firma_key"))
c_karty    = VAL(najdi_item (m.kousicek, "c_karty_jidla"))
nazev      = najdi_item (m.kousicek, "nazev_jidla")
objednano  = VAL(najdi_item (m.kousicek, "pocet_objednano"))
sezrano    = VAL(najdi_item (m.kousicek, "pocet_sezrano"))
INSERT INTO myObjednan FROM MEMVAR

