*:*****************************************************************************
*:        Program: E:\FOOD500\SKL2\GEN_STAV.PRG
*:*****************************************************************************
PARAMETERS dat_inv, lc_stredisk, cmena
PRIVATE viacako1, rcelkom,parms, boliobraty, bolidoklady
m.parms=PARAMETERS()
IF m.parms<2
     m.lc_stredisk=m.gic_stredisk
ENDIF 
IF m.parms<3
	m.cmena=m.giakt_mena
ENDIF 

IF !USED("doklady")
	m.bolidoklady=.f.
	USE (m.fn_dokl_s) ALIAS doklady IN 0
ELSE
	m.bolidoklady=.t.
ENDIF 
IF !USED("obraty")
	m.boliobraty=.f.
	USE (m.fn_obra_s) ALIAS obraty IN 0
ELSE
	m.boliobraty=.t.
ENDIF 


logshad = CREATEOBJECT("had", "gen_stav")
logshad.Show()
select disti c_karty, MAX(c_dokl) as c_dokl, $0 as zustatek, $0 as cena_prum, $0 as cena, ;
       space(30) as nazev, space(10) as druh, space(13) as kod, "   " as spart, " " as vycisleni, c_stredisk, m.cmena as c_mena ;
       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") 
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 TABLE (m.gcpri_path+"porcount")
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
	
	
sele obraty
set orde to
sele karty
set orde to c_karty
sele skl_i
SET RELATION TO c_dokl INTO doklady
REPLACE ALL c_mena WITH doklady.c_mena
SET RELATION OFF INTO doklady
REPLACE ALL cena_prum WITH NTOM(MTON(cena_prum)*en__meny(skl_i.c_mena,m.cmena))
REPLACE ALL c_mena WITH m.cmena
SET RELA TO c_karty INTO karty
if "SKL" $ gcpgm_param
   REPL ALL nazev with karty.nazev, druh WITH karty.druh_skl, kod WITH karty.kod, cena with cena_prum
   repl for empt(druh) .and. ! empt(karty.druh) druh with "* " + karty.druh
else
   REPL ALL nazev with karty.nazev, druh WITH karty.druh,     kod WITH karty.kod, cena with cena_prum
   repl for empt(druh) .and. ! empt(karty.druh_skl) druh with "* " + karty.druh_skl
endif
SET RELA TO
IF !bolidoklady
	USE IN doklady
ENDIF 
IF !boliobraty
	USE IN obraty
ENDIF 

RELEASE logshad
