IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_getaccount]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_getaccount]
GO
CREATE procedure [dbo].[mps_getaccount](
@tbname1 varchar(100) ,  -- 不能为空 #master #romaster #detailx 如果是子表，则应该这：#master,#detail2
@accountdate varchar(500) , -- 可以是字段名，也可以是值
@acctype varchar(500) , -- 帐类别  loan , fee ...   可以是字段名，也可以是值
@acccategory varchar(500) , -- 分类，主要是 dpt emp cst ....   可以是字段名，也可以是值
@fid varchar(500) , -- 费用类别 , 有些是没有费用类别的，则=0  可以是字段名，也可以是值
@objid varchar(500) -- 责任对象   可以是字段名，也可以是值 
)
as 
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @sql nvarchar(4000) 
create table #dptai(type1 varchar(30),cate1 varchar(30),dpid int,aiid int) 

set @sql = 'insert into #dptai(type1,cate1,dpid,aiid) '+
'select distinct ' 

if (charindex('ord2_',@acctype)>0 or charindex('ord_',@acctype)>0) 
set @sql = @sql+replace(@acctype,'''','''''')+',' 
else set @sql = @sql+''''+replace(@acctype,'''','''''')+''',' 

if (charindex('ord2_',@acccategory)>0 or charindex('ord_',@acccategory)>0) 
set @sql = @sql+replace(@acccategory,'''','''''')+',' 
else set @sql = @sql+''''+replace(@acccategory,'''','''''')+''',' 


if (@objid='') set @sql = @sql +'0,' else 
begin
if (charindex('ord2_',@objid)>0 or charindex('ord_',@objid)>0) 
set @sql = @sql+replace(@objid,'''','''''')+',' 
else set @sql = @sql+replace(@objid,'''','''''')+',' 
end 
if (@fid='') set @sql = @sql +'0' else 
begin
if (charindex('ord2_',@fid)>0 or charindex('ord_',@fid)>0) 
set @sql = @sql+replace(@fid,'''','''''') 
else set @sql = @sql+replace(@fid,'''','''''') 
end 

set @sql = @sql+'  from '+@tbname1 +'   '  
if (charindex('#detail',@tbname1)>0) 
set @sql=@sql+' where ord2_ordid=ord_id   ' 

 
exec sp_executesql @sql 

create table #dpt(olddpid int , newdpid int) 
create table #act(oldaiid int , newaiid int , bcid int) 
if (exists (select 1 from #dptai where type1='expn' and (cate1='dptf' or cate1='dpt'))) 
begin
insert into #dpt(olddpid,newdpid) select distinct dpid , 0 from #dptai 
insert into #act(oldaiid,newaiid,bcid) select distinct aiid , 0 , dp_bcid  from #dptai , mps_depart where dp_id=dpid 
-- 首先修正，部门是否有预算
update #dpt set newdpid=dp_id from mps_depart where dp_feebudget=0 and dp_id=olddpid 
update #dpt set newdpid=-1*dp_fdpid from mps_depart where dp_id=olddpid and newdpid=0 
update #dpt set newdpid=dp_id from mps_depart where dp_feebudget=0 and dp_id=newdpid*(-1) and  newdpid<0 
update #dpt set newdpid=-1*dp_ffdpid from mps_depart where dp_id=olddpid and newdpid<=0 
update #dpt set newdpid=dp_id from mps_depart where dp_feebudget=0 and dp_id=newdpid*(-1) and  newdpid<0 
update #dpt set newdpid=0 where newdpid<0 
delete #dpt where newdpid=0 
-- 修正费用是否有预算
update #act set newaiid=aib_aiid from mps_account_branch where aib_bcid=bcid and aib_budget=0 and aib_aiid=oldaiid 
update #act set newaiid=-1*ai_faiid  from mps_account_item where ai_id=oldaiid and newaiid=0 
update #act set newaiid=aib_aiid from mps_account_branch where aib_bcid=bcid and aib_budget=0 and aib_aiid=newaiid*(-1) and newaiid<0 
update #act set newaiid=-1*ai_ffaiid  from mps_account_item where ai_id=oldaiid and newaiid<0 
update #act set newaiid=aib_aiid from mps_account_branch where aib_bcid=bcid and aib_budget=0 and aib_aiid=newaiid*(-1) and newaiid<0 
update #act set newaiid=0 where newaiid<0 
delete #act where newaiid=0
end else
begin
insert into #dpt(olddpid,newdpid) select distinct dpid , dpid from #dptai 
insert into #act(oldaiid,newaiid) select distinct aiid , aiid from #dptai 
end 

create table #account11(acctype varchar(30),acccategory varchar(30),accdate varchar(30),fiidreal int,fiid int,objidreal bigint,objid bigint,origin int,value int,used int,suspended int,ready int,changed int,available int,actual int)
 
drop table #dptai
set @sql = 'insert into #account11(acctype,acccategory,accdate,fiidreal,fiid,objidreal,objid,origin,value,used,suspended,ready,changed) '+
'select ' 

if (charindex('ord2_',@acctype)>0 or charindex('ord_',@acctype)>0) 
set @sql = @sql+replace(@acctype,'''','''''')+',' 
else set @sql = @sql+''''+replace(@acctype,'''','''''')+''',' 

if (charindex('ord2_',@acccategory)>0 or charindex('ord_',@acccategory)>0) 
set @sql = @sql+replace(@acccategory,'''','''''')+',' 
else set @sql = @sql+''''+replace(@acccategory,'''','''''')+''',' 

if (charindex('ord2_',@accountdate)>0 or charindex('ord_',@accountdate)>0) 
set @sql = @sql+replace(@accountdate,'''','''''')+',' 
else set @sql = @sql+''''+replace(@accountdate,'''','''''')+''',' 

set @sql=@sql+'newaiid,0,newdpid,0,'

set @sql = @sql+'acc_origin,acc_value,acc_used,acc_suspended,acc_ready,acc_changed '+
' from mps_account,#dpt,#act,'+@tbname1 +' where acc_cmpid=ord_cmpid and '  
if (charindex('#detail',@tbname1)>0) 
set @sql=@sql+' ord2_ordid=ord_id and ' 

if (charindex('ord2_',@accountdate)>0 or charindex('ord_',@accountdate)>0) 
set @sql=@sql+' acc_date like '+replace(@accountdate,'''','''''')+' and ' else 
set @sql=@sql+' acc_date like '''+replace(@accountdate,'''','''''')+''' and ' 

set @sql=@sql+' acc_type=' 
if (charindex('ord2_',@acctype)>0 or charindex('ord_',@acctype)>0) 
set @sql=@sql+replace(@acctype,'''','''''')+' and ' else set @sql=@sql+''''+replace(@acctype,'''','''''')+''' and ' 

set @sql=@sql+' acc_category=' 
if (charindex('ord2_',@acccategory)>0 or charindex('ord_',@acccategory)>0) 
set @sql=@sql+replace(@acccategory,'''','''''')+' and ' else set @sql=@sql+''''+replace(@acccategory,'''','''''')+''' and ' 

set @sql=@sql+' acc_fiid=newaiid and oldaiid=' 
if (charindex('ord2_',@fid)>0 or charindex('ord_',@fid)>0) 
set @sql=@sql+replace(@fid,'''','''''')+' and ' else set @sql=@sql+replace(@fid,'''','''''')+' and ' 

set @sql=@sql+' acc_objid=newdpid and olddpid=' 
if (charindex('ord2_',@objid)>0 or charindex('ord_',@objid)>0) 
set @sql=@sql+replace(@objid,'''','''''')+'   ' else set @sql=@sql+replace(@objid,'''','''''') 
exec myprint @sql 
exec sp_executesql @sql 

insert into #account(acctype,acccategory,accdate,fiidreal,fiid,objidreal,objid,origin,value,used,suspended,ready,changed)
select acctype,acccategory,accdate,fiidreal,oldaiid,objidreal,olddpid,sum(origin),sum(value),sum(used),sum(suspended),sum(ready),sum(changed)
from #account11,#dpt,#act 
where fiidreal=newaiid and objidreal=newdpid 
group by acctype,acccategory,accdate,fiidreal,oldaiid,objidreal,olddpid  

update #account set available=0,actual=0 
update #account set available=value - used - suspended,actual=value-used 
drop table #account11
drop table #dpt
drop table #act
set nocount off 
end