*!*****************************************************************************
PROCEDURE den_v_tydnu
PARAMETERS datum_v_tydnu
TRY 
	dow_cislo = DOW(m.datum_v_tydnu, 2)
CATCH
	m.dow_cislo=1
ENDTRY 
IF !BETWEEN(m.dow_cislo,1,7) OR ISNULL(m.dow_cislo) 
	dow_cislo=1
ENDIF 
RETURN day_of_weak(m.dow_cislo)


*!*****************************************************************************
PROCEDURE kousektextu
PARAMETERS celytext, poradi

DO CASE
	CASE poradi < 1                && blby parametr, vraci cely text
		RETURN m.celytext

	CASE AT(",", m.celytext) = 0    && v textu se nenachazi oddelovac, vraci cely text
		RETURN m.celytext

	CASE poradi = 1
		RETURN LEFT(m.celytext, AT(",", m.celytext)-1)

	CASE AT(",", m.celytext, m.poradi-1) = 0    && v textu neni odd. na pozici PRED hledanym poradim
		RETURN ""

	CASE poradi > 1
		od_pozice = AT(",", m.celytext, m.poradi-1)+1
		delka = AT(",", m.celytext, m.poradi) - od_pozice
		DO CASE 
			CASE  delka < 0
				delka = 99
				RETURN SUBST(m.celytext, m.od_pozice, m.delka)
			CASE delka=0
				RETURN ""
			OTHERWISE 
			RETURN SUBST(m.celytext, m.od_pozice, m.delka)
		ENDCASE 
ENDCASE



*!*****************************************************************************
*!      Function: GETMODUL
*!*****************************************************************************
PROCEDURE getmodul
PARAMETERS lcjm_mod
PRIVATE old_sel
SET HELP OFF
PUSH KEY CLEAR
old_sel = SELE()
SELECT strediska
SET FILT TO m.lcjm_mod $ kas_man
IF prazdna()
	USE
	SELE (old_sel)
	POP KEY
	SET HELP ON
	RETURN ""
ELSE
	DO FORM dajdruh WITH "","strediska","id",50,"id_zkratka",200 TO m.modulxx
	IF EMPTY(m.modulxx)
		man_nazev = ""
		part_path = ""
		part_param = ""
		m.modulxx=""
	ELSE
		SELECT strediska
		LOCATE FOR ID=m.modulxx
		IF FOUND()
			man_nazev = strediska.id_zkratka
			part_path = ALLTRIM(strediska.dat_path)
			part_param = strediska.pgm_param
		ELSE
			man_nazev = ""
			part_path = ""
			part_param = ""
			m.modulxx = ""
		ENDIF
	ENDIF
	SET FILTER TO
	SELECT (old_sel)
	POP KEY
	SET HELP ON
	RETURN m.modulxx
ENDIF

*!*****************************************************************************
PROCEDURE del_record
IF DELETED()
	RECALL
ELSE
	DELETE
ENDIF


*!*****************************************************************************
*!      Procedure: CHECKIT
*!*****************************************************************************
PROCEDURE checkit
PUSH KEY CLEAR
REPLACE LABEL WITH IIF (EMPTY(LABEL), m.label_yes, "")
IF RECNO() > RECCOUNT()
	GO TOP
ELSE
	SKIP 1
ENDIF (RECNO() > RECCOUNT())
CLEAR TYPEAHEAD
POP KEY

*!*****************************************************************************
*!      Procedure: CHECKALL
*!*****************************************************************************
PROCEDURE checkall
PARAMETERS ON
PUSH KEY CLEAR
PRIVATE old_rec
old_rec = RECNO()
REPLACE ALL LABEL WITH IIF(m.on, m.label_yes, " ")
IF m.old_rec <= RECCOUNT()
	GOTO (m.old_rec)
	KEYBOARD "{DnArrow}{UpArrow}"
ELSE
	KEYBOARD "{UpArrow}{DnArrow}"
ENDIF (m.old_rec <= RECCOUNT())
CLEAR TYPEAHEAD
POP KEY

*!*****************************************************************************
*!      Procedure: SETKEYS
*!*****************************************************************************
PROCEDURE setkeys
PARAM nic
PRIV druhy_taky
druhy_taky = PARAM() > 0
*DO mssg WITH IIF(m.druhy_taky, jaz("spc-f2-f3-f7-f8-f10"), jaz("spc-f2-f3-f10"))
CLEAR TYPEAHEAD
PUSH KEY CLEAR
ON KEY LABEL spacebar  DO checkit
ON KEY LABEL f2        DO checkall WITH .F.
ON KEY LABEL f3        DO checkall WITH .T.
ON KEY LABEL f10       KEYBOARD "{Ctrl+W}"
IF m.druhy_taky
	ON KEY LABEL f7 DO add_druh WITH .F.
	ON KEY LABEL f8 DO add_druh WITH .T.
ENDIF
ON KEY LABEL ctrl+HOME GO TOP
ON KEY LABEL ctrl+END  GO BOTTOM
CLEAR TYPEAHEAD

*!*****************************************************************************
*!      Procedure: CLRKEYS
*!*****************************************************************************
PROCEDURE clrkeys
CLEAR TYPEAHEAD
POP KEY
*DO mssg WITH ""

*!*****************************************************************************
*!      Procedure: KEYS_ON
*!*****************************************************************************
PROC keys_on
*DO mssg WITH jaz("f4, f5, f10, f12 help")
GO TOP
ON KEY LABEL f10 KEYBOARD "{Ctrl+W}"
ON KEY LABEL f5 KEYBOARD "{Ctrl+N}"
ON KEY LABEL f4 DO del_record
ON KEY LABEL f12 ACTIVATE WINDOW calculator

*!*****************************************************************************
*!      Procedure: ADD_DRUH
*!*****************************************************************************
PROC add_druh
PARAM on_off
PUSH KEY CLEAR
CLEAR TYPEAHEAD
PRIVATE rn, dr
rn = RECNO()
dr = druh
REPLACE FOR druh = m.dr LABEL WITH IIF(m.on_off, m.label_yes, " ")
IF rn <= RECC()
	GO (m.rn)
ENDIF (rn <= RECC())
POP KEY

*!*****************************************************************************
*!      Procedure: ADD_DRUH
*!*****************************************************************************
PROCEDURE clrkeys
CLEAR TYPEAHEAD
POP KEY

PROC working
RETU

*!*****************************************************************************
*!      Procedure: SK2_BUFF
*!*****************************************************************************
PROC sk2_buff
IF USED("buffer")
	SELE BUFFER
	APPE FROM (m.gcpri_path + "sk2")
ELSE
	COPY ALL TO (m.fn_buffer)
	SELE 0
	USE (m.fn_buffer) ALIAS BUFFER EXCL
ENDIF

*!*****************************************************************************
PROC buff_1_2
SELE b.c_dokl, b.poradi, b.c_karty, - b.mnozstvi AS mnozstvi, b.cena, ;
	bourak.c_karty2, SPACE(30) AS nazev, SPACE(30) AS nazev2, ;
	- ROUND(b.mnozstvi * bourak.koef_mnoz, 4)      AS mnozstvi2, ;
	ROUND(bourak.koef_ceny, 4)                     AS cena2, ;
	b.cena AS cena_part, SPACE(m.sirkastlpcadruh) AS druh ;
	WHERE c_dokl = vyber.c_dokl ;
	FROM (m.gcpri_path + "buff_1") b ;
	LEFT JOIN (m.fn_bourak) bourak ON bourak.c_karty == b.c_karty AND bourak.bour_skup == m.gcbour_skup ;
	INTO TABLE (m.gcpri_path + "buff_2")
** bacha, v cena2 je koef_ceny !!

REPL FOR ISNULL(c_karty2) c_karty2 WITH c_karty, cena2 WITH 1, mnozstvi2 WITH mnozstvi
REPL FOR cena2 = 0        c_karty2 WITH c_karty, cena2 WITH 1, mnozstvi2 WITH mnozstvi
REPL ALL cena2 WITH ROUND(cena / cena2, 4)
SET RELA TO c_karty INTO karty
REPL ALL nazev WITH karty.nazev
SET RELA TO c_karty2 INTO karty
IF "SKL" $ m.gcpgm_param
	REPL ALL nazev2 WITH karty.nazev, druh WITH karty.druh_skl
ELSE
	REPL ALL nazev2 WITH karty.nazev, druh WITH karty.druh
ENDIF
SET RELA TO
GO TOP

*!*****************************************************************************
PROC buff_bour
SELE b.c_karty, - b.mnozstvi AS mnozstvi, b.cena, ;
	bourak.c_karty2, b.nazev, SPACE(30) AS nazev2, ;
	- ROUND(b.mnozstvi * bourak.koef_mnoz, 4)      AS mnozstvi2, ;
	ROUND(bourak.koef_ceny, 4)                AS cena2, ;
	b.cena AS cena_part ;
	WHERE b.c_karty = vyber.c_karty ;
	FROM (m.gcpri_path + "buff_1") b ;
	LEFT JOIN (m.fn_bourak) bourak ON bourak.c_karty == b.c_karty AND bourak.bour_skup = m.gcbour_skup ;
	INTO TABLE (m.gcpri_path + "buff_2")
** bacha, v cena2 je koef_ceny !!

REPL FOR ISNULL(c_karty2) c_karty2 WITH c_karty, cena2 WITH 1, mnozstvi2 WITH mnozstvi
REPL FOR cena2 = 0        c_karty2 WITH c_karty, cena2 WITH 1, mnozstvi2 WITH mnozstvi
REPL ALL cena2 WITH ROUND(cena / cena2, 4)
*!*	set rela to c_karty into karty
*!*	repl all nazev with karty.nazev
SET RELA TO c_karty2 INTO karty
REPL ALL nazev2 WITH karty.nazev
SET RELA TO
GO TOP

*!*****************************************************************************
PROCEDURE genzustat
SELECT c_dokl, c_karty, MAX(zustatek) AS zustatek FROM (m.fn_obraty) GROUP BY 1,2 INTO CURSOR zust1 ;
	WHERE c_stredisk = m.gic_stredisk AND c_dokl > m.c_dokl AND (LEFT(operace,1) IN ("P", "V"))

SELECT c_karty, MIN(zustatek) AS zustatek FROM zust1 GROUP BY 1 INTO CURSOR zustatky

*!*****************************************************************************
PROC genc_kar
RETURN i_max("karty")

*!*****************************************************************************
PROCEDURE buff_cre
PARAM prazdny
PRIVATE odkial
DO CASE
	CASE USED("menaz")
		m.odkial="menaz"
	CASE USED("karty")
		m.odkial="karty"
	CASE USED("sklad")
		m.odkial="sklad"
	OTHERWISE
		SELECT 0
		USE (m.fn_hesla) ALIAS odkial AGAIN
		m.odkial="odkial"
ENDCASE
SELECT 0

SELECT 00000000 as idriadok,0000000000 AS c_stredisk, 0000000000 AS c_dokl, 0000000000 AS c_partner, 0000000000 AS doklad, DATE() AS datum_dokl, SPACE(2) AS operace,;
	SPACE(15) AS pz1, SPACE(15) AS pz2, SPACE(5) AS platba, $0 AS trzba1, $0 AS trzba2, $0 AS trzba3, $0 AS trzba4, 00 AS splatnost, $0 AS sleva_abs,;
	$0 AS sleva_proc, SPACE(2) AS cen_hlad,  $0 AS zaloha, $0 AS halere, SPACE(3) AS provedl, DATETIME() AS kdy, 0000000000 AS c_mena, SPACE(30) AS partner ;
	FROM (m.odkial) INTO TABLE (m.fn_bufdokl) WHERE .F.
DELETE ALL
PACK
IF PARAM() = 0
	APPE BLANK
	REPL datum_dokl WITH DATE()
ENDIF

SELE 0
SELECT 00000000 as idriadok,0000000000 AS c_karty, SPACE(30) AS nazev, $0 AS mnozstvi, $0 AS dluh_mnoz, $0 AS cena, $0 AS cena_celk, $0 AS cena_part,;
	$0 AS na_sklade, $0 AS zustatek,SPACE(m.sirkastlpcadruh) AS druh, 0000000000 AS c_stredisk, 0000000000 AS poradi, SPACE(2) AS operace, SPACE(1) AS LABEL,;
	0.000 AS dan, SPACE(20) AS serial_no, 00000.0000 AS pov_objem, .F. AS zmaz, 0000000000 AS c_dokl,0000000000 as c_druh, $0 as spdan,$0 as spstupne,$0 as lieh FROM (m.odkial) INTO TABLE (m.fn_buffer) WHERE .F.
DELETE ALL
PACK
IF PARAM() = 0
	APPE BLANK
ENDIF
SELE 0
IF m.odkial="odkial"
	USE IN odkial
ENDIF

*!*****************************************************************************
PROC petra
LPARAM txt1
txt1 = STRTR(m.txt1, '    ' , ' ')
txt1 = STRTR(m.txt1, '   '  , ' ')
RETU   STRTR(m.txt1, '  '   , ' ')


*!*****************************************************************************
PROCEDURE copyright_txt
RETURN UPPE(m.repo_name) + gccopyright + "  Licensed for " + ALLT(m.hotel_name) + ", " + ALLT(m.hotel_dico) + "  " + DTOC(DATE()) + " " + LEFT(TIME(), 5) + " "  + m.gcuser_id


*!*****************************************************************************
PROCEDURE zurnal_log
LPARAMETERS parch1, parch2, parch3, parnum1, parnum2, parnum3, parch4
PRIVATE m.hladajmodul, m.hladajrok, m.hladajmesiac,old_sel
LOCAL lc1, lc2, lc3, lc4, lcpozn
old_sel=SELECT()
lcvyznam = parch1
parch2   = IIF(EMPTY(m.parch2), "", ALLTRIM(m.parch2))
parch3   = IIF(EMPTY(m.parch3), "", ALLTRIM(m.parch3))
parch4   = IIF(EMPTY(m.parch4), "", ALLTRIM(m.parch4))
lcch3    = STRTRAN(parch2, '"', "'")
lcch4    = STRTRAN(parch3, '"', "'")
parnum1  = IIF(EMPTY(parnum1), 0, m.parnum1)
parnum2  = IIF(EMPTY(parnum2), 0, m.parnum2)
parnum3  = IIF(EMPTY(parnum3), 0, m.parnum3)
lcpozn   = m.gcverze+" "+STRTRAN(parch4, '"', "'")

DO CASE
	CASE INLIST(m.lcvyznam, "OPERA", "xx")
		IF EMPT(lcch4)
			lcch4 = gcid_zkratka
		ENDIF
        lnnum1 = cislo_to_str(m.parnum1)
        lnnum2 = cislo_to_str(m.parnum2)
        lnnum3 = cislo_to_str(m.parnum3)

		lc1    = RIGHT(DTOS(DATE()),2)
		lc2    = LEFT(TIME(),5)
		lc3    = m.lcch4
		lc4    = m.gcuser_id
		lcpozn = m.gcverze+" "+m.lcch3
		retez = '"' + m.lcvyznam + '","' + m.lc1 + 'T' + m.lc2 + '","' + m.lc3 + '","' ;
			+ m.lc4 + '",' + m.lnnum1 + ',' + m.lnnum2 + ','  + m.lnnum3 + ',"' + m.lcpozn + '"'

	CASE INLIST(m.lcvyznam, "START", "UZAVE")
		m.lc1=DTOS(DATE())
		m.lc2=LEFT(TIME(),5)
		m.lc3=m.gcid_zkratka
		m.lc4=m.gcuser_id + " " + m.gcusername
		m.lcpozn=m.gcverze+" "+"SKL " + m.machine_semaf
        m.retez = '"' + m.lcvyznam + '","' + m.lc1 + 'T' + m.lc2 + '","' + m.lc3 + '","' + m.lc4 + '",,,,"SKL ' + m.machine_semaf + '"'

ENDCASE

IF m.issql
	hladajmodul=2
	hladajrok=0
	hladajmesiac=0
	rv("zurnaladd","zurnal")
	IF USED("zurnal")
		=CURSORSETPROP("Buffering" ,5, "zurnal")
		INSERT INTO zurnal(MODUL, rok,    mesiac,        machine      ,    autor,      vyznam,     c1,    c2,    c3,    c4,        n1,        n2,        n3,   pozn) ;
        	   VALUES (2, YEAR(DATE()), MONTH(DATE()), m.machine_semaf, m.gcuser_id, m.lcvyznam,m.lc1, m.lc2, m.lc3, m.lc4, m.parnum1, m.parnum2, m.parnum3, m.lcpozn)
		=tux(.T.,.T.,"zurnal")
		USE IN zurnal
	ENDIF 
ELSE 

    TRY      && kdyby nebyla dostupna promenna gcdat_path, ulozi se zurnal do aktualniho adresare
        Fn_Zurnal = m.gcdat_path + "zu" + SUBST(DTOS(DATE()), 3, 4)
    CATCH
        Fn_Zurnal =      ".\"    + "zu" + SUBST(DTOS(DATE()), 3, 4)
    ENDTRY 
    _pripona = ""
    _zapsano = STRTOFILE(m.retez + CHR(13)+CHR(10), m.fn_zurnal + m._pripona + ".txt", 1)
    DO WHILE m._zapsano = 0 .and. _pripona < "z"
       _iii = 0.1
       DO WHILE m._zapsano = 0 .and. m._iii < 1
         _zapsano = STRTOFILE(m.retez + CHR(13)+CHR(10), m.fn_zurnal + m._pripona + ".txt", 1)
		_iii = m._iii + 0.3
         WAIT jaz("form_cakajte_otvaram")+ " " + m.fn_zurnal + m._pripona + str(m._iii*10, 5) WINDOW TIMEOUT m._iii
       ENDDO
       _pripona = IIF(EMPTY(_pripona), "a", CHR(ASC(m._pripona) + 1))
    ENDDO 

ENDIF 
SELECT (m.old_sel)


*!*****************************************************************************
FUNCTION cislo_to_str (cisilko)
DO CASE 
CASE EMPT(cisilko)
    RETURN "0"
CASE cisilko = INT(cisilko)     && nema desetinnou cast
    RETURN ALLT(STR(m.cisilko, 12))
OTHERWISE 
    RETURN STRTR(ALLT(STR(m.cisilko, 12,4)) , ",", ".")
ENDCASE 


*!*****************************************************************************
PROCEDURE limit_log
LPARAMETERS retez, n1,n2,n3
PRIVATE m.hladajmodul, m.hladajrok, m.hladajmesiac
IF EMPTY(n1)
	m.n1=0
ENDIF 
IF EMPTY(n2)
	m.n2=0
ENDIF 
IF EMPTY(n3)
	m.n3=0
ENDIF 
lnnum1 = IIF (EMPT(m.n1), "0", STRTR(ALLT(STR(m.n1, 12,4)) , ",", ".")) + ","
lnnum1 = STRTR(lnnum1, "000,", ",")
lnnum1 = STRTR(lnnum1, "00,", ",")
lnnum1 = STRTR(lnnum1, "0,", ",")
lnnum1 = STRTR(lnnum1, ".,", ",")
lnnum2 = IIF (EMPT(m.n2), "0", STRTR(ALLT(STR(m.n2, 12,4)) , ",", ".")) + ","
lnnum2 = STRTR(lnnum2, "000,", ",")
lnnum2 = STRTR(lnnum2, "00,", ",")
lnnum2 = STRTR(lnnum2, "0,", ",")
lnnum2 = STRTR(lnnum2, ".,", ",")
lnnum3 = IIF (EMPT(m.n3), "0", STRTR(ALLT(STR(m.n3, 12,4)) , ",", ".")) + ","
lnnum3 = STRTR(lnnum3, "000,", ",")
lnnum3 = STRTR(lnnum3, "00,", ",")
lnnum3 = STRTR(lnnum3, "0,", ",")
lnnum3 = STRTR(lnnum3, ".,", ",")
m.retez='"OPERA","' + RIGHT(DTOS(DATE()),2) + '","' + LEFT(TIME(),5) + '",".","'+m.gcuser_id+'","'+ m.lnnum1 + m.lnnum2 + m.lnnum3 + '"' +m.retez+'"'
IF m.issql
	m.hladajmodul=4
	m.hladajrok=0
	m.hladajmesiac=0
	rv("zurnaladd","zurnal")
	=CURSORSETPROP("Buffering" ,5, "zurnal")
	INSERT INTO zurnal(MODUL, rok, mesiac, pozn, machine, autor, c1, c2, c4, vyznam, n1,n2,n3) VALUES (4, YEAR(DATE()), MONTH(DATE()), m.retez, m.machine_semaf, m.gcuser_id, RIGHT(DTOS(DATE()),2),LEFT(TIME(),5), m.gcuser_id,"OPERA", m.n1, m.n2, m.n3)
	=tux(.T.,.T.,"zurnal")
	USE IN zurnal
ELSE 
	fn_zurn = m.gcdat_path + "zu" + SUBST(DTOS(DATE()), 3, 4) + ".txt"

	IF .NOT. FILE(m.fn_zurn)
		fh__zur = FCREA(m.fn_zurn)
		= FPUTS(m.fh__zur, "vyznam,char1,char2,char3,char4,num1,num2,num3,pozn")
		= FCLOSE(m.fh__zur)
	ENDIF

	fh__zur = -99
	_iii = 0.1

	DO WHILE m.fh__zur < 0 .AND. m._iii < 1
		fh__zur = FOPEN(m.fn_zurn, 2)
		IF _iii > .3
			WAIT jaz("form_cakajte_otvaram")+ " " + m.fn_zurn + STR(m._iii*10, 5) WIND TIME m._iii
		ENDIF
		_iii = m._iii + 0.2
	ENDDO

	IF m.fh__zur > 0
		= FSEEK(m.fh__zur, 0, 2)
		= FPUTS(m.fh__zur, m.retez)
		= FCLOSE(m.fh__zur)
	ENDIF
	= FCLOSE(m.fh__zur)
ENDIF 


*!*****************************************************************************
PROCEDURE kuchmssg
CLOS DATA
IF m.issql
	DO open_database
	rv('fooddat','dk_fd')
	SELE DISTI id, id_zkratka WHERE "KAS" $ pgm .OR. "K32" $ pgm and "MASTER" $ pgm_param ;
		FROM dk_fd INTO cursor kasymssg readwrite 
	USE IN dk_fd
	SELECT "kasymssg"	
	DO FORM dajdruh WITH "", "kasymssg", "id", 50,"id_zkratka",200 TO m.hladajid
		
ELSE 
	SELE DISTI id,dat_path AS kas_path, id_zkratka WHERE "KAS" $ pgm .OR. "K32" $ pgm and "MASTER" $ pgm_param;
		FROM (m.fn_food_dat) INTO cursor kasymssg READWRITE
	DO FORM dajdruh WITH "", "kasymssg", "id", 50,"id_zkratka",200 TO m.hladajid	
ENDIF 	

 IF m.issql
  rv('zpravy',,5)
  DO FORM kuchmssg
 ELSE
 	IF FILE(ALLT(kasymssg.kas_path) + "zpravy.dbf")
 	   	USE (ALLT(kasymssg.kas_path) + "zpravy") ALIAS zpravy
		SELECT zpravy
		DO FORM kuchmssg
	ELSE
		WAIT jaz("prg_subor_zpravy_neexistuje") WIND TIME 2 AT 22,22
	ENDIF
 ENDIF 	
CLOS DATA

*!*****************************************************************************
PROCEDURE cpzero
PARAMETER m.fname

PRIVATE m.fp_in
fp_in = FOPEN(m.fname,2)
IF m.fp_in > 0
	=FSEEK(m.fp_in,29)
	=FWRITE(m.fp_in,CHR(0))
	=FCLOSE(m.fp_in)
ENDIF

*!*****************************************************************************
FUNCTION uniqbark2
PARAMETERS kodx, riadok
PRIVATE m.ori_naz, menodbf
m.ori_naz=""
m.kodx=PADR(ALLTRIM(m.kodx),13)
*!*	m.menodbf=SELECT()
m.menodbf=ALIAS()
SELECT DISTINCT ALLTRIM(bar_kod2) AS bar_kod2, nazev FROM (m.menodbf) INTO CURSOR aaaakod WHERE c_karty<>m.riadok
INDEX ON PADR(ALLTRIM(bar_kod2),13) TAG bar_kod2
IF !SEEK(m.kodx)
	m.kodx=""
ELSE
	m.ori_naz=nazev
	SELECT aaaakod
	DO WHILE .T.
		m.kodx=PADR(ALLTRIM(STR(VAL(m.kodx)+1,13,0)),13)
		IF !SEEK(m.kodx)
			EXIT
		ENDIF
	ENDDO
ENDIF
USE IN aaaakod
SELECT (m.menodbf)
RETURN m.kodx+m.ori_naz


*!*****************************************************************************
FUNCTION novestredisko
PARAMETERS cstr
PRIVATE oldsel, usedfooddat
m.oldsel=SELECT()
IF !USED("food_dat")
	SELECT 0
	DO daj with m.gchom_path + "food_dat",'ps_rfooddat','food_dat'
	m.usedfooddat=.F.
ELSE
	SELECT food_dat
	m.usedfooddat=.T.
	SET FILTER TO 
ENDIF
IF VARTYPE(m.cstr)="C"
	LOCATE FOR ALLTRIM(ID) = ALLTRIM(m.cstr)
ELSE
	LOCATE FOR c_stredisk = m.cstr
ENDIF

IF FOUND()

	DO prepni_sklad

	IF m.slovenskaverzia
		SELECT 0
		USE (m.fn_uctobd) ALIAS uctobd
		LOCATE FOR c_stredisk=m.ric_stredisk
		IF FOUND()
			IF uctobd.uctobd<>m.uctobd
				USE IN uctobd
				SELECT (m.oldsel)
				=MESSAGEBOX(jazparam("form_ine_obdobie_na_stredisku",m.gcid_zkratka),16,"FoodMan")
				RETURN .F.
			ELSE
				USE IN uctobd
			ENDIF
		ELSE
			USE IN uctobd
		ENDIF
	ENDIF 

	IF !m.usedfooddat
		USE IN food_dat
	ENDIF
	SELECT (m.oldsel)
	RETURN .T.
ELSE
	SELECT (m.oldsel)
	RETURN .F.
ENDIF




*!*****************************************************************************
PROCEDURE vratstredisko
PRIVATE bolazaloha
bolazaloha=("buf"$m.fn_obraty)
gcdat_path   = m.rcdat_path
gccen_path   = m.rccen_path
gcpgm_id     = m.rcpgm_id
gcpgm        = m.rcpgm
gcid_zkratka = m.rcid_zkratka
gcpgm_param  = m.rcpgm_param
gcsetup_skup = m.rcsetup_skup
gcbour_skup  = m.rcbour_skup
gcpgmskupina = m.rcpgmskupina
gcstredskup  = m.rcstredskup
gczadan_prn  = m.rczadan_prn
gcera_cen    = m.rcera_cen
gic_stredisk = m.ric_stredisk
gcprava_skup = m.rcprava_skup
fn_eany      = m.rn_eany
fn_karty     = m.rn_karty
fn_expfaktura= m.rn_expfaktura
fn_kkarty    = m.rn_kkarty
fn_kceny     = m.rn_kceny
fn_ktexty    = m.rn_ktexty
fn_krest     = m.rn_krest
fn_skupiny   = m.rn_skupiny
fn_vypocty   = m.rn_vypocty
fn_bourak    = m.rn_bourak
fn_umisteni  = m.rn_umisteni
fn_listovanie= m.rn_listovanie
fn_partneri  = m.rn_partneri
fn_polozky   = m.rn_polozky
fn_sklad     = m.rn_sklad
fn_hh_hlav   = m.rn_hh_hlav
fn_hh_druhy  = m.rn_hh_druhy
fn_hh_kalky  = m.rn_hh_kalky
fn_doklady   = m.rn_doklady
fn_obraty    = m.rn_obraty
fn_obr_alk   = m.rn_obr_alk
fn_dokl_a    = m.rn_dokl_a
fn_obra_a    = m.rn_obra_a
fn_dokl_sto  = m.rn_dokl_sto
fn_obra_sto  = m.rn_obra_sto
fn_buff_r	 = m.rn_buff_r
fn_buff_d	 = m.rn_buff_d
fn_davky     = m.rn_davky
fn_dluhy     = m.rn_dluhy
fn_uzav      = m.rn_uzav
fn_uzav_pohyby= m.rn_uzav_pohyby
fn_parovaky  = m.rn_parovaky
fn_uctobd    = gchom_path + "uctobd"
fn_vahinv    = m.fn_vahinv
fn_dokl_s    = m.rn_doklady    && tyhle 2 promenne se pouzivaji v sestavach, resi se jimi prepinani archivu
fn_obra_s    = m.rn_obraty
fn_limity    = m.rn_limity
fn_limitdni  = m.rn_limitdni
fn_limitrov  = m.rn_limitrov
fn_limit_hl  = m.rn_limit_hl
fn_limitchody= m.rn_limitchody
fn_limittypy = m.rn_limittypy
fn_limittypchody = m.rn_limittypchody
fn_limitsablhl = m.rn_limitsablhl
fn_limitsablpol = m.rn_limitsablpol
fn_limitsabldni = m.rn_limitsabldni
fn_limithladiny = m.rn_limithladiny
fn_limitrovhladiny = m.rn_limitrovhladiny

