* prepocet skladu z obratu za vsechna strediska najednou
PARAMETERS mincdokl, otvorenypohlad, fnk, fno, fnd, fns, ppid
PRIVATE prikaz, vysledok, pokracuj, m.hladajcstredisk, m.menobuffera, m.operace, m.c_dokl, m.c_karty
PRIVATE m.d_dokl, m.c_mena, m.c_mena1, m.ukonci, m.od_dokladu, m.vysledok, m.boladb, m.prikaz, m.c_stredisk, m.poceet, m.zustatek
PRIVATE  m.hladajcdokl, m.zmena, m.cena_prum
had = CREATEOBJECT("had", jaz("prg_prepocet_vsech_skladu") + IIF(m.mincdokl > 0, "  od " + nx(m.mincdokl), ""))
had.SHOW()
pokracuj=.t.
prog_chain = progchain()
seznam_programu = m.prog_chain 

#DEFINE POCET_DOKL_HAD 444

small_log("CRLF--zacatek-- " + STR(m.mincdokl,9) + "  " + m.prog_chain, PROG())
IF m.issql
	DO open_database
	IF !EMPTY(m.mincdokl) AND !USED("skl_i")
		menobuffera=LOWER(SUBSTR(SYS(2015),4))
		IF !EMPTY(m.otvorenypohlad)
			IF !USED(otvorenypohlad)
				otvorenypohlad = ""
			ENDIF 
		ENDIF 
		hladajcstredisk=m.gic_stredisk
		vysledok=sql_exec("SELECT food600.gen_stav(" +nx(0) + "," + nx(m.mincdokl) + "," + nx(m.giakt_mena) + ",'" + m.menobuffera + "'," + nx(m.cdata) + ")") 
		IF m.vysledok>0
			DO pozavirej WITH "skl_ix"
			rv("genstav_kontrola","skl_ix")
			SELECT skl_ix.*, $0 as suma, $0 as skladsuma, $0 as skladzust, $0 as skladcena, 1 as skladmena, $0 as min_zaso FROM skl_ix INTO CURSOR skl_i READWRITE 
			INDEX ON STR(c_stredisk,12)+STR(c_karty, 12) TAG sklstrcd
			vysledok=sql_exec("SELECT food600.gen_stav_maz('"+m.menobuffera+"',"+ ALLTRIM(STR(m.cdata,2,0))+")")    &&&,IIF(EMPTY(otvorenypohlad),"teraz",m.otvorenypohlad))
		ENDIF 
	ENDIF 

	prikaz="UPDATE food600.sklad SET c_data=food_dat.c_data from food600.food_dat where sklad.c_stredisk=food_dat.c_stredisk and sklad.c_data<>food_dat.c_data"
	vysledok=sql_exec(m.prikaz)
	SELECT 0
	USE (m.sqlprefix+"rprepocetsklad") ALIAS sklad
*	INDEX ON STR(c_stredisk,12)+STR(c_karty, 12) TO (m.gcpri_path + "asklstrcd")
	CURSORSETPROP("Buffering", 5, "sklad")
	IF !USED("skl_i")
		SELECT *, "   " AS spart FROM sklad  INTO CURSOR skl_i WHERE  c_karty<>0 READWRITE
		REPLACE ALL zustatek WITH 0, cena_prum WITH 0
	ENDIF
	SELECT skl_i
	INDEX ON STR(c_stredisk,12)+STR(c_karty, 12) TAG sklstrcd
	PRIVATE c_mena, c_mena1, d_dokl, ukonci, maxcdokl, odomykaj, novacena, zmena, minimum, bolsklad, oldfilt, menobuf, m.boladb, cenx
	boladb=.F.
	n_slock("VELKY PREPOCET_" + m.machine)
	SET ESCAPE ON

	prikaz="ALTER TABLE food600.obraty DISABLE TRIGGER obraty_a_prepocet_pred"
    vysledok=sql_exec(m.prikaz)

	IF m.vysledok>0
		prikaz="ALTER TABLE food600.obraty DISABLE TRIGGER obraty_prepocet_po"
        vysledok=sql_exec(m.prikaz)
		IF m.vysledok<=0
			prikaz="ALTER TABLE food600.obraty ENABLE TRIGGER obraty_a_prepocet_pred"
            vysledok=sql_exec(m.prikaz)
			pokracuj = .f.
		ELSE 
			prikaz="TRUNCATE TABLE food600.prikazy_prepocet"
            vysledok=sql_exec(m.prikaz)
		ENDIF 
	ELSE 
		pokracuj = .f.
	ENDIF 
