PARAMETERS c_stredisk, c_karty, max_c_dokl
PRIVATE old_sel, old_order, old_rec, hlasit,oldsklor,useddoklad,c_mena,d_dokl,c_mena1,old_dord
IF PARAMETERS() < 3
	max_c_dokl = 999999999
ENDIF
m.label = "@"
hlasit = .F.
old_sel = SELE()
SELECT sklad
SET FILTER TO
*** M.B. ***
*** Zindexovanie skladu a nahradenie LOCATE SEEKom***
oldsklor = SET ("order")
INDEX ON STR(c_stredisk,12)+STR(c_karty,12) TO (m.gcpri_path+"sklstrck")
=SEEK(STR(m.c_stredisk,12)+STR(m.c_karty,12))
IF FOUND()
	m.cena_prum=sklad.cena_prum
	m.c_mena=sklad.c_mena
ELSE
	m.cena_prum=0
	m.c_mena=m.giakt_mena
ENDIF 
m.d_dokl=0	
m.c_mena1=m.c_mena
*LOCATE FOR c_stredisk = m.c_stredisk .and. c_karty = m.c_karty
*** M.B. ***
IF USED("doklady")
	SELECT doklady
	SET FILTER TO
	m.old_dord = SET ("order")
	SET ORDER TO c_dokl
	m.useddoklad=.T.
ELSE
	SELECT 0
	USE (m.gcdat_path+"doklady") ORDER c_dokl
	m.useddoklad=.F.
ENDIF
*en__meny(sklad.c_mena,m.giakt_mena)
LOCATE FOR ALLTRIM(UPPER(pz1))="KONIEC FOOD500"
IF FOUND()
	m.od_dokladu = c_dokl+1
ELSE
	m.od_dokladu = 0
ENDIF
zustatek = 0
SELECT obraty
SET RELATION TO c_dokl INTO doklady
old_order = SET ("order")
old_errh = ON("ERROR")
ON ERROR INDEX ON STR(c_dokl, 12) + STR(poradi, 12) + STR(SIGN(mnozstvi)+3, 1) TO (m.gcpri_path + "dok_por_mn")
SET ORDER TO dok_por_mn
ON ERROR &old_errh


SCAN FOR c_stredisk = m.c_stredisk .AND. c_karty = m.c_karty .AND. c_dokl < m.max_c_dokl .AND. LEFT(obraty.operace, 1) $ "VP"
	DO CASE 
		CASE EMPTY(m.d_dokl)
			m.d_dokl=doklady.c_dokl
			m.c_mena1=doklady.c_mena
		CASE m.d_dokl<>doklady.c_dokl
			m.c_mena=m.c_mena1
			m.c_mena1=doklady.c_mena
			m.d_dokl=doklady.c_dokl
		CASE m.d_dokl=doklady.c_dokl
			m.c_mena=m.c_mena1
	ENDCASE 
	IF c_dokl<m.od_dokladu
		IF LOWER(operace) = "pi"
			REPLACE mnozstvi WITH zustatek - m.zustatek
		ENDIF
		IF mnozstvi <= 0 .OR. (LOWER(operace) = "pi" .AND. m.zustatek > 0)	&& vydej vzdy za prum.cenu
			REPLACE cena WITH NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena))
			m.cena_prum=NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena))
		ELSE
			IF (mnozstvi + m.zustatek <= 0) .OR. (m.zustatek <= 0)
				m.cena_prum = cena
			ELSE
				m.cena_prum = (m.zustatek * NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena)) + mnozstvi * cena) / (mnozstvi + m.zustatek)
			ENDIF
			IF m.cena_prum > 999999
*DO app_log WITH "PREPOCET:"+m.nazev + ": cena overflow"
				WAIT "PREPOCET:"+ALLTRIM(STR(m.c_karty )) + ": cena overflow" WINDOWS TIMEOUT 2
				cena_prum = 999999.9999
			ENDIF
		ENDIF
		zustatek = m.zustatek + mnozstvi
		GATHER FIELD zustatek, cena_prum MEMVAR
	ELSE
		DO CASE
			CASE operace = "PI"                    && inventura vzdy za prum.cenu
				REPLACE mnozstvi WITH zustatek - m.zustatek, cena WITH NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena)), cena_prum WITH NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena))
				SCATTER FIELDS zustatek, cena_prum MEMVAR
			CASE operace = "PR"
				REPLACE cena_prum WITH cena
				SCATTER FIELDS zustatek, cena_prum MEMVAR
			OTHERWISE
				IF (mnozstvi <= 0)                 && vydej vzdy za prum.cenu
					REPLACE cena WITH NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena))
					m.cena_prum=cena
				ELSE
					IF (mnozstvi + m.zustatek <= 0) .OR. (m.zustatek <= 0)
						cena_prum = cena
					ELSE
						cena_prum = (m.zustatek * NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena)) + mnozstvi * cena) / (mnozstvi + m.zustatek)
					ENDIF
					IF m.cena_prum > 99999999
						DO app_log WITH jaz("prg_app_log_prepocet") + STR(m.c_karty) + jaz("prg_app_log_cena_overflow")
						cena_prum = 99999999.9999
					ENDIF
				ENDIF
				zustatek = m.zustatek + mnozstvi
				GATHER FIELD zustatek, cena_prum MEMVAR
		ENDCASE
	ENDIF
	IF m.zustatek < -0.01
		hlasit = .T.
	ENDIF
ENDSCAN

IF m.hlasit
	= MESSAGEBOX(jaz("prg_problem_s_kartou") + STR(m.c_karty) + jaz("prg_volajte_servis") , 0+64+256, "", 3333)
	DO app_log WITH jaz("prg_app_log_prepocet") + STR(m.c_karty) + ", c_str " + STR(obraty.c_stredisk) + ", c_dokl " + STR(obraty.c_dokl) + jaz("prg_app_log_zustatek")+" " + STR(m.zustatek, 9,4)
ENDIF

SELECT sklad
*** M.B. ***
*** Nahradenie LOCATE SEEKom***
IF SEEK(STR(m.c_stredisk,12)+STR(m.c_karty,12))
*LOCATE FOR c_stredisk = m.c_stredisk .and. c_karty = m.c_karty
*IF FOUND()
*** M.B. ***
	GATHER FIELD zustatek, cena_prum MEMVAR
	REPLACE c_mena WITH m.c_mena1
ELSE
	IF m.zustatek # 0
		INSERT INTO (m.fn_sklad) (c_stredisk, c_karty, zustatek, cena_prum, c_mena) ;
			VALUES (m.c_stredisk, m.c_karty, m.zustatek, m.cena_prum, m.c_mena1)
	ENDIF
ENDIF
SELECT sklad
SET ORDER TO &oldsklor
SELECT obraty
SET RELATION OFF INTO doklady
SET ORDER TO &old_order
IF !m.useddoklad
	USE IN doklady
ELSE
	SELECT doklady
	SET ORDER to &old_dord
ENDIF

SELECT (m.old_sel)
