金蝶K3库存分类汇总表
按照仓库单据的类型分类统计出日期段内各种物料各种类型的单据出入数量
create Proc stkd_库存分类汇总表
@BegDate DateTime,@EndDate DateTime,
@BegItem Varchar(100),@EndItem Varchar(100),
@stock varchar(20)
As
--Select @BegDate='2004-01-01',@EndDate='2004-10-29',@BegItem='',@EndItem='',@BegCust='',@EndCust=''
select @enditem='zzzz' where @enditem=''
Set NoCount On
declare @st int
select @st=fitemid from t_stock where fname=@stock
create table #aa(
fitemid int,
fnumber varchar(80),
fname varchar(80),
fmodel varchar(50),
fbeg decimal(18,2) default(0),
fpur decimal(18,2) default(0),
fbz decimal(18,2) default(0),
fling decimal(18,2) default(0),
fdin decimal(18,2) default(0),
fdout decimal(18,2) default(0),
fpin decimal(18,2) default(0),
fpout decimal(18,2) default(0),
fsale decimal(18,2) default(0),
fqin decimal(18,2) default(0),
fqout decimal(18,2) default(0),
fend decimal(18,2) default(0)
)
declare @beg varchar(10)
select @beg=cast(year(@begdate) as varchar(4))+'-'+cast(month(@begdate) as varchar(2))
+'-01'
insert into #aa(fitemid,fnumber,fname,fmodel)
select fitemid,fnumber,fname,fmodel
from t_icitem
where fnumber>=@begitem and fnumber<=@enditem
update t1
set t1.fbeg=t2.fbegbal
from #aa t1,
(select fitemid,sum(fbegqty) as fbegbal from icinvbal
where fstockid in (select fitemid from t_stock where fname like '%'+@stock+'%')
and fyear=year(@begdate) and fperiod=month(@begdate)
group by fitemid)
t2
where t1.fitemid=t2.fitemid
update t1
set t1.fbeg=t1.fbeg+t2.fbal
from #aa t1,(
select ice.fitemid,
sum(case when ic.ftrantype in (1,2,3,5,10,40,41) and ice.fdcstockid=@st then ice.fqty
when ic.ftrantype in (21,24,29,28,43,41) and ice.fscstockid=@st then -ice.fqty else 0 end) as fbal
from icstockbill ic,icstockbillentry ice
where ic.finterid=ice.finterid and ic.fdate>=@beg and ic.fdate<@begdate
group by ice.fitemid
) t2
where t1.fitemid=t2.fitemid


update t1
set t1.fpur=t2.fpur,t1.fbz=t2.fbz,t1.fqin=t2.fqin,
t1.fqout=t2.fqout,t1.fsale=t2.fsale,t1.fpin=t2.fpin,t1.fpout=t2.fpout,t1.fling=t2.fling,
t1.fdin=t2.fdin,t1.fdout=t2.fdout
from #aa t1,(
select fitemid,
sum(case when ic.ftrantype=1 and ice.fdcstockid=@st then ice.fqty else 0 end) as fpur,
sum(case when ic.ftrantype=2 and ice.fdcstockid=@st then ice.fqty else 0 end) as fbz,
sum(case when ic.ftrantype=10 and ice.fdcstockid=@st then ice.fqty else 0 end) as fqin,
sum(case when ic.ftrantype=21 and ice.fdcstockid=@st then ice.fqty else 0 end) as fsale,
sum(case when ic.ftrantype=24 and ice.fscstockid=@st then ice.fqty else 0 end) as fling,
sum(case when ic.ftrantype=29 and ice.fdcstockid=@st then ice.fqty else 0 end) as fqout,
sum(case when ic.ftrantype=40 and ice.fdcstockid=@st then ice.fqty else 0 end) as fpin,
sum(case when ic.ftrantype=43 and ice.fdcstockid=@st then ice.fqty else 0 end) as fpout,
sum(case when ic.ftrantype=41 and ice.fdcstockid=@st then ice.fqty else 0 end) as fdin,
sum(case when ic.ftrantype=41 and ice.fscstockid=@st then ice.fqty else 0 end) as fdout
from icstockbill ic,icstockbillentry ice
where ic.finterid=ice.finterid and ic.fdate>=@begdate and ic.fdate<=@enddate
group by ice.fitemid
)
t2
where t1.fitemid=t2.fitemid


update #aa set fend=fbeg+fpur+fbz+fqin+fdin+fpin-fdout-fqout-fling-fpout-fsale
delete from #aa where fbeg=0 and fend=0 and fqout=0
and fqin=0 and fpur=0 and fsale=0
select fnumber as 物料编码,fname as 物料名称,fmodel as 规格,fbeg as 期初,fpur as 外购,
fbz as 产品入库,fdin as 调拨入库,
fdout as 调拨出库,fpin as 盘盈入库,fpout as 盘盈出库,fling as 领料,fsale as 销售出库,
fqin as 其他入库,fqout as 其他出库,fend as 期末
from #aa order by fnumber


GO

浙公网安备 33010602011771号