FUNCTION citajrastre
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd
LOCAL m.ret, m.lcClasslibWas
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/rasters/" , .F.)
Http.Option(4) = 13056 
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
Http.SetCredentials(m.or_usr,m.hor_pwd, 0)
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	IF http.status=200
		m.lcClasslibWas = SET("Classlib")
		SET CLASSLIB TO "JSON.VCX" ADDITIVE 
		m.ret = .t.
		m.loJSON = CREATEOBJECT("JSON")
		m.vysledok=lojson.parse(http.responsetext)
		EXTERNAL ARRAY raster
		DIMENSION raster(100,8)
		m.vysledok=lojson.parse(http.responsetext,"",@raster)
		RELEASE lojson
		CREATE CURSOR rastre (crc I, name c(30), variant i, ac_group i, id i, type I, vat N(5,2), code c(2),pmc L)
		FOR m.i=1 TO ALEN(raster)
			TRY 
				INSERT INTO rastre (crc, name, variant, ac_group, id, type, vat, code, pmc) VALUES(raster[m.i].cash_register_code, raster[m.i].name, raster[m.i].variant, raster[m.i].accounting_group, raster[m.i].id, raster[m.i].type, VAL(raster[m.i].vat), raster[m.i].code, raster[m.i].promote_to_cash)
			CATCH 
			ENDTRY 
		ENDFOR 
		SELECT rastre
		RELEASE raster
		SET CLASSLIB TO (m.lcClasslibWas)
	ELSE
		m.ret=.f.
	ENDIF 	
CATCH
	m.ret=.f.
ENDTRY 	
RELEASE m.http
RETURN m.ret


FUNCTION citajplatby
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd
LOCAL m.ret, m.lcClasslibWas
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST","https://localhost:7443/cash_register/paymentMethods/" , .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
Http.SetCredentials("alto","alto", 0)
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	IF http.status=200
		m.lcClasslibWas = SET("Classlib")
		SET CLASSLIB TO (LOCFILE("JSON.VCX", "VCX", "Locate JSON Class Library")) additive
		m.ret = .t.
		m.loJSON = CREATEOBJECT("JSON")
		EXTERNAL ARRAY platba
		DIMENSION platba(1,1)
		m.vysledok=lojson.parse(http.responsetext,"",@platba)
		RELEASE lojson
		CREATE CURSOR platby (type I, name c(30), code c(1), id i)
		FOR m.i=1 TO ALEN(platba)
			TRY 
				INSERT INTO platby (type, name, code, id) VALUES(platba[m.i].type, platba[m.i].name,platba[m.i].code,platba[m.i].id)
			CATCH
			ENDTRY  
		ENDFOR 
		RELEASE platba
		SET CLASSLIB TO (m.lcClasslibWas)
	ELSE
		m.ret=.f.
	ENDIF 	
CATCH
	m.ret=.f.
ENDTRY 	
RELEASE m.http
RETURN m.ret


FUNCTION citajkreditky
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd
LOCAL m.ret, m.lcClasslibWas
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST","https://localhost:7443/cash_register/paymentCardTypes/" , .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
Http.SetCredentials("alto","alto", 0)
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	IF http.status=200
		m.lcClasslibWas = SET("Classlib")
		SET CLASSLIB TO (LOCFILE("JSON.VCX", "VCX", "Locate JSON Class Library")) additive
		m.ret = .t.
		m.loJSON = CREATEOBJECT("JSON")
		EXTERNAL ARRAY kreditka
		DIMENSION kreditka(1,1)
		m.vysledok=lojson.parse(http.responaetxt,"",@kreditka)
		RELEASE lojson
		CREATE CURSOR kreditky (text c(30),bonus n(10,2), limit I, name c(30), code c(1), id i)
		FOR m.i=1 TO ALEN(kreditka)
			TRY 
				INSERT INTO kreditky (text, bonus, limit, name, code, id) VALUES(kreditka[m.i].text, VAL(kreditka[m.i].bonus), kreditka[m.i].limit, kreditka[m.i].name, kreditka[m.i].code, kreditka[m.i].id)
			CATCH 
			ENDTRY 
		ENDFOR 
		RELEASE kreditka
		SET CLASSLIB TO (m.lcClasslibWas)
	ELSE
		m.ret=.f.
	ENDIF 	
CATCH
	m.ret=.f.
ENDTRY 	
RELEASE m.http
RETURN m.ret


