PARAMETERS rok, mesiac
PRIVATE mesiac,rok, useduzaverka, pokracovat,oldsel,lmincdokl,lmaxcdokl,hladajcstredisk,hladajcdokl
IF PARAMETERS()=0
	m.rok=""
	m.mesiac=""
ENDIF
m.oldsel=SELECT()
DO pozavirej WITH 'uzaverka,food_dat'
IF m.issql

	rv('fooddat','food_dat')
*!*		sql_select('SELECT * FROM food600.food_dat','food_dat' )
	rv('uzaverka','uzaverka')
	m.pokracovat=.T.
	m.useduzaverka=.F.
ELSE
	USE (m.gchom_path + "food_dat") ALIAS food_dat IN 0
	USE (m.gcdat_path + 'uzaverka') ALIAS uzaverka AGAIN IN 0
	m.pokracovat=.T.
	m.useduzaverka=.F.
ENDIF

IF m.pokracovat
	DO pozavirej WITH 'interne,stred,vysledok,pohyby,asdf'
	CREATE CURSOR pohyby (cesta c(40),c_stredisk i,c_doklod i,c_dokldo i)
	CREATE CURSOR  stred(LABEL c(1), id_zkratka c(25), pgm c(8), dat_path c(40),c_stredisk i)
	SELECT stred
	INSERT INTO stred (id_zkratka,pgm,dat_path,c_stredisk) ;
		SELECT id_zkratka,pgm,dat_path,c_stredisk FROM food_dat WHERE LOWER(food_dat.pgm) = "man"
	SELECT stred
	INDEX ON c_stredisk TAG c_stredisk
	SET ORDER TO c_stredisk IN stred
	IF EMPTY(m.rok) .OR. EMPTY(m.mesiac)
		SELECT DISTINCT rok+" | "+mesiac AS mesiac,rok, mesiac AS mes FROM uzaverka INTO CURSOR asdf ORDER BY rok DESC,mes DESC
		DO FORM dajdruh WITH "","asdf","mesiac",100 TO m.mmx
		IF EMPTY(m.mmx)
			DO pozavirej WITH "interne,pohyby, stred, asdf, uzaverka, food_dat"
			SELECT (m.oldsel)
			RETURN
		ELSE
			m.rok=LEFT(m.mmx,4)
			m.mesiac=RIGHT(m.mmx,2)
		ENDIF
	ENDIF

	SELECT uzaverka
	SCAN FOR rok=m.rok .AND. mesiac=m.mesiac
		INSERT INTO pohyby(c_stredisk,c_doklod,c_dokldo) VALUES(uzaverka.c_stredisk,uzaverka.c_doklod,uzaverka.c_dokldo)
	ENDSCAN
	IF !useduzaverka
		USE IN uzaverka
	ENDIF
	SELECT pohyby
	SET RELATION TO c_stredisk INTO stred
	REPLACE ALL cesta WITH stred.dat_path IN pohyby
	SET RELATION OFF INTO stred
ENDIF

*!*	IF .T. && FILE(m.gcpri_path + 'pohyby.dbf')

