if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[huizong]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[huizong]
GOSET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GOCREATE  PROCEDURE huizong  @year varchar(4),@month varchar(2),@cWhCode varchar(20),@about varchar(20)
     AS
    set nocount on  declare   @temp1 varchar(2000),@temp2 varchar(6),@zhangbu varchar(20)  set @temp2=@year+@month   Create Table #temp ( -- 汇总表
iIsId int identity (1,1)  not null, 
cCode varchar(20) not null ,      -- 商品编号
cName varchar(40) null ,            -- 商品名称
benyueqichu float default 0 , --本月期初
benyueruku float default 0 , --本月入库
benyuechuku float default 0 ,  --本月出库
benyueleft float default 0 ) --本月余额   Create Table #temp2 (
iIsId int identity (1,1)  not null, 
dDate DateTime not null ,                  -- 月结日期
cMth varchar(6) not null ,                    -- 月结年月(200304)
cOrderNo varchar(12) not null ,   -- 入库单编号
dInDate DateTime  null ,                  -- 入库日期
iLineNo int not null ,                      -- 行号 
cCaseNo varchar(20) null ,         -- 案号
cCode varchar(20) not null ,      -- 商品编号
cName varchar(40) null ,            -- 商品名称
cStd varchar(30) null ,                 -- 商品规格
cUnit varchar(10) null ,                -- 计量单位
iQuantity float default 0 ,             -- 入库数量
iOutQty float default 0 ,               -- 已出库数量累计
iBalQty float default 0 ,               -- 库存数量                  --------------------
iPrice float default 0 ,                  -- 估价
cWhCode varchar(20) null ,        -- 仓库编号
cWhName varchar(40) null ,        -- 仓库名称
cPrdCCode varchar(10)  null ,    -- 商品类别编号
cPrdCName varchar(40) null ,    -- 商品类别名称
cInType varchar(10) null ,            -- 入库类别 ( ZK--表示暂扣入库 / FM--表示罚没入库 )
cRe varchar(60) null )          -- 备注 
if @about='暂扣'
begin
insert into #temp2(dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe)
            select dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe from MthFwd where cMth= ''+ @temp2 +'' and convert(char(6),dindate,112)<''+ @temp2 +'' and cInType='ZK' and cWhCode like ''+ @cWhCode+'%'+''
insert into #temp (cCode,cName,benyueqichu)
select cCode,cname,sum(iBalQty) from #temp2 group by cCode,cname,cUnit
delete #temp2
insert into #temp2(dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe)
            select dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe from MthFwd where cMth= ''+ @temp2 +'' and convert(char(6),dindate,112)=''+ @temp2 +'' and cInType='ZK' and cWhCode like ''+ @cWhCode+'%'+''
update #temp set benyueruku=b.benyueruku,benyuechuku=b.benyuechuku from #temp a,(
select ccode,sum(iQuantity) benyueruku,sum(iOutQty) benyuechuku from #temp2 group by ccode) b where a.ccode=b.ccode
delete #temp2 where cCode in (select ccode from #temp)
insert into #temp (cCode,cName,benyueruku,benyuechuku)
select cCode,cname,sum(iQuantity),sum(iOutQty)from #temp2 group by cCode,cname,cUnit
update #temp set benyueleft=benyueqichu+benyueruku-benyuechuku
end
if @about='罚没'
begin
insert into #temp2(dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe)
            select dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe from MthFwd where cMth= ''+ @temp2 +'' and convert(char(6),dindate,112)<''+ @temp2 +'' and cInType='FM' and cWhCode like ''+ @cWhCode+'%'+''
insert into #temp(cCode,cName,benyueqichu)
select cCode,cname,sum(iBalQty) from #temp2 group by cCode,cname
delete #temp2
insert into #temp2(dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe)
            select dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe from MthFwd where cMth= ''+ @temp2 +'' and convert(char(6),dindate,112)=''+ @temp2 +'' and cInType='FM' and cWhCode like ''+ @cWhCode+'%'+''
update #temp set benyueruku=b.benyueruku,benyuechuku=b.benyuechuku from #temp a,(
select ccode,sum(iQuantity) benyueruku,sum(iOutQty) benyuechuku from #temp2 group by ccode) b where a.ccode=b.ccode
delete #temp2 where cCode in (select ccode from #temp)
insert into #temp (cCode,cName,benyueruku,benyuechuku)
select cCode,cname,sum(iQuantity),sum(iOutQty)from #temp2 group by cCode,cname
update #temp set benyueleft=benyueqichu+benyueruku-benyuechukuend
if @about='专营专管'
begin
insert into #temp2(dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe)
            select dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe from MthFwd where cMth= ''+ @temp2 +'' and convert(char(6),dindate,112)<''+ @temp2 +'' and cInType='FM' and cPrdCName='专营专管' and cWhCode like ''+ @cWhCode+'%'+''
insert into #temp (cCode,cName,benyueqichu)
select cCode,cname,sum(iBalQty) from #temp2 group by cCode,cname
delete #temp2
insert into #temp2(dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe)
            select dDate,cMth,cOrderNo,dInDate,iLineNo,cCaseNo,cCode,cName,cStd,cUnit,iQuantity,iOutQty,iBalQty,iPrice,cWhCode,cWhName,cPrdCCode,cPrdCName,cInType,cRe from MthFwd where cMth= ''+ @temp2 +'' and convert(char(6),dindate,112)=''+ @temp2 +'' and cInType='FM' and cPrdCName='专营专管' and cWhCode like ''+ @cWhCode+'%'+''
update #temp set benyueruku=b.benyueruku,benyuechuku=b.benyuechuku from #temp a,(
select ccode,sum(iQuantity) benyueruku,sum(iOutQty) benyuechuku from #temp2 group by ccode) b where a.ccode=b.ccode
delete #temp2 where cCode in (select ccode from #temp)
insert into #temp (cCode,cName,benyueruku,benyuechuku)
select cCode,cname,sum(iQuantity),sum(iOutQty)from #temp2 group by cCode,cname
update #temp set benyueleft=benyueqichu+benyueruku-benyuechukuend
select * from #tempGO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO