***********************************************************************************************************************************ftp_ctichoice odvozeno z ftp_ctiobjednavku (pro YouNadMe)
*
* Cteni objednavek z ve formatu JSON (Hlobik) a ulozeni do dat_path kasy v _objednavka, okskud si
* to kasa natahne do stolu a vytiskne bony. Do _json_path se soubory dostanou klasickym Evzovym prenosem
**********************************************************************************************************************************

PARAMETERS _json_path, _dat_path
PRIVATE json, pov, kk, old_repro, maxpokus, _ok
maxpokus = 10
_json_path = ADDBS(_json_path)
_dat_path = ADDBS(_dat_path)
IF USED("cobjednavka")
	USE IN cobjednavka
ENDIF 
*"id_objednavky":11,"Stul":"Takeaway","poznamka":null,"count":4,"polozky":[{kod":"210","nazev":"Americká snídaně","cena":199,"mnozstvi":2,"dph":"1.15"},{"kod":"211","nazev":"Bezlepková snídaně","cena":189,"mnozstvi":2,"dph":"1.15"},{"kod":"212","nazev":"Vejce Benedikt Avocado","cena":165,"mnozstvi":1,"dph":"1.15"},{"kod":"213","nazev":"Horká čokoláda","cena":65,"mnozstvi":1,"dph":"1"}]}"zaplaceno":"True","druh_platby"":"Hotove"    
SELECT 0
CREATE CURSOR cobjednavka (id_objed I, stul_name C(30), poznamka C(25), ncena N(12,2), dph N(3,0), c_karty I, mnozstvi N(10,0), cnazev C(50), zaplaceno L, cdruh_pl C(8), zpracovano L)
m.nFiles = ADIR(aFiles, _json_path+"order_*.json", "AHRS", 0)
FOR m.kk = 1 TO m.nFiles
	m.json = FILETOSTR(_json_path+aFiles[m.kk,1])
	DO order_appe WITH m.json
ENDFOR
SELECT cobjednavka
IF RECCOUNT() > 0
	IF !FILE(m._dat_path+"_objednavka.dbf")
		COPY TO (m._dat_path+"_objednavka")
		m.nhand = FCREATE(m._dat_path+"_objednavka.txt")
		IF m.nhand > 0
			=FWRITE(m.nhand, TTOC(DATETIME(), 1))
			FCLOSE(m.nhand)
		ENDIF 
	ELSE
		npokus = 0
		_ok    = .F.
		DO WHILE .T.
			npokus = m.npokus + 1
			TRY
				IF !USED("_objednavka")
					USE m._dat_path+"_objednavka" IN 0
				ENDIF 
				SELECT _objednavka
				APPEND FROM DBF("cobjednavka")
				USE IN _objednavka
				m.nhand = FCREATE(m._dat_path+"_objednavka.txt")
				IF m.nhand > 0
					=FWRITE(m.nhand, TTOC(DATETIME(), 1))
					FCLOSE(m.nhand)
				ENDIF 
				m._ok = .T.
			CATCH
				WAIT "" TIMEOUT 1
			ENDTRY
			IF m._ok OR m.npokus > m.maxpokus
				EXIT 
			ENDIF 
		ENDDO
		IF m.npokus > m.maxpokus
			SELECT cobjednavka
			COPY TO (m._dat_path+SYS(2015))
		ENDIF
		IF USED("_objednavka")
			USE IN _objednavka
		ENDIF 
	ENDIF 
    *FOR m.kk = 1 TO m.nFiles
	*	ERASE _json_path+aFiles[m.kk,1])
	*ENDFOR
ENDIF
USE IN cobjednavka 

PROCEDURE order_appe
LPARAMETERS _json
LOCAL nii
*"id_objednavky":11,"Stul":"Takeaway","poznamka":null,"count":4,"polozky":[{kod":"210","nazev":"Americká snídaně","cena":199,"mnozstvi":2,"dph":"1.15"},{"kod":"211","nazev":"Bezlepková snídaně","cena":189,"mnozstvi":2,"dph":"1.15"},{"kod":"212","nazev":"Vejce Benedikt Avocado","cena":165,"mnozstvi":1,"dph":"1.15"},{"kod":"213","nazev":"Horká čokoláda","cena":65,"mnozstvi":1,"dph":"1"}]}"zaplaceno":"True","druh_platby"":"Hotove"    
m.id_objed = get_integer_item(_json, "id_objednavky")
m.poznamka = get_string_item(_json, "poznamka")
m.stul_name = ALLTRIM(get_string_item(_json, "stul"))
*m.stul_name = ALLTRIM(SUBSTR(m.stul_name, RAT(' ', m.stul_name)))
m.nPolozek = get_integer_item(_json, "count")
m.czaplaceno = get_string_item(_json, "zaplaceno")
m.zaplaceno = IIF(UPPER(m.czaplaceno)="TRUE", .T., .F.)
m.cdruh_pl = get_string_item(_json, "druh platby")
FOR m.nii = 1 TO m.nPolozek
	m.ncena 	= get_num_item(_json, "cena", m.nii)
	m.dph 		= get_num_item(_json, "dph", m.nii)
	m.cc_karty 	= get_string_item(_json, "kod", m.nii)
	m.c_karty	= VAL(ALLTRIM(m.cc_karty))
	m.mnozstvi 	= get_num_item(_json, "mnozstvi", m.nii)
	m.cnazev 	= get_string_item(_json, "nazev", m.nii)
	m.cnazev 	= STRCONV(STRCONV(m.cnazev,11),2)
	INSERT INTO cobjednavka FROM MEMVAR 	
ENDFOR 
RETURN

FUNCTION get_integer_item (json, item, poradi)
IF EMPTY(m.poradi)
    poradi = 1
ENDIF 
LOCAL at_item, texticek
at_item = ATC('"' + m.item + '":', m.json, m.poradi)
IF m.at_item > 0
   texticek = LEFT(SUBSTR(m.json, m.at_item + LEN(ALLTRIM(m.item)) + 3), 33)
   RETURN INT(VAL(m.texticek))
ELSE
   RETURN 0
ENDIF 

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION get_num_item (json, item, poradi)
IF EMPTY(m.poradi)
    poradi = 1
ENDIF 
LOCAL at_item, o_set_point, textcislo, texticek, ii, retval
at_item = ATC('"' + m.item + '":', m.json, m.poradi)
IF m.at_item > 0
   o_set_point = SET("POINT")
   SET POINT TO "."
   texticek = SUBSTR(m.json, m.at_item + LEN(ALLTRIM(m.item)) + 3)
   textcislo = ""
   FOR ii = 1 TO LEN(m.texticek)
      pismenko = SUBSTR(m.texticek, m.ii, 1)
      IF m.pismenko $ ",]}"
          EXIT 
      ELSE 
          textcislo = m.textcislo + m.pismenko
      ENDIF 
   ENDFOR 
   retval = VAL(m.textcislo)
   SET POINT TO (m.o_set_point)
   RETURN m.retval
ELSE
   RETURN .null.
ENDIF 

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
FUNCTION get_string_item (json, item, poradi)
IF EMPTY(m.poradi)
    poradi = 1
ENDIF 
LOCAL at_item, o_set_point, textcislo, texticek, ii, retval
at_item = ATC('"' + m.item + '":', m.json, m.poradi)
IF m.at_item > 0
   texticek = SUBSTR(m.json, m.at_item + LEN(ALLTRIM(m.item)) + 3)
   at_quote1 = AT('"', m.texticek, 1)
   at_quote2 = AT('"', m.texticek, 2)
   IF m.at_quote2 > 0
       retval = SUBSTR(m.texticek, m.at_quote1 + 1, m.at_quote2 - m.at_quote1-1)
       RETURN m.retval
   ELSE 
       RETURN .null.
   ENDIF 
ELSE
   RETURN .null.
ENDIF 
