* prepocet skladu z obratu za vsechna strediska najednou
PRIVATE c_mena,c_mena1,d_dokl, m.cenx

IF ! EMPTY(n_checkall("POHYBY_"))
	SELECT 0
	USE (m.gchom_path + "semafory")
	SET FILTER TO "POHYBY_" $ semafor
	seznam_stanic = ""
	SCAN 
		seznam_stanic = seznam_stanic + chr(13) + space(20) + semafor_id
	ENDSCAN 
	USE 
	= MESSAGEBOX(jaz("ted nelze, zkuste pozdeji") + ";" + jaz("pracuji stanice:") + m.seznam_stanic, 0+16+256, jaz("prg_prepocet"), 7777)
	RETURN 
ENDIF


IF n_slock("POHYBY_EXCL_")

	had = CREATEOBJECT("had", jaz("prg_prepocet_vsech_skladu"))
	had.SHOW()

	IF .NOT. (("INVENT" $ UPPER(PROGRAM(2) + PROGRAM(3) + PROGRAM(4) + PROGRAM(5) + PROGRAM(6) + PROGRAM(7))) .or. ;
		("HLEDEJ" $ UPPER(PROGRAM(2) + PROGRAM(3) + PROGRAM(4) + PROGRAM(5) + PROGRAM(6) + PROGRAM(7))))
		DO clos_data
		USE (m.fn_sklad)   ALIAS sklad   IN 0
		USE (m.fn_doklady) ALIAS doklady IN 0
		USE (m.fn_obraty)  ALIAS obraty  IN 0
		USE (m.fn_karty)   ALIAS karty   IN 0 ORDER c_karty
	ENDIF
	SELECT doklady
	SET ORDER TO c_dokl

    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")
*** 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
		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 ***
	SELECT sklad
	REPLACE ALL zustatek WITH 0, cena_prum WITH 0

	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 (m.fn_obraty) INTO TABLE (m.gcpri_path + "vickrat")

	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

	SELECT doklady
	LOCATE FOR ALLTRIM(UPPER(pz1))="KONIEC FOOD500"
	IF FOUND()
		m.od_dokladu = c_dokl+1
	ELSE
		m.od_dokladu = 0
	ENDIF

**** Stary prepocet skladu - podla Food500
	IF m.od_dokladu <> 0
		SELECT obraty
		SET FILTER TO
		SET ORDER TO c_dokl
		DO obr2f5 WITH m.od_dokladu
	ELSE
		SELECT obraty
		had.set_max(RECCOUNT())
	ENDIF
**** Koniec: Stary prepocet skladu - podla Food500

	SELECT obraty
	SET FILTER TO
	INDEX ON STR(c_dokl, 12) + STR(poradi, 12) + STR(SIGN(mnozstvi)+3, 1) TO (m.gcpri_path + "dok_por_mn")
	SET RELATION TO c_dokl INTO doklady
	m.d_dokl=0
	m.c_mena=m.giakt_mena
	m.c_mena1=m.c_mena


	SCAN FOR LEFT(obraty.operace, 1) $ "VP" .AND. c_dokl >= m.od_dokladu
		DO CASE 
			CASE EMPTY(m.d_dokl)
				m.d_dokl=doklady.c_dokl
				m.c_mena1=doklady.c_mena
				m.c_mena=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 

		had.INCREMENT("stredisko: " + ALLTRIM(STR(ABS(c_stredisk))) + ", doklad: " + operace + " " + ALLTRIM(STR(c_dokl)))
		SELECT obraty
		SCATTER MEMVAR
		IF m.operace="PP" AND m.mnozstvi<0
			m.operace="VP"
		ENDIF 
		obr_recno = RECNO()

		SELECT sklad
		SELECT * FROM sklad WHERE c_stredisk=m.c_stredisk AND c_karty=m.c_karty INTO CURSOR asklad
		DO CASE
			CASE RECCOUNT("asklad")>1
				DELETE FROM sklad  WHERE c_stredisk=m.c_stredisk AND c_karty=m.c_karty
				m.zustatek=0
				m.cena_prum=0
				m.c_mena=m.giakt_mena
			CASE RECCOUNT("asklad")=0
				m.zustatek=0
				m.cena_prum=0
				m.c_mena=m.giakt_mena
			OTHERWISE 
				SCATTER FIELD zustatek, cena_prum, c_mena MEMVAR
		ENDCASE 
		USE IN asklad