SELECT pohyby
GO TOP
SCAN ALL
	SCATTER MEMVAR FIELDS c_stredisk,c_doklod,c_dokldo
	IF m.issql
		m.lmincdokl = m.c_doklod
		m.lmaxcdokl = m.c_dokldo
		m.hladajcstredisk = m.c_stredisk

		lsql="SELECT "
		lsql=lsql+"MAX(Obraty.c_stredisk) AS c_stredisk, "
		lsql=lsql+"SUM(Obraty.mnozstvi*Obraty.cena) AS suma, "
		lsql=lsql+"Obraty.c_dokl AS pz2 "
		lsql=lsql+"FROM  food600.obraty Obraty "
		lsql=lsql+"WHERE Obraty.c_stredisk =  ?m.hladajcstredisk  AND "
		lsql=lsql+"Obraty.c_data =  ?m.cdata                      AND "
		lsql=lsql+"UPPER(Obraty.operace) IN ('PV','VV')           AND "
		lsql=lsql+"Obraty.c_dokl >=  ?m.lmincdokl                 AND "
		lsql=lsql+"Obraty.c_dokl <=  ?m.lmaxcdokl "
		lsql=lsql+"GROUP BY Obraty.c_dokl"
		sql_select(lsql,'dkobr','c_stredisk i,suma n(8,3),pz2 i')


		lsql="SELECT "
		lsql=lsql+"c_dokl, c_partner, "
		lsql=lsql+"doklad, "
		lsql=lsql+"c_mena "
		lsql=lsql+"FROM  food600.doklady  "
		lsql=lsql+"WHERE doklady.c_stredisk =  ?m.hladajcstredisk  AND "
		lsql=lsql+"doklady.c_data =  ?m.cdata                      AND "
		lsql=lsql+"doklady.c_dokl >=  ?m.lmincdokl                 AND "
		lsql=lsql+"doklady.c_dokl <=  ?m.lmaxcdokl "
		sql_select(lsql,'dkdokl','c_dokl i,c_partner i,doklad c(50),c_mena i')


		SELECT * FROM dkobr LEFT OUTER JOIN dkdokl ON dkobr.pz2 = dkdokl.c_dokl INTO CURSOR man1 READWRITE
		SELECT man1

		REPLACE ALL suma WITH ROUND(NTOM(MTON(suma)*en__meny(c_mena,m.giakt_mena)),2) IN man1
	ELSE && m.issql

		IF FILE(ALLTRIM(pohyby.cesta)+'obraty.dbf')
			SELECT 0
			USE (ALLTRIM(pohyby.cesta)+'obraty.dbf') ALIAS intobr AGAIN
			SELECT m.c_stredisk AS c_stredisk,SUM(intobr.mnozstvi*intobr.cena) AS suma,;
				intobr.c_dokl,intobr.operace,000000 AS c_partner,000000 AS doklad,;
				0 AS c_mena, c_dokl AS pz2 ;
				FROM intobr;
				WHERE INLIST(LOWER(intobr.operace),'pv','vv') .AND.;
				intobr.c_dokl>=m.c_doklod .AND. intobr.c_dokl<=m.c_dokldo .AND. ;
				c_stredisk=m.c_stredisk ;
				GROUP BY c_dokl INTO CURSOR man1 READWRITE

			USE IN intobr
			SELECT 0
			USE (ALLTRIM(pohyby.cesta)+'doklady') ORDER c_dokl ALIAS intdokl AGAIN
			SELECT man1
			SET RELATION TO c_dokl INTO intdokl
			REPLACE ALL man1.c_partner WITH intdokl.c_partner
			REPLACE ALL man1.doklad WITH intdokl.doklad
			REPLACE ALL man1.c_mena WITH intdokl.c_mena
			REPLACE ALL man1.pz2 WITH VAL(intdokl.pz2)
			REPLACE ALL suma WITH ROUND(NTOM(MTON(suma)*en__meny(c_mena,m.giakt_mena)),2)
			SET RELATION OFF INTO intdokl
			USE IN intdokl
		ENDIF && FILE(ALLTRIM(pohyby.cesta)+'obraty.dbf')
	ENDIF &&m.issql

	IF USED('interne')
		INSERT INTO interne SELECT * FROM man1
	ELSE
		IF USED("man1")
			SELECT * FROM man1 INTO CURSOR interne READWRITE
		ENDIF
	ENDIF
	SELECT pohyby

ENDSCAN
SELECT man1
USE IN man1
SELECT interne

SELECT c_dokl AS vydajka, c_dokl AS prijemka, c_partner, c_stredisk,pz2, suma AS vv,operace, suma AS PV, 0 AS pocetpv, SPACE(30) AS str_vyd, SPACE(30) AS str_prij, SPACE(200) AS riadky;
	FROM interne WHERE operace="VV" INTO CURSOR vysledok READWRITE

REPLACE ALL PV WITH 0, prijemka WITH 0
SELECT interne
SCAN FOR operace="PV"
	SELECT vysledok
	LOCATE FOR pz2=interne.c_dokl
	IF FOUND()
		REPLACE vysledok.PV WITH vysledok.PV+interne.suma, vysledok.pocetpv WITH vysledok.pocetpv+1, vysledok.prijemka WITH interne.c_dokl
	ELSE
		INSERT INTO vysledok (vydajka, prijemka      , c_partner         , c_stredisk, pz2, vv, PV, pocetpv) VALUES ;
			(0      , interne.c_dokl, interne.c_stredisk, 0,0,0,interne.suma,1)
	ENDIF
	SELECT interne
ENDSCAN
SELECT vysledok
REPLACE ALL PV WITH ABS(PV), vv WITH ABS(vv)
DELETE FOR PV=vv .AND. pocetpv=1
USE IN interne

SET RELATION TO c_stredisk INTO stred
REPLACE ALL str_vyd WITH stred.id_zkratka
SET RELATION OFF INTO stred
SET RELATION TO c_partner INTO stred
REPLACE ALL str_prij WITH stred.id_zkratka
SET RELATION OFF INTO stred
REPLACE str_vyd WITH STR(c_stredisk) FOR EMPTY(str_vyd)
REPLACE str_prij WITH STR(c_partner) FOR EMPTY(str_prij)
REPLACE riadky WITH jaz("form_vydane_v_inom_obdobi") FOR vv=0 .OR. c_stredisk=0
REPLACE riadky WITH jaz("form_prijate_v_inom_obdobi") FOR PV=0 .OR. c_partner=0
INDEX ON STR(c_stredisk)+STR(c_partner) TAG aaa
IF NOT m.issql
	USE (m.fn_obraty) ALIAS intobr AGAIN IN 0
ENDIF

SELECT vysledok

SCAN  FOR !EMPTY(vydajka) .AND. !EMPTY(prijemka)
	m.chyby=""
	SCATTER MEMVAR FIELDS vydajka, prijemka
	IF m.issql
		m.hladajcdokl =m.vydajka

		sql_select('SELECT MAX(poradi) as poradi, SUM(mnozstvi*cena) as suma_vv, 0 as suma_pv FROM food600.obraty WHERE c_dokl=?m.hladajcdokl GROUP BY poradi'               ,;
			'vydka','poradi i,suma_vv n(8,2),suma_pv n(8,2)')

		m.hladajcdokl =m.prijemka
		sql_select('SELECT MAX(poradi) as poradi, SUM(mnozstvi*cena) as suma_pv               FROM food600.obraty WHERE c_dokl=?m.hladajcdokl GROUP BY poradi',;
			'primka','poradi i,suma_pv n(8,2)')
	ELSE
		SELECT poradi, SUM(mnozstvi*cena) AS suma_vv, $0 AS suma_pv FROM intobr GROUP BY poradi WHERE c_dokl=m.vydajka  INTO CURSOR vydka  READWRITE
		SELECT poradi, SUM(mnozstvi*cena) AS suma_pv                FROM intobr GROUP BY poradi WHERE c_dokl=m.prijemka INTO CURSOR prijka READWRITE
	ENDIF
	SELECT prijka
	INDEX ON poradi TAG poradi
	SELECT vydka
	SET RELATION TO poradi INTO prijka
	REPLACE ALL suma_pv WITH prijka.suma_pv
	USE IN prijka && prejde
	SCAN FOR ABS(suma_vv)<>ABS(suma_pv)
		m.chyby=m.chyby+ALLTRIM(STR(vydka.poradi,5,0))+","
	ENDSCAN
	USE IN vydka
	SELECT vysledok
	IF !EMPTY(m.chyby)
		REPLACE riadky WITH jaz("form_chyby_na_riadkoch")+LEFT(m.chyby,LEN(m.chyby)-1)
	ENDIF
ENDSCAN
DO pozavirej WITH 'intobr'
DO pr_report WITH 'intpoh'
DO pozavirej WITH "stred,vysledok"
*!*	ELSE
*!*		WAIT 'Neprístupná uzaverka.DBF' WINDOW TIMEOUT 2
*!*	ENDIF
*DO clos_data
DO pozavirej WITH "interne,pohyby, stred, asdf, uzaverka, food_dat"
SELECT (m.oldsel)

RETURN
