*:*****************************************************************************
*:        Program: E:\FOOD500\SKL2\GEN_STAV.PRG
*:*****************************************************************************
PARAMETERS dat_inv, lc_stredisk, cmena, m.fnk, m.fno, m.fnd, m.fnsd, m.pparam
PRIVATE viacako1, rcelkom,parms, boliobraty, bolidoklady, bolikarty, bolsklad
IF EMPTY(m.lc_stredisk) AND VARTYPE(m.lc_stredisk)="L"
	m.lc_stredisk=m.gic_stredisk
ENDIF
IF EMPTY(m.cmena)
	m.cmena=m.giakt_mena
ENDIF
IF EMPTY(m.fnk)
	m.fnk=m.fn_karty
ENDIF 
IF EMPTY(m.fno)
	m.fno=m.fn_obra_s
ENDIF 
IF EMPTY(m.fnd)
	m.fnd=m.fn_dokl_s
ENDIF 
IF EMPTY(m.fnsd)
	m.fnsd=m.fn_spotrebnadan
ENDIF 
IF EMPTY(m.pparam)
	m.pparam=m.gcpgm_param
ENDIF 
IF !USED("doklady")
	m.bolidoklady=.F.
	USE (m.fnd) ALIAS doklady IN 0 order c_dokl
ELSE
	m.bolidoklady=.T.
ENDIF
IF !USED("obraty")
	m.boliobraty=.F.
	USE (m.fno) ALIAS obraty IN 0
ELSE
	m.boliobraty=.T.
ENDIF
IF !USED("karty")
	m.bolikarty=.F.
	USE (m.fnk) ALIAS karty IN 0
ELSE
	m.bolikarty=.T.
ENDIF
TRY 
	IF !USED("sklad")
		m.bolsklad=.F.
		USE (m.fn_sklad) ALIAS sklad IN 0 AGAIN 
	ELSE
		m.bolsklad=.T.
	ENDIF
CATCH
	m.bolsklad=.t.
ENDTRY 


logshad = CREATEOBJECT("had", "gen_stav")
logshad.SHOW()
IF m.lc_stredisk=0
	SELECT DISTI c_karty, MAX(c_dokl) AS c_dokl, $0 AS zustatek, $0 AS cena_prum, $0 AS cena, $0 AS spstupne, $0 AS spdan, ;
		SPACE(30) AS nazev, SPACE(m.sirkastlpcadruh) AS druh, SPACE(13) AS kod, "   " AS spart, " " AS vycisleni, c_stredisk, m.cmena AS c_mena, 000000.0000 AS min_zaso,000000000000 as idriadok,000000000000 as c_druh ;
		WHERE c_dokl < m.dat_inv .AND. LEFT(operace,1) $ "PV_" ;
		GROUP BY c_karty,c_stredisk FROM obraty INTO TABLE (m.gcpri_path + "skl_i")
ELSE
	SELECT DISTI c_karty, MAX(c_dokl) AS c_dokl, $0 AS zustatek, $0 AS cena_prum, $0 AS cena, $0 AS spstupne, $0 AS spdan, ;
		SPACE(30) AS nazev, SPACE(m.sirkastlpcadruh) AS druh, SPACE(13) AS kod, "   " AS spart, " " AS vycisleni, c_stredisk, m.cmena AS c_mena, 000000.0000 AS min_zaso,000000000000 as idriadok,000000000000 as c_druh ;
		WHERE c_stredisk = m.lc_stredisk .AND. c_dokl < m.dat_inv .AND. LEFT(operace,1) $ "PV_" ;
		GROUP BY c_karty,c_stredisk FROM obraty INTO TABLE (m.gcpri_path + "skl_i")
ENDIF 
INDEX ON STR(c_stredisk,12)+STR(c_karty, 12) TAG sklstrcd
INDEX ON c_karty TAG c_karty
logshad.set_had(5)
SELECT c_dokl,c_karty,COUNT(c_karty) AS riadky,MAX(poradi) AS poradi,operace FROM obraty WHERE c_dokl IN (SELECT DISTINCT c_dokl FROM skl_i) ;
	GROUP BY c_dokl,c_karty INTO CURSOR porcount READWRITE 
