PARAMETERS dennyexport, uzaverka1, uzaverka2, src_path, zkontroly
PRIVATE m.navrat
*src_path = m.gccen_path

old_errh = ON("ERROR")
*ON ERROR &old_errh
IF m.issql
	IF !USED("karty")
		USE (m.sqlprefix+"rkartyucto") ALIAS karty IN 0
		SELECT karty
		INDEX on c_karty TAG c_karty
	ENDIF 
	IF !USED("skupiny")
		USE (m.sqlprefix+"rskupinyucto") ALIAS skupiny IN 0
	ENDIF 
	IF !USED("food_dat")
		USE (m.sqlprefix+"rfooddat") ALIAS food_dat IN 0
	ENDIF 
ELSE 
	fn_sk_plt = m.src_path + "sk_plt"
	fn_sk_pol = m.src_path + "sk_pol"
	fn_sk_fin = m.src_path + "sk_fin"
	fn_sk_sez = m.src_path + "sk_sez"
	USE (m.fn_sk_fin) ALIAS sk_fin IN 0
	USE (m.fn_sk_pol) ALIAS sk_pol IN 0
	USE (m.fn_sk_plt) ALIAS sk_plt IN 0
	USE (fn_sk_sez) alias sk_sez IN 0
	SELECT sk_sez
	INDEX ON uzaverka TO (m.gcpri_path + "uzuniq") UNIQUE 
ENDIF 

u_ex_sk_had = CREATEOBJECT("had", jaz("form_export_trzieb"))
u_ex_sk_had.SHOW()
*!*	SELECT 0
*!*	USE (m.gcucto_path + "u_ex") EXCL
*!*	INSERT INTO (m.gcucto_path + "u_ex") (MD, dal, hs_md, hs_dal, md2, pz) ;
*!*		VALUES ("EXPORT", "EXPORT", RIGHT(src_path, 8), RIGHT(src_path, 8), STR(SECO()*50, 7), m.uzaverka1 + "   " + m.uzaverka2)
DO priprava
u_ex_sk_had.set_had(44)
DO exp_pol
u_ex_sk_had.set_had(66)
DO exp_plt
u_ex_sk_had.set_had(88)
*!*	SELE MD, dal, hs_md, hs_dal, md2, dal2, pz, ROUND(SUM(castka), 2) AS castka, poznamka ;
*!*		GROUP BY 1,2,3,4,5,6,7 FROM (m.gcucto_path + "u_ex") INTO TABLE (m.gcucto_path + "u_ex_sum")
RELEASE u_ex_sk_had
SELECT disti uzaverka FROM pol WHERE !exportuj INTO CURSOR nenastavene
IF m.dennyexport
	IF RECCOUNT("nenastavene")>0
		=MESSAGEBOX(jaz("form_nenast_ucty_polozky"),64,"FoodMan",2000)
		m.navrat=.f.
	ELSE
		SELECT disti uzaverka FROM plt WHERE !exportuj INTO CURSOR nenastavene
		IF RECCOUNT("nenastavene")>0
			=MESSAGEBOX(jaz("form_nenast_ucty_platby"),64,"FoodMan",2000)
			m.navrat=.f.
		ELSE
			m.navrat=.t.
		ENDIF 
	ENDIF 
ELSE
	IF RECCOUNT("nenastavene")>0
*		=MESSAGEBOX("Nenastavené účty pre položky.",64,"FoodMan")
		m.navrat=.f.
	ELSE
		SELECT disti uzaverka FROM plt WHERE !exportuj INTO CURSOR nenastavene
		IF RECCOUNT("nenastavene")>0
*			=MESSAGEBOX("Nenastavené účty pre platby.",64,"FoodMan")
			m.navrat=.f.
		ELSE
			m.navrat=.t.
		ENDIF 
	ENDIF 
ENDIF 
DO pozavirej WITH "nenastavene"
IF m.navrat
	*** kontrola, ci sa v pol nenachadzaju polozky, ktore sa nemaju exportovat
	SELECT SUM(castka) as celkom, $0 as beznul, uzaverka, u_cis FROM pol INTO CURSOR aaa GROUP BY uzaverka, u_cis READWRITE 
	DO CASE 
		CASE INLIST(LOWER(m.gcuct_typ),"pohoda")
			SELECT SUM(castka) as castka, uzaverka, u_cis FROM pol INTO CURSOR bbb GROUP BY uzaverka, u_cis WHERE !EMPTY(md) OR !EMPTY(dal)
		CASE LOWER(m.gcuct_typ)="kros" AND !INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")
			SELECT SUM(castka) as castka, uzaverka, u_cis FROM pol INTO CURSOR bbb GROUP BY uzaverka, u_cis WHERE !EMPTY(dal)
		OTHERWISE 
			SELECT SUM(castka) as castka, uzaverka, u_cis FROM pol INTO CURSOR bbb GROUP BY uzaverka, u_cis WHERE !EMPTY(md) AND !EMPTY(dal)
	ENDCASE 
	UPDATE aaa SET beznul=bbb.castka from bbb WHERE aaa.uzaverka=bbb.uzaverka AND aaa.u_cis=bbb.u_cis
	USE IN bbb
	DELETE FROM aaa WHERE celkom=beznul
	*** uprava ciastky v plt pomerne o sumu poloziek, ktore sa nemaju exportovat
	UPDATE plt SET castka=plt.castka*aaa.beznul/aaa.celkom from aaa WHERE plt.uzaverka=aaa.uzaverka AND plt.u_cis=aaa.u_cis AND aaa.celkom<>0
	USE IN aaa

	*** Pre uctovnictva MONEY a KARAT sa platby n_uver neposielaju, lebo sa robi export faktur. 
	IF INLIST(LOWER(m.gcuct_typ),"money","karat")
		SELECT plt
		REPLACE md WITH "" FOR "UVER"$druh_pl
	ENDIF 
	*** kontrola, ci sa v plt nenachadzaju platby, ktore sa nemaju exportovat
	SELECT SUM(castka) as celkom, $0 as beznul, uzaverka, u_cis FROM plt INTO CURSOR aaa GROUP BY uzaverka, u_cis READWRITE 
	DO CASE 
		CASE INLIST(LOWER(m.gcuct_typ),"pohoda")
			SELECT SUM(castka) as castka, uzaverka, u_cis FROM plt INTO CURSOR bbb GROUP BY uzaverka, u_cis WHERE !EMPTY(md) OR !EMPTY(dal)
		CASE LOWER(m.gcuct_typ)="kros" AND !INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")
			SELECT SUM(castka) as castka, uzaverka, u_cis FROM plt INTO CURSOR bbb GROUP BY uzaverka, u_cis WHERE !EMPTY(md)
		OTHERWISE  
			SELECT SUM(castka) as castka, uzaverka, u_cis FROM plt INTO CURSOR bbb GROUP BY uzaverka, u_cis WHERE !EMPTY(md) AND !EMPTY(dal)
	ENDCASE  
	UPDATE aaa SET beznul=bbb.castka from bbb WHERE aaa.uzaverka=bbb.uzaverka AND aaa.u_cis=bbb.u_cis
	USE IN bbb
	DELETE FROM aaa WHERE celkom=beznul
	*** uprava ciastky v pol pomerne o sumu platieb, ktore sa nemaju exportovat
	UPDATE pol SET castka=pol.castka*aaa.beznul/aaa.celkom from aaa WHERE pol.uzaverka=aaa.uzaverka AND pol.u_cis=aaa.u_cis AND aaa.celkom<>0
	USE IN aaa
	
	*** odmazanie riadkov z pol a plt, kde je nevyplnene md alebo dal
	DO CASE 
		CASE INLIST(LOWER(m.gcuct_typ),"pohoda")
			*** Pohoda pouziva predkontacie, ktore sa vyplnaju iba do MD alebo do DAL 
			DELETE FROM pol WHERE EMPTY(md) AND EMPTY(dal)		
			DELETE FROM plt WHERE EMPTY(md) AND EMPTY(dal)		
		CASE LOWER(m.gcuct_typ)="kros" AND !INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")
			*** Kros ma v platbach vyplnene md a polozkach dal
			DELETE FROM pol WHERE EMPTY(dal)		
			DELETE FROM plt WHERE EMPTY(md)
		OTHERWISE 
			DELETE FROM pol WHERE EMPTY(md) OR EMPTY(dal)		
			DELETE FROM plt WHERE EMPTY(md) OR EMPTY(dal)		
	ENDCASE  	
	**** doplnit select z pol a plt na u_ex_platby a u_ex_polozky
	IF m.zkontroly
		INSERT INTO u_ex_polozky (datum_dokl, uzaverka, u_cis, id_kas, id_man, castka, md, dal, md2, dal2, hs_md, hs_dal, pz, poznamka, druh, cen_hlad, dan_zakl, dan, faktura, zaloha, partner, oprava, doklad) SELECT datum_dokl, uzaverka, u_cis, id_kas, id_man, castka, md, dal, md2, dal2, hs_md, hs_dal, pz, poznamka, druh, cen_hlad, dan_zakl, dan, faktura, zaloha, partner, .f. as oprava, doklad FROM pol WHERE exportuj AND castka<>0
		INSERT INTO u_ex_platby (datum_dokl, uzaverka, u_cis, id_kas, druh_pl, md, dal, md2, dal2, hs_md, hs_dal, castka, pz, poznamka, faktura, zaloha, partner, oprava, doklad) SELECT datum_dokl, uzaverka, u_cis, id_kas, druh_pl, md, dal, md2, dal2, hs_md, hs_dal, castka, pz, poznamka, faktura, zaloha, partner, .f. as oprava, doklad FROM plt WHERE exportuj AND castka<>0
	ELSE 
		SELECT datum_dokl, uzaverka, u_cis, id_kas, id_man, castka, md, dal, md2, dal2, hs_md, hs_dal, pz, poznamka, druh, cen_hlad, dan_zakl, dan, faktura, zaloha, partner, .f. as oprava, doklad FROM pol INTO CURSOR u_ex_polozky READWRITE  WHERE exportuj AND castka<>0
		SELECT datum_dokl, uzaverka, u_cis, id_kas, druh_pl, md, dal, md2, dal2, hs_md, hs_dal, castka, pz, poznamka, faktura, zaloha, partner, .f. as oprava, doklad FROM plt INTO CURSOR u_ex_platby READWRITE WHERE exportuj AND castka<>0
	ENDIF 
ENDIF 		
DO pozavirej WITH "pol, plt, fin, u_ex, u_ex_sum"
RETURN m.navrat
*************************************************************

PROCEDURE exp_pol
PRIVATE m.hladajidkas, m.hladajidman, m.hladajdruh, m.hladajdan, m.hladajcenhlad
SELECT pol
SELECT disti id_kas, id_man, druh, cen_hlad, dan FROM pol INTO CURSOR obrdruhy
SCAN ALL 
	m.hladajidkas=obrdruhy.id_kas
	m.hladajidman=obrdruhy.id_man
	m.hladajdruh=obrdruhy.druh
	m.hladajcenhlad=obrdruhy.cen_hlad
	m.hladajdan=obrdruhy.dan
	IF testuexpol(m.hladajidkas, m.hladajidman, m.hladajdruh, m.hladajcenhlad, m.hladajdan)
		SELECT uexpol1
		GO TOP 
		IF uexpol1.stare
			SELECT pol
			REPLACE md WITH uexpol1.md, dal WITH uexpol1.dal, hs_md WITH uexpol1.hs_md, hs_dal WITH uexpol1.hs_dal, md2 WITH uexpol1.md2, dal2 WITH uexpol1.dal2, ;
				co WITH uexpol1.co, pz WITH uexpol1.pz, poznamka WITH uexpol1.poznamka, exportuj WITH .t. FOR druh=m.hladajdruh AND id_kas=m.hladajidkas AND id_man=m.hladajidman AND cen_hlad=m.hladajcenhlad AND dan=m.hladajdan
		ELSE 
			m.novinka=.t.
		ENDIF 
	ELSE
		m.novinka=.t.
	ENDIF 
	SELECT obrdruhy
