IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_getaccount_local]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_getaccount_local]
GO
CREATE procedure [dbo].[mps_getaccount_local](
@cmpid int ,
@accountdate varchar(100) ,
@acctype varchar(100) , -- 帐类别  loan , fee ...  
@acccategory varchar(100) , -- 分类，主要是 dpt emp cst .... 
@fid int , -- 费用类别 , 有些是没有费用类别的，则=0   
@objid int -- 责任对象    
)
as 
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

declare @dpt table(olddpid int , newdpid int) 
declare @act table(oldaiid int , newaiid int , bcid int) 
if (@acctype='expn' and (@acccategory='dptf' or @acccategory='dpt')) 
begin
insert into @dpt(olddpid,newdpid) values(@objid,0) 
insert into @act(oldaiid,newaiid,bcid) select @fid ,0 , dp_bcid from mps_depart where dp_id=@objid 
-- 首先修正，部门是否有预算
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 
-- 修正费用是否有预算
select * from @act
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) values(@objid,@objid) 
insert into @act(oldaiid,newaiid) values(@fid,@fid) 
end 
 
select acc_cmpid,acc_type,acc_category,@accountdate acc_date,acc_objid acc_objid_real,acc_fiid acc_fiid_real,olddpid acc_objid,oldaiid acc_fiid , 
sum(convert(decimal(18,2),acc_origin)/100) acc_origin,
sum(convert(decimal(18,2),acc_value)/100) acc_value,
sum(convert(decimal(18,2),acc_used)/100) acc_used,
sum(convert(decimal(18,2),acc_changed)/100) acc_changed,
sum(convert(decimal(18,2),acc_suspended)/100) acc_suspended,
sum(convert(decimal(18,2),acc_ready)/100) acc_ready,
sum(convert(decimal(18,2),acc_value - acc_used - acc_suspended)/100) acc_available , 
sum(convert(decimal(18,2),acc_value-acc_used)/100) acc_real   ,
sum(convert(decimal(18,2),acc_value-acc_used)/100) acc_actual    
from mps_account ,@dpt,@act  
where acc_cmpid=@cmpid and (acc_date like @accountdate)  
and  acc_type=@acctype
and  acc_category=@acccategory
and  acc_fiid=newaiid
and  acc_objid=newdpid  
group by acc_cmpid,acc_type,acc_category, acc_objid,acc_fiid,olddpid,oldaiid
set nocount off
end