fn_sk_sez    = m.rn_sk_sez
fn_sk_pol    = m.rn_sk_pol
fn_sk_fin    = m.rn_sk_fin
fn_sk_plt    = m.rn_sk_plt
fn_sk_txt    = m.rn_sk_txt
fn_sk_fis    = m.rn_sk_fis
fn_sk_vklvy    = m.rn_sk_vklvy
fn_sk_sez_a  = m.rn_sk_sez_a
fn_sk_pol_a  = m.rn_sk_pol_a
fn_sk_fin_a  = m.rn_sk_fin_a
fn_sk_plt_a  = m.rn_sk_plt_a
fn_sk_txt_a  = m.rn_sk_txt_a
fn_sk_fis_a  = m.rn_sk_fis_a
fn_sk_vklvy_a = m.rn_sk_vklvy_a
fn_umdruhy   = m.rn_umdruhy
fn_spotrebnadan = m.rn_spotrebnadan
fn_objednan  = gchom_path + "objednan"
fn_jidelak   = gchom_path + "jidelak"
fn_users     = gchom_path + "users"
gcid_klapky = m.rcid_klapky
STORE "NIC" TO operace, operace_txt, d1_txt, d1_dat_dokl, d2_txt, d2_dat_dokl
IF m.bolazaloha
	fn_obraty   = STRTR(m.fn_obraty,  "obraty",  "obra_buf")
	fn_doklady  = STRTR(m.fn_doklady, "doklady", "dokl_buf")
	fn_dokl_s	= m.fn_doklady
	fn_obra_s	= m.fn_obraty
	fn_sklad    = m.gcpri_path + "skla_buf"
ENDIF 
man_edi_kod=m.rcman_edi_kod


*!*****************************************************************************
FUNCTION i_zkrat
PARAMETER m.modul_id, m
PRIVATE m.hladajid, m.hladajcstredisk, o_sel, modul_zkr, bolfooddat, pary, pokracuj
pary=PARAMETERS()
modul_zkr=m.gcid_zkratka
pokracuj=.f.
bolfooddat=.t.
osel = SELECT()
IF m.issql AND DBUSED("food600_ps")
	SET DATABASE TO  food600_ps
	pokracuj=.t.
ENDIF 
IF !m.issql AND DBUSED("food600")
	SET DATABASE TO  food600
	pokracuj=.t.
ENDIF 

IF m.pokracuj
	SELECT 0
	IF m.issql
		IF VARTYPE(m.modul_id)="C"
			m.hladajid=m.modul_id
			DO CASE 
				CASE USED("food_dat")
					SELECT * FROM food_dat WHERE id=m.hladajid INTO CURSOR zkrstred READWRITE 
				CASE USED("fd")
					SELECT * FROM fd WHERE id=m.hladajid INTO CURSOR zkrstred READWRITE 
				CASE USED("fp_str")
					SELECT * FROM fp_str WHERE id=m.hladajid INTO CURSOR zkrstred READWRITE 
				OTHERWISE  
					USE (m.sqlprefix+"rfooddatid") ALIAS zkrstred IN 0
			ENDCASE 
		ELSE
			m.hladajcstredisk=m.modul_id
			DO CASE 
				CASE USED("food_dat")
					SELECT * FROM food_dat WHERE c_stredisk=m.hladajcstredisk INTO CURSOR zkrstred READWRITE 
				CASE USED("fd")
					SELECT * FROM fd WHERE c_stredisk=m.hladajcstredisk INTO CURSOR zkrstred READWRITE 
				CASE USED("fp_str")
					SELECT * FROM fp_str WHERE c_stredisk=m.hladajcstredisk INTO CURSOR zkrstred READWRITE 
				OTHERWISE  
					USE (m.sqlprefix+"rfooddatcs") ALIAS zkrstred IN 0
			ENDCASE 
		ENDIF 
	ELSE 
		IF !USED("food_dat")
			USE (m.fn_food_dat) ALIAS food_dat IN 0 AGAIN 
			m.bolfooddat=.f.
		ENDIF 
		IF VARTYPE(m.modul_id)="C"
			m.hladajid=m.modul_id
			USE vfooddatid ALIAS zkrstred IN 0
		ELSE
			m.hladajcstredisk=m.modul_id
			USE vfooddatcs ALIAS zkrstred IN 0
		ENDIF 
		IF !m.bolfooddat
			USE IN food_dat
		ENDIF 
	ENDIF 
	IF RECCOUNT("zkrstred")>0
		SELECT zkrstred
		GO TOP
		IF !ISNULL(zkrstred.id_zkratka)
		 	m.modul_zkr=zkrstred.id_zkratka
		ELSE
			IF VARTYPE(m.modul_id)="C"
				m.modul_zkr=m.modul_id
			ELSE
				m.modul_id=ALLTRIM(STR(m.modul_id,5,0))
			ENDIF 
		ENDIF 
	ELSE
		IF VARTYPE(m.modul_id)="C"
		 	IF LOWER(m.modul_id)='xx'
				m.modul_zkr="Všetky kasy"
			ELSE 
 				m.modul_zkr=m.modul_id
 			ENDIF 
		ELSE
			m.modul_id=ALLTRIM(STR(m.modul_id,5,0))
		ENDIF 
	ENDIF 
	USE IN zkrstred
ELSE 
	IF m.pary = 2       && MANAGERY
		SELE 0
		USE (m.gcpri_path + "strediska") ALIAS managery
		IF VARTYPE(m.modul_id)="C"
			LOCATE FOR managery.ID = m.modul_id
			IF FOUND()
				modul_zkr = managery.id_zkratka
			ELSE
				modul_zkr = m.modul_id
			ENDIF
			IF LEFT(m.gcid_zkratka,2) = m.modul_id
				modul_zkr = (m.gcid_zkratka)
			ENDIF
		ELSE
			LOCATE FOR managery.c_stredisk = m.modul_id
			IF FOUND()
				modul_zkr = managery.id_zkratka
			ELSE
				modul_zkr = m.modul_id
			ENDIF
		ENDIF 
		USE
	ELSE
		SELE 0
		USE (m.gcpri_path + "strediska") ALIAS kasy
		IF LOWER(m.modul_id)='xx'
			m.modul_zkr='Všetky kasy'
		ELSE
			LOCATE FOR kasy.ID = m.modul_id
			IF FOUND()
				modul_zkr = kasy.id_zkratka
			ELSE
				modul_zkr = m.modul_id
			ENDIF
		ENDIF
		USE
	ENDIF
ENDIF 
SELE(osel)
RETURN modul_zkr


*!*****************************************************************************
FUNCTION odstranprefix
PARAMETERS kod
kod=ALLTRIM(m.kod)
FOR m.i=1 TO LEN(m.kod)
	m.kod1=SUBSTR(m.kod,1,1)
	DO CASE
		CASE !ISDIGIT(m.kod1)
			m.kod=SUBSTR(m.kod,2)
		CASE m.kod1="0"
			m.kod=SUBSTR(m.kod,2)
		OTHERWISE
			EXIT
	ENDCASE
ENDFOR
RETURN m.kod


*!*****************************************************************************
FUNCTION nastavdatumdokladu
PARAMETERS oper, c_str
PRIVATE ret,o_sel, m.boladb, m.hladajcstredisk
o_sel=SELECT()
SELECT 0
boladb=.f.
DO open_database
IF !m.slovenskaverzia
	m.uctobd = LEFT(DTOS(DATE()), 6)
ENDIF 
IF !("buf"$m.fn_obraty)
	IF EMPTY(m.c_str)
		m.hladajcstredisk=m.gic_stredisk
	ELSE
		m.hladajcstredisk=m.c_str
	ENDIF 
	IF m.uctobd = LEFT(DTOS(DATE()),6)
		m.ret = DATE()
	ELSE 
		IF m.issql
			USE (m.sqlprefix+"rnastavdatdokl") ALIAS uctdatum
		ELSE
			USE vnastavdatdokl ALIAS uctdatum
		ENDIF 
	*	SELECT MAX(datum_dokl) as datum_dokl FROM doklady WHERE c_stredisk = m.gic_stredisk .and. operace = m.oper INTO CURSOR uctdatum
		DO CASE 
			CASE RECCOUNT() = 0 .or. LEFT(DTOS(uctdatum.datum_dokl),6) < m.uctobd .or. ISNULL(uctdatum.datum_dokl)
				m.ret = DATE(VAL(LEFT(m.uctobd,4)), VAL(RIGHT(m.uctobd,2)), 1)
			CASE LEFT(DTOS(uctdatum.datum_dokl),6) > m.uctobd
				m.ret = GOMONTH(DATE(VAL(LEFT(m.uctobd,4)), VAL(RIGHT(m.uctobd,2)),1), 1) - 1
			OTHERWISE
				m.ret=uctdatum.datum_dokl
		ENDCASE 
		USE IN uctdatum
	ENDIF  
ELSE 
	IF m.slovenskaverzia
		IF EMPTY(m.c_str)
			hladajcstredisk=m.gic_stredisk
		ELSE
			hladajcstredisk=m.c_str
		ENDIF 
		IF m.uctobd = LEFT(DTOS(DATE()),6)
			ret = DATE()
		ELSE 
			IF m.issql
				USE (m.sqlprefix+"rnastavdatdokl_buf") ALIAS uctdatum
			ELSE
				*** Otvorene mame dokl_buf pod aliasom doklady (fn_doklady je presmerovane na dokl_buf)
				USE vnastavdatdokl ALIAS uctdatum
			ENDIF 
			DO CASE 
				CASE !USED("uctdatum")
					m.ret = GOMONTH(DATE(VAL(LEFT(m.uctobd,4)), VAL(RIGHT(m.uctobd,2)), 1),1)
				CASE RECCOUNT() = 0 .or. LEFT(DTOS(uctdatum.datum_dokl),6) < m.uctobd .or. ISNULL(uctdatum.datum_dokl)
					m.ret = GOMONTH(DATE(VAL(LEFT(m.uctobd,4)), VAL(RIGHT(m.uctobd,2)), 1),1)
				OTHERWISE
					m.ret=uctdatum.datum_dokl
			ENDCASE 
			DO pozavirej WITH "uctdatum"
		ENDIF  
	ELSE
		m.ret=DATE()
	ENDIF 
ENDIF 

IF !m.boladb
	CLOSE DATABASES 
ENDIF 
SELECT (m.o_sel)
RETURN m.ret

	
*!*****************************************************************************
PROCEDURE testujkody
PARAMETERS zlimitov, fne, cd
PRIVATE bolieany, boladb, pokracuj
m.boladb=.f.
IF EMPTY(m.fne)
	m.fne=m.fn_eany
ENDIF 
IF EMPTY(m.cd)
	m.cd=m.cdata
ENDIF 
PRIVATE m.cdata
m.cdata=m.cd
m.pokracuj=.t.
IF m.issql
	m.bolieany=.t.
ELSE 
	IF USED("eany")
		m.bolieany=.t.
	ELSE
		m.bolieany=.f.
	ENDIF 
ENDIF 
IF ISNULL(m.nazev)
	m.nazev=""
ENDIF 
kodnazev = "0"
IF m.zlimitov
	SELECT menalim
ELSE
	SELECT menaz
ENDIF 
IF !SEEK(m.nazev)
	IF !EMPTY(m.ean_prefix)
		m.hladajean=odstranprefix(m.nazev)
	ELSE
		m.hladajean=ALLTRIM(m.nazev)
	ENDIF 
	IF !EMPTY(m.hladajean)
		SELECT 0
		DO open_database
		IF m.issql
			USE (m.sqlprefix+"rhladajean") ALIAS vhladajean IN 0
		ELSE
			IF !m.bolieany
				USE (m.fne) ALIAS eany IN 0
			ENDIF 
			USE vhladajean IN 0
		ENDIF 
		IF USED("vhladajean")
			SELECT vhladajean
			IF RECCOUNT("vhladajean")>0
				GO TOP 
				m.nazev=vhladajean.nazev
				m.nazevx=m.nazev
				m.serial_no=m.hladajean
				m.ean_koef=vhladajean.koef
				m.pov_objem=vhladajean.objem
			ENDIF
			DO pozavirej WITH "vhladajean"
		ENDIF 
	ELSE
		IF m.slovenskaverzia
			m.kodnazev=ALLTRIM(m.nazev)
			IF LEN(ALLTRIM(m.kodnazev))>6
				m.kodnazev=STRTRAN(m.kodnazev,"ľ","2")
				m.kodnazev=STRTRAN(m.kodnazev,"š","3")
				m.kodnazev=STRTRAN(m.kodnazev,"č","4")
				m.kodnazev=STRTRAN(m.kodnazev,"ť","5")
				m.kodnazev=STRTRAN(m.kodnazev,"ž","6")
				m.kodnazev=STRTRAN(m.kodnazev,"ý","7")
				m.kodnazev=STRTRAN(m.kodnazev,"á","8")
				m.kodnazev=STRTRAN(m.kodnazev,"í","9")
				m.kodnazev=STRTRAN(m.kodnazev,"é","0")
				m.kodnazev=STRTRAN(m.kodnazev,"+","1")
				m.kodnazev=STRTRAN(m.kodnazev,"ě","2")
				m.kodnazev=STRTRAN(m.kodnazev,"ř","5")
				m.jecislo=.T.
				FOR i=1 TO LEN(ALLTRIM(m.kodnazev))
					IF !ISDIGIT(SUBSTR(m.kodnazev,m.i,1))
						m.jecislo=.F.
					ENDIF
				ENDFOR
				IF !m.jecislo
					m.kodnazev=m.nazev
				ELSE
					IF !EMPTY(m.ean_prefix)
						m.hladajean=odstranprefix(m.kodnazev)
					ELSE
						m.hladajean=ALLTRIM(m.kodnazev)
					ENDIF 
					IF !EMPTY(m.hladajean)
						SELECT 0
						DO open_database
						IF m.issql
							USE (m.sqlprefix+"rhladajean") IN 0 ALIAS vhladajean
						ELSE
							IF !m.bolieany
								USE (m.fne) ALIAS eany IN 0
							ENDIF 
							USE vhladajean IN 0
						ENDIF 
						IF USED("vhladajean")
							SELECT vhladajean
							IF RECCOUNT("vhladajean")>0
								GO TOP 
								m.nazev=vhladajean.nazev
								m.nazevx=m.nazev
								m.serial_no=m.hladajean
								m.ean_koef=vhladajean.koef
								m.pov_objem=vhladajean.objem
							ELSE
								m.je_ean=m.kodnazev
							ENDIF  
							DO pozavirej WITH "vhladajean"
						ENDIF 
						IF !m.boladb
							CLOSE DATABASES	
						ENDIF 
					ENDIF 
				ENDIF
			ENDIF
		ENDIF
	ENDIF 
ENDIF
IF m.zlimitov
	SELECT menalim
ELSE
	SELECT menaz
ENDIF 
IF !m.bolieany
	DO pozavirej WITH "eany"
ENDIF 
IF VAL(m.nazev) > 0
	m.jecislo=.T.
	FOR i=1 TO LEN(ALLTRIM(m.nazev))
		IF !ISDIGIT(SUBSTR(m.nazev,m.i,1))
			m.jecislo=.F.
		ENDIF
	ENDFOR
	IF m.jecislo
		m.serial_no=ALLTRIM(m.nazev)
		IF m.zlimitov
			LOCATE FOR ALLTRIM(menalim.kod) == ALLTRIM(m.nazev)
		ELSE
			IF FSIZE("kod","menaz")>0
				LOCATE FOR ALLTRIM(menaz.kod) == ALLTRIM(m.nazev)
			ELSE
				LOCATE FOR menaz.c_karty=VAL(m.nazev)
			ENDIF 
		ENDIF 
		IF FOUND()
			IF m.zlimitov
				m.nazev = menalim.nazev
			ELSE
				m.nazev = menaz.nazev
			ENDIF 
		ELSE
		*	m.serial_no=""
			IF m.zlimitov
				LOCATE FOR menalim.c_karty = VAL(m.nazev)
			ELSE
				LOCATE FOR menaz.c_karty = VAL(m.nazev)
			ENDIF 
			IF FOUND()
				IF m.zlimitov
					m.nazev = menalim.nazev
				ELSE
					m.nazev = menaz.nazev
				ENDIF 
			ELSE
				m.je_ean=m.serial_no
				m.serial_no=""
			ENDIF
		ENDIF 
	ENDIF
ELSE
	kodnazev=ALLTRIM(m.nazev)
	IF m.slovenskaverzia
		kodnazev=STRTRAN(m.kodnazev,"ľ","2")
		kodnazev=STRTRAN(m.kodnazev,"š","3")
		kodnazev=STRTRAN(m.kodnazev,"č","4")
		kodnazev=STRTRAN(m.kodnazev,"ť","5")
		kodnazev=STRTRAN(m.kodnazev,"ž","6")
		kodnazev=STRTRAN(m.kodnazev,"ý","7")
		kodnazev=STRTRAN(m.kodnazev,"á","8")
		kodnazev=STRTRAN(m.kodnazev,"í","9")
		kodnazev=STRTRAN(m.kodnazev,"é","0")
		kodnazev=STRTRAN(m.kodnazev,"+","1")
		kodnazev=STRTRAN(m.kodnazev,"ě","2")
		kodnazev=STRTRAN(m.kodnazev,"ř","5")
		jecislo=.T.
		IF LEN(ALLTRIM(m.kodnazev))>0
			TRY 
				FOR i=1 TO LEN(ALLTRIM(m.kodnazev))
					IF !ISDIGIT(SUBSTR(m.kodnazev,m.i,1))
						jecislo=.F.
					ENDIF
				ENDFOR
			CATCH
				jecislo=.f.
			ENDTRY 
		ELSE
			jecislo=.f.
		ENDIF 
		IF !jecislo
			kodnazev=m.nazev
		ENDIF
	ENDIF

	IF VAL(m.kodnazev) > 0
		jecislo=.T.
		TRY 
			FOR i=1 TO LEN(m.kodnazev)
				IF !ISDIGIT(SUBSTR(m.kodnazev,m.i,1))
					jecislo=.F.
				ENDIF
			ENDFOR
		CATCH
			jecislo=.f.
		ENDTRY 
		IF m.jecislo
			serial_no=ALLTRIM(m.kodnazev)
			IF m.zlimitov
				LOCATE FOR menalim.kod == m.kodnazev
			ELSE
				LOCATE FOR menaz.kod == m.kodnazev
			ENDIF 
			IF FOUND()
				IF m.zlimitov
					nazev = menalim.nazev
				ELSE
					nazev = menaz.nazev
				ENDIF 
			ELSE 
				m.serial_no=""
				IF m.zlimitov
					LOCATE FOR menalim.c_karty = VAL(m.kodnazev)
				ELSE
					LOCATE FOR menaz.c_karty = VAL(m.kodnazev)
				ENDIF 
				IF FOUND()
					IF m.zlimitov
						m.nazev = menalim.nazev
					ELSE
						m.nazev = menaz.nazev
					ENDIF 
				ENDIF 
			ENDIF 
		ELSE
			m.je_ean=m.kodnazev
			m.serial_no=""
		ENDIF
	ENDIF
ENDIF


*!*****************************************************************************
PROCEDURE opr_index
PARAMETERS naopravu
PRIVATE olderror
m.olderror=ON("error")
ON ERROR USE (m.naopravu) EXCLU
USE (m.naopravu) EXCLUSIVE
DO indexuj IN indexy
USE
ON ERROR &olderror


*!*****************************************************************************
FUNCTION od_kdy_smi_opravit_doklady
PARAMETERS hladajcstredisk
IF PARAMETERS()=0
	m.hladajcstredisk=m.gic_stredisk
ENDIF 
min_c_dokl = -9999999
qwe = left(jaz("pravo_opravit_stare_doklady"), 2)
if left(m.qwe, 1) = "\" .and. m.qwe # "\<"
     SELECT MAX(c_dokl) AS max_c_dokl WHERE operace = "KK" .AND. c_stredisk = m.hladajcstredisk ;
            FROM doklady INTO CURSOR max_c_dokl
     IF RECCOUNT() > 0
            min_c_dokl = max_c_dokl.max_c_dokl
     ENDIF
     USE IN max_c_dokl

endif
RETURN min_c_dokl

*!*****************************************************************************
FUNCTION dajmincdoklnaopravu
PARAMETERS hladajcstredisk
IF PARAMETERS()=0
	hladajcstredisk=m.gic_stredisk
ENDIF 
PRIVATE min_c_dokl
IF m.glis_admin
	min_c_dokl = -999999
ELSE
	qwe = LEFT(jaz("pravo_opravit_stare_doklady"), 2)
	IF LEFT(m.qwe, 1) = "\" .and. m.qwe # "\<"
		IF m.issql
			USE (m.sqlprefix+"ropravymaxcdokl"+m.sqlsuffix) IN 0 ALIAS max_c_dokl
		ELSE
			USE vopravymaxcdokl IN 0 ALIAS max_c_dokl
		ENDIF 
		IF RECCOUNT() = 0
			min_c_dokl = -999999
		ELSE
			min_c_dokl = max_c_dokl.c_dokl
		ENDIF
	ELSE
		min_c_dokl = -999999
	ENDIF 
ENDIF
DO pozavirej WITH  "max_c_dokl"
RETURN min_c_dokl

*!*****************************************************************************
FUNCTION dajmincdoklnaopravupi
PARAMETERS hladajcstredisk
IF PARAMETERS()=0
	m.hladajcstredisk=m.gic_stredisk
ENDIF 
PRIVATE min_c_dokl
IF m.glis_admin
	min_c_dokl = -999999
ELSE
	IF m.issql
		USE (m.sqlprefix+"ropravymaxcdoklpi"+m.sqlsuffix) IN 0 ALIAS max_c_dokl
	ELSE
		USE vopravymaxcdoklpi IN 0 ALIAS max_c_dokl
	ENDIF 
	IF RECCOUNT() = 0
		min_c_dokl = -999999
	ELSE
		min_c_dokl = max_c_dokl.c_dokl
	ENDIF
ENDIF
DO pozavirej WITH  "max_c_dokl"
RETURN min_c_dokl

*!*****************************************************************************
PROCEDURE vymaz_buffer
PARAMETERS menobuffera, odlozenie
LOCAL zmenenyriadok
IF !m.odlozenie
	IF m.issql
		IF !USED("teraz")
			rv("teraz","teraz")
		ENDIF 
		m.vysledok=sql_exec("SELECT food600.mazbuffer("+ALLTRIM(STR(m.cdata,2,0))+",'"+m.menobuffera+"')","teraz")
		DO pozavirej WITH "teraz"
		IF USED("buffer")
			SELECT buffer
			IF CURSORGETPROP("Buffering","buffer")>1
				TRY 
					m.zmenenyriadok=GETNEXTMODIFIED(0,"buffer")
				CATCH
					m.zmenenyriadok=0
				ENDTRY 
				DO WHILE .t.
					IF m.zmenenyriadok<>0
						GOTO m.zmenenyriadok
						=TABLEREVERT(.f.,"buffer")
						m.zmenenyriadok=GETNEXTMODIFIED(m.zmenenyriadok,"buffer")
					ELSE
						EXIT 
					ENDIF 
				ENDDO 
				=TABLEREVERT(.t.,"buffer")
				TRY 
					=REQUERY("buffer")
				CATCH 
				ENDTRY 
				DELETE FOR menobuf=m.menobuffera
				=tux(.t.,.t.,"buffer")
			ELSE
				DELETE FOR menobuf=m.menobuffera
			ENDIF 
		ENDIF 
		IF USED("buf_dokl")
			SELECT  buf_dokl
			IF CURSORGETPROP("Buffering","buf_dokl")>0
				TRY 
					m.zmenenyriadok=GETNEXTMODIFIED(0,"buf_dokl")
				CATCH
					m.zmenenyriadok=0
				ENDTRY 
				DO WHILE .t.
					IF m.zmenenyriadok<>0
						GOTO m.zmenenyriadok
						=TABLEREVERT(.f.,"buf_dokl")
						m.zmenenyriadok=GETNEXTMODIFIED(m.zmenenyriadok,"buf_dokl")
					ELSE
						EXIT 
					ENDIF 
				ENDDO 
				=TABLEREVERT(.t.,"buf_dokl")
				TRY 
					=REQUERY("buf_dokl")
				CATCH 
				ENDTRY 
				DELETE FOR menobuf=m.menobuffera
				=tux(.t.,.t.,"buf_dokl")
			ELSE
				DELETE FOR menobuf=m.menobuffera
			ENDIF 
		ENDIF 
	ELSE
		IF USED("buffer")
			SELECT buffer
			SET FILTER TO 
			IF CURSORGETPROP("Buffering","buffer")>0
				TRY 
					m.zmenenyriadok=GETNEXTMODIFIED(0,"buffer")
				CATCH
					m.zmenenyriadok=0
				ENDTRY 
				DO WHILE .t.
					IF m.zmenenyriadok<>0
						GOTO m.zmenenyriadok
						=TABLEREVERT(.f.,"buffer")
						m.zmenenyriadok=GETNEXTMODIFIED(m.zmenenyriadok,"buffer")
					ELSE
						EXIT 
					ENDIF 
				ENDDO 
				=TABLEREVERT(.t.,"buffer")
				TRY 
					=REQUERY("buffer")
				CATCH 
				ENDTRY 
				DELETE FOR menobuf=m.menobuffera
				=tux(.t.,.t.,"buffer")
			ELSE
				DELETE FOR menobuf=m.menobuffera
			ENDIF 
		ENDIF 
		IF USED("buf_dokl")
			SELECT  buf_dokl
			IF CURSORGETPROP("Buffering","buf_dokl")>0
				TRY 
					m.zmenenyriadok=GETNEXTMODIFIED(0,"buf_dokl")
				CATCH
					m.zmenenyriadok=0
				ENDTRY 
				DO WHILE .t.
					IF m.zmenenyriadok<>0
						GOTO m.zmenenyriadok
						=TABLEREVERT(.f.,"buf_dokl")
						m.zmenenyriadok=GETNEXTMODIFIED(m.zmenenyriadok,"buf_dokl")
					ELSE
						EXIT 
					ENDIF 
				ENDDO 
				=TABLEREVERT(.t.,"buf_dokl")
				TRY 
					=REQUERY("buf_dokl")
				CATCH
				ENDTRY 
				DELETE FOR menobuf=m.menobuffera
				=tux(.t.,.t.,"buf_dokl")
			ELSE
				DELETE FOR menobuf=m.menobuffera
			ENDIF 
		ENDIF 
	ENDIF 
ENDIF 

*!*****************************************************************************
FUNCTION dajmindatum
PARAMETERS hladajcstredisk
PRIVATE min_datum, old_sel
m.old_sel=SELECT()
IF m.slovenskaverzia
	IF m.issql
		USE (m.sqlprefix+"rsksezmaxuzkk") ALIAS maxuz IN 0
	ELSE
		USE (m.fn_sk_sez) ALIAS sezvk AGAIN IN 0
		SELECT MAX(uzaverka) AS uzaverka FROM sezvk WHERE RIGHT(ALLTRIM(uzaverka),3)='***' INTO CURSOR maxuz
		USE IN sezvk
	ENDIF 
	IF RECCOUNT() = 0
		IF m.issql
			USE (m.sqlprefix+"rdokladymaxkk") IN 0 ALIAS max_c_dokl
		ELSE 
			SELECT MAX(c_dokl) AS c_dokl, datum_dokl AS min_datum WHERE operace = "KK" .AND. c_stredisk = m.gic_stredisk ;
				FROM (m.fn_doklady) INTO CURSOR max_c_dokl
		ENDIF 
		IF RECCOUNT() = 0
			m.min_datum = {^2004-01-01}
		ELSE
			m.min_datum = max_c_dokl.min_datum
		ENDIF
		USE IN max_c_dokl
	ELSE
		m.min_datum=CTOD(SUBSTR(maxuz.uzaverka,7,2)+"."+SUBSTR(maxuz.uzaverka,5,2)+"."+LEFT(maxuz.uzaverka,4))
	ENDIF
	USE IN maxuz
	IF LEFT(DTOS(m.min_datum),6)>m.uctobd
		=MESSAGEBOX(jaz("form_stare_uct_obdobie")+CHR(10)+jaz("form_neda_sa_pokracovat"),16,"")
		RETURN .F.
	ENDIF
ELSE
	IF m.glis_admin
		m.min_datum = {^2004-01-01}
	ELSE
		IF m.issql
			USE (m.sqlprefix+"rdokladymaxkk") IN 0 ALIAS max_c_dokl
		ELSE 
			SELECT MAX(c_dokl) AS c_dokl, datum_dokl AS min_datum WHERE operace = "KK" .AND. c_stredisk = m.gic_stredisk ;
				FROM (m.fn_doklady) INTO CURSOR max_c_dokl
		ENDIF 
		IF RECCOUNT() = 0
			m.min_datum = {^2004-01-01}
		ELSE
			m.min_datum = max_c_dokl.min_datum
		ENDIF
	ENDIF
ENDIF
SELECT (m.old_sel)
RETURN m.min_datum