INDEX ON STR(c_dokl,12)+STR(c_karty,12) TAG dokkart
logshad.set_had(10)
SELECT obraty
INDEX ON STR(c_dokl,12)+STR(c_karty,12) TO (m.gcpri_path+"dokkart")
logshad.set_had(20)
SELECT skl_i
SET RELATION TO STR(c_dokl,12)+STR(c_karty,12) INTO porcount
SET RELATION TO STR(c_dokl,12)+STR(c_karty,12) INTO obraty ADDITIVE
REPLACE zustatek WITH obraty.zustatek, cena_prum WITH obraty.cena_prum FOR porcount.riadky=1
logshad.set_had(25)
COUNT TO m.viacako1 FOR porcount.riadky>1
m.rcelkom=RECCOUNT("skl_i")
IF m.rcelkom=0
	m.rcelkom=1
ENDIF
IF m.viacako1>0
	SCAN FOR porcount.riadky>1
		SCATTER MEMVAR FIELDS skl_i.c_dokl,skl_i.c_karty,porcount.poradi
		SELECT zustatek,cena_prum FROM obraty WHERE c_dokl=m.c_dokl .AND. c_karty=m.c_karty .AND. poradi=m.poradi INTO CURSOR zostat
		SELECT skl_i
		REPLACE zustatek WITH zostat.zustatek, cena_prum WITH zostat.cena_prum
		USE IN zostat
		logshad.set_had(VAL(STR((m.rcelkom-m.viacako1)/m.rcelkom*100,3,0)))
		m.viacako1=m.viacako1-1
	ENDSCAN
ENDIF
COUNT TO m.viacako1 FOR porcount.riadky>1 .AND. INLIST(porcount.operace,"PC","PB","VP","PP")
IF m.viacako1>0
	SCAN FOR porcount.riadky>1 .AND. INLIST(porcount.operace,"PC","PB","VP","PP")
		IF porcount.operace="PC"
			SCATTER MEMVAR FIELDS skl_i.c_dokl,skl_i.c_karty
			SELECT zustatek,cena_prum FROM obraty WHERE c_dokl=m.c_dokl .AND. c_karty=m.c_karty .AND. mnozstvi>0 INTO CURSOR zostat
			SELECT skl_i
			REPLACE zustatek WITH zostat.zustatek, cena_prum WITH zostat.cena_prum
			USE IN zostat
		ELSE
			SCATTER MEMVAR FIELDS porcount.poradi
			SCATTER MEMVAR FIELDS skl_i.c_dokl,skl_i.c_karty
			SELECT COUNT(c_karty) AS riadky FROM obraty WHERE c_dokl=m.c_dokl .AND. c_karty=m.c_karty .AND. poradi=m.poradi;
				INTO CURSOR porporad
			IF porporad.riadky=1
				SELECT zustatek,cena_prum FROM obraty WHERE c_dokl=m.c_dokl .AND. c_karty=m.c_karty .AND. poradi=m.poradi INTO CURSOR zostat
			ELSE
				SELECT zustatek,cena_prum FROM obraty WHERE c_dokl=m.c_dokl .AND. c_karty=m.c_karty .AND. poradi=m.poradi .AND. mnozstvi>0 INTO CURSOR zostat
			ENDIF
			USE IN porporad
			SELECT skl_i
			REPLACE zustatek WITH zostat.zustatek, cena_prum WITH zostat.cena_prum
			USE IN zostat
		ENDIF
	ENDSCAN
ENDIF

USE IN porcount


SELECT obraty
SET ORDER TO
SELECT karty
SET ORDER TO c_karty
SELECT skl_i
SET RELATION TO c_dokl INTO doklady
REPLACE ALL c_mena WITH doklady.c_mena
SET RELATION OFF INTO doklady
REPLACE cena_prum WITH NTOM(MTON(cena_prum)*en__meny(skl_i.c_mena,m.cmena)) FOR skl_i.c_mena<>m.cmena
REPLACE c_mena WITH m.cmena FOR skl_i.c_mena<>m.cmena




