PARAMETERS jakasestava
SET COLLATE TO (m.gccollate)
SET CONFIRM ON
fn_pom = "pom"
fn_pomidx = m.gcpri_path + "pomidx"
ostatni = "ZZZostatni"
IF LOWER(m.jakasestava)="regleta"
	IF !m.slovenskaverzia
		DO (m.jakasestava)
	ENDIF 
ELSE 
	DO (m.jakasestava)
ENDIF 
****************************************
PROCEDURE s_obr_or2
PARAMETERS m.menostrediska, m.cislostrediska, m.tlac_operace, m.d1, m.d2, m.filter_priznakov
PRIVATE m.operace__txt
SET COLLATE TO (m.gccollate)
SET CONFIRM ON
fn_pom = "pom"
fn_pomidx = m.gcpri_path + "pomidx"
ostatni = "ZZZostatni"
IF "_B"$m.tlac_operace
	m.tlac_operace=STRTRAN(m.tlac_operace,"_B","VB,PB")
ENDIF
IF "_C"$m.tlac_operace
	m.tlac_operace=STRTRAN(m.tlac_operace,"_C","VC,PC")
ENDIF
IF "_P"$m.tlac_operace
	m.tlac_operace=STRTRAN(m.tlac_operace,"_P","VP,PP")
ENDIF
m.operace__txt='"'
FOR m.i=1 TO LEN(m.tlac_operace)
	IF SUBSTR(m.tlac_operace,m.i,1)=","
		m.operace__txt=m.operace__txt+'","'
	ELSE
		m.operace__txt=m.operace__txt+SUBSTR(m.tlac_operace,m.i,1)
	ENDIF
ENDFOR
m.operace__txt=m.operace__txt+'"'

SELECT selkasy
INDEX ON id_zkratka TAG id_zkratka
SELECT seldruhy
INDEX ON druh TAG druh
SELECT pom

SET RELATION TO partner INTO selkasy
REPLACE FOR .NOT. selkasy.lbl  partner WITH m.nechci, c_partner WITH 9999999 * SIGN(c_partner), c_dokl WITH 9999999 * SIGN(c_partner), doklad WITH 9999999 * SIGN(c_partner), c_karty WITH 9999999, nazev WITH m.nechci, druh WITH m.nechci, spart WITH m.nechci

SET RELATION TO druh INTO seldruhy
REPLACE FOR .NOT. seldruhy.lbl partner WITH m.nechci, c_partner WITH 9999999 * SIGN(c_partner), c_dokl WITH 9999999 * SIGN(c_partner), doklad WITH 9999999 * SIGN(c_partner), c_karty WITH 9999999, nazev WITH m.nechci, druh WITH m.nechci, spart WITH m.nechci
SET RELATION TO
IF !EMPTY(m.filter_priznakov)
	DO pozavirej WITH "ktriedfilt"
	IF m.issql
		TRY 
			sql_select('select distinct c_karty FROM  food600.ktried where c_data = ?m.cdata and '+m.filter_priznakov,'ktriedfilt','c_karty i')
		CATCH
			DO pozavirej WITH "ktriedfilt"
		ENDTRY 
	ELSE 
		IF !USED("ktried")
			m.bolktried=.f.
			USE (m.fn_ktried) IN 0 ALIAS ktried
		ELSE
			m.bolktried=.t.
		ENDIF 					
		TRY 
			SELECT * FROM ktried WHERE &m.filter_priznakov INTO CURSOR ktriedfilt
		CATCH
			DO pozavirej WITH "ktriedfilt"
		ENDTRY
		IF !m.bolktried 
			DO pozavirej WITH "ktried"
		ENDIF  
	ENDIF 
ELSE 
	DO pozavirej WITH "ktriedfilt"
ENDIF 
IF USED("ktriedfilt")
	UPDATE pom SET partner=m.nechci, c_partner=9999999 * SIGN(c_partner), c_dokl=9999999 * SIGN(c_partner), doklad=9999999 * SIGN(c_partner), c_karty=9999999, nazev=m.nechci, druh=m.nechci, spart=m.nechci WHERE !(c_karty in (select c_karty from ktriedfilt))
	USE IN ktriedfilt
ENDIF 