*!*****************************************************************************
FUNCTION dajpoleuzaverka
PARAMETERS zadanydatum, zdavky
PRIVATE m.hladajuzaverka, old_sel, m.hladajidkas
m.old_sel=SELECT()
SELECT 0
IF m.slovenskaverzia OR m.issql
	IF m.issql
		USE (m.sqlprefix+"rsksezmaxuzkk") ALIAS maxuz 
	ELSE 
		SELECT MAX(uzaverka) AS uzaverka FROM sk_sez WHERE RIGHT(ALLTRIM(uzaverka),3)="***" INTO CURSOR maxuz
	ENDIF 
	IF RECCOUNT()=0
		m.zamk_uz="20050101 23:59:59"
	ELSE
		m.zamk_uz=maxuz.uzaverka
	ENDIF
	USE IN maxuz
	m.hladajuzaverka = DTOS(m.zadanydatum) + " " + STRTR(TIME(), ":", ":")
	DO WHILE .T.
		IF m.hladajuzaverka<=m.zamk_uz
			IF VAL(RIGHT(m.hladajuzaverka,2))=59
				IF VAL(SUBSTR(m.hladajuzaverka,13,2))=59
					IF VAL(SUBSTR(m.hladajuzaverka,10,2))=23
						m.hladajuzaverka = LEFT(m.hladajuzaverka, 6) + STRTRAN(STR(VAL(SUBSTR(m.hladajuzaverka,7,2))+1,2,0),' ','0')+' 00:00:00'
					ELSE
						m.hladajuzaverka = LEFT(m.hladajuzaverka, 9) + STRTRAN(STR(VAL(SUBSTR(m.hladajuzaverka,10,2))+1,2,0),' ','0')+':00:00'
					ENDIF
				ELSE
					m.hladajuzaverka = LEFT(m.hladajuzaverka, 12) + STRTRAN(STR(VAL(SUBSTR(m.hladajuzaverka,13,2))+1,2,0),' ','0')+':00'
				ENDIF
			ELSE
				m.hladajuzaverka = LEFT(m.hladajuzaverka, 15) + STRTRAN(STR(VAL(RIGHT(m.hladajuzaverka, 2)) + 1,2),' ','0')
			ENDIF
		ELSE
			EXIT
		ENDIF
	ENDDO
	IF m.zdavky
		m.hladajuzaverka=STRTRAN(m.hladajuzaverka,":","*")
	ENDIF 
	SELECT 0
	IF m.issql
		USE (m.sqlprefix+"rsksezhladajvolnyuz") ALIAS volnyuz
	ELSE
		USE vsksezhladajvolnyuz ALIAS volnyuz
	ENDIF 
	DO WHILE RECCOUNT("volnyuz")>0
		m.hladajuzaverka = LEFT(uzaverka, 12) + TRAN(VAL(SUBSTR(uzaverka,13, 2))+1, "@L 99")+RIGHT(uzaverka,3)
		=REQUERY("volnyuz")
	ENDDO
ELSE
    m.hladajuzaverka = RIGHT(DTOS(m.zadanydatum), 6) + IIF( m.zdavky, " 12*00", " 12:00")
	IF m.issql
		USE (m.sqlprefix+"rsksezhladajvolnyuzcs") ALIAS volnyuz
	ELSE
		USE vsksezhladajvolnyuzcs ALIAS volnyuz
	ENDIF 
	DO WHILE RECCOUNT()>0
		IF RIGHT(m.hladajuzaverka,2)="99"
			m.hladajuzaverka=LEFT(m.hladajuzaverka, 7) + TRAN(VAL(SUBSTR(m.hladajuzaverka, 8,2))+1, "@L 99")+IIF(m.zdavky,"*00",":00")
		ELSE 
			m.hladajuzaverka = LEFT(m.hladajuzaverka, 10) + TRAN(VAL(RIGHT(m.hladajuzaverka, 2))+1, "@L 99")
		ENDIF 
		=REQUERY("volnyuz")
	ENDDO
ENDIF
USE IN volnyuz
SELECT (m.old_sel)
RETURN m.hladajuzaverka


*!*****************************************************************************
FUNCTION hladajuzaverku
PARAMETERS m.hladajuzaverka
PRIVATE m.hladajuzaverka, old_sel
m.old_sel=SELECT()
SELECT 0
IF m.slovenskaverzia OR m.issql
	IF m.issql
		USE (m.sqlprefix+"rsksezmaxuzkk") ALIAS maxuz 
	ELSE 
		SELECT MAX(uzaverka) AS uzaverka FROM sk_sez WHERE RIGHT(ALLTRIM(uzaverka),3)="***" INTO CURSOR maxuz
	ENDIF 
	IF RECCOUNT()=0
		m.zamk_uz="20050101 23:59:59"
	ELSE
		m.zamk_uz=maxuz.uzaverka
	ENDIF
	USE IN maxuz
	DO WHILE .T.
		IF m.hladajuzaverka<=m.zamk_uz
			IF VAL(RIGHT(m.hladajuzaverka,2))=59
				IF VAL(SUBSTR(m.hladajuzaverka,13,2))=59
					IF VAL(SUBSTR(m.hladajuzaverka,10,2))=23
						m.hladajuzaverka = LEFT(m.hladajuzaverka, 6) + STRTRAN(STR(VAL(SUBSTR(m.hladajuzaverka,7,2))+1,2,0),' ','0')+' 00:00:00'
					ELSE
						m.hladajuzaverka = LEFT(m.hladajuzaverka, 9) + STRTRAN(STR(VAL(SUBSTR(m.hladajuzaverka,10,2))+1,2,0),' ','0')+':00:00'
					ENDIF
				ELSE
					m.hladajuzaverka = LEFT(m.hladajuzaverka, 12) + STRTRAN(STR(VAL(SUBSTR(m.hladajuzaverka,13,2))+1,2,0),' ','0')+':00'
				ENDIF
			ELSE
				m.hladajuzaverka = LEFT(m.hladajuzaverka, 15) + STRTRAN(STR(VAL(RIGHT(m.hladajuzaverka, 2)) + 1,2),' ','0')
			ENDIF
		ELSE
			EXIT
		ENDIF
	ENDDO
	SELECT 0
	IF m.issql
		USE (m.sqlprefix+"rsksezhladajvolnyuz") ALIAS volnyuz
	ELSE
		USE vsksezhladajvolnyuz ALIAS volnyuz
	ENDIF 
	DO WHILE RECCOUNT("volnyuz")>0
		m.hladajuzaverka = LEFT(uzaverka, 12) + TRAN(VAL(SUBSTR(uzaverka,13, 2))+1, "@L 99")+RIGHT(uzaverka,3)
		=REQUERY("volnyuz")
	ENDDO
ELSE
	IF LEFT(m.hladajuzaverka, 1) = "0"
		m.hladajuzaverka = "O" + SUBST(m.hladajuzaverka, 2)
	ENDIF
	IF m.issql
		USE (m.sqlprefix+"rsksezhladajvolnyuzcs") ALIAS volnyuz
	ELSE
		USE vsksezhladajvolnyuzcs ALIAS volnyuz
	ENDIF 
	DO WHILE RECCOUNT()>0
		m.hladajuzaverka = LEFT(uzaverka, 10) + TRAN(VAL(RIGHT(uzaverka, 2))+1, "@L 99")
		=REQUERY("volnyuz")
	ENDDO
ENDIF
USE IN volnyuz
SELECT (m.old_sel)
RETURN m.hladajuzaverka


*!*****************************************************************************
FUNCTION act(COMMAND)
port = "2222"
PRIVATE m.ret
TRY 
	http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
	http.OPEN("POST", "http://localhost:" + port + "/", .F.)
	http.SetTimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type","application/x-alto-act-localcom;charset=utf-8;")
	http.SEND(COMMAND)
	m.ret= http.responsetext
CATCH 
	m.ret= "XX"
ENDTRY 
RETURN m.ret


*!*****************************************************************************
FUNCTION kontroladatumdokl
PARAMETERS datumnakontrolu
IF m.slovenskaverzia
	m.typ_konca="UZAVIERKA"
ELSE
	m.typ_konca=""
ENDIF

IF !("buf"$m.fn_obraty)
	DO CASE 
		CASE LOWER(m.typ_konca)=="uzavierka" OR LOWER(m.typ_konca)=="koniec"
			IF LEFT(DTOS(m.datumnakontrolu),6)<>m.uctobd
				=MESSAGEBOX(jaz("form_neaktualny_datum"),16,"FoodMan")
				RETURN .f.
			ELSE
				RETURN .t.
			ENDIF
		OTHERWISE 
			RETURN .t.
	ENDCASE 
ELSE
	DO CASE 
		CASE LOWER(m.typ_konca)=="uzavierka" OR LOWER(m.typ_konca)=="koniec"
			*** Do zasob sa moze zadavat doklad bud z aktualneho uctovneo obdobia, alebo buduceho (aktualne, ked napr. v polovici mesiaca robia inventuru a uz chcu nahravat prijemky po nej)
			IF LEFT(DTOS(GOMONTH(m.datumnakontrolu,-1)),6)<>m.uctobd AND LEFT(DTOS(m.datumnakontrolu),6)<>m.uctobd
				IF MESSAGEBOX(jaz("form_nebuduci_datum")+CHR(10)+jaz("form_ponechat_datum"),4+32+256,"FoodMan")=6
					IF MESSAGEBOX(jaz("form_chete_doklad_s_datumom")+" "+DTOC(m.datumnakontrolu)+"?",4+32+256,"FoodMan")=6
						RETURN .t.
					ELSE
						RETURN .f.
					ENDIF 
				ELSE
					RETURN .f.
				ENDIF 
			ELSE
				RETURN .t.
			ENDIF
		OTHERWISE 
			RETURN .t.
	ENDCASE 
ENDIF 


*!*****************************************************************************
FUNCTION i_drupla
LPARAMETER druh_pl

IF m.slovenskaverzia
    PRIVATE old_se
    old_se=SELECT()
    IF !USED("druhy_pl")
        SELECT 0
        IF m.issql 
            rv("druhy_pl","druhy_pl")
        ELSE 
            USE (m.gchom_path+"druhy_pl")
        ENDIF 
    ELSE
        SELECT druhy_pl
    ENDIF 
    LOCATE FOR ALLTRIM(nazev)==ALLTRIM(m.druh_pl)
    DO CASE
        CASE FOUND()
            platba=ALLTRIM(druhy_pl.j0)
        CASE ALLTRIM(druh_pl) == 'K_VAV'
            platba = "Kredit Vavcher"
        CASE ALLTRIM(druh_pl) == 'Hotove 2'
            platba = "Hotove Sk"
        CASE ALLTRIM(druh_pl) =='Hotove'
            platba = "Hotovost"
        CASE ALLTRIM(druh_pl) =='K_AMEX'
            platba = "Kredit AMEX"
        CASE ALLTRIM(druh_pl) == 'K_VISA'
            platba = "Kredit VISA"
        CASE ALLTRIM(druh_pl) == 'K_ECHQ'
            platba  = "Kredit ECHQ"
        CASE ALLTRIM(druh_pl) == 'K_DC'
            platba = "Kredit DC"
        CASE ALLTRIM(druh_pl) == 'K_JCB'
            platba = "Kredit JCB"
        CASE ALLTRIM(druh_pl) == 'K_EC'
            platba = "Kredit EC"
        CASE ALLTRIM(druh_pl) == 'N_BEZHO1'
            platba = "Penzia"
        CASE ALLTRIM(druh_pl) == 'N_BEZHO2'
            platba = "Repre fond"
        CASE ALLTRIM(druh_pl) == 'N_BEZHO3'
            platba = "Stravovacie šeky"
        CASE ALLTRIM(druh_pl) == 'N_HOTEL2'
            platba = "Hotelový účet 2.úroveň"
        CASE ALLTRIM(druh_pl) == 'N_UVER'
            platba = "Úver"
        CASE ALLTRIM(druh_pl) == 'N_HOTEL'
            platba = "Hotelový účet"
        CASE ALLTRIM(druh_pl) == 'N_SNIDAN'
            platba = "Raňajky"
        CASE ALLTRIM(druh_pl) == 'S_SMEN'
            platba = "Valutová platba"
        CASE ALLTRIM(druh_pl) == 'N_HOREC'
            platba = "Recepcia HOREC"
        CASE ALLTRIM(druh_pl) == 'N_HOTELV'
            platba = "Recepcia HOREC VIP"
        CASE ALLTRIM(druh_pl) == 'N_HORECV'
            platba = "Recepcia HOREC VIP"
        CASE ALLTRIM(druh_pl) == 'D_DAVKA'
            platba = "Dávka         "
        CASE ALLTRIM(druh_pl) == 'Preplat'
            platba = "Preplatok šeky"
        CASE ALLTRIM(druh_pl) == '50'
            platba  = "Sporožíro"
        CASE ALLTRIM(druh_pl) == 'Spolu'
            platba = "Spolu         "
        CASE !m.issql AND FILE(m.gchom_path+"def_spot.dbf") .AND. f_acc('(m.gchom_path+"def_spot")')
            LOCATE FOR def_spot.nazev=druh_pl
            IF FOUND()
                platba=ALLTRIM((def_spot.priznak))
            ELSE
                platba  = "Neidentifikovany "
            ENDIF
            USE IN def_spot
            SELECT(m.old_se)
        OTHERWISE
            platba  = IIF(ISNULL(m.druh_pl),"Neznáma platba",m.druh_pl)
    ENDCASE
    SELECT (old_se)
    RETURN platba
ELSE
    RETURN jaz2(druh_pl)
*    RETURN m.druh_pl
ENDIF 


*!*****************************************************************************
FUNCTION i_mencis
PARAMETER m.kas_id, m.cis_id
PRIVATE osel
IF LOWER(m.cis_id) = "adm"
	cis_jmeno = UPPER("administrator")
	RETURN cis_jmeno
ENDIF
IF LOWER(m.cis_id) = "sup"
	cis_jmeno = UPPER("supervisor alto")
	RETURN cis_jmeno
ENDIF

osel = SELECT()
SELECT 0
cis_jmeno = UPPER("neidentifikovany")
IF !USED("users")
	IF m.issql
		rv("users","users")
	ELSE 
		IF FILE(m.gchom_path + "users.dbf")
			USE m.gchom_path + "users"
		ENDIF 
	ENDIF 
ENDIF 
IF USED("users")
	SELECT users
	LOCATE FOR USERS.user_id = m.cis_id
	IF FOUND()
		cis_jmeno = USERS.username
	ENDIF 
ENDIF 
SELECT (osel)
RETURN cis_jmeno


*!*****************************************************************************
FUNCTION numtobinchar
PARAMETERS pnNumber, pnNoOfBytes
  LOCAL Num, i, ret
  ret = ""
  Num = pnNumber
  IF Num<0
    Num = Num+0xFFFFFFFF+1
  ENDIF
  FOR i = 1 TO pnNoOfBytes
    ret = STR(MOD(Num,2),1,0)+ret
    Num = INT(Num/2)
  ENDFOR
  RETURN ret


*!*****************************************************************************
FUNCTION numtodecchar
PARAMETERS pnNumber, pnNoOfBytes
IF EMPTY(m.pnNoOfBytes)
    pnNoOfBytes = 28
ENDIF 
LOCAL Num, i, ret, polealergenu, string_alergenu

IF m.alergeny_detail
    string_alergenu = "_1 2 3 4 5 6 7 8 9 10111213141A1B1C1D1E1F8A8B8C8D8E8F8G8H"
ELSE    &&&&            1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 
    string_alergenu = "_1 2 3 4 5 6 7 8 9 1011121314ZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
ENDIF 

DIMENSION polealergenu( pnNoOfBytes )
Num = pnNumber
IF m.Num < 0
    Num = m.Num + 0xFFFFFFFF+1
ENDIF
FOR i = 1 TO pnNoOfBytes
    IF MOD(m.Num,2) = 1
        polealergenu( m.i ) = SUBSTR( m.string_alergenu, m.i*2, 2)
    ELSE
        polealergenu( m.i ) = "ZZ"
    ENDIF 
    Num = INT(m.Num / 2)
ENDFOR

ASORT(polealergenu, 1)

ret = ""
FOR i = 1 TO pnNoOfBytes
    IF polealergenu( m.i ) # "ZZ"
        ret = m.ret + "," + ALLTRIM(polealergenu( m.i ))
    ENDIF 
ENDFOR

IF LEFT(m.ret,1) = ","
    ret = SUBSTR(m.ret,2)
ENDIF 

RETURN ret

*!*****************************************************************************
FUNCTION numtodecchar___zrusena_puvodni
PARAMETERS pnNumber, pnNoOfBytes
LOCAL Num, i, ret
ret = ""
Num = pnNumber
IF Num<0
	Num = Num+0xFFFFFFFF+1
ENDIF
FOR i = 1 TO pnNoOfBytes
	ret = ret+IIF(MOD(Num,2)=1,","+ALLTRIM(STR(m.i,2,0)),"")
	Num = INT(Num/2)
ENDFOR
IF LEFT(m.ret,1)=","
	m.ret=SUBSTR(m.ret,2)
ENDIF 
IF m.alergeny_detail
	m.ret=STRTRAN(m.ret,"15","1A")
	m.ret=STRTRAN(m.ret,"16","1B")
	m.ret=STRTRAN(m.ret,"17","1C")
	m.ret=STRTRAN(m.ret,"18","1D")
	m.ret=STRTRAN(m.ret,"19","1E")
	m.ret=STRTRAN(m.ret,"20","1F")
	m.ret=STRTRAN(m.ret,"21","8A")
	m.ret=STRTRAN(m.ret,"22","8B")
	m.ret=STRTRAN(m.ret,"23","8C")
	m.ret=STRTRAN(m.ret,"24","8D")
	m.ret=STRTRAN(m.ret,"25","8E")
	m.ret=STRTRAN(m.ret,"26","8F")
	m.ret=STRTRAN(m.ret,"27","8G")
	m.ret=STRTRAN(m.ret,"28","8H")
ELSE
	m.ret=STRTRAN(m.ret,"15","")
	m.ret=STRTRAN(m.ret,"16","")
	m.ret=STRTRAN(m.ret,"17","")
	m.ret=STRTRAN(m.ret,"18","")
	m.ret=STRTRAN(m.ret,"19","")
	m.ret=STRTRAN(m.ret,"20","")
	m.ret=STRTRAN(m.ret,"21","")
	m.ret=STRTRAN(m.ret,"22","")
	m.ret=STRTRAN(m.ret,"23","")
	m.ret=STRTRAN(m.ret,"24","")
	m.ret=STRTRAN(m.ret,"25","")
	m.ret=STRTRAN(m.ret,"26","")
	m.ret=STRTRAN(m.ret,"27","")
	m.ret=STRTRAN(m.ret,"28","")
	*Aby sa mi odpratali prazdne ciarky po vyhodeni detailnych alergenov
	FOR m.i=1 TO 7
		m.ret=STRTRAN(m.ret,",,",",")
	ENDFOR 
	IF RIGHT(m.ret,1)=","
		m.ret=LEFT(m.ret,LEN(m.ret)-1)
	ENDIF 
ENDIF 
RETURN ret

*!*****************************************************************************
FUNCTION binchartonum
PARAMETERS retaz
LOCAL Num, i, dlzka
Num = 0
m.dlzka=LEN(retaz)
FOR i = 1 TO m.dlzka
    DO CASE 
    	CASE SUBSTR(m.retaz,m.dlzka-m.i+1,1)="0"
    	CASE SUBSTR(m.retaz,m.dlzka-m.i+1,1)="1"
    		m.num=m.num+2^(m.i-1)
    	OTHERWISE 
    		m.num=0
    		EXIT
    ENDCASE 
ENDFOR 
RETURN m.num


*!*****************************************************************************
PROCEDURE editestujkody
PARAMETERS ean, doplndomenaz
PRIVATE bolieany, boladb, m.hladajean, bolikarty, m.hladajckarty, m.dan, m.boliskupiny, m.bolirskupiny, m.pov_objem
m.boladb=.f.
m.pov_objem=1
IF m.issql
	m.bolieany=.t.
ELSE 
	IF USED("eany")
		m.bolieany=.t.
	ELSE
		m.bolieany=.f.
	ENDIF 
ENDIF 
IF !EMPTY(m.ean)
	m.hladajean=ALLTRIM(m.ean)
ELSE 
	m.hladajean=ALLTRIM(m.ico)+"::"+ALLTRIM(m.kod)
ENDIF 
IF !EMPTY(m.hladajean)
	SELECT 0
	DO open_database
	IF m.issql
		USE (m.sqlprefix+"rhladajean") ALIAS vhladajean IN 0
	ELSE
		IF !m.bolieany
			USE (m.fn_eany) ALIAS eany IN 0
		ENDIF 
		USE vhladajean IN 0
	ENDIF 
	SELECT vhladajean
	IF RECCOUNT("vhladajean")>0
		m.nazev=vhladajean.nazev
		m.c_karty=vhladajean.c_karty
		m.ean_koef=vhladajean.koef
		m.pov_objem=vhladajean.objem
	ELSE
		m.c_karty=0
		m.nazev=""
		m.ean_koef=0
		m.pov_objem=1
	ENDIF
	DO pozavirej WITH "vhladajean"
	IF !m.boladb
		CLOSE DATABASES	
	ENDIF 
ELSE
	m.c_karty=0
	m.nazev=""
	m.ean_koef=0
	m.pov_objem=1
ENDIF
IF !EMPTY(m.c_karty) AND !EMPTY(m.doplndomenaz)
	SELECT menaz
	LOCATE FOR c_karty=m.c_karty
	IF !FOUND()
		m.hladajckarty=m.c_karty
		IF m.issql
			m.bolikarty=.t.
			USE (m.sqlprefix+"r1karta") ALIAS rxkarta IN 0
			m.boliskupiny=.t.
			IF USED("rskupiny")
				m.bolirskupiny=.t.
			ELSE
				USE (m.sqlprefix+"rskupiny") ALIAS rskupiny IN 0
				m.bolirskupiny=.f.
			ENDIF 
		ELSE 
			IF USED("karty")
				m.bolikarty=.t.
			ELSE
				m.bolikarty=.f.
				USE (m.fn_karty) ALIAS karty IN 0
			ENDIF 
			USE v1karta IN 0 ALIAS rxkarta
			IF USED("skupiny")
				m.boliskupiny=.t.
			ELSE
				m.boliskupiny=.f.
				USE (m.fn_skupiny) ALIAS skupiny IN 0
			ENDIF 
			IF USED("rskupiny")
				m.bolirskupiny=.t.
			ELSE
				m.bolirskupiny=.f.
				USE vskupiny) ALIAS rskupiny IN 0
			ENDIF 
		ENDIF 
		DO CASE 
			CASE rxkarta.nazev1=m.smazana_karta
				m.c_karty=0
				m.nazev=""
				m.ean_koef=0
				IF m.issql
					USE (m.sqlprefix+"rhladajean") ALIAS vhladajean IN 0
				ELSE
					IF !m.bolieany
						USE (m.fn_eany) ALIAS eany IN 0
					ENDIF 
					USE vhladajean IN 0
				ENDIF 
				=CURSORSETPROP("Buffering",5,"vhladajean")
				SELECT vhladajean
				DELETE ALL
				=tux(.t.,.t.,"vhladajean")
				DO pozavirej WITH "vhladajean"
			CASE "skl"$LOWER(m.gcpgm_param) AND rxkarta.c_druhs<>0
				SELECT rskupiny
				LOCATE FOR c_druh=rxkarta.c_druhs
				m.dan=rskupiny.dan01
				IF EMPTY(m.dan) OR ISNULL(m.dan)
					m.dan=1
				ENDIF
				INSERT INTO menaz (nazev, c_karty, druh, pov_objem, dan, kod, kartymena, lieh, numkod) ;
					VALUE (rxkarta.nazev, m.c_karty, rxkarta.druh_skl, m.pov_objem, m.dan, rxkarta.kod, rxkarta.c_mena, rxkarta.lieh, rxkarta.kod)
			CASE !("skl"$LOWER(m.gcpgm_param)) AND rxkarta.c_druh<>0
				SELECT rskupiny
				LOCATE FOR c_druh=rxkarta.c_druh
				m.dan=rskupiny.dan01
				IF EMPTY(m.dan) OR ISNULL(m.dan)
					m.dan=1
				ENDIF
				INSERT INTO menaz (nazev, c_karty, druh, pov_objem, dan, kod, kartymena, lieh, numkod) ;
					VALUE (rxkarta.nazev, m.c_karty, rxkarta.druh, m.pov_objem, m.dan, rxkarta.kod, rxkarta.c_mena, rxkarta.lieh, rxkarta.kod)
		ENDCASE
		IF !m.bolikarty
			DO pozavirej WITH "karty"
		ENDIF 
		IF !m.boliskupiny
			DO pozavirej WITH "skupiny"
		ENDIF 
		IF !m.bolirskupiny
			DO pozavirej WITH "rskupiny"
		ENDIF 
		DO pozavirej WITH "rxkarta"  
	ENDIF 
ENDIF 
IF !m.bolieany
	DO pozavirej WITH "eany"
ENDIF 


*!*****************************************************************************
FUNCTION citajrastre
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
DO FORM snake NAME lsnakex LINKED WITH 'Čítam rastre'
lsnakex.DRAW()
LOCAL m.ret, m.lcClasslibWas, vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/rasters/" , .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/rasters/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/rasters/" , .F.)
	ENDIF 
	Http.Option(4) = 13056 
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/rasters/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/rasters/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/rasters/" , .F.)
	ENDIF 
	Http.Option(4) = 13056 
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
TRY 
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	lsnakex.DRAW()
	m.vysledok=lojson.parse(http.responsetext)
	lsnakex.DRAW()
	EXTERNAL ARRAY raster
	DIMENSION raster(100,18)
	IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
		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(10),pmc L)
		INDEX on id TAG id
		FOR m.i=1 TO ALEN(vysledok.data)
			TRY 
				INSERT INTO rastre (crc, name, variant, ac_group, id, type, vat, code, pmc) VALUES(vysledok.data[m.i].cash_register_code, vysledok.data[m.i].name, vysledok.data[m.i].variant, vysledok.data[m.i].accounting_group, vysledok.data[m.i].id, vysledok.data[m.i].type, VAL(vysledok.data[m.i].vat), vysledok.data[m.i].code, vysledok.data[m.i].promote_to_cash)
				lsnakex.DRAW()
			CATCH 
			ENDTRY 
		ENDFOR 
	ELSE
		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(10),pmc L)
		INDEX on id TAG id
		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)
				lsnakex.DRAW()
			CATCH 
			ENDTRY 
		ENDFOR 
	ENDIF 
	SELECT rastre
	RELEASE raster
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret

*!*****************************************************************************
FUNCTION citajstravkody
PARAMETERS hor_adr, hor_port, api_usr, api_pwd
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_str_kody')
lsnakex.DRAW()
LOCAL m.ret, m.lcClasslibWas, vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF EMPTY(m.hor_http_port)
	IF EMPTY(m.hor_port)
		http.OPEN("GET","https://"+m.hor_adr+"/api/fandb_codes/" , .F.)
	ELSE
		http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/fandb_codes/" , .F.)
	ENDIF 
ELSE
	http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/fandb_codes/" , .F.)
ENDIF 
Http.Option(4) = 13056 
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	lsnakex.DRAW()
	m.vysledok=lojson.parse(http.responsetext)
	lsnakex.DRAW()
	EXTERNAL ARRAY kody
	DIMENSION kody(100,3)
	RELEASE lojson
	CREATE CURSOR stravkody (id I, kod c(30), meno c(30))
	INDEX on id TAG id
	FOR m.i=1 TO ALEN(vysledok.data)
		TRY 
			INSERT INTO stravkody (id, kod, meno) VALUES(vysledok.data[m.i].id, vysledok.data[m.i].code, vysledok.data[m.i].name)
			lsnakex.DRAW()
		CATCH 
		ENDTRY 
	ENDFOR 
	SELECT stravkody
	RELEASE kody
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret

*!*****************************************************************************
FUNCTION citajnabory
PARAMETERS hor_adr, hor_port, api_usr, api_pwd
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_hores_nabory')
lsnakex.DRAW()
LOCAL m.ret, m.lcClasslibWas, vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF EMPTY(m.hor_http_port)	
	IF EMPTY(m.hor_port)
		http.OPEN("GET","https://"+m.hor_adr+"/api/reservation_sources/" , .F.)
	ELSE
		http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/reservation_sources/" , .F.)
	ENDIF 
ELSE
	http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/reservation_sources/" , .F.)
ENDIF 
Http.Option(4) = 13056 
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	lsnakex.DRAW()
	m.vysledok=lojson.parse(http.responsetext)
	lsnakex.DRAW()
	EXTERNAL ARRAY kody
	DIMENSION kody(100,3)
	RELEASE lojson
	CREATE CURSOR nabory (id I, kod c(30), meno c(30))
	INDEX on id TAG id
	FOR m.i=1 TO ALEN(vysledok.data)
		TRY 
			INSERT INTO nabory (id, kod, meno) VALUES(vysledok.data[m.i].id, vysledok.data[m.i].code, vysledok.data[m.i].name)
			lsnakex.DRAW()
		CATCH 
		ENDTRY 
	ENDFOR 
	SELECT nabory
	RELEASE kody
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret

*!*****************************************************************************
FUNCTION citajstravkodypohyb
PARAMETERS hor_adr, hor_port, api_usr, api_pwd, datefrom
LOCAL textik
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_str_kody_pohyby')
lsnakex.DRAW()
LOCAL m.ret, m.lcClasslibWas, vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
m.textik="?date_from="+ALLTRIM(STR(YEAR(m.datefrom),4,0))+"-"+PADL(ALLTRIM(STR(MONTH(m.datefrom),2,0)),2,"0")+"-"+PADL(ALLTRIM(STR(DAY(m.datefrom),2,0)),2,"0")+";date_to="+ALLTRIM(STR(YEAR(m.datefrom),4,0))+"-"+PADL(ALLTRIM(STR(MONTH(m.datefrom),2,0)),2,"0")+"-"+PADL(ALLTRIM(STR(DAY(m.datefrom),2,0)),2,"0")
IF EMPTY(m.hor_http_port)	
	IF EMPTY(m.hor_port)
		http.OPEN("GET","https://"+m.hor_adr+"/api/fandb/"+m.textik , .F.)
	ELSE
		http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/fandb/"+m.textik , .F.)
	ENDIF 
ELSE
	http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/fandb/"+m.textik , .F.)