ENDSCAN 
USE IN obrdruhy
IF m.spotrebnadan<>0
	SELECT * FROM pol INTO CURSOR polspdan WHERE spdan<>0 READWRITE 
	REPLACE ALL exportuj WITH .f.
	REPLACE dan_zakl WITH ground(spdan/dan,2) FOR dan<>0
	SELECT DISTINCT id_kas, dan FROM polspdan INTO CURSOR obrdruhy
	SCAN ALL 
		m.hladajidkas=obrdruhy.id_kas
		m.hladajdan=obrdruhy.dan
		IF testuexpolspdan(m.hladajidkas, m.hladajdan)
			SELECT uexpol1
			GO TOP 
			IF uexpol1.stare
				SELECT polspdan
				REPLACE md WITH uexpol1.md, dal WITH uexpol1.dal, hs_md WITH uexpol1.hs_md, hs_dal WITH uexpol1.hs_dal, md2 WITH uexpol1.md2, dal2 WITH uexpol1.dal2, ;
					co WITH uexpol1.co, pz WITH uexpol1.pz, poznamka WITH uexpol1.poznamka, exportuj WITH .t. FOR id_kas=m.hladajidkas AND dan=m.hladajdan
			ELSE 
				m.novinka=.t.
			ENDIF 
		ELSE
			m.novinka=.t.
		ENDIF 
		SELECT obrdruhy
	ENDSCAN 	
	USE IN obrdruhy
	SELECT pol
	APPEND FROM DBF("polspdan")
	USE IN polspdan 
ENDIF 
SELECT pol
REPLACE ALL pz WITH STRTRAN(pz,"m.","")
REPLACE castka WITH EVALUATE(co), pz WITH EVALUATE(pz) FOR exportuj
SELECT pol
IF FSIZE("pohladavka")>0 AND FSIZE("text_link")>0
	REPLACE faktura WITH ALLTRIM(text_link) FOR pohladavka
ENDIF 



*************************************************************
PROCEDURE exp_plt
PRIVATE m.hladajidkas, m.hladajdruhpl
SELECT plt
SELECT disti id_kas, druh_pl FROM plt INTO CURSOR obrdruhy
SCAN ALL 
	m.hladajidkas=obrdruhy.id_kas
	m.hladajdruhpl=obrdruhy.druh_pl
	IF testuexplt(m.hladajidkas, m.hladajdruhpl)
		SELECT uexplt1
		GO TOP 
		IF uexplt1.stare
			SELECT plt
			REPLACE md WITH uexplt1.md, dal WITH uexplt1.dal, hs_md WITH uexplt1.hs_md, hs_dal WITH uexplt1.hs_dal, md2 WITH uexplt1.md2, dal2 WITH uexplt1.dal2, ;
				co WITH uexplt1.co, pz WITH uexplt1.pz, poznamka WITH uexplt1.poznamka, exportuj WITH .t. FOR druh_pl=m.hladajdruhpl AND id_kas=m.hladajidkas
		ELSE 
			m.novinka=.t.
		ENDIF 
	ELSE
		m.novinka=.t.
	ENDIF 
	SELECT obrdruhy
ENDSCAN 
USE IN obrdruhy
SELECT plt
REPLACE ALL pz WITH STRTRAN(pz,"m.","")
TRY 
	REPLACE castka WITH EVALUATE(co), pz WITH EVALUATE(pz) FOR exportuj
CATCH
ENDTRY 


*************************************************************
PROCEDURE priprava
* * * * * * * * * * * *    F I N    * * * * * * * * * * * * *
IF m.issql
	IF m.dennyexport
		IF LOWER(m.gcuct_typ)="kros" AND INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")
			lsql = "SELECT DISTINCT uzaverka, c_uzaverka, id_kas, uct_den FROM food600.sk__sez WHERE uctexport=0 AND SUBSTR(id_kas,1,1)='0'"
			lsql2 = "SELECT DISTINCT c_uzaverka FROM food600.sk__sez WHERE uctexport=0 AND SUBSTR(id_kas,1,1)='0'"
		ELSE 
			lsql = 'SELECT DISTINCT uzaverka, c_uzaverka, id_kas, uct_den FROM food600.sk__sez WHERE uctexport=0'
			lsql2 = 'SELECT DISTINCT c_uzaverka FROM food600.sk__sez WHERE uctexport=0'
		ENDIF 
	ELSE 
		IF LOWER(m.gcuct_typ)="kros" AND INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")
			lsql = 'SELECT DISTINCT uzaverka, c_uzaverka, id_kas, uct_den FROM food600.sk__sez WHERE uzaverka>=?m.uzaverka1 and uzaverka<=?m.uzaverka2'
			lsql2 = 'SELECT DISTINCT c_uzaverka FROM food600.sk__sez WHERE uzaverka>=?m.uzaverka1 and uzaverka<=?m.uzaverka2'
		ELSE 
			lsql = "SELECT DISTINCT uzaverka, c_uzaverka, id_kas, uct_den FROM food600.sk__sez WHERE to_char(uct_den,'YYYYMMDD')>=?m.uzaverka1 and to_char(uct_den,'YYYYMMDD')<=?m.uzaverka2"
			lsql2 = "SELECT DISTINCT c_uzaverka FROM food600.sk__sez WHERE to_char(uct_den,'YYYYMMDD')>=?m.uzaverka1 and to_char(uct_den,'YYYYMMDD')<=?m.uzaverka2"
		ENDIF 
	ENDIF 
	sql_select(lsql,'sk_sez',"uzaverka c(17), c_uzaverka I, id_kas c(2), uct_den D")
	lsql1="SELECT c_uzaverka, ucet, c_stredisk, c_karty, prachy, prachy_puv, dan, dan_sazba, cen_hlad, mnozstvi FROM food600.sk__pol where kompez=0 AND k_odpoctu<>'' AND c_uzaverka in ("+m.lsql2+")"
	sql_select(lsql1,'sk_pol',"c_uzaverka I, ucet I, c_stredisk I, c_karty I, prachy Y, prachy_puv Y, dan Y, dan_sazba N(5,2), cen_hlad c(2), mnozstvi Y")
	lsql1="SELECT c_uzaverka, ucet, druh_pl, cena_pl, mena, cena_mena, tip FROM food600.sk__plt where kompez=0 AND c_uzaverka in ("+m.lsql2+")"
	sql_select(lsql1,'sk_plt',"c_uzaverka I, ucet I, druh_pl C(8), cena_pl Y, mena C(3), cena_mena Y, tip Y")
	lsql1="SELECT c_uzaverka, ucet, text_link FROM food600.sk__fin where pohladavka=1 AND c_uzaverka in ("+m.lsql2+")"
	sql_select(lsql1,'sk_fin_pohl',"c_uzaverka I, ucet I, text_link C(25)")

	IF LOWER(m.gcuct_typ)="money"
		lsql = "SELECT DISTINCT uzaverka FROM food600.uexarsk"
		sql_select(lsql,'uexarsk',"uzaverka c(7)")
		DELETE * from sk_sez where uzaverka in (SELECT uzaverka from uexarsk)
		DELETE * from sk_pol where uzaverka in (SELECT uzaverka from uexarsk)
		DELETE * from sk_plt where uzaverka in (SELECT uzaverka from uexarsk)
		USE IN uexarsk
	ENDIF 
		
	u_ex_sk_had.set_had(10)

	SELECT sk_sez.uzaverka, sk_sez.id_kas, sk_pol.ucet, sk_pol.c_stredisk, sk_pol.c_karty, sk_pol.prachy as trzba, sk_pol.prachy_puv as trzba_puv, sk_pol.dan, ;
		$0 as dan_zakl, SPACE(7) as u_cis, sk_sez.uct_den as datum_dokl, sk_pol.dan_sazba/100+1 as dan_sazba, sk_pol.cen_hlad, karty.c_druh, karty.c_druhs,;
		karty.c_druhk, SPACE(m.sirkastlpcadruh) as druh, SPACE(m.sirkastlpcadruh) as spart, SPACE(2) as id_man, 00000 as spstupne, m.spotrebnadan as spdan, karty.objem, sk_pol.mnozstvi, sk_sez.c_uzaverka;
		FROM sk_pol LEFT JOIN sk_sez ON sk_pol.c_uzaverka=sk_sez.c_uzaverka LEFT JOIN karty ON sk_pol.c_karty=karty.c_karty INTO CURSOR pol1 READWRITE  WHERE !ISNULL(sk_pol.prachy)
	UPDATE pol1 SET druh=skupiny.druh, spart=skupiny.spart_kas from skupiny WHERE pol1.c_druhk=skupiny.c_druh
	UPDATE pol1 SET druh=skupiny.druh, spart=skupiny.spart_kas from skupiny WHERE pol1.c_druh=skupiny.c_druh AND (EMPTY(pol1.druh) OR ISNULL(pol1.druh))
	UPDATE pol1 SET druh=skupiny.druh, spart=skupiny.spart_kas from skupiny WHERE pol1.c_druhs=skupiny.c_druh AND (EMPTY(pol1.druh) OR ISNULL(pol1.druh))
	UPDATE pol1 SET id_man=food_dat.id from food_dat WHERE pol1.c_stredisk=food_dat.c_stredisk
	SELECT pol1
	REPLACE ALL  u_cis WITH id_kas+PADL(ALLTRIM(STR(ucet,5,0)),5,"0"), dan_zakl WITH trzba-dan
	IF m.spotrebnadan>0 
		USE (m.sqlprefix+"rspdanzoznam") ALIAS spdan IN 0
		UPDATE pol1 SET spstupne=spdan.koeficient from spdan WHERE pol1.c_karty=spdan.c_karty
		USE IN spdan
	ENDIF 
	IF LOWER(m.gcuct_pole)="spart"
		REPLACE ALL druh WITH spart
	ENDIF 
	SELECT id_kas, id_man, uzaverka, u_cis, druh, dan_sazba as dan, cen_hlad, datum_dokl, SUM(mnozstvi*spdan*objem*spstupne/100*dan_sazba) as spdan, ;
		SUM(trzba_puv) AS trzba_puv, SUM(trzba) AS trzba, SUM(dan_zakl) AS dan_zakl, 0000 AS uz_cislo, ;
		SPACE(8) as md, SPACE(8) as dal, SPACE(15) as hs_md, SPACE(15) as hs_dal, .f. as  exportuj, SPACE(15) as md2, SPACE(15) as dal2, ;
		SPACE(30) as co, SPACE(50) as pz, SPACE(30) as poznamka,$0 as castka, SPACE(20) as faktura, .f. as zaloha, SPACE(30) as partner, SPACE(15) as doklad, .f. as pohladavka, SPACE(25) as text_link, c_uzaverka, ucet ;
		GROUP BY uzaverka, u_cis, id_kas, id_man, druh, dan_sazba, cen_hlad ;
		FROM pol1 INTO CURSOR pol READWRITE 
	REPLACE spdan WITH 0 FOR ISNULL(spdan)
	REPLACE trzba WITH 0 FOR ISNULL(trzba)
	REPLACE ALL spdan WITH ground(spdan,2)
	REPLACE ALL trzba WITH trzba-spdan
	REPLACE dan_zakl WITH dan_zakl-ground(spdan/dan,2) FOR dan<>0 AND spdan<>0
	u_ex_sk_had.set_had(20)
	SELECT c_uzaverka, ucet, SUM(prachy_puv) as cena_celk, $0 as sleva_abs, SUM(prachy) as cena_slev, SUM(dan) as dph_celk, 0.000 as dan1, 0.000 as dan2, 0.000 as dan3, 0.000 as dan4, ;
		00000000.00 AS trzba_dph1, 00000000.00 AS trzba_dph2, 00000000.00 AS trzba_dph3, 00000000.00 AS trzba_dph4, "   " AS mena, .f. as pohladavka, SPACE(25) as text_link;
		FROM sk_pol INTO CURSOR fin READWRITE GROUP BY c_uzaverka, ucet WHERE !ISNULL(sk_pol.prachy)
	SELECT fin
	REPLACE ALL sleva_abs WITH cena_celk-cena_slev
	IF USED("sk_fin_pohl")
		UPDATE fin SET pohladavka=.t., text_link=sk_fin_pohl.text_link from sk_fin_pohl WHERE fin.c_uzaverka=sk_fin_pohl.c_uzaverka AND fin.ucet=sk_fin_pohl.ucet
		UPDATE pol SET pohladavka=.t., text_link=sk_fin_pohl.text_link from sk_fin_pohl WHERE pol.c_uzaverka=sk_fin_pohl.c_uzaverka AND pol.ucet=sk_fin_pohl.ucet
		USE IN sk_fin_pohl
	ENDIF 
	*** Kontrola jedinecnosti nastavenia povolenych sadzieb dph a vyplnenie trzba_dph1-4
	m.j=1
	m.sj="1"
	LOCAL m.uzbolo
	FOR m.i=1 TO 7
		m.uzbolo=.f.
		m.si=STR(m.i,1,0)
		IF m.i>1
			FOR m.k=1 TO m.i-1
				sk=STR(m.k,1,0)
				IF m.dph&si=m.dph&sk
					m.uzbolo=.t.
					EXIT
				ENDIF 
			ENDFOR 
		ENDIF 
		IF !m.uzbolo
			SELECT c_uzaverka, ucet, SUM(prachy) as suma FROM sk_pol INTO CURSOR podaniach WHERE dan_sazba=(m.dph&si-1)*100 AND !ISNULL(sk_pol.prachy) GROUP BY c_uzaverka, ucet
			IF RECCOUNT("podaniach")>0
				SELECT fin
				REPLACE ALL dan&sj WITH m.dph&si
				UPDATE fin SET trzba_dph&sj=podaniach.suma from podaniach WHERE fin.c_uzaverka=podaniach.c_uzaverka AND fin.ucet=podaniach.ucet
				m.j=m.j+1
				m.sj=STR(m.j,1,0)
			ENDIF 
			USE IN podaniach
		ENDIF 
	ENDFOR 
	u_ex_sk_had.set_had(30)

	SELECT sk_sez.uzaverka, sk_sez.id_kas, SPACE(7) as u_cis, sk_plt.druh_pl, sk_plt.cena_pl, sk_plt.tip, sk_plt.mena, sk_plt.cena_mena, sk_sez.uct_den AS datum_dokl, sk_plt.c_uzaverka, sk_plt.ucet,;
		SPACE(8) as md, SPACE(8) as dal, SPACE(15) as hs_md, SPACE(15) as hs_dal, .f. as  exportuj, SPACE(15) as md2, SPACE(15) as dal2, ;
		SPACE(30) as co, SPACE(50) as pz, SPACE(30) as poznamka,$0 as castka, SPACE(20) as faktura, .f. as zaloha, SPACE(30) as partner, SPACE(15) as doklad, ; 
		00000000.00 AS trzba_dph1, 00000000.00 AS trzba_dph2, 00000000.00 AS trzba_dph3, 00000000.00 AS trzba_dph4 FROM sk_plt LEFT JOIN sk_sez ON sk_plt.c_uzaverka=sk_sez.c_uzaverka;
		INTO CURSOR plt READWRITE WHERE !ISNULL(sk_plt.cena_pl)
	SELECT plt
	REPLACE ALL  u_cis WITH id_kas+PADL(ALLTRIM(STR(ucet,5,0)),5,"0")
	UPDATE plt SET trzba_dph1=fin.trzba_dph1*plt.cena_pl/fin.cena_slev, trzba_dph2=fin.trzba_dph2*plt.cena_pl/fin.cena_slev, trzba_dph3=fin.trzba_dph3*plt.cena_pl/fin.cena_slev, trzba_dph4=fin.trzba_dph4*plt.cena_pl/fin.cena_slev from fin;
		WHERE plt.c_uzaverka=fin.c_uzaverka AND plt.ucet=fin.ucet AND fin.cena_slev<>0
	u_ex_sk_had.set_had(40)
ELSE 
	IF m.dennyexport
		IF LOWER(m.gcuct_typ)="kros" AND INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")
			SELECT DISTI uzaverka, id_kas, .f. as vyber, .f. as prenos FROM sk_sez WHERE !uctexport AND LEFT(id_kas,1)="0" INTO CURSOR uzky READWRITE 
		ELSE
			SELECT DISTI uzaverka FROM sk_sez WHERE !uctexport INTO CURSOR uzky READWRITE 
		ENDIF			
	ELSE
		IF (LOWER(m.gcuct_typ)="kros" AND INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")) OR LOWER(m.gcuct_typ)="orion"
			SELECT DISTINCT uzaverka FROM sk_sez WHERE BETWEEN(sk_sez.uzaverka, m.uzaverka1, m.uzaverka2) INTO CURSOR uzky READWRITE 
		ELSE
			SELECT DISTINCT uzaverka FROM sk_sez WHERE BETWEEN(DTOS(uct_den), m.uzaverka1, m.uzaverka2) INTO CURSOR uzky READWRITE 
			IF LOWER(m.gcuct_typ)="money"
				IF FILE(m.gccen_path+"uexarsk.dbf")
					USE (m.gccen_path+"uexarsk.dbf") IN 0
					DELETE FROM uzky WHERE uzaverka IN (SELECT uzaverka FROM uexarsk)
				ENDIF 
			ENDIF 
		ENDIF 
	ENDIF 
	SELECT id_kas, uzaverka, u_cis, cena_celk, sleva_abs, cena_slev, dan1, dan2, dan3, dan4, ;
		trzba_dph1, trzba_dph2, trzba_dph3, trzba_dph4, "   " AS mena, round50,;
		00000000.00 AS trzba1, 00000000.00 AS trzba2, 00000000.00 AS trzba3, 00000000.00 AS trzba4 ;
		WHERE (uzaverka in (SELECT uzaverka FROM uzky)) ;
		FROM sk_fin INTO CURSOR fin1 READWRITE 
	SELECT uzaverka, u_cis, pohladavka, text_link WHERE pohladavka AND (uzaverka in (SELECT uzaverka FROM uzky)) ;
		FROM sk_fin INTO CURSOR sk_fin_pohl READWRITE 

	u_ex_sk_had.set_had(10)
	SELECT fin1
	SCAN
		FOR iii=1 TO 4
			iiii = STR(iii, 1)
			dph_xx = m.dph&iiii
			pole_dbf = "trzba" + iiii
			DO CASE
				CASE dan1 = m.dph_xx
					REPL (pole_dbf) WITH trzba_dph1
				CASE dan2 = m.dph_xx
					REPL (pole_dbf) WITH trzba_dph2
				CASE dan3 = m.dph_xx
					REPL (pole_dbf) WITH trzba_dph3
				CASE dan4 = m.dph_xx
					REPL (pole_dbf) WITH trzba_dph4
			ENDCASE
		ENDFOR
	ENDSCAN

	SELECT id_kas, uzaverka, u_cis, 00.00000000000 AS slev_koef, ;
		SUM(cena_celk) AS trzba_puv, SUM(cena_slev+round50) AS trzba, ;
		SUM(trzba1) AS trzba_dph1, SUM(trzba2) AS trzba_dph2, SUM(trzba3) AS trzba_dph3, SUM(trzba4) AS trzba_dph4, 0000000000.00 as kompezsuma, .f. as pohladavka, SPACE(25) as text_link ;
		GROUP BY 1, 2, 3 FROM fin1 INTO CURSOR fin READWRITE 

	REPLACE ALL slev_koef WITH IIF(trzba_puv # 0, trzba / trzba_puv, 1)
	INDEX ON uzaverka + u_cis TAG uz_ucet


	* * * * * * * * * * * *    P O L    * * * * * * * * * * * * *
	IF !USED("skupiny")
		USE (m.src_path + "skupiny") ORDER c_druh IN 0 ALIAS skupiny AGAIN 
	ENDIF 
	IF !USED("karty")
		USE (m.src_path + "karty") ORDER c_karty ALIAS karty AGAIN IN 0
	ENDIF 
	
	IF LOWER(m.gcuct_typ)="kros" AND INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")
		SELECT id_kas, id_man, uzaverka, u_cis, dan, cen_hlad, mnozstvi, SPACE(m.sirkastlpcadruh) AS druh, prachy, 00000 as spstupne, m.spotrebnadan as spdan, 00000000.00 as objem, c_karty, 0000000000 as c_druh, kompez ;
			WHERE (uzaverka in (SELECT DISTINCT uzaverka FROM uzky)) AND !EMPTY(k_odpoctu) ;
			FROM sk_pol INTO CURSOR pol1 READWRITE 
	ELSE
		SELECT id_kas, id_man, uzaverka, u_cis, dan, cen_hlad, mnozstvi, SPACE(m.sirkastlpcadruh) AS druh, prachy, 00000 as spstupne, m.spotrebnadan as spdan, 00000000.00 as objem, c_karty, 0000000000 as c_druh, kompez ;
			WHERE (uzaverka in (SELECT DISTINCT uzaverka FROM uzky)) AND !EMPT(k_odpoctu) ;
			FROM sk_pol INTO CURSOR pol1 READWRITE 
	ENDIF
	UPDATE pol1 SET c_druh=karty.c_druhk, objem=karty.objem from karty WHERE pol1.c_karty=karty.c_karty

	*  *  *  *  uzije se jedno nebo druhe  *  *  *  *
	IF LOWER(m.gcuct_pole)="druh"
		UPDATE pol1 SET druh=skupiny.druh from skupiny WHERE pol1.c_druh=skupiny.c_druh
	ELSE
		UPDATE pol1 SET druh=skupiny.spart from skupiny WHERE pol1.c_druh=skupiny.c_druh
	ENDIF 
	REPLACE druh WITH "____________" FOR EMPTY(druh)
	IF m.spotrebnadan>0 .and. FILE(m.fn_spotrebnadan+".dbf")
		USE (fn_spotrebnadan) ALIAS spdan ORDER c_karty IN 0
		SELECT pol1
		SET RELATION TO c_karty INTO spdan
		REPLACE pol1.spstupne WITH spdan.koeficient FOR !EMPTY(spdan.c_karty)
		SET RELATION OFF INTO spdan
		USE IN spdan
	ENDIF 
	SELECT id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, DATE() AS datum_dokl, SUM(mnozstvi*spdan*objem*spstupne/100*dan) as spdan, ;
		SPACE(8) as md, SPACE(8) as dal, SPACE(15) as hs_md, SPACE(15) as hs_dal, .f. as  exportuj, SPACE(15) as md2, SPACE(15) as dal2, ;
		SPACE(30) as co, SPACE(50) as pz, SPACE(30) as poznamka,$0 as castka, SPACE(20) as faktura, .f. as zaloha, SPACE(30) as partner, SPACE(15) as doklad,;
		SUM(prachy) AS trzba_puv, 00000000.00 AS trzba, 00000000.0000 AS dan_zakl, SPACE(8) AS druh_pl, 0000 AS uz_cislo, kompez, .f. as pohladavka, SPACE(25) as text_link ;
		GROUP BY uzaverka, u_cis, id_man, druh, dan, cen_hlad, kompez ;
		FROM pol1 INTO CURSOR pol READWRITE 
	REPLACE ALL spdan WITH ground(spdan,2)

	IF USED("sk_fin_pohl")
		UPDATE fin SET pohladavka=.t., text_link=sk_fin_pohl.text_link from sk_fin_pohl WHERE fin.uzaverka=sk_fin_pohl.uzaverka AND fin.u_cis=sk_fin_pohl.u_cis
		UPDATE pol SET pohladavka=.t., text_link=sk_fin_pohl.text_link from sk_fin_pohl WHERE pol.uzaverka=sk_fin_pohl.uzaverka AND pol.u_cis=sk_fin_pohl.u_cis
		USE IN sk_fin_pohl
	ENDIF 



	SELECT uzaverka, u_cis, trzba_puv as prachy FROM pol WHERE kompez AND (uzaverka+u_cis in (SELECT uzaverka+u_cis FROM fin WHERE slev_koef<>1)) INTO CURSOR komsleva
	IF RECCOUNT("komsleva")>0
		UPDATE fin SET kompezsuma=-komsleva.prachy from komsleva WHERE fin.uzaverka=komsleva.uzaverka AND fin.u_cis=komsleva.u_cis
		UPDATE fin SET slev_koef =IIF((trzba_puv+kompezsuma) # 0, (trzba+kompezsuma) / (trzba_puv+kompezsuma), 1) WHERE kompezsuma<>0
	ENDIF 
	DO pozavirej WITH "komsleva"
	u_ex_sk_had.set_had(20)

	SELECT sk_sez
	INDEX ON uzaverka + id_kas TO (m.gcpri_path + "uzuniq") UNIQ
	SELECT pol
	SET RELATION TO uzaverka + id_kas INTO sk_sez
	REPLACE ALL uz_cislo WITH sk_sez.uz_cislo, datum_dokl WITH sk_sez.uct_den

	SET RELATION TO uzaverka + u_cis INTO fin
	REPLACE trzba WITH ROUND(trzba_puv * fin.slev_koef, 2) FOR !kompez
	REPLACE trzba WITH trzba_puv FOR kompez
	

	SET RELATION TO
	DELETE FOR trzba = 0
	INDEX ON uzaverka + u_cis TAG uz_uc
	* * * * *    oprava pripadnych zaokrouhlovacich rozdilu     * * * * *
	SELECT uzaverka, u_cis, SUM(trzba) AS oprava GROUP BY uzaverka, u_cis ;
		FROM pol INTO CURSOR opr_cen READWRITE 
	SET RELATION TO uzaverka + u_cis INTO fin
	REPLACE ALL oprava WITH fin.trzba - opr_cen.oprava
	SET RELATION OFF INTO fin
	SCAN FOR oprava # 0
		SELECT pol
		LOCATE FOR uzaverka + u_cis = opr_cen.uzaverka + opr_cen.u_cis
		REPLACE trzba WITH trzba + opr_cen.oprava
		SELECT opr_cen
	ENDSCAN

	SELECT pol
	SET ORDER TO
	REPLACE ALL trzba WITH trzba-spdan
	REPLACE ALL dan_zakl WITH trzba / dan
	DELETE FOR kompez
	
	u_ex_sk_had.set_had(30)

	* * * * * * * * * * * *    P L T    * * * * * * * * * * * * *
	IF LOWER(m.gcuct_typ)="kros" AND INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")
		SELECT uzaverka, u_cis, druh_pl, cena_pl, tip, mena, cena_mena, LEFT(u_cis, 2) AS id_kas, DATE() AS datum_dokl, ;
			SPACE(8) as md, SPACE(8) as dal, SPACE(15) as hs_md, SPACE(15) as hs_dal, .f. as  exportuj, SPACE(15) as md2, SPACE(15) as dal2, ;
			SPACE(30) as co, SPACE(50) as pz, SPACE(30) as poznamka,$0 as castka, SPACE(20) as faktura, .f. as zaloha, SPACE(30) as partner, SPACE(15) as doklad, ;
			00000000.00 AS trzba_dph1, 00000000.00 AS trzba_dph2, 00000000.00 AS trzba_dph3, 00000000.00 AS trzba_dph4, kompez ;
			WHERE (uzaverka in (SELECT DISTINCT uzaverka FROM uzky)) ;
			FROM sk_plt INTO  CURSOR plt READWRITE 
	ELSE
		SELECT uzaverka, u_cis, druh_pl, cena_pl, tip, mena, cena_mena, LEFT(u_cis, 2) AS id_kas, DATE() AS datum_dokl, ;
			SPACE(8) as md, SPACE(8) as dal, SPACE(15) as hs_md, SPACE(15) as hs_dal, .f. as  exportuj, SPACE(15) as md2, SPACE(15) as dal2, ;
			SPACE(30) as co, SPACE(50) as pz, SPACE(30) as poznamka,$0 as castka, SPACE(20) as faktura, .f. as zaloha, SPACE(30) as partner, SPACE(15) as doklad, ;
			00000000.00 AS trzba_dph1, 00000000.00 AS trzba_dph2, 00000000.00 AS trzba_dph3, 00000000.00 AS trzba_dph4, kompez ;
			WHERE (uzaverka in (SELECT DISTINCT uzaverka FROM uzky)) ;
			FROM sk_plt INTO CURSOR plt READWRITE 
	ENDIF

	UPDATE plt SET datum_dokl=sk_sez.uct_den from sk_sez WHERE plt.uzaverka=sk_sez.uzaverka
	SET RELATION TO uzaverka + u_cis INTO fin
	REPLACE FOR fin.trzba # 0 trzba_dph1 WITH fin.trzba_dph1 * cena_pl / fin.trzba, ;
		trzba_dph2 WITH fin.trzba_dph2 * cena_pl / fin.trzba, ;
		trzba_dph3 WITH fin.trzba_dph3 * cena_pl / fin.trzba, ;
		trzba_dph4 WITH fin.trzba_dph4 * cena_pl / fin.trzba
	SET RELATION TO
	DELETE FOR kompez
	u_ex_sk_had.set_had(40)
ENDIF 
DO pozavirej WITH "karty, opr_cen, pol1, sk_pol, sk_plt, fin1, sk_fin, sk_sez, skupiny"



PROCEDURE hores_pol_niefakt
IF m.gcuct_rastre
	SELECT hfak 
	SCAN FOR hfak.plac<>"F" .AND. DTOS(hfak.datum)>=LEFT(m.uuz1,8) .AND. DTOS(hfak.datum)<=LEFT(m.uuz2,8) .and. (!EMPTY(hfak.ucet) .or. LEFT(hfak.pokoj,4)="Pkl.")
*		SCATTER MEMVAR
		IF LEFT(hfak.pokoj,4)="Pkl."
			IF LOWER(m.gcuct_typ)="orion" AND hfak.plac="X"
			ELSE 
				m.sd1=VAL(hfak.kod1)/100+1
				IF m.sd1>=1.18 AND m.sd1<=1.26
					m.sd1=1.20
				ENDIF 
				doklad		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
				m.datum_dokl= hfak.datum
				datum		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
				IF testuexpolhores(RIGHT(hfak.pokoj,2),m.sd1)
					SELECT uexpol1
					SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
					m.faktura	= ""
					id_man		= "WH"
					IF (LOWER(m.gcuct_typ)="magma")
						partner		= "WH"
					ELSE
						partner		= hfak.subnab
					ENDIF 
					TRY
					 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
					 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
					 	ELSE 
							pz = EVALUATE(uexpol1.pz)
						ENDIF 
					CATCH
						pz = PADR(DTOS(m.datum_dokl),17)
					ENDTRY 
					m.co="hfak."+m.co
					TRY 
						castka = &co
					CATCH
						castka = 0
					ENDTRY 
					IF m.castka # 0
						IF !EMPTY(m.alto_predplatby)
							TRY  	
								IF INLIST(ALLTRIM(RIGHT(hfak.pokoj,2)),&alto_predplatby)
									m.poznamka=STRTRAN(m.poznamka,ALLTRIM(RIGHT(hfak.pokoj,2)),"ZALOHA")
								ENDIF 
							CATCH
								m.poznamka=""
							ENDTRY 
						ENDIF 
						INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
							("WH", "WH", DTOS(hfak.datum), m.doklad, RIGHT(hfak.pokoj,2), m.sd1, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
					ENDIF 
				ELSE 
					INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
						("WH", "WH", DTOS(hfak.datum), m.doklad, RIGHT(hfak.pokoj,2), m.sd1, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
				ENDIF
			ENDIF 
		ELSE 
			SELECT hfau
			SCAN FOR sequc=hfak.sequc .AND. ALLTRIM(kod1)=ALLTRIM(hfak.plac)
				m.sd1=ROUND(100*hfau.pole1/(hfau.castka-hfau.pole1),0)/100+1
				IF m.sd1>=1.15 AND m.sd1<=1.26
					m.sd1=1.20
				ENDIF 
				doklad		= ALLTRIM(STR(hfau.ucet,10,0))+" "+ALLTRIM(STR(hfau.sequc,10,0))
				m.datum_dokl= hfau.datum
				datum		= ALLTRIM(STR(hfau.ucet,10,0))+" "+ALLTRIM(STR(hfau.sequc,10,0))

				IF testuexpolhores(hfau.rkod,m.sd1)
					SELECT uexpol1
					SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
					m.faktura	= ""
					id_man		= "WH"
					IF (LOWER(m.gcuct_typ)="magma")
						partner		= "WH"
					ELSE
						partner		= hfak.subnab
					ENDIF 
				TRY
				 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
				 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
				 	ELSE 
						pz = EVALUATE(uexpol1.pz)
					ENDIF 
				CATCH
					pz = PADR(DTOS(m.datum_dokl),17)
				ENDTRY 
					m.co="hfau."+m.co
					TRY 
						castka = &co
					CATCH
						castka = 0
					ENDTRY 
					IF m.castka # 0
						IF !EMPTY(m.alto_predplatby)
							TRY 
								IF  INLIST(ALLTRIM(hfau.rkod),&alto_predplatby)
									m.poznamka=STRTRAN(m.poznamka,ALLTRIM(hfau.rkod),"ZALOHA")
								ENDIF 
							CATCH
								m.poznamka=""
							ENDTRY 
						ENDIF 
						INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
							("WH", "WH", DTOS(hfak.datum), m.doklad, hfau.rkod, m.sd1, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
					ENDIF 
				ELSE 
					INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
						("WH", "WH", DTOS(hfak.datum), m.doklad, hfau.rkod, m.sd1, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
				ENDIF
				SELECT hfau
			ENDSCAN
		ENDIF 
		SELECT hfak
	ENDSCAN 
ELSE
	SELECT hfak 
	SCAN FOR hfak.plac<>"F" .AND. DTOS(hfak.datum)>=LEFT(m.uuz1,8) .AND. DTOS(hfak.datum)<=LEFT(m.uuz2,8) .and. !EMPTY(hfak.ucet)
		doklad		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
		m.datum_dokl= hfak.datum
		datum		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
		m.ucet=hfak.ucet
		IF !EMPTY(m.citytyp)
			SELECT SUM(castka) as zfau FROM hfau WHERE ucet = m.ucet .and. rkod=m.gcuct_city INTO CURSOR castkafau
			m.zfau=castkafau.zfau
			USE 
			DO CASE 
				CASE m.citytyp="S"
					SELECT SUM(strava) as zfak FROM hfak WHERE ucet = m.ucet INTO CURSOR castkafak
				CASE m.citytyp="O"
					SELECT SUM(ostat) as zfak FROM hfak WHERE ucet = m.ucet INTO CURSOR castkafak
				CASE m.citytyp="U"
					SELECT SUM(ubyt) as zfak FROM hfak WHERE ucet = m.ucet INTO CURSOR castkafak
			ENDCASE 
			m.zfak=castkafak.zfak
			USE 
		ENDIF 
		IF testuexpolhores("Strav",1+VAL(hfak.kod1)/100,"Strava","hfak.strav")
			SELECT uexpol1
			SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
			m.faktura	= ""
			id_man		= "WH"
			IF (LOWER(m.gcuct_typ)="magma")
				partner		= "WH"
			ELSE
				partner		= hfak.subnab
			ENDIF 
			TRY
			 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
			 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
			 	ELSE 
					pz = EVALUATE(uexpol1.pz)
				ENDIF 
			CATCH
				pz = PADR(DTOS(m.datum_dokl),17)
			ENDTRY 
			TRY 
				castka = &co
			CATCH
				castka = 0
			ENDTRY 
			IF m.citytyp="S"
				IF m.zfak<>0
					m.castka=m.castka*(1-(m.zfau/m.zfak))
				ENDIF 
			ENDIF 
			IF m.castka # 0 
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
					("WH", "WH", DTOS(hfak.datum), m.doklad, "Strav", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
			ENDIF 
		ELSE 
			INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
				("WH", "WH", DTOS(hfak.datum), m.doklad, "Strav", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
		ENDIF 
		IF testuexpolhores("Ubyt",1+VAL(hfak.kod1)/100,"Ubytovanie","hfak.ubyt")
			SELECT uexpol1
			SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
			m.faktura	= ""
			id_man		= "WH"
			IF (LOWER(m.gcuct_typ)="magma")
				partner		= "WH"
			ELSE
				partner		= hfak.subnab
			ENDIF 
			TRY
			 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
			 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
			 	ELSE 
					pz = EVALUATE(uexpol1.pz)
				ENDIF 
			CATCH
				pz = PADR(DTOS(m.datum_dokl),17)
			ENDTRY 
			TRY 
				castka = &co
			CATCH
				castka = 0
			ENDTRY 
			IF m.citytyp="U"
				IF m.zfak<>0
					m.castka=m.castka*(1-(m.zfau/m.zfak))
				ENDIF 
			ENDIF 
			IF m.castka # 0 
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
					("WH", "WH", DTOS(hfak.datum), m.doklad, "Ubyt", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
			ENDIF 
		ELSE 
			INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
				("WH", "WH", DTOS(hfak.datum), m.doklad, "Ubyt", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
		ENDIF 
		IF testuexpolhores("Ostat",1+VAL(hfak.kod1)/100,"Ostatne","hfak.ostat")
			SELECT uexpol1
			SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
			m.faktura	= ""
			id_man		= "WH"
			IF (LOWER(m.gcuct_typ)="magma")
				partner		= "WH"
			ELSE
				partner		= hfak.subnab
			ENDIF 
			TRY
			 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
			 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
			 	ELSE 
					pz = EVALUATE(uexpol1.pz)
				ENDIF 
			CATCH
				pz = PADR(DTOS(m.datum_dokl),17)
			ENDTRY 
			TRY 
				castka = &co
			CATCH
				castka = 0
			ENDTRY 
			IF m.citytyp="O"
				IF m.zfak<>0
					m.castka=m.castka*(1-(m.zfau/m.zfak))
				ENDIF 
			ENDIF 
			IF m.castka # 0 
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
					("WH", "WH", DTOS(hfak.datum), m.doklad, "Ostat", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
			ENDIF 
		ELSE 
			INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
				("WH", "WH", DTOS(hfak.datum), m.doklad, "Ostat", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
		ENDIF 
		IF !EMPTY(m.citytyp)
			SELECT hras
			LOCATE FOR rkod=m.gcuct_city
			IF 	testuexpolhores("City",1+(hras.dan)/100,"City tax","suma")
				SELECT uexpol1
				SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
				m.faktura	= ""
				id_man		= "WH"
				IF (LOWER(m.gcuct_typ)="magma")
					partner		= "WH"
				ELSE
					partner		= hfak.subnab
				ENDIF 
				doklad		= ALLTRIM(STR(m.ucet,10,0))+" "+ALLTRIM(STR(m.sequc,10,0))
				m.datum_dokl= hfak.datum
				datum		= ALLTRIM(STR(m.ucet,10,0))+" "+ALLTRIM(STR(m.sequc,10,0))
				TRY
				 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
				 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
				 	ELSE 
						pz = EVALUATE(uexpol1.pz)
					ENDIF 
				CATCH
					pz = PADR(DTOS(m.datum_dokl),17)
				ENDTRY 
				IF m.zfak<>0
					DO CASE 
						CASE m.citytyp="S"
							m.castka=hfak.strava*m.zfau/m.zfak
						CASE m.citytyp="O"
							m.castka=hfak.ostat*m.zfau/m.zfak
						CASE m.citytyp="U"
							m.castka=hfak.ubyt*m.zfau/m.zfak
					ENDCASE 
				ELSE 
					m.castka=0
				ENDIF 
				IF m.castka # 0 
					INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
						("WH", "WH", DTOS(hfak.datum), m.doklad, "City", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
				ENDIF 
			ELSE 
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
					("WH", "WH", DTOS(hfak.datum), m.doklad, "City", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
			ENDIF 
		ENDIF 
		SELECT hfak
	ENDSCAN
ENDIF 

PROCEDURE hores_pol_fakt
IF m.gcuct_rastre
	SELECT hfak 
	SCAN FOR hfak.plac="F" .AND. DTOS(hfak.datum)>=LEFT(m.uuz1,8) .AND. DTOS(hfak.datum)<=LEFT(m.uuz2,8) .and. (!EMPTY(hfak.ucet) .or. LEFT(hfak.pokoj,4)="Pkl.")
		doklad		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
		m.datum_dokl= hfak.datum
		datum		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
		m.ucet=hfak.ucet
		SELECT hf_rad
		LOCATE FOR sequc=hfak.sequc
		IF FOUND()
			m.hf_cislo=hf_rad.cislo
		ELSE
			m.hf_cislo=""
		ENDIF 
		IF LEFT(hfak.pokoj,4)="Pkl."
			m.sd1=VAL(hfak.kod1)/100+1
			SELECT hras
			LOCATE FOR rkod=RIGHT(hfak.pokoj,2)
			IF FOUND()
				m.zaloha=(hras.rdruh="P")
			ELSE
				m.zaloha=.f.
			ENDIF 
			IF testuexpolhores(RIGHT(hfak.pokoj,2),m.sd1)
				SELECT uexpol1
				SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
*				m.faktura	= ""
				id_man		= "WH"
				IF (LOWER(m.gcuct_typ)="magma")
					IF EMPTY(hfak.subnab) .or. ALLTRIM(hfak.subnab)="P"
						partner=hfak.jmeno
					ELSE
						partner=hfak.subnab
					ENDIF 
				ELSE
					partner		= hfak.subnab
				ENDIF 
				TRY
				 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
				 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
				 	ELSE 
						pz = EVALUATE(uexpol1.pz)
					ENDIF 
				CATCH
					pz = PADR(DTOS(m.datum_dokl),17)
				ENDTRY 
				m.co="hfak."+m.co
				TRY 
					castka = &co
				CATCH
					castka = 0
				ENDTRY 
				IF m.castka # 0 .and. !(EMPTY(md) .and. EMPTY(dal) .and. EMPTY(md2) .and. EMPTY(dal2))
					IF !EMPTY(m.alto_predplatby) .AND. INLIST(ALLTRIM(RIGHT(hfak.pokoj,2)),&alto_predplatby)
						m.poznamka=STRTRAN(m.poznamka,ALLTRIM(RIGHT(hfak.pokoj,2)),"ZALOHA")
					ENDIF 
					INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, zaloha, doklad) VALUES ;
						("WH", "WH", DTOS(hfak.datum), m.doklad, RIGHT(hfak.pokoj,2), m.sd1, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.zaloha, m.doklad)
				ENDIF 
			ELSE 
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
					("WH", "WH", DTOS(hfak.datum), m.doklad, RIGHT(hfak.pokoj,2), m.sd1, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
			ENDIF
		ELSE 
			SELECT hfau
			SCAN FOR sequc=hfak.sequc
				m.sd1=ROUND(100*hfau.pole1/(hfau.castka-hfau.pole1),0)/100+1
				IF m.sd1>=1.15 AND m.sd1<=1.26
					m.sd1=1.20
				ENDIF 
				IF testuexpolhores(hfau.rkod,m.sd1)
					SELECT uexpol1
					SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
					m.faktura	= m.hf_cislo
					id_man		= "WH"
					IF (LOWER(m.gcuct_typ)="magma")
						IF EMPTY(hfak.subnab) .or. ALLTRIM(hfak.subnab)="P"
							partner=hfak.jmeno
						ELSE
							partner=hfak.subnab
						ENDIF 
					ELSE
						partner		= hfak.subnab
					ENDIF 
					TRY
					 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
					 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
					 	ELSE 
							pz = EVALUATE(uexpol1.pz)
						ENDIF 
					CATCH
						pz = PADR(DTOS(m.datum_dokl),17)
					ENDTRY 
					m.co="hfau."+m.co
					TRY 
						castka = &co
					CATCH
						castka = 0
					ENDTRY 
					IF m.castka # 0 .and. !(EMPTY(md) .and. EMPTY(dal) .and. EMPTY(md2) .and. EMPTY(dal2))
						IF !EMPTY(m.alto_predplatby) .AND. INLIST(ALLTRIM(hfau.rkod),&alto_predplatby)
							m.poznamka=STRTRAN(m.poznamka,ALLTRIM(hfau.rkod),"ZALOHA")
						ENDIF 
						INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
							("WH", "WH", DTOS(hfak.datum), m.doklad, hfau.rkod, m.sd1, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
					ENDIF 
				ELSE 
					INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
						("WH", "WH", DTOS(hfak.datum), m.doklad, hfau.rkod, m.sd1, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
				ENDIF
				SELECT hfau
			ENDSCAN
		ENDIF 
		SELECT hfak
	ENDSCAN 
ELSE
	SELECT hfak 
	SCAN FOR hfak.plac="F" .AND. DTOS(hfak.datum)>=LEFT(m.uuz1,8) .AND. DTOS(hfak.datum)<=LEFT(m.uuz2,8) && .and. !EMPTY(hfak.ucet)
		doklad		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
		m.datum_dokl= hfak.datum
		datum		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
		m.ucet=hfak.ucet
		SELECT hf_rad
		LOCATE FOR sequc=hfak.sequc
		IF FOUND()
			m.hf_cislo=hf_rad.cislo
		ELSE
			m.hf_cislo=""
		ENDIF 
		IF !EMPTY(m.citytyp)
			SELECT SUM(castka) as zfau FROM hfau WHERE ucet = m.ucet .and. rkod=m.gcuct_city INTO CURSOR castkafau
			m.zfau=castkafau.zfau
			USE 
			DO CASE 
				CASE m.citytyp="S"
					SELECT SUM(strava) as zfak FROM hfak WHERE ucet = m.ucet INTO CURSOR castkafak
				CASE m.citytyp="O"
					SELECT SUM(ostat) as zfak FROM hfak WHERE ucet = m.ucet INTO CURSOR castkafak
				CASE m.citytyp="U"
					SELECT SUM(ubyt) as zfak FROM hfak WHERE ucet = m.ucet INTO CURSOR castkafak
			ENDCASE 
			m.zfak=castkafak.zfak
			USE 
		ENDIF 
		IF testuexpolhores("Strav",1+VAL(hfak.kod1)/100,"Strava","hfak.strav")
			SELECT uexpol1
			SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
			m.faktura	= m.hf_cislo
			id_man		= "WH"
			IF (LOWER(m.gcuct_typ)="magma")
				IF EMPTY(hfak.subnab) .or. ALLTRIM(hfak.subnab)="P"
					partner=hfak.jmeno
				ELSE
					partner=hfak.subnab
				ENDIF 
			ELSE
				partner		= hfak.subnab
			ENDIF 
			TRY
			 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
			 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
			 	ELSE 
					pz = EVALUATE(uexpol1.pz)
				ENDIF 
			CATCH
				pz = PADR(DTOS(m.datum_dokl),17)
			ENDTRY 
			TRY 
				castka = &co
			CATCH
				castka = 0
			ENDTRY 
			IF m.citytyp="S"
				IF m.zfak<>0
					m.castka=m.castka*(1-(m.zfau/m.zfak))
				ENDIF 
			ENDIF 
			IF m.castka # 0 
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
					("WH", "WH", DTOS(hfak.datum), m.doklad, "Strav", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
			ENDIF 
		ELSE 
			INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
				("WH", "WH", DTOS(hfak.datum), m.doklad, "Strav", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
		ENDIF 
		IF testuexpolhores("Ubyt",1+VAL(hfak.kod1)/100,"Ubytovanie","hfak.ubyt")
			SELECT uexpol1
			SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
			m.faktura	= m.hf_cislo
			id_man		= "WH"
			IF (LOWER(m.gcuct_typ)="magma")
				IF EMPTY(hfak.subnab) .or. ALLTRIM(hfak.subnab)="P"
					partner=hfak.jmeno
				ELSE
					partner=hfak.subnab
				ENDIF 
			ELSE
				partner		= hfak.subnab
			ENDIF 
			TRY
			 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
			 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
			 	ELSE 
					pz = EVALUATE(uexpol1.pz)
				ENDIF 
			CATCH
				pz = PADR(DTOS(m.datum_dokl),17)
			ENDTRY 
			TRY 
				castka = &co
			CATCH
				castka = 0
			ENDTRY 
			IF m.citytyp="U"
				IF m.zfak<>0
					m.castka=m.castka*(1-(m.zfau/m.zfak))
				ENDIF 
			ENDIF 
			IF m.castka # 0 
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
					("WH", "WH", DTOS(hfak.datum), m.doklad, "Ubyt", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
			ENDIF 
		ELSE 
			INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
				("WH", "WH", DTOS(hfak.datum), m.doklad, "Ubyt", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
		ENDIF 
		IF testuexpolhores("Ostat",1+VAL(hfak.kod1)/100,"Ostatne","hfak.ostat")
			SELECT uexpol1
			SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
			m.faktura	= m.hf_cislo
			id_man		= "WH"
			IF (LOWER(m.gcuct_typ)="magma")
				IF EMPTY(hfak.subnab) .or. ALLTRIM(hfak.subnab)="P"
					partner=hfak.jmeno
				ELSE
					partner=hfak.subnab
				ENDIF 
			ELSE
				partner		= hfak.subnab
			ENDIF 
			TRY
			 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
			 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
			 	ELSE 
					pz = EVALUATE(uexpol1.pz)
				ENDIF 
			CATCH
				pz = PADR(DTOS(m.datum_dokl),17)
			ENDTRY 
			TRY 
				castka = &co
			CATCH
				castka = 0
			ENDTRY 
			IF m.citytyp="O"
				IF m.zfak<>0
					m.castka=m.castka*(1-(m.zfau/m.zfak))
				ENDIF 
			ENDIF 
			IF m.castka # 0 
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
					("WH", "WH", DTOS(hfak.datum), m.doklad, "Ostat", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
			ENDIF 
		ELSE 
			INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
				("WH", "WH", DTOS(hfak.datum), m.doklad, "Ostat", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
		ENDIF 
		IF !EMPTY(m.citytyp)
			SELECT hras
			LOCATE FOR rkod=m.gcuct_city
			IF 	testuexpolhores("City",1+(hras.dan)/100,"City tax","suma")
				SELECT uexpol1
				SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
				m.faktura	= m.hf_cislo
				id_man		= "WH"
				IF (LOWER(m.gcuct_typ)="magma")
					IF EMPTY(hfak.subnab) .or. ALLTRIM(hfak.subnab)="P"
						partner=hfak.jmeno
					ELSE
						partner=hfak.subnab
					ENDIF 
				ELSE
					partner		= hfak.subnab
				ENDIF 
				TRY
				 	IF "datum"$LOWER(uexpol1.pz) and "doklad"$LOWER(uexpol1.pz)
				 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
				 	ELSE 
						pz = EVALUATE(uexpol1.pz)
					ENDIF 
				CATCH
					pz = PADR(DTOS(m.datum_dokl),17)
				ENDTRY 
				TRY 
					IF m.zfak<>0
						DO CASE 
							CASE m.citytyp="S"
								m.castka=hfak.strava*m.zfau/m.zfak
							CASE m.citytyp="O"
								m.castka=hfak.ostat*m.zfau/m.zfak
							CASE m.citytyp="U"
								m.castka=hfak.ubyt*m.zfau/m.zfak
						ENDCASE 
					ELSE 
						m.castka=0
					ENDIF 
				CATCH
					m.castka=0
				ENDTRY 
				IF m.castka # 0 
					INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
						("WH", "WH", DTOS(hfak.datum), m.doklad, "City", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
				ENDIF 
			ELSE 
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
					("WH", "WH", DTOS(hfak.datum), m.doklad, "City", 1+VAL(hfak.kod1)/100, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
			ENDIF 
		ENDIF 
		SELECT hfak
	ENDSCAN
ENDIF 



PROCEDURE hores_plt_fakt
SELECT hfak 
SCAN FOR hfak.plac="F" .AND. DTOS(hfak.datum)>=LEFT(m.uuz1,8) .AND. DTOS(hfak.datum)<=LEFT(m.uuz2,8) .and.  (!EMPTY(hfak.ucet) .or. LEFT(hfak.pokoj,4)="Pkl.")
	doklad		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
	m.datum_dokl= hfak.datum
	datum		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
	SELECT hf_rad
	LOCATE FOR sequc=hfak.sequc
	IF FOUND()
		m.hf_cislo=hf_rad.cislo
	ELSE
		m.hf_cislo=""
	ENDIF 
	IF testuexplthores("F")
		SELECT uexplt1
		SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
		m.faktura	= m.hf_cislo
		id_man		= ""
		IF (LOWER(m.gcuct_typ)="magma")
			IF EMPTY(hfak.subnab) .or. ALLTRIM(hfak.subnab)="P"
				partner=hfak.jmeno
			ELSE
				partner=hfak.subnab
			ENDIF 
		ELSE
			partner		= hfak.subnab
		ENDIF 
		TRY
		 	IF "datum"$LOWER(uexplt1.pz) and "doklad"$LOWER(uexplt1.pz)
		 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
		 	ELSE 
				pz = EVALUATE(uexplt1.pz)
			ENDIF 
		CATCH
			pz = PADR(DTOS(m.datum_dokl),17)
		ENDTRY 
		TRY 
			castka = hfak.castka
		CATCH
			castka = 0
		ENDTRY 
		INSERT INTO plt (id_kas, uzaverka, u_cis, druh_pl, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
			("WH", DTOS(hfak.datum), m.doklad, "F", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
	ELSE 
		INSERT INTO plt (id_kas, uzaverka, u_cis, druh_pl, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner) VALUES ;
			("WH", DTOS(hfak.datum), m.doklad, "F", m.datum_dokl, "", "", "", "", "", "", "", "", 0, "", .f., "")
	ENDIF 
	SELECT hfak
ENDSCAN

PROCEDURE hores_plt_niefakt
SELECT hfak 
SCAN FOR hfak.plac<>"F" .AND. DTOS(hfak.datum)>=LEFT(m.uuz1,8) .AND. DTOS(hfak.datum)<=LEFT(m.uuz2,8) .and.  (!EMPTY(hfak.ucet) .or. LEFT(hfak.pokoj,4)="Pkl.")
	IF LOWER(m.gcuct_typ)="orion" AND hfak.plac="X"
	ELSE 
		doklad		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
		m.datum_dokl= hfak.datum
		datum		= ALLTRIM(STR(hfak.ucet,10,0))+" "+ALLTRIM(STR(hfak.sequc,10,0))
		IF testuexplthores(hfak.plac)
			SELECT uexplt1
			SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
			m.faktura	= ""
			id_man		= ""
			IF (LOWER(m.gcuct_typ)="magma")
				partner		= "WH"
			ELSE
				partner		= hfak.subnab
			ENDIF 
			TRY
			 	IF "datum"$LOWER(uexplt1.pz) and "doklad"$LOWER(uexplt1.pz)
			 		pz = PADR(DTOS(m.datum_dokl),17)+" "+m.doklad
			 	ELSE 
					pz = EVALUATE(uexplt1.pz)
				ENDIF 
			CATCH
				pz = PADR(DTOS(m.datum_dokl),17)
			ENDTRY 
			TRY 
				castka = hfak.castka
			CATCH
				castka = 0
			ENDTRY 
			INSERT INTO plt (id_kas, uzaverka, u_cis, druh_pl, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
				("WH", DTOS(hfak.datum), m.doklad, hfak.plac, m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
		ELSE 
			INSERT INTO plt (id_kas, uzaverka, u_cis, druh_pl, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner) VALUES ;
				("WH", DTOS(hfak.datum), m.doklad, "F", m.datum_dokl, "", "", "", "", "", "", "", "", 0, "", .f., "")
		ENDIF 
	ENDIF 
	SELECT hfak
ENDSCAN


PROCEDURE hdek_plt
SELECT hdek
SCAN FOR DTOS(hdek.datum)>=LEFT(m.uuz1,8) .AND. DTOS(hdek.datum)<=LEFT(m.uuz2,8)  AND INLIST(ALLTRIM(hdek.typ),"U","P") AND hdek.pole1<>0
	SCATTER MEMVAR
	IF testuexplthores(ALLTRIM(hdek.kod1),"pole1","DTOS(m.datum_dokl)")
		SELECT uexplt1
		SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
		m.faktura	= ""
		id_man		= ""
		partner		= "WH"
		m.datum_dokl= hdek.datum
		datum		= DTOS(hdek.datum)
		m.doklad=SUBSTR(m.datum,3)
		TRY 
			pz = EVALUATE(uexplt1.pz)
		CATCH
			pz = PADR(DTOS(m.datum_dokl),17)
		ENDTRY 
		TRY 
			castka = hdek.pole1
		CATCH
			castka = 0
		ENDTRY 
		IF m.castka # 0 
			INSERT INTO plt (id_kas, uzaverka, u_cis, druh_pl, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
				("WH", m.datum, m.doklad, ALLTRIM(hdek.kod1), hdek.datum, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
		ENDIF 
	ELSE 
		INSERT INTO plt (id_kas, uzaverka, u_cis, druh_pl, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner) VALUES ;
			("WH", m.datum, m.doklad, ALLTRIM(hdek.kod1), hdek.datum, "", "", "", "", "", "", "", "", 0, "", .f., "")
	ENDIF 
	SELECT hdek
ENDSCAN



PROCEDURE hdek_pol
SELECT hdek
SCAN FOR DTOS(hdek.datum)>=LEFT(m.uuz1,8) .AND. DTOS(hdek.datum)<=LEFT(m.uuz2,8) .and. ALLTRIM(hdek.typ)="R"
	SCATTER MEMVAR
	m.sd1=1+hdek.pole2/100
	IF testuexpolhores(hdek.kod1,m.sd1,"","pole1","DTOS(m.datum_dokl)")
		SELECT uexpol1
		SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
		m.faktura	= ""
		m.id_man		= "WH"
		m.partner		= "WH"
		m.datum_dokl= hdek.datum
		m.datum		= DTOS(hdek.datum)
		m.doklad=SUBSTR(m.datum,3)
		TRY 
			pz = EVALUATE(uexplt1.pz)
		CATCH
			pz = PADR(DTOS(m.datum_dokl),17)
		ENDTRY 
		TRY 
			m.castka = hdek.pole1
		CATCH
			m.castka=9999.99
		ENDTRY 
		IF m.castka # 0
			INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
				("WH", "WH", m.datum, m.doklad, hdek.kod1, m.sd1, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
		ENDIF 
	ELSE 
		INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
			("WH", "WH", m.datum, m.doklad, hdek.kod1, m.sd1, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
	ENDIF
	SELECT hdek
ENDSCAN 

SCAN FOR DTOS(hdek.datum)>=LEFT(m.uuz1,8) .AND. DTOS(hdek.datum)<=LEFT(m.uuz2,8) .and. INLIST(ALLTRIM(hdek.typ),"U","P") AND hdek.pole1<>0
	SCATTER MEMVAR
	m.sd1=1+hdek.pole2/100
	IF testuexpolhores(hdek.kod1,m.sd1,"","pole1","DTOS(m.datum_dokl)")
		SELECT uexpol1
		SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
		m.faktura	= ""
		m.id_man		= "WH"
		m.partner		= "WH"
		m.datum_dokl= hdek.datum
		m.datum		= DTOS(hdek.datum)
		TRY 
			pz = EVALUATE(uexplt1.pz)
		CATCH
			pz = PADR(DTOS(m.datum_dokl),17)
		ENDTRY 
		IF ALLTRIM(hdek.kod1)=="F" AND "\d_patria\"$LOWER(m.gchom_path)
			TRY 
				m.castka = hdek.pole4
			CATCH
				m.castka=9999.99
			ENDTRY 
		ELSE 
			TRY 
				m.castka = hdek.pole3+hdek.pole4
			CATCH
				m.castka=9999.99
			ENDTRY 
		ENDIF 
		IF m.castka # 0
			INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
				("WH", "WH", m.datum, m.doklad, hdek.kod1, m.sd1, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
		ENDIF 
	ELSE 
		INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
			("WH", "WH", m.datum, m.doklad, hdek.kod1, m.sd1, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
	ENDIF
	SELECT hdek
	IF hdek.pole5<>0
		m.sd1=1+hdek.pole5/100
		IF testuexpolhores(hdek.kod1,m.sd1,"","pole1","DTOS(m.datum_dokl)")
			SELECT uexpol1
			SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
			m.faktura	= ""
			m.id_man		= "WH"
			m.partner		= "WH"
			m.datum_dokl= hdek.datum
			m.datum		= DTOS(hdek.datum)
			TRY 
				pz = EVALUATE(uexplt1.pz)
			CATCH
				pz = PADR(DTOS(m.datum_dokl),17)
			ENDTRY 
			TRY 
				m.castka = hdek.pole6+hdek.pole7
			CATCH 
				m.castka=9999.99
			ENDTRY 
			IF m.castka # 0
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
					("WH", "WH", m.datum, m.doklad, hdek.kod1, m.sd1, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
			ENDIF 
		ELSE 
			INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
				("WH", "WH", m.datum, m.doklad, hdek.kod1, m.sd1, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
		ENDIF
	ENDIF 	
	SELECT hdek
	IF hdek.pole8<>0
		m.sd1=1
		IF testuexpolhores(hdek.kod1,m.sd1,"","pole1","DTOS(m.datum_dokl)")
			SELECT uexpol1
			SCATTER FIELDS MD, dal, hs_md, hs_dal, md2, dal2, co, poznamka MEMVAR
			m.faktura	= ""
			m.id_man		= "WH"
			m.partner		= "WH"
			m.datum_dokl= hdek.datum
			m.datum		= DTOS(hdek.datum)
			TRY 
				pz = EVALUATE(uexplt1.pz)
			CATCH
				pz = PADR(DTOS(m.datum_dokl),17)
			ENDTRY 
			TRY 
				m.castka = hdek.pole8
			CATCH 
				m.castka=9999.99
			ENDTRY 
			IF m.castka # 0
				INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, faktura, exportuj, partner, doklad) VALUES ;
					("WH", "WH", m.datum, m.doklad, hdek.kod1, m.sd1, "*", m.datum_dokl, m.md, m.dal, m.hs_md, m.hs_dal, m.md2, m.dal2, m.pz, m.poznamka, m.castka, m.faktura, .t., m.partner, m.doklad)
			ENDIF 
		ELSE 
			INSERT INTO pol (id_kas, id_man, uzaverka, u_cis, druh, dan, cen_hlad, datum_dokl, md, dal, hs_md, hs_dal, md2, dal2, pz, poznamka, castka, exportuj) VALUES ;
				("WH", "WH", m.datum, m.doklad, hdek.kod1, m.sd1, "*", m.datum_dokl, "", "", "", "", "", "", "", "", 0, .f.)
		ENDIF
	ENDIF 	
	SELECT hdek
ENDSCAN 



FUNCTION testuexplt
PARAMETERS m.nkas, m.nplatba
PRIVATE m.hladajdruhpl, m.hladajidkas
m.hladajdruhpl=m.nplatba
m.hladajidkas=m.nkas
m.navrat=.t.
*** konkretny druh a cenova hladina
IF USED("uexplt1")
	=REQUERY("uexplt1")
ELSE
	IF m.issql
		USE (m.sqlprefix+"ruexplt1") ALIAS uexplt1 IN 0
	ELSE
		IF !USED("u_ex_plt")
			USE (m.gcucto_path+"u_ex_plt") IN 0
		ENDIF 
		USE vuexplt1 ALIAS uexplt1 IN 0
	ENDIF 
	=CURSORSETPROP("Buffering",5,"uexplt1")
ENDIF
IF RECCOUNT("uexplt1")=0
	*** konkretny druh_pl a kasa
	*** nic sa nenaslo, zakladam do u_ex_plt riadok s konkretnym druhom, partnerom a cenovou hladinou
	IF LOWER(m.gcuct_typ)="kros" AND !INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")
		INSERT INTO uexplt1 (id_kas, druh_pl, co, pz, poznamka, c_data, hs_md, hs_dal, md2, dal) VALUES ;
			(m.hladajidkas, m.hladajdruhpl, "cena_pl", 'uzaverka +" "+ u_cis', "Platba " + m.hladajdruhpl + " na kase " + m.hladajidkas, m.cdata, m.kros_dp_platba, m.kros_kod_str_platba, m.kros_kv_dph_platba, "ID")
	ELSE
		INSERT INTO uexplt1 (id_kas, druh_pl, co, pz, poznamka, c_data) VALUES ;
			(m.hladajidkas, m.hladajdruhpl, "cena_pl", 'uzaverka +" "+ u_cis', "Platba " + m.hladajdruhpl + " na kase " + m.hladajidkas, m.cdata)
	ENDIF 
	=tux(.t.,.t.,"uexplt1")	
	m.novinka=.t.
	m.pokracuj=.f.
	=REQUERY("uexplt1")
	m.navrat=.f.
ENDIF 
RETURN m.navrat


FUNCTION testuexpol
PARAMETERS m.nkas, m.nman, m.ndruh, m.nhlad, m.ndan
PRIVATE m.hladajdruh, m.hladajidkas, m.hladajidman, m.hladajcenhlad, m.hladajdan
m.hladajdruh=ALLTRIM(m.ndruh)
m.hladajidkas=m.nkas
m.hladajidman=m.nman
m.hladajcenhlad=ALLTRIM(m.nhlad)
m.hladajdan=INT(m.ndan*100)
m.navrat=.t.
*** konkretny man, druh, dan a cenova hladina
IF USED("uexpol1")
	=REQUERY("uexpol1")
ELSE
	IF m.issql
		USE (m.sqlprefix+"ruexpol1") ALIAS uexpol1 IN 0
	ELSE
		IF !USED("u_ex_pol")
			USE (m.gcucto_path+"u_ex_pol") IN 0
		ENDIF 
		USE vuexpol1 ALIAS uexpol1 IN 0
	ENDIF 
	=CURSORSETPROP("Buffering",5,"uexpol1")
ENDIF
IF RECCOUNT("uexpol1")=0
	*** konkretny man, vsetky druhy, konkretna cenova hladina
	m.hladajdruh="*"
	=REQUERY("uexpol1")
	IF RECCOUNT("uexpol1")=0
		*** konkretny man, konkretny druh a vsetky cenove hladiny
		m.hladajcenhlad="*"
		m.hladajdruh=ALLTRIM(m.ndruh)
		=REQUERY("uexpol1")
		IF RECCOUNT("uexpol1")=0
			*** konkretny man, vsetky druhy a vsetky cenove hladiny
			m.hladajcenhlad="*"
			m.hladajdruh="*"
			=REQUERY("uexpol1")
			IF RECCOUNT("uexpol1")=0
				*** konkretny man, vsetky druhy a konkretna cenova hladina
				m.hladajcenhlad=m.nhlad
				m.hladajdruh="*"
				=REQUERY("uexpol1")
				IF RECCOUNT("uexpol1")=0
					*** vsetky many, konkretny druh a konkretna cenova hladina
					m.hladajman="*"
					m.hladajdruh=ALLTRIM(m.ndruh)
					m.hladajcenhlad=ALLTRIM(m.nhlad)
					=REQUERY("uexpol1")
					IF RECCOUNT("uexpol1")=0
						*** vsetky man, konkretny druh a vsetky cenove hladiny
						m.hladajcenhlad="*"
						=REQUERY("uexpol1")
						IF RECCOUNT("uexpol1")=0
							*** vsetky many, vsetky druhy a vsetky cenove hladiny
							m.hladajcenhlad="*"
							m.hladajdruh="*"
							=REQUERY("uexpol1")
							IF RECCOUNT("uexpol1")=0
								*** vsetky many, vsetky druhy a konkretna cenova hladina
								m.hladajcenhlad=ALLTRIM(m.nhlad)
								m.hladajdruh="*"
								=REQUERY("uexpol1")
								IF RECCOUNT("uexpol1")=0
									*** nic sa nenaslo, musim zalozit novy riadok do u_ex_pol
									IF LOWER(m.gcuct_typ)="kros" AND !INLIST(LOWER(m.gcuct_spec),"kosice","masaryk")
										INSERT INTO uexpol1 (id_kas, id_man, druh, cen_hlad, dan, co, pz, poznamka, c_data, md, md2, hs_md, hs_dal) VALUES ;
											(m.nkas, m.nman, m.ndruh, m.nhlad, m.ndan, "trzba",  'uzaverka+" "+u_cis', "POL "+ m.nkas + " " + m.nman + " " + ALLTRIM(m.ndruh) + STR(m.ndan*100-100, 3), m.cdata, m.kros_kod_str_polozka, m.kros_kv_dph_polozka, m.kros_dp_polozka, m.kros_dp_dph)
									ELSE
										INSERT INTO uexpol1 (id_kas, id_man, druh, cen_hlad, dan, co, pz, poznamka, c_data) VALUES ;
											(m.nkas, m.nman, m.ndruh, m.nhlad, m.ndan, "trzba",  'uzaverka+" "+u_cis', "POL "+ m.nkas + " " + m.nman + " " + ALLTRIM(m.ndruh) + STR(m.ndan*100-100, 3), m.cdata)
									ENDIF 
									=tux(.t.,.t.,"uexpol1")	
									m.novinka=.t.
									m.pokracuj=.f.
									m.hladajdruh=ALLTRIM(m.ndruh)
									m.hladajidkas=m.nkas
									m.hladajidkas=m.nman
									m.hladajcenhlad=ALLTRIM(m.nhlad)
									m.hladajdan=INT(m.ndan*100)
									=REQUERY("uexpol1")
									m.navrat=.f.
								ENDIF 
							ENDIF 
						ENDIF 
					ENDIF 
				ENDIF 
			ENDIF 
		ENDIF 
	ENDIF 
ENDIF 
RETURN m.navrat



FUNCTION testuexpolspdan
PARAMETERS m.nkas, m.ndan
PRIVATE m.hladajdruh, m.hladajidkas, m.hladajidman, m.hladajcenhlad, m.hladajdan
m.hladajdan=INT(m.ndan*100)
m.hladajidkas=m.nkas
m.hladajdruh="SPDAN"
m.hladajidman="*"
m.hladajcenhlad="*"
m.navrat=.t.
*** konkretny druh, partner a cenova hladina
IF USED("uexpol1")
	=REQUERY("uexpol1")
ELSE
	IF m.issql
		USE (m.sqlprefix+"ruexpol1") ALIAS uexpol1 IN 0
	ELSE
		IF !USED("u_ex_pol")
			USE (m.gcucto_path+"u_ex_pol") IN 0
		ENDIF 
		USE vuexpol1 ALIAS uexpol1 IN 0
	ENDIF 
	=CURSORSETPROP("Buffering",5,"uexpol1")
ENDIF
IF RECCOUNT("uexpol1")=0
	*** nic sa nenaslo, zakladam do u_ex_pol riadok pre spotrebnu dan tejto kasy a dane
	INSERT INTO uexpol1 (id_kas, id_man, druh, cen_hlad, dan, co, pz, poznamka, c_data) VALUES ;
		(m.hladajidkas, m.hladajidman, m.hladajdruh, m.hladajcenhlad, m.ndan, "spdan",  'uzaverka+" "+u_cis', "POL "+ m.hladajidkas + "    " + "Spotrebna dan" + STR(m.ndan*100-100, 3), m.cdata)
	=tux(.t.,.t.,"uexpol1")	
	m.novinka=.t.
	m.pokracuj=.f.
	=REQUERY("uexpol1")
	m.navrat=.f.
ENDIF 
RETURN m.navrat


FUNCTION testuexpolhores
PARAMETERS m.ndruh, m.ndan, m.npozn, m.nco, m.npz
PRIVATE m.hladajdruh, m.hladajidkas, m.hladajidman, m.hladajcenhlad, m.hladajdan
m.hladajdruh=m.ndruh
m.hladajidkas="WH"
m.hladajidman="*"
m.hladajcenhlad="*"
m.hladajdan=INT(m.ndan*100)

m.navrat=.t.
*** konkretny man, druh, dan a cenova hladina
IF USED("uexpol1")
	=REQUERY("uexpol1")
ELSE
	IF m.issql
		USE (m.sqlprefix+"ruexpol1") ALIAS uexpol1 IN 0
	ELSE
		IF !USED("u_ex_pol")
			USE (m.gcucto_path+"u_ex_pol") IN 0
		ENDIF 
		USE vuexpol1 ALIAS uexpol1 IN 0
	ENDIF 
	=CURSORSETPROP("Buffering",5,"uexpol1")
ENDIF
IF RECCOUNT("uexpol1")=0
	*** nic sa nenaslo, musim zalozit novy riadok do u_ex_pol
	INSERT INTO uexpol1 (id_kas, id_man, druh, cen_hlad, dan, co, pz, poznamka, c_data) VALUES ;
		(m.hladajidkas, m.hladajidman, m.hladajdruh, m.hladajcenhlad, m.ndan, IIF(EMPTY(m.nco),"castka",m.nco),  IIF(EMPTY(m.npz),"DTOS(m.datum_dokl)+' '+m.doklad",m.npz), "POL HORES "+IIF(EMPTY(m.npozn),m.hladajdruh, m.npozn)+" "+ALLTRIM(STR((m.hladajdan-1)*100)), m.cdata)
		=tux(.t.,.t.,"uexpol1")	
		=REQUERY("uexpol1")
		m.navrat=.f.
ENDIF 
RETURN m.navrat

FUNCTION testuexplthores
PARAMETERS m.ndruh, m.nco, m.npz
PRIVATE m.hladajdruhpl, m.hladajidkas, m.nco
m.hladajdruhpl=m.ndruh
m.hladajidkas="WH"
DO CASE 
	CASE m.hladajdruhpl="H"
		m.npozn="Hotove"
	CASE m.hladajdruhpl="C"
		m.npozn="K_"
	CASE m.hladajdruhpl="F"
		m.npozn="N_UVER"
	CASE m.hladajdruhpl="Z"
		m.npozn="Hotove"
	OTHERWISE
		m.npozn=m.hladajdruhpl
ENDCASE 
m.navrat=.t.
IF USED("uexplt1")
	=REQUERY("uexplt1")
ELSE
	IF m.issql
		USE (m.sqlprefix+"ruexplt1") ALIAS uexplt1 IN 0
	ELSE
		IF !USED("u_ex_plt")
			USE (m.gcucto_path+"u_ex_plt") IN 0
		ENDIF 
		USE vuexplt1 ALIAS uexplt1 IN 0
	ENDIF 
	=CURSORSETPROP("Buffering",5,"uexplt1")
ENDIF
IF RECCOUNT("uexplt1")=0
	*** nic sa nenaslo, musim zalozit novy riadok do u_ex_pol
	INSERT INTO uexplt1 (id_kas, druh_pl, co, pz, poznamka, c_data) VALUES ;
		(m.hladajidkas, m.hladajdruhpl, IIF(EMPTY(m.nco), "castka",m.nco),IIF(EMPTY(m.npz),"DTOS(m.datum_dokl)+' '+m.doklad", m.npz) , "Platba " + m.npozn + " HORES", m.cdata)
	=tux(.t.,.t.,"uexplt1")	
	=REQUERY("uexplt1")
	m.navrat=.f.
ENDIF 
RETURN m.navrat



PROCEDURE hores_sk
PARAMETERS dennyexport, uuz1, uuz2
PUBLIC citytyp
PRIVATE m.navrat
LOCAL robhada
m.robhada=(uuz2<>uuz1)
IF m.hor_sql
	IF !test_hor_con()
		=MESSAGEBOX(jaz("form_nespr_param_sql_hores"),48,"FoodMan",2000)
		RETURN
	ENDIF 
ENDIF 
IF m.robhada
	hores_sk_had = CREATEOBJECT("had", jaz("form_export_trzieb_horesu"))
	hores_sk_had.SHOW()
ENDIF 
old_errh = ON("ERROR")
DO pozavirej WITH "hf_rad, hf_hlv, hfak, hfau, hras"
IF m.uct_natazene
	SELECT 0
	IF m.hor_sql
		USE hdek ALIAS hdek IN 0
		=CURSORSETPROP("Buffering",5,"hdek")
	ELSE 
		USE (m.hor_path+"hdek.dbf")
	ENDIF 
	DO hdek_pol
	IF m.robhada
		hores_sk_had.set_had(60)
	ENDIF 
	IF !("\d_patria\"$LOWER(m.gchom_path))
		DO hdek_plt
	ENDIF 
	USE IN hdek
	IF m.robhada
		hores_sk_had.set_had(90)
	ENDIF 
ELSE 
	IF m.hor_sql
		USE hores_hfak ALIAS hfak IN 0
		USE hores_hfau ALIAS hfau IN 0
		USE hores_hf_rad ALIAS hf_rad IN 0
		USE hores_hf_hlv ALIAS hf_hlv IN 0
		USE hores_hras ALIAS hras IN 0
	ELSE 
		USE (m.hor_path+"hfak.dbf") IN 0
		USE (m.hor_path+"hfau.dbf") IN 0
		USE (m.hor_path+"hf_rad.dbf") IN 0
		USE (m.hor_path+"hras.dbf") IN 0
	ENDIF 
	SELECT hras 
	m.alto_predplatby=""
	SCAN FOR rdruh="P"
		m.alto_predplatby=m.alto_predplatby+"'"+ALLTRIM(rkod)+"',"
	ENDSCAN 
	IF RIGHT(m.alto_predplatby,1)=","
		m.alto_predplatby=LEFT(m.alto_predplatby,LEN(m.alto_predplatby)-1)
	ENDIF
	IF !EMPTY(m.gcuct_city)
		LOCATE FOR 	UPPER(ALLTRIM(rkod))=m.gcuct_city
		IF FOUND()
			m.citytyp=hras.rtyp
		ELSE
			m.citytyp=""
		ENDIF 
	ELSE
		m.citytyp=""
	ENDIF 
	IF !USED("pol")
		CREATE CURSOR pol (id_kas c(2), id_man c(2), uzaverka c(17), u_cis c(7), druh c(20), dan N(5,2), cen_hlad c(2), datum_dokl D, spdan Y, ;
			trzba_puv Y, trzba Y, dan_zakl Y, uz_cislo N(4,0), ;
			md c(8), dal c(8), hs_md c(15), hs_dal c(15), exportuj L, md2 c(7), dal2 c(7), ;
			co c(30), pz c(50), poznamka c(30), castka Y, faktura c(20), zaloha L, partner c(30), doklad C(15))
	ENDIF 
	IF !USED("plt")
		CREATE CURSOR plt (uzaverka c(17), id_kas c(2), u_cis c(7), druh_pl c(8), cena_pl Y, tip Y, mena c(3), cena_mena Y, datum_dokl D, c_uzaverka I, ucet I,;
			md c(8), dal c(8), hs_md c(15), hs_dal c(15), exportuj L, md2 c(7), dal2 c(7), ;
			co c(30), pz c(50), poznamka c(30), castka Y, faktura c(20), zaloha L, partner c(30), doklad c(15))
	ENDIF 	
	IF m.robhada
		hores_sk_had.set_had(10)
	ENDIF 
	DO CASE 
		CASE  (LOWER(m.gcuct_typ)="sap") AND EMPTY(m.gcuct_spec)
			DO hores_pol_fakt
			IF m.robhada
				hores_sk_had.set_had(30)
			ENDIF 
			DO hores_pol_niefakt
			IF m.robhada
				hores_sk_had.set_had(50)
			ENDIF 
			DO hores_plt_fakt
			IF m.robhada
				hores_sk_had.set_had(70)
			ENDIF 
			DO hores_plt_niefakt
			IF m.robhada
				hores_sk_had.set_had(90)
			ENDIF 
		CASE INLIST(LOWER(m.gcuct_typ),"magma","kros","asseco","softip","proluc")
			DO hores_pol_fakt
			IF m.robhada
				hores_sk_had.set_had(30)
			ENDIF 
			DO hores_pol_niefakt
			IF m.robhada
				hores_sk_had.set_had(50)
			ENDIF 
			DO hores_plt_fakt
			IF m.robhada
				hores_sk_had.set_had(70)
			ENDIF 
			DO hores_plt_niefakt
			IF m.robhada
				hores_sk_had.set_had(90)
			ENDIF 
		CASE INLIST(LOWER(m.gcuct_typ), "karat", "money", "navision", "orion")
			DO hores_pol_niefakt
			IF m.robhada
				hores_sk_had.set_had(50)
			ENDIF 
			DO hores_plt_niefakt
			IF m.robhada
				hores_sk_had.set_had(90)
			ENDIF 
	ENDCASE 
	DO pozavirej WITH "hfak, hfau, hf_rad, hras, hf_hlv, u_ex, hdek"
ENDIF 
IF USED("pol") AND USED("plt")
	SELECT disti uzaverka FROM pol WHERE !exportuj INTO CURSOR nenastavene
	IF m.dennyexport
		IF RECCOUNT("nenastavene")>0
			m.navrat=.f.
		ELSE
			SELECT disti uzaverka FROM plt WHERE !exportuj INTO CURSOR nenastavene
			IF RECCOUNT("nenastavene")>0
				m.navrat=.f.
			ELSE
				m.navrat=.t.
			ENDIF 
		ENDIF 
	ELSE
		IF RECCOUNT("nenastavene")>0
			m.navrat=.f.
		ELSE
			SELECT disti uzaverka FROM plt WHERE !exportuj INTO CURSOR nenastavene
			IF RECCOUNT("nenastavene")>0
				m.navrat=.f.
			ELSE
				m.navrat=.t.
			ENDIF 
		ENDIF 
	ENDIF 
	IF m.navrat
		*** kontrola, ci sa v pol nenachadzaju polozky, ktore sa nemaju exportovat
		SELECT SUM(castka) as celkom, $0 as beznul, uzaverka, doklad FROM pol INTO CURSOR aaa GROUP BY uzaverka, doklad READWRITE 
		SELECT SUM(castka) as castka, uzaverka, doklad FROM pol INTO CURSOR bbb GROUP BY uzaverka, doklad WHERE !EMPTY(md) AND !EMPTY(dal)
		UPDATE aaa SET beznul=bbb.castka from bbb WHERE aaa.uzaverka=bbb.uzaverka AND aaa.doklad=bbb.doklad
		USE IN bbb
		DELETE FROM aaa WHERE celkom=beznul
		*** uprava ciastky v plt pomerne o sumu poloziek, ktore sa nemaju exportovat
		UPDATE plt SET castka=plt.castka*aaa.beznul/aaa.celkom from aaa WHERE plt.uzaverka=aaa.uzaverka AND plt.doklad=aaa.doklad AND aaa.celkom<>0
		USE IN aaa

		*** kontrola, ci sa v plt nenachadzaju platby, ktore sa nemaju exportovat
		SELECT SUM(castka) as celkom, $0 as beznul, uzaverka, doklad FROM plt INTO CURSOR aaa GROUP BY uzaverka, doklad READWRITE 
		SELECT SUM(castka) as castka, uzaverka, doklad FROM plt INTO CURSOR bbb GROUP BY uzaverka, doklad WHERE !EMPTY(md) AND !EMPTY(dal)
		UPDATE aaa SET beznul=bbb.castka from bbb WHERE aaa.uzaverka=bbb.uzaverka AND aaa.doklad=bbb.doklad
		USE IN bbb
		DELETE FROM aaa WHERE celkom=beznul
		*** uprava ciastky v pol pomerne o sumu platieb, ktore sa nemaju exportovat
		UPDATE pol SET castka=pol.castka*aaa.beznul/aaa.celkom from aaa WHERE pol.uzaverka=aaa.uzaverka AND pol.doklad=aaa.doklad AND aaa.celkom<>0
		USE IN aaa
		*** odmazanie riadkov z pol a plt, kde je nevyplnene md alebo dal
		DELETE FROM pol WHERE EMPTY(md) OR EMPTY(dal)		
		DELETE FROM plt WHERE EMPTY(md) OR EMPTY(dal)		
		**** doplnit select z pol a plt na u_ex_platby a u_ex_polozky
		SELECT pol
		REPLACE ALL dan_zakl WITH ground(castka/dan,2)
		IF m.robhada
			hores_sk_had.set_had(99)
		ENDIF 
		IF USED("u_ex_polozky")
			INSERT INTO u_ex_polozky (datum_dokl, uzaverka, u_cis, id_kas, id_man, castka, md, dal, md2, dal2, hs_md, hs_dal, pz, poznamka, druh, cen_hlad, dan_zakl, dan, faktura, zaloha, partner, oprava, doklad);
				SELECT datum_dokl, uzaverka, u_cis, id_kas, id_man, castka, md, dal, md2, dal2, hs_md, hs_dal, pz, poznamka, druh, cen_hlad, dan_zakl, dan, faktura, zaloha, partner, .f. as oprava, doklad FROM pol  WHERE exportuj AND castka<>0
		ELSE 
			SELECT datum_dokl, uzaverka, u_cis, id_kas, id_man, castka, md, dal, md2, dal2, hs_md, hs_dal, pz, poznamka, druh, cen_hlad, dan_zakl, dan, faktura, zaloha, partner, .f. as oprava, doklad FROM pol INTO CURSOR u_ex_polozky READWRITE  WHERE exportuj AND castka<>0
		ENDIF 
		IF USED("u_ex_platby")
			INSERT INTO u_ex_platby (datum_dokl, uzaverka, u_cis, id_kas, druh_pl, md, dal, md2, dal2, hs_md, hs_dal, castka, pz, poznamka, faktura, zaloha, partner, oprava, doklad);
				SELECT datum_dokl, uzaverka, u_cis, id_kas, druh_pl, md, dal, md2, dal2, hs_md, hs_dal, castka, pz, poznamka, faktura, zaloha, partner, .f. as oprava, doklad FROM plt WHERE exportuj AND castka<>0
		ELSE 
			SELECT datum_dokl, uzaverka, u_cis, id_kas, druh_pl, md, dal, md2, dal2, hs_md, hs_dal, castka, pz, poznamka, faktura, zaloha, partner, .f. as oprava, doklad FROM plt INTO CURSOR u_ex_platby READWRITE WHERE exportuj AND castka<>0
		ENDIF 
	ENDIF 
ENDIF 		
DO pozavirej WITH "pol, plt, fin, u_ex, u_ex_sum"
IF m.robhada
	RELEASE hores_sk_had
ENDIF 
RETURN m.navrat
