arrowolf

按照部门业务员客户汇总出客户的来款,并列出到期的应收款,以方便在未核销时

 

create proc stkd_销售_应收来款明细    --stkd_销售_应收来款明细 \'2006-4-1\'
@begdate datetime,
@enddate datetime
as

set nocount on

create table #aa(
fitemid 
int,
fdept 
varchar(30),
fnumber 
varchar(80),
fname 
varchar(80),
famount 
decimal(18,2default(0),
fexpireamount 
decimal(18,2default(0),
fallamount 
decimal(18,2default(0),
femp 
varchar(80)
)

insert into #aa(fitemid,fnumber,fname,femp,fdept)
select t1.fitemid,t1.fnumber,t1.fname,t2.fname,td.fname
from t_organization t1 left join t_emp t2 on t1.femployee=t2.fitemid
left join t_department td on t2.fdepartmentid=td.fitemid
where t1.fdeleted=0

update #aa set femp=\'\' where femp is null

update t1 
set t1.famount=t2.famount
from #aa t1,(
select fcustomer,sum(case when ftype in (1,3then fremainamount when ftype=5 then -fremainamount else 0 endas famount
from t_rp_contact where fdate<=@enddate and fdate>=@begdate
group by fcustomer
) t2
where t1.fitemid=t2.fcustomer

update t1
set t1.fexpireamount=t2.fex
from #aa t1,(
select fcustomer,sum(tr.fremainamount) as fex
from t_rp_contact tr inner join t_RP_Plan_Ar tp on tr.fid=tp.forgid and tp.fdate<=@begdate  
group by fcustomer
) t2
where t1.fitemid=t2.fcustomer

update t1
set t1.fexpireamount=t1.fexpireamount-t2.fex
from #aa t1,(
select fcustomer,sum(tr.fremainamount) as fex
from t_rp_contact tr where tr.fdate<=@enddate and tr.fdate>=@begdate and tr.ftype=5
group by fcustomer
) t2
where t1.fitemid=t2.fcustomer

delete from #aa where famount=0 and fexpireamount=0

insert into #aa(fdept,femp,famount,fexpireamount,fallamount)
select fdept,femp+\'合计\',sum(famount),sum(fexpireamount),sum(fallamount)
from #aa
group by fdept,femp

insert into #aa(fdept,famount,fexpireamount)
select fdept+\'合计\',sum(famount),sum(fexpireamount)
from #aa
where femp like \'%合计\'
group by fdept

select fdept as 部门,femp as 业务员,fnumber as 客户编码,fname as 客户名称,famount as 金额,fexpireamount as 到期应收款
from #aa 
order by fdept,femp

drop table #aa

GO 

分类:

技术点:

相关文章:

  • 2021-12-12
  • 2021-11-16
  • 2021-12-12
  • 2021-12-02
  • 2021-12-02
  • 2022-01-16
  • 2021-09-04
  • 2021-12-24
猜你喜欢
  • 2021-10-19
  • 2020-04-26
  • 2021-11-16
  • 2021-12-12
  • 2021-12-12
  • 2021-12-12
相关资源
相似解决方案