ENDIF 
Http.Option(4) = 13056 
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
*	http.SEND('{"date_from":"'+DTOC(m.datefrom)+'","date_to":"'+DTOC(m.datefrom)+'"}')
	http.SEND('')
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	lsnakex.DRAW()
	CREATE CURSOR stravapohyb (kod c(30), quantity I, source_id I)
	IF '"data": []'$http.responsetext OR '[]'$http.responsetext
	ELSE 
		m.vysledok=lojson.parse(http.responsetext)
		lsnakex.DRAW()
		RELEASE lojson
		FOR m.i=1 TO ALEN(vysledok.data)
			TRY 
				INSERT INTO stravapohyb (kod, quantity, source_id) VALUES(vysledok.data[m.i].fb_code, vysledok.data[m.i].quantity, vysledok.data[m.i].source_id)
				lsnakex.DRAW()
			CATCH 
			ENDTRY 
		ENDFOR 
		SELECT stravapohyb
	ENDIF 
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret


*!*****************************************************************************
FUNCTION citajplatby
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_platby')
lsnakex.DRAW()
LOCAL m.ret, m.lcClasslibWas, m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/payment_methods/" , .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/payment_methods/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/payment_methods/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/paymentMethods/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/paymentMethods/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/paymentMethods/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
TRY 
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext)
		lsnakex.DRAW()
		RELEASE lojson
		CREATE CURSOR platby (type I, name c(30), code c(2), id i)
		FOR m.i=1 TO ALEN(vysledok.data)
			TRY 
				INSERT INTO platby (type, name, code, id) VALUES(vysledok.data[m.i].type, vysledok.data[m.i].name,vysledok.data[m.i].code,vysledok.data[m.i].id)
				lsnakex.DRAW()
			CATCH
			ENDTRY  
		ENDFOR 
	ELSE 
		EXTERNAL ARRAY platba
		DIMENSION platba(1,1)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext,"",@platba)
		lsnakex.DRAW()
		RELEASE lojson
		CREATE CURSOR platby (type I, name c(30), code c(2), 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)
				lsnakex.DRAW()
			CATCH
			ENDTRY  
		ENDFOR 
		RELEASE platba
	ENDIF 
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret

*!*****************************************************************************
FUNCTION citajkreditky
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
LOCAL m.ret, m.lcClasslibWas, m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/payment_card_types/" , .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/payment_card_types/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/payment_card_types/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/paymentCardTypes/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/paymentCardTypes/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/paymentCardTypes/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
		m.vysledok=lojson.parse(http.responsetext)
		RELEASE lojson
		CREATE CURSOR kreditky (text c(30),bonus n(10,2), limit I, name c(30), code c(3), id i)
		FOR m.i=1 TO ALEN(vysledok.data)
			TRY 
				INSERT INTO kreditky (text, bonus, limit, name, code, id) VALUES(vysledok.data[m.i].text, VAL(vysledok.data[m.i].bonus), vysledok.data[m.i].limit, vysledok.data[m.i].name, vysledok.data[m.i].code, vysledok.data[m.i].id)
			CATCH 
			ENDTRY 
		ENDFOR 
	ELSE
		EXTERNAL ARRAY kreditka
		DIMENSION kreditka(1,1)
		m.vysledok=lojson.parse(http.responsetext,"",@kreditka)
		RELEASE lojson
		CREATE CURSOR kreditky (text c(30),bonus n(10,2), limit I, name c(30), code c(3), 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
	ENDIF 
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
RETURN m.ret

*!*****************************************************************************
FUNCTION citajcurrencies
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
LOCAL m.ret, m.lcClasslibWas, m.vysledok
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_meny')
lsnakex.DRAW()
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/currencies/" , .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/currencies/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/currencies/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/currencies/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/currencies/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/currencies/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
*Http.SetCredentials(m.hor_usr,m.hor_pwd, 0)
TRY 
	lsnakex.DRAW()
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext)
		lsnakex.DRAW()
		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(vysledok.data)
			TRY 
				INSERT INTO currencies (round, units, code, deviza, name, valuta, id);
					VALUES(vysledok.data[m.i].round, vysledok.data[m.i].units, vysledok.data[m.i].code, VAL(vysledok.data[m.i].deviza), vysledok.data[m.i].name, VAL(vysledok.data[m.i].valuta), vysledok.data[m.i].id)
				lsnakex.DRAW()
			CATCH
			ENDTRY 
		ENDFOR 
	ELSE 
		EXTERNAL ARRAY curency
		DIMENSION curency(1,1)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext,"",@curency)
		lsnakex.DRAW()
		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)
				lsnakex.DRAW()
			CATCH
			ENDTRY 
		ENDFOR 
		RELEASE curency
	ENDIF 
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret

*!*****************************************************************************
FUNCTION citajizby
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
LOCAL m.ret, m.lcClasslibWas, m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/current_accounts/" , .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/current_accounts/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/current_accounts/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/currentAccounts/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/currentAccounts/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/currentAccounts/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
*Http.SetCredentials(m.hor_usr,m.hor_pwd, 0)
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	m.vysledok=lojson.parse(http.responsetext)
	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), przv c(30), meno c(30))
	*** L.L. (10.09.2015.02) Milal poslal opravu -> doplneny TRY
	TRY 
		FOR m.i=1 TO ALEN(vysledok.groups)
			IF !ISNULL(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 
	CATCH
	ENDTRY
	*** L.L. (10.09.2015.02) Milal poslal opravu -> doplneny TRY
	TRY
		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, przv, meno);
					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)), IIF(EMPTY(vysledok.rooms[m.i].last_name) OR ISNULL(vysledok.rooms[m.i].last_name),"",vysledok.rooms[m.i].last_name), IIF(EMPTY(vysledok.rooms[m.i].first_name) OR ISNULL(vysledok.rooms[m.i].first_name),"",vysledok.rooms[m.i].first_name))
			ENDIF 
		ENDFOR 
	CATCH
	ENDTRY
	RELEASE m.vysledok	
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
RETURN m.ret

*!*****************************************************************************
FUNCTION citajapirooms
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd
LOCAL m.ret, m.lcClasslibWas, m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/rooms/" , .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/rooms/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/rooms/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
*Http.SetCredentials(m.hor_usr,m.hor_pwd, 0)
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	EXTERNAL ARRAY rooms
	DIMENSION rooms(1,1)
	m.vysledok=lojson.parse(http.responsetext)
	RELEASE lojson
	CREATE CURSOR rooms (room_id i, room_code c(10), roomtypeid i, description c(30), poschodie I, beds I, servicecode c(30), phonenumber I, attr1 I, attr2 I, floorcode c(10), building c(30),clean L, inspection L, obsadena L, ord I)
	TRY 
		FOR m.i=1 TO ALEN(vysledok.data)
			IF !ISNULL(vysledok.data[m.i])
				INSERT INTO rooms (room_id, room_code, roomtypeid, description, poschodie, beds, servicecode, phonenumber, attr1, attr2, floorcode, building, clean, inspection, obsadena, ord);
					VALUES(IIF(ISNULL(vysledok.data[m.i].id),0,vysledok.data[m.i].id),;
					IIF(ISNULL(vysledok.data[m.i].code),"",vysledok.data[m.i].code),;
					IIF(ISNULL(vysledok.data[m.i].room_type_id),0,vysledok.data[m.i].room_type_id),;
					IIF(ISNULL(vysledok.data[m.i].description),"",vysledok.data[m.i].description),;
					IIF(ISNULL(vysledok.data[m.i].floor),0,vysledok.data[m.i].floor),;
					IIF(ISNULL(vysledok.data[m.i].beds),0,vysledok.data[m.i].beds),;
					IIF(ISNULL(vysledok.data[m.i].room_service_code),"",vysledok.data[m.i].room_service_code),;
					IIF(ISNULL(vysledok.data[m.i].phone_number),0,vysledok.data[m.i].phone_number),;
					IIF(ISNULL(vysledok.data[m.i].attr_1),0,vysledok.data[m.i].attr_1),;
					IIF(ISNULL(vysledok.data[m.i].attr_2),0,vysledok.data[m.i].attr_2),;
					IIF(ISNULL(vysledok.data[m.i].floor_code),"",vysledok.data[m.i].floor_code),;
					IIF(ISNULL(vysledok.data[m.i].building),"",vysledok.data[m.i].building),;
					IIF(ISNULL(vysledok.data[m.i].clean),.f.,vysledok.data[m.i].clean),;
					IIF(ISNULL(vysledok.data[m.i].inspection),.f.,vysledok.data[m.i].inspection),;
					IIF(ISNULL(vysledok.data[m.i].used),.f.,vysledok.data[m.i].used),;
					IIF(ISNULL(vysledok.data[m.i].ord),0,vysledok.data[m.i].ord))
			ENDIF 
		ENDFOR 
	CATCH
	ENDTRY
	RELEASE m.vysledok	
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
RETURN m.ret


*!*****************************************************************************
FUNCTION citajhosti
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, id_izby, api_usr, api_pwd &&accounts.id
LOCAL m.ret, m.lcClasslibWas, m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/current_guests/" , .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/current_guests/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/current_guests/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/currentGuests/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/currentGuests/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/currentGuests/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
*Http.SetCredentials(m.hor_usr,m.hor_pwd, 0)
TRY 
	http.SEND('{"room_id":'+ALLTRIM(STR(m.id_izby,10,0))+'}')
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
		m.vysledok=lojson.parse(http.responsetext)
		RELEASE lojson
		CREATE CURSOR hostia (id I, first c(100), last c(100))
		FOR m.i=1 TO ALEN(vysledok.data)
			TRY 
				INSERT INTO hostia (id, first, last) VALUES(vysledok.data[m.i].id, IIF(ISNULL(vysledok.data[m.i].first_name) OR EMPTY(vysledok.data[m.i].first_name),"",vysledok.data[m.i].first_name), IIF(ISNULL(vysledok.data[m.i].last_name) OR EMPTY(vysledok.data[m.i].last_name),"",vysledok.data[m.i].last_name))
			CATCH
			ENDTRY 
		ENDFOR 
		SELECT hostia
	ELSE 
		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, IIF(ISNULL(host[m.i].first_name) OR EMPTY(host[m.i].first_name),"",host[m.i].first_name), IIF(ISNULL(host[m.i].last_name) OR EMPTY(host[m.i].last_name),"",host[m.i].last_name))
			CATCH
			ENDTRY 
		ENDFOR 
		SELECT hostia
		RELEASE host
	ENDIF 
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
RETURN m.ret

*!*****************************************************************************
FUNCTION citajkartu
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, id_karty, api_usr, api_pwd &&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, m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/check_card/" , .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/check_card/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/check_card/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/checkCard/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/checkCard/" , .F.)
		ENDIF  
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/checkCard/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
*Http.SetCredentials(m.hor_usr,m.hor_pwd, 0)
TRY 
	http.SEND('{"card_id":'+ALLTRIM(STR(m.id_karty,10,0))+'}')
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	m.lcClasslibWas = SET("Classlib")
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	m.vysledok=lojson.parse(http.responsetext)
	RELEASE lojson
	CREATE CURSOR hotkarta (result I, account_id I, room_code c(10), message c(100), room_id I)
	IF !ISNULL(vysledok)
		IF vysledok.result=0
			INSERT INTO hotkarta (result, account_id, room_code, room_id) VALUES(vysledok.result, vysledok.account_id, vysledok.room_code, vysledok.room_id)
		ELSE 
			INSERT INTO hotkarta (result, message) VALUES(vysledok.result, vysledok.message)
		ENDIF 		
	ELSE
		INSERT INTO hotkarta (result, message) VALUES(99, jaz("form_hores_neodpovedal"))
	ENDIF 
	SELECT hotkarta
	RELEASE karta
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
RETURN m.ret

*!*****************************************************************************
FUNCTION citajzoznamhosti
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
LOCAL m.ret, m.lcClasslibWas, m.vysledok, datumnarodenia, m.prichod, m.odchod
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/currentGuests/" , .F.)
		ELSE 
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/currentGuests/" , .F.)
		ENDIF 	
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C", m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/current_guests/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/currentGuests/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/currentGuests/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/currentGuests/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
TRY 
	http.SEND('{}')
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
		m.vysledok=lojson.parse(http.responsetext)
		RELEASE lojson
		CREATE CURSOR hostia (id I, first c(100), last c(100), od D, do D, nar D, izba c(10))
		FOR m.i=1 TO ALEN(vysledok.data)
			IF ISNULL(vysledok.data[m.i].birth_date) OR EMPTY(vysledok.data[m.i].birth_date)
				m.datumnarodenia={..}
			ELSE 
				m.datumnarodenia=vysledok.data[m.i].birth_date
				m.datumnarodenia=CTOD(RIGHT(m.datumnarodenia,2)+"."+SUBSTR(m.datumnarodenia,6,2)+"."+LEFT(m.datumnarodenia,4))
			ENDIF 
			IF ISNULL(vysledok.data[m.i].checkin_date) OR EMPTY(vysledok.data[m.i].checkin_date)
				m.prichod={..}
			ELSE 
				m.prichod=vysledok.data[m.i].checkin_date
				m.prichod=CTOD(RIGHT(m.prichod,2)+"."+SUBSTR(m.prichod,6,2)+"."+LEFT(m.prichod,4))
			ENDIF 
			IF ISNULL(vysledok.data[m.i].checkout_date) OR EMPTY(vysledok.data[m.i].checkout_date)
				m.odchod={..}
			ELSE 
				m.odchod=vysledok.data[m.i].checkout_date
				m.odchod=CTOD(RIGHT(m.odchod,2)+"."+SUBSTR(m.odchod,6,2)+"."+LEFT(m.odchod,4))
			ENDIF 
			TRY 
				INSERT INTO hostia (id, first, last, od, do, nar, izba) VALUES(vysledok.data[m.i].id, IIF(ISNULL(vysledok.data[m.i].first_name) OR EMPTY(vysledok.data[m.i].first_name),"",vysledok.data[m.i].first_name), IIF(ISNULL(vysledok.data[m.i].last_name) OR EMPTY(vysledok.data[m.i].last_name),"",vysledok.data[m.i].last_name), m.prichod, m.odchod, m.datumnarodenia, vysledok.data[m.i].room)
			CATCH
			ENDTRY 
		ENDFOR 
		SELECT hostia
	ELSE
		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), od D, do D, nar D, izba c(10))
		FOR m.i=1 TO ALEN(host)
			IF ISNULL(host[m.i].birth_date) OR EMPTY(host[m.i].birth_date)
				m.datumnarodenia={..}
			ELSE 
				m.datumnarodenia=host[m.i].birth_date
				m.datumnarodenia=CTOD(RIGHT(m.datumnarodenia,2)+"."+SUBSTR(m.datumnarodenia,6,2)+"."+LEFT(m.datumnarodenia,4))
			ENDIF 
			IF ISNULL(host[m.i].checkin_date) OR EMPTY(host[m.i].checkin_date)
				m.prichod={..}
			ELSE 
				m.prichod=host[m.i].checkin_date
				m.prichod=CTOD(RIGHT(m.prichod,2)+"."+SUBSTR(m.prichod,6,2)+"."+LEFT(m.prichod,4))
			ENDIF 
			IF ISNULL(host[m.i].checkout_date) OR EMPTY(host[m.i].checkout_date)
				m.odchod={..}
			ELSE 
				m.odchod=host[m.i].checkout_date
				m.odchod=CTOD(RIGHT(m.odchod,2)+"."+SUBSTR(m.odchod,6,2)+"."+LEFT(m.odchod,4))
			ENDIF 
			TRY 
				INSERT INTO hostia (id, first, last, od, do, nar, izba) VALUES(host[m.i].id, IIF(ISNULL(host[m.i].first_name) OR EMPTY(host[m.i].first_name),"",host[m.i].first_name), IIF(ISNULL(host[m.i].last_name) OR EMPTY(host[m.i].last_name),"",host[m.i].last_name), m.prichod, m.odchod, m.datumnarodenia, host[m.i].room)
			CATCH
			ENDTRY 
		ENDFOR 
		SELECT hostia
		RELEASE host
	ENDIF 
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
RETURN m.ret

*!*****************************************************************************
FUNCTION natazhosta
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, acc_id, host_id, api_usr, api_pwd
*acc_id=10028
*host_id=0
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/api/charge_account/" , .F.)
		ELSE 
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/charge_account/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/charge_account/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/chargeAccount/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/chargeAccount/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/chargeAccount/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
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("prevodh10")
REPLACE jsontxt.jsontxt WITH STRTRAN(jsontxt.jsontxt,'"VFPData": {"prevodh10": [','')
TRY 
	http.SEND(jsontxt.jsontxt)
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
USE IN jsontxt
IF m.vysledok=200
	IF !ISNULL(http.responsetext)
		m.vysledok=lojson.parse(http.responsetext)
		RELEASE lojson
		TRY 
			m.ret=vysledok.result
			m.odpoved=jaz("form_polozky_natazene")
		CATCH
			m.ret=100
			m.odpoved=vysledok.message
		ENDTRY 
	ELSE
		m.ret=http.status
		m.odpoved=http.statustext
	ENDIF 
ELSE
	m.ret=m.vysledok
	m.odpoved=http.statustext
ENDIF 
RELEASE m.http
RELEASE CLASSLIB json.vcx
RETURN m.ret

*!*****************************************************************************
FUNCTION natazpokladnu
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/api/transfer_cash/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/transfer_cash/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/transfer_cash/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/transferCash/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/transferCash/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/transferCash/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
m.loJSON = CREATEOBJECT("JSON")
CREATE CURSOR jsontxt (jsontxt M)
INSERT INTO jsontxt (jsontxt) VALUES ("") 
REPLACE jsontxt.jsontxt WITH '[{"cash_register_code":"'+ALLTRIM(m.id_zkratka)+'",'+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("prevodh10")
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+']'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH STRTRAN(jsontxt.jsontxt,'"VFPData": {"prevodh10": [','')
TRY 
	http.SEND(jsontxt.jsontxt)
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
USE IN jsontxt
IF m.vysledok=200
	IF !ISNULL(http.responsetext)
		m.vysledok=lojson.parse(http.responsetext)
		TRY 
			m.ret=vysledok.result
			m.odpoved=jaz("form_polozky_natazene")
		CATCH
			m.ret=100
			m.odpoved=vysledok.message
		ENDTRY 
		RELEASE lojson
	ELSE
		m.ret=http.status
		m.odpoved=http.statustext
	ENDIF 
ELSE
	m.ret=m.vysledok
	m.odpoved=http.statustext
ENDIF 
RELEASE m.http
RELEASE CLASSLIB json.vcx
RETURN m.ret 

*!*****************************************************************************
FUNCTION citajskupinydp
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
LOCAL pocetriadkov
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_skupiny_dp')
lsnakex.DRAW()
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/product_categories/" , .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/product_categories/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/product_categories/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/productCategories/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/productCategories/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/productCategories/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
*Http.SetCredentials(m.hor_usr,m.hor_pwd, 0)
TRY 
	lsnakex.DRAW()
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext)
		lsnakex.DRAW()
		RELEASE lojson
		CREATE CURSOR dp_skupiny (druh_id i, druh_meno c(30), oprava L)
		INDEX on druh_id TAG druh_id
		INDEX on druh_meno TAG druh_meno
		TRY 
			m.pocetriadkov=ALEN(vysledok.data,1)
		CATCH
			m.pocetriadkov=0
		ENDTRY 
		IF m.pocetriadkov>0
			FOR m.i=1 TO ALEN(vysledok.data,1)
				IF !ISNULL(vysledok.data[m.i].id)
					TRY 
						INSERT INTO dp_skupiny (druh_id, druh_meno) VALUES(vysledok.data[m.i].id, vysledok.data[m.i].title)
					CATCH
					ENDTRY 
					lsnakex.DRAW()
				ENDIF 
			ENDFOR 
		ENDIF 
	ELSE
		EXTERNAL ARRAY dppol
		DIMENSION dppol(1,1)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext,"",@dppol)
		lsnakex.DRAW()
		RELEASE lojson
		CREATE CURSOR dp_skupiny (druh_id i, druh_meno c(30), oprava L)
		INDEX on druh_id TAG druh_id
		INDEX on druh_meno TAG druh_meno
		TRY 
			m.pocetriadkov=ALEN(dppol,1)
		CATCH
			m.pocetriadkov=0
		ENDTRY 
		IF pocetriadkov=1 
			TRY
				IF dppol(1)=.f.
					m.pocetriadkov=0
				ENDIF 
			CATCH
			ENDTRY
		ENDIF 
		IF m.pocetriadkov>0
			FOR m.i=1 TO m.pocetriadkov
				IF !ISNULL(dppol[m.i].id)
					TRY 
						INSERT INTO dp_skupiny (druh_id, druh_meno) VALUES(dppol[m.i].id, dppol[m.i].title)
					CATCH
					ENDTRY 
					lsnakex.DRAW()
				ENDIF 
			ENDFOR 
		ENDIF 
		RELEASE dppol
	ENDIF 
	RELEASE m.vysledok
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret

*!*****************************************************************************
FUNCTION zapisskupinydp
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
DO FORM snake NAME lsnakex LINKED WITH jaz('form_zapisujem_skupiny_dp')
lsnakex.DRAW()
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/api/product_categories/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/product_categories/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/product_categories/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/updateProductCategories/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/updateProductCategories/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/updateProductCategories/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
*Http.SetCredentials(m.hor_usr,m.hor_pwd, 0)
m.loJSON = CREATEOBJECT("JSON")
CREATE CURSOR jsontxt (jsontxt M)
INSERT INTO jsontxt (jsontxt) VALUES ("") 
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+lojson.stringify("dpskupiny")
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+']'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH STRTRAN(jsontxt.jsontxt,'"VFPData": {"dpskupiny": ','')
REPLACE jsontxt.jsontxt WITH STRTRAN(jsontxt.jsontxt,'}]}]','}]')
REPLACE jsontxt.jsontxt WITH STRTRAN(jsontxt.jsontxt,'\u009D','ť')
TRY 
	lsnakex.DRAW()
	http.SEND(jsontxt.jsontxt)
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
USE IN jsontxt
IF m.vysledok=200
	IF !ISNULL(http.responsetext)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext)
		lsnakex.DRAW()
		TRY 
			m.ret=vysledok.result
			m.odpoved=jaz("form_skupiny_zaktualizovane")
		CATCH
			m.ret=100
			m.odpoved=vysledok.message
		ENDTRY 
		RELEASE lojson
	ELSE
		m.ret=http.status
		m.odpoved=http.statustext
	ENDIF 
ELSE
	m.ret=m.vysledok
	m.odpoved=http.statustext
ENDIF 
RELEASE m.http
lsnakex.release()
RELEASE CLASSLIB json.vcx
RETURN m.ret 

*!*****************************************************************************
FUNCTION zapispolozkydp
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
DO FORM snake NAME lsnakex LINKED WITH jaz('form_zapisujem_polozky_dp')
lsnakex.DRAW()
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/api/products/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/products/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/products/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/updateProducts/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/updateProducts/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/updateProducts/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
*Http.SetCredentials(m.hor_usr,m.hor_pwd, 0)
m.loJSON = CREATEOBJECT("JSON")
CREATE CURSOR jsontxt (jsontxt M)
INSERT INTO jsontxt (jsontxt) VALUES ("") 
lsnakex.DRAW()
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+lojson.stringify("dppolozky")
lsnakex.DRAW()
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+']'+CHR(13)+CHR(10)
lsnakex.DRAW()
REPLACE jsontxt.jsontxt WITH STRTRAN(jsontxt.jsontxt,'"VFPData": {"dppolozky": ','')
lsnakex.DRAW()
REPLACE jsontxt.jsontxt WITH STRTRAN(jsontxt.jsontxt,'}]}]','}]')
REPLACE jsontxt.jsontxt WITH STRTRAN(jsontxt.jsontxt,'\u009D','ť')
lsnakex.DRAW()
TRY 
	lsnakex.DRAW()
	http.SEND(jsontxt.jsontxt)
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
USE IN jsontxt
IF m.vysledok=200
	IF !ISNULL(http.responsetext)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext)
		lsnakex.DRAW()
		TRY 
			m.ret=vysledok.result
			m.odpoved="Položky zaktualizované"
		CATCH
			m.ret=100
			m.odpoved=vysledok.message
		ENDTRY 
		RELEASE lojson
	ELSE
		m.ret=http.status
		m.odpoved=http.statustext
	ENDIF 
ELSE
	m.ret=m.vysledok
	m.odpoved=http.statustext
ENDIF 
RELEASE m.http
lsnakex.release()
RELEASE CLASSLIB json.vcx
RETURN m.ret 

