*!*****************************************************************************
*!      Procedure: KALKULUJ
*!*****************************************************************************
PROCEDURE kalkuluj
PARAMETERS c_sklad, m.dlouhy_log, cdokldo
PRIVATE m.hloubka

* v ODECTENO je odectene mnozstvi, neodectene zustane v MNOZSTVI
SELECT 000000000000 as c_karty, $0 as hruba, $0 as cista FROM sk1 INTO CURSOR kalk READWRITE  WHERE .f.
m.hloubka=1
SELECT sk1
GO TOP
IF ! EOF()
	SET RELATION TO c_karty INTO a_skl
	DO WHILE .T.
		IF !a_skl.prepocet
			SET RELATION OFF INTO a_skl
			DO akt_a_skl WITH sk1.c_karty
			SELECT sk1
			SET RELATION TO c_karty INTO a_skl
		ENDIF 
		REPLACE sk1.odecteno WITH MIN(sk1.mnozstvi, MAX(a_skl.k_odpoctu, 0)), sk1.mnozstvi WITH sk1.mnozstvi - sk1.odecteno
		REPLACE a_skl.k_odpoctu WITH a_skl.k_odpoctu - sk1.odecteno
		IF sk1.odecteno # 0 .AND. ! EMPTY(m.dlouhy_log)
			INSERT INTO odp_log (  c_karty,     hruba, o_mnozstvi,     hloubka,     o_nazev) ;
				VALUE (sk1.c_karty, sk1.odecteno, sk1.odecteno, m.hloubka, ">>>  ODECTENO  <<<")
		ENDIF
		IF sk1.mnozstvi > 0 .AND. EMPT(a_skl.vycisleni)
			SCATTER FIELD c_karty, mnozstvi MEMVAR
			SELECT polozky
			LOCATE FOR c_karty = m.c_karty .AND. ! EMPT(c_poloz) .AND. ! EMPT(hruba)
			IF FOUND()
				IF ! EMPTY(m.dlouhy_log)
					INSERT INTO odp_log (c_karty, hruba, o_c_karty, o_mnozstvi,   hloubka) ;
						VALUE (    0,     0,   m.c_karty, m.mnozstvi, m.hloubka)
				ENDIF
				SCAN FOR c_karty = m.c_karty .AND. ! EMPT(c_poloz) .AND. ! EMPT(hruba)
					TRY 
						INSERT INTO kalk (c_karty, hruba, cista) ;
							VALUE (polozky.c_poloz, ROUND(m.mnozstvi * polozky.hruba / IIF(polozky.pocet=0 OR ISNULL(polozky.pocet),1,polozky.pocet), 4),ROUND(m.mnozstvi * polozky.cista / IIF(polozky.pocet=0 OR ISNULL(polozky.pocet),1,polozky.pocet), 4))
					CATCH
						=MESSAGEBOX(jaz("form_chyba_pri_odp_kalk_c")+ALLTRIM(STR(polozky.c_karty))+". "+jaz("rep_pocet mj")+":"+ALLTRIM(STR(polozky.pocet,10,5))+".", 64,"FoodMan")
					ENDTRY 
					IF ! EMPTY(m.dlouhy_log)
						TRY 
							INSERT INTO odp_log (      c_karty,     hruba,                                            o_c_karty, o_mnozstvi,   hloubka) ;
								VALUE (polozky.c_poloz, ROUND(m.mnozstvi * polozky.hruba / IIF(polozky.pocet=0 OR ISNULL(polozky.pocet),1,polozky.pocet), 4), m.c_karty,      0    , m.hloubka)
						CATCH
						ENDTRY 
					ENDIF
				ENDSCAN
				SELECT sk1
				REPLACE mnozstvi WITH 0
			ELSE
				SELECT sk1
			ENDIF
		ENDIF
		SKIP
		IF EOF()
			m.hloubka = m.hloubka + 1
			SELECT c_karty, SUM(hruba) AS mnozstvi,SUM(hruba) AS hruba, SUM(cista) AS cista GROUP BY 1 ;
				FROM kalk INTO CURSOR qqq
			
			IF RECCOUNT() > 0
				SELECT kalk
				DELETE ALL 
				SELECT prep
				DELETE ALL
				INSERT INTO prep SELECT c_karty, 0 AS mnozstvi, zustatek, cena_prum, c_mena, c_stredisk FROM a_skl WHERE c_karty IN (SELECT DISTINCT c_karty FROM qqq) .AND. !a_skl.prepocet .AND. a_skl.c_stredisk=m.c_sklad
				DO prepocet2 WITH "prep",0,m.cdokldo,.F.,.F.
				SELECT prep
				SET RELATION TO c_karty INTO a_skl
				SCAN ALL
					IF a_skl.k_odpoctu<>prep.zustatek
						REPLACE a_skl.k_odpoctu WITH prep.zustatek, a_skl.zustatek WITH prep.zustatek,a_skl.prepocet WITH .T., cena_prum WITH prep.cena_prum, c_mena WITH prep.c_mena
						SELECT sklad
						REPLACE zustatek WITH prep.zustatek, cena_prum WITH prep.cena_prum, c_mena WITH prep.c_mena FOR c_karty=prep.c_karty .AND. c_stredisk=prep.c_stredisk
						SELECT prep
					ELSE
						REPLACE a_skl.prepocet WITH .t.
					ENDIF 
				ENDSCAN 
*				SELECT sk1
				pozice = RECNO("sk1")
				INSERT INTO sk1 (c_karty, mnozstvi) SELECT c_karty, mnozstvi FROM qqq
				USE IN qqq
				SELECT sk1
				GOTO (m.pozice)
			ELSE
				EXIT
			ENDIF
		ENDIF
	ENDDO
ENDIF
USE IN kalk
datum = ser_time()


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE akt_a_skl
PARAMETERS ck
SELECT prep
DELETE ALL
INSERT INTO prep (c_karty,zustatek,cena_prum, c_mena, c_stredisk) VALUES (m.ck,0,0,m.giakt_mena, m.c_sklad)
DO prepocet2 WITH "prep",0,m.cdokldo,.f.,.f.
SELECT prep
SET RELATION TO c_karty INTO a_skl
GO TOP 
IF a_skl.k_odpoctu<>prep.zustatek
	REPLACE a_skl.k_odpoctu WITH prep.zustatek, a_skl.zustatek WITH prep.zustatek, a_skl.prepocet WITH .t., cena_prum WITH prep.cena_prum, c_mena WITH prep.c_mena
	SELECT sklad
	REPLACE zustatek WITH prep.zustatek, cena_prum WITH prep.cena_prum, c_mena WITH prep.c_mena FOR c_karty=prep.c_karty .and. c_stredisk=prep.c_stredisk
	SELECT prep
ELSE
	REPLACE a_skl.prepocet WITH .t. 
ENDIF 
SET RELATION OFF INTO a_skl
DELETE ALL 
SELECT sk1