PARAMETERS subor,cdoklod, cdokldo,doskladu, rob_hada, menobuf,mmenaakt, c_str, cd, m.gcdp
PRIVATE old_sel, old_order, old_rec, hlasit,oldsklor,useddoklad,c_mena,d_dokl,c_mena1,old_dord, m.label, m.od_dokladu, old_errh, cena_prum, zustatek
PRIVATE boladb, c_stredisk, c_karty, novacena, l_prip, l_vysledok, l_ret, m.nulmena, m.doklmena
IF EMPTY(m.mmenaakt)
	m.mmenaakt=m.giakt_mena
ENDIF 
IF EMPTY(m.c_str)
	m.c_str=m.gic_stredisk
ENDIF 
IF EMPTY(m.cd)
	IF m.issql
		m.cd=m.cdata
	ELSE
		m.cd=1
	ENDIF 
ENDIF 
IF EMPTY(m.gcdp)
	m.gcdp=m.gcdat_path
ENDIF 
IF m.slovenskaverzia
	m.nulmena=2
ELSE
	m.nulmena=1
ENDIF
m.boladb=.f.
old_sel = SELECT()
IF m.issql
	SELECT buffer
	=tux(.t.,.t.,"buffer")
	DO WHILE !CURSORGETPROP("FetchIsComplete","buffer") 
		WAIT jaz("form_ukladam_zmeny") WINDOW TIMEOUT 0.2 
	ENDDO
	m.vysledok=sql_exec("SELECT food600.prep1po"+m.sqlsuffix+"('"+m.menobuf+"',"+ALLTRIM(STR(m.cd,1,0))+","+ALLTRIM(STR(m.mmenaakt,2,0))+","+ALLTRIM(STR(cdokldo,15,0))+","+ALLTRIM(STR(m.c_str,15,0))+")")
	SELECT (m.old_sel)
	RETURN m.vysledok