*!*****************************************************************************
FUNCTION citajpolozkydp
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
PRIVATE pocetriadkov
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_polozky_dp')
lsnakex.DRAW()
LOCAL m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/products/" , .F.)
		ELSE 
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/products/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/products/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE 
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/products/" , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/products/" , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/products/" , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
*Http.SetCredentials(m.hor_usr,m.hor_pwd, 0)
TRY 
	lsnakex.DRAW()
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext)
		lsnakex.DRAW()
		RELEASE lojson
		CREATE CURSOR dp_polozky (id i, title c(30), unit c(3), rkod c(2), nc y, pc1 y, kod c(13), druh_id I, nasklade I, sluzba L, pc2 Y, pc3 Y, raster_id I, c_karty I, stav C(30), id_man c(2))
		INDEX on id TAG id
		IF ALEN(vysledok.data,1)>0
			FOR m.i=1 TO ALEN(vysledok.data,1)
				IF !ISNULL(vysledok.data[m.i].id)
					IF VARTYPE(vysledok.data[m.i].catalogue_number)="L"
						IF SET("point")="."
							INSERT INTO dp_polozky (id, title, unit, raster_id, nc, pc1, kod, druh_id, nasklade, sluzba, pc2, pc3, c_karty) VALUES(;
								 vysledok.data[m.i].id,;
								 vysledok.data[m.i].title,;
								 IIF(ISNULL(vysledok.data[m.i].unit),"",vysledok.data[m.i].unit),;
								 vysledok.data[m.i].raster_id,;
								 VAL(STRTRAN(vysledok.data[m.i].purchase_price,",",".")),;
								 VAL(STRTRAN(vysledok.data[m.i].selling_price,",",".")),;
								 IIF(ISNULL(vysledok.data[m.i].product_code),"",vysledok.data[m.i].product_code),;
								 vysledok.data[m.i].category_id,;
								 vysledok.data[m.i].stock,;
								 .t.,;
								 IIF(ISNULL(VAL(STRTRAN(vysledok.data[m.i].selling_price1,",","."))),0,VAL(STRTRAN(vysledok.data[m.i].selling_price1,",","."))),;
								 IIF(ISNULL(VAL(STRTRAN(vysledok.data[m.i].selling_price2,",","."))),0,VAL(STRTRAN(vysledok.data[m.i].selling_price2,",","."))),;
								 0)
						ELSE
							INSERT INTO dp_polozky (id, title, unit, raster_id, nc, pc1, kod, druh_id, nasklade, sluzba, pc2, pc3, c_karty) VALUES(;
								 vysledok.data[m.i].id,;
								 vysledok.data[m.i].title,;
								 IIF(ISNULL(vysledok.data[m.i].unit),;
								 "",;
								 vysledok.data[m.i].unit),;
								 vysledok.data[m.i].raster_id,;
								 VAL(STRTRAN(vysledok.data[m.i].purchase_price,".",",")),;
								 VAL(STRTRAN(vysledok.data[m.i].selling_price,".",",")),;
								 IIF(ISNULL(vysledok.data[m.i].product_code),;
								 "",;
								 vysledok.data[m.i].product_code),;
								 vysledok.data[m.i].category_id,;
								 vysledok.data[m.i].stock,;
								 .t.,;
								 IIF(ISNULL(VAL(STRTRAN(vysledok.data[m.i].selling_price1,".",","))),0,VAL(STRTRAN(vysledok.data[m.i].selling_price1,".",","))),;
								 IIF(ISNULL(VAL(STRTRAN(vysledok.data[m.i].selling_price2,".",","))),0,VAL(STRTRAN(vysledok.data[m.i].selling_price2,".",","))),;
								 0)
						ENDIF 
						lsnakex.DRAW()
					ELSE 
						IF SET("point")="."
							INSERT INTO dp_polozky (id, title, unit, raster_id, nc, pc1, kod, druh_id, nasklade, sluzba, pc2, pc3, c_karty) VALUES(;
								 vysledok.data[m.i].id,;
								 vysledok.data[m.i].title,;
								 IIF(ISNULL(vysledok.data[m.i].unit),"",vysledok.data[m.i].unit),;
								 vysledok.data[m.i].raster_id,;
								 VAL(STRTRAN(vysledok.data[m.i].purchase_price,",",".")),;
								 VAL(STRTRAN(vysledok.data[m.i].selling_price,",",".")),;
								 IIF(ISNULL(vysledok.data[m.i].product_code),;
								 "",;
								 vysledok.data[m.i].product_code),;
								 vysledok.data[m.i].category_id,;
								 vysledok.data[m.i].stock,;
								 .t.,;
								 IIF(ISNULL(VAL(STRTRAN(vysledok.data[m.i].selling_price1,",","."))),0,VAL(STRTRAN(vysledok.data[m.i].selling_price1,",","."))),;
								 IIF(ISNULL(VAL(STRTRAN(vysledok.data[m.i].selling_price2,",","."))),0,VAL(STRTRAN(vysledok.data[m.i].selling_price2,",","."))),;
								 IIF(ISNULL(vysledok.data[m.i].catalogue_number),0,VAL(vysledok.data[m.i].catalogue_number)))
						ELSE
							INSERT INTO dp_polozky (id, title, unit, raster_id, nc, pc1, kod, druh_id, nasklade, sluzba, pc2, pc3, c_karty) VALUES(;
								 vysledok.data[m.i].id,;
								 vysledok.data[m.i].title,;
								 IIF(ISNULL(vysledok.data[m.i].unit),"",vysledok.data[m.i].unit),;
								 vysledok.data[m.i].raster_id,;
								 VAL(STRTRAN(vysledok.data[m.i].purchase_price,".",",")),;
								 VAL(STRTRAN(vysledok.data[m.i].selling_price,".",",")),;
								 IIF(ISNULL(vysledok.data[m.i].product_code),;
								 "",;
								 vysledok.data[m.i].product_code),;
								 vysledok.data[m.i].category_id,;
								 vysledok.data[m.i].stock,;
								 .t.,;
								 IIF(ISNULL(VAL(STRTRAN(vysledok.data[m.i].selling_price1,".",","))),0,VAL(STRTRAN(vysledok.data[m.i].selling_price1,".",","))),;
								 IIF(ISNULL(VAL(STRTRAN(vysledok.data[m.i].selling_price2,",","."))),0,VAL(STRTRAN(vysledok.data[m.i].selling_price2,".",","))),;
								 IIF(ISNULL(vysledok.data[m.i].catalogue_number),0,VAL(vysledok.data[m.i].catalogue_number)))
						ENDIF 
						lsnakex.DRAW()
					ENDIF 
				ENDIF 
			ENDFOR 
		ENDIF 
	ELSE
		EXTERNAL ARRAY dppol
		DIMENSION dppol(1,1)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext,"",@dppol)
		lsnakex.DRAW()
		RELEASE lojson
		CREATE CURSOR dp_polozky (id i, title c(30), unit c(3), rkod c(2), nc y, pc1 y, kod c(13), druh_id I, nasklade I, sluzba L, pc2 Y, pc3 Y, raster_id I, c_karty I, stav C(30), id_man c(2))
		INDEX on id TAG id
		TRY 
			m.pocetriadkov=ALEN(dppol,1)
		CATCH
			m.pocetriadkov=0
		ENDTRY 
		IF pocetriadkov=1 
			TRY
				IF dppol(1)=.f.
					m.pocetriadkov=0
				ENDIF 
			CATCH
			ENDTRY
		ENDIF 
		IF m.pocetriadkov>0
			FOR m.i=1 TO m.pocetriadkov
				IF !ISNULL(dppol[m.i].id)
					IF VARTYPE(dppol[m.i].catalogue_number)="L"
						IF SET("point")="."
							INSERT INTO dp_polozky (id, title, unit, raster_id, nc, pc1, kod, druh_id, nasklade, sluzba, pc2, pc3, c_karty) VALUES(dppol[m.i].id, dppol[m.i].title,;
								 IIF(ISNULL(dppol[m.i].unit),"",dppol[m.i].unit),dppol[m.i].raster_id,VAL(STRTRAN(dppol[m.i].purchase_price,",",".")),VAL(STRTRAN(dppol[m.i].selling_price,",",".")),IIF(ISNULL(dppol[m.i].product_code),"",dppol[m.i].product_code),dppol[m.i].category_id,;
								 dppol[m.i].stock,.t.,VAL(STRTRAN(dppol[m.i].selling_price1,",",".")),VAL(STRTRAN(dppol[m.i].selling_price2,",",".")), 0)
						ELSE
							INSERT INTO dp_polozky (id, title, unit, raster_id, nc, pc1, kod, druh_id, nasklade, sluzba, pc2, pc3, c_karty) VALUES(dppol[m.i].id, dppol[m.i].title,;
								 IIF(ISNULL(dppol[m.i].unit),"",dppol[m.i].unit),dppol[m.i].raster_id,VAL(STRTRAN(dppol[m.i].purchase_price,".",",")),VAL(STRTRAN(dppol[m.i].selling_price,".",",")),IIF(ISNULL(dppol[m.i].product_code),"",dppol[m.i].product_code),dppol[m.i].category_id,;
								 dppol[m.i].stock,.t.,VAL(STRTRAN(dppol[m.i].selling_price1,".",",")),VAL(STRTRAN(dppol[m.i].selling_price2,".",",")), 0)
						ENDIF 
						lsnakex.DRAW()
					ELSE 
						IF SET("point")="."
							INSERT INTO dp_polozky (id, title, unit, raster_id, nc, pc1, kod, druh_id, nasklade, sluzba, pc2, pc3, c_karty) VALUES(dppol[m.i].id, dppol[m.i].title,;
								 IIF(ISNULL(dppol[m.i].unit),"",dppol[m.i].unit),dppol[m.i].raster_id,VAL(STRTRAN(dppol[m.i].purchase_price,",",".")),VAL(STRTRAN(dppol[m.i].selling_price,",",".")),IIF(ISNULL(dppol[m.i].product_code),"",dppol[m.i].product_code),dppol[m.i].category_id,;
								 dppol[m.i].stock,.t.,VAL(STRTRAN(dppol[m.i].selling_price1,",",".")),VAL(STRTRAN(dppol[m.i].selling_price2,",",".")), IIF(ISNULL(dppol[m.i].catalogue_number),0,VAL(dppol[m.i].catalogue_number)))
						ELSE
							INSERT INTO dp_polozky (id, title, unit, raster_id, nc, pc1, kod, druh_id, nasklade, sluzba, pc2, pc3, c_karty) VALUES(dppol[m.i].id, dppol[m.i].title,;
								 IIF(ISNULL(dppol[m.i].unit),"",dppol[m.i].unit),dppol[m.i].raster_id,VAL(STRTRAN(dppol[m.i].purchase_price,".",",")),VAL(STRTRAN(dppol[m.i].selling_price,".",",")),IIF(ISNULL(dppol[m.i].product_code),"",dppol[m.i].product_code),dppol[m.i].category_id,;
								 dppol[m.i].stock,.t.,VAL(STRTRAN(dppol[m.i].selling_price1,".",",")),VAL(STRTRAN(dppol[m.i].selling_price2,".",",")), IIF(ISNULL(dppol[m.i].catalogue_number),0,VAL(dppol[m.i].catalogue_number)))
						ENDIF 
						lsnakex.DRAW()
					ENDIF 
				ENDIF 
			ENDFOR 
		ENDIF 
		RELEASE dppol
	ENDIF 
	RELEASE m.vysledok
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
IF USED("dp_polozky") AND RECCOUNT("dp_polozky")>0 
	IF USED("rastre")
		UPDATE dp_polozky SET rkod=rastre.code FROM rastre WHERE dp_polozky.raster_id=rastre.id
	ELSE 
		=citajrastre(hor_adr, hor_port, hor_usr, hor_pwd)
		UPDATE dp_polozky SET rkod=rastre.code FROM rastre WHERE dp_polozky.raster_id=rastre.id
		USE IN rastre
	ENDIF 
	SELECT dp_polozky
ENDIF 
lsnakex.release()
RETURN m.ret


*!*****************************************************************************
FUNCTION citajpredajrastrov
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, aid, cid, api_usr, api_pwd
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_predaj_rastrov')
lsnakex.DRAW()
LOCAL m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/raster_movements?aid="+ALLTRIM(STR(m.aid,10,0))+"&cid="+ALLTRIM(STR(m.cid,10,0)) , .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/raster_movements?aid="+ALLTRIM(STR(m.aid,10,0))+"&cid="+ALLTRIM(STR(m.cid,10,0)) , .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/raster_movements?aid="+ALLTRIM(STR(m.aid,10,0))+"&cid="+ALLTRIM(STR(m.cid,10,0)) , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/rasterMovements?aid="+ALLTRIM(STR(m.aid,10,0))+"&cid="+ALLTRIM(STR(m.cid,10,0)) , .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/rasterMovements?aid="+ALLTRIM(STR(m.aid,10,0))+"&cid="+ALLTRIM(STR(m.cid,10,0)) , .F.)
		ENDIF 
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/rasterMovements?aid="+ALLTRIM(STR(m.aid,10,0))+"&cid="+ALLTRIM(STR(m.cid,10,0)) , .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
TRY 
	lsnakex.DRAW()
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
		lsnakex.DRAW()
		CREATE CURSOR dp_predaj (typ c(1), id I, raster_id I, cena_celk Y, id_meny I, datcas T, doklad c(30), cena Y, pocet I, id_rodic I, id_pol I, rkod c(2), mena c(3), c_karty I, raster c(30), polozka c(30), id_platba I, platba c(8), pr n(5,3), storno I, c_uzav I)
		IF http.responsetext='{"result": 0, "data": []}'
		ELSE
			m.vysledok=lojson.parse(http.responsetext)
			lsnakex.DRAW()
			RELEASE lojson
			FOR m.i=1 TO ALEN(vysledok.data,1)
				IF !ISNULL(vysledok.data(m.i,1)) AND VARTYPE(vysledok.data(m.i,1))<>"L"
					TRY 
						IF SET("point")="."
							INSERT INTO dp_predaj (typ, id, raster_id, cena_celk, id_meny, datcas, doklad, cena, pocet, id_rodic, id_pol, id_platba, storno, c_uzav) VALUES ;
								 (vysledok.data(m.i,1),vysledok.data(m.i,2),IIF(ISNULL(vysledok.data(m.i,3)),0,vysledok.data(m.i,3)),VAL(STRTRAN(vysledok.data(m.i,14),",",".")),vysledok.data(m.i,5),vysledok.data(m.i,6),;
								 IIF(ISNULL(vysledok.data(m.i,7)),"",vysledok.data(m.i,7)),VAL(STRTRAN(vysledok.data(m.i,8),",",".")),vysledok.data(m.i,9),IIF(ISNULL(vysledok.data(m.i,10)),0,vysledok.data(m.i,10)),IIF(ISNULL(vysledok.data(m.i,11)),0,VAL(STRTRAN(vysledok.data(m.i,11),",","."))),IIF(ISNULL(vysledok.data(m.i,12)),0,vysledok.data(m.i,12)),vysledok.data(m.i,13),vysledok.data(m.i,15))
						ELSE 
							INSERT INTO dp_predaj (typ, id, raster_id, cena_celk, id_meny, datcas, doklad, cena, pocet, id_rodic, id_pol, id_platba, storno, c_uzav) VALUES ;
								 (vysledok.data(m.i,1),vysledok.data(m.i,2),IIF(ISNULL(vysledok.data(m.i,3)),0,vysledok.data(m.i,3)),VAL(STRTRAN(vysledok.data(m.i,14),".",",")),vysledok.data(m.i,5),vysledok.data(m.i,6),;
								 IIF(ISNULL(vysledok.data(m.i,7)),"",vysledok.data(m.i,7)),VAL(STRTRAN(vysledok.data(m.i,8),".",",")),vysledok.data(m.i,9),IIF(ISNULL(vysledok.data(m.i,10)),0,vysledok.data(m.i,10)),IIF(ISNULL(vysledok.data(m.i,11)),0,VAL(STRTRAN(vysledok.data(m.i,11),".",","))),IIF(ISNULL(vysledok.data(m.i,12)),0,vysledok.data(m.i,12)),vysledok.data(m.i,13),vysledok.data(m.i,15))
						ENDIF 
					CATCH
						IF SET("point")="."
							INSERT INTO dp_predaj (typ, id, raster_id, cena_celk, id_meny, datcas, doklad, cena, pocet, id_rodic, id_pol, id_platba, storno, c_uzav) VALUES ;
								 (vysledok.data(m.i,1),vysledok.data(m.i,2),IIF(ISNULL(vysledok.data(m.i,3)),0,vysledok.data(m.i,3)),0,vysledok.data(m.i,5),vysledok.data(m.i,6),;
								 IIF(ISNULL(vysledok.data(m.i,7)),"",vysledok.data(m.i,7)),0,vysledok.data(m.i,9),IIF(ISNULL(vysledok.data(m.i,10)),0,vysledok.data(m.i,10)),IIF(ISNULL(vysledok.data(m.i,11)),0,VAL(STRTRAN(vysledok.data(m.i,11),",","."))),IIF(ISNULL(vysledok.data(m.i,12)),0,vysledok.data(m.i,12)),vysledok.data(m.i,13),vysledok.data(m.i,15))
						ELSE 
							INSERT INTO dp_predaj (typ, id, raster_id, cena_celk, id_meny, datcas, doklad, cena, pocet, id_rodic, id_pol, id_platba, storno, c_uzav) VALUES ;
								 (vysledok.data(m.i,1),vysledok.data(m.i,2),IIF(ISNULL(vysledok.data(m.i,3)),0,vysledok.data(m.i,3)),0,vysledok.data(m.i,5),vysledok.data(m.i,6),;
								 IIF(ISNULL(vysledok.data(m.i,7)),"",vysledok.data(m.i,7)),0,vysledok.data(m.i,9),IIF(ISNULL(vysledok.data(m.i,10)),0,vysledok.data(m.i,10)),IIF(ISNULL(vysledok.data(m.i,11)),0,VAL(STRTRAN(vysledok.data(m.i,11),".",","))),IIF(ISNULL(vysledok.data(m.i,12)),0,vysledok.data(m.i,12)),vysledok.data(m.i,13),vysledok.data(m.i,15))
						ENDIF 
					ENDTRY 
					lsnakex.DRAW()
				ENDIF 
			ENDFOR 
		ENDIF 
	ELSE
		EXTERNAL ARRAY dppol
		DIMENSION dppol(1,1)
		lsnakex.DRAW()
		m.vysledok=lojson.parse(http.responsetext,"",@dppol)
		lsnakex.DRAW()
		RELEASE lojson
		CREATE CURSOR dp_predaj (typ c(1), id I, raster_id I, cena_celk Y, id_meny I, datcas T, doklad c(30), cena Y, pocet I, id_rodic I, id_pol I, rkod c(2), mena c(3), c_karty I, raster c(30), polozka c(30), id_platba I, platba c(8), pr n(5,3), storno I, c_uzav I)
		FOR m.i=1 TO ALEN(dppol,1)
			IF !ISNULL(dppol(m.i,1)) AND VARTYPE(dppol(m.i,1))<>"L"
				TRY 
					IF SET("point")="."
						INSERT INTO dp_predaj (typ, id, raster_id, cena_celk, id_meny, datcas, doklad, cena, pocet, id_rodic, id_pol, id_platba, storno, c_uzav) VALUES ;
							 (dppol(m.i,1),dppol(m.i,2),IIF(ISNULL(dppol(m.i,3)),0,dppol(m.i,3)),VAL(STRTRAN(dppol(m.i,14),",",".")),dppol(m.i,5),dppol(m.i,6),;
							 IIF(ISNULL(dppol(m.i,7)),"",dppol(m.i,7)),VAL(STRTRAN(dppol(m.i,8),",",".")),dppol(m.i,9),IIF(ISNULL(dppol(m.i,10)),0,dppol(m.i,10)),IIF(ISNULL(dppol(m.i,11)),0,VAL(STRTRAN(dppol(m.i,11),",","."))),IIF(ISNULL(dppol(m.i,12)),0,dppol(m.i,12)),dppol(m.i,13),dppol(m.i,15))
					ELSE 
						INSERT INTO dp_predaj (typ, id, raster_id, cena_celk, id_meny, datcas, doklad, cena, pocet, id_rodic, id_pol, id_platba, storno, c_uzav) VALUES ;
							 (dppol(m.i,1),dppol(m.i,2),IIF(ISNULL(dppol(m.i,3)),0,dppol(m.i,3)),VAL(STRTRAN(dppol(m.i,14),".",",")),dppol(m.i,5),dppol(m.i,6),;
							 IIF(ISNULL(dppol(m.i,7)),"",dppol(m.i,7)),VAL(STRTRAN(dppol(m.i,8),".",",")),dppol(m.i,9),IIF(ISNULL(dppol(m.i,10)),0,dppol(m.i,10)),IIF(ISNULL(dppol(m.i,11)),0,VAL(STRTRAN(dppol(m.i,11),".",","))),IIF(ISNULL(dppol(m.i,12)),0,dppol(m.i,12)),dppol(m.i,13),dppol(m.i,15))
					ENDIF 
				CATCH
					IF SET("point")="."
						INSERT INTO dp_predaj (typ, id, raster_id, cena_celk, id_meny, datcas, doklad, cena, pocet, id_rodic, id_pol, id_platba, storno, c_uzav) VALUES ;
							 (dppol(m.i,1),dppol(m.i,2),IIF(ISNULL(dppol(m.i,3)),0,dppol(m.i,3)),0,dppol(m.i,5),dppol(m.i,6),;
							 IIF(ISNULL(dppol(m.i,7)),"",dppol(m.i,7)),0,dppol(m.i,9),IIF(ISNULL(dppol(m.i,10)),0,dppol(m.i,10)),IIF(ISNULL(dppol(m.i,11)),0,VAL(STRTRAN(dppol(m.i,11),",","."))),IIF(ISNULL(dppol(m.i,12)),0,dppol(m.i,12)),dppol(m.i,13),dppol(m.i,15))
					ELSE 
						INSERT INTO dp_predaj (typ, id, raster_id, cena_celk, id_meny, datcas, doklad, cena, pocet, id_rodic, id_pol, id_platba, storno, c_uzav) VALUES ;
							 (dppol(m.i,1),dppol(m.i,2),IIF(ISNULL(dppol(m.i,3)),0,dppol(m.i,3)),0,dppol(m.i,5),dppol(m.i,6),;
							 IIF(ISNULL(dppol(m.i,7)),"",dppol(m.i,7)),0,dppol(m.i,9),IIF(ISNULL(dppol(m.i,10)),0,dppol(m.i,10)),IIF(ISNULL(dppol(m.i,11)),0,VAL(STRTRAN(dppol(m.i,11),".",","))),IIF(ISNULL(dppol(m.i,12)),0,dppol(m.i,12)),dppol(m.i,13),dppol(m.i,15))
					ENDIF 
				ENDTRY 
				lsnakex.DRAW()
			ENDIF 
		ENDFOR 
		RELEASE dppol
	ENDIF 
	RELEASE m.vysledok
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret


*!*****************************************************************************
FUNCTION citajdatumuzavierky
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, fid, api_usr, api_pwd
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_datum_uz_c')+' '+ALLTRIM(STR(m.fid))  
lsnakex.DRAW()
LOCAL m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
	IF EMPTY(m.hor_http_port)	
		IF EMPTY(m.hor_port)
			http.OPEN("GET","https://"+m.hor_adr+"/api/final_account?fid="+ALLTRIM(STR(m.fid,10,0)), .F.)
		ELSE
			http.OPEN("GET","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/api/final_account?fid="+ALLTRIM(STR(m.fid,10,0)), .F.)
		ENDIF 
	ELSE
		http.OPEN("GET","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/api/final_account?fid="+ALLTRIM(STR(m.fid,10,0)), .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
ELSE
	IF EMPTY(m.hor_http_port)
		IF EMPTY(m.hor_port)
			http.OPEN("POST","https://"+m.hor_adr+"/cash_register/finalAccount?fid="+ALLTRIM(STR(m.fid,10,0)), .F.)
		ELSE
			http.OPEN("POST","https://"+m.hor_adr+":"+ALLTRIM(STR(m.hor_port,10,0))+"/cash_register/finalAccount?fid="+ALLTRIM(STR(m.fid,10,0)), .F.)
		ENDIF
	ELSE
		http.OPEN("POST","http://"+m.hor_adr+":"+IIF(VARTYPE(m.hor_http_port)="C",m.hor_http_port,ALLTRIM(STR(m.hor_http_port,10,0)))+"/cash_register/finalAccount?fid="+ALLTRIM(STR(m.fid,10,0)), .F.)
	ENDIF 
	Http.Option(4) = 13056
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	http.setrequestheader("Authorization", "Basic " + STRCONV(m.hor_usr + ":" + m.hor_pwd,13))
ENDIF 
*Http.SetCredentials(m.hor_usr,m.hor_pwd, 0)
TRY 
	lsnakex.DRAW()
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	lsnakex.DRAW()
	m.vysledok=lojson.parse(http.responsetext,"")
	lsnakex.DRAW()
	RELEASE lojson
	CREATE CURSOR dp_uzav (datum d, datcas c(17))
	IF !EMPTY(m.api_usr) AND !EMPTY(m.api_pwd)
		IF !ISNULL(vysledok.data.valid_date) AND VARTYPE(vysledok.data.valid_date)=="C"
			INSERT INTO dp_uzav (datum, datcas) VALUES (CTOD(RIGHT(vysledok.data.valid_date,2)+"."+SUBSTR(vysledok.data.valid_date,6,2)+"."+LEFT(vysledok.data.valid_date,4)),LEFT(TTOC(vysledok.data.datetime_start,1),8)+" "+TTOC(vysledok.data.datetime_start,2))
			lsnakex.DRAW()
		ENDIF 
	ELSE
	IF !ISNULL(vysledok.valid_date) AND VARTYPE(vysledok.valid_date)=="C"
		INSERT INTO dp_uzav (datum, datcas) VALUES (CTOD(RIGHT(vysledok.valid_date,2)+"."+SUBSTR(vysledok.valid_date,6,2)+"."+LEFT(vysledok.valid_date,4)),STRTRAN(vysledok.datetime_start,"-","")+":00")
		lsnakex.DRAW()
	ENDIF 
	ENDIF 
	RELEASE m.vysledok
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret

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


*!*****************************************************************************
FUNCTION prep1karty
PARAMETERS ck, lev
PRIVATE m.hladajckarty, m.cena
LOCAL hruba, pocet, m.bolipolozky, c_poloz
m.bolipolozky=.t.
m.cena=0
IF m.lev>15
ELSE 
	LOCAL m.levx
	m.levx="cenymenaz"+ALLTRIM(STR(m.lev,5,0))
	m.hladajckarty=m.ck
	IF m.issql
		rv("polozkycenymenaz",m.levx)
	ELSE
		IF !USED("polozky")
			USE (m.gccen_path+"polozky") IN 0
			m.bolipolozky=.f.
		ENDIF 
		USE vpolozkycenymenaz ALIAS (levx) IN 0
	ENDIF 
	SELECT (m.levx)
	IF RECCOUNT()>0
		SCAN ALL
			m.hruba=MTON(hruba)
			m.pocet=MTON(pocet)
			m.c_poloz=c_poloz
			IF m.pocet=0
				m.pocet=1
			ENDIF 
			SELECT (kurzor)
			LOCATE FOR (c_karty=m.hladajckarty AND zustatek<>0) OR (c_karty=m.hladajckarty AND c_druhk=0)
			IF FOUND()
				m.cena=m.cena+m.hruba*cena_prum/m.pocet
			ELSE
				SELECT uzprep
				LOCATE FOR c_karty=cenymenaz.c_poloz
				IF FOUND()
					m.cena=m.cena+m.hruba*MTON(uzprep.cena)/m.pocet
				ELSE
					m.cena=m.cena+m.hruba*prep1karty(m.c_poloz, m.lev+1)/m.pocet
				ENDIF 
			ENDIF 
			SELECT (m.levx)
		ENDSCAN
	ELSE
		SELECT (kurzor)
		LOCATE FOR c_karty=m.hladajckarty
		IF FOUND()
			m.cena=cena_prum
		ELSE
			m.cena=0
		ENDIF 
	ENDIF 
	INSERT INTO uzprep (c_karty, cena) VALUES (m.hladajckarty, m.cena)
	USE IN (levx)
	SELECT (kurzor)
	REPLACE cena_prum WITH m.cena, c_mena WITH m.giakt_mena FOR c_karty=m.hladajckarty
ENDIF 
IF !m.bolipolozky
	DO pozavirej WITH "polozky"
ENDIF 
RETURN m.cena



PROCEDURE cenymenaz
PARAMETERS kurzor
PRIVATE m.hladajckarty, m.cena, m.bolipolozky
LOCAL m.uzcena, cmpocet
m.bolipolozky=.t.
hadvypocet = CREATEOBJECT("had", "Výpočet nákupných cien")
hadvypocet.SHOW()
IF !USED("uzprep")
	CREATE CURSOR uzprep (c_karty I, cena Y)
ENDIF 
*SELECT * FROM (kurzor) WHERE zustatek=0 AND !EMPTY(c_druhk) AND !EMPTY(c_stredisk) INTO CURSOR kkurzor
SELECT * FROM (kurzor) WHERE zustatek=0 AND !EMPTY(c_druhk) INTO CURSOR kkurzor
SELECT kkurzor
hadvypocet.set_max(RECCOUNT())
SCAN ALL 
	m.cena=0
	m.hladajckarty=kkurzor.c_karty
	hadvypocet.INCREMENT(ALLTRIM(STR(m.hladajckarty,12,0)))
	SELECT uzprep
	LOCATE FOR c_karty=m.hladajckarty
	IF FOUND()
		SELECT (kurzor)
		REPLACE cena_prum WITH uzprep.cena, c_mena WITH m.giakt_mena FOR c_karty=m.hladajckarty
	ELSE
		IF USED("cenymenaz")
			=REQUERY("cenymenaz")
		ELSE 
			IF m.issql
				rv("polozkycenymenaz","cenymenaz")
			ELSE
				IF !USED("polozky")
					m.bolipolozky=.f.
					USE (m.gccen_path+"polozky") IN 0
				ENDIF 
				USE vpolozkycenymenaz ALIAS cenymenaz IN 0
			ENDIF
		ENDIF  
		SELECT cenymenaz
		IF RECCOUNT()>0
			SCAN ALL 
				m.cmpocet=MTON(cenymenaz.pocet)
				IF m.cmpocet=0
					m.cmpocet=1
				ENDIF 
				SELECT (kurzor)
				LOCATE FOR (c_karty=cenymenaz.c_poloz AND zustatek<>0) OR (c_karty=cenymenaz.c_poloz AND c_druhk=0)
				IF FOUND()
					m.cena=m.cena+NTOM(MTON(cenymenaz.hruba)*MTON(cena_prum)/m.cmpocet)
				ELSE
					SELECT uzprep
					LOCATE FOR c_karty=cenymenaz.c_poloz
					IF FOUND()
						m.cena=m.cena+NTOM(MTON(cenymenaz.hruba)*MTON(uzprep.cena)/m.cmpocet)
					ELSE
						m.cena=m.cena+NTOM(MTON(cenymenaz.hruba)*prep1karty(cenymenaz.c_poloz,1)/m.cmpocet)
					ENDIF 
				ENDIF 
				SELECT cenymenaz
			ENDSCAN 
			SELECT (kurzor)
			REPLACE cena_prum WITH m.cena, c_mena WITH m.giakt_mena FOR c_karty=m.hladajckarty
		ELSE
			SELECT kkurzor
			m.cena=cena_prum
		ENDIF 
		INSERT INTO uzprep (c_karty, cena) VALUES (m.hladajckarty, m.cena)
	ENDIF 
	SELECT kkurzor
ENDSCAN 
IF !m.bolipolozky
	DO pozavirej WITH "polozky"
ENDIF 
RELEASE hadvypocet
DO pozavirej WITH "uzprep, cenymenaz, kkurzor"
SELECT (kurzor)


*!*****************************************************************************
FUNCTION test_hor_con
LOCAL horconnectstring, m.horconnection, m.ret
*DRIVER=SQL Server;SERVER=MILAN-T530\SQLEXPRESS;UID=alto;PWD=klopes;Trusted_Connection=no;APP=Microsoft Visual FoxPro;WSID=MILAN-T530;DATABASE=WhoresA
m.horconnectstring="DRIVER=SQL Server;APP=Microsoft Visual FoxPro;Trusted_Connection=no;DATABASE="+ALLTRIM(m.hor_databaza)+";SERVER="+ALLTRIM(m.hor_server)+IIF(!EMPTY(m.hor_port),";PORT="+ALLTRIM(m.hor_port),"")+";UID="+ALLTRIM(m.hor_user)+";PWD="+m.hor_heslo
IF m.horconnectstring<>DBGETPROP("Hores","CONNECTION","Connectstring")
	DBSETPROP("Hores","CONNECTION","ConnectString",m.horconnectstring)
ENDIF 
m.horconnection=SQLCONNECT("Hores")
IF m.horconnection>0
	=SQLDISCONNECT(m.horconnection)
	m.ret=.t.
ELSE
	m.ret=.f.
ENDIF 
RETURN m.ret


*!*****************************************************************************
PROCEDURE nacitajmojedruhy
PARAMETERS hladajuserid
IF m.issql
	USE (m.sqlprefix+"rugskupuserid") ALIAS ugskup IN 0
ELSE
	USE (m.gchom_path+"ug_skup") IN 0 ALIAS ug_skup
	USE vugskupuserid ALIAS ugskup IN 0
ENDIF 
*SELECT c_druh FROM ugskup INTO CURSOR vidiet WHERE g0
SELECT c_druh FROM ugskup INTO CURSOR uzivat WHERE g1
INDEX on c_druh TAG c_druh
*SELECT c_druh FROM ugskup INTO CURSOR zakladat WHERE g2
DO pozavirej WITH "ugskup, ug_skup"


*!*****************************************************************************
FUNCTION testujpravobuffera
PARAMETERS menobuf
PRIVATE ret
IF VARTYPE(m.prava_na_druhy)=="L" AND m.prava_na_druhy AND !m.glis_admin
	IF m.issql
		USE (m.sqlprefix+"rbufferpravo") ALIAS buff_prav IN 0
	ELSE 
		USE vbufferpravo ALIAS buff_prav IN 0
	ENDIF 
	SELECT c_druh FROM buff_prav WHERE !EMPTY(c_druh) AND !(c_druh in (SELECT c_druh FROM uzivat)) INTO CURSOR chyba
	IF RECCOUNT("chyba")>0
		DO pozavirej WITH "buff_prav, chyba"
		m.ret=.f.
	ELSE 
		USE IN chyba
		SELECT c_druhs FROM buff_prav WHERE !EMPTY(c_druhs) AND !(c_druhs in (SELECT c_druh FROM uzivat)) INTO CURSOR chyba
		IF RECCOUNT("chyba")>0
			DO pozavirej WITH "buff_prav, chyba"
			m.ret=.f.
		ELSE 
			USE IN chyba
			SELECT c_druhk FROM buff_prav WHERE !EMPTY(c_druhk) AND !(c_druhk in (SELECT c_druh FROM uzivat)) INTO CURSOR chyba
			IF RECCOUNT("chyba")>0
				DO pozavirej WITH "buff_prav, chyba"
				m.ret=.f.
			ELSE 
				USE IN chyba
			ENDIF 
		ENDIF 
	ENDIF 
ELSE
	m.ret=.t.
ENDIF 
RETURN m.ret


*!*****************************************************************************
PROCEDURE get_hotlineid
PARAMETERS odpov
IF !EMPTY(m.odpov)
	m.naslosa=AT('cf_855',m.odpov)
	IF m.naslosa>0
		m.odpov=SUBSTR(m.odpov,m.naslosa+6)
		m.naslosa=AT('>',m.odpov)
		IF m.naslosa>0
			m.odpov=SUBSTR(m.odpov,m.naslosa+1)
			m.naslosa=AT('</cf_855>',m.odpov)
			IF m.naslosa>1
				m.hotline_id=STRCONV(LEFT(m.odpov,m.naslosa-1),14)
				IF !EMPTY(m.hotline_id)
					PRIVATE m.hladajmodul, m.hladajvarname, m.hladajsetupskup
					m.hladajmodul="FOOD_SET"
					m.hladajvarname="hotline_id"
					m.hladajsetupskup=""
					DO open_database
					IF m.issql
						rv('defsuborvarname',"food_set")
						=CURSORSETPROP("Buffering",5,"food_set")
						SELECT food_set
						IF RECCOUNT("food_set")=0
							INSERT INTO food_set (var_name, var_value) VALUES (m.hladajvarname,"'"+ALLTRIM(m.hotline_id)+"'")
						ELSE
							GO TOP 
							REPLACE var_value WITH "'"+ALLTRIM(m.hotline_id)+"'" FOR LOWER(ALLTRIM(var_name))=m.hladajvarname
						ENDIF 
						=tux(.t.,.t.,"food_set")
					ELSE
						USE (m.gchom_path + "food_set") IN 0 ALIAS food_set
						SELECT food_set
						LOCATE FOR ALLTRIM(LOWER(var_name))=m.hladajvarname
						IF !FOUND()
							INSERT INTO food_set (var_name, var_value) VALUES (m.hladajvarname,"'"+ALLTRIM(m.hotline_id)+"'")
						ELSE
							REPLACE var_value WITH "'"+ALLTRIM(m.hotline_id)+"'" FOR LOWER(ALLTRIM(var_name))=m.hladajvarname
						ENDIF 
					ENDIF
					DO pozavirej WITH "food_set"
				ELSE
					m.hotline_id=""
				ENDIF 
			ELSE
				m.hotline_id=""
			ENDIF 
		ELSE 
			m.hotline_id=""
		ENDIF 
	ELSE 
		m.hotline_id=""
	ENDIF 
ELSE
	m.hotline_id=""
ENDIF 


*!*****************************************************************************
FUNCTION citajvtiger
PARAMETERS phpskript
PRIVATE m.ret, m.aaa
LOCAL m.lokal_hotelid
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST", "http://www.alto.sk/services/general.json", .F.)
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/x-alto-act-localcom;charset=utf-8;")
IF EMPTY(phpskript)
	m.phpskript="ftinfo100"
*	m.phpskript="atinfo106"
ENDIF 
TRY 
	http.SEND("")
	m.ret = http.responsetext
CATCH
	m.ret=""
ENDTRY 
DO CASE 
	CASE VARTYPE(m.hotel_id)=="N" AND m.hotel_id>0
		m.lokal_hotelid=m.hotel_id
	CASE VARTYPE(m.hotel_id)=="N" AND m.hotel_id<0
		m.lokal_hotelid=ABS(m.hotel_id)
	OTHERWISE 
		m.lokal_hotelid=378856
ENDCASE 
m.aaa= AT('"aza_crm_info"   :',m.ret)
IF m.aaa>0
	m.ret=SUBSTR(m.ret,m.aaa+20)
	m.aaa=AT('"',m.ret)
	IF m.aaa>0
		m.ret=LEFT(m.ret,m.aaa-1)
	ENDIF 
	m.ret=m.ret+"vtigercrm/"+m.phpskript+".php?id="+STRCONV(ALLTRIM(STR(m.lokal_hotelid,10,0)),13)
	http.OPEN("POST", m.ret, .F.)
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/x-alto-act-localcom;charset=utf-8;")
	TRY 
		http.SEND("")
		m.ret = http.responsetext
	CATCH
		m.ret=""
	ENDTRY 
ENDIF 
RELEASE http
RETURN m.ret

*!*****************************************************************************
FUNCTION citajaktualizacie
PRIVATE m.ret, m.aaa
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST", "http://www.alto.sk/services/general.json", .F.)
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/x-alto-act-localcom;charset=utf-8;")
TRY 
	http.SEND("")
	m.ret = http.responsetext
CATCH
	m.ret=""
ENDTRY 
m.aaa= AT('"aza_crm_info"   :',m.ret)
IF m.aaa>0
	m.ret=SUBSTR(m.ret,m.aaa+20)
	m.aaa=AT('"',m.ret)
	IF m.aaa>0
		m.ret=LEFT(m.ret,m.aaa-1)
	ENDIF 
	m.ret=m.ret+"vtigercrm/atinfo106.php?id="+STRCONV(ALLTRIM(STR(m.hotel_id,10,0)),13)
	http.OPEN("POST", m.ret, .F.)
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/x-alto-act-localcom;charset=utf-8;")
	TRY 
		http.SEND("")
		m.ret = http.responsetext
	CATCH
		m.ret=""
	ENDTRY 
ENDIF 
RELEASE http
RETURN m.ret


*!*****************************************************************************
FUNCTION appendujvtiger
PARAMETERS pole, hodnota, maz, mailuj
PRIVATE m.ret, m.aaa
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST", "http://www.alto.sk/services/general.json", .F.)
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/x-alto-act-localcom;charset=utf-8;")
TRY 
	http.SEND("")
	m.ret = http.responsetext
CATCH
	m.ret=""
ENDTRY 
m.aaa= AT('"aza_crm_info"   :',m.ret)
IF m.aaa>0
	m.ret=SUBSTR(m.ret,m.aaa+20)
	m.aaa=AT('"',m.ret)
	IF m.aaa>0
		m.ret=LEFT(m.ret,m.aaa-1)
	ENDIF 
	IF EMPTY(m.maz)
		IF EMPTY(mailuj)
			m.ret=m.ret+"vtigercrm/updatefood.php?id="+STRCONV(ALLTRIM(STR(m.hotel_id,10,0)),13)+"&"+m.pole+"="+m.hodnota
		ELSE 
			m.ret=m.ret+"vtigercrm/updatefood.php?id="+STRCONV(ALLTRIM(STR(m.hotel_id,10,0)),13)+"&"+m.pole+"="+m.hodnota+"&mail="+STRCONV('T',13)
		ENDIF 
	ELSE
		IF EMPTY(mailuj)
			m.ret=m.ret+"vtigercrm/updatefood.php?id="+STRCONV(ALLTRIM(STR(m.hotel_id,10,0)),13)+"&"+m.pole+"="+m.hodnota+"&delete="+STRCONV('T',13)
		ELSE
			m.ret=m.ret+"vtigercrm/updatefood.php?id="+STRCONV(ALLTRIM(STR(m.hotel_id,10,0)),13)+"&"+m.pole+"="+m.hodnota+"&delete="+STRCONV('T',13)+"&mail="+STRCONV('T',13)
		ENDIF 
	ENDIF 
	http.OPEN("POST", m.ret, .F.)
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/x-alto-act-localcom;charset=utf-8;")
	TRY 
		http.SEND("")
		m.ret = http.responsetext
	CATCH
		m.ret=""
	ENDTRY 
ENDIF 
RELEASE http
RETURN m.ret

*!*****************************************************************************
FUNCTION zapisujvtiger
PARAMETERS pole, hodnota
PRIVATE m.ret, m.aaa
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST", "http://www.alto.sk/services/general.json", .F.)
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/x-alto-act-localcom;charset=utf-8;")
TRY 
	http.SEND("")
	m.ret = http.responsetext
CATCH
	m.ret=""
ENDTRY 
m.aaa= AT('"aza_crm_info"   :',m.ret)
IF m.aaa>0
	m.ret=SUBSTR(m.ret,m.aaa+20)
	m.aaa=AT('"',m.ret)
	IF m.aaa>0
		m.ret=LEFT(m.ret,m.aaa-1)
	ENDIF 
	m.ret=m.ret+"vtigercrm/atupdate.php?id="+STRCONV(ALLTRIM(STR(m.hotel_id,10,0)),13)+"&"+m.pole+"="+STRCONV(ALLTRIM(m.hodnota),13)
	http.OPEN("POST", m.ret, .F.)
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/x-alto-act-localcom;charset=utf-8;")
	TRY 
		http.SEND("")
		m.ret = http.responsetext
	CATCH
		m.ret=""
	ENDTRY 
ENDIF 
RELEASE http
RETURN m.ret


*!*****************************************************************************
FUNCTION get_vtiger_pole
PARAMETERS odpov,pole
PRIVATE m.navrat
navrat = ""
IF !EMPTY(m.odpov)
	m.naslosa=AT("<"+pole,m.odpov)
	IF m.naslosa>0
		m.odpov=SUBSTR(m.odpov,m.naslosa+10)
		m.naslosa=AT('>',m.odpov)
		IF m.naslosa>0
			m.odpov=SUBSTR(m.odpov,m.naslosa+1)
			m.naslosa=AT('</'+m.pole+'>',m.odpov)
			IF m.naslosa>1
				navrat = STRCONV(LEFT(m.odpov,m.naslosa-1),14)
			ENDIF
		ENDIF
	ENDIF
ELSE
ENDIF
RETURN m.navrat


*!*****************************************************************************
FUNCTION setucttxt
PARAMETERS uctxx, aaa, bbb
RETURN vrat_nazev_mesice(RIGHT(m.uctxx,2)) + " " + LEFT(m.uctxx,4)


*!*****************************************************************************
*    vrati textove nazev mesice podle parametru, ktery muze byt numeric ci string.
*    nejprve hleda v jazycich, pri neuspechu natvrdo pode slovenskaverzia

FUNCTION vrat_nazev_mesice(mesicek)

DO CASE 
CASE VARTYPE(mesicek) = "C"
    mesicek = LEFT(ALLTRIM(m.mesicek), 2)
CASE VARTYPE(mesicek) = "N"
    mesicek = TRANS(m.mesicek, "@L 99")
OTHERWISE 
    mesicek = "xx"
ENDCASE 

IF BETWEEN(m.mesicek, "01", "12")

    retmesic = jaz("mesic " + m.mesicek)
    IF "mesic " $ m.retmesic
        DO CASE
        CASE  m.mesicek = "01"
            retmesic = IIF( m.slovenskaverzia, "Január", "Leden")
        CASE  m.mesicek = "02"
            retmesic = IIF( m.slovenskaverzia, "Február", "Únor")
        CASE  m.mesicek = "03"
            retmesic = IIF( m.slovenskaverzia, "Marec", "Březen")
        CASE  m.mesicek = "04"
            retmesic = IIF( m.slovenskaverzia, "April", "Duben")
        CASE  m.mesicek = "05"
            retmesic = IIF( m.slovenskaverzia, "Máj", "Květen")
        CASE  m.mesicek = "06"
            retmesic = IIF( m.slovenskaverzia, "Jún", "Červen")
        CASE  m.mesicek = "07"
            retmesic = IIF( m.slovenskaverzia, "Júl", "Červenec")
        CASE  m.mesicek = "08"
            retmesic = IIF( m.slovenskaverzia, "August", "Srpen")
        CASE  m.mesicek = "09"
            retmesic = IIF( m.slovenskaverzia, "September", "Září")
        CASE  m.mesicek = "10"
            retmesic = IIF( m.slovenskaverzia, "Október", "Říjen")
        CASE  m.mesicek = "11"
            retmesic = IIF( m.slovenskaverzia, "November", "Listopad")
        CASE  m.mesicek = "12"
            retmesic = IIF( m.slovenskaverzia, "December", "Prosinec")
        ENDCASE
    ENDIF 
ELSE 
    retmesic = "mesic XX"
ENDIF 
RETURN m.retmesic 


*!*****************************************************************************
FUNCTION kontroladatumudokladuprioprave
PARAMETERS n_datum, o_datum, hladajcstredisk, hladajcdokl, hlavicka
PRIVATE m.hladajmesiac, m.hladajrok, navrat, m.chyba, bolauzaverka
m.chyba=.f.
m.bolauzaverka=.t.
IF m.slovenskaverzia
	IF EMPTY(m.n_datum)
		=MESSAGEBOX(jaz("frm_datum_nemoze_byt_prazdny"),16,m.hlavicka,5000)
		m.navrat=m.o_datum
	ELSE 
		IF LEFT(DTOS(m.n_datum),6)<>LEFT(DTOS(m.o_datum),6)
			m.hladajmesiac=SUBSTR(DTOS(m.o_datum),5,2)
			m.hladajrok=LEFT(DTOS(m.o_datum),4)
			IF m.issql
				USE (m.sqlprefix+"ruzavstredcdokldo") IN 0 ALIAS uu
			ELSE 
				IF !USED("uzaverka")
					USE (m.fn_uzav) IN 0 ALIAS uzaverka
					m.bolauzaverka=.f.
				ENDIF 
				USE vuzavstredcdokldo IN 0 ALIAS uu
			ENDIF 
			SELECT uu
			LOCATE FOR mesiac=m.hladajmesiac AND rok=m.hladajrok
			IF FOUND()
				**** Povodny mesiac dokladu je uz uzavrety
				IF uu.c_dokldo>m.hladajcdokl
					**** C_dokl opravovaneho dokladu patri do uzavierky povodneho dokladu - nemozem ho zmenit
					m.chyba=.t.
				ENDIF 
			ELSE 
				LOCATE FOR c_dokldo>=m.hladajcdokl
				IF FOUND()
					**** Nasla sa uzavierka, do ktorej opravovany doklad patri
					IF (uu.rok+uu.mesiac)<>(m.hladajrok+m.hladajmesiac)
						**** Uzavierka, do ktorej doklad patri je za starsi mesiac ako mesiac opravovaneho dokladu
						IF (uu.rok+uu.mesiac)<>(LEFT(DTOS(m.n_datum),4)+SUBSTR(DTOS(m.n_datum),5,2))
							**** Novy datum dokladu nepatri do uzavierky, v ktorej bol doklad uzavrety
							m.chyba=.t.
						ENDIF 
					ENDIF 
				ENDIF 
			ENDIF 
			IF m.chyba
				=MESSAGEBOX(jaz("form_nemoz_zmenit_uct_obd_dokl"),16,m.hlavicka,5000)
				m.navrat=m.o_datum
			ELSE
				m.navrat=m.n_datum
			ENDIF 
			DO pozavirej WITH "uu"
			IF !m.bolauzaverka
				DO pozavirej WITH "uzaverka"
			ENDIF 
		ELSE
			m.navrat=m.n_datum
		ENDIF 
	ENDIF 
ELSE
	m.navrat=m.n_datum
ENDIF
RETURN m.navrat


*!*****************************************************************************
FUNCTION poslimaildoalta
PARAMETERS subjekt, telo, priloha1,priloha2,priloha3, kamvlastneposlat
IF EMPTY(m.telo)
	telo=m.subjekt
ENDIF 
IF EMPTY(m.kamvlastneposlat)
	kamvlastneposlat="errors@alto.sk"
ENDIF 

IF ATC("BUDVAR", m.wi_zakaz) > 0    && pro zakazky, kde firewall nedovoluje odeslat mail
    RETURN 
ENDIF 

TRY

  LOCAL loConfig AS CDO.Configuration, loFlds AS Object, loMsg AS CDO.Message
  loConfig = CREATEOBJECT("CDO.Configuration")
  loFlds = loConfig.Fields
  IF m.slovenskaverzia
	WITH loFlds
		*- Set the CDOSYS configuration fields to use port 25 on the SMTP server.
		.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
		*- Enter name or IP address of remote SMTP server.
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.hostcreators.sk"
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
		*- Assign timeout in seconds
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = .t.
		*- Commit changes to the object
		.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "errors@alto.sk"
		.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "AlEr*2022"
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1           
		.Update()
	ENDWITH
	*- Create and send the message.
	loMsg = CREATEOBJECT("CDO.Message")

    TRY
      WITH loMsg
		.Configuration = loConfig
		.To = m.kamvlastneposlat
		.From = m.kamvlastneposlat
		.Subject = m.subjekt
		.HTMLBody = m.telo
		IF !EMPTY(m.priloha1) AND FILE(m.priloha1)
			.AddAttachment(m.priloha1)
		ENDIF 
		IF !EMPTY(m.priloha2) AND FILE(m.priloha2)
			.AddAttachment(m.priloha2)
		ENDIF 
		IF !EMPTY(m.priloha3) AND FILE(m.priloha3)
			.AddAttachment(m.priloha3)
		ENDIF 
		.Send()
      ENDWITH 
    CATCH TO oerr
        MESSAGEBOX(oerr.message)
    ENDTRY
  ELSE
	WITH loFlds
		*- Set the CDOSYS configuration fields to use port 25 on the SMTP server.
		.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
		*- Enter name or IP address of remote SMTP server.
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.websupport.cz"
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
		*- Assign timeout in seconds
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = .f.
		*- Commit changes to the object
		.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "errors@altopraha.cz"
		.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "1ERRORerror."
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1           
		.Update()
	ENDWITH
	*- Create and send the message.
	loMsg = CREATEOBJECT("CDO.Message")
    TRY
      WITH loMsg
		.Configuration = loConfig
		.To = m.kamvlastneposlat
		IF m.kamvlastneposlat="errors@alto.sk"		
			IF "Licencia pre"$m.subjekt
				.cc="licence@altopraha.cz"
			ELSE
				.cc="evzen@altopraha.cz"
			ENDIF 
		ENDIF 
		.From = "errors@altopraha.cz"
		.Subject = "Food600 error: " + m.subjekt
		.HTMLBody = m.telo
		IF !EMPTY(m.priloha1) AND FILE(m.priloha1)
			.AddAttachment(m.priloha1)
		ENDIF 
		IF !EMPTY(m.priloha2) AND FILE(m.priloha2)
			.AddAttachment(m.priloha2)
		ENDIF 
		IF !EMPTY(m.priloha3) AND FILE(m.priloha3)
			.AddAttachment(m.priloha3)
		ENDIF 
		.Send()
	ENDWITH 
    CATCH TO oerr
        DO app_log WITH oerr.message
    ENDTRY
  ENDIF

CATCH TO ooeerr
  DO app_log WITH ooeerr.message
ENDTRY



*!*****************************************************************************
FUNCTION update_vtiger
PARAMETERS ip_id,ip_login, ip_heslo,ip_params
LOCAL m.vysledok, m.ret, txtjson
lvs_params = '"params":'
lvs_json = '{ '
txtjson  = ''
lvs_json = lvs_json + '"id":"' + STRCONV(STRCONV(ALLTRIM(ip_id),9), 13) + '",'
txtjson  = txtjson   + "<b>ID:    </b>" + ALLTRIM(ip_id)+"<br>"
lvs_json = lvs_json + '"login":"' + STRCONV(STRCONV(ALLTRIM(ip_login),9), 13) + '",'
txtjson  = txtjson + + "<b>Login: </b>" + ALLTRIM(ip_login)+"<br>"
lvs_json = lvs_json + '"heslo":"' + STRCONV(STRCONV(ALLTRIM(ip_heslo),9), 13) + '",'
txtjson  = txtjson + + "<b>Heslo: </b>" + ALLTRIM(ip_heslo)+"<br>"
lvs_json = lvs_json + '"params":{'
txtjson  = txtjson   + "<br>"+ "<b>Parametre:</b>" + "<br>"
FOR li = 1 TO ALEN(ip_params,1)
	IF !EMPTY(ip_params[li,1])&& AND !EMPTY(ip_params[li,2])
		lvs_json = lvs_json + '"' + ip_params[li,1] + '":"' + STRCONV(STRCONV(ALLTRIM(ip_params[li,2]),9), 13) + '",'
		txtjson  = txtjson + "<b>"+ip_params[li,1] + ": </b>" + ALLTRIM(ip_params[li,2])+"<br>"
	ENDIF 
ENDFOR 

lvs_json = LEFT(lvs_json,LEN(lvs_json)-1)
lvs_json = lvs_json + '} }'
op_hod = updatejsonvtiger(lvs_json)
m.ret=0
m.pokec=""
IF AT('"error_code":',m.op_hod)>0
	m.ret=""
	FOR m.i=AT('"error_code":',m.op_hod)+13 TO 1000
		IF INLIST(SUBSTR(m.op_hod,m.i,1),",","}")
			m.ret=VAL(m.ret)
			EXIT
		ELSE 
			m.ret=m.ret+SUBSTR(m.op_hod,m.i,1)
		ENDIF 
	ENDFOR 
	IF VARTYPE(m.ret)<>"N"
		TRY 
			m.ret=VAL(m.ret)
		CATCH
			m.ret=100
		ENDTRY 
	ENDIF 
	IF AT('"error_text":"',m.op_hod)>0
		m.pokec=""
		FOR m.i=AT('"error_text":"',m.op_hod)+14 TO 1000
			IF INLIST(SUBSTR(m.op_hod,m.i,1),'"',"}")
				EXIT
			ELSE 
				TRY 
					m.pokec=m.pokec+SUBSTR(m.op_hod,m.i,1)
				CATCH
				ENDTRY 
			ENDIF 
		ENDFOR 
	ELSE
		m.ret=100
		m.pokec="Neznáma chyba"
	ENDIF 		
ELSE
	m.ret=100
	m.pokec="Neznáma chyba"
ENDIF 		

IF m.ret<>0
	DO CASE 
		CASE m.ret=30
*			=MESSAGEBOX("Neautorizovaný zásah do zoznamu stredísk."+CHR(10)+"Vraciam pôvodný zoznam stredísk",64,"FoodMan")
			DO poslimaildoalta WITH "Neautorizovaný zásah do FOOD_DAT :"+m.hotel_name+" ID:"+ALLTRIM(m.ip_id), m.txtjson 
		OTHERWISE 
			IF m.issql
			ELSE
				IF !FILE(m.gchom_path+"vtiger_comm.dbf")
					CREATE TABLE (m.gchom_path+"vtiger_comm.dbf") FREE (meno c(50), heslo c(50), cf c(50), prikaz M, datum T)
					DO pozavirej WITH "vtiger_comm"
				ENDIF 
				USE (m.gchom_path+"vtiger_comm.dbf") IN 0
				FOR li = 1 TO ALEN(ip_params,1)
					IF !EMPTY(ip_params[li,1]) AND !EMPTY(ip_params[li,2])
						INSERT INTO vtiger_comm (meno, heslo, cf, prikaz,datum) VALUES (STRCONV(ip_login,13), STRCONV(ip_heslo,13), ip_params[li,1], ip_params[li,2], DATETIME())
					ENDIF 
				ENDFOR 
				USE IN vtiger_comm
			ENDIF
	ENDCASE 
ENDIF 

RETURN m.ret


*!*****************************************************************************
FUNCTION updatejsonvtiger
PARAMETERS ip_json
PRIVATE m.ret, m.aaa
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST", "http://www.alto.sk/services/general.json", .F.)
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
TRY 
	http.SEND("")
	m.ret = http.responsetext
CATCH
	m.ret=""
ENDTRY 
m.aaa= AT('"aza_crm_info"   :',m.ret)
IF m.aaa>0
	m.ret=SUBSTR(m.ret,m.aaa+20)
	m.aaa=AT('"',m.ret)
	IF m.aaa>0
		m.ret=LEFT(m.ret,m.aaa-1)
	ENDIF 
	m.ret=m.ret+"vtigercrm/cf_update.php"
	http.OPEN("POST", m.ret, .F.)
	http.settimeouts(0, 0, 0, 0)
	http.setrequestheader("content-type", "application/json;charset=utf-8;")
	TRY 
		http.SEND(ALLTRIM(ip_json))
		m.ret = http.responsetext
	CATCH
		m.ret=""
	ENDTRY 
ENDIF 
RELEASE http
RETURN m.ret


*!*****************************************************************************
FUNCTION vytvorfooddatstr
PARAMETERS menotabulky
LOCAL strtxt, oldsel
m.oldsel=SELECT()
SELECT * FROM (menotabulky) INTO CURSOR vytvorfooddatstr ORDER BY id
m.strtxt=""
SCAN ALL 
	m.strtxt=m.strtxt+"meno='"+ALLTRIM(id_zkratka)+"';"
	m.strtxt=m.strtxt+"id='"+id+"';"
	m.strtxt=m.strtxt+"c_stredisk="+ALLTRIM(STR(c_stredisk))+";"
	m.strtxt=m.strtxt+"pgm='"+ALLTRIM(pgm)+"';"
	m.strtxt=m.strtxt+"param='"+ALLTRIM(pgm_param)+"';"
	m.strtxt=m.strtxt+"p_p='"+ALLTRIM(pgm_path)+"';"
	m.strtxt=m.strtxt+"d_p='"+ALLTRIM(dat_path)+"';"
	m.strtxt=m.strtxt+"c_p='"+ALLTRIM(cen_path)+"';"
	m.strtxt=m.strtxt+"b_s='"+ALLTRIM(bour_skup)+"';"
	m.strtxt=m.strtxt+"s_s='"+ALLTRIM(setup_skup)+"';"
	m.strtxt=m.strtxt+"p_s='"+ALLTRIM(prava_skup)+"';"
	m.strtxt=m.strtxt+"id_klapky='"+ALLTRIM(id_klapky)+"';"
	m.strtxt=m.strtxt+"master_kas='"+ALLTRIM(master_kas)+"';"
	m.strtxt=m.strtxt+"typ_inst="+ALLTRIM(STR(typ_inst))+";"
	IF m.issql
		m.strtxt=m.strtxt+"c_d="+ALLTRIM(STR(c_data))+";"
	ELSE 
		m.strtxt=m.strtxt+"c_d=1;"
	ENDIF 
	IF FSIZE("plateny")>0
		m.strtxt=m.strtxt+"plateny="+ALLTRIM(STR(plateny))+";"
	ENDIF 
	IF FSIZE("servis")>0
		m.strtxt=m.strtxt+"servis="+ALLTRIM(STR(servis))+";"
	ENDIF 
	IF FSIZE("datinst")>0
		m.strtxt=m.strtxt+"datum="+DTOS(datinst)+";"
	ENDIF 
	m.strtxt=m.strtxt+CHR(13)+CHR(10)
ENDSCAN 
USE IN vytvorfooddatstr
SELECT (m.oldsel)
RETURN m.strtxt

*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION hextod
PARAMETERS p_hexcis
m.deccrd = 0
m.digdec = 0
FOR k=1 TO LEN(m.p_hexcis)
  m.dig = SUBSTR(m.p_hexcis,k,1)
  DO case
  CASE INLIST(m.dig,'0','1','2','3','4','5','6','7','8','9')
    m.digdec = VAL(m.dig)
  CASE UPPER(m.dig) = 'A'
    m.digdec = 10
  CASE UPPER(m.dig) = 'B'
    m.digdec = 11
  CASE UPPER(m.dig) = 'C'
    m.digdec = 12
  CASE UPPER(m.dig) = 'D'
    m.digdec = 13
  CASE UPPER(m.dig) = 'E'
    m.digdec = 14
  CASE UPPER(m.dig) = 'F'
    m.digdec = 15
  ENDCASE 
  m.deccrd = m.deccrd + m.digdec*16**(LEN(m.p_hexcis)-k)
ENDFOR 
RETURN m.deccrd


*!*****************************************************************************
FUNCTION dtohex
parameter p_cislo
privat p_hex,p_zn
m.p_hex = ""
m.pom = m.p_cislo
do case
case m.p_cislo = 0
  m.p_hex = "00"
case between(m.p_cislo, 0, 9)
  m.p_hex = '0'+str(m.p_cislo,1)  
case between(m.p_cislo, 10, 15)
  m.p_hex = '0'+chr(m.p_cislo+55)
otherwise
 do while .T.
  m.n_cislo = round((pom/16 - int(m.pom/16)) * 16,0)
  do case
  case between(n_cislo, 0, 9)
    m.p_zn = str(m.n_cislo, 1)
  case m.n_cislo = 10
    m.p_zn = 'A'
  case m.n_cislo = 11
    m.p_zn = 'B'
  case m.n_cislo = 12
    m.p_zn = 'C'
  case m.n_cislo = 13
    m.p_zn = 'D'
  case m.n_cislo = 14
    m.p_zn = 'E'
  case m.n_cislo = 15
    m.p_zn = 'F'
  endcase
  m.p_hex = m.p_zn + m.p_hex
  m.pom = int(m.pom/16)
  if m.pom = 0
    exit
  endif
 enddo
ENDCASE
DO WHILE LEN(p_hex)>1
	IF LEFT(p_hex,1)="0"
		m.p_hex=SUBSTR(m.p_hex,2)
	ELSE
		EXIT
	ENDIF 
ENDDO 
return p_hex


*!*****************************************************************************
FUNCTION getrgb
PARAMETERS colour
LOCAL m.x
m.x=PADL(dtohex(colour),6,"0")
RETURN "#"+RIGHT(m.x,2)+SUBSTR(m.x,3,2)+LEFT(m.x,2)

FUNCTION getcolozrgb
PARAMETERS rgbfarba
IF LEFT(m.rgbfarba,1)="#"
	m.rgbfarba=SUBSTR(m.rgbfarba,2)
ENDIF 
RETURN hextod(RIGHT(rgbfarba,2)+SUBSTR(rgbfarba,3,2)+LEFT(rgbfarba,2))


*!*****************************************************************************
FUNCTION testobjkod
PARAMETERS ico, kod, m.hladajckarty, edimankod
PRIVATE m.hladajean, m.c_karty, hladajdalej
m.c_karty=0
m.hladajdalej=.f.
IF m.hotel_ico="54145384" && MAX FOOD SE - Cortelazzi - maju naimportovane ich kod_zak do nasho c_karty
	IF USED("vhladajtexty")
		=REQUERY("vhladajtexty")
	ELSE
		USE (m.sqlprefix+"rktexty") ALIAS vhladajtexty IN 0
		=CURSORSETPROP("Buffering",5,"vhladajtexty")
	ENDIF 
	IF RECCOUNT("vhladajtexty")=1
		SELECT vhladajtexty
		GO TOP 
		IF !EMPTY(meno1)
			m.popis=vhladajtexty.meno1
		ELSE
			m.hladajdalej=.t.
		ENDIF 
	ELSE
		m.hladajdalej=.t.
	ENDIF 
	DO pozavirej WITH "vhladajtexty"
	IF m.hladajdalej
		USE (m.sqlprefix+"r1karta") ALIAS vhladajtexty IN 0
		=CURSORSETPROP("Buffering",5,"vhladajtexty")
		SELECT vhladajtexty
		GO TOP 
		m.popis=vhladajtexty.nazev
	ENDIF 
	DO pozavirej WITH "vhladajtexty"
	m.c_karty=m.hladajckarty
ELSE  
	IF ATC("MAKRO", m.wi_zakaz) > 0 OR m.edimankod
		m.hladajean=ALLTRIM(m.ico)+"::"+ALLTRIM(m.kod)
	ELSE
		IF LEN(ALLTRIM(m.kod))>7
			m.hladajean=ALLTRIM(m.kod)
		ELSE 
			m.hladajean=ALLTRIM(m.ico)+"::"+ALLTRIM(m.kod)
		ENDIF 
	ENDIF 
	IF USED("vhladajean")
		=REQUERY("vhladajean")
	ELSE
		USE (m.sqlprefix+"robjednavanie_pridajean") ALIAS vhladajean IN 0
		=CURSORSETPROP("Buffering",5,"vhladajean")
	ENDIF 
	SELECT vhladajean
	IF RECCOUNT("vhladajean")>0
		GO TOP 
		m.popis=vhladajean.popis
		m.c_karty=vhladajean.c_karty
		m.koef=vhladajean.koef
		m.objem=vhladajean.objem
		IF !EMPTY(m.hladajckarty) AND m.c_karty<>m.hladajckarty
			REPLACE c_karty WITH m.hladajckarty
			=tux(.t.,.t.,"vhladajean")
			=REQUERY("vhladajean")
		ENDIF 
	ENDIF 
	DO pozavirej WITH "vhladajean"
ENDIF 
RETURN !EMPTY(m.c_karty)



*!*****************************************************************************
FUNCTION nastavzmenuprefoodie
IF m.issql
	m.vysledok=sql_exec("SELECT food600.f_zmeny_auto_cas('foodie',"+ ALLTRIM(STR(m.cdata,2,0))+")")
ENDIF 
RETURN .t.


*!*****************************************************************************
FUNCTION koniecpohybovehoformu
PARAMETERS dalsidoklad
IF EMPTY(m.dalsidoklad)
	= n_sunlock("POHYBY_" + m.gcpgm_id + "_" + m.machine_semaf)
	IF TYPE("tbscreen")="O" AND TYPE("_screen.infobox")="O" AND tbscreen.timer1.enabled
		tbscreen.timer1.interval=1000
	ENDIF 

    prog_chain = progchain()
    DO zurnal_log WITH "OPERA", "koniecpohybovehoformu " + prog_chain, "KON_INV " + m.machine_semaf, m.gic_stredisk
ENDIF 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION zapisheslofoodie
PARAMETERS hladajidriadok, heslo, heslo2, heslo3
PRIVATE oldsel
m.oldsel=SELECT()
SELECT 0
TRY 
	USE (m.sqlprefix+"rusers_foodie") ALIAS foodie
	=CURSORSETPROP("Buffering",5,"foodie")
	IF !EMPTY(m.heslo)
		REPLACE hashfoodie WITH hashfoodie(ALLTRIM(m.heslo))
	ENDIF 
	IF !EMPTY(m.heslo2)
		REPLACE hashfoodie2 WITH hashfoodie(ALLTRIM(m.heslo2))
	ENDIF 
	IF !EMPTY(m.heslo3)
		REPLACE hashfoodie3 WITH hashfoodie(ALLTRIM(m.heslo3))
	ENDIF 
	=tux(.t.,.t.,"foodie")
	USE IN foodie 
CATCH 
ENDTRY 
SELECT (m.oldsel)
RETURN .t.


*!*****************************************************************************
FUNCTION vytvorfoodiesetup
PARAMETERS odkial
PRIVATE m.foodiesetup, m.oldsel
m.oldsel=SELECT()
m.foodiesetup=""
SELECT * FROM (odkial) INTO CURSOR vytvorfooddatstr ORDER BY id
SCAN FOR pgm="KAS"
	m.foodiesetup=m.foodiesetup+'ID:'+id+',Pocet:'+ALLTRIM(STR(priority,5,0))+',Meno:'+ALLTRIM(id_zkratka)+CHR(13)+CHR(10)
ENDSCAN 
USE IN vytvorfooddatstr
SELECT (m.oldsel)
RETURN m.foodiesetup


*!*****************************************************************************
FUNCTION vytvorkratkysetupfoodie
PARAMETERS dlhy
LOCAL m.riadok, m.xxx, m.meno, m.id, m.pocet, m.kratky, m.oldsel
m.oldsel=SELECT()
m.kratky=""
IF !EMPTY(m.dlhy)
	CREATE cursor importovane (riadok c(200), id c(2), priority I, meno c(30))
	DO WHILE LEN(m.dlhy)>0
		m.xxx=AT(CHR(10),m.dlhy)
		IF m.xxx>0
			m.riadok=LEFT(m.dlhy,m.xxx)
			m.dlhy=SUBSTR(m.dlhy,m.xxx+1)
			INSERT INTO importovane(riadok) VALUES (m.riadok)
		ELSE
			INSERT INTO importovane(riadok) VALUES (m.dlhy)
			m.dlhy=""
		ENDIF 
	ENDDO 
	REPLACE ALL riadok WITH STRTRAN(riadok,CHR(13),"")
	REPLACE ALL riadok WITH STRTRAN(riadok,CHR(10),"")
	SCAN ALL 
		IF LEFT(importovane.riadok,3)="ID:"
			m.pozpocet=AT(",Pocet:",importovane.riadok)
			m.pozmeno=AT(",Meno:",importovane.riadok)
			m.id=STRCONV(ALLTRIM(SUBSTR(importovane.riadok,4,m.pozpocet-4)),11)
			m.pocet=STRCONV(ALLTRIM(SUBSTR(importovane.riadok,m.pozpocet+7,m.pozmeno-m.pozpocet-7)),11)
			m.meno=STRCONV(ALLTRIM(SUBSTR(importovane.riadok,m.pozmeno+6)),11)
			REPLACE id WITH m.id, priority WITH VAL(m.pocet), meno WITH m.meno
		ENDIF 
	ENDSCAN 
	SELECT importovane
	SCAN ALL 
		m.kratky=m.kratky+"ID:"+importovane.id+",Pocet:"+ALLTRIM(STR(importovane.priority,10,0))+CHR(13)+CHR(10)
	ENDSCAN 
	USE IN importovane
ENDIF 		
SELECT (m.oldsel)
RETURN m.kratky


*!*****************************************************************************
PROCEDURE publikuj
LPARAMETERS jmeno_promenne, default_value
LOCAL nastavit_hodnotu
nastavit_hodnotu = (PARAMETERS() > 1)
IF TYPE(jmeno_promenne) = "U" 
    PUBLIC (jmeno_promenne)
ENDIF 
IF m.nastavit_hodnotu 
    &jmeno_promenne = m.default_value
ENDIF 


*!*****************************************************************************
*!       Function: OBL_NUM6()
*!*****************************************************************************
FUNCTION obl_num6
PARAMETERS datum
* vrati pismenko nazvu databaze pro dane datum
PRIVATE old_sel, err_h_old, datum, ok
old_sel = SELE()
IF PARAMETERS() = 0
	m.datum = DATE()
ENDIF
SELECT 0
CREATE CURSOR oblasti (datum D, oblast C(1))
INSERT INTO oblasti (datum, oblast) VALUES (m.oblast_a, 'a')
INSERT INTO oblasti (datum, oblast) VALUES (m.oblast_b, 'b')
INSERT INTO oblasti (datum, oblast) VALUES (m.oblast_c, 'c')
INDEX ON datum TAG datum 
m.rt="x"
SCAN 
	IF m.datum >= oblasti.datum
		m.rt = oblasti.oblast
	ENDIF 
ENDSCAN 
USE 
SELECT (m.old_sel)
IF m.rt="x"
	=MESSAGEBOX(jaz("obl_num6_err"),64,"FoodMan")
	m.rt="a"
ENDIF 
RETURN rt


*!*****************************************************************************
PROCEDURE nastavpopisypohybov
PARAMETERS tabulka
PRIVATE m.hladajpz2
LOCAL popis, bolsksez, bollimithl, bolnlimithl, prikaz, oldsel
m.bolsksez=.t.
m.bollimithl=.t.
m.bolnlimithl=.t.
m.oldsel=SELECT()
SELECT &tabulka
IF FSIZE('operace') <> 0 AND FSIZE('pz2') <> 0 AND FSIZE('popis') <> 0
	IF m.issql
		USE (m.sqlprefix+"rpz2_popisy_all") IN 0 ALIAS pzx
		UPDATE &tabulka SET popis=pzx.popis from pzx WHERE &tabulka..pz2=pzx.pz2 AND &tabulka..operace="VK" AND !ISNULL(pzx.popis)
		USE IN pzx
	ELSE
		IF !USED("sk_sez")
			TRY 
				USE (m.fn_sk_sez) IN 0 ALIAS sk_sez
				m.bolsksez=.f.
			CATCH 
			ENDTRY 
		ENDIF 
		IF !USED("limit_hl")
			TRY 
				USE (m.gccen_path+"limit_hl") IN 0 ALIAS limit_hl
				m.bollimithl=.f.
			CATCH 
			ENDTRY 
		ENDIF 
		IF !USED("nlimit_hl") AND FILE(m.gccen_path+"nlimit_hl.dbf")
			TRY 
				USE (m.gccen_path+"nlimit_hl") IN 0 ALIAS nlimit_hl
				m.bolnlimithl=.f.
			CATCH
			ENDTRY 
		ENDIF 
		IF USED("sk_sez")
			IF m.slovenskaverzia
				m.prikaz="UPDATE "+m.tabulka+" SET popis=sk_sez.lastuzav from sk_sez WHERE "+m.tabulka+".pz2=SUBSTR(sk_sez.uzaverka,3,12)+'/'+sk_sez.id_kas AND "+m.tabulka+".operace='VK'"
			ELSE
				m.prikaz="UPDATE "+m.tabulka+" SET popis=sk_sez.lastuzav from sk_sez WHERE "+m.tabulka+".pz2=sk_sez.uzaverka+'/'+sk_sez.id_kas AND "+m.tabulka+".operace='VK'"
			ENDIF 			
			&prikaz
			m.prikaz="UPDATE "+m.tabulka+" SET popis='' WHERE SUBSTR(popis,2,1)<>'_'"
			&prikaz
			IF USED("limit_hl")
				m.prikaz="UPDATE "+m.tabulka+" SET popis=limit_hl.limit from limit_hl WHERE LEFT("+m.tabulka+".popis,2)='L_' AND VAL(SUBSTR("+m.tabulka+".popis,3))=limit_hl.id_limit"
				&prikaz
			ENDIF 
			IF USED("nlimit_hl")
				m.prikaz="UPDATE "+m.tabulka+" SET popis=nlimit_hl.menolimit from nlimit_hl WHERE LEFT("+m.tabulka+".popis,2)='L_' AND VAL(SUBSTR("+m.tabulka+".popis,3))=nlimit_hl.id_limit"
				&prikaz
			ENDIF 
		ENDIF 
		SELECT &tabulka
	ENDIF 
ENDIF 
IF !m.bolsksez
	DO pozavirej WITH "sk_sez"
ENDIF 
IF !m.bollimithl
	DO pozavirej WITH "limit_hl"
ENDIF 
IF !m.bolnlimithl
	DO pozavirej WITH "nlimit_hl"
ENDIF 
DO pozavirej WITH "pzs,pzx"
SELECT (m.oldsel)

*!*****************************************************************************
function detxt
param txt
delka = len(m.txt)
detxt = ""
for iii = 1 to m.delka step 2
  cislo1 = asc(subst(m.txt, m.iii  , 1)) - 97 - int(m.iii/2 + .51) % 5
  cislo2 = asc(subst(m.txt, m.iii+1, 1)) - 97 - int(m.iii/2 + .51) % 4
  detxt = m.detxt + chr(cislo2*8 + cislo1)
endfor
return allt(m.detxt)

*!*****************************************************************************
function txt
param untxt
delka = len(m.untxt)
txt = ""
for iii = 1 to m.delka
  pismenko = asc(subst(m.untxt, m.iii, 1))
  txt = m.txt + ;
        chr(97 + bitand(m.pismenko, 7)    + m.iii % 5) + ;
        chr(97 + bitrshift(m.pismenko, 3) + m.iii % 4)
endfor
return m.txt


*!*****************************************************************************
function hladiny(cisilko, pismenko)

jednotky = MOD(cisilko, 10)
desitky  = INT(cisilko/10)
RETURN CHR(ASC(m.pismenko) + m.desitky) + STR(m.jednotky, 1)


*!*****************************************************************************
FUNCTION cislohladiny(cisilko)
IF cisilko<41
	RETURN PADL(ALLTRIM(STR(m.cisilko,2,0)),2,"0")
ELSE
	jednotky = MOD(m.cisilko-41,10)
	desitky  = INT((cisilko-41)/10)
	RETURN CHR(ASC("A")+m.desitky)+STR(m.jednotky,1)
ENDIF 

*!*****************************************************************************
FUNCTION kontrolahladiny
PARAMETERS m.hladina
DO CASE 
	CASE LEFT(m.hladina,1)="0" AND BETWEEN(RIGHT(m.hladina,1),"1","9")
		RETURN VAL(m.hladina)
	CASE BETWEEN(LEFT(m.hladina,1),"1","3") AND BETWEEN(RIGHT(m.hladina,1),"0","9")
		RETURN VAL(m.hladina)
	CASE m.hladina="40"
		RETURN VAL(m.hladina)
	CASE BETWEEN(LEFT(m.hladina,1),"A","L") AND BETWEEN(RIGHT(m.hladina,1),"0","9")
		RETURN 41+10*(ASC(LEFT(m.hladina,1))-ASC("A"))+VAL(RIGHT(m.hladina,1))
	OTHERWISE 
		RETURN 0
ENDCASE 


*!*****************************************************************************
PROCEDURE zoraddruhy
PARAMETERS menosuboru
PRIVATE hladajdruh, hladajdruh, idriadokrodic, idriadokslave, poradie, m.i, m.si, m.por1, m.por2, m.pocet, m.pocetcelkom, m.idriadok1, poradierodica, maxuroven
m.pocet=0
m.pocetcelkom=RECCOUNT(m.menosuboru)
m.ixx=1
m.si="1"
m.por1="a"
m.por2="a"
SELECT c_druh, druh FROM (m.menosuboru) WHERE EMPTY(c_druh_2) ORDER BY druh INTO CURSOR uroven
SCAN ALL 
	IF m.pocet>m.pocetcelkom
		EXIT
	ENDIF 
	m.hladajidriadok=uroven.c_druh
	m.hladajdruh=uroven.druh
	UPDATE (m.menosuboru) SET poradie=ALLTRIM(poradie)+m.por1+m.por2, druh_meno = REPLICATE("    ",m.ixx-1)+ALLTRIM(druh), uroven = m.ixx WHERE druh=m.hladajdruh
	IF m.por2="z"
		m.por1=CHR(ASC(m.por1)+1)
		m.por2="a"
	ELSE
		m.por2=CHR(ASC(m.por2)+1)
	ENDIF 
	m.pocet=m.pocet+1
ENDSCAN 
USE IN uroven
DO WHILE m.pocet < m.pocetcelkom
	SELECT c_druh, druh, poradie FROM (m.menosuboru) INTO CURSOR uroven0 WHERE uroven = m.ixx AND c_druh > 0
	SCAN ALL
		m.por1="a"
		m.por2="a"
		m.idriadok1=c_druh
		m.poradierodica=ALLTRIM(poradie)
		IF LEFT(druh,2)="* "
			SELECT c_druh, druh FROM (m.menosuboru) WHERE c_druh_2=m.idriadok1 AND LEFT(druh,2) = "* " ORDER BY druh INTO CURSOR uroven
		ELSE
			SELECT c_druh, druh FROM (m.menosuboru) WHERE c_druh_2=m.idriadok1 AND LEFT(druh,2) # "* " ORDER BY druh INTO CURSOR uroven
		ENDIF 
		SCAN ALL 
			IF m.pocet>m.pocetcelkom
				EXIT
			ENDIF 
			m.hladajidriadok=uroven.c_druh
			m.hladajdruh=uroven.druh
			SELECT (m.menosuboru)
			UPDATE (m.menosuboru) SET poradie=m.poradierodica+m.por1+m.por2, druh_meno=REPLICATE("----",m.ixx)+ALLTRIM(druh), uroven=m.ixx+1 where druh=m.hladajdruh
			IF m.por2="z"
				m.por1=CHR(ASC(m.por1)+1)
				m.por2="a"
			ELSE
				m.por2=CHR(ASC(m.por2)+1)
			ENDIF 
			m.pocet=m.pocet+1
		ENDSCAN 
		USE IN uroven
	ENDSCAN 
	USE IN uroven0
	ixx = m.ixx + 1
    IF m.ixx > 444
        WAIT jaz("form_chybny_strom_druhov") WINDOW TIMEOUT 3 AT 22,22
        EXIT 
    ENDIF 
ENDDO 
SELECT (m.menosuboru)
SELECT DISTINCT c_druh_2 FROM (m.menosuboru) INTO CURSOR aaa WHERE !EMPTY(c_druh_2)
UPDATE (m.menosuboru) SET rozbal=m.priznak_zbalene WHERE c_druh in (select c_druh_2 from aaa)
DO pozavirej WITH "aaa"
SELECT (m.menosuboru)
REPLACE ALL ukaz WITH INLIST(uroven,1)
INDEX on poradie TAG poradie
SET FILTER TO ukaz
GO TOP 


*!*****************************************************************************
PROCEDURE citajmenuland
PRIVATE m.pokracuj, m.uvod, m.hladajucet, m.results, m.jsontxt, m.zaciatok, m.koniec, m.order1, m.sumauctu, m.doplnky
PRIVATE m.hladajuzaverka, m.hladajcuzaverka, m.hladajidkas
pokracuj=.t.
DO open_database
LOCAL m.ret, m.lcClasslibWas, m.vysledok, m.httpodpoved, m.price_level
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF !EMPTY(m.menuland_ip) AND !EMPTY(m.menuland_kluc)
	DO WHILE .t.
		IF !m.pokracuj
			EXIT
		ELSE 
			m.poslednyden=get_max("menuland")
			IF m.poslednyden<VAL(SYS(11,DATE()-1))
				IF m.poslednyden=0
					m.poslednyden=DATE()-15
				ELSE
					m.poslednyden=CTOD(SYS(10,m.poslednyden+1))
				ENDIF 
				DO FORM snake NAME lsnakex LINKED WITH jaz("form_citam_predaj")+' '+DTOC(m.poslednyden)
				lsnakex.DRAW()
				m.datefrom=STR(YEAR(m.poslednyden),4,0)+"-"+PADL(ALLTRIM(STR(MONTH(m.poslednyden),2,0)),2,"0")+"-"+PADL(ALLTRIM(STR(DAY(m.poslednyden),2,0)),2,"0")
				http.OPEN("GET",m.menuland_ip+"?is_valid=1&api_key="+m.menuland_kluc+"&date="+m.datefrom , .F.)
				Http.Option(4) = 13056 
				http.settimeouts(0, 0, 0, 0)
				http.setrequestheader("content-type", "application/json;charset=utf-8;")
				http.setrequestheader("Authorization", "Basic " + STRCONV("hlados:dajmijest",13))
				TRY 
					http.SEND("")
					lsnakex.DRAW()
					httpodpoved=http.statustext
					vysledok=http.status
				CATCH
					vysledok=-100
					httpodpoved=jaz("form_menuland_neodpoveda")
				ENDTRY 
				IF m.vysledok=200
					SET CLASSLIB TO "JSON.VCX" ADDITIVE 
					ret = .t.
					loJSON = CREATEOBJECT("JSON")
					lsnakex.DRAW()
			*		=STRTOFILE(http.responsetext,"m:\menuland.txt",1)
					uvod=LEFT(http.responsetext,AT(',"results":',http.responsetext)-1)+"}"
					uvodobj=lojson.parse(STRTRAN(m.uvod,":null",':""'))
					hladajucet=0
					IF uvodobj.count>0
						results=SUBSTR(http.responsetext,AT(',"results":',http.responsetext)+12)
						results=LEFT(m.results,LEN(m.results)-2)
						hladajuzaverka="menuland"
						hladajcuzaverka=-1000
						hladajidkas="01"
						IF USED("sk_pol")
							REQUERY("sk_pol")
						ELSE 
							USE (m.sqlprefix+"rskpoluzaverka") ALIAS sk_pol IN 0
							=CURSORSETPROP("Buffering",5,"sk_pol")
						ENDIF 
						IF USED("sk_fin")
							REQUERY("sk_fin")
						ELSE 
							USE (m.sqlprefix+"rskfinuzaverka_menuland") ALIAS sk_fin IN 0
							=CURSORSETPROP("Buffering",5,"sk_fin")
						ENDIF 
						IF USED("sk_plt")
							REQUERY("sk_plt")
						ELSE 
							USE (m.sqlprefix+"rskpltuzaverka") ALIAS sk_plt IN 0
							=CURSORSETPROP("Buffering",5,"sk_plt")
						ENDIF 
						IF USED("sk_sez")
							REQUERY("sk_sez")
						ELSE 
							USE (m.sqlprefix+"rsksezuzaverka") ALIAS sk_sez IN 0
							=CURSORSETPROP("Buffering",5,"sk_sez")
						ENDIF 
						IF USED("fd")
							REQUERY("fd")
						ELSE 
							USE (m.sqlprefix+"rfooddat") ALIAS fd IN 0
						ENDIF 
						SELECT 000000 as c_kas, u_datetime, SPACE(17) as uzaverka, SPACE(2) as id_kas FROM sk_fin INTO CURSOR kasy READWRITE WHERE .f.
						DO WHILE .t.
							DO WHILE .t.
								m.zaciatok=AT('{"order_id":',m.results,1)
								IF m.zaciatok>0 AND m.pokracuj
									m.koniec=AT('{"order_id":',m.results,2)
									IF m.koniec>0
										m.order1=SUBSTR(m.results,m.zaciatok,m.koniec-m.zaciatok)
										m.results=SUBSTR(m.results, m.koniec)
									ELSE
										m.order1=SUBSTR(m.results,m.zaciatok)
										m.results=""
									ENDIF 
									m.hladajucet=m.hladajucet+1
									m.sumauctu=0					
									m.jsontxt=LEFT(m.order1,AT(',"items":',m.order1)-1)+"}"
									vysledok=lojson.parse(m.jsontxt)
									SELECT kasy
									LOCATE FOR c_kas=VAL(vysledok.cash_register)
									IF FOUND()
										IF vysledok.date_placed>kasy.u_datetime
											REPLACE u_datetime WITH vysledok.date_placed
										ENDIF 
									ELSE
										INSERT INTO kasy (c_kas, u_datetime) VALUES (VAL(vysledok.cash_register), vysledok.date_placed)
									ENDIF 	
									SELECT sk_sez
									LOCATE FOR c_kas=VAL(vysledok.cash_register) AND c_stredisk=VAL(vysledok.warehouse)
									IF !FOUND()
										INSERT INTO sk_sez (c_kas, c_stredisk) VALUES (VAL(vysledok.cash_register), VAL(vysledok.warehouse))
									ENDIF 
									DO WHILE .t.
										m.zaciatok=AT('{"product_id":',m.order1,1)
										IF m.zaciatok>0 AND m.pokracuj
											m.koniec=AT(',"additions":',m.order1,1)
											m.jsontxt=SUBSTR(m.order1, m.zaciatok, m.koniec-m.zaciatok)+"}"
											polozka=lojson.parse(m.jsontxt)
											IF ISNULL(polozka.product_id)
												m.pokracuj=.f.
												=MESSAGEBOX(jaz("form_nenastavena_pol_z_webu")+DTOC(m.poslednyden)+CHR(10)+jaz("form_neda_sa_pokracovat"),16,"FoodMan")
												EXIT
											ELSE 
												IF polozka.quantity<>0
													INSERT INTO sk_pol (c_stredisk, c_uzaverka, ucet, c_karty, dan_sazba, k_odpoctu, jc, mnozstvi, cen_hlad) VALUES;
														(VAL(vysledok.warehouse), VAL(vysledok.cash_register), m.hladajucet, VAL(polozka.product_id), polozka.price_tax_rate*100,"X", polozka.price_incl_tax/polozka.quantity, polozka.quantity,polozka.price_level)
													m.price_level=polozka.price_level
												ELSE
													m.price_level="1"
												ENDIF 
												m.sumauctu=m.sumauctu+polozka.price_incl_tax
												m.zaciatok=AT(',"additions":',m.order1,1)+14
												m.koniec=AT(']',m.order1,1)
												m.doplnky=SUBSTR(m.order1, m.zaciatok, m.koniec-m.zaciatok)
												m.order1=SUBSTR(m.order1,m.koniec+1)
												IF !EMPTY(m.jsontxt)
													DO WHILE .t.
														m.zaciatok=AT('{"product_id":',m.doplnky,1)
														IF m.zaciatok>0 AND m.pokracuj
															m.koniec=AT('}',m.doplnky,1)
															m.jsontxt=SUBSTR(m.doplnky, m.zaciatok, m.koniec-m.zaciatok)+"}"
															m.doplnky=SUBSTR(m.doplnky,m.koniec+1)
															polozka=lojson.parse(m.jsontxt)
															IF ISNULL(polozka.product_id)
																m.pokracuj=.f.
																=MESSAGEBOX(jaz("form_nenastavena_pol_z_webu")+DTOC(m.poslednyden)+CHR(10)+jaz("form_neda_sa_pokracovat"),16,"FoodMan")
																EXIT 
															ELSE 
																IF polozka.quantity<>0
																	INSERT INTO sk_pol (c_stredisk, c_uzaverka, ucet, c_karty, dan_sazba, k_odpoctu, jc, mnozstvi, cen_hlad) VALUES;
																		(VAL(vysledok.warehouse), VAL(vysledok.cash_register), m.hladajucet, VAL(polozka.product_id), polozka.price_tax_rate*100,"X", polozka.price_incl_tax/polozka.quantity, polozka.quantity,m.price_level)
																ENDIF 
																m.sumauctu=m.sumauctu+polozka.price_incl_tax
															ENDI
														ELSE
															EXIT
														ENDIF 
													ENDDO 
												ENDIF 
											ENDIF 
										ELSE
											EXIT
										ENDIF 
									ENDDO 
									IF EMPTY(vysledok.payment_method) OR ISNULL(vysledok.payment_method)
										INSERT INTO sk_plt (c_uzaverka, ucet, druh_pl, cena_pl, tip, cena_mena) VALUES (VAL(vysledok.cash_register), m.hladajucet, "Hotove", m.sumauctu, 0, m.sumauctu)
									ELSE
										INSERT INTO sk_plt (c_uzaverka, ucet, druh_pl, cena_pl, tip, cena_mena) VALUES (VAL(vysledok.cash_register), m.hladajucet, vysledok.payment_method, m.sumauctu, 0, m.sumauctu)
									ENDIF 
									INSERT INTO sk_fin (c_uzaverka, ucet, autor, u_minut, u_datetime, text_link) VALUES (VAL(vysledok.cash_register), m.hladajucet, m.gcuser_id, 1, vysledok.date_placed, vysledok.order_id)
									
								ELSE
									EXIT
								ENDIF 
							ENDDO
							IF uvodobj.next=="" OR !m.pokracuj
								EXIT
							ELSE
								http.OPEN("GET",uvodobj.next , .F.)
								Http.Option(4) = 13056 
								http.settimeouts(0, 0, 0, 0)
								http.setrequestheader("content-type", "application/json;charset=utf-8;")
								http.setrequestheader("Authorization", "Basic " + STRCONV("hlados:dajmijest",13))
								TRY 
									http.SEND("")
									lsnakex.DRAW()
									m.httpodpoved=http.statustext
									m.vysledok=http.status
								CATCH
									m.vysledok=-100
									m.httpodpoved=jaz("form_menuland_neodpoveda")
								ENDTRY 
								IF m.vysledok=200
									m.uvod=LEFT(http.responsetext,AT(',"results":',http.responsetext)-1)+"}"
									uvodobj=lojson.parse(STRTRAN(m.uvod,":null",':""'))
									IF uvodobj.count>0
										m.hladajucet=m.hladajucet+1
										m.results=SUBSTR(http.responsetext,AT(',"results":',http.responsetext)+12)
										m.results=LEFT(m.results,LEN(m.results)-2)
									ENDIF
								ELSE
									m.pokracuj=.f. 
								ENDIF 
							ENDIF 
						ENDDO 
						IF m.pokracuj
							SELECT sk_pol
							REPLACE ALL prachy WITH jc*mnozstvi
							REPLACE all prachy_puv WITH prachy, dan WITH ROUND(prachy*(dan_sazba/100),2)
							SELECT sk_fin
							REPLACE ALL u_cas WITH SUBSTR(STRTRAN(STRTRAN(TTOC(u_datetime,3),"-",""),"T"," "),7,8)
							SELECT kasy
							UPDATE kasy SET id_kas=fd.id from fd WHERE kasy.c_kas=fd.c_stredisk
							REPLACE id_kas WITH "xx" FOR EMPTY(id_kas) OR ISNULL(id_kas)
							REPLACE all uzaverka WITH STRTRAN(STRTRAN(TTOC(u_datetime,3),"-",""),"T"," ")
*							REPLACE ALL uzaverka WITH hladajuzaverku(uzaverka)
							GO TOP 
							SCAN FOR !EMPTY(uzaverka)
								m.hladajuzaverka=hladajuzaverku(kasy.uzaverka)
								m.hladajidkas=kasy.id_kas
								SELECT 0
								USE (m.sqlprefix+"rskuzahladajcuzaverky") ALIAS maxuz 
								IF RECCOUNT()=0
									INSERT INTO maxuz (uzaverka, id_kas, c_data) VALUES (m.hladajuzaverka, m.hladajidkas, m.cdata)
									=tux(.t.,.t.,"maxuz")
									=REQUERY("maxuz")
									SELECT maxuz
								ENDIF 
								GO TOP 
								m.hladajcuzaverka=maxuz.idriadok
								USE IN maxuz
								SELECT sk_pol
								REPLACE c_uzaverka WITH m.hladajcuzaverka FOR c_uzaverka=kasy.c_kas
								SELECT sk_plt
								REPLACE c_uzaverka WITH m.hladajcuzaverka FOR c_uzaverka=kasy.c_kas
								SELECT sk_fin
								REPLACE c_uzaverka WITH m.hladajcuzaverka FOR c_uzaverka=kasy.c_kas
								SELECT sk_sez
								REPLACE uzaverka WITH m.hladajuzaverka, c_uzaverka WITH m.hladajcuzaverka, c_mena WITH m.giakt_mena FOR c_kas=kasy.c_kas
								UPDATE sk_sez SET id_kas=fd.id from fd WHERE sk_sez.c_kas=fd.c_stredisk
								UPDATE sk_sez SET id_man=fd.id from fd WHERE sk_sez.c_stredisk=fd.c_stredisk
							ENDSCAN 
							=tux(.t.,.t.,"sk_sez")
							=tux(.t.,.t.,"sk_fin")
							=tux(.t.,.t.,"sk_pol")
							=tux(.t.,.t.,"sk_plt")
							=set_max("menuland",VAL(SYS(11, m.poslednyden)))
						ELSE
							=TABLEREVERT(.t.,"sk_sez")
							=TABLEREVERT(.t.,"sk_fin")
							=TABLEREVERT(.t.,"sk_pol")
							=TABLEREVERT(.t.,"sk_plt")
						ENDIF 
					ELSE 
						=set_max("menuland",VAL(SYS(11, m.poslednyden)))
					ENDIF 
					lsnakex.release()
					RELEASE CLASSLIB JSON.VCX
				ELSE
					m.pokracuj=.f.
				ENDIF 	
			ELSE
				EXIT 
			ENDIF 
		ENDIF 
		DO pozavirej WITH "fd, sk_sez, sk_plt, sk_pol, sk_fin"
	ENDDO 
ENDIF 
RELEASE m.http


*!*****************************************************************************
FUNCTION citajpreviorastre
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
DO FORM snake NAME lsnakex LINKED WITH 'Čítam rastre'
lsnakex.DRAW()
LOCAL m.ret, m.lcClasslibWas, vysledok, m.xml, m.pocetsegmentov, m.segment, m.httpodpoved
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST",m.hor_adr+"/system/getSegments/" , .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/xml")
TEXT TO m.content TEXTMERGE NOSHOW PRETEXT 7
<?xml version="1.0"?>
<request>
	<login><<m.hor_usr>></login>
	<password><<m.hor_pwd>></password>
	<lanId>2</lanId>