SELECT c_stredisk, c_dokl, operace, c_karty, nazev, mnozstvi, MTON(cena)*en__meny(c_mena,m.girep_mena) AS cena, MTON(cena_part)*en__meny(c_mena,m.girep_mena) AS cena_part, MTON(cen_pardph)*en__meny(c_mena,m.girep_mena) AS cen_pardph, ;
	SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy, SUM(MTON(mnozstvi)*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachy_par, SUM(MTON(mnozstvi)*MTON(cen_pardph)*en__meny(c_mena,m.girep_mena)) AS prachysdph, ;
	c_partner, partner, datum_dokl, doklad, pz1, pz2,m.girep_mena AS c_mena, ;
	druh, spart, stredisko, cena1, kod, stredskup, platba, provedl, cen_hlad ;
	WHERE partner = m.nechci OR nazev = m.nechci ;
	GROUP BY c_partner, c_dokl, druh FROM pom INTO CURSOR pom22 READWRITE
REPLACE FOR mnozstvi # 0 cena WITH prachy/mnozstvi, cena_part WITH prachy_par/mnozstvi, cen_pardph WITH prachysdph/mnozstvi

SELECT pom
DELETE FOR partner = m.nechci .OR. nazev = m.nechci
INSERT INTO pom (c_stredisk, c_dokl, operace, c_karty, nazev, mnozstvi, cena, cena_part, cen_pardph, prachy, prachy_par, prachysdph, c_partner, partner, datum_dokl, doklad, pz1, pz2, c_mena,;
	druh, spart, stredisko, cena1, kod, stredskup, platba, provedl, cen_hlad) SELECT c_stredisk, c_dokl, operace, c_karty, nazev, mnozstvi, cena, cena_part, cen_pardph, prachy, prachy_par, prachysdph, c_partner, partner, datum_dokl, doklad, pz1, pz2, c_mena,;
	druh, spart, stredisko, cena1, kod, stredskup, platba, provedl, cen_hlad FROM pom22
USE IN pom22

radek_v_sestavach = 1
pocet_radku_v_sestavach = 99

DO WHILE radek_v_sestavach <= pocet_radku_v_sestavach
	SELECT sestavy
	pocet_radku_v_sestavach = RECCOUNT()
	GOTO (m.radek_v_sestavach)
	IF ! EMPTY(lbl)
		SCATTER FIELD cmd, dily MEMVAR
		&cmd
	ENDIF
	radek_v_sestavach = radek_v_sestavach + 1
ENDDO

*!*****************************************************************************
PROCEDURE daj_datum
PARAMETERS c_str, fnd, fnda, fno, fnoa, pgmparam, tlac_operace, rodic
DO pozavirej WITH "doklady,obraty"
PRIVATE m.zarchivu, m.hladajcstredisk, m.hladajcdoklod, m.hladajcdokldo, akt_mena, m.tlac_operace, m.d1, m.d2, m.operace__txt
m.akt_mena=m.giakt_mena
m.hladajcstredisk=m.c_str
SELECT 0
CREATE CURSOR selkasy (c_partner i, id_zkratka c(25), lbl l, stredskup c(4))
STORE "NIC" TO d1_txt, d2_txt
IF EMPTY(n_checks("POHYBY_EXCL_"))

	CREATE CURSOR seldruhy (operace c(2), druh c(25), lbl l)
	INSERT INTO seldruhy (operace, druh, lbl) VALUE ("PE", lng_oper("PE"), .T. )
	INSERT INTO seldruhy (operace, druh, lbl) VALUE ("PV", lng_oper("PV"), .T. )
	INSERT INTO seldruhy (operace, druh, lbl) VALUE ("VE", lng_oper("VE"), .T. )
	INSERT INTO seldruhy (operace, druh, lbl) VALUE ("VV", lng_oper("VV"), .T. )
	INSERT INTO seldruhy (operace, druh, lbl) VALUE ("PI", lng_oper("PI"), .T. )
	INSERT INTO seldruhy (operace, druh, lbl) VALUE ("VK", lng_oper("VK"), .T. )
	INSERT INTO seldruhy (operace, druh, lbl) VALUE ("VR", lng_oper("VR"), .T. )
	INSERT INTO seldruhy (operace, druh, lbl) VALUE ("_P", lng_oper("VP"), .T. )
	INSERT INTO seldruhy (operace, druh, lbl) VALUE ("_B", lng_oper("VB"), .T. )
	INSERT INTO seldruhy (operace, druh, lbl) VALUE ("_C", lng_oper("VC"), .T. )
	INDEX ON operace TAG operace

	DO FORM seldruhy WITH "seldruhy","operace",200 TO m.druhx
	LOCATE FOR EMPTY(lbl)
	IF FOUND()
		m.tlac_operace=""
		m.operace_txt=""
		SCAN FOR !EMPTY(lbl)
			m.tlac_operace=m.tlac_operace+seldruhy.operace+","
			m.operace_txt= m.operace_txt + ALLTRIM(seldruhy.druh) + ","
		ENDSCAN
		IF RIGHT(m.tlac_operace,1)=","
			m.tlac_operace=LEFT(m.tlac_operace,LEN(m.tlac_operace)-1)
		ENDIF

		IF RIGHT(m.operace_txt,1)=","
			m.operace_txt=LEFT(m.operace_txt,LEN(m.operace_txt)-1)
		ENDIF
		IF LEN(m.operace_txt) > 33
			m.operace_txt=m.operace
		ENDIF
	ELSE
		m.tlac_operace="__"
		m.operace_txt=""
	ENDIF
	USE IN seldruhy
	IF "_B"$m.tlac_operace
		m.tlac_operace=STRTRAN(m.tlac_operace,"_B","VB,PB")
	ENDIF
	IF "_C"$m.tlac_operace
		m.tlac_operace=STRTRAN(m.tlac_operace,"_C","VC,PC")
	ENDIF
	IF "_P"$m.tlac_operace
		m.tlac_operace=STRTRAN(m.tlac_operace,"_P","VP,PP")
	ENDIF
	m.operace__txt='"'
	FOR m.i=1 TO LEN(m.tlac_operace)
		IF SUBSTR(m.tlac_operace,m.i,1)=","
			m.operace__txt=m.operace__txt+'","'
		ELSE
			m.operace__txt=m.operace__txt+SUBSTR(m.tlac_operace,m.i,1)
		ENDIF
	ENDFOR
	m.operace__txt=m.operace__txt+'"'
	m.zarchivu=(MESSAGEBOX(jaz("prg_obraty_z_archivu"),4+32+256,jaz("m_ses_obratove sestavy"))=6)
	IF m.issql
		rv("zostobr_doklady","doklady")
		IF m.zarchivu
			rv("zostobr_doklady_a","dokl_a")
			SELECT *, m.hladajcstredisk AS c_stredisk, doklad AS ndoklad,.T. AS archiv FROM dokl_a WHERE INLIST(LEFT(operace,1),"P","V","K");
				UNION SELECT *,m.hladajcstredisk AS c_stredisk, doklad AS ndoklad,.F. AS archiv FROM doklady WHERE INLIST(LEFT(operace,1),"P","V","K") .AND. operace<>"PR" INTO CURSOR dajdokldokl READWRITE
		ELSE
			SELECT *, m.hladajcstredisk AS c_stredisk, doklad AS ndoklad,.F. AS archiv FROM doklady WHERE INLIST(LEFT(operace,1),"P","V","K") INTO CURSOR  dajdokldokl READWRITE
		ENDIF
	ELSE
		IF m.zarchivu
			USE (m.fnd) ALIAS doklady AGAIN IN 0 SHARED
			USE (m.fnda) ALIAS dokl_a AGAIN IN 0 SHARED
			SELECT *, doklad AS ndoklad,.T. AS archiv FROM dokl_a WHERE INLIST(LEFT(operace,1),"P","V","K") AND c_stredisk=m.hladajcstredisk ;
				UNION SELECT *,doklad AS ndoklad,.F. AS archiv FROM doklady WHERE INLIST(LEFT(operace,1),"P","V","K") .AND. operace<>"PR" AND c_stredisk=m.hladajcstredisk INTO CURSOR dajdokldokl READWRITE
		ELSE
			USE (m.fnd) ALIAS doklady AGAIN IN 0 SHARED
			SELECT *, doklad AS ndoklad,.F. AS archiv FROM doklady WHERE INLIST(LEFT(operace,1),"P","V","K") AND c_stredisk=m.hladajcstredisk INTO CURSOR  dajdokldokl READWRITE
		ENDIF
	ENDIF
	DO pozavirej WITH "doklady,dokl_a"
	SELECT dajdokldokl
	IF LEN(m.tlac_operace)=2
		IF m.tlac_operace<>"__"
			DELETE FOR operace<>m.tlac_operace AND operace<>"KK" AND c_dokl<>0
		ENDIF
	ELSE
		DELETE FOR !INLIST(operace,&operace__txt) AND operace<>"KK"	AND c_dokl<>0
	ENDIF
	WAIT "  working  dokl  " WIND AT 22,22 NOWA
	SELECT dajdokldokl.*, partneri.partner FROM dajdokldokl LEFT JOIN partneri ON dajdokldokl.c_partner=partneri.c_partner INTO CURSOR doklady READWRITE ORDER BY dajdokldokl.c_dokl
	INDEX ON c_dokl TAG c_dokl
	REPLACE FOR operace = "KK" partner WITH REPLICATE("*", 31)
	REPLACE FOR EMPTY(datum_dokl) datum_dokl WITH {^1888-11-11}
	REPLACE FOR ISNULL(partner) partner WITH "neznamy partner"
	SELECT c_dokl, m.hladajcstredisk AS c_stredisk, operace, " | " + STR(doklad, 6) + " | " AS doklad, datum_dokl, partner, pz1, pz2, ndoklad, c_mena, platba, provedl, cen_hlad, archiv FROM doklady INTO CURSOR doklx READWRITE ORDER BY c_dokl
	REPLACE doklad WITH " |" + partner FOR operace="KK"
	GO TOP
	LOCATE FOR c_dokl >=m.d1

	WAIT CLEAR
	SET CONFIRM ON
	DEFINE POPUP datumy FROM 0, 7 TO SROWS()-2,155 TITLE " Počátek " FONT m.gcdefa_font ;
		PROMPT FIELD STR(c_dokl, 8) + " | " + DTOC(datum_dokl) + " | " + operace + doklad + partner + "| " + pz1
	ON SELECT POPUP datumy DEACTIVATE POPUP
	ACTIVATE POPUP datumy REST
	IF ! EMPTY(PROMPT())
		d1 = c_dokl
		d1_txt = operace + " " + ALLT(STR(c_dokl)) + " " + DTOC(datum_dokl)
		d1_dat_dokl = datum_dokl
		d1arch = archiv
		SET FILTER TO c_dokl >= d1
		GO TOP
		LOCATE FOR c_dokl >=m.d1
		DEFINE POPUP datumy FROM 0, 7 TO SROWS()-2,155 TITLE " Konec " FONT m.gcdefa_font ;
			PROMPT FIELD STR(c_dokl, 8) + " | " + DTOC(datum_dokl) + " | " + operace + doklad + partner + " | " + pz1
		ON SELECT POPUP datumy DEACTIVATE POPUP
		ACTIVATE POPUP datumy REST
		IF ! EMPTY(PROMPT())
			d2arch = archiv
			d2 = c_dokl
			d2_txt = operace + " " + ALLT(STR(c_dokl)) + " " + DTOC(datum_dokl)
			d2_dat_dokl = datum_dokl
			m.hladajcdoklod=m.d1
			m.hladajcdokldo=m.d2
			m.fn_doklady_tlac="doklady"
			SELECT doklady
			DELETE FOR (c_dokl<m.d1 OR c_dokl>m.d2) AND c_dokl<>0
			IF m.d1arch
				IF m.issql
					DO FORM snake NAME lsnake LINKED WITH 'Počítam stav zásob'
					lsnake.DRAW()
					m.vysledok=sql_exec("SELECT food600.gen_stav_a("+ALLTRIM(STR(m.hladajcstredisk,10,0))+","+ALLTRIM(STR(m.d1,10,0))+","+ALLTRIM(STR(m.giakt_mena,2,0))+",'"+m.menobuffera+"',"+ ALLTRIM(STR(m.cdata,2,0))+")","skupiny")
					IF m.vysledok>0
						rv("genstav","skl_i")
						m.vysledok=sql_exec("SELECT food600.gen_stav_maz('"+m.menobuffera+"',"+ ALLTRIM(STR(m.cdata,2,0))+")","skupiny")
					ENDIF
					lsnake.RELEASE()
				ELSE
					USE (m.fnoa) IN 0 ALIAS obraty
					DO gen_stav WITH m.d1, m.hladajcstredisk, m.akt_mena
					USE IN obraty
				ENDIF
				IF m.issql
					rv("zostobr_obraty","obra")
					SELECT m.hladajcstredisk AS c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
						FROM obra WHERE c_dokl>=m.d1 .AND. c_dokl<=m.d2 .AND. INLIST(LEFT(operace,1),"V","P") .AND. c_karty>0  INTO CURSOR obratyx READWRITE;
						UNION SELECT m.hladajcstredisk AS c_stredisk,"PR" AS operace, 0 AS c_dokl, c_karty, zustatek AS mnozstvi, cena_prum AS cena, $0 AS cena_part,;
						zustatek, cena_prum, 0 AS dan, "" AS serial_no,c_karty AS poradi, 0 AS idriadok, spdan, spstupne FROM skl_i WHERE c_karty>0 .AND. zustatek<>0
				ELSE
					USE (m.fno) IN 0 ALIAS obra
					SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
						FROM obra WHERE c_dokl>=m.d1 .AND. c_dokl<=m.d2 .AND. INLIST(LEFT(operace,1),"V","P") .AND. c_stredisk=m.hladajcstredisk .AND. c_karty>0 INTO CURSOR obratyx READWRITE;
						UNION SELECT c_stredisk,"PR" AS operace, 0 AS c_dokl, c_karty, zustatek AS mnozstvi, cena_prum AS cena, $0 AS cena_part,;
						zustatek, cena_prum, 0 AS dan, "" AS serial_no,c_karty AS poradi, 0 AS idriadok, spdan, spstupne FROM skl_i WHERE c_karty>0 .AND. zustatek<>0
				ENDIF
				INSERT INTO doklady (c_dokl, c_stredisk, operace, c_mena) VALUES (0, m.hladajcstredisk, "PR", m.giakt_mena)

				IF m.d2arch
					IF m.issql
						rv("zostobr_obraty_a","obra")
						SELECT m.hladajcstredisk AS c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
							FROM obra WHERE c_dokl>=m.d1 .AND. c_dokl<=m.d2 .AND. INLIST(LEFT(operace,1),"V","P") .AND. c_karty>0;
							UNION SELECT m.hladajcstredisk AS c_stredisk,"PR" AS operace, 0 AS c_dokl, c_karty, zustatek AS mnozstvi, cena_prum AS cena, $0 AS cena_part,;
							zustatek, cena_prum, 0 AS dan, "" AS serial_no,c_karty AS poradi, 0 AS idriadok, spdan, spstupne FROM skl_i WHERE c_karty>0 .AND. zustatek<>0 INTO CURSOR obratyx READWRITE
					ELSE
						USE (m.fnoa) IN 0 ALIAS obra
						SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
							FROM obra WHERE c_dokl>=m.d1 .AND. c_dokl<=m.d2 .AND. INLIST(LEFT(operace,1),"V","P") .AND. c_stredisk=m.hladajcstredisk .AND. c_karty>0;
							UNION SELECT c_stredisk,"PR" AS operace, 0 AS c_dokl, c_karty, zustatek AS mnozstvi, cena_prum AS cena, $0 AS cena_part,;
							zustatek, cena_prum, 0 AS dan, "" AS serial_no,c_karty AS poradi, 0 AS idriadok, spdan, spstupne FROM skl_i WHERE c_karty>0 .AND. zustatek<>0 INTO CURSOR obratyx READWRITE
					ENDIF
					DO pozavirej WITH "obra"
				ELSE
					IF m.issql
						rv("zostobr_obraty","obra")
						rv("zostobr_obraty_a","obra_a")
						SELECT m.hladajcstredisk AS c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
							FROM obra_a WHERE c_dokl>=m.d1 .AND. INLIST(LEFT(operace,1),"V","P") .AND. c_karty>0;
							UNION SELECT m.hladajcstredisk AS c_stredisk,"PR" AS operace, 0 AS c_dokl, c_karty, zustatek AS mnozstvi, cena_prum AS cena, $0 AS cena_part,;
							zustatek, cena_prum, 0 AS dan, "" AS serial_no,c_karty AS poradi, 0 AS idriadok, spdan, spstupne FROM skl_i WHERE c_karty>0 .AND. zustatek<>0;
							UNION  SELECT m.hladajcstredisk AS c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
							FROM obra WHERE c_dokl<=m.d2 .AND. INLIST(LEFT(operace,1),"V","P") .AND. operace<>"PR" .AND. c_karty>0 INTO CURSOR obratyx READWRITE
					ELSE
						USE (m.fnoa) IN 0 ALIAS obra_a
						USE (m.fno) IN 0 ALIAS obra
						SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
							FROM obra_a WHERE c_dokl>=m.d1 .AND. INLIST(LEFT(operace,1),"V","P") .AND. c_stredisk=m.hladajcstredisk .AND. c_karty>0;
							UNION SELECT c_stredisk,"PR" AS operace, 0 AS c_dokl, c_karty, zustatek AS mnozstvi, cena_prum AS cena, $0 AS cena_part,;
							zustatek, cena_prum, 0 AS dan, "" AS serial_no,c_karty AS poradi, 0 AS idriadok, spdan, spstupne FROM skl_i WHERE c_karty>0 .AND. zustatek<>0;
							UNION  SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
							FROM obra WHERE c_dokl<=m.d2 .AND. INLIST(LEFT(operace,1),"V","P") .AND. c_stredisk=m.hladajcstredisk .AND. operace<>"PR" .AND. c_karty>0 INTO CURSOR obratyx READWRITE
					ENDIF
					DO pozavirej WITH "obra, obra_a"
				ENDIF
				SELECT obraty
				INDEX ON c_dokl TAG c_dokl
			ELSE
				IF m.issql
					DO FORM snake NAME lsnake LINKED WITH jaz('form_pocitam_stav_zasob')
					lsnake.DRAW()
					m.vysledok=sql_exec("SELECT food600.gen_stav("+ALLTRIM(STR(m.hladajcstredisk,10,0))+","+ALLTRIM(STR(m.d1,10,0))+","+ALLTRIM(STR(m.giakt_mena,2,0))+",'"+m.menobuffera+"',"+ ALLTRIM(STR(m.cdata,2,0))+")","skupiny")
					IF m.vysledok>0
						rv("genstav","skl_i")
						m.vysledok=sql_exec("SELECT food600.gen_stav_maz('"+m.menobuffera+"',"+ ALLTRIM(STR(m.cdata,2,0))+")","skupiny")
					ENDIF
					lsnake.RELEASE()
				ELSE
					USE (m.fno) IN 0 ALIAS obraty
					DO gen_stav WITH m.d1, m.hladajcstredisk, m.akt_mena
					USE IN obraty
				ENDIF
				INSERT INTO doklady (c_dokl, c_stredisk, operace, c_mena) VALUES (0, m.hladajcstredisk, "PR", m.giakt_mena)
				IF m.issql
					rv("zostobr_obraty","obra")
					SELECT m.hladajcstredisk AS c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
						FROM obra WHERE c_dokl>=m.d1 .AND. c_dokl<=m.d2 .AND. INLIST(LEFT(operace,1),"V","P") .AND. c_karty>0 INTO CURSOR obratyx READWRITE;
						UNION SELECT m.hladajcstredisk AS c_stredisk,"PR" AS operace, 0 AS c_dokl, c_karty, zustatek AS mnozstvi, cena_prum AS cena, $0 AS cena_part,;
						zustatek, cena_prum, 0 AS dan, "" AS serial_no,c_karty AS poradi, 0 AS idriadok, spdan, spstupne FROM skl_i WHERE c_karty>0 .AND. zustatek<>0
				ELSE
					USE (m.fno) IN 0 ALIAS obra
					SELECT c_stredisk, operace, c_dokl, c_karty, mnozstvi, cena, cena_part,zustatek, cena_prum, dan, serial_no,poradi, idriadok, spdan, spstupne ;
						FROM obra WHERE c_dokl>=m.d1 .AND. c_dokl<=m.d2 .AND. INLIST(LEFT(operace,1),"V","P") .AND. c_stredisk=m.hladajcstredisk .AND. c_karty>0 INTO CURSOR obratyx READWRITE;
						UNION SELECT c_stredisk,"PR" AS operace, 0 AS c_dokl, c_karty, zustatek AS mnozstvi, cena_prum AS cena, $0 AS cena_part,;
						zustatek, cena_prum, 0 AS dan, "" AS serial_no,c_karty AS poradi, 0 AS idriadok, spdan, spstupne FROM skl_i WHERE c_karty>0 .AND. zustatek<>0
				ENDIF
				DO pozavirej WITH "obra"
			ENDIF
			*** Spocitanie pociatocneho stavu na sklade
			IF USED("rezie")
				DELETE FROM skl_i WHERE c_karty in (select c_karty FROM rezie)
				DELETE FROM obratyx WHERE (c_karty in (select c_karty FROM rezie)) AND !(INLIST(operace,"VP","PP","_P"))
			ENDIF 
			SELECT skl_i
			SUM ROUND(MTON(zustatek) * MTON(cena_prum) ,4) TO  m.cena_poc ALL
			m.cena_poc=ROUND(m.cena_poc,2)
			*** Spocitanie pociatocneho stavu na sklade
			*** Spocitanie konecneho stavu este pred zmazanim nevybranych operacii, aby to bol realny konecny stav na sklade
			SELECT  obratyx
			SET RELATION TO c_dokl INTO doklady
			SUM ROUND(MTON(mnozstvi) * MTON(cena) * en__meny(doklady.c_mena,m.girep_mena),4) TO  m.cena_kon FOR INLIST(LEFT(obratyx.operace, 1), "P", "V")
			m.cena_kon=ROUND(m.cena_kon,2)
			SET RELATION OFF INTO doklady
			*** Spocitanie konecneho stavu este pred zmazanim nevybranych operacii, aby to bol realny konecny stav na sklade
			IF LEN(m.tlac_operace)=2
				IF m.tlac_operace<>"__"
					DELETE FOR operace<>m.tlac_operace AND operace<>"KK" AND c_dokl<>0
				ENDIF
			ELSE
				DELETE FOR !INLIST(operace,&operace__txt) AND operace<>"KK"	AND c_dokl<>0
			ENDIF
			DO pozavirej WITH "skl_i"
			m.fn_obraty_tlac="obratyx"
			m.fn_dokl_dokl="doklady"
			m.fn_doklady_tlac="doklady"
			rodic.operace=m.tlac_operace
			rodic.d1=m.d1
			rodic.d2=m.d2
			DO s_obr_xxx WITH m.tlac_operace, m.gcpgmparam
		ENDIF
	ENDIF
	DO pozavirej WITH "doklx"

ELSE

	= MESSAGEBOX(jaz("ted nelze, zkuste pozdeji"),0+16+256, jaz("m_s_obr vyber obdobi"), 3333)
ENDIF

*!*****************************************************************************
PROCEDURE s_liga_zbozi
IF !USED("pom")
	RETURN 
ENDIF 
IF "1" $ m.dily OR "2" $ m.dily 
	IF m.tlac_operace="__"
		SELECT stredisko, spart, nazev, druh, ;
			SUM(mnozstvi) AS mnozstvi, SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS prachy_par, $0 AS sumprachy ;
			GROUP BY stredisko, spart, nazev FROM pom INTO CURSOR  BUFFER READWRITE WHERE INLIST(LEFT(operace, 1), "P", "V") 
	ELSE
		SELECT stredisko, spart, nazev, druh, ;
			SUM(mnozstvi) AS mnozstvi, SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS prachy_par, $0 AS sumprachy ;
			GROUP BY stredisko, spart, nazev FROM pom INTO CURSOR  BUFFER READWRITE WHERE INLIST(LEFT(operace, 1), "P", "V") AND INLIST(operace,&operace__txt)
	ENDIF 
ENDIF 
IF "1" $ m.dily
	SELECT buffer
	INDEX ON stredisko + spart + STR(mnozstvi, 12) TAG px
	repo_txt = jaz("podle množství")
	DO pr_report WITH "obrliga@mnoz"
ENDIF

IF "2" $ m.dily
	SELECT buffer
	INDEX ON stredisko + spart + STR(prachy, 12) TAG px
	repo_txt = jaz("podle peněz")
	DO pr_report WITH "obrliga"
ENDIF

IF "3" $ m.dily

	DO liga2
ENDIF

IF "4" $ m.dily
	DO pozavirej WITH "buffer"
	IF m.tlac_operace="__"
		SELECT stredisko, spart, nazev, druh, ;
			SUM(mnozstvi) AS mnozstvi, cena, SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS prachy_par, $0 AS sumprachy ;
			GROUP BY stredisko, spart, nazev, cena FROM pom INTO CURSOR  BUFFER READWRITE WHERE INLIST(LEFT(operace, 1), "P", "V") ORDER BY stredisko, spart, cena
	ELSE
		SELECT stredisko, spart, nazev, druh, ;
			SUM(mnozstvi) AS mnozstvi, cena, SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS prachy_par, $0 AS sumprachy ;
			GROUP BY stredisko, spart, nazev, cena FROM pom INTO CURSOR  BUFFER READWRITE WHERE INLIST(LEFT(operace, 1), "P", "V") AND INLIST(operace,&operace__txt) ORDER BY stredisko, spart, cena
	ENDIF 
	repo_txt = jaz("podle jc")
	DO pr_report WITH "obrliga2"
ENDIF

USE
DO pozavirej WITH "buffer"

*       *       *       *       *       *       *       *       *       *
PROCEDURE liga2
SELECT SPACE(30) AS partner ,obratyx.c_karty, SUM(obratyx.mnozstvi) AS mnozstvi, SUM(NTOM(MTON(obratyx.cena)*obratyx.mnozstvi*en__meny(doklady.c_mena,m.girep_mena))) AS prachy, doklady.c_partner,karty.nazev, karty.c_druhs AS c_druh, $0 AS proc_skup, $0 AS sum_procsk, "1" AS radic, SPACE(m.sirkastlpcadruh) AS druh ;
	WHERE INLIST(obratyx.operace,"PE") AND BETWEEN(obratyx.c_dokl, m.d1, m.d2);
	FROM obratyx LEFT JOIN karty ON obratyx.c_karty=karty.c_karty LEFT JOIN doklady ON obratyx.c_dokl=doklady.c_dokl ;
	GROUP BY obratyx.c_karty, karty.nazev, karty.c_druhs, doklady.c_partner INTO CURSOR px READWRITE
UPDATE px SET druh=skupiny.druh FROM skupiny WHERE px.c_druh=skupiny.c_druh
REPLACE FOR EMPTY(druh) druh WITH m.ostatni
SELECT selkasy
INDEX ON c_partner TO (m.gcpri_path+"selk")
SELECT px
SET RELATION TO c_partner INTO selkasy
DELETE FOR !selkasy.lbl
SET RELATION OFF INTO selkasy
SELECT selkasy
SET ORDER TO id_zkratka
SELECT px
SET RELATION TO druh INTO seldruhy
DELETE FOR !seldruhy.lbl
SET RELATION TO

SELECT c_karty, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, nazev, druh, proc_skup, sum_procsk, radic ;
	FROM px GROUP BY c_karty,nazev, druh INTO CURSOR p1 READWRITE

SELECT druh, SUM(prachy) AS prachy ;
	GROUP BY druh FROM p1 INTO CURSOR p1_sum READWRITE
INDEX ON druh TAG druh

SELECT p1
SET RELATION TO druh INTO p1_sum

REPLACE proc_skup WITH prachy / p1_sum.prachy * 100 FOR ABS(p1_sum.prachy)>0.001
REPLACE proc_skup WITH 0  FOR ABS(p1_sum.prachy)<=0.001
INDEX ON druh + radic + STR(9999-proc_skup, 10, 5) TAG druh_proc	&& aby byl druh ASC, procento DESC

o_druh = "v1f2r3w4s5"
sum_procsk = 0
SCAN
	IF druh # m.o_druh
		m.sum_procsk = 0
		o_druh = druh
	ENDIF
	sum_procsk = m.sum_procsk + proc_skup
	GATHER FIELDS sum_procsk MEMVAR
ENDSCAN
IF !prazdna()
	m.txt="PE"
	DO pr_report WITH "safino@pe"
ENDIF
DO pozavirej WITH "p1, px, p1_sum"


SELECT SPACE(30) AS partner ,obratyx.c_karty, SUM(obratyx.mnozstvi) AS mnozstvi, SUM(NTOM(MTON(obratyx.cena)*MTON(obratyx.mnozstvi)*en__meny(doklady.c_mena,m.girep_mena))) AS prachy, doklady.c_partner,karty.nazev, karty.c_druhs AS c_druh, SPACE(m.sirkastlpcadruh) AS druh, $0 AS proc_skup, $0 AS sum_procsk, "1" AS radic ;
	WHERE INLIST(obratyx.operace,"VE") AND BETWEEN(obratyx.c_dokl, m.d1, m.d2);
	FROM obratyx LEFT JOIN karty ON obratyx.c_karty=karty.c_karty LEFT JOIN doklady ON obratyx.c_dokl=doklady.c_dokl ;
	GROUP BY obratyx.c_karty, karty.nazev, karty.c_druhs, doklady.c_partner INTO CURSOR px READWRITE
UPDATE px SET druh=skupiny.druh FROM skupiny WHERE px.c_druh=skupiny.c_druh
REPLACE FOR EMPTY(druh) druh WITH m.ostatni
SELECT selkasy
INDEX ON c_partner TO (m.gcpri_path+"selk")
SELECT px
SET RELATION TO c_partner INTO selkasy
DELETE FOR !selkasy.lbl
SET RELATION OFF INTO selkasy
SELECT selkasy
SET ORDER TO id_zkratka
SELECT px
SET RELATION TO druh INTO seldruhy
DELETE FOR !seldruhy.lbl
SET RELATION TO

SELECT c_karty, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, nazev, druh, proc_skup,sum_procsk, radic ;
	FROM px GROUP BY c_karty,nazev, druh INTO CURSOR p1 READWRITE

SELECT druh, SUM(prachy) AS prachy ;
	GROUP BY druh FROM p1 INTO CURSOR p1_sum READWRITE
INDEX ON druh TAG druh

SELECT p1
IF RECCOUNT()>0
	SET RELATION TO druh INTO p1_sum

	REPLACE proc_skup WITH prachy / p1_sum.prachy * 100 FOR ABS(p1_sum.prachy)>0.001
	REPLACE proc_skup WITH 0  FOR ABS(p1_sum.prachy)<=0.001
ENDIF
INDEX ON druh + radic + STR(9999-proc_skup, 10, 5) TAG druh_proc	&& aby byl druh ASC, procento DESC

o_druh = "v1f2r3w4s5"
sum_procsk = 0
SCAN
	IF druh # m.o_druh
		m.sum_procsk = 0
		o_druh = druh
	ENDIF
	sum_procsk = m.sum_procsk + proc_skup
	GATHER FIELDS sum_procsk MEMVAR
ENDSCAN
IF !prazdna()
	m.txt="VE"
	DO pr_report WITH "safino@ve"
ENDIF
DO pozavirej WITH "p1, px, p1_sum"



*!*****************************************************************************
PROCEDURE s_obr1a

IF !USED("pom")
	RETURN 
ENDIF 
SELECT pom

SELECT DISTINCT stredisko, druh, partner, spart, stredskup, ;
	SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS prachy_par, SUM(NTOM(MTON(prachysdph)*en__meny(c_mena,m.girep_mena))) AS prachysdph, SUM(mnozstvi*NTOM(MTON(cena1)*en__meny(c_mena,m.girep_mena))) AS prachy1 ;
	GROUP BY stredisko, druh, partner FROM pom INTO CURSOR BUFFER READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
IF "1" $ m.dily
	INDEX ON stredskup + partner + spart + druh TO (m.fn_pomidx)
	DO pr_report WITH "OBR_UCT2"
ENDIF

IF "2" $ m.dily
	INDEX ON spart + druh + stredskup + partner TO (m.fn_pomidx)
	DO pr_report WITH "OBR_UCT1"
ENDIF
DO pozavirej WITH "buffer"

*!*****************************************************************************
PROCEDURE s_obr1b

IF !USED("pom")
	RETURN 
ENDIF 
SELECT pom
IF "1" $ m.dily
	SELECT DISTINCT operace, partner, spart, druh, stredisko, typ_pohybu,;
		SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS prachy_par, SUM(NTOM(MTON(prachysdph)*en__meny(c_mena,m.girep_mena))) AS prachysdph, SUM(mnozstvi*NTOM(MTON(cena1)*en__meny(c_mena,m.girep_mena))) AS prachy1 ;
		GROUP BY operace, typ_pohybu, partner, spart, druh FROM pom INTO CURSOR BUFFER READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
	DO pr_report WITH "O_OPSD"
ENDIF

IF "2" $ m.dily
	SELECT c_partner, spart, druh, cen_hlad, c_stredisk, stredisko, partner, SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, ;
		prachy AS prachy1, prachy AS prachy2, prachy AS prachy3, prachy AS prachy4, prachy AS prachy5, ;
		prachy AS prachy6, prachy AS prachy7, prachy AS prachy8, prachy AS prachy9 ;
		GROUP BY 1,2,3,4 FROM pom INTO CURSOR BUFFER READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)

	REPLACE ALL prachy1 WITH 0, prachy2 WITH 0, prachy3 WITH 0, prachy4 WITH 0, prachy5 WITH 0, prachy6 WITH 0, prachy7 WITH 0, prachy8 WITH 0, prachy9 WITH 0
	SCAN
		DO CASE
			CASE cen_hlad = "2" OR cen_hlad="02"
				REPLACE prachy2 WITH prachy
			CASE cen_hlad = "3" OR cen_hlad="03"
				REPLACE prachy3 WITH prachy
			CASE cen_hlad = "4" OR cen_hlad="04"
				REPLACE prachy4 WITH prachy
			CASE cen_hlad = "5" OR cen_hlad="05"
				REPLACE prachy5 WITH prachy
			CASE cen_hlad = "6" OR cen_hlad="06"
				REPLACE prachy6 WITH prachy
			CASE cen_hlad = "7" OR cen_hlad="07"
				REPLACE prachy7 WITH prachy
			CASE cen_hlad = "8" OR cen_hlad="08"
				REPLACE prachy8 WITH prachy
			CASE cen_hlad = "9" OR cen_hlad="09"
				REPLACE prachy9 WITH prachy
			OTHER
				REPLACE prachy1 WITH prachy
		ENDCASE
	ENDSCAN
	SELECT c_partner, spart, druh, c_stredisk, stredisko, partner, ;
		SUM(prachy1) AS prachy1, SUM(prachy2) AS prachy2, SUM(prachy3) AS prachy3, ;
		SUM(prachy4) AS prachy4, SUM(prachy5) AS prachy5, ;
		SUM(prachy6) AS prachy6, SUM(prachy7) AS prachy7, ;
		SUM(prachy8) AS prachy8, SUM(prachy9) AS prachy9  ;
		GROUP BY 1,2,3,4 FROM BUFFER INTO CURSOR obr77

	DO pr_report WITH "O_PDR_HL"
ENDIF

DO pozavirej WITH "buffer, obr77, prir1"

*!*****************************************************************************
PROCEDURE s_obr4_2

IF !USED("pom")
	RETURN 
ENDIF 
IF "SPITAL" $ UPPER(m.gchom_path)

	SELECT stredskup, partner, c_karty, nazev, SUM(mnozstvi) AS vydano, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS pr_vydano, "  " as skupina ;
		GROUP BY 1, 2, 3 WHERE mnozstvi # 0 AND LEFT(operace, 1) = "V" AND BETWEEN(c_dokl, m.d1, m.d2) FROM pom INTO CURSOR vydano

	SELECT stredskup, partner, c_karty, nazev, SUM(mnozstvi) AS prijato, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS pr_prijato, $0 AS vydano, $0 AS pr_vydano, "  " as skupina ;
		GROUP BY 1, 2, 3 WHERE mnozstvi # 0 AND LEFT(operace, 1) = "P" AND BETWEEN(c_dokl, m.d1, m.d2) FROM pom INTO CURSOR prijato1 READWRITE
ELSE

	SELECT stredskup, partner, c_karty, nazev, SUM(mnozstvi) AS vydano, SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS pr_vydano, "  " as skupina ;
		GROUP BY 1, 2, 3 WHERE mnozstvi # 0 AND LEFT(operace, 1) = "V" AND BETWEEN(c_dokl, m.d1, m.d2) FROM pom INTO CURSOR vydano

	SELECT stredskup, partner, c_karty, nazev + "  " as nazev, SUM(mnozstvi) AS prijato, SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS pr_prijato, $0 AS vydano, $0 AS pr_vydano, "  " as skupina ;
		GROUP BY 1, 2, 3 WHERE mnozstvi # 0 AND LEFT(operace, 1) = "P" AND BETWEEN(c_dokl, m.d1, m.d2) FROM pom INTO CURSOR prijato1 READWRITE
ENDIF

INSERT INTO prijato1 (stredskup, partner, c_karty, nazev, vydano, pr_vydano, skupina) SELECT stredskup, partner, c_karty, nazev, vydano, pr_vydano, skupina FROM vydano