*** na zaciatku musim vypnut trigger na obratoch - treba spravit kontrolu, aby pocas vypnuteho triggera sa nedali robit skladove pohyby
ELSE
	IF EMPTY(m.fnk)
		fnk=m.fn_karty
	ENDIF
	IF EMPTY(m.fno)
		fno=m.fn_obra_s
	ENDIF
	IF EMPTY(m.fnd)
		fnd=m.fn_dokl_s
	ENDIF
	IF EMPTY(m.fns)
		fns=m.fn_sklad
	ENDIF
	IF EMPTY(m.ppid)
		ppid=m.gcpgm_id
	ENDIF
	PRIVATE c_mena, c_mena1, d_dokl, ukonci, maxcdokl, odomykaj, novacena, zmena, minimum, bolsklad, oldfilt, menobuf, m.boladb, cenx
	boladb=.F.
	n_slock("VELKY PREPOCET_" + m.machine)
	SET ESCAPE ON

	IF !USED("sklad")
		USE (m.fns)   ALIAS sklad   IN 0
		bolsklad=.F.
		oldfilt=""
	ELSE
		bolsklad=.T.
		SELECT sklad
		oldfilt=SET("FILTER")
		SET FILTER TO
	ENDIF
	IF !USED("food_dat")
		USE (m.fn_food_dat)   ALIAS food_dat   IN 0
	ENDIF
	IF !USED("doklady")
		USE (m.fnd) ALIAS doklady IN 0
	ENDIF
	IF !USED("obraty")
		USE (m.fno)  ALIAS obraty  IN 0 ORDER idriadok
	ELSE
		SET ORDER TO idriadok IN obraty
	ENDIF
	IF !USED("karty")
		USE (m.fnk)   ALIAS karty   IN 0 ORDER c_karty
	ENDIF
	*** M.B. 13.6.14 - Odmazanie kariet, ktore sa v sklad.dbf nachadzaju viackrat ***
	SELECT c_stredisk, c_karty, COUNT(c_karty) AS pocet, MAX(min_zaso) AS min_zaso, MAX(max_zaso) AS max_zaso, MAX(vycisleni) AS vycisleni FROM sklad GROUP BY c_stredisk, c_karty INTO CURSOR asklad READWRITE HAVING pocet>1
	IF RECCOUNT("asklad")>0
		DELETE FROM sklad WHERE sklad.c_stredisk=asklad.c_stredisk AND sklad.c_karty=asklad.c_karty AND asklad.pocet>1
		INSERT INTO sklad (c_stredisk, c_karty, min_zaso, max_zaso, vycisleni) SELECT c_stredisk, c_karty, min_zaso, max_zaso, vycisleni FROM asklad
	ENDIF
	USE IN asklad

	*** M.B. Koniec ***
	***  kontrola, zda vice ruznych dokladu ma stejne c_dokl ***    zacatek
	SELECT DISTI c_stredisk, c_dokl, operace FROM obraty INTO CURSOR dokl111 READWRITE
	DELETE FOR operace = "PP"
	SELECT c_dokl, CNT(*) AS pocet GROUP BY 1 FROM dokl111 INTO CURSOR dokl222 READWRITE
	seznam_opakovanych = ""
	SCAN FOR pocet > 1
		seznam_opakovanych = seznam_opakovanych + ALLTRIM(STR(c_dokl, 12)) + " "
	ENDSCAN
	IF .NOT. EMPTY(m.seznam_opakovanych )
		DO app_log WITH "VICE DOKLADU MA STEJNE C_DOKL !!  " + m.seznam_opakovanych
		MESSAGEBOX(jaz("form_volajte_helpdesk") + CHR(13) + m.seznam_opakovanych, 1+48, "FoodMan")
	ENDIF
	***  kontrola, zda vice ruznych dokladu ma stejne c_dokl ***    konec
	had.set_had( 1 )
	IF !USED("skl_i")
		SELECT *, "   " AS spart FROM sklad  INTO CURSOR skl_i WHERE  c_karty<>0 READWRITE
		INDEX ON STR(c_stredisk,12)+STR(c_karty, 12) TAG sklstrcd
		REPLACE ALL zustatek WITH 0, cena_prum WITH 0
	ELSE
		SET ORDER TO sklstrcd IN skl_i
	ENDIF
	minimum=-1.1111
	SET ORDER TO c_dokl IN doklady

	SELECT c_dokl, poradi, operace, CNT(*) AS pocet FROM obraty WHERE operace = "VE" GROUP BY 1,2 INTO CURSOR duplicity_poradi
	SCAN FOR pocet > 1
		SCATTER FIELDS c_dokl MEMVAR
		SELECT obraty
		REPLACE FOR c_dokl = m.c_dokl poradi WITH RECNO() % 10000
	ENDSCAN

	SELECT obraty
	REPLACE FOR EMPTY(poradi) poradi WITH RECNO() % 10000

	DELETE FOR mnozstvi = 0 .AND. !INLIST(operace, "PI", "PR") .AND. c_karty<>0

	had.set_had( 2 )
	SELECT c_stredisk, c_dokl, c_karty, ;
		CNT(STR(c_stredisk) + STR(c_dokl) + STR(c_karty)) AS pocet, SUM(zustatek) AS zustatek ;
		WHERE operace = "PI" GROUP BY 1,2,3 HAVING pocet > 1 ;
		FROM obraty INTO CURSOR vickrat READWRITE
	IF RECCOUNT() > 0
		SCAN
			SCATTER MEMVAR
			SELECT obraty
			SET FILTER TO c_stredisk = m.c_stredisk .AND. c_dokl = m.c_dokl .AND. c_karty = m.c_karty
			GO TOP
			REPLACE zustatek WITH m.zustatek
			SKIP
			DELETE NEXT (m.pocet)
			SELECT vickrat
		ENDSCAN
	ENDIF
	USE IN vickrat
	SELECT obraty
	SET ORDER TO idriadok
	SET FILTER TO
ENDIF

IF !m.pokracuj
	MESSAGEBOX(jaz("form_neodtavene_triggre_nepokr"),16,"FoodMan")
	RELEASE had
	RETURN 
ENDIF 

od_dokladu = m.mincdokl
DO open_database
SELECT 0
c_dokl=0
ukonci=.F.
IF m.issql
	USE (m.sqlprefix+"rprepall") ALIAS vprepall
	RECCOUNT("vprepall")
ELSE
	USE vprepall