</request>
ENDTEXT 
TRY 
	http.SEND(m.content)
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	LOCAL oldprocedure
	m.oldprocedure=SET("procedure")
	SET PROCEDURE TO spracuj_xml ADDITIVE 
	m.ret = .t.
	lsnakex.DRAW()
	CREATE CURSOR rastre (crc I, name c(30), variant i, ac_group i, id i, type I, vat N(5,2), code c(10),pmc L)
	INDEX on id TAG id
	m.xml=http.responsetext
	m.xml=STRTRAN(m.xml,'<?xml version="1.0" encoding="UTF-8"?>'+CHR(10)+'<segment>','<?xml version="1.0" encoding="UTF-8"?>'+CHR(10)+'<segments>')
	m.xml=STRTRAN(m.xml,'</segment>'+CHR(10)+'</segment>','</segment>'+CHR(10)+'</segments>')
	m.pocetsegmentov=hladajsekciu("segments",m.xml)	
	IF m.pocetsegmentov>0
		m.segmenty=vratsekciu("segments", m.xml,1,"X")
   		m.pocetsegmentov=hladajsekciu("segment",m.segmenty)	
		FOR m.i=1 TO m.pocetsegmentov
		    m.segment=vratsekciu("segment", m.segmenty,m.i,"X")
			TRY 
				INSERT INTO rastre (name, id) VALUES(vratsekciu("name",m.segment,1,"C"),vratsekciu("segId",m.segment,1,"N"))
				lsnakex.DRAW()
			CATCH 
			ENDTRY 
		ENDFOR 
	ENDIF 
	SELECT rastre
	REPLACE ALL code WITH ALLTRIM(STR(id,10,0))
	RELEASE raster
	SET PROCEDURE TO &oldprocedure
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http, m.xml, m.pocetsegmentov, m.segment, m.httpodpoved, m.segmenty
lsnakex.release()
RETURN m.ret

*!*****************************************************************************
FUNCTION citajpreviocurrencies
PARAMETERS hor_adr, hor_port, hor_usr, hor_pwd, api_usr, api_pwd
DO FORM snake NAME lsnakex LINKED WITH 'Čítam currencies'
lsnakex.DRAW()
LOCAL m.ret, m.lcClasslibWas, vysledok, m.xml, m.pocetsegmentov, m.segment, m.httpodpoved
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("POST",m.hor_adr+"/system/getCurrencies/" , .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/xml")
TEXT TO m.content TEXTMERGE NOSHOW PRETEXT 7
<?xml version="1.0"?>
<request>
	<login><<m.hor_usr>></login>
	<password><<m.hor_pwd>></password>
</request>
ENDTEXT 
TRY 
	http.SEND(m.content)
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_hores_neodpoveda")
ENDTRY 
IF m.vysledok=200
	LOCAL oldprocedure
	m.oldprocedure=SET("procedure")
	SET PROCEDURE TO spracuj_xml ADDITIVE 
	m.ret = .t.
	lsnakex.DRAW()
	lsnakex.DRAW()
	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)
	INDEX on id TAG id
	m.xml=http.responsetext
	m.pocetsegmentov=hladajsekciu("currencies",m.xml)	
	IF m.pocetsegmentov>0
	    m.segmenty=vratsekciu("currencies", m.xml,1,"X")
		m.pocetsegmentov=hladajsekciu("currency",m.segmenty)
		FOR m.i=1 TO m.pocetsegmentov
		    m.segment=vratsekciu("segment", m.segmenty,m.i,"X")
			TRY 
				INSERT INTO currencies (code, name, id) VALUES(vratsekciu("code",m.segment,1,"C"), vratsekciu("name",m.segment,1,"C"),vratsekciu("curId",m.segment,1,"N"))
				lsnakex.DRAW()
			CATCH 
			ENDTRY 
		ENDFOR 
	ENDIF 
	SELECT currencies
	SET PROCEDURE TO &oldprocedure
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http, m.xml, m.pocetsegmentov, m.segment
lsnakex.release()
RETURN m.ret


PROCEDURE vyplnsscc
PARAMETERS partnerico, hladajcpartner
PRIVATE m.serial_no, m.hladajckarty
LOCAL dlzkaica
m.hhotel_gln=IIF(EMPTY(m.hotel_edikod), ALLTRIM(m.hotel_ico), ALLTRIM(m.hotel_edikod))
IF ATC("sushitime", m.wi_zakaz) > 0
	IF m.issql
		USE (m.sqlprefix+"rediprevpartnera") IN 0 ALIAS dodpartner
	ELSE
		USE vediprevpartnera IN 0 ALIAS dodpartner
	ENDIF 
	IF RECCOUNT("dodpartner")>0
		SELECT dodpartner
		GO TOP 
		IF !EMPTY(dodpartner.gln)
			m.hhotel_gln=ALLTRIM(dodpartner.gln)
		ENDIF 
	ENDIF 
	USE IN dodpartner		
ENDIF 
SELECT buffer 
SCAN FOR EMPTY(serial_no)
	m.hladajckarty=buffer.c_karty
	IF USED("dodean")
		=REQUERY("dodean")
	ELSE
		IF m.issql
			USE (m.sqlprefix+"reanykukarte") ALIAS dodean IN 0
		ELSE 
			USE veanykukarte ALIAS dodean IN 0
		ENDIF 
	ENDIF 
	SELECT dodean
	IF RECCOUNT()>0
		m.dlzkaica=LEN(ALLTRIM(m.partnerico))
		IF m.dlzkaica>0
			LOCATE FOR LEFT(dodean.ean,m.dlzkaica+2)=ALLTRIM(m.partnerico)+"::"
			IF FOUND()
				m.serial_no=ALLTRIM(SUBSTR(dodean.ean,m.dlzkaica+3))
			ELSE
				IF EMPTY(buffer.serial_no)
					GO TOP 
					LOCATE FOR !("::"$dodean.ean)
					IF FOUND()
						m.serial_no=ALLTRIM(dodean.ean)
					ELSE
						GO TOP 
						m.serial_no=ALLTRIM(dodean.ean)
					ENDIF 
				ELSE
					m.serial_no=ALLTRIM(buffer.serial_no)
				ENDIF 
			ENDIF 
		ELSE
			IF EMPTY(buffer.serial_no)
				m.serial_no=""
			ELSE
				m.serial_no=ALLTRIM(buffer.serial_no)
			ENDIF 
		ENDIF 
	ELSE
		IF EMPTY(buffer.serial_no)
			m.serial_no=""
		ELSE
			m.serial_no=ALLTRIM(buffer.serial_no)
		ENDIF 
	ENDIF 
	SELECT buffer
	REPLACE serial_no WITH m.serial_no
ENDSCAN 
GO TOP 
SCAN FOR !EMPTY(sscc)
	IF SUBSTR(sscc,6,7)<>SUBSTR(m.hhotel_gln,7)
		REPLACE sscc WITH ""
	ENDIF 
ENDSCAN 
GO TOP 
REPLACE sscc WITH "" FOR ISNULL(sscc)
GO TOP 
SCAN FOR EMPTY(sscc) OR ISNULL(sscc)
	m.cislopalety=i_max("sscc"+ALLTRIM(m.hhotel_gln))
	IF m.cislopalety=0
		=set_max("sscc"+ALLTRIM(m.hhotel_gln),1)
		m.cislopalety=1
	ELSE 
		IF m.cislopalety>=100000000000
			=set_max("sscc"+ALLTRIM(m.hhotel_gln),1)
			m.cislopalety=1
		ENDIF 	
	ENDIF 
	m.prefix="0"
	DO CASE 
		CASE m.cislopalety>=90000000000
			m.prefix="9"
			m.cislopalety=m.cislopalety-90000000000
		CASE m.cislopalety>=80000000000
			m.prefix="8"
			m.cislopalety=m.cislopalety-80000000000
		CASE m.cislopalety>=70000000000
			m.prefix="7"
			m.cislopalety=m.cislopalety-70000000000
		CASE m.cislopalety>=60000000000
			m.prefix="6"
			m.cislopalety=m.cislopalety-60000000000
		CASE m.cislopalety>=50000000000
			m.prefix="5"
			m.cislopalety=m.cislopalety-50000000000
		CASE m.cislopalety>=50000000000
			m.prefix="4"
			m.cislopalety=m.cislopalety-40000000000
		CASE m.cislopalety>=40000000000
			m.prefix="3"
			m.cislopalety=m.cislopalety-30000000000
		CASE m.cislopalety>=20000000000
			m.prefix="2"
			m.cislopalety=m.cislopalety-20000000000
		CASE m.cislopalety>=10000000000
			m.prefix="1"
			m.cislopalety=m.cislopalety-10000000000
	ENDCASE 
	DO CASE 
		CASE m.cislopalety<1000000
			sscccislo="0"+LEFT(m.hhotel_gln,10)+PADL(ALLTRIM(STR(m.cislopalety,6,0)),6,"0")
		CASE m.cislopalety<10000000
			sscccislo="0"+LEFT(m.hhotel_gln,9)+PADL(ALLTRIM(STR(m.cislopalety,7,0)),7,"0")
		CASE m.cislopalety<100000000
			sscccislo="0"+LEFT(m.hhotel_gln,8)+PADL(ALLTRIM(STR(m.cislopalety,8,0)),8,"0")
		CASE m.cislopalety<1000000000
			sscccislo="0"+LEFT(m.hhotel_gln,7)+PADL(ALLTRIM(STR(m.cislopalety,9,0)),9,"0")
		CASE m.cislopalety<10000000000
			sscccislo="0"+LEFT(m.hhotel_gln,6)+PADL(ALLTRIM(STR(m.cislopalety,10,0)),10,"0")
	ENDCASE 
	m.dlzkasscc=LEN(m.sscccislo)
	parne=0
	neparne=0
	FOR m.i=1 TO m.dlzkasscc
		IF m.i=1
			m.parne=m.parne+VAL(SUBSTR(m.sscccislo,m.dlzkasscc-m.i+1,1))
		ELSE
			IF MOD(m.i,2)=0
				m.neparne=m.neparne+VAL(SUBSTR(m.sscccislo,m.dlzkasscc-m.i+1,1))
			ELSE
				m.parne=m.parne+VAL(SUBSTR(m.sscccislo,m.dlzkasscc-m.i+1,1))
			ENDIF 
		ENDIF 
	ENDFOR 
	m.parne=m.parne*3
	m.kc=m.parne+m.neparne
	m.kc=STR(10-VAL(RIGHT(ALLTRIM(STR(m.kc,10,0)),1)),1,0)
	IF m.kc="*"
		m.kc="0"
	ENDIF 
	SELECT buffer
	REPLACE sscc WITH "(00)"+m.sscccislo+m.kc
ENDSCAN 
DO pozavirej WITH "dodean,dodpartner"

PROCEDURE filtrujpriznaky
PARAMETERS tabulka, nemazat
IF !EMPTY(m.gcfilter_triedenia)
	DO pozavirej WITH "ktriedfilt"
	IF m.issql
		TRY 
			sql_select('select distinct c_karty FROM  food600.ktried where c_data = ?m.cdata and '+m.gcfilter_triedenia,'ktriedfilt','c_karty i')
		CATCH
			DO pozavirej WITH "ktriedfilt"
		ENDTRY 
	ELSE 
		IF !USED("ktried")
			thisform.bolktried=.f.
			USE (m.fn_ktried) IN 0 ALIAS ktried
		ELSE
			thisform.bolktried=.t.
		ENDIF 					
		TRY 
			SELECT * FROM ktried WHERE &m.gcfilter_triedenia INTO CURSOR ktriedfilt
		CATCH
			DO pozavirej WITH "ktriedfilt"
		ENDTRY
		IF !thisform.bolktried 
			DO pozavirej WITH "ktried"
		ENDIF  
	ENDIF 
ELSE 
	DO pozavirej WITH "ktriedfilt"
ENDIF 
IF USED("ktriedfilt")
	IF nemazat
		UPDATE (tabulka) SET label=UPPER(label) WHERE !EMPTY(label) AND c_karty in (select c_karty from ktriedfilt)
	ELSE 
		DELETE FROM (tabulka) WHERE !(c_karty in (select c_karty FROM ktriedfilt))
	ENDIF 
	USE IN ktriedfilt
ENDIF 
SELECT (tabulka)


************************************************************************
PROCEDURE CreateGUID
PARAMETERS bezzatvorky
********************
*** Author: Rick Strahl, West Wind Technologies
*** http://www.west-wind.com/
*** Modified: 01/26/98
*** Function: Creates a globally unique identifier using Win32
*** COM services. The value is guaranteed to be unique
*** Format: {9F47F480-9641-11D1-A3D0-00600889F23B}
*** Return: GUID as a string or "" if the function failed
*************************************************************************

DECLARE INTEGER CoCreateGuid IN Ole32.dll ;
STRING @cGUIDStruc

DECLARE INTEGER StringFromGUID2 IN Ole32.dll ;
STRING cGUIDStruc, ;
STRING @cGUID, ;
LONG nSize

LOCAL lcStruc_GUID, lcGUID, lnSize

*** Simulate GUID strcuture with a string
lcStruc_GUID = REPLICATE(" ",16)
lcGUID = REPLICATE(" ",80)
lnSize = INT(LEN(lcGUID) / 2)
IF CoCreateGuid(@lcStruc_GUID) # 0
RETURN ""
ENDIF

*** Now convert the structure to the GUID string
IF StringFromGUID2(lcStruc_GUID,@lcGuid,lnSize) = 0
RETURN ""
ENDIF

IF m.bezzatvorky
	RETURN STRTRAN(STRTRAN(StrConv(LEFT(lcGUID,76),6),"{",""),"}","")
ELSE
	RETURN StrConv(LEFT(lcGUID,76),6)
ENDIF 


*!*****************************************************************************
FUNCTION citajantikzony
PARAMETERS antik_adr
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_zony')
*m.antik_adr="https://tense.rc.api.smartcityportal.sk"
*m.antik_adr="http://192.168.88.227:8080"
lsnakex.DRAW()
LOCAL m.vysledok, m.zaciatok, m.koniec, textik
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("GET",m.antik_adr+"/api/integrator/zones", .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
	lsnakex.DRAW()
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	lsnakex.DRAW()
	m.textik=http.responsetext
	DO WHILE '"turnstiles":['$m.textik
		m.zaciatok=AT(',"turnstiles":[',m.textik)
		m.koniec=AT(']',SUBSTR(m.textik,m.zaciatok))
		m.textik=LEFT(m.textik,m.zaciatok-1)+SUBSTR(m.textik,m.zaciatok+m.koniec)
	ENDDO 
	m.vysledok=lojson.parse(m.textik,"")
	lsnakex.DRAW()
	RELEASE lojson
	CREATE CURSOR zoznamzon (id c(50), name c(50))
	FOR m.i=1 TO ALEN(vysledok.data)
		TRY 
			INSERT INTO zoznamzon (id, name) VALUES(vysledok.data[m.i].id, vysledok.data[m.i].name)
			lsnakex.DRAW()
		CATCH 
		ENDTRY 
	ENDFOR 
	RELEASE m.vysledok
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret


*!*****************************************************************************
FUNCTION citajantikturnikety
PARAMETERS antik_adr
*m.antik_adr="https://tense.rc.api.smartcityportal.sk"
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_turnikety')
lsnakex.DRAW()
LOCAL m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("GET",m.antik_adr+"/api/integrator/turnstiles", .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
	lsnakex.DRAW()
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	lsnakex.DRAW()
	m.vysledok=lojson.parse(http.responsetext,"")
	lsnakex.DRAW()
	RELEASE lojson
	CREATE CURSOR zoznamturn (sn i, id i)
	FOR m.i=1 TO ALEN(vysledok.data)
		TRY 
			INSERT INTO zoznamturn (id, sn) VALUES(vysledok.data[m.i].id, vysledok.data[m.i].sn)
			lsnakex.DRAW()
		CATCH 
		ENDTRY 
	ENDFOR 
	RELEASE m.vysledok
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret

*!*****************************************************************************
FUNCTION vytvorantikzonu
PARAMETERS antik_adr, meno, id_oprava
*m.antik_adr="https://tense.rc.api.smartcityportal.sk"
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF EMPTY(m.id_oprava)
	http.OPEN("POST",m.antik_adr+"/api/integrator/zones/" , .F.)
ELSE
	http.OPEN("PUT",m.antik_adr+"/api/integrator/zones/"+ALLTRIM(id_oprava) , .F.)
ENDIF 
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
m.loJSON = CREATEOBJECT("JSON")
CREATE CURSOR jsontxt (jsontxt M)
INSERT INTO jsontxt (jsontxt) VALUES ("") 
REPLACE jsontxt.jsontxt WITH '{"name":"'+ALLTRIM(m.meno)+'",'+CHR(13)+CHR(10)    &&meno kasy
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"turnstiles":'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'['+CHR(13)+CHR(10)
SELECT turnikety
SCAN ALL 
	SELECT jsontxt
	REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+ALLTRIM(STR(turnikety.sn,50,0))+","
	SELECT turnikety
ENDSCAN 
SELECT jsontxt
IF RIGHT(jsontxt.jsontxt,1)==","
	REPLACE jsontxt.jsontxt WITH LEFT(jsontxt.jsontxt,LEN(jsontxt.jsontxt)-1)
ENDIF 
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+']}'+CHR(13)+CHR(10)
TRY 
	http.SEND(jsontxt.jsontxt)
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
USE IN jsontxt
IF m.vysledok=200
	IF !ISNULL(http.responsetext)
		m.vysledok=lojson.parse(http.responsetext)
		TRY 
			m.ret=vysledok.data.id
			m.odpoved=jaz("form_zona_vytvorena")
		CATCH
			m.ret=""
			m.odpoved="Nepodarilo sa vytvoriť zónu."
		ENDTRY 
		RELEASE lojson
	ELSE
		m.ret=""
		m.odpoved=http.statustext
	ENDIF 
ELSE
	m.ret=""
	m.odpoved=http.statustext
ENDIF 
RELEASE m.http
RELEASE CLASSLIB json.vcx
RETURN m.ret 


*!*****************************************************************************
FUNCTION zmazantikzonu
PARAMETERS antik_adr, zona
*m.antik_adr="https://tense.rc.api.smartcityportal.sk"
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("DELETE",m.antik_adr+"/api/integrator/zones/"+ALLTRIM(m.zona) , .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
DO CASE 
	CASE m.vysledok=202
		m.ret=.t.
		m.odpoved=jaz("form_zona_odstranena")
	CASE m.vysledok=404
		m.ret=.f.
		m.odpoved=jaz("form_zona_neexistuje")
	OTHERWISE 
		m.ret=.f.
		m.odpoved=jaz("form_zona_mazenie_nepreslo")
ENDCASE 
RELEASE m.http
RELEASE CLASSLIB json.vcx
RETURN m.ret 

*!*****************************************************************************
FUNCTION vytvorantikkartu
PARAMETERS antik_adr, karta, oprava
*m.antik_adr="https://tense.rc.api.smartcityportal.sk"
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF EMPTY(m.oprava)
	http.OPEN("POST",m.antik_adr+"/api/integrator/tickets/" , .F.)
ELSE
	http.OPEN("PUT",m.antik_adr+"/api/integrator/tickets/"+ALLTRIM(m.karta) , .F.)
ENDIF 
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
m.loJSON = CREATEOBJECT("JSON")
CREATE CURSOR jsontxt (jsontxt M)
INSERT INTO jsontxt (jsontxt) VALUES ("") 
IF EMPTY(m.oprava)
	REPLACE jsontxt.jsontxt WITH '{"reference_number":"'+ALLTRIM(m.karta)+'",'+CHR(13)+CHR(10)    &&meno kasy
	REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"from":"'+STRTRAN(TTOC(DATETIME(),3),"T"," ")+'",'+CHR(13)+CHR(10)
ELSE 
	REPLACE jsontxt.jsontxt WITH '{"from":"'+STRTRAN(TTOC(DATETIME(),3),"T"," ")+'",'+CHR(13)+CHR(10)
ENDIF 
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"to":"'+STRTRAN(TTOC(DATE()+1,3),"T"," ")+'",'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"zones":'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'['+CHR(13)+CHR(10)
SELECT povolenezony
SCAN ALL 
	SELECT jsontxt
	REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"'+ALLTRIM(povolenezony.id)+'",'
	SELECT povolenezony
ENDSCAN 
SELECT jsontxt
IF RIGHT(jsontxt.jsontxt,1)==","
	REPLACE jsontxt.jsontxt WITH LEFT(jsontxt.jsontxt,LEN(jsontxt.jsontxt)-1)
ENDIF 
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+']}'+CHR(13)+CHR(10)
TRY 
	http.SEND(jsontxt.jsontxt)
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
USE IN jsontxt
IF m.vysledok=200
	IF !ISNULL(http.responsetext)
		m.textik=http.responsetext
		DO WHILE '"zones":['$m.textik
			m.zaciatok=AT(',"zones":[',m.textik)
			m.koniec=AT(']',SUBSTR(m.textik,m.zaciatok))
			m.textik=LEFT(m.textik,m.zaciatok-1)+SUBSTR(m.textik,m.zaciatok+m.koniec)
		ENDDO 
		m.vysledok=lojson.parse(m.textik,"")
		TRY 
			m.ret=vysledok.data.id
			m.odpoved=jaz("form_karta_vytvorena")
		CATCH
			m.ret=0
			m.odpoved=vysledok.message
		ENDTRY 
		RELEASE lojson
	ELSE
		m.ret=0
		m.odpoved=http.statustext
	ENDIF 
ELSE
	m.ret=0
	m.odpoved=http.statustext
ENDIF 
RELEASE m.http
RELEASE CLASSLIB json.vcx
RETURN m.ret 

*!*****************************************************************************
FUNCTION zmazantikkartu
PARAMETERS antik_adr, karta
*m.antik_adr="https://tense.rc.api.smartcityportal.sk"
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("DELETE",m.antik_adr+"/api/integrator/tickets/"+ALLTRIM(m.karta) , .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
DO CASE 
	CASE m.vysledok=202
		m.ret=.t.
		m.odpoved=jaz("form_karta_odstranena")
	CASE m.vysledok=404
		m.ret=.f.
		m.odpoved=jaz("form_karta_neexistuje")
	OTHERWISE 
		m.ret=.f.
		m.odpoved=jaz("form_karta_mazenie_nepreslo")
ENDCASE 
RELEASE m.http
RELEASE CLASSLIB json.vcx
RETURN m.ret 

*!*****************************************************************************
FUNCTION citajantikpohyby
PARAMETERS antik_adr, karta
*m.antik_adr="https://tense.rc.api.smartcityportal.sk"
LOCAL m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("GET",m.antik_adr+"/api/integrator/tickets/"+ALLTRIM(karta)+"/timeline", .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
IF m.vysledok=200
	m.textik=http.responsetext
*	m.textik='{"data": {"total": 223,"zones": [{"start": "2023-01-01 00:00:01","end": "2023-01-01 00:00:02","zone": {"id": "01h0qb8bmvr26he62gpbfdce2j","name": "A"},"duration": 223},'
*	m.textik=m.textik+'{"start": "2023-01-01 00:00:01","end": "2023-01-01 00:00:02","zone": {"id": "01h0qa91gr5s62dh4fct16fjnf","name": "A"},"duration": 223}]}}'
	IF '"zones":'$m.textik
		m.zaciatok=AT('"zones":',m.textik)
		m.koniec=AT(']',m.textik)
		m.textik=ALLTRIM(SUBSTR(m.textik,m.zaciatok,m.koniec-m.zaciatok+1))
		m.textik="{"+m.textik+"}"
		SET CLASSLIB TO "JSON.VCX" ADDITIVE 
		m.ret = .t.
		m.loJSON = CREATEOBJECT("JSON")
		m.vysledok=lojson.parse(m.textik,"")
		RELEASE lojson
		CREATE CURSOR zoznampohybov (zona character(50), start t, end t, cas I, id_zona I)
		FOR m.i=1 TO ALEN(vysledok.zones)
			IF !ISNULL(vysledok.zones[m.i].zone.id) AND !ISNULL(vysledok.zones[m.i].duration)
				INSERT INTO zoznampohybov (zona, start, end, cas) VALUES (vysledok.zones[m.i].zone.id,vysledok.zones[m.i].start,vysledok.zones[m.i].end, vysledok.zones[m.i].duration)
			ENDIF 
		ENDFOR 
		RELEASE m.vysledok
		RELEASE CLASSLIB JSON.VCX
	ELSE
		m.ret=.f.
	ENDIF 
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
RETURN m.ret


********** vraci formatovane cislo  ********** delka se uplatni pouze je-li vetsi nez 
FUNCTION nx (cislo, des_mist, delka)

IF EMPTY(m.des_mist)
    des_mist = 0
ENDIF 

IF EMPTY(m.delka)
    delka = 0
ENDIF 

IF ISNULL(m.cislo)
    cislo = 0
ENDIF 
nxpolotovar = STRTRAN(ALLTRIM(STR(m.cislo, 15, m.des_mist)), ",", ".")    && tohle je alltrimovany

IF m.delka > LEN(m.nxpolotovar)
    nxpolotovar = PADL(m.nxpolotovar, m.delka)
ENDIF 
RETURN nxpolotovar

*RETURN STRTRAN(ALLTRIM(STR(IIF(ISNULL(m.cislo), 0, m.cislo), 12, m.des_mist)), ",", ".")