*!*			IF SEEK(STR(m.c_stredisk,12)+STR(m.c_karty,12))
*!*				SCATTER FIELD zustatek, cena_prum, c_mena MEMVAR
*!*			ELSE
*!*				STORE 0 TO zustatek, cena_prum
*!*			ENDIF

		SELECT obraty
		DO CASE

			CASE m.operace = "PI"        && inventura
				IF mnozstvi>0 .AND. cena_prum>0 .AND. m.cena_prum=0
					IF mnozstvi<>(obraty.zustatek - m.zustatek)
						REPLACE mnozstvi WITH obraty.zustatek - m.zustatek
					ENDIF 
					IF cena<>cena_prum
						REPLACE cena WITH cena_prum
					ENDIF 
				ELSE
					IF mnozstvi<>(obraty.zustatek - m.zustatek)
						REPLACE mnozstvi WITH obraty.zustatek - m.zustatek
					ENDIF 
					m.cenx=ROUND(NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena)),4)
					IF cena<>m.cenx
						REPLACE cena WITH m.cenx
					ENDIF 
					IF cena_prum<>m.cenx
						REPLACE cena_prum WITH m.cenx
					ENDIF 
				ENDIF
			CASE  m.operace = "PP"             && prijem vyrobou polotovaru
				SUM mnozstvi * cena TO prachy_pp FOR c_dokl = m.c_dokl .AND. poradi = m.poradi .AND. operace = "VP"
				GO (m.obr_recno)
				IF zustatek<>(m.zustatek + obraty.mnozstvi)
					REPLACE zustatek WITH m.zustatek + obraty.mnozstvi
				ENDIF 
				IF cena<>-ROUND( m.prachy_pp / obraty.mnozstvi, 4 )
				 	REPLACE cena WITH - ROUND( m.prachy_pp / obraty.mnozstvi, 4 )
				 ENDIF 
				IF obraty.zustatek # 0
					IF m.zustatek >= 0 .OR. m.c_dokl < m.prepoc_ooo
						m.cenx=ROUND((m.zustatek * NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena)) + obraty.mnozstvi * obraty.cena) / obraty.zustatek, 4)
						IF cena_prum<>m.cenx
							REPLACE cena_prum WITH m.cenx
						ENDIF 
					ELSE
						IF cena_prum<>obraty.cena
							REPLACE cena_prum WITH obraty.cena
						ENDIF 
					ENDIF
				ENDIF


			CASE m.operace = "VV"        && vydej interni    ( opravi se i cena na prislusne prijemce )
				REPLACE zustatek WITH m.zustatek + obraty.mnozstvi, 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 FIELD cena MEMVAR
				prachy_vv = - mnozstvi * cena
				SELECT doklady
				IF SEEK(m.c_dokl)
					c_dokl_prij = VAL(doklady.pz2)
					IF c_dokl_prij > 0
						SELECT obraty
						SUM mnozstvi * cena TO prachy_pp FOR c_dokl = m.c_dokl_prij .AND. poradi = m.poradi
						IF m.prachy_pp # 0
							REPLACE FOR c_dokl = m.c_dokl_prij .AND. poradi = m.poradi .and. cena<>ROUND(cena * m.prachy_vv / m.prachy_pp, 4) ;
								cena WITH ROUND(cena * m.prachy_vv / m.prachy_pp, 4)
						ELSE
							REPLACE FOR c_dokl = m.c_dokl_prij .AND. poradi = m.poradi AND cena<>ROUND(m.prachy_vv / mnozstvi, 4);
								cena WITH ROUND(m.prachy_vv / mnozstvi, 4)
							SUM mnozstvi * cena TO prachy_pp FOR c_dokl = m.c_dokl_prij .AND. poradi = m.poradi
							IF m.prachy_pp # 0
								REPLACE FOR c_dokl = m.c_dokl_prij .AND. poradi = m.poradi AND cena<>ROUND(cena * m.prachy_vv / m.prachy_pp, 4);
									cena WITH ROUND(cena * m.prachy_vv / m.prachy_pp, 4)
							ENDIF
						ENDIF
					ENDIF
				ENDIF


			CASE INLIST(m.operace, "VB", "PB") .AND. mnozstvi < 0        && bourani - vydej
				REPLACE zustatek WITH m.zustatek + obraty.mnozstvi, 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))
				prachy_vv = - mnozstvi * cena
				SUM mnozstvi * cena TO prachy_pp FOR c_dokl = m.c_dokl .AND. poradi = m.poradi .AND. mnozstvi > 0
				IF m.prachy_pp # 0
					REPLACE FOR c_dokl = m.c_dokl .AND. poradi = m.poradi .AND. mnozstvi > 0 AND cena<>ROUND(cena * m.prachy_vv / m.prachy_pp, 4);
						cena WITH ROUND(cena * m.prachy_vv / m.prachy_pp, 4)
				ELSE
					REPLACE FOR c_dokl = m.c_dokl .AND. poradi = m.poradi .AND. mnozstvi > 0 AND cena<>ROUND(m.prachy_vv / mnozstvi, 4);
						cena WITH ROUND(m.prachy_vv / mnozstvi, 4)
					SUM mnozstvi * cena TO prachy_pp FOR c_dokl = m.c_dokl .AND. poradi = m.poradi .AND. mnozstvi > 0
					IF m.prachy_pp # 0
						REPLACE FOR c_dokl = m.c_dokl .AND. poradi = m.poradi .AND. mnozstvi > 0 AND cena<>ROUND(cena * m.prachy_vv / m.prachy_pp, 4);
							cena WITH ROUND(cena * m.prachy_vv / m.prachy_pp, 4)
					ENDIF
				ENDIF


			CASE INLIST(m.operace, "VC", "PC")        && preceneni
				IF mnozstvi < 0
					REPLACE mnozstvi WITH - m.zustatek, cena WITH NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena)), zustatek WITH 0, cena_prum WITH NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena))
					REPLACE FOR c_dokl = m.c_dokl .AND. c_karty = m.c_karty .AND. mnozstvi > 0 .AND. mnozstvi<> zustatek mnozstvi WITH m.zustatek
				ELSE
					IF zustatek<>obraty.mnozstvi				
						REPLACE zustatek WITH obraty.mnozstvi
					ENDIF 
					IF cena_prum<>obraty.cena
						REPLACE cena_prum WITH obraty.cena
					ENDIF 
				ENDIF

			CASE INLIST(m.operace, "PR")        && archivacia
				IF cena_prum<>obraty.cena
					REPLACE cena_prum WITH obraty.cena
				ENDIF 
				IF zustatek<>(m.zustatek + obraty.mnozstvi)
					REPLACE zustatek  WITH m.zustatek + obraty.mnozstvi
				ENDIF 
				SCATTER MEMVAR FIELDS cena_prum, zustatek

			CASE LEFT( m.operace, 1) = "P" .OR. (INLIST(operace,"VE","VR") .AND. mnozstvi>0)        && vsechny ostatni prijmy
				REPLACE zustatek  WITH m.zustatek + obraty.mnozstvi
				IF obraty.zustatek # 0
					IF m.zustatek >= 0 .OR. m.c_dokl < m.prepoc_ooo
						m.cenx=ROUND((m.zustatek * NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena)) + obraty.mnozstvi * obraty.cena) / obraty.zustatek, 4)
						IF cena_prum<>m.cenx
							REPLACE cena_prum WITH m.cenx
						ENDIF 
					ELSE
						IF cena_prum<>obraty.cena
							REPLACE cena_prum WITH obraty.cena
						ENDIF 
					ENDIF
				ENDIF

			CASE LEFT( m.operace, 1) = "V"        && vsechny ostatni vydeje ( i prip. kladne mnozstvi (ze storen) se bere za prum.cenu )
				IF zustatek<>(m.zustatek + obraty.mnozstvi)
					REPLACE zustatek WITH m.zustatek + obraty.mnozstvi
				ENDIF 
				m.cenx=NTOM(MTON(m.cena_prum)*en__meny(m.c_mena,doklady.c_mena))
				IF cena_prum<>m.cenx
					REPLACE cena_prum WITH m.cenx
				ENDIF 
				IF cena<>m.cenx
					REPLACE cena WITH m.cenx
				ENDIF 
		ENDCASE

		SELECT obraty
		GO (m.obr_recno)
		SCATT FIELD zustatek, cena_prum MEMVAR
		SELECT sklad
		SELECT * FROM sklad WHERE c_stredisk=m.c_stredisk AND c_karty=m.c_karty INTO CURSOR asklad
		DO CASE
			CASE RECCOUNT("asklad")>1
				DELETE FROM sklad  WHERE c_stredisk=m.c_stredisk AND c_karty=m.c_karty
				IF m.zustatek # 0
					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 
			CASE RECCOUNT("asklad")=0
				IF m.zustatek # 0
					INSERT INTO sklad (c_stredisk, c_karty, zustatek, cena_prum, c_mena, min_zaso) ;
						VALUES (m.c_stredisk, m.c_karty, m.zustatek, m.cena_prum, m.c_mena1, -1.1111)
				ENDIF 
			OTHERWISE 
				SELECT sklad
				REPLACE zustatek WITH m.zustatek, cena_prum WITH m.cena_prum, c_mena WITH m.c_mena1 FOR c_stredisk=m.c_stredisk AND c_karty=m.c_karty	