ENDIF
CURSORSETPROP("Buffering",5,"vprepall")
DO WHILE !ukonci
	d_dokl=m.od_dokladu-1
	c_mena=m.giakt_mena
	c_mena1=m.c_mena
	IF m.issql
		SELECT 0
		USE (m.sqlprefix+"rprepocetzoznamdokladovzobratov") ALIAS prepdokl
	ELSE
		SELECT DISTINCT c_dokl, c_stredisk FROM obraty WHERE LEFT(operace, 1) $ "VP" .AND. c_dokl >= m.od_dokladu ORDER BY c_dokl INTO CURSOR prepdokl
	ENDIF 
	had.set_max(RECCOUNT("prepdokl") / POCET_DOKL_HAD * 1.3)
	IF RECCOUNT("prepdokl") = 0
		ukonci=.T.
		c_dokl=999999999999
	ELSE
		pocitadlo_had = 0
		SCAN ALL
			SCATTER MEMVAR
			hladajcdokl = m.c_dokl
			IF m.issql
				SELECT 0
				USE (m.sqlprefix+"r1doklady") ALIAS dokl1
			ELSE 
				SELECT pz2, c_mena, operace FROM doklady WHERE c_dokl = m.c_dokl ORDER BY c_dokl INTO CURSOR dokl1
			ENDIF 
			IF RECCOUNT()=0
				c_mena = IIF(m.slovenskaverzia, 2, 1)
				operace="PE"
				pz2=""
			ELSE
				GO TOP
				SCATTER MEMVAR
				IF EMPTY(m.c_mena) OR ISNULL(m.c_mena)
					c_mena = IIF( m.slovenskaverzia, 2, 1)
				ENDIF
			ENDIF
			USE IN dokl1
			zmena=.F.
			d_dokl=m.c_dokl
			c_mena1=m.c_mena
			IF m.operace="VV" .OR. m.operace="PV"
				c_dokl2=VAL(m.pz2)
			ELSE
				c_dokl2=0
			ENDIF

			pocitadlo_had = pocitadlo_had + 1
			IF m.pocitadlo_had > POCET_DOKL_HAD
				had.INCREMENT( "doklad: " + ALLTRIM(STR(c_dokl)) )
				pocitadlo_had = 0
			ENDIF

			IF m.operace="PP"
			     operace="VP"
			ENDIF

			SELECT vprepall
			REQUERY("vprepall")
			=RECCOUNT("vprepall")
			SCAN FOR c_karty<>0
				SCATTER MEMVAR
				obr_recno = RECNO()
				SELECT skl_i
				IF SEEK(STR(m.c_stredisk,12)+STR(m.c_karty,12))
					SCATTER FIELD zustatek, cena_prum, c_mena MEMVAR
					IF EMPTY(m.c_mena) OR ISNULL(m.c_mena)
						c_mena = IIF( m.slovenskaverzia, 2, 1)
					ENDIF
				ELSE
					STORE 0 TO zustatek, cena_prum
					c_mena=m.giakt_mena
				ENDIF
				SELECT vprepall
				DO CASE
					CASE m.operace = "PI"        && inventura
						IF vprepall.mnozstvi>0 .AND. vprepall.cena_prum>0 .AND. m.cena_prum=0
							IF vprepall.mnozstvi<>vprepall.zustatek - m.zustatek
								REPLACE vprepall.mnozstvi WITH vprepall.zustatek - m.zustatek
								zmena=.T.
							ENDIF
							IF m.c_mena=m.c_mena1
								IF vprepall.cena_prum<>vprepall.cena
									REPLACE vprepall.cena WITH vprepall.cena_prum
									zmena=.T.
								ENDIF
							ELSE
								cenx=ROUND(MTON(vprepall.cena_prum)*en__meny(m.c_mena,m.c_mena1),4)
								IF cena_prum<>m.cenx
									REPLACE vprepall.cena_prum WITH m.cenx
									REPLACE vprepall.cena WITH vprepall.cena_prum
									zmena=.T.
								ENDIF
							ENDIF
						ELSE
							IF vprepall.mnozstvi<>vprepall.zustatek - m.zustatek
								REPLACE vprepall.mnozstvi WITH vprepall.zustatek - m.zustatek
								zmena=.T.
							ENDIF
							m.novacena=ROUND(MTON(m.cena_prum)*en__meny(m.c_mena,m.c_mena1),4)
							IF vprepall.cena<>m.novacena
								REPLACE vprepall.cena WITH m.novacena
								zmena=.T.
							ENDIF
							IF vprepall.cena_prum<>m.novacena
								REPLACE vprepall.cena_prum WITH m.novacena
								zmena=.T.
							ENDIF
						ENDIF
					CASE  (m.operace = "VP" AND mnozstvi>0) OR operace="PP"             && prijem vyrobou polotovaru
						SELECT 0
						c_dokl2=m.c_dokl
						poradi=m.c_karty
						IF m.issql
							USE (m.sqlprefix+"rprep2dokl") ALIAS vprep2dokl
						ELSE
							USE vprep2dokl
						ENDIF
						SUM MTON(mnozstvi) * MTON(cena) TO prachy_pp FOR operace="VP" .AND. mnozstvi<0
						USE
						SELECT vprepall
						IF vprepall.zustatek<>m.zustatek+vprepall.mnozstvi
							REPLACE vprepall.zustatek WITH m.zustatek + vprepall.mnozstvi
							zmena=.T.
						ENDIF
						novacena = -ROUND( MTON(m.prachy_pp) / MTON(vprepall.mnozstvi), 4 )
						IF vprepall.cena<>m.novacena
							REPLACE vprepall.cena WITH m.novacena
							zmena=.T.
						ENDIF
						IF vprepall.zustatek # 0
							IF m.zustatek >= 0 .OR. m.c_dokl < m.prepoc_ooo
								novacena = ROUND((MTON(m.zustatek) * MTON(m.cena_prum)*en__meny(m.c_mena,m.c_mena1) + MTON(vprepall.mnozstvi) * MTON(vprepall.cena)) / MTON(vprepall.zustatek), 4)
							ELSE
								novacena=vprepall.cena
							ENDIF
							IF vprepall.cena_prum<>m.novacena
								REPLACE vprepall.cena_prum WITH m.novacena
								zmena=.T.
							ENDIF
						ENDIF
					CASE m.operace = "VV"        && vydej interni    ( opravi se i cena na prislusne prijemce )
						novacena=ROUND(MTON(m.cena_prum)*en__meny(m.c_mena,m.c_mena1),4)
						IF vprepall.zustatek<>(m.zustatek+vprepall.mnozstvi)
							REPLACE vprepall.zustatek WITH m.zustatek + vprepall.mnozstvi
							zmena=.T.
						ENDIF
						IF vprepall.cena<>m.novacena
							REPLACE vprepall.cena WITH m.novacena
							zmena=.T.
						ENDIF
						IF vprepall.cena_prum<>m.novacena
							REPLACE vprepall.cena_prum WITH m.novacena
							zmena=.T.
						ENDIF
						SCATTER FIELD cena MEMVAR
						prachy_vv = - MTON(mnozstvi) * MTON(cena)
						IF m.c_dokl2>0
							SELECT 0
							IF m.issql
								USE (m.sqlprefix+"rprep2dokl") ALIAS vprep2dokl
							ELSE
								USE vprep2dokl
							ENDIF
							IF vprep2dokl.operace="PV"
								=CURSORSETPROP("Buffering",5,"vprep2dokl")
								SUM MTON(mnozstvi) * MTON(cena) TO prachy_pp ALL
								IF m.prachy_pp # 0 AND ABS(m.prachy_pp)>0.0001
									REPLACE vprep2dokl.cena WITH ROUND(MTON(vprep2dokl.cena) * MTON(m.prachy_vv) / MTON(m.prachy_pp), 4) FOR vprep2dokl.cena<>ROUND(vprep2dokl.cena * m.prachy_vv / m.prachy_pp, 4)
								ELSE
									REPLACE vprep2dokl.cena WITH ROUND(MTON(m.prachy_vv) / MTON(vprep2dokl.mnozstvi), 4) FOR vprep2dokl.mnozstvi<>0 .AND. vprep2dokl.cena<>ROUND(m.prachy_vv / vprep2dokl.mnozstvi, 4)
									SUM MTON(vprep2dokl.mnozstvi) * MTON(vprep2dokl.cena) TO prachy_pp ALL
									IF m.prachy_pp # 0 AND ABS(m.prachy_pp)>0.0001
										REPLACE vprep2dokl.cena WITH ROUND(MTON(vprep2dokl.cena) * MTON(m.prachy_vv) / MTON(m.prachy_pp), 4) FOR vprep2dokl.cena<>ROUND(vprep2dokl.cena * m.prachy_vv / m.prachy_pp, 4)
									ENDIF
								ENDIF
								IF GETNEXTMODIFIED(0,"vprep2dokl")<>0
									tux(.T.,.T.,"vprep2dokl")
								ELSE
									TABLEREVERT(.T.,"vprep2dokl")
								ENDIF
							ENDIF
							USE IN vprep2dokl
						ENDIF
						SELECT vprepall

					CASE INLIST(m.operace, "VB", "PB") .AND. vprepall.mnozstvi < 0        && bourani - vydej
						novacena=MTON(m.cena_prum)*en__meny(m.c_mena,m.c_mena1)
						IF vprepall.zustatek<>(m.zustatek+vprepall.mnozstvi)
							REPLACE vprepall.zustatek WITH m.zustatek + vprepall.mnozstvi
							zmena=.T.
						ENDIF
						IF vprepall.cena<>m.novacena
							REPLACE vprepall.cena WITH m.novacena
							zmena=.T.
						ENDIF
						IF vprepall.cena_prum<>m.novacena
							REPLACE vprepall.cena_prum WITH m.novacena
							zmena=.T.
						ENDIF
						prachy_vv = - mnozstvi * cena
						SELECT 0
						c_dokl2=m.c_dokl
						IF m.issql
							USE (m.sqlprefix+"rprep2dokl") ALIAS vprep2dokl
						ELSE
							USE vprep2dokl
						ENDIF
						CURSORSETPROP("Buffering",5,"vprep2dokl")
						SUM MTON(mnozstvi) * MTON(cena) TO prachy_pp FOR mnozstvi > 0
						IF m.prachy_pp # 0 AND ABS(m.prachy_pp)>0.0001
							REPLACE FOR  mnozstvi > 0 .AND. vprep2dokl.cena<>ROUND(MTON(vprep2dokl.cena) * MTON(m.prachy_vv) / MTON(m.prachy_pp), 4);
								vprep2dokl.cena WITH ROUND(MTON(vprep2dokl.cena) * MTON(m.prachy_vv) / MTON(m.prachy_pp), 4)
						ELSE
							REPLACE FOR mnozstvi > 0 .AND. vprep2dokl.cena<>ROUND(MTON(m.prachy_vv) / MTON(mnozstvi), 4);
								vprep2dokl.cena WITH ROUND(MTON(m.prachy_vv) / MTON(mnozstvi), 4)
							SUM MTON(vprep2dokl.mnozstvi) * MTON(vprep2dokl.cena) TO prachy_pp FOR mnozstvi > 0
							IF m.prachy_pp # 0 AND ABS(m.prachy_pp)>0.0001
								REPLACE FOR vprep2dokl.mnozstvi > 0 .AND. vprep2dokl.cena<>ROUND(MTON(vprep2dokl.cena) * MTON(m.prachy_vv) / MTON(m.prachy_pp), 4);
									vprep2dokl.cena WITH ROUND(MTON(vprep2dokl.cena) * MTON(m.prachy_vv) / MTON(m.prachy_pp), 4)
							ENDIF
						ENDIF
						IF GETNEXTMODIFIED(0,"vprep2dokl")<>0
							tux(.T.,.T.,"vprep2dokl")
						ELSE
							TABLEREVERT(.T.,"vprep2dokl")
						ENDIF
						USE IN vprep2dokl
						SELECT vprepall
						LOCAL riadokxx
						m.riadokxx=RECNO()
						IF m.issql
							USE (m.sqlprefix+"rprep2dokl") ALIAS vprep2dokl IN 0
						ELSE
							USE vprep2dokl IN 0
						ENDIF
						UPDATE vprepall SET cena=vprep2dokl.cena FROM vprep2dokl WHERE vprepall.mnozstvi>0 AND vprepall.poradi=vprep2dokl.poradi AND vprepall.c_karty=vprep2dokl.c_karty AND vprepall.mnozstvi=vprep2dokl.mnozstvi AND vprepall.cena<>vprep2dokl.cena
						USE IN vprep2dokl
						SELECT vprepall
						GOTO m.riadokxx
						RELEASE m.riadokxx

					CASE INLIST(m.operace, "VC", "PC")        && preceneni
						DO CASE
							CASE vprepall.mnozstvi < 0
								novacena=MTON(m.cena_prum)*en__meny(m.c_mena,m.c_mena1)
								IF vprepall.mnozstvi<>-m.zustatek
									REPLACE vprepall.mnozstvi WITH - m.zustatek
									zmena=.T.
								ENDIF
								IF vprepall.cena<>m.novacena
									REPLACE vprepall.cena WITH m.novacena
									zmena=.T.
								ENDIF
								IF vprepall.cena_prum<>m.novacena
									REPLACE vprepall.cena_prum WITH m.novacena
									zmena=.T.
								ENDIF
								IF vprepall.zustatek<>0
									REPLACE vprepall.zustatek WITH 0
									zmena=.T.
								ENDIF
								IF m.zmena
									m.vysledok=tux(.T.,.T.,"vprepall")
									zmena=.F.
								ELSE
									=TABLEREVERT(.T.,"vprepall")
								ENDIF
								SELECT 0
								c_dokl2=m.c_dokl
								IF m.issql
									USE (m.sqlprefix+"rprep2dokl") ALIAS vprep2dokl
								ELSE
									USE vprep2dokl
								ENDIF
								CURSORSETPROP("Buffering",5,"vprep2dokl")
								REPLACE vprep2dokl.mnozstvi WITH m.zustatek,vprep2dokl.zustatek WITH m.zustatek FOR vprep2dokl.mnozstvi > 0 .AND. vprep2dokl.mnozstvi<>m.zustatek
								IF GETNEXTMODIFIED(0,"vprep2dokl")<>0
									tux(.T.,.T.,"vprep2dokl")
								ELSE
									TABLEREVERT(.T.,"vprep2dokl")
								ENDIF
								USE IN vprep2dokl
								SELECT vprepall
								REQUERY("vprepall")
								LOCATE FOR poradi=m.poradi .AND. mnozstvi<0

							CASE vprepall.mnozstvi>0
								IF vprepall.zustatek<>vprepall.mnozstvi
									REPLACE vprepall.zustatek WITH vprepall.mnozstvi
									zmena=.T.
								ENDIF
								IF vprepall.cena_prum<>vprepall.cena
									REPLACE vprepall.cena_prum WITH vprepall.cena
									zmena=.T.
								ENDIF
								IF m.zmena
									tux(.T.,.T.,"vprepall")
									zmena=.F.
								ELSE
									=TABLEREVERT(.T.,"vprepall")
								ENDIF
								SELECT vprepall
								REQUERY("vprepall")
								LOCATE FOR poradi=m.poradi .AND. mnozstvi>0
							OTHERWISE
									SELECT 0
									c_dokl2=m.c_dokl
									novacena=MTON(m.cena_prum)*en__meny(m.c_mena,m.c_mena1)
									IF m.issql
										USE (m.sqlprefix+"rprep3dokl") ALIAS vprep2dokl
									ELSE
										USE vprep3dokl ALIAS vprep1dokl
									ENDIF
									CURSORSETPROP("Buffering",5,"vprep2dokl")
									GO TOP 
									IF vprep2dokl.mnozstvi<>-m.zustatek
										REPLACE vprep2dokl.mnozstvi WITH -m.zustatek,vprep2dokl.zustatek WITH 0, cena WITH m.novacena, cena_prum WITH m.novacena
									ENDIF 
									GO BOTTOM 
									IF vprep2dokl.mnozstvi<>m.zustatek
										REPLACE vprep2dokl.mnozstvi WITH m.zustatek,vprep2dokl.zustatek WITH m.mnozstvi 
									ENDIF 
									IF GETNEXTMODIFIED(0,"vprep2dokl")<>0
										tux(.T.,.T.,"vprep2dokl")
									ELSE
										TABLEREVERT(.T.,"vprep2dokl")
									ENDIF
									USE IN vprep2dokl
									SELECT vprepall
									REQUERY("vprepall")
									LOCATE FOR poradi=m.poradi .AND. mnozstvi<0
						ENDCASE

					CASE INLIST(m.operace, "PR")        && archivacia
						IF m.c_mena=m.c_mena1
							IF vprepall.cena_prum<>vprepall.cena
								REPLACE vprepall.cena_prum WITH vprepall.cena
								zmena=.T.
							ENDIF
						ELSE
							IF m.cena_prum=0
								c_mena=m.c_mena1
								IF vprepall.cena_prum<>vprepall.cena
									REPLACE vprepall.cena_prum WITH vprepall.cena
									zmena=.T.
								ENDIF
							ELSE
								REPLACE vprepall.cena WITH MTON(vprepall.cena)*en__meny(m.c_mena,m.c_mena1)
								REPLACE vprepall.cena_prum WITH vprepall.cena
								zmena=.T.
							ENDIF
						ENDIF
						IF vprepall.zustatek<>m.zustatek+vprepall.mnozstvi
							REPLACE vprepall.zustatek  WITH m.zustatek + vprepall.mnozstvi
							zmena=.T.
						ENDIF
						SCATTER MEMVAR FIELDS cena_prum, zustatek
					CASE LEFT( m.operace, 1) = "P"  .OR. (INLIST(operace,"VE","VR") .AND. mnozstvi>0)       && vsechny ostatni prijmy
						IF vprepall.zustatek<>(m.zustatek+vprepall.mnozstvi)
							REPLACE vprepall.zustatek WITH m.zustatek + vprepall.mnozstvi
							zmena=.T.
						ENDIF
						IF vprepall.zustatek # 0
							IF m.zustatek >= 0 .OR. m.c_dokl < m.prepoc_ooo
								novacena=ROUND((MTON(m.zustatek) * MTON(m.cena_prum)*en__meny(m.c_mena,m.c_mena1) + MTON(vprepall.mnozstvi) * MTON(vprepall.cena)) / MTON(vprepall.zustatek), 4)
							ELSE
								novacena=vprepall.cena
							ENDIF
							IF vprepall.cena_prum<>m.novacena
								REPLACE vprepall.cena_prum WITH m.novacena
								zmena=.T.
							ENDIF
						ENDIF
					CASE LEFT( m.operace, 1) = "V"        && vsechny ostatni vydeje ( i prip. kladne mnozstvi (ze storen) se bere za prum.cenu )
						novacena=MTON(m.cena_prum)*en__meny(m.c_mena,m.c_mena1)
						IF vprepall.zustatek<>(m.zustatek+vprepall.mnozstvi)
							REPLACE vprepall.zustatek WITH m.zustatek + vprepall.mnozstvi
							zmena=.T.
						ENDIF
						IF vprepall.cena<>m.novacena
							REPLACE vprepall.cena WITH m.novacena
							zmena=.T.
						ENDIF
						IF vprepall.cena_prum<>m.novacena
							REPLACE vprepall.cena_prum WITH m.novacena
							zmena=.T.
						ENDIF
				ENDCASE

				SELECT vprepall
				SCATTER FIELD zustatek, cena_prum MEMVAR


				SELECT skl_i
				IF SEEK(STR(m.c_stredisk,12)+STR(m.c_karty,12))
					GATHER FIELD zustatek, cena_prum MEMVAR
					REPLACE c_mena WITH m.c_mena1
				ELSE
					INSERT INTO skl_i ( c_stredisk, c_karty, zustatek, cena_prum, c_mena, min_zaso) ;
						VALUE (m.c_stredisk, m.c_karty, m.zustatek, m.cena_prum, m.c_mena1, -1.1111)
				ENDIF

				SELECT vprepall
			ENDSCAN
			IF m.zmena
				tux(.T.,.T.,"vprepall")
			ELSE
				TABLEREVERT(.T.,"vprepall")
			ENDIF
			SELECT prepdokl
		ENDSCAN
	ENDIF
    had.set_had( 95 )
	had.set_txt2 ("doscanovano jeste jednou")
    small_log("doscanovano jeste jednou", PROG())
	USE IN prepdokl
	IF m.issql
		SELECT 0
		USE (m.sqlprefix+"rprepocetmaxcdokl") ALIAS maxcdokl
	ELSE 
		SELECT MAX(c_dokl) AS c_dokl FROM obraty INTO CURSOR maxcdokl WHERE INLIST(LEFT(operace,1),"V","P")
	ENDIF 
	IF maxcdokl.c_dokl > m.c_dokl
		USE IN maxcdokl
		ukonci=.F.
		od_dokladu=m.c_dokl+2
	ELSE
		USE IN maxcdokl
		IF !m.issql
			SELECT DISTINCT c_stredisk FROM skl_i INTO CURSOR cstredy
			SELECT cstredy.c_stredisk, .F. AS locknute, food_dat.ID AS id_man ;
				FROM cstredy LEFT JOIN food_dat ON cstredy.c_stredisk=food_dat.c_stredisk INTO CURSOR idmany READWRITE
			USE IN cstredy
			SELECT idmany
			INDEX ON id_man FOR id_man <> m.ppid TAG id_man UNIQUE
			odomykaj=.F.
			SCAN
				IF n_slock_ex("BUF_OUT_" + idmany.id_man)
					SELECT idmany
					REPLACE locknute WITH .T.
				ELSE
					WAIT jaz("form_nezamknuty_buff_out") + idmany.id_man WINDOW TIMEOUT 3
					odomykaj=.T.
					EXIT
				ENDIF
			ENDSCAN
			IF m.odomykaj
				SELECT idmany
				SCAN FOR locknute
					n_sunlock("BUF_OUT_" + idmany.id_man)
				ENDSCAN
				ukonci=.F.
				od_dokladu=m.c_dokl+2
			ELSE
				ukonci=.T.
			ENDIF
		ELSE
			ukonci=.t.
		ENDIF 
	ENDIF