ELSE 
	m.label = "@"
	hlasit = .F.
	IF m.rob_hada
		hadx = CREATEOBJECT("had", jaz("prg_prepocet_kariet_dokladu"))
		hadx.SHOW()
	ENDIF 
    jmeno_indexu = (m.gcpri_path + "sklstrck" + SYS(2015))
	IF m.doskladu
		SELECT sklad
		SET FILTER TO
		*** 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 ***
		oldsklor = SET ("order")
		SELECT sklad
		INDEX ON STR(c_stredisk,12)+STR(c_karty,12) TO (m.jmeno_indexu)
	ENDIF 
	IF USED("doklady")
		SELECT doklady
		m.old_dord = SET ("order")
		SET ORDER TO c_dokl
		m.useddoklad=.T.
	ELSE
		SELECT 0
		USE (m.gcdp+"doklady") ORDER c_dokl
		m.useddoklad=.F.
	ENDIF
	=RECCOUNT("doklady")
	LOCATE FOR ALLTRIM(UPPER(pz1))="KONIEC FOOD500"
	IF FOUND()
		m.od_dokladu = c_dokl+1
	ELSE
		m.od_dokladu = 0
	ENDIF
	SELECT obraty
	old_order = SET ("order")
	DO open_database
	m.c_stredisk=0
	m.c_karty=-999
	USE  vprepocet2 IN 0 ALIAS vprepocet
	=CURSORSETPROP("Buffering",5,"vprepocet")

	SELECT (subor)
	SET ORDER TO csck
	IF m.rob_hada
		hadx.set_max(RECCOUNT())
	ENDIF
	SCAN FOR c_karty<>0
		IF m.cdoklod=0
			SCATTER MEMVAR FIELDS c_stredisk, c_karty
			m.cena_prum=0
			m.c_mena=m.mmenaakt
			m.d_dokl=0	
			m.c_mena1=m.c_mena
			zustatek = 0
		ELSE
			SCATTER MEMVAR FIELDS c_stredisk, c_karty, zustatek, cena_prum, c_mena
			m.d_dokl=m.cdoklod
			m.c_mena1=m.c_mena
		ENDIF 		
		IF m.rob_hada
			hadx.INCREMENT(ALLTRIM(STR(m.c_karty,12,0)))
		ENDIF 
		SELECT vprepocet
		REQUERY("vprepocet")
		SET RELATION TO c_dokl INTO doklady
		SCAN ALL 
			IF EMPTY(doklady.c_dokl) OR ISNULL(doklady.c_dokl)
				m.doklmena=m.nulmena
			ELSE
				m.doklmena=doklady.c_mena
			ENDIF 
			DO CASE 
				CASE EMPTY(m.d_dokl)
					m.d_dokl=vprepocet.c_dokl
					m.c_mena1=m.doklmena
				CASE m.d_dokl<>vprepocet.c_dokl
					m.c_mena=m.c_mena1
					m.c_mena1=m.doklmena					
					m.d_dokl=vprepocet.c_dokl
				CASE m.d_dokl=vprepocet.c_dokl
					m.c_mena=m.c_mena1
			ENDCASE 
			IF c_dokl<m.od_dokladu
				IF LOWER(operace) = "pi"
					IF vprepocet.mnozstvi<>vprepocet.zustatek - m.zustatek
						REPLACE vprepocet.mnozstvi WITH vprepocet.zustatek - m.zustatek
					ENDIF 
				ENDIF
				IF vprepocet.mnozstvi <= 0 .OR. (LOWER(vprepocet.operace) = "pi" .AND. m.zustatek > 0)	&& vydej vzdy za prum.cenu
					m.cena_prum=ROUND(MTON(m.cena_prum)*en__meny(m.c_mena,m.doklmena),4)
					IF vprepocet.cena<> m.cena_prum
						REPLACE vprepocet.cena WITH m.cena_prum
					ENDIF 
				ELSE
					IF (vprepocet.mnozstvi + m.zustatek <= 0) .OR. (m.zustatek <= 0)
						m.cena_prum = vprepocet.cena
					ELSE
						m.cena_prum = ROUND((MTON(m.zustatek) * MTON(m.cena_prum)*en__meny(m.c_mena,m.doklmena) + MTON(mnozstvi) * MTON(cena)) / MTON(mnozstvi + m.zustatek),4)
					ENDIF
					IF m.cena_prum > 999999
						WAIT "PREPOCET:"+ALLTRIM(STR(m.c_karty )) + ": cena overflow" WINDOWS TIMEOUT 2
						m.cena_prum = 999999.9999
					ENDIF
				ENDIF
				m.zustatek = m.zustatek + vprepocet.mnozstvi
				IF vprepocet.cena_prum<>m.cena_prum
					REPLACE vprepocet.cena_prum WITH m.cena_prum
				ENDIF 
				IF vprepocet.zustatek<>m.zustatek 
					REPLACE vprepocet.zustatek WITH m.zustatek
				ENDIF 
			ELSE
				DO CASE
					CASE vprepocet.operace = "PI"                    && inventura vzdy za prum.cenu
						IF vprepocet.mnozstvi<>(vprepocet.zustatek-m.zustatek)
							REPLACE vprepocet.mnozstvi WITH vprepocet.zustatek - m.zustatek
						ENDIF 
						m.novacena=ROUND(MTON(m.cena_prum)*en__meny(m.c_mena,m.doklmena),4)
						IF vprepocet.cena<>m.novacena
							REPLACE vprepocet.cena WITH m.novacena
						ENDIF 
						IF vprepocet.cena_prum<>m.novacena
							REPLACE vprepocet.cena_prum WITH m.novacena
						ENDIF 
						SCATTER FIELDS zustatek, cena_prum MEMVAR
					CASE vprepocet.operace = "PR"
						IF vprepocet.cena_prum<>vprepocet.cena
							REPLACE vprepocet.cena_prum WITH vprepocet.cena
						ENDIF 
						SCATTER FIELDS zustatek, cena_prum MEMVAR
					OTHERWISE
						IF (vprepocet.mnozstvi <= 0)                 && vydej vzdy za prum.cenu
							m.novacena=ROUND(MTON(m.cena_prum)*en__meny(m.c_mena,m.doklmena),4)
							IF vprepocet.cena<>m.novacena
								REPLACE vprepocet.cena WITH m.novacena
							ENDIF 
							m.cena_prum=m.novacena
						ELSE
							IF (vprepocet.mnozstvi + m.zustatek <= 0) .OR. (m.zustatek <= 0)
								m.cena_prum = vprepocet.cena
							ELSE
								m.cena_prum = ROUND((MTON(m.zustatek) * MTON(m.cena_prum)*en__meny(m.c_mena,m.doklmena) + MTON(vprepocet.mnozstvi) * MTON(vprepocet.cena)) / MTON(vprepocet.mnozstvi + m.zustatek),4)
							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")
								m.cena_prum = 99999999.9999
							ENDIF
						ENDIF
						m.zustatek = m.zustatek + mnozstvi
						IF vprepocet.zustatek<>m.zustatek
							REPLACE vprepocet.zustatek WITH m.zustatek
						ENDIF 
						IF vprepocet.cena_prum<>m.cena_prum
							REPLACE vprepocet.cena_prum WITH m.cena_prum
						ENDIF 
				ENDCASE
			ENDIF
			IF m.zustatek < -0.01
				hlasit = .T.
			ENDIF
		ENDSCAN

		IF m.doskladu
			SELECT sklad
			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
				IF m.zustatek # 0
					LOCATE FOR c_stredisk=m.c_stredisk AND c_karty=m.c_karty
					IF FOUND()
						GATHER FIELD zustatek, cena_prum MEMVAR
						REPLACE c_mena WITH m.c_mena1
					ELSE 
						INSERT INTO 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 
			ENDIF 
			SELECT (subor)
		ELSE
			SELECT (subor)
			GATHER FIELD zustatek, cena_prum MEMVAR
			REPLACE c_mena WITH m.c_mena1
		ENDIF 
		=tux(.t.,.t.,"vprepocet")
	ENDSCAN 
	USE IN vprepocet
	IF m.doskladu
		SELECT sklad
		SET ORDER TO &oldsklor
	ENDIF 
	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
	IF m.rob_hada
		RELEASE hadx
	ENDIF 
	RELEASE old_order, old_rec, hlasit,oldsklor,useddoklad,c_mena,d_dokl,c_mena1,old_dord, m.label, m.od_dokladu, old_errh, cena_prum, zustatek
    SELECT sklad
    SET ORDER TO
    SET INDEX TO
    ERASE (m.jmeno_indexu) + ".idx"
ENDIF 
SELECT (m.old_sel)
