*!*****************************************************************************
*!      Procedure: KALKULUJ
*!*****************************************************************************
PROCEDURE kalkuluj
PARAMETERS c_sklad
IF PARAMETERS()=0
	m.c_sklad=m.gic_stredisk
ENDIF 
* v ODECTENO je odectene mnozstvi, neodectene zustane v MNOZSTVI
*CREATE TABLE (m.gcpri_path + "kalk") (c_karty i, hruba Y, cista Y)
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 (m.gcpri_path + "odp_log") (  c_karty,     hruba,      hloubka,     o_nazev) ;
				VALUE (sk1.c_karty, 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 (m.gcpri_path + "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)
					INSERT INTO kalk (c_karty, hruba, cista) ;
						VALUE (polozky.c_poloz, ROUND(m.mnozstvi * polozky.hruba / polozky.pocet, 4),ROUND(m.mnozstvi * polozky.cista / polozky.pocet, 4))
					IF ! EMPTY(m.dlouhy_log)
						INSERT INTO (m.gcpri_path + "odp_log") (      c_karty,     hruba,                                            o_c_karty, o_mnozstvi,   hloubka) ;
							VALUE (polozky.c_poloz, ROUND(m.mnozstvi * polozky.hruba / polozky.pocet, 4), m.c_karty,      0    , m.hloubka)
					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(cista) AS cista GROUP BY 1 ;
				FROM kalk INTO TABLE (m.gcpri_path + "qqq")
			IF RECCOUNT() > 0
				SELECT kalk
				ZAP
				SELECT prep
				DELETE ALL
*				PACK
				INSERT INTO prep SELECT c_karty, 0 AS mnozstvi, zustatek, cena_prum, c_mena, c_stredisk FROM (m.gcpri_path+"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()
				APPEND FROM (m.gcpri_path + "qqq")
				GOTO (m.pozice)
			ELSE
				EXIT
			ENDIF
		ENDIF
	ENDDO
ENDIF
USE IN kalk
datum = ser_time()


PROCEDURE akt_a_skl
PARAMETERS ck
IF "bystrina"$LOWER(m.gchom_path)
	WAIT m.ck WINDOW NOWAIT 
ENDIF 
SELECT prep
DELETE ALL
*PACK
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 
*PACK
SELECT sk1