FUNCTION citajcurrencies
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd
LOCAL m.ret, m.lcClasslibWas
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST","https://localhost:7443/cash_register/currencies/" , .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
Http.SetCredentials("alto","alto", 0)
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	IF http.status=200
		m.lcClasslibWas = SET("Classlib")
		SET CLASSLIB TO (LOCFILE("JSON.VCX", "VCX", "Locate JSON Class Library")) additive
		m.ret = .t.
		m.loJSON = CREATEOBJECT("JSON")
		EXTERNAL ARRAY curency
		DIMENSION curency(1,1)
		m.vysledok=lojson.parse(http.responsetext,"",@curency)
		RELEASE lojson
		CREATE CURSOR currencies (round i, note c(100), units I, code c(3), deviza N(10,3), name c(30), valuta N(10,3), id i)
		FOR m.i=1 TO ALEN(curency)
			TRY 
				INSERT INTO currencies (round, units, code, deviza, name, valuta, id);
					VALUES(curency[m.i].round, curency[m.i].units, curency[m.i].code, VAL(curency[m.i].deviza), curency[m.i].name, VAL(curency[m.i].valuta), curency[m.i].id)
			CATCH
			ENDTRY 
		ENDFOR 
		RELEASE curency
		SET CLASSLIB TO (m.lcClasslibWas)
	ELSE
		m.ret=.f.
	ENDIF 	
CATCH
	m.ret=.f.
ENDTRY 	

RELEASE m.http
RETURN m.ret


FUNCTION citajizby
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd
LOCAL m.ret, m.lcClasslibWas
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST","https://localhost:7443/cash_register/currentAccounts/" , .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
Http.SetCredentials("alto","alto", 0)
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	IF http.status=200
		m.lcClasslibWas = SET("Classlib")
		SET CLASSLIB TO (LOCFILE("JSON.VCX", "VCX", "Locate JSON Class Library")) additive
		m.ret = .t.
		m.loJSON = CREATEOBJECT("JSON")
		EXTERNAL ARRAY account
		DIMENSION account(1,1)
		m.vysledok=lojson.parse(http.responsetext)
		RELEASE lojson
		CREATE CURSOR accounts (account_id i, id i, name c(30), checkout_date D, checkin_date D, code c(10))
		FOR m.i=1 TO ALEN(vysledok.groups)
			IF !ISNULL(vysledok.groups[m.i]) AND !EMPTY(vysledok.groups[m.i])
				INSERT INTO accounts (account_id, id, name,  checkout_date,  checkin_date);
					VALUES(vysledok.groups[m.i].account_id, vysledok.groups[m.i].id, vysledok.groups[m.i].name, ctod(RIGHT(vysledok.groups[m.i].checkout_date,2)+"."+SUBSTR(vysledok.groups[m.i].checkout_date,6,2)+"."+LEFT(vysledok.groups[m.i].checkout_date,4)), ctod(RIGHT(vysledok.groups[m.i].checkin_date,2)+"."+SUBSTR(vysledok.groups[m.i].checkin_date,6,2)+"."+LEFT(vysledok.groups[m.i].checkin_date,4)))
			ENDIF 
		ENDFOR 
		FOR m.i=1 TO ALEN(vysledok.rooms)
			IF !ISNULL(vysledok.rooms[m.i])
				INSERT INTO accounts (account_id, id, code,  checkout_date,  checkin_date);
					VALUES(vysledok.rooms[m.i].account_id, vysledok.rooms[m.i].id, vysledok.rooms[m.i].code, ctod(RIGHT(vysledok.rooms[m.i].checkout_date,2)+"."+SUBSTR(vysledok.rooms[m.i].checkout_date,6,2)+"."+LEFT(vysledok.rooms[m.i].checkout_date,4)), ctod(RIGHT(vysledok.rooms[m.i].checkin_date,2)+"."+SUBSTR(vysledok.rooms[m.i].checkin_date,6,2)+"."+LEFT(vysledok.rooms[m.i].checkin_date,4)))
			ENDIF 
		ENDFOR 
		RELEASE m.vysledok
		SET CLASSLIB TO (m.lcClasslibWas)
	ELSE
		m.ret=.f.
	ENDIF 	
CATCH
	m.ret=.f.
ENDTRY 	
RELEASE m.http
RETURN m.ret


FUNCTION citajhosti
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, id_izby &&accounts.id
LOCAL m.ret, m.lcClasslibWas
SET CLASSLIB TO (LOCFILE("JSON.VCX", "VCX", "Locate JSON Class Library")) additive
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST","https://localhost:7443/cash_register/currentGuests/" , .f.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
Http.SetCredentials("alto","alto", 0)
TRY 
	http.SEND('{"room_id":'+ALLTRIM(STR(m.id_izby,10,0))+'}')
	m.httpodpoved=http.statustext
	IF http.status=200
		m.lcClasslibWas = SET("Classlib")
		SET CLASSLIB TO (LOCFILE("JSON.VCX", "VCX", "Locate JSON Class Library")) additive
		m.ret = .t.
		m.loJSON = CREATEOBJECT("JSON")
		EXTERNAL ARRAY host
		DIMENSION host(1,1)
		m.vysledok=lojson.parse(http.responsetext,"",@host)
		RELEASE lojson
		CREATE CURSOR hostia (id I, first c(100), last c(100))
		FOR m.i=1 TO ALEN(host)
			TRY 
				INSERT INTO hostia (id, first, last) VALUES(host[m.i].id, host[m.i].first_name, host[m.i].last_name)
			CATCH
			ENDTRY 
		ENDFOR 
		SELECT hostia
		RELEASE host
		SET CLASSLIB TO (m.lcClasslibWas)
	ELSE
		m.ret=.f.
	ENDIF 	
