金蝶K3核算项目余额表(应收应付

增强的总帐的核算项目余额表,上面增加了客户和业务员的信息,可以统计出应收应付业务员的具体情况

CREATE proc stkd_核算项目余额表     --stkd_核算项目余额表 6,2006,'1131.01'
@period int,
@year int,
@account varchar(10)
as
set nocount on

create table #aa(
faccountid 
int,
faccountnumber 
varchar(50),
faccountname 
varchar(80),
fcurrency 
varchar(10),
fnumber 
varchar(80),
fname 
varchar(80),
femp 
varchar(20),
fdept 
varchar(30),
fbeginc 
decimal(18,2default(0),
fbegind 
decimal(18,2default(0),
finc 
decimal(18,2default(0),
find 
decimal(18,2default(0),
fendc 
decimal(18,2default(0),
fendd 
decimal(18,2default(0)
)

insert into #aa
select tb.faccountid,ta.fnumber,ta.fname,tc.fname,
case when ti.f1=-1 then too.fnumber when ti.f8=-1 then ts.fnumber else '' end,
case when ti.f1=-1 then too.fshortname when ti.f8=-1 then ts.fname else '' end,
case when tid.fitemclassid=1 then te.fname when tid.fitemclassid=8 then tee.fname else '' end,
case when tid.fitemclassid=1 then td.fname when tid.fitemclassid=8 then tdd.fname else '' end,
case when tb.fbeginbalancefor>0 then tb.fbeginbalancefor else 0 end,
case when tb.fbeginbalancefor<0 then -tb.fbeginbalancefor else 0 end,
tb.fdebitfor,tb.fcreditfor,
case when tb.fendbalancefor>0 then tb.fendbalancefor else 0 end,
case when tb.fendbalancefor<0 then -tb.fendbalancefor else 0 end 
from t_balance tb,
t_account ta 
left join t_itemdetail ti on ta.fdetailid=ti.fdetailid,t_itemdetailv tid 
left join t_organization too on tid.fitemid=too.fitemid and tid.fitemclassid=1 
left join t_emp te on too.femployee=te.fitemid left join t_department td on te.fdepartmentid=td.fitemid
left join t_supplier ts on tid.fitemid=ts.fitemid and tid.fitemclassid=8
left join t_emp tee on ts.femployee=tee.fitemid left join t_department tdd on tee.fdepartmentid=tdd.fitemid,
t_currency tc
where tb.faccountid=ta.faccountid and tb.fdetailid=tid.fdetailid 
and tb.fcurrencyid=tc.fcurrencyid and tb.fperiod=@period and tb.fyear=@year and ta.fnumber=@account

delete from #aa where fbeginc=0 and fbegind=0 and finc=0 and find=0

update t1 set t1.fbeginc=t2.fbeginc,t1.fbegind=t2.fbegind,t1.fendc=t2.fendc,t1.fendd=t2.fendd
from #aa t1,(select fcurrency,sum(fbeginc) as fbeginc,sum(fbegind) as fbegind,
sum(fendc) as fendc,sum(fendd) as fendd
from #aa where fnumber is not null group by fcurrency) t2
where t1.fcurrency=t2.fcurrency and t1.fnumber is null
delete from #aa where fcurrency='*'

select faccountnumber as 科目代码,faccountname as 科目名称,fcurrency as 币别,fdept as 部门,femp as 专营业务员,fnumber as 项目代码,
fname 
as 项目名称,fbeginc as 期初借方,fbegind as 期初贷方,finc as 本期借方,FINd as 本期贷方,
fendc 
as 期末借方,fendd as 期末贷方 from #aa order by fdept,femp,fcurrency

drop table #aa


GO
posted @ 2008-08-07 02:07  arrowolf  阅读(2679)  评论(0)    收藏  举报