*				UPDATE sklad SET zustatek=m.zustatek, cena_prum=m.cena_prum, c_mena=m.c_mena1 WHERE c_stredisk=m.c_stredisk AND c_karty=m.c_karty	
		ENDCASE 
		USE IN asklad
		SELECT obraty
	ENDSCAN
	SELECT sklad
	DELETE FOR zustatek = 0 .AND. min_zaso = -1.1111
	REPLACE FOR min_zaso = -1.1111 min_zaso WITH 0
	
	SELECT obraty
	SET RELATION OFF INTO doklady
    
    SELECT doklady
    REPLACE FOR operace = "PI" platba WITH "PREP"

	= n_sunlock ("POHYBY_EXCL_")
	SELECT o.c_stredisk, o.c_karty, o.c_dokl, o.operace, k.nazev, o.zustatek, o.mnozstvi, o.cena ORDER BY 1,2,3 ;
		WHERE LEFT(operace, 1) IN ("P", "V") AND zustatek < 0 AND mnozstvi < 0;
		FROM (m.fn_obraty) o LEFT JOIN (m.fn_karty) k ON k.c_karty == o.c_karty INTO CURSOR zapory
	IF RECC() > 0
		had.HIDE()
		ACTIVATE SCREE
		SET RELATION TO c_dokl INTO doklady
		DO pr_report WITH "zapory"
	ENDIF

	SELECT karty
	SET ORDER TO c_karty
	SELECT sklad
	SET RELATION TO c_karty INTO karty
	REPLACE FOR zustatek = 0 .AND. cena_prum=0 cena_prum WITH karty.cena_prum,c_mena WITH karty.c_mena_n


* * *  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


	IF .NOT. (("INVENT" $ UPPER(PROGRAM(2) + PROGRAM(3) + PROGRAM(4) + PROGRAM(5) + PROGRAM(6) + PROGRAM(7))) .or. ;
		("HLEDEJ" $ UPPER(PROGRAM(2) + PROGRAM(3) + PROGRAM(4) + PROGRAM(5) + PROGRAM(6) + PROGRAM(7))))
		CLOSE DATABASES 
	ENDIF
    ACTIVATE SCREEN
    DO zurnal_log WITH "OPERA", "PREPOCET", had.trvalo()
	RELEASE had

ELSE
	= MESSAGEBOX(jaz("ted nelze, zkuste pozdeji"),0+16+256, jaz("prg_prepocet"), 3333)
ENDIF




*!*****************************************************************************
*!
*!      Procedure: obr2f5 - prepocet skladov s obratov - podla starej Food500-ky
*!
*!*****************************************************************************
PROCEDURE obr2f5
PARAMETERS p_od_dokladu
PRIVATE m.xxxxxx
SELE obraty
COUNT TO m.xxxxxx FOR c_dokl>m.p_od_dokladu

SELECT sklad
SELECT DISTINCT c_karty, c_stredisk FROM (m.fn_obraty) ;
	WHERE LEFT(operace, 1) IN (UPPER("p"), UPPER("v")) .AND. c_dokl<p_od_dokladu INTO TABLE (m.gcpri_path  + "obr2f5")
had.set_max(RECC()+m.xxxxxx)
SCAN
	had.INCREMENT("stredisko: " + ALLT(STR(ABS(obr2f5.c_stredisk))) + ", karta: " + ALLT(STR(ABS(obr2f5.c_karty))))
	DO prepoc_f5 WITH obr2f5.c_karty, obr2f5.c_stredisk, m.p_od_dokladu
	SELECT obr2f5
ENDSCAN
RELEASE m.xxxxxx
RETURN


*:*****************************************************************************
*:        Program: prepoc_f5
*:*****************************************************************************
PROCEDURE prepoc_f5
PARAMETERS c_karty, c_stredisk, do_dokl,d_dokl,c_mena,c_mena1
m.d_dokl=0
m.c_mena=m.giakt_mena
m.c_mena1=m.c_mena


SELECT sklad
LOCATE FOR c_karty = m.c_karty .AND. c_stredisk = m.c_stredisk
IF FOUND()
	SCATTER FIELD cena_prum MEMVAR
ELSE
	cena_prum = 0
ENDIF
zustatek = 0

SELECT obraty
SET RELATION TO c_dokl INTO doklady
SCAN FOR c_karty = m.c_karty .AND. c_stredisk=m.c_stredisk .AND. c_dokl<m.do_dokl .AND. INLIST(LOWER(LEFT(obraty.operace, 1)), "v", "p")
	DO CASE 
		CASE EMPTY(m.d_dokl)
			m.d_dokl=doklady.c_dokl
			m.c_mena1=doklady.c_mena
			m.c_mena=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 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)
			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 > 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
ENDSCAN
SELECT obraty
SET RELATION OFF INTO doklady

SELECT sklad
LOCATE FOR c_karty = m.c_karty .AND. c_stredisk=m.c_stredisk
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

DO zurnal_log WITH "OPERA", "PREPOCET Food500", "c_karty:" + ALLTRIM(STR(c_karty, 12)) + ", stredisk:" + ALLTRIM(STR(c_stredisk, 12)) + ", do dokl.:" + ALLTRIM(STR(do_dokl, 12))