CATCH
	m.ret=.f.
ENDTRY 	
RELEASE m.http
RETURN m.ret


FUNCTION citajkartu
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, id_karty &&Identifikátor zámkové karty je typu integer, zapsaný v první použitelné stopě nebo prvním použitelném poli na kartě.
LOCAL m.ret, m.lcClasslibWas
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST","https://localhost:7443/cash_register/checkCard/" , .f.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
Http.SetCredentials("alto","alto", 0)
TRY 
	http.SEND('{"card_id":'+ALLTRIM(STR(m.id_izby,10,0))+'}')
	m.httpodpoved=http.statustext
	IF http.status=200
		m.lcClasslibWas = SET("Classlib")
		SET CLASSLIB TO (LOCFILE("JSON.VCX", "VCX", "Locate JSON Class Library")) additive
		m.ret = .t.
		m.loJSON = CREATEOBJECT("JSON")
		m.vysledok=lojson.parse(m.ret)
		RELEASE lojson
		CREATE CURSOR hotkarta (result I, account_id I, room_code c(10), message c(100))
		IF !ISNULL(vysledok)
			IF vysledok.result=0
				INSERT INTO hotkarta (result, account_id, room_code) VALUES(vysledok.result, vysledok.account_id, vysledok.room_code)
			ELSE 
				INSERT INTO hotkarta (result, message) VALUES(vysledok.result, vysledok.message)
			ENDIF 		
		ELSE
			INSERT INTO hotkarta (result, message) VALUES(99, "Hores neodpovedal")
		ENDIF 
		SELECT hotkarta
		RELEASE karta
		SET CLASSLIB TO (m.lcClasslibWas)
	ELSE
		m.ret=.f.
	ENDIF 	
CATCH
	m.ret=.f.
ENDTRY 	
RELEASE m.http
RETURN m.ret


PROCEDURE natazhosta
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, acc_id, host_id 
*acc_id=10028
*host_id=0
SET CLASSLIB TO (LOCFILE("JSON.VCX", "VCX", "Locate JSON Class Library")) additive
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST","https://localhost:7443/cash_register/chargeAccount/" , .f.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
Http.SetCredentials("alto","alto", 0)
CREATE CURSOR polo (price C(10),raster_id I, currency_id I, description C(30), unit_price C(10), units C(10),vat C(5), receipt_number c(20))
INSERT INTO polo (price ,raster_id , currency_id, description, unit_price, units,vat, receipt_number) VALUES ("4.14",5,2,"Polozka 1","2.07","2","20", "u_cis")
INSERT INTO polo (price ,raster_id , currency_id, description, unit_price, units,vat, receipt_number) VALUES ("9",10,2,"Polozka 2","9","1","20", "u_cis")
*** Units moze byt iba cele cislo. Ak tam mame desatinne miesta, treba to prepocitat tak, aby ostalo mnozstvo 1 a cela suma v cene.
*** Polia typu N sa musia posielat ako stringy, inak to server interpretuje ako float a nesedia mu suciny a sucty
m.loJSON = CREATEOBJECT("JSON")
CREATE CURSOR jsontxt (jsontxt M)
INSERT INTO jsontxt (jsontxt) VALUES ("") 
REPLACE jsontxt.jsontxt WITH '{"account_id":'+ALLTRIM(STR(acc_id,10,0))+','+CHR(13)+CHR(10)
IF !EMPTY(m.host_id)
	REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"guest_id":'+ALLTRIM(STR(host_id,10,0))+','+CHR(13)+CHR(10)