IF m.lc_stredisk=0
	SET RELATION TO c_karty INTO karty
	REPLACE ALL nazev WITH karty.nazev, druh WITH karty.druh, kod WITH karty.kod, cena WITH cena_prum, c_druh WITH karty.c_druh
	REPLACE FOR EMPTY(druh) .AND. ! EMPTY(karty.druh_skl) druh WITH "* " + karty.druh_skl, c_druh WITH karty.c_druhs
	REPLACE FOR EMPTY(druh) .AND. ! EMPTY(karty.druh_kalk) druh WITH "* " + karty.druh_kalk, c_druh WITH karty.c_druhk
ELSE
	IF USED("sklad")
*		INSERT INTO skl_i (c_karty, zustatek, cena_prum, c_stredisk, c_mena) SELECT c_karty, zustatek, cena_prum, c_stredisk, c_mena FROM sklad WHERE c_stredisk=m.lc_stredisk AND !(c_karty IN (SELECT DISTINCT c_karty FROM skl_i))
	ENDIF 
	SELECT skl_i
	SET RELATION TO c_karty INTO karty
	IF "SKL" $ m.pparam
		REPLACE ALL nazev WITH karty.nazev, druh WITH karty.druh_skl, kod WITH karty.kod, cena WITH cena_prum, c_druh WITH karty.c_druhs
		REPLACE FOR EMPTY(druh) .AND. ! EMPTY(karty.druh) druh WITH "* " + karty.druh, c_druh WITH karty.c_druh
	ELSE
		REPLACE ALL nazev WITH karty.nazev, druh WITH karty.druh,     kod WITH karty.kod, cena WITH cena_prum, c_druh WITH karty.c_druh
		REPLACE FOR EMPTY(druh) .AND. ! EMPTY(karty.druh_skl) druh WITH "* " + karty.druh_skl, c_druh WITH karty.c_druhs
	ENDIF
ENDIF 
SET RELATION TO
IF m.spotrebnadan<>0
	SELECT obraty.c_stredisk, SUM(obraty.mnozstvi*obraty.spdan*obraty.spstupne*karty.objem/100) as spdan, obraty.c_karty FROM obraty LEFT JOIN karty ON obraty.c_karty=karty.c_karty GROUP BY obraty.c_stredisk, obraty.c_karty WHERE obraty.spdan<>0 .AND. obraty.c_dokl < m.dat_inv .AND. obraty.operace<>"PR" INTO CURSOR spdanex ;
		UNION 	SELECT obraty.c_stredisk, SUM(obraty.spdan) as spdan, obraty.c_karty FROM obraty GROUP BY obraty.c_stredisk, obraty.c_karty WHERE obraty.spdan<>0 .AND. obraty.c_dokl < m.dat_inv .AND. obraty.operace="PR" 
	SELECT c_stredisk, SUM(spdan) as spdan, c_karty FROM spdanex INTO CURSOR spdane GROUP BY c_stredisk, c_karty
	USE IN spdanex
	SELECT skl_i
	UPDATE skl_i SET skl_i.spdan=IIF(ISNULL(spdane.spdan),0,spdane.spdan) FROM spdane WHERE skl_i.c_karty=spdane.c_karty .and. skl_i.c_stredisk=spdane.c_stredisk
	USE IN spdane
	SELECT 0
	USE (m.fnsd) ALIAS spdane ORDER c_karty
	SELECT skl_i
	SET RELATION TO c_karty INTO spdane
	REPLACE spstupne WITH spdane.koeficient FOR !EMPTY(spdane.c_karty)
	SET RELATION OFF INTO spdane
	USE IN spdane	
ENDIF 


IF !bolidoklady
	USE IN doklady
ENDIF
IF !boliobraty
	USE IN obraty
ENDIF
IF !bolikarty
	USE IN karty
ENDIF
IF !bolsklad
	USE IN sklad
ENDIF
RELEASE logshad