ENDDO
had.set_txt2 ("enddo")
small_log("enddo", PROG())
USE IN vprepall

IF m.ukonci
	SELECT skl_i
	DELETE FOR zustatek = 0 .AND. min_zaso = -1.1111
	REPLACE FOR min_zaso = -1.1111 min_zaso WITH 0
	IF !m.issql
		SELECT obraty
		SET RELATION OFF INTO doklady
		SELECT doklady
		REPLACE FOR operace = "PI" platba WITH "PREP"
	ENDIF 
	SELECT sklad
*!*		SET RELATION TO STR(c_stredisk,12)+STR(c_karty,12) INTO skl_i
*!*		REPLACE cena_prum WITH skl_i.cena_prum FOR cena_prum<>skl_i.cena_prum
*!*		REPLACE zustatek WITH skl_i.zustatek FOR zustatek<>skl_i.zustatek
*!*		REPLACE c_mena WITH skl_i.c_mena FOR c_mena<>skl_i.c_mena
*!*		DELETE FOR EMPTY(skl_i.c_karty)
*!*		SET RELATION OFF INTO skl_i
*!*		SELECT sklad
*!*		SET FILTER TO
*!*		IF !m.issql
*!*			INDEX ON STR(c_stredisk,12)+STR(c_karty, 12) TO (m.gcpri_path + "asklstrcd")
*!*		ENDIF 
*!*		SELECT skl_i
*!*		SET RELATION TO STR(c_stredisk,12)+STR(c_karty,12) INTO sklad
*!*		COPY TO (m.gcpri_path+"novinky") FOR EMPTY(sklad.c_karty)
*!*		SELECT 0
*!*		USE (m.gcpri_path+"novinky") EXCLUSIVE
*!*		DELETE FOR c_karty=0
*!*		IF !m.issql
*!*			SELECT karty
*!*			SET ORDER TO c_karty
*!*			SELECT novinky
*!*			SET RELATION TO c_karty INTO karty
*!*			DELETE FOR zustatek=0 AND karty.nazev1=m.smazana_karta
*!*			SET RELATION OFF INTO karty
*!*		ENDIF 
*!*		PACK
*!*		IF RECCOUNT()>0
*!*			USE
*!*			SELECT sklad
*!*			APPEND FROM (m.gcpri_path+"novinky")
*!*			IF m.issql
*!*				SELECT sklad
*!*				REPLACE c_data WITH m.cdata FOR EMPTY(c_data)
*!*				=tux(.t.,.t.,"sklad")
*!*				=REQUERY("sklad")
*!*			ENDIF 
*!*		ENDIF
*!*		DO pozavirej WITH "novinky"
*!*		ERASE (m.gcpri_path+"novinky.dbf")

	had.set_txt2 ("sklad UPDATE")
	small_log("sklad UPDATE", PROG())
	UPDATE sklad SET cena_prum=skl_i.cena_prum from skl_i WHERE sklad.c_stredisk=skl_i.c_stredisk AND sklad.c_karty=skl_i.c_karty AND sklad.cena_prum<>skl_i.cena_prum
	UPDATE sklad SET zustatek=skl_i.zustatek from skl_i WHERE sklad.c_stredisk=skl_i.c_stredisk AND sklad.c_karty=skl_i.c_karty AND sklad.zustatek<>skl_i.zustatek
	UPDATE sklad SET c_mena=skl_i.c_mena from skl_i WHERE sklad.c_stredisk=skl_i.c_stredisk AND sklad.c_karty=skl_i.c_karty AND sklad.c_mena<>skl_i.c_mena
	SELECT disti c_stredisk FROM sklad INTO CURSOR sdewertre
	SCAN ALL 
		hladajcstredisk = sdewertre.c_stredisk
        had.set_txt2 ("sklad insert " + STR(m.hladajcstredisk))
		DELETE FROM sklad WHERE c_stredisk=m.hladajcstredisk AND !(c_karty in (select distinct c_karty FROM skl_i WHERE c_stredisk = m.hladajcstredisk))
		IF m.issql
            INSERT INTO sklad (c_stredisk, c_karty, c_mena, c_data, zustatek, cena_prum) SELECT c_stredisk, c_karty, c_mena, m.cdata, zustatek, cena_prum FROM skl_i WHERE c_stredisk=m.hladajcstredisk AND !(c_karty in (select distinct c_karty FROM sklad WHERE c_stredisk=m.hladajcstredisk AND c_data = m.cdata)) AND zustatek<>0
		ELSE
			INSERT INTO sklad (c_stredisk, c_karty, c_mena, zustatek, cena_prum) SELECT skl_i.c_stredisk, skl_i.c_karty, skl_i.c_mena, skl_i.zustatek, skl_i.cena_prum FROM skl_i inner join karty on skl_i.c_karty=karty.c_karty WHERE skl_i.c_stredisk=m.hladajcstredisk AND !(skl_i.c_karty in (select distinct c_karty FROM sklad WHERE c_stredisk=m.hladajcstredisk)) AND zustatek<>0 AND karty.nazev1<>m.smazana_karta
		ENDIF 
	ENDSCAN 
	USE IN sdewertre
    had.set_txt2 ("sklad doinsertovan ")
	IF m.issql
		had.set_txt2 ("sklad update")
		tux(.t.,.t.,"sklad", "requery")
		had.set_txt2 ("sklad konec prepoctu")
        small_log("sklad konec prepoctu tuxed", PROG())
		*** dokoncenie prepoctu v sklade
		prikaz="SELECT food600.koniecprepoctu("+IIF(m.slovenskaverzia,"0","0")+","+ALLTRIM(STR(m.cdata))+")"
*        vysledok=SQLEXEC(CURSORGETPROP('ConnectHandle', "sklad"), m.prikaz, "sqlresult")
		vysledok=sql_exec(m.prikaz)
 
        small_log("sklad konec prepoctu" + STR(m.vysledok), PROG())
	ELSE 
		SELECT sklad
		SET RELATION TO c_karty INTO karty
		*		REPLACE FOR sklad.zustatek = 0 .AND. sklad.cena_prum=0 sklad.cena_prum WITH karty.cena_prum,c_mena WITH karty.c_mena_n
		DELETE FOR sklad.zustatek=0 AND karty.nazev1=m.smazana_karta
		SET RELATION OFF INTO karty

		IF !m.slovenskaverzia
			*   zmeni ve sklad.dbf prumernou cenu u receptur s nulovym zustatkem
			SELECT DISTI c_karty FROM (m.fn_polozky) INTO CURSOR receptury READWRITE
			INDEX ON c_karty TAG c_karty
			SELECT sklad
			SET RELATION TO c_karty INTO karty, c_karty INTO receptury
			SET FILTER TO .NOT. EMPTY(receptury.c_karty) .AND. zustatek = 0
			REPLACE ALL cena_prum WITH karty.cena_prum
		ENDIF

		SELECT idmany
		SCAN FOR locknute .AND. idmany.id_man<>m.ppid
			DO n_sunlock WITH "BUF_OUT_"+idmany.id_man
		ENDSCAN
		DO n_sunlock WITH "OPRAVA"
		USE IN idmany
		IF !("ZALOHOVANIE" $ m.seznam_programu)
			SELECT obraty.c_stredisk, obraty.c_karty, obraty.c_dokl, obraty.operace, karty.nazev, obraty.zustatek, obraty.mnozstvi, obraty.cena ORDER BY 1,2,3 ;
				WHERE LEFT(operace, 1) IN ("P", "V") AND zustatek < 0 AND mnozstvi < 0;
				FROM obraty LEFT JOIN karty ON karty.c_karty == obraty.c_karty INTO CURSOR zapory
			IF RECCOUNT() > 0
				had.HIDE()
				ACTIVATE SCREEN
				SET RELATION TO c_dokl INTO doklady
				DO pr_report WITH "zapory"
			ENDIF
			USE IN zapory
		ENDIF
		* * *  tohle otestuje, odkdy je mozno pocitat prum. ceny podle noveho algoritmu  * * *  ZACATEK
		IF m.prepoc_ooo > -999999999
			had.SHOW()
			had.set_txt2 ("test na novy algoritmus cen")
			SELECT obraty
			SET ORDER TO c_dokl
			SET FILTER TO zustatek < 0
			IF prazdna()    && nejsou zadne zaporne zustatky, prejde se na novy algoritmus
				UPDATE (m.gchom_path + "food_set") SET var_value = "-999999999" WHERE var_name = "prepoc_ooo"
			ELSE
				IF m.prepoc_ooo > 2111000111    && po instalaci je tam MAXINT, tak se najde posledni zapor a napriste bude od nej poitano novou metodou
					GO BOTTOM
					max_pod_nulou = ALLTRIM(STR(obraty.c_dokl, 12))
					UPDATE (m.gchom_path + "food_set") SET var_value = max_pod_nulou WHERE var_name = "prepoc_ooo"

				ELSE    && uz bylo nekdy nastaveno prepoc_ooo, ale pokud vsechny puvodni zapory jsou zarchivovany, prejde se na novy algoritmus
					SET FILTER TO zustatek < 0 .AND. c_dokl < m.prepoc_ooo
					IF prazdna()
						UPDATE (m.gchom_path + "food_set") SET var_value = "-999999999" WHERE var_name = "prepoc_ooo"
					ENDIF
				ENDIF
			ENDIF
		ENDIF
		* * *  tohle otestuje, odkdy je mozno pocitat prum. ceny podle noveho algoritmu  * * *  KONEC
	ENDIF 
ENDIF
IF m.issql
    small_log("ENABLE TRIG", PROG())
	SELECT 0
	USE (m.sqlprefix+"rkontrolatriggerovobraty") ALIAS kkxx
	RECCOUNT()
	LOCATE FOR ALLTRIM(LOWER(tgenabled))="d"
	IF FOUND()
		prikaz="ALTER TABLE food600.obraty ENABLE TRIGGER obraty_a_prepocet_pred"
        vysledok1=sql_exec(m.prikaz)

		prikaz="ALTER TABLE food600.obraty ENABLE TRIGGER obraty_prepocet_po"
        vysledok2=sql_exec(m.prikaz)
        small_log("ENABLE TRIG" + str(m.vysledok1) + str(vysledok2), PROG())
	ENDIF 
	DO pozavirej WITH "kkxx, sklad"
ELSE 
	IF m.bolsklad
		SELECT sklad
		SET FILTER TO &oldfilt
	ELSE
		DO pozavirej WITH "sklad"
	ENDIF
ENDIF 

n_sunlock("VELKY PREPOCET_" + m.machine)
small_log("--konec-- " + had.trvalo(), PROG())
DO pozavirej WITH "skl_i"
ACTIVATE SCREEN

IF ATC("INVENT", m.seznam_programu) + ATC("HLEDEJ", m.seznam_programu) + ATC("STOR_ODP", m.seznam_programu) + ATC("STOR_POL", m.seznam_programu) + ATC("BUFF_OUT2", m.seznam_programu) + ATC("ZALOHOVANIE", m.seznam_programu) = 0

    IF m.issql .and. (VAL(had.trvalo()) > 9 .or. ATC("evzen", m.machine) > 0)
        sql_select("select c_stredisk, count(*) from food600.obraty group by c_stredisk order by count desc ", "sqlresult", "c_stredisk I, pocet N(9)") 
        SELECT sqlresult
        message_txt = ""
        SUM pocet TO celkovy_pocet
        SCAN FOR  pocet > 33333
            message_txt = m.message_txt + STR(c_stredisk, 6) + TRANSFORM(99 * pocet / m.celkovy_pocet, ":999% pohybù") + CHR(13)
        ENDSCAN 
        IF .not. EMPTY(m.message_txt)
            had.hide()
            MESSAGEBOX(m.message_txt, 1+64, "tato strediska by chtela archivovat:", 999999)
        ENDIF 
    ENDIF 

    clos_data()
ENDIF

RELEASE had