ENDIF 
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"items":'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'['+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+lojson.stringify("polo")
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+STRTRAN(jsontxt.jsontxt,'"VFPData": {"polo": [','')
TRY 
	http.SEND(jsontxt.jsontxt)
	USE IN jsontxt
	IF http.status=200
		IF !ISNULL(http.responsetext)
			m.vysledok=lojson.parse(http.responsetext)
			RELEASE lojson
			TRY 
				m.ret=vysledok.result
				m.odpoved="Položky naťažené"
			CATCH
				m.ret=100
				m.odpoved=vysledok.message
			ENDTRY 
		ELSE
			m.ret=http.status
			m.odpoved=http.statustext
		ENDIF 
	ELSE
		m.ret=http.status
		m.odpoved=http.statustext
	ENDIF 
CATCH
	USE IN jsontxt
	m.ret=.f.
ENDTRY 	
RELEASE m.http
RELEASE CLASSLIB json.vcx


PROCEDURE natazpokladnu
SET CLASSLIB TO (LOCFILE("JSON.VCX", "VCX", "Locate JSON Class Library")) additive
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST","https://localhost:7443/cash_register/transferCash/" , .f.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
Http.SetCredentials("alto","alto", 0)
CREATE CURSOR polo (price C(10),raster_id I, currency_id I, payment_method_id I, note C(30), vat_price C(10), default_currency_price C(10), exchange_rate C(10), receipt_number c(20), credit_card_type_id I NULL, credit_card_number C(30))
INSERT INTO polo (price, raster_id, currency_id, payment_method_id, note, vat_price, default_currency_price, exchange_rate, receipt_number, credit_card_type_id, credit_card_number) ;
	VALUES ("200.00", 5, 2, 1, "Uzávierka 01/001", "66.67", "200.00", "1.00", "Uzávierka 01/001", 0, "")
INSERT INTO polo (price, raster_id, currency_id, payment_method_id, note, vat_price, default_currency_price, exchange_rate, receipt_number, credit_card_type_id, credit_card_number) ;
	VALUES ("100.00", 5, 2, 2, "Uzávierka 01/001", "33.34", "100.00", "1.00", "Uzávierka 01/001", 1, "5234")
REPLACE credit_card_type_id WITH null FOR credit_card_type_id=0
*** Polia typu N sa musia posielat ako stringy, inak to server interpretuje ako float a nesedia mu suciny a sucty
m.loJSON = CREATEOBJECT("JSON")
CREATE CURSOR jsontxt (jsontxt M)
INSERT INTO jsontxt (jsontxt) VALUES ("") 
REPLACE jsontxt.jsontxt WITH '[{"cash_register_code":"'+ALLTRIM("Meno kasy")+'",'+CHR(13)+CHR(10)    &&meno kasy
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"items":'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'['+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+lojson.stringify("polo")
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+']'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH STRTRAN(jsontxt.jsontxt,'"VFPData": {"polo": [','')
TRY 
	http.SEND(jsontxt.jsontxt)
	USE IN jsontxt
	IF http.status=200
		IF !ISNULL(http.responsetext)
			m.vysledok=lojson.parse(http.responsetext)
			TRY 
				m.ret=vysledok.result
				m.odpoved="Položky naťažené"
			CATCH
				m.ret=100
				m.odpoved=vysledok.message
			ENDTRY 
			RELEASE lojson
		ELSE
			m.ret=http.status
			m.odpoved=http.statustext
		ENDIF 
	ELSE
		m.ret=http.status
		m.odpoved=http.statustext
	ENDIF 
CATCH
	USE IN jsontxt
	m.ret=.f.
ENDTRY 	
RELEASE m.http
RELEASE CLASSLIB json.vcx


PROCEDURE CON_PYSTR_TO_UNICODE
LPARAMETERS m.lStringIn
LOCAL lStringOut, lStringTmp, lcToConvert, lcChrToConverPosition
 
 m.lStringTmp = m.lStringIn
 m.lStringOut = ""
 
 DO WHILE .T.
  m.lcChrToConverPosition = AT("\u", m.lStringTmp)
  IF  m.lcChrToConverPosition # 0    &&ak je este znak unicode
   m.lStringOut = m.lStringOut + LEFT(m.lStringTmp ,  m.lcChrToConverPosition-1) &&najskor cas retazca po \u
   m.lStringOut = m.lStringOut + CON_PYCHR_TO_UNICODE(SUBSTR(m.lStringTmp, m.lcChrToConverPosition,6))
   m.lStringTmp = SUBSTR(m.lStringTmp, m.lcChrToConverPosition+6)
  ELSE
   m.lStringOut = m.lStringOut + m.lStringTmp
   EXIT
  ENDIF    
 ENDDO 
RETURN m.lStringOut


*** znak pride vo formate \u017d = Ž
*** vysledok = STRCONV(CHR(0x7d)+CHR(0x01),6)
PROCEDURE CON_PYCHR_TO_UNICODE  
LPARAMETERS m.ChrToCnovert
LOCAL m.lCmd, m.lcReturn

 m.lcReturn = m.ChrToCnovert
 TRY 
  m.lCmd = "STRCONV(CHR(0x"+SUBSTR(m.ChrToCnovert,5,2)+")+CHR(0x"+SUBSTR(m.ChrToCnovert,3,2)+"),6)"
  m.lcReturn = &lCmd
 CATCH
 
 ENDTRY
RETURN  m.lcReturn