IF ATC("HLOBIK", m.wi_zakaz) > 0    &&& pridaji se suroviny z prvni urovne receptur     zacatek
    SELECT stredskup, partner, vydano.c_karty, vydano, polozky.c_poloz, polozky.hruba, polozky.pocet ;
           FROM vydano LEFT JOIN (m.fn_polozky) ON polozky.c_karty = vydano.c_karty ;
           INTO table prodane_polozky1 READWRITE
    DELETE FOR ISNULL(c_poloz)    &&*REPLACE FOR ISNULL(c_poloz) c_poloz WITH c_karty, hruba WITH 1, pocet WITH 1
	REPLACE pocet WITH 1 FOR pocet=0 OR ISNULL(pocet)
    SELECT pp.stredskup, pp.partner, pp.c_poloz as c_karty, karty.nazev, SUM(pp.vydano * pp.hruba / pp.pocet) as vydano, karty.cena39 as pr_prijato ;
           GROUP BY 1,2,3 ORDER BY partner, nazev ;
           FROM prodane_polozky1 pp LEFT JOIN karty ON karty.c_karty = pp.c_poloz ;
           INTO table prodane_polozky2 READWRITE
    REPLACE ALL pr_prijato WITH pr_prijato * vydano
    INSERT INTO prijato1 (stredskup, partner, c_karty,         nazev, vydano, pr_vydano, pr_prijato, skupina) ;
                   SELECT stredskup, partner, c_karty, "__ " + nazev, vydano,     0,     pr_prijato,  "sur" FROM prodane_polozky2 
ENDIF                                                                 &&& pridaji se suroviny z prvni urovne receptur      konec

SELECT stredskup, partner, skupina, nazev, c_karty, SUM(prijato) AS prijato, SUM(pr_prijato) AS pr_prijato, -SUM(vydano) AS vydano, -SUM(pr_vydano) AS pr_vydano, $0 AS cena ;
       GROUP BY 1, 2, 3, 4 FROM prijato1 INTO CURSOR prijato READWRITE
REPLACE FOR prijato-vydano # 0 cena WITH (pr_prijato - pr_vydano) / (prijato-vydano)
RELEASE sox_had
IF "1" $ m.dily
	SELECT DISTINCT nazev AS druh, .T. AS lbl FROM prijato INTO CURSOR vybery READWRITE
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT prijato
			DELETE FROM prijato WHERE nazev IN (SELECT druh AS nazev FROM vybery WHERE EMPTY(lbl))
		ENDIF
	ENDIF
	SELECT prijato
	IF RECCOUNT()>0
		DO pr_report WITH "zboz_par"
	ENDIF
ENDIF

IF "2" $ m.dily
	SELECT partner, c_dokl, doklad, datum_dokl, operace, pz1, pz2, SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy,SUM(MTON(mnozstvi)*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachypar ;
		GROUP BY 1, 2 FROM pom INTO CURSOR par_dokl WHERE BETWEEN(c_dokl, m.d1, m.d2)
	DO pr_report WITH "par_dokl@2"
ENDIF

IF "3" $ m.dily
	IF !USED("vybery")
		SELECT DISTINCT nazev AS druh, .T. AS lbl FROM prijato INTO CURSOR vybery READWRITE
		INDEX ON druh TAG druh
		IF RECCOUNT()>0
			DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
			SELECT vybery
			LOCATE FOR !EMPTY(lbl)
			IF FOUND()
				SELECT prijato
				DELETE FROM prijato WHERE nazev IN (SELECT druh AS nazev FROM vybery WHERE EMPTY(lbl))
			ENDIF
		ENDIF
	ENDIF
	SELECT prijato
	SCAN
		SCATTER FIELD partner MEMVAR
		REPLACE partner WITH nazev, nazev WITH m.partner
	ENDSCAN
	INDEX ON partner + nazev TAG pn
	DO pr_report WITH "zboz_par@3"
ENDIF

IF ATC("HLOBIK", m.wi_zakaz) > 0
    SET ORDER TO c_karty IN karty
    SELECT prijato1
    SET RELATION TO c_karty INTO karty
    REPLACE ALL                partner WITH karty.druh_skl
    REPLACE FOR EMPTY(partner) partner WITH karty.druh
    SELECT "  " as stredskup, partner, skupina, nazev, c_karty, SUM(prijato) AS prijato, SUM(pr_prijato) AS pr_prijato, SUM(vydano) AS vydano, SUM(pr_vydano) AS pr_vydano, $0 as cena ;
           GROUP BY 1, 2, 3, 4 FROM prijato1 INTO CURSOR prijato READWRITE
    REPLACE FOR prijato-vydano # 0 cena WITH (pr_prijato - pr_vydano) / (prijato-vydano)
    DO pr_report WITH "zboz_par@4"
ENDIF


DO pozavirej WITH "vydano,prijato1,prijato,par_dokl, vybery"

*!*****************************************************************************
PROCEDURE s_obr4_dat_dokl

sox_had = CREATEOBJECT("had")
sox_had.SHOW()
IF m.tlac_operace="__"
	SELECT c_stredisk, c_dokl, operace, c_karty, SPACE(30) AS nazev, mnozstvi, cena, cena_part, 0 AS cen_pardph, ;
		mnozstvi * cena AS prachy, mnozstvi * cena_part AS prachy_par, 0 AS prachysdph, ;
		00000000 AS c_partner, SPACE(30) AS partner, {^1900-01-01} AS datum_dokl, 0000000000 AS doklad, SPACE(15) AS pz1, SPACE(15) AS pz2, ;
		SPACE(30) AS stredisko, $0 AS cena1, SPACE(13) AS kod, "    " AS stredskup ;
		WHERE c_stredisk = m.cislostrediska AND INLIST(LEFT(operace, 1), "P", "V") AND BETWEEN(c_dokl, m.d1, m.d2);
		ORDER BY c_stredisk, c_dokl FROM obratyx INTO CURSOR pom4 READWRITE
ELSE
	SELECT c_stredisk, c_dokl, operace, c_karty, SPACE(30) AS nazev, mnozstvi, cena, cena_part, 0 AS cen_pardph, ;
		mnozstvi * cena AS prachy, mnozstvi * cena_part AS prachy_par, 0 AS prachysdph, ;
		00000000 AS c_partner, SPACE(30) AS partner, {^1900-01-01} AS datum_dokl, 0000000000 AS doklad, SPACE(15) AS pz1, SPACE(15) AS pz2, ;
		SPACE(30) AS stredisko, $0 AS cena1, SPACE(13) AS kod, "    " AS stredskup ;
		WHERE c_stredisk = m.cislostrediska AND INLIST(LEFT(operace, 1), "P", "V") AND INLIST(operace,&operace__txt) AND BETWEEN(c_dokl, m.d1, m.d2);
		ORDER BY c_stredisk, c_dokl FROM obratyx INTO CURSOR pom4 READWRITE
ENDIF 

sox_had.set_had(33)
REPLACE FOR LEFT(operace, 1) # "V" .AND. operace # "PI" prachy_par WITH prachy, cena_part WITH cena

sox_had.set_had(66)
SET RELATION TO c_dokl INTO doklady
REPLACE ALL c_partner WITH doklady.c_partner, doklad WITH doklady.doklad, ;
	datum_dokl WITH doklady.datum_dokl, pz1 WITH doklady.pz1, pz2 WITH doklady.pz2
REPLACE ALL cena WITH NTOM(MTON(cena)*en__meny(doklady.c_mena,m.giakt_mena)),;
	cena_part WITH NTOM(MTON(cena_part)*en__meny(doklady.c_mena,m.giakt_mena)),;
	cen_pardph WITH NTOM(MTON(cen_pardph)*en__meny(doklady.c_mena,m.giakt_mena)),;
	prachy WITH NTOM(MTON(prachy)*en__meny(doklady.c_mena,m.giakt_mena)),;
	prachy_par WITH NTOM(MTON(prachy_par)*en__meny(doklady.c_mena,m.giakt_mena)),;
	prachysdph WITH NTOM(MTON(prachysdph)*en__meny(doklady.c_mena,m.giakt_mena))
SET RELATION TO c_partner INTO partneri
REPLACE FOR ! EMPTY(partneri.partner) partner WITH partneri.partner
SET RELATION TO c_stredisk INTO partneri
REPLACE FOR ! EMPTY(partneri.partner) stredisko WITH partneri.partner

sox_had.set_had(88)
SET RELATION TO c_karty INTO karty
IF m.issql
	REPLACE FOR EMPTY(nazev) nazev WITH karty.nazev, kod WITH karty.kod
	UPDATE pom4 SET cena1=ckarty.cena FROM ckarty WHERE pom4.c_karty=ckarty.c_karty
ELSE
	REPLACE FOR EMPTY(nazev) nazev WITH karty.nazev, cena1 WITH karty.&cena_teor, kod WITH karty.kod
ENDIF
*!*	SET RELATION TO partner INTO food_dat
*!*	REPLACE ALL stredskup WITH food_dat.stredskup
*!*	SET RELATION TO
UPDATE pom4 SET stredskup=food_dat.stredskup FROM food_dat WHERE pom4.partner=food_dat.partner

SELECT partner, nazev, SUM(mnozstvi) AS vydano, SUM(prachy) AS pr_vydano ;
	GROUP BY 1, 2 WHERE mnozstvi # 0 AND LEFT(operace, 1) = "V" FROM pom4 INTO CURSOR vydano

SELECT partner, nazev, SUM(mnozstvi) AS prijato, SUM(prachy) AS pr_prijato, 0000000.0000 AS vydano, 0000000.0000 AS pr_vydano ;
	GROUP BY 1, 2 WHERE mnozstvi # 0 AND LEFT(operace, 1) = "P" FROM pom4 INTO CURSOR prijato1 READWRITE
INSERT INTO prijato1 (partner, nazev, vydano, pr_vydano) SELECT partner, nazev, vydano, pr_vydano FROM vydano

SELECT partner, nazev, SUM(prijato) AS prijato, SUM(pr_prijato) AS pr_prijato, -SUM(vydano) AS vydano, -SUM(pr_vydano) AS pr_vydano, 000000.0 AS cena ;
	GROUP BY 1, 2 FROM prijato1 INTO CURSOR prijato READWRITE
REPLACE FOR prijato-vydano # 0 cena WITH (pr_prijato - pr_vydano) / (prijato-vydano)

RELEASE sox_had
IF "1" $ m.dily
	DO pr_report WITH "zboz_par@x"
ENDIF

IF "2" $ m.dily

	**** bacha, je tady filtr na DATUM_DOKL
	SELECT partner, c_dokl, doklad, datum_dokl, operace, pz1, pz2, SUM(mnozstvi*cena) AS prachy,SUM(mnozstvi*NTOM(MTON(cena_part)*en__meny(c_mena,m.girep_mena))) AS prachypar ;
		WHERE BETWEEN( datum_dokl, m.d1_dat_dokl, m.d2_dat_dokl ) GROUP BY 1, 2 ;
		FROM pom4 INTO CURSOR par_dokl
	o_d1_txt = m.d1_txt
	o_d2_txt = m.d2_txt

	d1_txt = DTOC(m.d1_dat_dokl)
	d2_txt = DTOC(m.d2_dat_dokl)

	DO pr_report WITH "par_dokl@zp"

	d1_txt = m.o_d1_txt
	d2_txt = m.o_d2_txt
ENDIF
DO pozavirej WITH "vydano,prijato1,pom4,prijato,par_dokl"

*!*****************************************************************************
PROCEDURE s_obr4
IF !USED("pom")
	RETURN 
ENDIF 
SELECT pom

IF "1" $ m.dily
	SELECT * FROM pom WHERE BETWEEN(c_dokl, m.d1, m.d2) INTO CURSOR pomxx ORDER BY c_dokl
	INDEX ON c_dokl TO (m.gcpri_path + "s_obr4")
	DO pr_report WITH "obrpre"
	DO pozavirej WITH "pomxx"
ENDIF

IF "2" $ m.dily

	SELECT c_dokl, spart + " " AS spart, SIGN(mnozstvi) AS znamenko, doklad, partner, datum_dokl, operace, provedl, ;
		pz1, pz2, SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy,SUM(MTON(mnozstvi)*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachypar ;
		GROUP BY 1, 2, 3 FROM pom INTO CURSOR dok_spart READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
	REPLACE FOR operace = "PI" .AND. znamenko < 0 spart WITH "- " + spart
	DO pr_report WITH "dok_spar"
	DO pozavirej WITH "dok_spart"
ENDIF

IF "3" $ m.dily
	SELECT partner, c_dokl, doklad, datum_dokl, operace, dan, pz1, pz2, ;
		SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy, SUM(MTON(mnozstvi)*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachypar ;
		GROUP BY 1, 2 FROM pom INTO CURSOR par_dokl READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
	REPLACE FOR operace = "PE" prachypar WITH prachy * dan
	DO pr_report WITH "par_dokl@3"
	DO pozavirej WITH "par_dokl"
ENDIF
IF "4" $ m.dily
	SELECT platba, partner, c_dokl, doklad, datum_dokl, operace, dan, pz1, pz2, ;
		SUM(MTON(mnozstvi)*NTOM(MTON(cena)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(MTON(mnozstvi)*NTOM(MTON(cena_part)*en__meny(c_mena,m.girep_mena))) AS prachypar ;
		GROUP BY 1, 2, 3 FROM pom INTO CURSOR par_dokl READWRITE
	REPLACE FOR operace = "PE" prachypar WITH prachy * dan
	DO pr_report WITH "obr_plat_par_dokl"
	DO pozavirej WITH "par_dokl"
ENDIF

*!*****************************************************************************
PROCEDURE s_obr6a
DO s_obr6 WITH "1"

*!*****************************************************************************
PROCEDURE s_obr6b
DO s_obr6 WITH "2"

*!*****************************************************************************
PROCEDURE s_obr6
PARAMETERS jakesestavy
IF !USED("pom")
	RETURN 
ENDIF 
SELECT pom

IF ("STRAH" $ UPPER(m.gchom_path))                                                      &&&&&&&&&&&&&&& BACHA
	IF FILE(m.gcdat_path + "dod_list.dbf")
		SELECT 0
		USE (m.gcdat_path + "dod_list") ALIAS dod_list
		INDEX ON STR(c_partner,12) + STR(c_karty, 12) TO (m.gcpri_path + "ddl")
		SELECT pom
		SET RELATION TO STR(c_partner,12) + STR(c_karty, 12) INTO dod_list
		REPLACE ALL cen_pardph WITH dod_list.cena, prachysdph WITH mnozstvi * dod_list.cena
	ENDIF
	SELECT pom
	REPLACE ALL cena_part WITH cena * dan, prachy_par WITH prachy * dan
ENDIF                                                                                    &&&&&&&&&&&&&&& BACHA

*!*		SELECT c_karty, SUM(mnozstvi) AS zustatek, SUM(MTON(mnozstvi) * MTON(cena) * en__meny(c_mena,m.girep_mena)) AS prachy FROM pom ;
*!*			GROUP BY 1 WHERE c_dokl < m.d1 AND INLIST(LEFT(operace, 1), "P", "V") ;
*!*			INTO CURSOR skl_obr6
*!*		SUM ROUND(prachy,4) TO m.cena_poc
*!*		m.cena_poc=ROUND(m.cena_poc,2)
*!*		USE IN skl_obr6

*!*		SELECT pom
*!*		SUM ROUND(MTON(mnozstvi) * MTON(cena) * en__meny(c_mena,m.girep_mena),4) TO  m.cena_kon FOR INLIST(LEFT(operace, 1), "P", "V")
*!*		m.cena_kon=ROUND(m.cena_kon,2)
IF "1" $ m.jakesestavy
	SELECT spart, SPACE(4) AS stredskup, partner, ;
		SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS prachy_par, SUM(prachysdph*en__meny(c_mena,m.girep_mena)) AS prachysdph, SUM(mnozstvi*NTOM(MTON(cena1)*en__meny(c_mena,m.girep_mena))) AS prachy1 ;
		GROUP BY 1,2,3 FROM pom INTO CURSOR BUFFER READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
	IF "1" $ m.dily
		repo_txt = "spart/partner"
		DO pr_report WITH "OBR_UCT6"
	ENDIF

	IF "2" $ m.dily
		SELECT spart, cen_hlad AS stredskup, partner, ;
			SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS prachy_par, SUM(NTOM(MTON(prachysdph)*en__meny(c_mena,m.girep_mena))) AS prachysdph, SUM(mnozstvi*NTOM(MTON(cena1)*en__meny(c_mena,m.girep_mena))) AS prachy1 ;
			GROUP BY 1,2,3 FROM pom INTO CURSOR BUFFER READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
		repo_txt = "spart/cen_hlad/partner"
		DO pr_report WITH "OBR_UCT6@2"
	ENDIF
ENDIF

IF "STRAH" $ UPPER(m.gchom_path) .AND. (.NOT. ("MTZ" $ UPPER(m.gchom_path))) .AND. (m.cislostrediska # -100) &&&&&&&&&&&&&&& BACHA
	SELECT pom
	REPLACE FOR c_partner = m.cislostrediska .AND. .NOT. INLIST(c_partner, -105, -658) c_partner WITH 0, partner WITH "SPOTREBA KASY"
ENDIF                                                                                    &&&&&&&&&&&&&&& BACHA

IF "2" $ m.jakesestavy
	SELECT ALLT(STR(SIGN(c_partner)+1))+ "  " AS stredskup, partner, " " AS spart, c_partner, ;
		SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS prachy_par, SUM(NTOM(MTON(prachysdph)*en__meny(c_mena,m.girep_mena))) AS prachysdph, SUM(mnozstvi*NTOM(MTON(cena1)*en__meny(c_mena,m.girep_mena))) AS prachy1 ;
		GROUP BY 1,2 FROM pom INTO CURSOR BUFFER READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
	REPLACE FOR "0" $ stredskup stredskup WITH "Int"
	REPLACE FOR "1" $ stredskup stredskup WITH "Kas"
	REPLACE FOR "2" $ stredskup stredskup WITH "Ext"
	IF "1" $ m.dily
		INDEX ON stredskup + partner TAG ss_part
		repo_txt = "partner"

		DO pr_report WITH "OBR_UCT6@part"
	ENDIF

	IF "2" $ m.dily
		SELECT stredisko, stredskup, partner, spart, ;
			SUM(NTOM(MTON(prachy)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(NTOM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena))) AS prachy_par, SUM(NTOM(MTON(prachysdph)*en__meny(c_mena,m.girep_mena))) AS prachysdph, SUM(mnozstvi*NTOM(MTON(cena1)*en__meny(c_mena,m.girep_mena))) AS prachy1 ;
			GROUP BY 1, 2, 3, 4 FROM pom INTO CURSOR BUFFER READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
		DO pr_report WITH "OBR_UCT3"
	ENDIF

ENDIF
USE
DO pozavirej WITH "buffer,skl_iii,dod_list"

*!*****************************************************************************
PROCEDURE s_obr_xxx
PARAMETERS tlac_operace, pgmparam
SET COLLATE TO (m.gccollate)
SET CONFIRM ON
fn_pom = "pom"
fn_pomidx = m.gcpri_path + "pomidx"
ostatni = "ZZZostatni"
sox_had = CREATEOBJECT("had")
sox_had.SHOW()
IF LEN(m.tlac_operace)=2
	SELECT c_stredisk, c_dokl, operace, c_karty, SPACE(30) AS nazev, mnozstvi, cena, cena_part * m.s_cen_part AS cena_part, $0 AS cen_pardph, spdan, spstupne, $0 AS objem, zustatek as qzust, cena_prum as qcena,;
		mnozstvi * cena AS prachy, mnozstvi * cena_part * m.s_cen_part AS prachy_par, $0 AS prachysdph, m.giakt_mena AS c_mena, m.giakt_mena AS c_menap, ;
		00000000 AS c_partner, SPACE(30) AS partner, {^1111-01-01} AS datum_dokl, 0000000000 AS doklad, SPACE(15) AS pz1, SPACE(15) AS pz2, 0.000 AS dan, 00000 as typ_pohybu, SPACE(12) as txtoper,;
		00000000 AS c_druh, SPACE(m.sirkastlpcadruh) AS druh, SPACE(30) AS stredisko, $0 AS cena1, "   " AS spart, SPACE(13) AS kod, "    " AS stredskup, "     " AS platba, "   " AS provedl, "  " AS cen_hlad ;
		WHERE INLIST(LEFT(operace, 1), "P", "V") AND ((operace LIKE m.tlac_operace) OR operace="PR");
		ORDER BY c_stredisk, c_dokl FROM obratyx INTO CURSOR pomx READWRITE
	SET RELATION TO c_dokl INTO doklady
	REPLACE ALL c_partner WITH doklady.c_partner, doklad WITH doklady.ndoklad, datum_dokl WITH doklady.datum_dokl,c_mena WITH doklady.c_mena, ;
		pz1 WITH doklady.pz1, pz2 WITH doklady.pz2, platba WITH doklady.platba, provedl WITH doklady.provedl, cen_hlad WITH doklady.cen_hlad
	IF FSIZE("typ_pohybu","doklady")>0
		REPLACE ALL typ_pohybu WITH doklady.typ_pohybu
	ENDIF 
	REPLACE ALL txtoper WITH operace 
ELSE 
	m.operace__txt='"'
	FOR m.i=1 TO LEN(m.tlac_operace)
		IF SUBSTR(m.tlac_operace,m.i,1)=","
			m.operace__txt=m.operace__txt+'","'
		ELSE
			m.operace__txt=m.operace__txt+SUBSTR(m.tlac_operace,m.i,1)
		ENDIF
	ENDFOR
	m.operace__txt=m.operace__txt+'"'
	SELECT c_stredisk, c_dokl, operace, c_karty, SPACE(30) AS nazev, mnozstvi, cena, cena_part * m.s_cen_part AS cena_part, $0 AS cen_pardph, spdan, spstupne, $0 AS objem, zustatek as qzust, cena_prum as qcena,;
		mnozstvi * cena AS prachy, mnozstvi * cena_part * m.s_cen_part AS prachy_par, $0 AS prachysdph, m.giakt_mena AS c_mena, m.giakt_mena AS c_menap, ;
		00000000 AS c_partner, SPACE(30) AS partner, {^1111-01-01} AS datum_dokl, 0000000000 AS doklad, SPACE(15) AS pz1, SPACE(15) AS pz2, 0.000 AS dan, 00000 as typ_pohybu, SPACE(12) as txtoper,;
		00000000 AS c_druh, SPACE(m.sirkastlpcadruh) AS druh, SPACE(30) AS stredisko, $0 AS cena1, "   " AS spart, SPACE(13) AS kod, "    " AS stredskup, "     " AS platba, "   " AS provedl, "  " AS cen_hlad ;
		WHERE INLIST(LEFT(operace, 1), "P", "V");
		ORDER BY c_stredisk, c_dokl FROM obratyx INTO CURSOR pomx1 READWRITE
	SET RELATION TO c_dokl INTO doklady
	REPLACE ALL c_partner WITH doklady.c_partner, doklad WITH doklady.ndoklad, datum_dokl WITH doklady.datum_dokl,c_mena WITH doklady.c_mena, ;
		pz1 WITH doklady.pz1, pz2 WITH doklady.pz2, platba WITH doklady.platba, provedl WITH doklady.provedl, cen_hlad WITH doklady.cen_hlad
	IF FSIZE("typ_pohybu","doklady")>0
		REPLACE ALL typ_pohybu WITH doklady.typ_pohybu
	ENDIF 
	REPLACE txtoper WITH operace FOR typ_pohybu=0
	REPLACE txtoper WITH operace+ALLTRIM(STR(typ_pohybu,10,0)) FOR typ_pohybu<>0
	SELECT * FROM pomx1 INTO CURSOR pomx WHERE operace="PR" OR INLIST(txtoper,&operace__txt) READWRITE 
	USE IN pomx1
ENDIF
SELECT pomx
REPLACE FOR operace = "VP" operace WITH "PP"


IF "D_GROS" $ UPPER(m.gchom_path) AND !m.issql   &&   * * * * * * * * * * * * * *   v GROSETU se jako CENA_PAR pouzije SANKCNICENA   * * * * * * * * * * * * * *
	DO CASE
		CASE m.gcpgm_id = "65"
			sankcnicena = "30"
		CASE m.gcpgm_id = "42"
			sankcnicena = "31"
		OTHERWISE
			*
	ENDCASE
	sc = "karty.cena" + m.sankcnicena
	SET ORDER TO c_karty IN karty
	SET RELATION TO c_karty INTO karty
	REPLACE FOR .NOT. INLIST( operace, "VE", "VR") ;
		cena_part WITH NTOM(MTON(&sc)*en__meny(karty.c_mena,m.girep_mena)), prachy_par WITH mnozstvi * cena_part
	SET RELATION TO
ENDIF   &&   * * * * * * * * * * * * * *   v GROSETU se jako CENA_PAR pouzije SANKCNICENA   * * * * * * * * * * * * * *


sox_had.set_had(33)
REPLACE FOR LEFT(operace, 1) # "V" .AND. operace # "PI" prachy_par WITH prachy * m.s_cen_part, cena_part WITH cena * m.s_cen_part

sox_had.set_had(66)
SET RELATION TO c_partner INTO partneri
REPLACE FOR ! EMPTY(partneri.partner) partner WITH partneri.partner

REPLACE FOR "SKUP" $ partneri.naz6 stredskup WITH partneri.naz6

SET RELATION TO c_stredisk INTO partneri
REPLACE FOR ! EMPTY(partneri.partner) stredisko WITH partneri.partner
REPLACE FOR EMPTY(partner) partner WITH "##  " + ALLT(STR(c_partner)) + "  ##"

sox_had.set_had(88)
SET RELATION TO c_karty INTO karty
IF "SKL" $ m.pgmparam
	REPLACE FOR EMPTY(nazev) nazev WITH karty.nazev, c_druh WITH karty.c_druhs, kod WITH karty.kod, objem WITH karty.objem
	REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druh
ELSE
	REPLACE FOR EMPTY(nazev) nazev WITH karty.nazev, c_druh WITH karty.c_druh, kod WITH karty.kod, objem WITH karty.objem
	REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhs
ENDIF
IF m.issql
	SET RELATION OFF INTO karty
	UPDATE pomx SET cena1=ckarty.cena, c_menap=ckarty.c_mena FROM ckarty WHERE pomx.c_karty=ckarty.c_karty
ELSE
	REPLACE ALL cena1 WITH karty.&cena_teor, c_menap WITH karty.c_mena
	SET RELATION OFF INTO karty
ENDIF
REPLACE ALL cena1 WITH NTOM(MTON(cena1)*en__meny(c_menap,c_mena))

UPDATE pomx SET spart=skupiny.spart,dan=skupiny.dan01, druh=skupiny.druh FROM skupiny WHERE pomx.c_druh=skupiny.c_druh

UPDATE pomx SET stredskup=food_dat.stredskup FROM food_dat WHERE pomx.c_partner=food_dat.c_stredisk

REPLACE FOR EMPTY(druh) druh WITH m.ostatni
REPLACE FOR EMPTY(spart) spart WITH m.ostatni
REPLACE FOR EMPTY(cen_hlad) cen_hlad WITH "1"
REPLACE FOR c_partner > 0 .AND. EMPTY(stredskup) stredskup WITH "_Ext"
REPLACE FOR EMPTY(stredskup) stredskup WITH " Int"

SELECT DISTINCT c_partner, partner AS id_zkratka, .T. AS lbl, stredskup ;
	FROM pomx INTO CURSOR selkasy READWRITE
REPLACE FOR ISNULL(id_zkratka) id_zkratka WITH "-- " + ALLTRIM(STR(c_partner)) + " --"
REPLACE FOR ISNULL(stredskup) stredskup WITH ""
INDEX ON stredskup + id_zkratka TAG ss_part
IF prazdna()
	INSERT INTO selkasy (c_partner, id_zkratka) VALUE (918273, "-- nic nevybrano --")
ENDIF

SELECT DISTINCT druh AS druh, .T. AS lbl, c_druh, SPACE(50) as poradie, 00000000 as c_druh_2, SPACE(50) as druh_meno, 0000 as uroven, SPACE(3) as rozbal, .t. as ukaz  FROM pomx INTO CURSOR  "seldruhy" READWRITE
IF FSIZE("c_druh_2","skupiny")>0
	UPDATE seldruhy SET c_druh_2=IIF(ISNULL(skupiny.c_druh_2),0,skupiny.c_druh_2) from skupiny WHERE seldruhy.c_druh=skupiny.c_druh
ENDIF 
DO WHILE .t.
	SELECT c_druh_2 as c_druh FROM seldruhy WHERE c_druh_2<>0 AND !(c_druh_2 IN (SELECT c_druh FROM seldruhy)) INTO CURSOR pridaj
	IF RECCOUNT("pridaj")>0
		INSERT INTO seldruhy (c_druh, druh, c_druh_2, lbl) SELECT IIF(ISNULL(c_druh),0,c_druh), IIF(ISNULL(druh),"",druh), IIF(ISNULL(c_druh_2),0,c_druh_2), .t. FROM skupiny WHERE c_druh in (select distinct c_druh FROM pridaj)
	ELSE
		USE IN pridaj
		EXIT 
	ENDIF 
ENDDO 
DO pozavirej WITH "pridaj"	

SELECT pomx
pom_sel = SELECT()
RELEASE sox_had

*!*****************************************************************************
PROCEDURE na1kar
IF m.issql
	USE (m.sqlprefix+"rsksezdavkylimity") IN 0 ALIAS sdl
	USE (m.sqlprefix+"rlimhlkratkyzoznam") IN 0 ALIAS lhl
ELSE
	IF !USED("sk_sez")
		USE (m.fn_sk_sez) IN 0 ALIAS sk_sez
	ENDIF 
	USE vsksezdavkylimity IN 0 ALIAS sdl
	IF m.is_newlim
		IF !USED("limit_hl")
			USE (m.gccen_path+"limit_hl") IN 0 ALIAS limit_hl
		ENDIF 
		USE vlimhlkratkyzoznamold IN 0 ALIAS lhl
	ELSE 
		IF !USED("nlimit_hl")
			USE (m.fn_limit_hl) IN 0 ALIAS nlimit_hl
		ENDIF 
		USE vlimhlkratkyzoznam IN 0 ALIAS lhl
	ENDIF 
ENDIF 
SELECT doklady
SET ORDE TO c_dokl
SELECT karty
SET ORDE TO c_karty
SELECT DISTINCT c_karty, nazev WHERE BETWEEN(c_dokl, m.d1, m.d2) ;
	FROM pom INTO CURSOR nazvy READWRITE
INDEX ON nazev TAG nazev
DO FORM dajdruh WITH "","nazvy","nazev",250,"c_karty",100 TO m.nazev
LOCAL txtpocstav
txtpocstav=UPPER(jaz("form pocatecni stav"))
DO WHILE !EMPTY(m.nazev)
	SELECT nazvy
	=SEEK(m.nazev)
	m.c_karty=nazvy.c_karty
	nakar_had = CREATEOBJECT("had")
	nakar_had.SHOW()
	
	SELECT -9999 AS c_dokl, m.c_karty AS c_karty, 0.0 AS mnozstvi, SUM(mnozstvi) AS zustatek, SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS cena, SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS cena_prum, m.txtpocstav AS partner ;
		WHERE c_karty = m.c_karty AND INLIST(LEFT(obratyx.operace, 1), "P", "V") AND mnozstvi # 0 AND obratyx.c_dokl < m.d1 ;
		FROM obratyx LEFT JOIN doklady ON obratyx.c_dokl=doklady.c_dokl INTO CURSOR na1poc READWRITE
	IF RECCOUNT() = 0
		INSERT INTO na1poc (c_dokl, c_karty, mnozstvi, cena, partner) VALUE (-9999, m.c_karty, 0, 0, m.txtpocstav)
	ELSE
		IF zustatek # 0
			REPLACE  cena WITH cena/zustatek
		ENDIF
	ENDIF
	nakar_had.set_had(77)
	SELECT *, 0000000.0000 AS prirustek, 0000000.0000 AS ubytek, {^1111-11-11} AS datum_dokl,0 AS c_mena, ;
		0000000000 AS c_partner, SPACE(25) AS partner, SPACE(12) AS doklad, SPACE(15) AS pz1, SPACE(15) AS pz2, SPACE(17) as lastuzav ;
		WHERE c_karty = m.c_karty AND INLIST(LEFT(operace, 1), "P", "V") AND mnozstvi # 0 AND BETWEEN(c_dokl, m.d1, m.d2) ;
		FROM obratyx INTO CURSOR na1kar ORDER BY c_dokl READWRITE
	REPLACE ALL prirustek WITH MAX(mnozstvi, 0), ubytek WITH MIN(mnozstvi, 0)
	SET RELATION TO c_dokl INTO doklady
	REPLACE ALL datum_dokl WITH doklady.datum_dokl, c_partner WITH doklady.c_partner, cena WITH NTOM(MTON(cena)*en__meny(doklady.c_mena,m.girep_mena)), cena_prum WITH NTOM(MTON(cena_prum)*en__meny(doklady.c_mena,m.girep_mena)),;
		doklad WITH STR(doklady.doklad,12,0), pz1 WITH doklady.pz1, pz2 WITH doklady.pz2
	SET RELATION TO c_partner INTO partneri
	REPLACE ALL partner WITH partneri.partner
	SET RELATION TO
	IF m.slovenskaverzia OR m.issql
		UPDATE na1kar SET lastuzav=sdl.lastuzav from sdl WHERE na1kar.operace="VK" AND na1kar.pz2=SUBSTR(sdl.uzaverka,3,12)+"/"+sdl.id_kas
	ELSE
		UPDATE na1kar SET lastuzav=sdl.lastuzav from sdl WHERE na1kar.operace="VK" AND na1kar.pz2=ALLTRIM(sdl.uzaverka)+"/"+sdl.id_kas
	ENDIF 
	IF USED("lhl")
		UPDATE na1kar SET partner=lhl.menolimit from lhl WHERE LEFT(na1kar.lastuzav,2)="L_" AND VAL(SUBSTR(na1kar.lastuzav,3))=lhl.id_limit
	ENDIF 
	UPDATE na1kar SET partner=SUBSTR(lastuzav,3) WHERE LEFT(lastuzav,2)="D_"
	INSERT INTO na1kar (c_dokl, c_karty, mnozstvi, zustatek, cena, cena_prum, partner) SELECT c_dokl, c_karty, mnozstvi, zustatek, cena, cena_prum, partner FROM na1poc
	INDEX ON STR(c_dokl) + STR(idriadok) TAG cdokl_idr
	operace_txt_nakar = ""
	nakar_had.HIDE()
	DO pr_report WITH "NAKAR"

	IF ! EMPTY(operace_txt)
		nakar_had.SHOW()
		nakar_had.set_had(77)

		SELECT *, 0000000.0000 AS prirustek, 0000000.0000 AS ubytek, {^1111-11-11} AS datum_dokl, ;
			0000000000 AS c_partner, SPACE(25) AS partner, SPACE(12) AS doklad, SPACE(15) AS pz1, SPACE(15) AS pz2, SPACE(17) as lastuzav ;
			WHERE c_karty = m.c_karty AND INLIST(LEFT(operace, 1), "P", "V") AND operace LIKE m.tlac_operace AND mnozstvi # 0 AND BETWEEN(c_dokl, m.d1, m.d2) ;
			FROM obratyx INTO CURSOR na1kar ORDER BY c_dokl READWRITE
		IF ! prazdna()
			REPLACE ALL prirustek WITH MAX(mnozstvi, 0), ubytek WITH MIN(mnozstvi, 0)
			SET RELATION TO c_dokl INTO doklady
			REPLACE ALL datum_dokl WITH doklady.datum_dokl, c_partner WITH doklady.c_partner, cena WITH NTOM(MTON(cena)*en__meny(doklady.c_mena,m.girep_mena)), cena_prum WITH NTOM(MTON(cena_prum)*en__meny(doklady.c_mena,m.girep_mena)), ;
				doklad WITH STR(doklady.doklad,12,0), pz1 WITH doklady.pz1, pz2 WITH doklady.pz2
			SET RELATION TO c_partner INTO partneri
			REPLACE ALL partner WITH partneri.partner
			SET RELATION TO
			UPDATE na1kar SET lastuzav=sdl.lastuzav from sdl WHERE na1kar.operace="VK" AND na1kar.pz2=SUBSTR(sdl.uzaverka,3,12)+"/"+sdl.id_kas
			IF USED("lhl")
				UPDATE na1kar SET partner=lhl.menolimit from lhl WHERE LEFT(na1kar.lastuzav,2)="L_" AND VAL(SUBSTR(na1kar.lastuzav,3))=lhl.id_limit
			ENDIF 
			UPDATE na1kar SET partner=SUBSTR(lastuzav,3) WHERE LEFT(lastuzav,2)="D_"
			INSERT INTO na1kar (c_dokl, c_karty, mnozstvi, zustatek, cena, cena_prum, partner) SELECT c_dokl, c_karty, mnozstvi, zustatek, cena, cena_prum, partner FROM na1poc
*			INDEX ON c_dokl TAG c_dokl
            INDEX ON STR(c_dokl) + STR(idriadok) TAG cdokl_idr
			operace_txt_nakar = m.operace_txt
			nakar_had.HIDE()
			DO pr_report WITH "NAKAR@2"
		ENDIF
	ENDIF
	RELEASE nakar_had
	IF .T.    && tohle bude jen pro JLV
		SELECT obratyx.c_stredisk, obratyx.operace, obratyx.c_dokl, obratyx.c_dokl AS c_prijmu, obratyx.c_karty, obratyx.mnozstvi,;
			obratyx.mnozstvi AS celk_mnoz, obratyx.mnozstvi AS mnoz_pom, obratyx.cena, obratyx.serial_no, 001 AS poradi, obratyx.c_stredisk AS radek, ;
			doklady.datum_dokl, doklady.c_partner, SPACE(25) AS partner, doklady.doklad, doklady.pz1, doklady.pz2, SPACE(17) as lastuzav ;
			ORDER BY obratyx.c_dokl, obratyx.poradi WHERE obratyx.c_stredisk = m.cislostrediska AND obratyx.c_karty = m.c_karty AND LEFT(obratyx.operace,1) $ "PV" ;
			FROM obratyx LEFT JOIN doklady ON obratyx.c_dokl=doklady.c_dokl INTO CURSOR oooo READWRITE
		REPLACE ALL radek WITH RECNO()
		REPLACE FOR operace = "PI" serial_no WITH ""
		SET RELATION TO c_dokl INTO doklady
		REPLACE ALL datum_dokl WITH doklady.datum_dokl, c_partner WITH doklady.c_partner, doklad WITH doklady.doklad, pz1 WITH doklady.pz1, pz2 WITH doklady.pz2
		SET RELATION TO
		INDEX ON c_karty TAG c_karty
		INDEX ON radek TAG radek
		FOR radek_prijmu = 1 TO RECCOUNT()
			GO (m.radek_prijmu)
			IF oooo.mnozstvi > 0    && hledam PRIJMY
				SCATTER FIELDS c_stredisk, c_karty, mnozstvi, c_prijmu, serial_no MEMVAR
				c_prijmu = oooo.c_dokl
				k_vydeji = oooo.mnozstvi
				DO CASE
					CASE oooo.operace = "PI"
						serial_no = oooo.operace + " " + DTOC(oooo.datum_dokl)
					CASE EMPTY(m.serial_no)
						serial_no = oooo.operace + " " + DTOC(oooo.datum_dokl)
				ENDCASE
				DO WHILE m.k_vydeji > 0
					LOCATE FOR (c_stredisk = m.c_stredisk) .AND. (c_karty = m.c_karty) .AND. (radek > m.radek_prijmu) .AND. (mnoz_pom < 0)    && hledam VYDEJ
					IF FOUND()
						k_vydeji = m.k_vydeji + oooo.mnoz_pom
						IF k_vydeji >= 0    && cela vydeja se pokryje
							REPLACE serial_no WITH m.serial_no, mnozstvi WITH mnoz_pom, mnoz_pom WITH 0, c_prijmu WITH m.c_prijmu
						ELSE
							SCATTER FIELDS operace, c_dokl, radek, celk_mnoz, poradi, cena MEMVAR     && zapamatovat pro pridany radek
							REPLACE serial_no WITH m.serial_no, mnozstvi WITH - m.k_vydeji + oooo.celk_mnoz, mnoz_pom WITH 0, c_prijmu WITH m.c_prijmu
							INSERT INTO oooo (c_stredisk,   operace,   c_dokl, c_karty,   mnoz_pom,   celk_mnoz,     poradi  ,   radek,   cena) ;
								VALUES (m.c_stredisk, m.tlac_operace, m.c_dokl, c_karty, m.k_vydeji, m.celk_mnoz, m.poradi + 1, m.radek, m.cena)
						ENDIF
					ELSE
						EXIT
					ENDIF
				ENDDO
			ENDIF
		ENDFOR
		SET FILTER TO BETWEEN(c_dokl, m.d1, m.d2)
		UPDATE oooo SET partner=partneri.partner FROM partneri WHERE oooo.c_partner=partneri.c_partner
		UPDATE oooo SET lastuzav=sdl.lastuzav from sdl WHERE oooo.operace="VK" AND oooo.pz2=SUBSTR(sdl.uzaverka,3,12)+"/"+sdl.id_kas
		IF USED("lhl")
			UPDATE oooo SET partner=lhl.menolimit from lhl WHERE LEFT(oooo.lastuzav,2)="L_" AND VAL(SUBSTR(oooo.lastuzav,3))=lhl.id_limit
		ENDIF 
		UPDATE oooo SET partner=SUBSTR(lastuzav,3) WHERE LEFT(lastuzav,2)="D_"
		REPLACE FOR poradi > 1 partner WITH '    -- "" --'
		DO pr_report WITH "NAKAR_serial_no"
		USE IN oooo
	ENDIF     && tohle bude jen pro JLV
	SELECT nazvy
	DO FORM dajdruh WITH "","nazvy","nazev",250,"c_karty",100 TO m.nazev
ENDDO
DO pozavirej WITH "nazvy,na1poc,na1kar,sdl, lhl, limit_hl, nlimit_hl, sk_sez"


*!*****************************************************************************
*!      Procedure: OBRATOVKA1
*!*****************************************************************************
PROCEDURE obratovka1

SELECT c_karty, operace, nazev, druh, SUM(mnozstvi) AS mnozstvi, SUM(MTON(mnozstvi) * MTON(cena)* en__meny(c_mena,m.girep_mena)) AS prachy, SUM(MTON(mnozstvi) * MTON(cena_part)* en__meny(c_mena,m.girep_mena)) AS prachypart ;
	GROUP BY 1, 2 FROM pom INTO CURSOR obr READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
REPLACE FOR operace # "VE" prachypart WITH 0
DELETE FOR mnozstvi = 0

SELECT c_karty, nazev, druh, operace, SUM(mnozstvi) AS mnozstvi, SUM(MTON(mnozstvi) * MTON(cena)* en__meny(c_mena,m.girep_mena)) AS prachy ;
	GROUP BY 1 FROM pom INTO CURSOR skl_i READWRITE WHERE c_dokl<m.d1
DELETE FOR mnozstvi=0
REPLACE ALL prachy WITH prachy/mnozstvi

CREATE CURSOR obrat ( ;
	c_karty i, operace c(2), ;
	zustatek Y, cena_prum Y, mnozstvi Y, prachy Y, prachypart Y, ;
	mnoz_p_ Y, prachy_p_ Y, mnoz_ve Y, prachy_ve Y, mnoz_vv Y, prachy_vv Y, mnoz_vk Y, prachy_vk Y, mnoz_xx Y, prachy_xx Y, nazev c(30), druh c(m.sirkastlpcadruh))
INSERT INTO obrat(c_karty, operace, zustatek, cena_prum, nazev, druh) SELECT c_karty, operace, mnozstvi, prachy, nazev, druh FROM skl_i
INSERT INTO obrat(c_karty, operace, mnozstvi, prachy, prachypart, nazev, druh) SELECT c_karty, operace, mnozstvi, prachy, prachypart, nazev, druh FROM obr

REPLACE FOR operace = "VE"              mnoz_ve WITH mnozstvi, prachy_ve WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VV"              mnoz_vv WITH mnozstvi, prachy_vv WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR operace = "VK"              mnoz_vk WITH mnozstvi, prachy_vk WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE FOR INLIST(operace, "PE", "PV") mnoz_p_ WITH mnozstvi, prachy_p_ WITH prachy, mnozstvi WITH 0, prachy WITH 0
REPLACE ALL                             mnoz_xx WITH mnozstvi, prachy_xx WITH prachy, mnozstvi WITH 0, prachy WITH 0

SELECT c_karty, nazev, druh, SPACE(3) AS spart, ;
	SUM(zustatek) AS pocatek, SUM(zustatek * cena_prum) AS prachy_poc, 00000000.00 AS sc, ;
	SUM(mnoz_p_) AS mnoz_p_, SUM(prachy_p_)  AS prachy_p_, ;
	SUM(mnoz_ve) AS mnoz_ve, SUM(prachy_ve)  AS prachy_ve, ;
	SUM(mnoz_vv) AS mnoz_vv, SUM(prachy_vv)  AS prachy_vv, ;
	SUM(mnoz_vk) AS mnoz_vk, SUM(prachy_vk)  AS prachy_vk, ;
	SUM(mnoz_xx) AS mnoz_xx, SUM(prachy_xx)  AS prachy_xx, ;
	SUM(prachypart) AS prachypart, $0 as konec, $0 as prachy_kon ;
	GROUP BY 1 FROM obrat INTO CURSOR obratovk READWRITE

DELETE FOR pocatek = 0 .AND. mnoz_ve = 0 .AND. mnoz_vv = 0 .AND. mnoz_vk = 0 .AND. mnoz_p_ = 0 .AND. mnoz_xx = 0
REPLACE ALL konec      WITH pocatek    +   mnoz_p_ +   mnoz_ve +   mnoz_vv +   mnoz_vk +   mnoz_xx, ;
            prachy_kon WITH prachy_poc + prachy_p_ + prachy_ve + prachy_vv + prachy_vk + prachy_xx

IF m.issql
	PRIVATE m.hladajhladinu
	m.hladajhladinu=20
	SELECT 0
	rv("predceny","prc")
	UPDATE obratovk SET sc=prc.cena FROM prc WHERE obratovk.c_karty=prc.c_karty
	USE IN prc
ELSE
	UPDATE obratovk SET sc=karty.cena20 FROM karty WHERE obratovk.c_karty=karty.c_karty
ENDIF
UPDATE obratovk SET spart=skupiny.spart FROM skupiny WHERE obratovk.druh=skupiny.druh

SELECT obratovk
REPLACE FOR EMPTY(nazev) nazev WITH m.ostatni
REPLACE FOR EMPTY(druh)  druh  WITH m.ostatni
REPLACE FOR EMPTY(spart) spart WITH m.ostatni

INDEX ON spart + druh + nazev TAG dr_naz
IF "1" $ m.dily
	DO pr_report WITH "obratovk"
ENDIF
IF "2" $ m.dily
	DO pr_report WITH "obratov2"
ENDIF

IF "D_SNEM" $ UPPER(m.gchom_path)
    SELECT * FROM pom INTO CURSOR nakupy WHERE BETWEEN(c_dokl, m.d1, m.d2) AND operace = "PE"
    DO to_csv
ENDIF 

DO pozavirej WITH "obr, obrat, obratovk,skl_i"


*!*****************************************************************************
*!      Procedure: OBRATOVKA2
*!*****************************************************************************
PROCEDURE obratovka2
SELECT c_karty, obratyx.operace, doklady.typ_pohybu, SUM(mnozstvi) AS mnozstvi, SUM(MTON(mnozstvi) * MTON(cena)*en__meny(doklady.c_mena,m.girep_mena)) AS prachy, SUM(MTON(mnozstvi) * MTON(cena_part)*en__meny(doklady.c_mena,m.girep_mena)) AS prachypart ;
	GROUP BY c_karty, obratyx.operace, doklady.typ_pohybu WHERE BETWEEN(obratyx.c_dokl, m.d1, m.d2) AND INLIST(LEFT(obratyx.operace, 1), "P", "V") ;
	FROM obratyx LEFT JOIN doklady ON obratyx.c_dokl=doklady.c_dokl INTO CURSOR obr READWRITE
*REPLACE FOR operace # "VE" prachypart WITH 0
DELETE FOR mnozstvi = 0

SELECT c_karty, nazev, druh, operace, mnozstvi AS zustatek, MTON(cena)* en__meny(c_mena,m.girep_mena) AS cena_prum ;
	GROUP BY c_karty FROM pom INTO CURSOR skl_i READWRITE WHERE c_dokl<m.d1
DELETE FOR zustatek=0
SELECT skl_i
SUM ROUND(zustatek * cena_prum, 4) TO prachy_poc
SELECT operace, typ_pohybu, SPACE(30) AS lng_oper, SUM(MTON(prachy)*en__meny(c_mena,m.girep_mena)) AS prachy, SUM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena)) AS prachypart ;
	GROUP BY operace, typ_pohybu FROM pom INTO CURSOR obr_oper READWRITE WHERE BETWEEN(c_dokl,m.d1,m.d2)
REPLACE lng_oper WITH lng_oper(operace) FOR typ_pohybu=0
REPLACE lng_oper WITH lng_oper_pohyb(operace, typ_pohybu) FOR typ_pohybu<>0
SUM prachy TO obrat
INDEX ON operace TAG op
IF "1" $ m.dily
	DO pr_report WITH "obr_oper"
ENDIF
DO pozavirej WITH "obr, skl_i, obr_oper"

************************ BACHA BACHA BACHA BACHA BACHA BACHA
IF "2" $ m.dily
	SELECT food_dat
	SELECT * FROM pom INTO CURSOR pom1 READWRITE  WHERE BETWEEN(c_dokl,m.d1,m.d2)
*	REPLACE FOR operace = "PE" spart WITH "--"   zruseno, protoze si nikdo nepamatuje, kdo tam ty sparty nechtel
	SELECT operace, typ_pohybu, spart, partner, c_partner, SUM(MTON(prachy)* en__meny(c_mena,m.girep_mena)) AS prachy, SUM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena)) AS prachypart, SPACE(30) AS lng_oper, SPACE(4) AS stredskup ;
		GROUP BY operace, typ_pohybu, spart, partner FROM pom1 INTO CURSOR op_sp_par READWRITE
	UPDATE op_sp_par SET stredskup=food_dat.stredskup FROM food_dat WHERE op_sp_par.c_partner=food_dat.c_stredisk
	SELECT op_sp_par
	REPLACE lng_oper WITH lng_oper(operace) FOR typ_pohybu=0
	REPLACE lng_oper WITH lng_oper_pohyb(operace, typ_pohybu) FOR typ_pohybu<>0
	REPLACE FOR ISNULL(stredskup) stredskup WITH ""
	DO pr_report WITH "obr_opsp"

*	REPLACE ALL partner WITH stredskup + " " + SUBSTR(partner, 3)   zruseno, protoze si nikdo nepamatuje, kdo to chtel
	SELECT operace, typ_pohybu, spart, partner, c_partner, SUM(prachy) AS prachy, SUM(prachypart) AS prachypart, lng_oper ;
		GROUP BY operace, typ_pohybu, spart, stredskup ;
		FROM op_sp_par INTO CURSOR op_sp_parx
	DO pr_report WITH "obr_opsp@b"
	DO pozavirej WITH "op_sp_par,op_sp_parx,pom1"
ENDIF

* * *
IF "3" $ m.dily .OR. "4"$m.dily
	SELECT operace, typ_pohybu, spart, partner, SUM(MTON(prachy)* en__meny(c_mena,m.girep_mena)) AS prachy, SUM(MTON(prachy_par)*en__meny(c_mena,m.girep_mena)) AS prachypart ;
		GROUP BY operace, typ_pohybu, spart, partner FROM pom INTO CURSOR  op_sp_par1 READWRITE  WHERE BETWEEN(c_dokl,m.d1,m.d2)

	IF .NOT. ("D_PANORA" $ UPPER(m.gchom_path))
		REPLACE FOR INLIST( operace, "PE", "VE", "VR" ) partner WITH "-- dohromady --"
	ENDIF

	IF "3" $ m.dily
		SELECT operace, typ_pohybu, spart, partner, SUM(prachy) AS prachy, SUM(prachypart) AS prachypart, SPACE(30) AS lng_oper ;
			GROUP BY operace, typ_pohybu, spart, partner FROM op_sp_par1 INTO CURSOR op_sp_par READWRITE
		REPLACE lng_oper WITH lng_oper(operace) FOR typ_pohybu=0
		REPLACE lng_oper WITH lng_oper_pohyb(operace, typ_pohybu) FOR typ_pohybu<>0
		DO pr_report WITH "obr_opsp@3"
		DO pozavirej WITH "op_sp_par"
	ENDIF
	IF "4" $ m.dily
		SELECT operace, typ_pohybu, "  " AS spart, partner, SUM(prachy) AS prachy, SUM(prachypart) AS prachypart, SPACE(30) AS lng_oper ;
			GROUP BY operace, typ_pohybu, partner FROM op_sp_par1 INTO CURSOR op_sp_par READWRITE
		REPLACE lng_oper WITH lng_oper(operace) FOR typ_pohybu=0
		REPLACE lng_oper WITH lng_oper_pohyb(operace, typ_pohybu) FOR typ_pohybu<>0
		DO pr_report WITH "obr_opsp@4"
	ENDIF
	DO pozavirej WITH "op_sp_par, op_sp_par1"
ENDIF



*!*****************************************************************************
*!      Procedure: S_DLUHY
*!*****************************************************************************
PROCEDURE s_dluhy
PRIVATE m.hladajcstredisk, m.hladajcdoklod, m.hladajcdokldo
m.hladajcstredisk=m.cislostrediska
m.hladajcdoklod=m.d1
m.hladajcdokldo=m.d2
IF m.issql
	rv("zostobr_dlhy","dlh")
ELSE
	USE vzostobr_dlhy ALIAS dlh IN 0
ENDIF
=RECCOUNT("dlh")
SELECT dlh.*, k.nazev, k.c_druh,k.c_druhs, k.c_druhk, SPACE(m.sirkastlpcadruh) AS druh FROM dlh LEFT JOIN karty k ON k.c_karty==dlh.c_karty INTO CURSOR d1 READWRITE
REPLACE c_druh WITH c_druhk FOR EMPTY(c_druh)
REPLACE c_druh WITH c_druhs FOR EMPTY(c_druh)
UPDATE d1 SET druh=skupiny.druh FROM skupiny WHERE d1.c_druh=skupiny.c_druh

SELECT d1.*, doklady.pz2 ;
	FROM d1 LEFT JOIN doklady ON doklady.c_dokl==d1.c_dokl INTO CURSOR d2 READWRITE

REPLACE cena_prum WITH MTON(cena_prum)*en__meny(c_mena,m.girep_mena) ALL

REPLACE FOR ! EMPTY(provedl) .AND. !INLIST(provedl,"INV","MAZ") provedl WITH "ODE"

SELECT provedl, nazev, c_karty, druh, SUM(mnozstvi) AS mnozstvi, cena_prum GROUP BY 1,2 ;
	FROM d2 INTO CURSOR d3
SELECT d3
IF "1"$m.dily
	SET FILTER TO EMPTY(provedl)    && cekajici dluhy
	m.repo_txt = jaz("čekající")
	DO pr_report WITH "dluhy@cekaji"
	SET FILTER TO 
ENDIF

IF "2"$m.dily
	SET FILTER TO provedl= "INV"    && dluhy, zrusene inventurou
	m.repo_txt = jaz("zrušené")
	DO pr_report WITH "dluhy@zrusene"
	SET FILTER TO 
ENDIF

IF "3"$m.dily
	SET FILTER TO provedl= "ODE"    && dluhy odectene odpoctem dluhu
	m.repo_txt = jaz("odečtené")
	DO pr_report WITH "dluhy@odect"
	SET FILTER TO 
ENDIF
IF "4"$m.dily
	SET FILTER TO provedl= "MAZ"    && dluhy zmazane rucne
	m.repo_txt = jaz("zmazane")
	DO pr_report WITH "dluhy@smaz"
	SET FILTER TO 
ENDIF
USE
DO pozavirej WITH "dlh,d1,d2,d3"


PROCEDURE clos_data1
PRIVATE lainuse,lipocet,lixx,menoxx
m.lipocet=AUSED(lainuse)
IF m.lipocet>0
	FOR  m.lixx=1 TO m.lipocet
		m.menoxx=LOWER(lainuse(m.lixx,1))
		IF !INLIST(m.menoxx,"sestavy","seldruhy","selkasy","fn_hesla","existujici_texty","obraty","doklady")
			USE IN &menoxx
		ENDIF
	ENDFOR
ENDIF

*!*****************************************************************************
*!
*!      Procedure: pohybovka
*!
*!*****************************************************************************
PROCEDURE pohybovka
IF !USED("pom")
	RETURN 
ENDIF 
SELECT 0

IF "1"$m.dily OR "2"$m.dily
	SELECT c_karty, nazev, operace, mnozstvi AS zustatek, MTON(cena)* en__meny(c_mena,m.girep_mena) AS cena_prum, c_druh ;
		GROUP BY c_karty FROM pom INTO CURSOR skl_i READWRITE WHERE c_dokl<m.d1 AND mnozstvi<>0
	SELECT c_karty, nazev, operace, MTON(cena)*en__meny(c_mena,m.girep_mena) AS cena, mnozstvi, qzust as zustatek, qcena AS cena_prum, c_dokl, doklad, datum_dokl, c_partner, SPACE(10) AS ico, SPACE(14) AS dic, SPACE(25) AS partner, c_druh;
		FROM pom INTO CURSOR skl_n WHERE BETWEEN(c_dokl, m.d1, m.d2) READWRITE

	SET RELATION TO c_partner INTO partneri
	REPLACE ALL ico WITH partneri.ico, dic WITH partneri.dic,partner WITH partneri.partner
	*SET RELATION OFF INTO partneri
	SET RELATION TO c_karty INTO karty
	REPLACE ALL nazev WITH karty.nazev
	IF "SKL" $ m.gcpgm_param
		REPLACE ALL c_druh WITH karty.c_druhs
		REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druh
	ELSE
		REPLACE ALL c_druh WITH karty.c_druh
		REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhs
	ENDIF
	SET RELATION OFF INTO karty

	SELECT 0
	CREATE CURSOR obratka ( ;
		nazev      c(30), ;
		operace    c(5), ;
		mnozstvi   N(11,4), ;
		cena       N(11,4), ;
		zustatek   N(12,4), ;
		cena_prum  N(11,4), ;
		partner    c(25), ;
		doklad     c(15), ;
		c_dokl     i, ;
		druh       c(m.sirkastlpcadruh), ;
		dan_1      N(7,4), ;
		ico		   c(10),;
		dic		   c(14),;
		datum_dokl d, ;
		c_druh 		i;
		)

	INSERT INTO obratka (nazev, operace, mnozstvi, cena, zustatek, cena_prum, c_dokl, c_druh, doklad) SELECT nazev, "PS" AS operace, zustatek AS mnozstvi, cena_prum AS cena,;
		zustatek, cena_prum, 0 AS c_dokl, c_druh,"Počiatok" AS doklad FROM skl_i

	INSERT INTO obratka (nazev, operace, mnozstvi, cena, zustatek, cena_prum, partner, doklad, c_dokl, ico, dic, datum_dokl, c_druh) SELECT nazev, operace, mnozstvi, cena,;
		zustatek, cena_prum, partner, doklad, c_dokl, ico, dic, datum_dokl, c_druh FROM skl_n

	DELETE FOR EMPTY(nazev)
	SELECT obratka
	UPDATE obratka SET druh=skupiny.druh, dan_1=skupiny.dan01 FROM skupiny WHERE obratka.c_druh=skupiny.c_druh
	INDEX ON druh+nazev+STR(c_dokl,10,0) TAG celkom
	REPLACE mnozstvi WITH zustatek   FOR operace='PS'
	REPLACE cena     WITH cena_prum  FOR operace='PS'
	REPLACE doklad   WITH "Počiatok" FOR operace='PS'
	REPLACE dan_1 WITH 1 FOR dan_1=0
	SELECT DISTINCT druh,.T. AS lbl FROM obratka INTO CURSOR vybery READWRITE
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		DO FORM seldruhy WITH "vybery" TO m.druhxx
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT obratka
			SET RELATION TO druh INTO vybery
			DELETE FOR ALLTRIM(druh)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			USE IN vybery
			filtrujpriznaky("obratka")
			RELEASE m.gcfilter_triedenia

			SELECT DISTINCT nazev AS druh,.T. AS lbl FROM obratka INTO CURSOR vybery READWRITE
			INDEX ON druh TAG druh
			IF RECCOUNT()>0
				DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
				SELECT vybery
				LOCATE FOR !EMPTY(lbl)
				IF FOUND()
					SELECT obratka
					SET RELATION TO nazev  INTO vybery
					DELETE FOR ALLTRIM(nazev)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
					SET RELATION OFF INTO vybery

					IF "1"$m.dily
						DO pr_report WITH "OBRATKA"
					ENDIF
					IF "2"$m.dily
						DO pr_report WITH "OBRATKA1"
					ENDIF
				ENDIF
			ENDIF
		ENDIF
	ENDIF

ENDIF 

IF "3"$m.dily OR "4"$m.dily

	SELECT c_karty, nazev, SUM(mnozstvi) AS zustatek, SUM(mnozstvi*MTON(cena)* en__meny(c_mena,m.girep_mena)) AS prachy, c_druh ;
		GROUP BY c_karty FROM pom INTO CURSOR skl_i READWRITE WHERE c_dokl<m.d1 AND mnozstvi<>0
	SELECT c_karty, nazev, SUM(mnozstvi) AS zustatek, SUM(mnozstvi*MTON(cena)* en__meny(c_mena,m.girep_mena)) AS prachy, c_druh ;
		FROM pom INTO CURSOR skl_n READWRITE GROUP BY c_karty
	SELECT c_karty, operace, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy GROUP BY c_karty, operace WHERE INLIST(LEFT(operace, 1), "P", "V") FROM pom INTO CURSOR reg0 READWRITE

	SELECT c_karty, operace, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, ;
		$0 AS m_prije, $0 AS p_prije, ;
		$0 AS m_privl, $0 AS p_privl, ;
		$0 AS m_vydej, $0 AS p_vydej, ;
		$0 AS m_vydvr, $0 AS p_vydvr, ;
		$0 AS m_vydvl, $0 AS p_vydvl, ;
		$0 AS m_pri_i, $0 AS p_pri_i, ;
		$0 AS m_pri_b, $0 AS p_pri_b, ;
		$0 AS m_pri_o, $0 AS p_pri_o, ;
		$0 AS m_pri_p, $0 AS p_pri_p, ;
		$0 AS m_vk, $0 AS p_vk, ;
		$0 AS m_vk_lim, $0 AS p_vk_lim, ;
		$0 AS m_vk_dav, $0 AS p_vk_dav, ;
		$0 AS m_vk_1, $0 AS p_vk_1, ;
		$0 AS m_vk_2, $0 AS p_vk_2, ;
		$0 AS m_vk_3, $0 AS p_vk_3, ;
		$0 AS m_vk_4, $0 AS p_vk_4, ;
		$0 AS m_vk_5, $0 AS p_vk_5, ;
		$0 AS m_vk_6, $0 AS p_vk_6, ;
		$0 AS m_vk_7, $0 AS p_vk_7, ;
		$0 AS m_vk_8, $0 AS p_vk_8, ;
		$0 AS m_vk_9, $0 AS p_vk_9, ;
		$0 AS m_vk_o, $0 AS p_vk_o, ;
		$0 AS m_ps, $0 AS p_ps, ;
		$0 AS m_ks, $0 AS p_ks ;
		GROUP BY c_karty,operace FROM reg0 INTO CURSOR reg2 READWRITE 

	SELECT reg2
	REPLACE FOR operace = "PE" m_prije WITH mnozstvi, p_prije WITH prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "PV" m_privl WITH mnozstvi, p_privl WITH prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "VE" m_vydej WITH mnozstvi, p_vydej WITH prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "VR" m_vydvr WITH mnozstvi, p_vydvr WITH prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "VV" m_vydvl WITH mnozstvi, p_vydvl WITH prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "PI" m_pri_i WITH mnozstvi, p_pri_i WITH prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "PB" m_pri_b WITH mnozstvi, p_pri_b WITH prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "VB" m_pri_b WITH m_pri_b+mnozstvi, p_pri_b WITH p_pri_b+prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "PP" m_pri_o WITH mnozstvi, p_pri_o WITH prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "VP" m_pri_o WITH m_pri_o+mnozstvi, p_pri_o WITH p_pri_o+prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "PC" m_pri_p WITH mnozstvi, p_pri_p WITH prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "VC" m_pri_p WITH m_pri_p+mnozstvi, p_pri_p WITH p_pri_p+prachy, mnozstvi WITH 0, prachy WITH 0
	REPLACE FOR operace = "VK" m_vk WITH mnozstvi, p_vk WITH prachy, mnozstvi WITH 0, prachy WITH 0
	INSERT INTO reg2 (c_karty, operace, m_ps, p_ps) SELECT c_karty, "PS" AS operace, zustatek AS m_ps, prachy AS p_ps FROM skl_i
	INSERT INTO reg2 (c_karty, operace, m_ks, p_ks) SELECT c_karty, "KS" AS operace, zustatek AS m_ks, prachy AS p_ks FROM skl_n


	SELECT c_karty, SPACE(30) as nazev, 0000000 as c_druh, SPACE(m.sirkastlpcadruh) as druh, SUM(mnozstvi) AS mnozstvi, ROUND(SUM(prachy), 2) AS prachy, ;
		SUM(m_prije) AS m_prije, SUM(p_prije) AS p_prije, ;
		SUM(m_privl) AS m_privl, SUM(p_privl) AS p_privl, ;
		SUM(m_vydej) AS m_vydej, SUM(p_vydej) AS p_vydej, ;
		SUM(m_vydvl) AS m_vydvl, SUM(p_vydvl) AS p_vydvl, ;
		SUM(m_vydvr) AS m_vydvr, SUM(p_vydvr) AS p_vydvr, ;
		SUM(m_pri_b) AS m_pri_b, SUM(p_pri_b) AS p_pri_b, ;
		SUM(m_pri_o) AS m_pri_o, SUM(p_pri_o) AS p_pri_o, ;
		SUM(m_pri_p) AS m_pri_p, SUM(p_pri_p) AS p_pri_p, ;
		SUM(m_pri_i) AS m_pri_i, SUM(p_pri_i) AS p_pri_i, ;
		SUM(m_ps) AS m_ps, SUM(p_ps) AS p_ps, ;
		SUM(m_ks) AS m_ks, SUM(p_ks) AS p_ks, ;
		SUM(m_vk) AS m_vk, SUM(p_vk) AS p_vk, ;
		SUM(m_vk_lim) AS m_vk_lim, SUM(p_vk_lim) AS p_vk_lim, ;
		SUM(m_vk_dav) AS m_vk_dav, SUM(p_vk_dav) AS p_vk_dav, ;
		SUM(m_vk_1) AS m_vk_1, SUM(p_vk_1) AS p_vk_1, ;
		SUM(m_vk_2) AS m_vk_2, SUM(p_vk_2) AS p_vk_2, ;
		SUM(m_vk_3) AS m_vk_3, SUM(p_vk_3) AS p_vk_3, ;
		SUM(m_vk_4) AS m_vk_4, SUM(p_vk_4) AS p_vk_4, ;
		SUM(m_vk_5) AS m_vk_5, SUM(p_vk_5) AS p_vk_5, ;
		SUM(m_vk_6) AS m_vk_6, SUM(p_vk_6) AS p_vk_6, ;
		SUM(m_vk_7) AS m_vk_7, SUM(p_vk_7) AS p_vk_7, ;
		SUM(m_vk_8) AS m_vk_8, SUM(p_vk_8) AS p_vk_8, ;
		SUM(m_vk_9) AS m_vk_9, SUM(p_vk_9) AS p_vk_9, ;
		SUM(m_vk_o) AS m_vk_o, SUM(p_vk_o) AS p_vk_o ;
		GROUP BY c_karty FROM reg2 INTO CURSOR reg3 READWRITE 

	IF "4"$m.dily
		SELECT c_karty, cen_hlad, pz2, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy ;
			GROUP BY c_karty, cen_hlad, pz2 WHERE operace="VK" ;
			FROM pom INTO CURSOR reg_vk0 READWRITE
		REPLACE cen_hlad WITH "D" FOR "*"$pz2
		REPLACE cen_hlad WITH "L" FOR RIGHT(pz2,2)=m.lim_idkas
		SELECT c_karty, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy, ;
			cen_hlad GROUP BY c_karty, cen_hlad FROM reg_vk0 INTO CURSOR reg_vk READWRITE 
		USE IN reg_vk0
		UPDATE reg3 SET m_vk_lim=reg_vk.mnozstvi, p_vk_lim=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="L"
		UPDATE reg3 SET m_vk_dav=reg_vk.mnozstvi, p_vk_dav=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="D"
		UPDATE reg3 SET m_vk_1=reg_vk.mnozstvi, p_vk_1=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="1"
		UPDATE reg3 SET m_vk_2=reg_vk.mnozstvi, p_vk_2=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="2"
		UPDATE reg3 SET m_vk_3=reg_vk.mnozstvi, p_vk_3=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="3"
		UPDATE reg3 SET m_vk_4=reg_vk.mnozstvi, p_vk_4=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="4"
		UPDATE reg3 SET m_vk_5=reg_vk.mnozstvi, p_vk_5=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="5"
		UPDATE reg3 SET m_vk_6=reg_vk.mnozstvi, p_vk_6=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="6"
		UPDATE reg3 SET m_vk_7=reg_vk.mnozstvi, p_vk_7=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="7"
		UPDATE reg3 SET m_vk_8=reg_vk.mnozstvi, p_vk_8=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="8"
		UPDATE reg3 SET m_vk_9=reg_vk.mnozstvi, p_vk_9=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND ALLTRIM(reg_vk.cen_hlad)="9"
		UPDATE reg3 SET m_vk_o=reg_vk.mnozstvi, p_vk_o=reg_vk.prachy from reg_vk WHERE reg3.c_karty=reg_vk.c_karty AND !INLIST(ALLTRIM(reg_vk.cen_hlad),"9","8","7","6","5","4","3","2","1","D","L")
		USE IN reg_vk
	ENDIF 			


	SELECT reg3
	SET RELATION TO c_karty INTO karty
	REPLACE ALL nazev WITH karty.nazev
	IF "SKL" $ m.gcpgm_param
		REPLACE ALL c_druh WITH karty.c_druhs
		REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druh
	ELSE
		REPLACE ALL c_druh WITH karty.c_druh
		REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhs
	ENDIF
	SET RELATION OFF INTO karty
	UPDATE reg3 SET druh=skupiny.druh FROM skupiny WHERE reg3.c_druh=skupiny.c_druh
	INDEX on druh+nazev TAG aaa

	SELECT DISTINCT druh,.T. AS lbl FROM reg3 INTO CURSOR vybery READWRITE
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		DO FORM seldruhy WITH "vybery" TO m.druhxx
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT reg3
			SET RELATION TO druh INTO vybery
			DELETE FOR ALLTRIM(druh)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			USE IN vybery
			filtrujpriznaky("reg3")
			RELEASE m.gcfilter_triedenia		

			SELECT DISTINCT nazev AS druh,.T. AS lbl FROM reg3 INTO CURSOR vybery READWRITE
			INDEX ON druh TAG druh
			IF RECCOUNT()>0
				DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
				SELECT vybery
				LOCATE FOR !EMPTY(lbl)
				IF FOUND()
					SELECT reg3
					SET RELATION TO nazev  INTO vybery
					DELETE FOR ALLTRIM(nazev)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
					SET RELATION OFF INTO vybery
					IF "3"$m.dily
						DO pr_report WITH "obratka3"
					ENDIF 
					IF "4"$m.dily
						SELECT reg3
						SET FILTER TO m_vk<>0 AND p_vk<>0
						DO pr_report WITH "obratka4"
					ENDIF 
				ENDIF 
			ENDIF 
		ENDIF 
	ENDIF 
	*    *    *    *    *    *    *    *    *    *    *    *
	DO pozavirej WITH "reg0, reg1, reg2, reg3"
ENDIF 


DO pozavirej WITH "obratka, skl_i, skl_n, vybery"


*!*****************************************************************************
*!
*!      Procedure: GET_POC
*!
*!*****************************************************************************
PROCEDURE get_poc
PARAMETERS meno,oper,sk
PRIVATE mmm,mmm1,riadok,dans,prir_nakup,dph
m.dph=dan_1
m.riadok=RECNO()
IF sk
	IF LEN(ALLTRIM(m.oper))=1
		SUM mnozstvi*cena TO mmm FOR LEFT(operace,1)=m.oper .AND. nazev=m.meno .AND. operace<>"PS"
	ELSE
		SUM mnozstvi*cena TO mmm FOR ALLTRIM(operace)==ALLTRIM(m.oper) .AND. nazev=m.meno
	ENDIF
ELSE
	IF LEN(ALLTRIM(m.oper))=1
		SUM mnozstvi TO mmm FOR LEFT(operace,1)=m.oper .AND. nazev=m.meno .AND. operace<>"PS"
	ELSE
		SUM mnozstvi TO mmm FOR ALLTRIM(operace)==ALLTRIM(m.oper) .AND. nazev=m.meno
	ENDIF
ENDIF
SELECT obratka
GO m.riadok
RETURN mmm


*!*****************************************************************************
*!
*!      Procedure: GET_DRUH
*!
*!*****************************************************************************
PROCEDURE get_druh
PARAMETERS meno,oper,sk
PRIVATE mmm,riadok,dans,prir_nakup,dph
m.dph=dan_1
m.riadok=RECNO()
IF sk
	IF LEN(ALLTRIM(m.oper))=1
		SUM mnozstvi*cena TO mmm FOR LEFT(operace,1)=m.oper .AND. druh=m.meno
	ELSE
		SUM mnozstvi*cena TO mmm FOR operace==m.oper .AND. druh=m.meno
	ENDIF
ELSE
	IF LEN(ALLTRIM(m.oper))=1
		SUM mnozstvi TO mmm FOR LEFT(operace,1)=m.oper .AND. druh=m.meno
	ELSE
		SUM mnozstvi TO mmm FOR operace==m.oper .AND. druh=m.meno
	ENDIF
ENDIF
SELECT obratka
GO m.riadok
RETURN mmm

*!*****************************************************************************
*!
*!      Procedure: GET_CELK
*!
*!*****************************************************************************
PROCEDURE get_celk
PARAMETERS oper
PRIVATE mmm,mmm1,riadok,dans,prir_nakup,dph
m.dph=dan_1
m.riadok=RECNO()
IF LEN(ALLTRIM(m.oper))=1
	SUM mnozstvi*cena TO mmm FOR LEFT(operace,1)=m.oper
ELSE
	SUM mnozstvi*cena TO mmm FOR operace==m.oper
ENDIF

SUM mnozstvi*cena TO mmm1 FOR oper$operace

SELECT obratka
GO m.riadok
RETURN mmm


*!*****************************************************************************
*!      Procedure: PRIPRAV_VYBRANE_NAKUPY
*!*****************************************************************************
PROCEDURE priprav_vybrane_nakupy
    SELECT obratyx.c_karty,SPACE(30) AS nazev, SPACE(m.sirkastlpcadruh) AS druh, 000000000 AS c_druh, obratyx.c_dokl, doklady.datum_dokl, partneri.partner, obratyx.mnozstvi, obratyx.cena, 0000.0000 AS percento ;
           WHERE obratyx.operace="PE" .AND. obratyx.c_dokl=doklady.c_dokl .AND. doklady.c_partner=partneri.c_partner ;
           FROM obratyx, doklady, partneri INTO CURSOR nakupy READWRITE ORDER BY obratyx.c_karty ASC, obratyx.c_dokl DESC
    SET RELATION TO c_karty INTO karty
    REPLACE ALL nazev WITH karty.nazev
    IF "SKL" $ m.gcpgm_param
        REPLACE ALL c_druh WITH karty.c_druhs
        REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druh
    ELSE
        REPLACE ALL c_druh WITH karty.c_druh
        REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhs
    ENDIF
    REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhk
    SET RELATION TO
	UPDATE nakupy SET druh=skupiny.druh FROM skupiny WHERE nakupy.c_druh=skupiny.c_druh

	SELECT DISTINCT druh, .T. AS lbl  FROM nakupy INTO CURSOR vybery READWRITE
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		DO FORM seldruhy WITH "vybery" TO m.druhxx
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT nakupy
			SET RELATION TO druh INTO vybery
			DELETE FOR ALLTRIM(druh)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
		ENDIF
		filtrujpriznaky("nakupy")
		RELEASE m.gcfilter_triedenia
	ENDIF
	USE IN vybery


	SELECT DISTINCT nazev AS druh,.T. AS lbl FROM nakupy INTO CURSOR vybery READWRITE
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT nakupy
			SET RELATION TO nazev  INTO vybery
			DELETE FOR ALLTRIM(nazev)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
		ENDIF
	ENDIF
	USE IN vybery

*!*****************************************************************************
*!
*!      Procedure: NAKUPY
*!
*!*****************************************************************************
PROCEDURE nakupy
IF "1" $ m.dily .or. "3" $ m.dily
    DO priprav_vybrane_nakupy
ENDIF 

IF "1"$m.dily
	SELECT nakupy
	IF RECCOUNT()>0
		SELECT * FROM nakupy ORDER BY druh ASC, nazev ASC, c_dokl DESC INTO CURSOR nakupy1 READWRITE
		GO TOP
		SCATTER MEMVAR FIELDS nazev, cena
		REPLACE percento WITH 0
		SCAN ALL
			IF nakupy1.nazev=m.nazev
				IF m.cena=0 .OR. m.cena=nakupy1.cena
					REPLACE percento WITH 0
				ELSE
					REPLACE percento WITH (nakupy1.cena-m.cena)/m.cena*100
				ENDIF
			ELSE
				SCATTER MEMVAR FIELDS nazev, cena
				REPLACE percento WITH 0
			ENDIF
		ENDSCAN
		DO pr_report WITH "m_nakupy"
		USE IN nakupy1
	ENDIF
ENDIF 

IF "2"$m.dily
	SELECT c_karty, nazev, MTON(cena)* en__meny(c_mena,m.girep_mena) AS cena_prum, c_druh, $0 as cena_kon, SPACE(m.sirkastlpcadruh) as druh ;
		GROUP BY c_karty FROM pom INTO CURSOR obratka READWRITE WHERE c_dokl<m.d1
	SET RELATION TO c_karty INTO karty
	REPLACE ALL nazev WITH karty.nazev
	IF "SKL" $ m.gcpgm_param
		REPLACE ALL c_druh WITH karty.c_druhs
		REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druh
	ELSE
		REPLACE ALL c_druh WITH karty.c_druh
		REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhs
	ENDIF
	SET RELATION OFF INTO karty
	UPDATE obratka SET druh=skupiny.druh FROM skupiny WHERE obratka.c_druh=skupiny.c_druh
	INDEX ON druh+nazev TAG celkom

	SELECT obratyx
	SELECT obratyx.c_karty,MTON(obratyx.cena_prum)*en__meny(doklady.c_mena,m.girep_mena) AS cena_prum,doklady.c_dokl, 0 as maxriadok;
		FROM obratyx LEFT JOIN doklady ON obratyx.c_dokl=doklady.c_dokl INTO CURSOR skl_n WHERE BETWEEN(obratyx.c_dokl, m.d1, m.d2) READWRITE
	SELECT c_karty, MAX(c_dokl) as c_dokl, 1 as maxriadok FROM skl_n INTO CURSOR sklnman GROUP BY c_karty
	UPDATE skl_n SET maxriadok=sklnman.maxriadok from sklnman WHERE skl_n.c_karty=sklnman.c_karty AND skl_n.c_dokl=sklnman.c_dokl
	DELETE FROM skl_n WHERE maxriadok=0
	UPDATE obratka SET cena_kon=skl_n.cena_prum from skl_n WHERE obratka.c_karty=skl_n.c_karty
	SELECT DISTINCT druh,.T. AS lbl FROM obratka INTO CURSOR vybery READWRITE
	INDEX ON druh TAG druh
	IF RECCOUNT()>0
		m.gcfilter_triedenia=""
		DO FORM seldruhy WITH "vybery" TO m.druhxx
		SELECT vybery
		LOCATE FOR !EMPTY(lbl)
		IF FOUND()
			SELECT obratka
			SET RELATION TO druh INTO vybery
			DELETE FOR ALLTRIM(druh)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
			SET RELATION OFF INTO vybery
			USE IN vybery
			filtrujpriznaky("obratka")
			RELEASE m.gcfilter_triedenia
			SELECT DISTINCT nazev AS druh,.T. AS lbl FROM obratka INTO CURSOR vybery READWRITE
			INDEX ON druh TAG druh
			IF RECCOUNT()>0
				DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
				SELECT vybery
				LOCATE FOR !EMPTY(lbl)
				IF FOUND()
					SELECT obratka
					SET RELATION TO nazev  INTO vybery
					DELETE FOR ALLTRIM(nazev)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
					SET RELATION OFF INTO vybery
					DO pr_report WITH "m_nakupy2"
				ENDIF
			ENDIF
		ENDIF
	ENDIF

ENDIF 

IF "3" $ m.dily
    SELECT *, SPACE(7) as mesic FROM nakupy INTO CURSOR nakupy_mes READWRITE 
    REPLACE ALL mesic WITH TRANS(YEAR(datum_dokl), "9999") + "-" + TRANSFORM(MONTH(datum_dokl), "@L 99")
    SELECT druh, c_karty, nazev, mesic, SUM(mnozstvi) as mnozstvi, SUM(mnozstvi*cena) as prachy, 0 as percento ;
           GROUP BY 1,2,3,4 FROM nakupy_mes INTO CURSOR nakupy_mes_sum
    DO pr_report WITH "m_nakupy_mes"
    
    SELECT "skupiny" as druh,  druh as nazev, 0 as c_karty, mesic, SUM(mnozstvi) as mnozstvi, SUM(mnozstvi*prachy) as prachy, 0 as percento ;
           GROUP BY 1,2,3,4 FROM nakupy_mes_sum INTO CURSOR nakupy_mes_sum_sum
    DO pr_report WITH "m_nakupy_mes"

ENDIF 

IF "4" $ m.dily
    SELECT obratyx.c_karty,SPACE(30) AS nazev, SPACE(m.sirkastlpcadruh) AS druh, 000000000 AS c_druh, obratyx.c_dokl, doklady.datum_dokl, partneri.partner, obratyx.mnozstvi, obratyx.cena, obratyx.cena_part, 0000.0000 AS percento, SPACE(7) as mesic ;
           WHERE obratyx.operace="VE" .AND. obratyx.c_dokl=doklady.c_dokl .AND. doklady.c_partner=partneri.c_partner ;
           FROM obratyx, doklady, partneri INTO CURSOR prodeje_mes READWRITE ORDER BY obratyx.c_karty ASC, obratyx.c_dokl DESC
    SET RELATION TO c_karty INTO karty
    REPLACE ALL nazev WITH karty.nazev
    IF "SKL" $ m.gcpgm_param
        REPLACE ALL c_druh WITH karty.c_druhs
        REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druh
    ELSE
        REPLACE ALL c_druh WITH karty.c_druh
        REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhs
    ENDIF
    REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhk
    SET RELATION TO
    UPDATE prodeje_mes SET druh=skupiny.druh FROM skupiny WHERE prodeje_mes.c_druh=skupiny.c_druh

    SELECT DISTINCT druh, .T. AS lbl  FROM prodeje_mes INTO CURSOR vybery READWRITE
    INDEX ON druh TAG druh
    IF RECCOUNT()>0
		m.gcfilter_triedenia=""
        DO FORM seldruhy WITH "vybery" TO m.druhxx
        SELECT vybery
        LOCATE FOR !EMPTY(lbl)
        IF FOUND()
            SELECT prodeje_mes
            SET RELATION TO druh INTO vybery
            DELETE FOR ALLTRIM(druh)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
            SET RELATION OFF INTO vybery
        ENDIF
        filtrujpriznaky("prodeje_mes")
		RELEASE m.gcfilter_triedenia
    ENDIF
    USE IN vybery

    SELECT DISTINCT nazev AS druh,.T. AS lbl FROM prodeje_mes INTO CURSOR vybery READWRITE
    INDEX ON druh TAG druh
    IF RECCOUNT()>0
        DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
        SELECT vybery
        LOCATE FOR !EMPTY(lbl)
        IF FOUND()
            SELECT prodeje_mes
            SET RELATION TO nazev INTO vybery
            DELETE FOR ALLTRIM(nazev)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
            SET RELATION OFF INTO vybery
        ENDIF
    ENDIF
    USE IN vybery

    SELECT prodeje_mes
    REPLACE ALL mesic WITH TRANS(YEAR(datum_dokl), "9999") + "-" + TRANSFORM(MONTH(datum_dokl), "@L 99")
    SELECT druh, c_karty, nazev, mesic, SUM(mnozstvi) as mnozstvi, - SUM(mnozstvi*cena) as prachy, - SUM(mnozstvi*cena_part) as prachyprod, $0 as percento ;
           GROUP BY 1,2,3,4 FROM prodeje_mes INTO CURSOR prodeje_mes_sum READWRITE 
    REPLACE FOR prachy # 0 percento WITH (100 * prachyprod / prachy) - 100
    DO pr_report WITH "prodeje_mes"
ENDIF 
DO pozavirej WITH "obratka, sklnmax, skl_n, vybery, prodeje_mes, prodeje_mes_sum, nakupy1, nakupy2, vybery"


*!*****************************************************************************
*!
*!      Procedure: NAKUPPAR
*!
*!*****************************************************************************
PROCEDURE nakuppar
SELECT SPACE(30) AS nazev, SPACE(m.sirkastlpcadruh) AS druh, 000000000 AS c_druh, obratyx.c_karty, obratyx.c_dokl AS c_dokl1, obratyx.c_dokl AS c_dokl2, obratyx.c_dokl AS c_dokl3, doklady.datum_dokl AS datum_dok1, doklady.datum_dokl AS datum_dok2,;
	doklady.datum_dokl AS datum_dok3, partneri.partner AS partner1, partneri.partner AS partner2, partneri.partner AS partner3, obratyx.mnozstvi AS mnozstvi1, obratyx.mnozstvi AS mnozstvi2, obratyx.mnozstvi AS mnozstvi3, obratyx.cena AS cena1,;
	obratyx.cena AS cena2, obratyx.cena AS cena3 FROM obratyx, doklady, partneri;
	WHERE obratyx.operace="PE" .AND. obratyx.c_dokl=doklady.c_dokl .AND. doklady.c_partner=partneri.c_partner INTO CURSOR nakupy READWRITE
SET RELATION TO c_karty INTO karty
REPLACE ALL nazev WITH karty.nazev
SET RELATION TO c_karty INTO karty
IF "SKL" $ m.gcpgm_param
	REPLACE ALL  c_druh WITH karty.c_druhs
	REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druh
ELSE
	REPLACE c_druh WITH karty.c_druh
	REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhs
ENDIF
REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druhk
SET RELATION OFF INTO karty
UPDATE nakupy SET druh=skupiny.druh FROM skupiny WHERE nakupy.c_druh=skupiny.c_druh
SELECT seldruhy
INDEX ON druh TAG druh
SELECT nakupy
SET RELATION TO druh INTO seldruhy
DELETE FOR ALLTRIM(druh)=ALLTRIM(seldruhy.druh).AND.EMPTY(seldruhy.lbl)
SET RELATION OFF INTO seldruhy


SELECT DISTINCT nazev AS druh,.T. AS lbl FROM nakupy INTO CURSOR vybery READWRITE
INDEX ON druh TAG druh
IF RECCOUNT()>0
	m.gcfilter_triedenia=""
	DO FORM seldruhy WITH "vybery","nazev",250 TO m.druhxx
	SELECT vybery
	LOCATE FOR !EMPTY(lbl)
	IF FOUND()
		SELECT nakupy
		SET RELATION TO nazev  INTO vybery
		DELETE FOR ALLTRIM(nazev)=ALLTRIM(vybery.druh).AND.EMPTY(vybery.lbl)
		SET RELATION OFF INTO vybery
	ENDIF
	filtrujpriznaky("nakupy")
	RELEASE m.gcfilter_triedenia
ENDIF
USE IN vybery
IF "2" $ m.dily
	SELECT c_karty, nazev, druh, AVG(cena1) as cena, cena1*mnozstvi1 as suma, SUM(mnozstvi1) as mnozstvi, partner1 as partner FROM nakupy INTO CURSOR nakupyx GROUP BY c_karty, nazev, druh, partner1 ORDER BY druh, nazev READWRITE 
ENDIF 
IF "1" $ m.dily
	SELECT nakupy
	IF RECCOUNT()>0
		SELECT * FROM nakupy ORDER BY druh ASC, nazev ASC, c_dokl1 DESC INTO CURSOR nakupy2 READWRITE
		REPLACE ALL partner2 WITH "", partner3 WITH "", mnozstvi2 WITH 0, mnozstvi3 WITH 0, cena2 WITH 0, cena3 WITH 0, c_dokl2 WITH 0, c_dokl3 WITH 0, datum_dok2 WITH CTOD("01.01.1900"), datum_dok3 WITH CTOD("01.01.1900")
		GO TOP
		m.nazev=REPLICATE("_",30)
		m.partner1="__"
		m.partner2="__"
		m.pocetpart=0
		SCAN ALL
			IF nakupy2.nazev=m.nazev
				DO CASE
					CASE m.pocetpart=0
						m.partner1=nakupy2.partner1
						m.pocetpart=1
					CASE m.pocetpart=1
						IF m.partner1=nakupy2.partner1
							REPLACE datum_dok1 WITH CTOD("01.01.1900"), partner1 WITH "", c_dokl1 WITH 0, cena1 WITH 0, mnozstvi1 WITH 0
						ELSE
							REPLACE partner2 WITH partner1, c_dokl2 WITH c_dokl1, cena2 WITH cena1, mnozstvi2 WITH mnozstvi1, datum_dok2 WITH datum_dok1
							REPLACE datum_dok1 WITH CTOD("01.01.1900"), partner1 WITH "", c_dokl1 WITH 0, cena1 WITH 0, mnozstvi1 WITH 0
							m.partner2=nakupy2.partner2
							m.pocetpart=2
						ENDIF
					CASE m.pocetpart=2
						IF m.partner1=nakupy2.partner1 .OR. m.partner2=nakupy2.partner1
							REPLACE datum_dok1 WITH CTOD("01.01.1900"), partner1 WITH "", c_dokl1 WITH 0, cena1 WITH 0, mnozstvi1 WITH 0
						ELSE
							REPLACE partner3 WITH partner1, c_dokl3 WITH c_dokl1, cena3 WITH cena1, mnozstvi3 WITH mnozstvi1, datum_dok3 WITH datum_dok1
							REPLACE datum_dok1 WITH CTOD("01.01.1900"), partner1 WITH "", c_dokl1 WITH 0, cena1 WITH 0, mnozstvi1 WITH 0
							m.pocetpart=3
						ENDIF
					OTHERWISE
						REPLACE datum_dok1 WITH CTOD("01.01.1900"), partner1 WITH "", c_dokl1 WITH 0, cena1 WITH 0, mnozstvi1 WITH 0
				ENDCASE
			ELSE
				SCATTER MEMVAR FIELDS nazev, partner1
				m.pocetpart=1
			ENDIF
		ENDSCAN
		SELECT c_karty,nazev, druh, MAX(c_dokl1) AS c_dokl1, MAX(c_dokl2) AS c_dokl2, MAX(c_dokl3) AS c_dokl3, MAX(datum_dok1) AS datum_dok1, MAX(datum_dok2) AS datum_dok2,;
			MAX(datum_dok3) AS datum_dok3, MAX(partner1) AS partner1, MAX(partner2) AS partner2, MAX(partner3) AS partner3, SUM(mnozstvi1) AS mnozstvi1, SUM(mnozstvi2) AS mnozstvi2, SUM(mnozstvi3) AS mnozstvi3, SUM(cena1) AS cena1,;
			SUM(cena2) AS cena2, SUM(cena3) AS cena3 FROM nakupy2;
			GROUP BY nazev INTO CURSOR nakupy1 ORDER BY druh, nazev
		GO TOP
		DO pr_report WITH "m_nakpar"
		USE IN nakupy1
		USE IN nakupy
	ELSE
		USE IN nakupy
	ENDIF
ENDIF 
IF "2" $ m.dily
	SELECT nakupyx
	DO pr_report WITH "m_nakpar1"
ENDIF 	
DO pozavirej WITH "nakupy, nakupy1, nakupy2, vybery, nakupyx"

*!*****************************************************************************
*!      Procedure: VYKAZSPDANE
*!*****************************************************************************
PROCEDURE vykazspdane
PRIVATE m.hladajcdoklod, m.hladajcdokldo
m.hladajcdoklod=m.d1
m.hladajcdokldo=m.d2
IF m.spotrebnadan<>0
	IF MESSAGEBOX(jaz("form_zacely_hotel"),4+32+256,"")=6
		IF m.issql
			rv("zostobr_spdan","spd")
		ELSE
			USE (m.fn_obraty) ALIAS obraty IN 0
			USE vzostobr_spdan ALIAS spd IN 0
			USE IN obraty
		ENDIF
		IF m.tlac_operace="__"
			SELECT operace, c_karty, SPACE(30) AS nazev, mnozstvi, spdan, spstupne, $0 AS objem, ;
				SPACE(m.sirkastlpcadruh) AS druh, 000000 AS c_druh, "   " AS spart WHERE INLIST(LEFT(operace, 1), "P", "V") ;
				ORDER BY c_stredisk FROM spd INTO CURSOR pomxx READWRITE
		ELSE
			SELECT operace, c_karty, SPACE(30) AS nazev, mnozstvi, spdan, spstupne, $0 AS objem, ;
				SPACE(m.sirkastlpcadruh) AS druh, 000000 AS c_druh, "   " AS spart WHERE INLIST(LEFT(operace, 1), "P", "V") AND INLIST(operace,&operace__txt);
				ORDER BY c_stredisk FROM obratyx INTO CURSOR pomxx READWRITE
		ENDIF
		REPLACE FOR operace = "VP" operace WITH "PP"
		SET RELATION TO c_karty INTO karty
		REPLACE ALL objem WITH karty.objem

		SELECT c_karty, operace, SUM(mnozstvi) AS mnozstvi, spdan, spstupne, objem ;
			GROUP BY 1, 2 FROM pomxx INTO CURSOR obr READWRITE
	ELSE
		SELECT * FROM pom INTO CURSOR pomxx READWRITE
		SELECT c_karty, operace, SUM(mnozstvi) AS mnozstvi, spdan, spstupne, objem ;
			GROUP BY 1, 2 FROM pomxx INTO CURSOR obr WHERE spdan<>0 READWRITE
	ENDIF
	m.hladajcdoklod=0
	m.hladajcdokldo=m.d1-1
	IF m.issql
		rv("zostobr_spdan","skl_ix")
	ELSE
		USE (m.fn_obraty) ALIAS obraty IN 0
		USE vzostobr_spdan ALIAS skl_ix IN 0
		USE (m.fn_spotrebnadan) ALIAS spdane ORDER c_karty IN 0
		USE IN obraty
		SELECT skl_ix
		SET RELATION TO c_karty INTO spdane
		REPLACE spstupne WITH spdane.koeficient FOR !EMPTY(spdane.c_karty)
		SET RELATION OFF INTO spdane
		USE IN spdane
	ENDIF

	SELECT skl_ix.c_stredisk, SUM(skl_ix.mnozstvi) AS mnozstvi,SUM(skl_ix.mnozstvi*skl_ix.spdan*skl_ix.spstupne*karty.objem/100) AS spdan, skl_ix.c_karty, skl_ix.spstupne FROM skl_ix LEFT JOIN karty ON skl_ix.c_karty=karty.c_karty GROUP BY skl_ix.c_stredisk, skl_ix.c_karty WHERE skl_ix.operace<>"PR" INTO CURSOR spdanex ;
		UNION 	SELECT skl_ix.c_stredisk, skl_ix.mnozstvi AS mnozstvi, SUM(skl_ix.spdan) AS spdan, skl_ix.c_karty, skl_ix.spstupne FROM skl_ix GROUP BY skl_ix.c_stredisk, skl_ix.c_karty WHERE skl_ix.operace="PR"
	SELECT SUM(skl_ix.mnozstvi) AS mnozstvi, SUM(spdan) AS spdan, c_karty, spstupne FROM spdanex INTO CURSOR spdane GROUP BY c_karty, spstupne
	USE IN spdanex

	CREATE CURSOR obrat(c_karty i, operace c(2), zustatek Y, mnozstvi Y, spd_poc Y,;
		mnoz_p_ Y, spd_p_ Y, mnoz_ve Y, spd_ve Y, mnoz_pv Y, spd_pv Y, mnoz_vv Y, spd_vv Y, mnoz_vk Y, spd_vk Y, mnoz_xx Y, spd_xx Y, spdan Y, spstupne Y , objem Y)

	INSERT INTO obrat (c_karty, spd_poc, spstupne, zustatek) SELECT c_karty, spdan, spstupne, mnozstvi FROM spdane
	USE IN spdane

	INSERT INTO obrat (c_karty, spdan, operace, spstupne, mnozstvi, objem) SELECT c_karty, spdan, operace, spstupne, mnozstvi, objem FROM obr

	REPLACE FOR operace = "VE"              mnoz_ve WITH mnozstvi, spd_ve WITH mnozstvi*spdan*spstupne*objem/100, mnozstvi WITH 0
	REPLACE FOR operace = "VV"              mnoz_vv WITH mnozstvi, spd_vv WITH mnozstvi*spdan*spstupne*objem/100, mnozstvi WITH 0
	REPLACE FOR operace = "VK"              mnoz_vk WITH mnozstvi, spd_vk WITH mnozstvi*spdan*spstupne*objem/100, mnozstvi WITH 0
	REPLACE FOR operace = "PV"              mnoz_pv WITH mnozstvi, spd_pv WITH mnozstvi*spdan*spstupne*objem/100, mnozstvi WITH 0
	REPLACE FOR operace = "PE"			    mnoz_p_ WITH mnozstvi, spd_p_ WITH mnozstvi*spdan*spstupne*objem/100, mnozstvi WITH 0
	REPLACE ALL                             mnoz_xx WITH mnozstvi, spd_xx WITH mnozstvi*spdan*spstupne*objem/100, mnozstvi WITH 0

	SELECT c_karty, SPACE(30) AS nazev, SPACE(m.sirkastlpcadruh) AS druh, 0000000 AS c_druh, SPACE(3) AS spart, ;
		SUM(zustatek) AS pocatek, objem, spstupne, spdan, ;
		SUM(spd_poc) AS spd_poc, ;
		SUM(mnoz_p_) AS mnoz_p_, SUM(spd_p_)  AS spd_p_, ;
		SUM(mnoz_ve) AS mnoz_ve, SUM(spd_ve)  AS spd_ve, ;
		SUM(mnoz_vv) AS mnoz_vv, SUM(spd_vv)  AS spd_vv, ;
		SUM(mnoz_pv) AS mnoz_pv, SUM(spd_pv)  AS spd_pv, ;
		SUM(mnoz_vk) AS mnoz_vk, SUM(spd_vk)  AS spd_vk, ;
		SUM(mnoz_xx) AS mnoz_xx, SUM(spd_xx)  AS spd_xx ;
		GROUP BY 1 FROM obrat INTO CURSOR obratovk READWRITE

	DELETE FOR pocatek = 0 .AND. mnoz_ve = 0 .AND. mnoz_pv = 0 .AND. mnoz_vv = 0 .AND. mnoz_vk = 0 .AND. mnoz_p_ = 0 .AND. mnoz_xx = 0

	SET RELATION TO c_karty INTO karty
	REPLACE FOR EMPTY(nazev) nazev WITH karty.nazev, c_druh WITH karty.c_druhs, objem WITH karty.objem
	REPLACE FOR EMPTY(c_druh) c_druh WITH karty.c_druh
	UPDATE pomxx SET druh=skupiny.druh, spart=skupiny.spart FROM skupiny WHERE pomxx.c_druh=skupiny.c_druh
	REPLACE FOR EMPTY(druh) druh WITH m.ostatni
	REPLACE FOR EMPTY(spart) spart WITH m.ostatni

	REPLACE FOR EMPTY(nazev) nazev WITH m.ostatni
	REPLACE FOR EMPTY(druh)  druh  WITH m.ostatni
	REPLACE FOR EMPTY(spart) spart WITH m.ostatni

	INDEX ON druh + nazev TAG dr_naz
	IF "1" $ m.dily
		DO pr_report WITH "spdan"
	ENDIF
	IF "2" $ m.dily
		SELECT SUM(zustatek) AS pocatek, spstupne, spdan, ;
			SUM(spd_poc) AS spd_poc, ;
			SUM(mnoz_p_) AS mnoz_p_, SUM(spd_p_)  AS spd_p_, ;
			SUM(mnoz_ve) AS mnoz_ve, SUM(spd_ve)  AS spd_ve, ;
			SUM(mnoz_vv) AS mnoz_vv, SUM(spd_vv)  AS spd_vv, ;
			SUM(mnoz_pv) AS mnoz_pv, SUM(spd_pv)  AS spd_pv, ;
			SUM(mnoz_vk) AS mnoz_vk, SUM(spd_vk)  AS spd_vk, ;
			SUM(mnoz_xx) AS mnoz_xx, SUM(spd_xx)  AS spd_xx ;
			GROUP BY spstupne FROM obrat INTO CURSOR obratovk
		DO pr_report WITH "spdan2"
	ENDIF
	DO pozavirej WITH "obr, obrat, obratovk, skl_i, spdane, spdanex, skl_ix, pomxx, spd"
ELSE
	=MESSAGEBOX(jaz("forn_nenastavena_spotrebna_dan"),64,"FoodMan")
ENDIF


*!*****************************************************************************
PROCEDURE daj_datum_all
DO pozavirej WITH "doklady,obraty"
SELECT 0
CREATE TABLE (m.gcpri_path + "selkasy") (c_partner i, id_zkratka c(25), lbl l, stredskup c(4))
USE
CREATE TABLE (m.gcpri_path + "seldruhy") (druh c(m.sirkastlpcadruh), lbl l)
USE
STORE "NIC" TO d1_txt, d2_txt

IF EMPTY(n_checks("POHYBY_EXCL_"))

	STORE "" TO operace, operace_txt, operace__txt
	DO daj_operace

	DO spoj_obraty_vice_stredisek

	d1 = 0
	d2 = 99999999
	d1_dat_dokl = m.d1_txt
	d2_dat_dokl = m.d2_txt

	DO pozavirej WITH "doklady, obraty"
	USE (m.fn_dokl_s) IN 0 ALIAS doklady ORDER c_dokl
	USE (m.fn_obra_s) IN 0 ALIAS obraty
	DO s_obr_xxx

ELSE

	= MESSAGEBOX(jaz("ted nelze, zkuste pozdeji"),0+16+256, jaz("m_s_obr vyber obdobi"), 3333)
ENDIF

*!*****************************************************************************
PROCEDURE daj_operace
CREA  TABLE (m.gcpri_path + "seldruhy") (operace c(2), druh c(25), lbl l)
INSERT INTO (m.gcpri_path + "seldruhy") (operace, druh, lbl) VALUE ("PE", lng_oper("PE"), .T. )
INSERT INTO (m.gcpri_path + "seldruhy") (operace, druh, lbl) VALUE ("PV", lng_oper("PV"), .T. )
INSERT INTO (m.gcpri_path + "seldruhy") (operace, druh, lbl) VALUE ("VE", lng_oper("VE"), .T. )
INSERT INTO (m.gcpri_path + "seldruhy") (operace, druh, lbl) VALUE ("VV", lng_oper("VV"), .T. )
INSERT INTO (m.gcpri_path + "seldruhy") (operace, druh, lbl) VALUE ("PI", lng_oper("PI"), .T. )
INSERT INTO (m.gcpri_path + "seldruhy") (operace, druh, lbl) VALUE ("VK", lng_oper("VK"), .T. )
INSERT INTO (m.gcpri_path + "seldruhy") (operace, druh, lbl) VALUE ("VR", lng_oper("VR"), .T. )
INSERT INTO (m.gcpri_path + "seldruhy") (operace, druh, lbl) VALUE ("_P", lng_oper("VP"), .T. )
INSERT INTO (m.gcpri_path + "seldruhy") (operace, druh, lbl) VALUE ("_B", lng_oper("VB"), .T. )
INSERT INTO (m.gcpri_path + "seldruhy") (operace, druh, lbl) VALUE ("_C", lng_oper("VC"), .T. )
INDEX ON operace TAG operace

DO FORM seldruhy TO m.druhx
operace_txt = ""
LOCATE FOR EMPTY(lbl)
IF FOUND()
	operace = ""
	SCAN FOR .NOT. EMPTY(lbl)
		operace     = m.tlac_operace + seldruhy.operace + ","
		operace_txt = m.operace_txt + ALLTRIM(seldruhy.druh) + ","
	ENDSCAN

	IF RIGHT(m.tlac_operace,1)=","
		m.tlac_operace     = LEFT(m.tlac_operace    ,LEN(m.tlac_operace)-1)
		m.operace_txt = LEFT(m.operace_txt,LEN(m.operace_txt)-1)
	ENDIF

	IF LEN(m.operace_txt) > 33
		operace_txt = m.tlac_operace
	ENDIF
	m.tlac_operace = STRTRAN(m.tlac_operace, "_B", "VB,PB")
	m.tlac_operace = STRTRAN(m.tlac_operace, "_C", "VC,PC")
	m.tlac_operace = STRTRAN(m.tlac_operace, "_P", "VP,PP")
ELSE
	m.tlac_operace="__"
ENDIF
USE IN seldruhy

operace__txt = '"' + STRTRAN(m.tlac_operace, ',' , '","') + '"'    && tohle se pouzije jako parametr INLISTu


*!*****************************************************************************
PROCEDURE s_obr_all
sox_had = CREATEOBJECT("had")
sox_had.SHOW()

SELECT o_c_stred, c_dokl, c_karty, SUM(mnozstvi) AS mnozstvi, SUM(mnozstvi * cena) AS prachy, ;
	00000000 AS c_partner, SPACE(30) AS partner, SPACE(30) AS stredisko, SPACE(30) AS nazev, SPACE(m.sirkastlpcadruh) AS druh ;
	GROUP BY 1,2 FROM (m.fn_obra_s) INTO TABLE (m.gcpri_path + "pom_all_1")

sox_had.set_had(22)
SET RELATION TO c_dokl INTO doklady
REPLACE ALL c_partner WITH doklady.c_partner
SET RELATION TO c_partner INTO partneri
REPLACE FOR ! EMPTY(partneri.partner) partner WITH partneri.partner
SET RELATION TO o_c_stred INTO partneri
REPLACE FOR ! EMPTY(partneri.partner) stredisko WITH partneri.partner

sox_had.set_had(44)
SET ORDER TO c_karty IN karty
SET RELATION TO c_karty INTO karty
REPLACE ALL nazev WITH karty.nazev, druh WITH karty.druh_skl

sox_had.set_had(66)
SET RELATION TO partner INTO selkasy
REPLACE FOR .NOT. selkasy.lbl  partner WITH m.nechci, c_partner WITH 9999999 * SIGN(c_partner), c_dokl WITH 9999999 * SIGN(c_partner), c_karty WITH 9999999, nazev WITH m.nechci, stredisko WITH m.nechci

SET RELATION TO druh INTO seldruhy
REPLACE FOR .NOT. seldruhy.lbl partner WITH m.nechci, c_partner WITH 9999999 * SIGN(c_partner), c_dokl WITH 9999999 * SIGN(c_partner), c_karty WITH 9999999, nazev WITH m.nechci, stredisko WITH m.nechci
SET RELATION TO

sox_had.set_had(88)
SELECT partner, stredisko, nazev, c_karty, SUM(mnozstvi) AS mnozstvi, SUM(prachy) AS prachy ;
	GROUP BY 1,2,3 FROM (m.gcpri_path + "pom_all_1") INTO TABLE (m.gcpri_path + "pom_all")

*INDEX on partner + stredisko + nazev TAG p_s_n

RELEASE sox_had

DO pr_report WITH "OBR_ALL"

DO pozavirej WITH "dok_all, obr_all, pom_all"


*!*****************************************************************************
PROCEDURE s_obr_dav
SELECT pom

IF "1" $ m.dily
	SELECT * FROM pom WHERE BETWEEN(c_dokl, m.d1, m.d2) AND operace="VK" AND "*"$pz2 INTO CURSOR pomxx ORDER BY c_dokl 
	INDEX ON c_dokl TO (m.gcpri_path + "s_obr4")
	DO pr_report WITH "obrpre@dav"
	DO pozavirej WITH "pomxx"
ENDIF

IF "2" $ m.dily

	SELECT c_dokl, spart + " " AS spart, SIGN(mnozstvi) AS znamenko, doklad, partner, datum_dokl, operace, provedl, ;
		pz1, pz2, SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy,SUM(MTON(mnozstvi)*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachypar ;
		GROUP BY 1, 2, 3 FROM pom INTO CURSOR dok_spart READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2) AND operace="VK" AND "*"$pz2
	DO pr_report WITH "dok_spar@dav"
	DO pozavirej WITH "dok_spart"
ENDIF


*!*****************************************************************************
PROCEDURE s_obr4_3
SELECT pom

IF "1" $ m.dily
	SELECT partner, c_dokl, doklad, datum_dokl, operace, typ_pohybu, dan, pz1, pz2, ;
		SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy, SUM(MTON(mnozstvi)*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachypar ;
		GROUP BY 1, 2 FROM pom INTO CURSOR par_dokl READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
	REPLACE FOR operace = "PE" prachypar WITH prachy * dan
	INDEX on operace+STR(typ_pohybu,10,0)+STR(doklad,10,0) TAG aaa
	DO pr_report WITH "par_dokl_2@3"
	DO pozavirej WITH "par_dokl"
ENDIF
IF "2" $ m.dily
	SELECT platba, partner, c_dokl, doklad, datum_dokl, operace, typ_pohybu, dan, pz1, pz2, ;
		SUM(MTON(mnozstvi)*NTOM(MTON(cena)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(MTON(mnozstvi)*NTOM(MTON(cena_part)*en__meny(c_mena,m.girep_mena))) AS prachypar ;
		GROUP BY 1, 2, 3 FROM pom INTO CURSOR par_dokl READWRITE
	INDEX on operace+STR(typ_pohybu,10,0)+platba+STR(doklad,10,0) TAG aaa
	REPLACE FOR operace = "PE" prachypar WITH prachy * dan
	DO pr_report WITH "obr_plat_par_dokl_2"
	DO pozavirej WITH "par_dokl"
ENDIF
IF "3" $ m.dily
	SELECT partner, c_dokl, doklad, datum_dokl, operace, typ_pohybu, dan, pz1, pz2, ;
		SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy, SUM(MTON(mnozstvi)*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachypar ;
		GROUP BY 1, 2 FROM pom INTO CURSOR par_dokl READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
	REPLACE FOR operace = "PE" prachypar WITH prachy * dan
	INDEX on operace+STR(typ_pohybu,10,0)+DTOS(datum_dokl)+STR(doklad,10,0) TAG aaa
	DO pr_report WITH "par_dokl_2@4"
	DO pozavirej WITH "par_dokl"
ENDIF
IF "4" $ m.dily
	SELECT platba, partner, c_dokl, doklad, datum_dokl, operace, typ_pohybu, dan, pz1, pz2, ;
		SUM(MTON(mnozstvi)*NTOM(MTON(cena)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(MTON(mnozstvi)*NTOM(MTON(cena_part)*en__meny(c_mena,m.girep_mena))) AS prachypar ;
		GROUP BY 1, 2, 3 FROM pom INTO CURSOR par_dokl READWRITE
	INDEX on operace+STR(typ_pohybu,10,0)+platba+DTOS(datum_dokl)+STR(doklad,10,0) TAG aaa
	REPLACE FOR operace = "PE" prachypar WITH prachy * dan
	DO pr_report WITH "obr_plat_par_dokl_2@1"
	DO pozavirej WITH "par_dokl"
ENDIF

IF "4" $ m.dily
	SELECT platba, partner, c_dokl, doklad, datum_dokl, operace, typ_pohybu, dan, pz1, pz2, ;
		SUM(MTON(mnozstvi)*NTOM(MTON(cena)*en__meny(c_mena,m.girep_mena))) AS prachy, SUM(MTON(mnozstvi)*NTOM(MTON(cena_part)*en__meny(c_mena,m.girep_mena))) AS prachypar ;
		GROUP BY 1, 2, 3 FROM pom INTO CURSOR par_dokl READWRITE
	INDEX on operace+STR(typ_pohybu,10,0)+platba+DTOS(datum_dokl)+STR(doklad,10,0) TAG aaa
	REPLACE FOR operace = "PE" prachypar WITH prachy * dan
	DO pr_report WITH "obr_plat_par_dokl_2@1"
	DO pozavirej WITH "par_dokl"
ENDIF

IF "5" $ m.dily
	SELECT c_dokl, spart + " " AS spart, dan, ;
		 SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy,SUM(MTON(mnozstvi)*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachypar ;
		GROUP BY 1, 2, 3 FROM pom INTO CURSOR dokspart1 READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2) AND operace="PE"

	SELECT c_dokl, spart + " " AS spart, SIGN(mnozstvi) AS znamenko, doklad, partner, datum_dokl, operace, provedl, ;
		pz1, pz2, SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy,SUM(MTON(mnozstvi)*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachypar, ;
		$0 as sdph1, $0 as sdph2, $0 as sdph3, $0 as sdph4, 0.00 as dan1, 0.00 as dan2, 0.00 as dan3, 0.00 as dan4  ;
		GROUP BY 1, 2, 3 FROM pom INTO CURSOR dok_spart READWRITE WHERE BETWEEN(c_dokl, m.d1, m.d2)
	IF USED("dokspart1") AND RECCOUNT("dokspart1")>0
		LOCAL sadzba, cisloriadku
		m.cisloriadku="1"
		UPDATE dok_spart SET dan1=1, sdph1=prachypar WHERE prachy=prachypar
		SELECT disti dan FROM dokspart1 INTO CURSOR aaa1
		SCAN ALL 
			m.sadzba=aaa1.dan
			m.cisloriadku=STR(VAL(m.cisloriadku)+1,1,0)
			IF cisloriadku<="4"
				UPDATE dok_spart SET dan&cisloriadku=m.sadzba WHERE .t.
				UPDATE dok_spart SET sdph&cisloriadku=dokspart1.prachypar from dokspart1 WHERE dok_spart.c_dokl=dokspart1.c_dokl AND dok_spart.spart=dokspart1.spart AND dokspart1.dan=m.sadzba AND dok_spart.prachy<>dok_spart.prachypar
			ELSE 
				=MESSAGEBOX(jaz("form_privela_sadzieb_dph"),64, "FoodMan")
				EXIT
			ENDIF 
		ENDSCAN 
	ENDIF 
	SELECT dok_spart
	REPLACE FOR operace = "PI" .AND. znamenko < 0 spart WITH "- " + spart
	DO pr_report WITH "dok_spar_dph"
	DO pozavirej WITH "dok_spart, dokspart1, aaa1"
ENDIF

*!*****************************************************************************
PROCEDURE s_obr_polot
IF USED("pom")
	SELECT pom
	IF "1" $ m.dily
		SELECT * FROM pom WHERE BETWEEN(c_dokl, m.d1, m.d2) AND INLIST(operace,"VP","PP") AND mnozstvi<0 INTO CURSOR pomxx ORDER BY c_dokl READWRITE 
		REPLACE ALL mnozstvi WITH -mnozstvi
		DO pr_report WITH "obrpre@dav"
		DO pozavirej WITH "pomxx"
	ENDIF

	IF "2" $ m.dily

		SELECT c_karty, nazev, SUM(mnozstvi) as mnozstvi, SUM(mnozstvi*cena) as suma, druh, spart FROM pom WHERE BETWEEN(c_dokl, m.d1, m.d2) AND INLIST(operace,"VP","PP") AND mnozstvi<0 INTO CURSOR pomxx ORDER BY druh, nazev GROUP BY c_karty READWRITE 
		REPLACE ALL mnozstvi WITH -mnozstvi, suma WITH -suma
		DO pr_report WITH "obrpol"
		DO pozavirej WITH "pomxx"
	ENDIF
ENDIF 

PROCEDURE s_obr_refcena
SELECT obratyx.c_stredisk,SPACE(30) AS partner ,obratyx.c_karty, obratyx.mnozstvi, (NTOM(MTON(obratyx.cena)*en__meny(doklady.c_mena,m.girep_mena))) AS cena, doklady.c_partner,karty.nazev, karty.c_druhs AS c_druh, karty.c_druhk, karty.c_druh as c_druhstr, $0 AS percento, $0 AS refcena, $0 as pomer, SPACE(m.sirkastlpcadruh) AS druh, doklady.datum_dokl, doklady.doklad ;
	WHERE INLIST(obratyx.operace,"PE") AND BETWEEN(obratyx.c_dokl, m.d1, m.d2);
	FROM obratyx LEFT JOIN karty ON obratyx.c_karty=karty.c_karty LEFT JOIN doklady ON obratyx.c_dokl=doklady.c_dokl ;
	INTO CURSOR px READWRITE
UPDATE px SET c_druh=c_druhk WHERE EMPTY(c_druh) OR ISNULL(c_druh)
UPDATE px SET c_druh=c_druhstr WHERE EMPTY(c_druh) OR ISNULL(c_druh)
UPDATE px SET druh=skupiny.druh, percento=skupiny.prir2 FROM skupiny WHERE px.c_druh=skupiny.c_druh
REPLACE FOR EMPTY(druh) druh WITH m.ostatni


DELETE FROM px WHERE c_partner IN (SELECT c_partner FROM selkasy WHERE !lbl)
DELETE FROM px WHERE druh IN (SELECT druh FROM seldruhy WHERE !lbl)
UPDATE px SET partner=selkasy.id_zkratka from selkasy WHERE px.c_partner=selkasy.c_partner

SELECT disti c_stredisk FROM px INTO CURSOR strpx
LOCAL m.bolsklad
m.bolsklad=.t.
SCAN ALL 
	m.hladajcstredisk=strpx.c_stredisk
	IF USED("skl1")
		=REQUERY("skl1")
	ELSE 
		IF m.issql
			rv("sklad","skl1")
		ELSE
    		IF !USED("sklad")
    			m.bolsklad=.f.
				USE (m.fn_sklad) ALIAS sklad IN 0
			ENDIF 
			USE vsklad ALIAS skl1 IN 0
		ENDIF 
	ENDIF 
	UPDATE px SET refcena=skl1.rezervace from skl1 WHERE px.c_karty=skl1.c_karty AND px.c_stredisk=skl1.c_stredisk AND px.c_stredisk=m.hladajcstredisk
ENDSCAN 
DO pozavirej WITH "skl1"
IF !m.bolsklad
	DO pozavirej WITH "sklad"
ENDIF 
SELECT px
DELETE FOR refcena=0
REPLACE pomer WITH (-1+cena/refcena)*100 FOR refcena<>0
INDEX on druh+partner+nazev+DTOS(datum_dokl) TAG aaa
m.txt="PE"
DO pr_report WITH "refceny"
DO pozavirej WITH "px"

FUNCTION lng_oper_pohyb
PARAMETERS operace, typ_pohybu
LOCAL m.ret, oldsel
m.oldsel=SELECT()
IF !USED("rtypypohybov")
	IF m.issql
		USE (m.sqlprefix+"rtypypohybov") ALIAS rtypypohybov IN 0
	ELSE 
		USE (m.gchom_path+"typypohybov") ALIAS typypohybov IN 0
		USE vtypypohybov ALIAS rtypypohybov IN 0
	ENDIF 
ENDIF 
SELECT rtypypohybov
LOCATE FOR typ_zakladny=m.operace AND id_typ=m.typ_pohybu
IF FOUND()
	m.ret=rtypypohybov.meno
ELSE
	m.ret=m.operace+ALLTRIM(STR(m.typ_pohybu,10,0))
ENDIF 
SELECT (m.oldsel)
RETURN m.ret


PROCEDURE s_sum_spotr_kariet
IF !USED("pom")
	RETURN 
ENDIF 
SELECT c_stredisk, operace, c_karty, nazev, SUM(mnozstvi) as mnozstvi, ;
	SUM(MTON(mnozstvi)*MTON(cena)*en__meny(c_mena,m.girep_mena)) AS prachy, SUM(MTON(mnozstvi)*MTON(cena_part)*en__meny(c_mena,m.girep_mena)) AS prachy_par, SUM(MTON(mnozstvi)*MTON(cen_pardph)*en__meny(c_mena,m.girep_mena)) AS prachysdph, ;
	c_partner, partner, LEFT(DTOS(datum_dokl),6) as datum, m.girep_mena AS c_mena, ;
	druh, spart, stredisko, cena1, kod, stredskup, cen_hlad, SPACE(4) as rok, SPACE(2) as mesiac, SPACE(30) as txtmes, SPACE(30) as lngoper, .f. as lbl ;
	GROUP BY operace, druh, c_karty, datum, c_partner, cen_hlad FROM pom INTO CURSOR pom22 READWRITE WHERE nazev<>m.nechci AND partner<>m.nechci AND operace<>"PR"
REPLACE ALL rok WITH LEFT(datum,4), mesiac WITH RIGHT(datum,2), lngoper WITH lng_oper(operace)
REPLACE ALL txtmes WITH vrat_nazev_mesice(mesiac)
IF RECCOUNT("pom22")>0
	SELECT DISTINCT druh, .t. as lbl FROM pom22 INTO CURSOR seldruhyxx READWRITE 
	DO FORM seldruhy WITH "seldruhyxx","druh",200 TO m.druhx
	UPDATE pom22 SET lbl=.t. WHERE druh in (select druh FROM seldruhyxx WHERE lbl)
	DO pozavirej WITH "seldruhyxx"
	SELECT pom22
	LOCATE FOR lbl
	IF FOUND()
		SELECT DISTINCT nazev as druh, .t. as lbl FROM pom22 INTO CURSOR seldruhyxx READWRITE  WHERE lbl
		DO FORM seldruhy WITH "seldruhyxx","druh",200 TO m.druhx
		UPDATE pom22 SET lbl=.f. WHERE !(nazev in (select druh FROM seldruhyxx WHERE lbl))
	ENDIF 
	DO pozavirej WITH "seldruhyxx"
	SELECT pom22
	SET FILTER TO lbl
ENDIF 

IF "1"$dily
	SELECT pom22
	INDEX ON operace+druh+nazev+partner+rok+mesiac+cen_hlad TAG xxx1
	DO pr_report WITH "obr_sum_karta_partner"
ENDIF 

IF "2"$dily
	SELECT pom22
	INDEX ON operace+druh+nazev+rok+mesiac+partner+cen_hlad TAG xxx2
	DO pr_report WITH "obr_sum_karta_rok_partner"
ENDIF 

IF "3"$dily
	SELECT c_stredisk, operace, c_karty, nazev, SUM(mnozstvi) as mnozstvi, ;
		SUM(prachy) AS prachy, SUM(prachy_par) AS prachy_par, SUM(prachysdph) AS prachysdph, ;
		"" AS  partner, datum, c_mena, ;
		druh, spart, stredisko, cena1, kod, stredskup, cen_hlad, rok, mesiac, txtmes, lngoper, lbl ;
		GROUP BY operace, druh, c_karty, datum, cen_hlad FROM pom22 INTO CURSOR pom222 READWRITE WHERE lbl
	INDEX ON operace+druh+nazev+partner+rok+mesiac+cen_hlad TAG xxx
	SELECT pom222
	DO pr_report WITH "obr_sum_karta_partner"
	DO pozavirej WITH "pom222"
ENDIF 
