资金计划报表sql 希望大家给点意见,指出我的存储过程里面的错误的地方

IF EXISTS (SELECT * FROM sys.procedures WHERE object_id = OBJECT_ID(N'[dbo].[Proc_FM_RollingBudgetRollingCapitalplan]'))
DROP proc [dbo].[Proc_FM_RollingBudgetRollingCapitalplan]
GO

--exec [Proc_FM_RollingBudgetRollingCapitalplan] 6342,2015,5

--exec [Proc_FM_RollingBudgetRollingCapitalplan] 2,2015,3
CREATE proc [dbo].[Proc_FM_RollingBudgetRollingCapitalplan]
@CompanyID int, --填报区域公司
@BudgetYear int,
@Monthpayment int --月付款
as
begin
--@Startversionfirst 开始月第一版,@Startversionlast开始月最后一版
declare @Startversionfirsto int , @Startversionlast int,@versionA int
--@Startversionfirst 结束月第一版,@Startversionlast结束月最后一版
declare @endversionfirst int , @endversionlast int
--@StartMonth 开始月 ,@结束月
declare @StartMonth int, @EndMonth int
select @versionA= mAX(VersionID) from FM_BudgetRollingVersion (nolock) where RecordStatus= 'Approved' and (StartMonth=@Monthpayment or EndMonth=@Monthpayment) and CheckType='A' and BudgetYear= @BudgetYear

--取出查的滚动区间
if( @Monthpayment =1 or @Monthpayment =3 or @Monthpayment =5 or @Monthpayment =7 or @Monthpayment =9 or @Monthpayment =11 )
set @StartMonth=@Monthpayment
else
set @EndMonth=@Monthpayment
if(isnull(@StartMonth,0)>0)
begin
select @Startversionfirsto= min(VersionID) from FM_BudgetRollingVersion (nolock) where RecordStatus= 'Approved' and StartMonth=@StartMonth and CompanyID=@CompanyID and CheckType='B' and BudgetYear= @BudgetYear
select @Startversionlast= max(VersionID) from FM_BudgetRollingVersion (nolock) where RecordStatus= 'Approved' and StartMonth=@StartMonth and CompanyID=@CompanyID and CheckType='B' and BudgetYear= @BudgetYear and VersionID<>@Startversionfirsto

--declare @startcount int
--if object_id('tempdb..#FM_Version') is not null drop table #FM_Version
-- create table #FM_Version ( [id] int identity(1,1), VersionID nvarchar(50))
-- insert into #FM_Version (VersionID) select VersionID from FM_BudgetRollingVersion (nolock) where RecordStatus= 'Approved' and StartMonth=@StartMonth and CompanyID=@CompanyID and CheckType='B' and BudgetYear= @BudgetYear and VersionID <>@Startversionfirsto
-- order by VersionID
-- set @startcount=(select COUNT(VersionID) from #FM_Version )
-- if (isnull(@startcount,0)>1)
-- begin
-- declare @StartMonthSql varchar(1000)
-- declare @i int, @rows int
-- select @i=1
-- select @rows = @@rowcount
-- declare @isver int
-- set @isver=-1
-- while @isver <=0
-- begin
--declare @nowVersionID varchar (22) ,@sql varchar(1000)
--set @StartMonthSql=+'Month'+CAST(@StartMonth+1 as varchar(5))
-- select @nowVersionID=VersionID from #FM_Version where id=@i
-- set @sql ='select ((select COUNT('+@StartMonthSql+') from FM_BudgetRollingItemHistory where RecordStatus=''Active'' and '+@StartMonthSql+'>0 and VersionID='+@nowVersionID+')
-- + (select COUNT('+@StartMonthSql+') from FM_BudgetRollingDtlHistory where RecordStatus=''Active'' and Month6>0 and VersionID= '+@nowVersionID+'))'
-- if object_id('tempdb..#FM_Versionshu') is not null drop table #FM_Versionshu
-- create table #FM_Versionshu ( VersionID nvarchar(50))
-- insert into #FM_Versionshu(VersionID)
-- exec ( @sql)
-- select @isver =VersionID from #FM_Versionshu
-- if object_id('tempdb..#FM_Versionshu') is not null drop table #FM_Versionshu
-- if (isnull(@isver,0)>0)
-- set @Startversionlast= @nowVersionID
-- set @i = @i + 1
-- end
-- end
-- else if(isnull(@startcount,0)=1)
-- set @Startversionlast=(select VersionID from #FM_Version )
--select @Startversionlast;
end
else if (isnull(@EndMonth,0)>0)
begin
select @endversionlast= max(VersionID) from FM_BudgetRollingVersion (nolock) where RecordStatus= 'Approved' and EndMonth=isnull(@EndMonth,0) and CompanyID=isnull(@CompanyID,0) and CheckType='B' and BudgetYear= isnull(@BudgetYear,0)
declare @endcount int
if object_id('tempdb..#FM_endVersion') is not null drop table #FM_endVersion
create table #FM_endVersion ( [id] int identity(1,1), VersionID nvarchar(50))
insert into #FM_endVersion (VersionID) select VersionID from FM_BudgetRollingVersion (nolock) where RecordStatus = 'Approved' and EndMonth=@EndMonth and CompanyID=@CompanyID and CheckType='B' and BudgetYear= @BudgetYear and VersionID <>@endversionlast
order by VersionID

set @endcount=(select COUNT(VersionID) from #FM_endVersion )
if (isnull(@endcount,0)>1)
begin
declare @i int, @rows int
declare @endMonthSql varchar(1000)
declare @j int
select @i=1
declare @isverend int
set @isverend=-1
while isnull(@isverend,0) <=0
begin
declare @endnowVersionID varchar (22) ,@endsql varchar(1000)
set @endMonthSql=+'Month'+CAST(@EndMonth as varchar(5))
select @endnowVersionID=VersionID from #FM_endVersion where id=@i
set @endsql ='select ((select COUNT('+@endMonthSql+') from FM_BudgetRollingItemHistory where RecordStatus=''Active'' and '+@endMonthSql+'>0 and VersionID='+@endnowVersionID+')
+ (select COUNT('+@endMonthSql+') from FM_BudgetRollingDtlHistory where RecordStatus=''Active'' and Month6>0 and VersionID= '+@endnowVersionID+'))'
if object_id('tempdb..#FM_endVersionshu') is not null drop table #FM_endVersionshu
create table #FM_endVersionshu (VersionID nvarchar(50))
insert into #FM_endVersionshu(VersionID)
exec ( @endsql)
select @isverend =VersionID from #FM_endVersionshu
if object_id('tempdb..#FM_Versionshu') is not null drop table #FM_Versionshu
if (isnull(@isverend,0)>0)
set @endversionfirst= @endnowVersionID
set @i = @i + 1
end
end
else if(isnull(@endcount,0)=1)
set @endversionfirst=(select VersionID from #FM_endVersion )

--select @endversionlast,@endversionfirst
end

--开的发成本
if object_id('tempdb..#FM_Cost') is not null drop table #FM_Cost
create table #FM_Cost(NodeID varchar(50) not null,ContractLibID int ,ProjectID int ,CompanyID int,BudgetCodeID int
,Monthpayment int ,Theprojectarea varchar(500),ProjectName varchar(500),Fillinregionalcompany varchar(500)
,DeptName varchar(500),CostCategory varchar(500),TwoCodeName varchar(500),ThreeCodeName varchar(500)
,FourCodeName varchar(500),Tosignsituation varchar(500),SignTime datetime,ContractNo varchar(500)
,ContractAName varchar(500),PartyUnit varchar(5000),PaymentTerms varchar(max)
,ContractAmount decimal(22,2) ,Negotiationchange decimal(22,2),SettlementAmount decimal(22,2)
,Totaloutputvalue decimal(22,2),TotalpayableAmt decimal(22,2),ContractAmountPaid decimal(22,2)
,TotalpayableNoPaidAmt decimal(22,2),Paymentrate decimal(22,2),Paymentplansthismonth decimal(22,2)
,Remark varchar(max),CreateUserID int ,EMPLOYEENAME varchar(100),TwoMonthYearReportingAmt decimal(22,2)
,TwoMonthYearIssuedAmt decimal(22,2),MonthYearFirstIssuedAmt decimal(22,2),AdditionandchangespayAmt decimal(22,2)
,MonthYearlastIssuedAmt decimal(22,2),GrandtotalpayAmt decimal(22,2),ApplyPaymentAmount decimal(22,2)
,Amountpaid decimal(22,2),AvailableBalance decimal(22,2))
--专项管理费用(合同)
if object_id('tempdb..#FM_SpecialExpenses') is not null drop table #FM_SpecialExpenses
create table #FM_SpecialExpenses(NodeID varchar(50) not null,MatterContractID int ,ProjectID int ,CompanyID int,BudgetCodeID int
,Monthpayment int ,Theprojectarea varchar(500),ProjectName varchar(500),Fillinregionalcompany varchar(500)
,DeptName varchar(500),CostCategory varchar(500),TwoCodeName varchar(500),ThreeCodeName varchar(500)
,FourCodeName varchar(500),Tosignsituation varchar(500),SignTime datetime,ContractNo varchar(500)
,ContractAName varchar(500),PartyUnit varchar(5000),PaymentTerms varchar(max)
,ContractAmount decimal(22,2) ,Negotiationchange decimal(22,2),SettlementAmount decimal(22,2)
,Totaloutputvalue decimal(22,2),TotalpayableAmt decimal(22,2),ContractAmountPaid decimal(22,2)
,TotalpayableNoPaidAmt decimal(22,2),Paymentrate decimal(22,2),Paymentplansthismonth decimal(22,2)
,Remark varchar(max),CreateUserID int ,EMPLOYEENAME varchar(100),TwoMonthYearReportingAmt decimal(22,2)
,TwoMonthYearIssuedAmt decimal(22,2),MonthYearFirstIssuedAmt decimal(22,2),AdditionandchangespayAmt decimal(22,2)
,MonthYearlastIssuedAmt decimal(22,2),GrandtotalpayAmt decimal(22,2),ApplyPaymentAmount decimal(22,2)
,Amountpaid decimal(22,2),AvailableBalance decimal(22,2))


--剩余的科目 管理费用,经营收支,投融资流出
if object_id('tempdb..#FM_remainingsubjects') is not null drop table #FM_remainingsubjects
create table #FM_remainingsubjects(NodeID varchar(50)not null,MatterContractID int ,ProjectID int ,CompanyID int,BudgetCodeID int
,Monthpayment int ,Theprojectarea varchar(500),ProjectName varchar(500),Fillinregionalcompany varchar(500)
,DeptName varchar(500),CostCategory varchar(500),TwoCodeName varchar(500),ThreeCodeName varchar(500)
,FourCodeName varchar(500),Tosignsituation varchar(500),SignTime datetime,ContractNo varchar(500)
,ContractAName varchar(500),PartyUnit varchar(5000),PaymentTerms varchar(max)
,ContractAmount decimal(22,2) ,Negotiationchange decimal(22,2) ,SettlementAmount decimal(22,2)
,Totaloutputvalue decimal(22,2),TotalpayableAmt decimal(22,2),ContractAmountPaid decimal(22,2)
,TotalpayableNoPaidAmt decimal(22,2),Paymentrate decimal(22,2),Paymentplansthismonth decimal(22,2)
,Remark varchar(max),CreateUserID int ,EMPLOYEENAME varchar(100),TwoMonthYearReportingAmt decimal(22,2)
,TwoMonthYearIssuedAmt decimal(22,2),MonthYearFirstIssuedAmt decimal(22,2),AdditionandchangespayAmt decimal(22,2)
,MonthYearlastIssuedAmt decimal(22,2),GrandtotalpayAmt decimal(22,2),ApplyPaymentAmount decimal(22,2)
,Amountpaid decimal(22,2),AvailableBalance decimal(22,2))

-- 薪酬福利
if object_id('tempdb..#FM_Salaries') is not null drop table #FM_Salaries
create table #FM_Salaries(NodeID varchar(50) not null,MatterContractID int ,ProjectID int ,CompanyID int,BudgetCodeID int
,Monthpayment int ,Theprojectarea varchar(500),ProjectName varchar(500),Fillinregionalcompany varchar(500)
,DeptName varchar(500),CostCategory varchar(500),TwoCodeName varchar(500),ThreeCodeName varchar(500)
,FourCodeName varchar(500),Tosignsituation varchar(500) ,SignTime datetime,ContractNo varchar(500)
,ContractAName varchar(500),PartyUnit varchar(5000),PaymentTerms varchar(max)
,ContractAmount decimal(22,2) ,Negotiationchange decimal(22,2),SettlementAmount decimal(22,2)
,Totaloutputvalue decimal(22,2),TotalpayableAmt decimal(22,2),ContractAmountPaid decimal(22,2)
,TotalpayableNoPaidAmt decimal(22,2),Paymentrate decimal(22,2),Paymentplansthismonth decimal(22,2)
,Remark varchar(max),CreateUserID int ,EMPLOYEENAME varchar(100),TwoMonthYearReportingAmt decimal(22,2)
,TwoMonthYearIssuedAmt decimal(22,2),MonthYearFirstIssuedAmt decimal(22,2),AdditionandchangespayAmt decimal(22,2)
,MonthYearlastIssuedAmt decimal(22,2),GrandtotalpayAmt decimal(22,2),ApplyPaymentAmount decimal(22,2)
,Amountpaid decimal(22,2),AvailableBalance decimal(22,2))

if object_id('tempdb..#FM_BudgetRollingItemHistory') is not null drop table #FM_BudgetRollingItemHistory
create table #FM_BudgetRollingItemHistory(BudgetRollingItemID int,BudgetRollingID int
,CompanyID int ,BudgetCodeID int,ProjectID int, DeptID int,
TotalActualAmt decimal(22,2),Month1 decimal(22,2),Month2 decimal(22,2),Month3 decimal(22,2),
Month4 decimal(22,2),Month5 decimal(22,2),Month6 decimal(22,2),Month7 decimal(22,2),
Month8 decimal(22,2),Month9 decimal(22,2),Month10 decimal(22,2),Month11 decimal(22,2),
Month12 decimal(22,2),IsCurrent varchar(10) ,ParentID int , CreateUserID int ,
ContractAmt decimal(22,2),PaidAmt decimal(22,2),YearAmt decimal(22,2),Remark varchar(max), VersionID int,
Totaloutputvalue decimal(22,2),TotalpayableAmt decimal(22,2),TotalpayableNoPaidAmt decimal(22,2),
ContractedArea decimal(22,2),SignedTaoShu decimal(22,2), SignedAmt decimal(22,2))
insert into #FM_BudgetRollingItemHistory(BudgetRollingItemID ,BudgetRollingID
,CompanyID ,BudgetCodeID ,ProjectID ,DeptID ,
TotalActualAmt ,Month1 ,Month2 ,Month3 ,
Month4 ,Month5 ,Month6 ,Month7 ,
Month8 ,Month9 ,Month10 ,Month11 ,
Month12 ,IsCurrent ,ParentID , CreateUserID ,
ContractAmt ,PaidAmt ,YearAmt ,Remark , VersionID ,
Totaloutputvalue ,TotalpayableAmt ,TotalpayableNoPaidAmt ,
ContractedArea ,SignedTaoShu , SignedAmt )
SELECT ih.BudgetRollingItemID ,ih.BudgetRollingID
,ih.CompanyID ,ih.BudgetCodeID ,ih.ProjectID , ih.DeptID ,
ih.TotalActualAmt ,ih.Month1 ,ih.Month2 ,ih.Month3 ,
ih.Month4 ,ih.Month5 ,ih.Month6 ,ih.Month7 ,
ih.Month8 ,ih.Month9 ,ih.Month10 ,ih.Month11 ,
ih.Month12 ,ih.IsCurrent ,ih.ParentID , ih.CreateUserID ,
ih.ContractAmt ,ih.PaidAmt ,ih.YearAmt ,ih.Remark , ih.VersionID ,
ih.Totaloutputvalue ,ih.TotalpayableAmt ,ih.TotalpayableNoPaidAmt ,
ih.ContractedArea ,ih.SignedTaoShu ,ih.SignedAmt
FROM dbo.FM_BudgetRollingItemHistory (nolock) ih
inner join FM_BudgetRollingHistory (nolock) rh on ih.BudgetRollingID=rh.BudgetRollingID and ih.VersionID=rh.VersionID and ih.CompanyID=rh.CompanyID
where rh.RecordStatus='Approved' and ih.RecordStatus='active' and rh.CompanyID=@CompanyID and --rh.VersionID=@endversionlast and rh.EndMonth=@Monthpayment
(rh.VersionID=isnull(@Startversionfirsto,0) or rh.VersionID=isnull( @Startversionlast,0) or rh.VersionID= isnull( @endversionfirst,0) or rh.VersionID= isnull( @endversionlast,0) or rh.VersionID=ISNULL(@versionA,0))
and (rh.StartMonth=isnull(@Monthpayment,0) or rh.EndMonth=@Monthpayment)
-- select COUNT(BudgetRollingItemID) from #FM_BudgetRollingItemHistory
--select @Startversionfirsto, @Startversionlast, @endversionfirst,@endversionlast, @Monthpayment;
if object_id('tempdb..#FM_BudgetRollingItem') is not null drop table #FM_BudgetRollingItem
create table #FM_BudgetRollingItem(BudgetRollingItemID int,BudgetRollingID int
,CompanyID int ,BudgetCodeID int,ProjectID int, DeptID int,
TotalActualAmt decimal(22,2),Month1 decimal(22,2),Month2 decimal(22,2),Month3 decimal(22,2),
Month4 decimal(22,2),Month5 decimal(22,2),Month6 decimal(22,2),Month7 decimal(22,2),
Month8 decimal(22,2),Month9 decimal(22,2),Month10 decimal(22,2),Month11 decimal(22,2),
Month12 decimal(22,2),IsCurrent varchar(10) ,ParentID int , CreateUserID int ,
ContractAmt decimal(22,2),PaidAmt decimal(22,2),YearAmt decimal(22,2),Remark varchar(max),
Totaloutputvalue decimal(22,2),TotalpayableAmt decimal(22,2),TotalpayableNoPaidAmt decimal(22,2),
ContractedArea decimal(22,2),SignedTaoShu decimal(22,2), SignedAmt decimal(22,2))
insert into #FM_BudgetRollingItem(BudgetRollingItemID ,BudgetRollingID
,CompanyID ,BudgetCodeID ,ProjectID ,DeptID ,
TotalActualAmt ,Month1 ,Month2 ,Month3 ,
Month4 ,Month5 ,Month6 ,Month7 ,
Month8 ,Month9 ,Month10 ,Month11 ,
Month12 ,IsCurrent ,ParentID , CreateUserID ,
ContractAmt ,PaidAmt ,YearAmt ,Remark ,
Totaloutputvalue ,TotalpayableAmt ,TotalpayableNoPaidAmt ,
ContractedArea ,SignedTaoShu , SignedAmt )
SELECT ih.BudgetRollingItemID ,ih.BudgetRollingID
,ih.CompanyID ,ih.BudgetCodeID ,ih.ProjectID , ih.DeptID ,
ih.TotalActualAmt ,ih.Month1 ,ih.Month2 ,ih.Month3 ,
ih.Month4 ,ih.Month5 ,ih.Month6 ,ih.Month7 ,
ih.Month8 ,ih.Month9 ,ih.Month10 ,ih.Month11 ,
ih.Month12 ,ih.IsCurrent ,ih.ParentID , ih.CreateUserID ,
ih.ContractAmt ,ih.PaidAmt ,ih.YearAmt ,ih.Remark ,
ih.Totaloutputvalue ,ih.TotalpayableAmt ,ih.TotalpayableNoPaidAmt ,
ih.ContractedArea ,ih.SignedTaoShu ,ih.SignedAmt
FROM dbo.FM_BudgetRollingItem (nolock) ih
inner join FM_BudgetRolling (nolock) rh on ih.BudgetRollingID=rh.BudgetRollingID and ih.CompanyID=rh.CompanyID
where rh.RecordStatus='Approved' and ih.RecordStatus='active' and rh.CompanyID=@CompanyID and --rh.VersionID=@endversionlast and rh.EndMonth=@Monthpayment
(rh.StartMonth=isnull(@Monthpayment,0) or rh.EndMonth=@Monthpayment)
--select * from #FM_BudgetRollingItem
if object_id('tempdb..#FM_BudgetRollingDtlHistory') is not null drop table #FM_BudgetRollingDtlHistory
create table #FM_BudgetRollingDtlHistory(BudgetRollingDtlID int,BudgetRollingItemID int,
CompanyID int, ProjectID int, BudgeFactorID int,
TotalActualAmt decimal(22,2),Month1 decimal(22,2),Month2 decimal(22,2),Month3 decimal(22,2),Month4 decimal(22,2),
Month5 decimal(22,2),Month6 decimal(22,2),Month7 decimal(22,2),Month8 decimal(22,2),
Month9 decimal(22,2),Month10 decimal(22,2), Month11 decimal(22,2),Month12 decimal(22,2),
IsCurrent varchar(20),ParentID decimal(22,2),CreateUserID int,
BudgetRollingID decimal(22,2),ContractLibID int,BudgetMatterID int,MatterContractID int,PartnerName varchar(500),
BudgetCodeID decimal(22,2),ContractAmt decimal(22,2),PaidAmt decimal(22,2),YearAmt decimal(22,2),
Remark varchar(max),DeptID decimal(22,2),PartnerID int,TypeID int,
VersionID int,
AddNumber int,Totaloutputvalue decimal(22,2),TotalpayableAmt decimal(22,2),TotalpayableNoPaidAmt decimal(22,2))
insert into #FM_BudgetRollingDtlHistory(BudgetRollingDtlID ,BudgetRollingItemID ,
CompanyID , ProjectID , BudgeFactorID ,
TotalActualAmt ,Month1 ,Month2 ,Month3 ,Month4 ,
Month5 ,Month6 ,Month7 ,Month8 ,
Month9 ,Month10 , Month11 ,Month12 ,
IsCurrent ,ParentID ,CreateUserID ,
BudgetRollingID ,ContractLibID ,BudgetMatterID ,MatterContractID ,PartnerName ,
BudgetCodeID ,ContractAmt ,PaidAmt ,YearAmt ,
Remark ,DeptID ,PartnerID ,TypeID ,
VersionID ,
AddNumber ,Totaloutputvalue ,TotalpayableAmt ,TotalpayableNoPaidAmt )
SELECT ih.BudgetRollingDtlID ,ih.BudgetRollingItemID ,
ih.CompanyID , ih.ProjectID , ih.BudgeFactorID ,
ih.TotalActualAmt ,ih.Month1 ,ih.Month2 ,ih.Month3 ,ih.Month4 ,
ih.Month5 ,ih.Month6 ,ih.Month7 ,ih.Month8 ,
ih.Month9 ,ih.Month10 , ih.Month11 ,ih.Month12 ,
ih.IsCurrent ,ih.ParentID ,ih.CreateUserID ,
ih.BudgetRollingID ,ih.ContractLibID ,ih.BudgetMatterID ,ih.MatterContractID ,ih.PartnerName ,
ih.BudgetCodeID ,ih.ContractAmt ,ih.PaidAmt ,ih.YearAmt ,
ih.Remark ,ih.DeptID ,ih.PartnerID ,ih.TypeID ,
ih.VersionID ,
ih.AddNumber ,ih.Totaloutputvalue ,ih.TotalpayableAmt ,ih.TotalpayableNoPaidAmt
FROM dbo.FM_BudgetRollingDtlHistory (nolock) ih
inner join FM_BudgetRollingHistory (nolock) rh on ih.BudgetRollingID=rh.BudgetRollingID and ih.VersionID=rh.VersionID and ih.CompanyID=rh.CompanyID
where rh.RecordStatus='Approved' and ih.RecordStatus='active' and rh.CompanyID=@CompanyID and --rh.VersionID=@endversionlast and rh.EndMonth=@Monthpayment
(rh.VersionID=isnull( @Startversionfirsto,0) or rh.VersionID=isnull( @Startversionlast,0) or rh.VersionID= isnull( @endversionfirst,0) or rh.VersionID= isnull( @endversionlast,0)or rh.VersionID=ISNULL(@versionA,0))
and (rh.StartMonth=isnull(@Monthpayment,0) or rh.EndMonth=isnull(@Monthpayment,0))
-- select COUNT(BudgetRollingItemID) from #FM_BudgetRollingDtlHistory
--select @Startversionfirsto, @Startversionlast, @endversionfirst,@endversionlast, @Monthpayment;
--select * from #FM_BudgetRollingItem
if object_id('tempdb..#FM_BudgetRollingDtl') is not null drop table #FM_BudgetRollingDtl
create table #FM_BudgetRollingDtl(BudgetRollingDtlID int,BudgetRollingItemID int,
CompanyID int, ProjectID int, BudgeFactorID int,
TotalActualAmt decimal(22,2),Month1 decimal(22,2),Month2 decimal(22,2),Month3 decimal(22,2),Month4 decimal(22,2),
Month5 decimal(22,2),Month6 decimal(22,2),Month7 decimal(22,2),Month8 decimal(22,2),
Month9 decimal(22,2),Month10 decimal(22,2), Month11 decimal(22,2),Month12 decimal(22,2),
IsCurrent varchar(20),ParentID decimal(22,2),CreateUserID int,
BudgetRollingID decimal(22,2),ContractLibID int,BudgetMatterID int,MatterContractID int,PartnerName varchar(500),
BudgetCodeID decimal(22,2),ContractAmt decimal(22,2),PaidAmt decimal(22,2),YearAmt decimal(22,2),
Remark varchar(max),DeptID decimal(22,2),PartnerID int,TypeID int,
Totaloutputvalue decimal(22,2),TotalpayableAmt decimal(22,2),TotalpayableNoPaidAmt decimal(22,2))
insert into #FM_BudgetRollingDtl(BudgetRollingDtlID ,BudgetRollingItemID ,
CompanyID , ProjectID , BudgeFactorID ,
TotalActualAmt ,Month1 ,Month2 ,Month3 ,Month4 ,
Month5 ,Month6 ,Month7 ,Month8 ,
Month9 ,Month10 , Month11 ,Month12 ,
IsCurrent ,ParentID ,CreateUserID ,
BudgetRollingID ,ContractLibID ,BudgetMatterID ,MatterContractID ,PartnerName ,
BudgetCodeID ,ContractAmt ,PaidAmt ,YearAmt ,
Remark ,DeptID ,PartnerID ,TypeID ,
Totaloutputvalue ,TotalpayableAmt ,TotalpayableNoPaidAmt )
SELECT ih.BudgetRollingDtlID ,ih.BudgetRollingItemID ,
ih.CompanyID , ih.ProjectID , ih.BudgeFactorID ,
ih.TotalActualAmt ,ih.Month1 ,ih.Month2 ,ih.Month3 ,ih.Month4 ,
ih.Month5 ,ih.Month6 ,ih.Month7 ,ih.Month8 ,
ih.Month9 ,ih.Month10 , ih.Month11 ,ih.Month12 ,
ih.IsCurrent ,ih.ParentID ,ih.CreateUserID ,
ih.BudgetRollingID ,ih.ContractLibID ,ih.BudgetMatterID ,ih.MatterContractID ,ih.PartnerName ,
ih.BudgetCodeID ,ih.ContractAmt ,ih.PaidAmt ,ih.YearAmt ,
ih.Remark ,ih.DeptID ,ih.PartnerID ,ih.TypeID ,
ih.Totaloutputvalue ,ih.TotalpayableAmt ,ih.TotalpayableNoPaidAmt
FROM dbo.FM_BudgetRollingDtl (nolock) ih
inner join FM_BudgetRolling(nolock) rh on ih.BudgetRollingID=rh.BudgetRollingID and ih.CompanyID=rh.CompanyID
where rh.RecordStatus='Approved' and ih.RecordStatus='active' and rh.CompanyID=@CompanyID and --rh.VersionID=@endversionlast and rh.EndMonth=@Monthpayment
(rh.StartMonth=isnull(@Monthpayment,0) or rh.EndMonth=isnull(@Monthpayment,0))

if object_id('tempdb..#FM_BudgetRollingHistory') is not null drop table #FM_BudgetRollingHistory
create table #FM_BudgetRollingHistory(BudgetRollingID int,CompanyID int,
ProjectID int, DeptID int, BudgetType nvarchar(50),BudgetYear int,StartMonth int ,EndMonth int,
Remark nvarchar(max),InchargeByID int ,RecordStatus nvarchar(50),BudgetID int ,IsHQReject nvarchar(50),
IsPublish nvarchar(5),VersionID int ,OldBudgetID int ,RefcordId int ,IfIssued nvarchar(50),AddNumber int)
insert into #FM_BudgetRollingHistory(
BudgetRollingID,CompanyID,ProjectID,DeptID,BudgetType,BudgetYear,
StartMonth,EndMonth,Remark,InchargeByID,RecordStatus,BudgetID,IsHQReject,
IsPublish,VersionID,OldBudgetID,RefcordId,IfIssued,AddNumber)
SELECT BudgetRollingID,CompanyID,ProjectID,DeptID,BudgetType,BudgetYear,
StartMonth,EndMonth,Remark,InchargeByID,RecordStatus,BudgetID,IsHQReject,
IsPublish,VersionID,OldBudgetID,RefcordId,IfIssued,AddNumber
FROM FM_BudgetRollingHistory (nolock)
where RecordStatus='Approved' and CompanyID=@CompanyID
and (VersionID=isnull(@Startversionfirsto,0) or VersionID=isnull(@Startversionlast,0) or VersionID= isnull(@endversionfirst,0) or VersionID= isnull(@endversionlast,0) or VersionID=ISNULL(@versionA,0))
and (StartMonth=isnull(@Monthpayment,0) or EndMonth=isnull(@Monthpayment,0))
and BudgetYear=isnull(@BudgetYear,0)


if object_id('tempdb..#FM_ContractLib') is not null drop table #FM_ContractLib
create table #FM_ContractLib(ContractLibID int,Remark nvarchar(max),
BudgetYear int, ContractNo varchar(5000), ContractAName varchar(5000), PartyUnit varchar(5000), ContractAmount decimal(22,2),
ContractAmountPaid decimal(22,2),VirtualcontractId varchar(500) ,FormalcontractId varchar(500), Obsolete nvarchar(50) , SignTime datetime
,CreateUserID int ,Tosignsituation NVARCHAR (30),PaymentTerms NVARCHAR (max) ,Negotiationchange DECIMAL (22,2),
SettlementAmount DECIMAL (22,2) ,Paymentrate DECIMAL (10,2) ,PaymentTermsmain NVARCHAR (max) ,IsInstallment NVARCHAR (30) ,
)
insert into #FM_ContractLib(
ContractLibID,Remark,BudgetYear,ContractNo,ContractAName,PartyUnit,ContractAmount,ContractAmountPaid,
VirtualcontractId,FormalcontractId,Obsolete,SignTime
,CreateUserID,Tosignsituation ,PaymentTerms ,Negotiationchange ,
SettlementAmount ,Paymentrate ,PaymentTermsmain,IsInstallment)
SELECT ContractLibID,Remark,BudgetYear,ContractNo,ContractAName,PartyUnit,ContractAmount,ContractAmountPaid,
VirtualcontractId,FormalcontractId,Obsolete,SignTime
,CreateUserID,Tosignsituation,PaymentTerms ,Negotiationchange ,
SettlementAmount,Paymentrate,PaymentTermsmain,IsInstallment
FROM FM_ContractLib (nolock)
where ContractLibName NOT LIKE '%(历史)' AND RecordStatus='Active'


--处理导出是开始月的
if(isnull(@StartMonth,0)>0)
begin
--处理可支付最后打版
if (isnull(@Startversionlast,0)>0)
begin

--select @StartMonth,@Startversionlast,@Startversionfirsto,@endversionfirst,@endnowVersionID,@endversionlast
--开的发成本
insert into #FM_Cost(NodeID ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cpy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime ,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms
,isnull(clib.ContractAmount,'0.00') as ContractAmount ,isnull(clib.Negotiationchange,'0.00') as Negotiationchange
,isnull(clib.SettlementAmount,'0.00') as SettlementAmount ,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue
, isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
, 0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearlastIssuedAmt -- 最后一版可支付
,0 as GrandtotalpayAmt
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
--into #FM_Cost
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@Startversionlast
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'

UNION ALL --可支付第一版

SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cpy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@Startversionfirsto
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
UNION ALL --双月预算下发金额

SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cpy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'

UNION ALL --双月上报

SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cpy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cpy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
, case when @StartMonth=1 then isnull(vfda.sjMonth1,'0.00')
when @StartMonth=3 then isnull(vfda.sjMonth3,'0.00')
when @StartMonth=5 then isnull(vfda.sjMonth5,'0.00')
when @StartMonth=7 then isnull(vfda.sjMonth7,'0.00')
when @StartMonth=2 then isnull(vfda.sjMonth2,'0.00')
when @StartMonth=4 then isnull(vfda.sjMonth4,'0.00')
when @StartMonth=6 then isnull(vfda.sjMonth6,'0.00')
when @StartMonth=8 then isnull(vfda.sjMonth8,'0.00')
when @StartMonth=9 then isnull(vfda.sjMonth9,'0.00')
when @StartMonth=10 then isnull(vfda.sjMonth10,'0.00')
when @StartMonth=11 then isnull(vfda.sjMonth11,'0.00')
else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
--实际
LEFT JOIN VIEW_FM_DevelopmentCost_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND vfda.BudgetYear=FBRH.BudgetYear
AND FBRDH.BudgetCodeID=vfda.BudgetCodeID AND FBRDH.ContractLibID=vfda.ContractLibID
--冻结
LEFT JOIN FM_ActualCostItemDtlFromSystemDevelopmentCostView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@StartMonth
AND FBRDH.BudgetCodeID=vfds.BudgetCodeID AND FBRDH.ContractLibID=vfds.ContractCode
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@Startversionlast
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
--专项管理费用(合同)
insert into #FM_SpecialExpenses(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime ,isnull(CMT.ContractNo,'') as ContractNo
,isnull(CMT.ContractAName,'') as ContractAName ,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms
,isnull(CMT.ContractAmount,'0.00') as ContractAmount ,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange
,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount ,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue
, isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
, 0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearlastIssuedAmt -- 最后一半可支付
,0 as GrandtotalpayAmt
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
and FBRH.VersionID=isnull(@Startversionlast,0)
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

UNION ALL --可支付第一版

SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
and FBRH.VersionID=@Startversionfirsto
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
UNION ALL --双月预算下发金额

SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
,case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
UNION ALL --双月上报

SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
, case when @StartMonth=1 then isnull(vfda.sjMonth1,'0.00')
when @StartMonth=3 then isnull(vfda.sjMonth3,'0.00')
when @StartMonth=5 then isnull(vfda.sjMonth5,'0.00')
when @StartMonth=7 then isnull(vfda.sjMonth7,'0.00')
when @StartMonth=2 then isnull(vfda.sjMonth2,'0.00')
when @StartMonth=4 then isnull(vfda.sjMonth4,'0.00')
when @StartMonth=6 then isnull(vfda.sjMonth6,'0.00')
when @StartMonth=8 then isnull(vfda.sjMonth8,'0.00')
when @StartMonth=9 then isnull(vfda.sjMonth9,'0.00')
when @StartMonth=10 then isnull(vfda.sjMonth10,'0.00')
when @StartMonth=11 then isnull(vfda.sjMonth11,'0.00')
else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
--实际
LEFT JOIN VIEW_FM_SpecialExpenses_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID AND FBRDH.MatterContractID=vfda.MatterContractID
--冻结
LEFT JOIN FM_ActualCostItemDtlFromSystemSpecialExpensesView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@StartMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID AND FBRDH.MatterContractID=vfds.ContractCode
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
and FBRH.VersionID=@Startversionlast
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth


-- --预算外
-- insert into #FM_SpecialExpenses(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
-- ,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
-- ,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
-- ,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
-- ,ContractAName ,PartyUnit ,PaymentTerms
-- ,ContractAmount ,Negotiationchange ,SettlementAmount
-- ,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
-- ,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
-- ,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
-- ,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
-- ,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
-- ,Amountpaid ,AvailableBalance)
-- SELECT (cast(vfda.BudgetCodeID as varchar(22))+'_'+cast(vfda.MatterContractID as varchar(22))+'_'+cast(vfda.DeptID as varchar(22))) as NodeID
-- ,CMT.MatterContractID , 0 as ProjectID ,vfda.CompanyID ,vfda.BudgetCodeID
-- ,@StartMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea , '' as ProjectName
-- ,'' as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo
-- ,isnull(CMT.ContractAName,'') as ContractAName ,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms
-- ,isnull(CMT.ContractAmount,'0.00') as ContractAmount ,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange
-- ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount ,0 as Totaloutputvalue
-- ,0 as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
-- ,0 as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
-- , 0 as Paymentplansthismonth,0 as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
-- ,'' as EMPLOYEENAME
-- ,0 as TwoMonthYearReportingAmt--双月预算上报金额
-- , 0 as TwoMonthYearIssuedAmt--双月预算下发金额
-- ,0 as MonthYearFirstIssuedAmt --可支付下发金额
-- , 0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- , 0 as MonthYearlastIssuedAmt -- 最后一半可支付
-- ,0 as GrandtotalpayAmt
-- ,case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
-- else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
-- ,case when @StartMonth=1 then isnull(vfda.sjMonth1,'0.00')
-- when @StartMonth=3 then isnull(vfda.sjMonth3,'0.00')
-- when @StartMonth=5 then isnull(vfda.sjMonth5,'0.00')
-- when @StartMonth=7 then isnull(vfda.sjMonth7,'0.00')
-- when @StartMonth=2 then isnull(vfda.sjMonth2,'0.00')
-- when @StartMonth=4 then isnull(vfda.sjMonth4,'0.00')
-- when @StartMonth=6 then isnull(vfda.sjMonth6,'0.00')
-- when @StartMonth=8 then isnull(vfda.sjMonth8,'0.00')
-- when @StartMonth=9 then isnull(vfda.sjMonth9,'0.00')
-- when @StartMonth=10 then isnull(vfda.sjMonth10,'0.00')
-- when @StartMonth=11 then isnull(vfda.sjMonth11,'0.00')
-- else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from VIEW_FM_SpecialExpenses_Actual(nolock) vfda
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=vfda.CompanyID and cy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=vfda.DEPTID and dt.RecordStatus='active'
-- inner join FM_MatterContract(nolock) CMT on vfda.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
-- inner join FM_BudgetCode(nolock) bc on vfda.BudgetCodeID=bc.BudgetCodeID and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on bg3.BudgetCodeID=vfda.BudgetCodeID and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=vfda.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=vfda.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- LEFT JOIN FM_ActualCostItemDtlFromSystemSpecialExpensesView vfds ON vfda.CompanyID=vfds.CompanyID
-- AND vfda.DeptID=vfds.DeptID AND vfda.BudgetType=vfds.BudgetType
-- AND vfda.BudgetYear=vfds.BudgetYear AND vfds.monthly=@EndMonth
-- AND vfda.BudgetCodeID=vfds.BudgetCodeID AND vfda.MatterContractID=vfds.ContractCode

-- WHERE vfda.BudgetType='SpecialExpenses'
-- and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
-- AND (vfda.CompanyID=@CompanyId or @CompanyId=0)
-- AND vfda.BudgetYear=@BudgetYear
-- and( vfda.MatterContractID not in (select MatterContractID from #FM_SpecialExpenses (nolock))
-- or vfda.BudgetCodeID not in(select BudgetCodeID from #FM_SpecialExpenses (nolock)))
-- --select * from #FM_SpecialExpenses

-- UNION ALL


-- SELECT
-- (cast(vfds.BudgetCodeID as varchar(22))+'_'+cast(vfds.ContractCode as varchar(22))+'_'+cast(vfds.DeptID as varchar(22))) as NodeID,
-- vfda.MatterContractID ,0 as ProjectID ,vfda.CompanyID ,vfda.BudgetCodeID
-- ,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
-- ,isnull(bg1.CodeName,'') as CostCategory ,isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName,isnull(CMT.ContractNo,'') as ContractNo
-- ,isnull(CMT.ContractAName,'') as ContractAName ,isnull(CMT.PartyUnit,'') as PartyUnit ,isnull(CMT.ContractAmount,'0.00') as ContractAmount
-- ,0 as SumBudgetRollingAmt
-- ,0 as StartMonthBudgetRollingAmt
-- ,0 as EndMonthBudgetRollingAmt --双月预算上报金额
-- ,0 as SumBudgetRollingissuedAmt ,0 as StartMonthBudgetRollingissuedAmt ,0 as EndMonthBudgetRollingissuedAmt --双月预算下发金额
-- ,0 as SumpayBudgetRollingAmt
-- ,0 as StartMonthpayBudgetRollingAmt
-- ,0 as EndMonthpayBudgetRollingAmt -- 可支付下发金额
-- ,0 as SumpayBudgetRollingchangesAmt ,0 as StartMonthpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as SumcumulativepayBudgetRollingAmt
-- ,0 as StartMonthcumulativepayBudgetRollingAmt
-- ,0 as EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
-- ,0 as SumHavecancelafterverificationAmt
-- ,case when @StartMonth=1 then isnull(vfda.sjMonth1,'0.00')
-- when @StartMonth=3 then isnull(vfda.sjMonth3,'0.00')
-- when @StartMonth=5 then isnull(vfda.sjMonth5,'0.00')
-- when @StartMonth=7 then isnull(vfda.sjMonth7,'0.00')
-- when @StartMonth=2 then isnull(vfda.sjMonth2,'0.00')
-- when @StartMonth=4 then isnull(vfda.sjMonth4,'0.00')
-- when @StartMonth=6 then isnull(vfda.sjMonth6,'0.00')
-- when @StartMonth=8 then isnull(vfda.sjMonth8,'0.00')
-- when @StartMonth=9 then isnull(vfda.sjMonth9,'0.00')
-- when @StartMonth=10 then isnull(vfda.sjMonth10,'0.00')
-- when @StartMonth=11 then isnull(vfda.sjMonth11,'0.00')
-- else isnull(vfda.sjMonth12,'0.00') end as StartMonthHavecancelafterverificationAmt
-- ,case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
-- when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
-- when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
-- when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
-- when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
-- when @EndMonth=4 then isnull(vfda.sjMonth4,'0.00')
-- when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
-- when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
-- when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
-- when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
-- when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
-- else (isnull(vfda.sjMonth12,'0.00')) end as EndMonthHavecancelafterverificationAmt -- 已付款金额(核减) --申请付款金额(冻结)
-- ,0 as SumHavebeenfrozenAmt
-- ,case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
-- else (isnull(vfds.FreeAmt,'0.00')) end as StartHavebeenfrozenAmt
-- ,0 as EndMonthHavebeenfrozenAmt ----申请付款金额(冻结)
-- ,0 as CanpaythebalanceAmt --可用余额
-- from FM_ActualCostItemDtlFromSystemSpecialExpensesView (nolock) vfds
-- LEFT JOIN VIEW_FM_SpecialExpenses_Actual vfda ON vfda.CompanyID=vfds.CompanyID
-- AND vfda.DeptID=vfds.DeptID AND vfda.BudgetType=vfds.BudgetType
-- AND vfda.BudgetYear=vfds.BudgetYear
-- AND vfda.BudgetCodeID=vfds.BudgetCodeID AND vfda.MatterContractID=vfds.ContractCode
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=vfds.CompanyID and cy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=vfds.DEPTID and dt.RecordStatus='active'
-- inner join FM_MatterContract(nolock) CMT on vfds.ContractCode=CMT.MatterContractID and CMT.RecordStatus='active'
-- inner join FM_BudgetCode(nolock) bc on vfds.BudgetCodeID=bc.BudgetCodeID and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on bg3.BudgetCodeID=vfds.BudgetCodeID and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=vfds.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=vfds.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- WHERE vfds.BudgetType='SpecialExpenses'
-- and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
-- AND (vfds.CompanyID=@CompanyId or @CompanyId=0)
-- AND vfds.BudgetYear=@BudgetYear AND vfds.Monthly=@StartMonth --and vfds.ContractCode=1197
--and( vfds.ContractCode not in (select MatterContractID from #FM_SpecialExpenses (nolock))
-- or vfds.BudgetCodeID not in (select BudgetCodeID from #FM_SpecialExpenses (nolock)))
-- UNION ALL
-- --1.2. 获取股份公司下发可支付版本的预算 实际 结束月冻结
-- SELECT
-- (cast(vfds.BudgetCodeID as varchar(22))+'_'+cast(vfds.ContractCode as varchar(22))+'_'+cast(vfds.DeptID as varchar(22))) as NodeID,
-- vfda.MatterContractID ,0 as ProjectID ,vfda.CompanyID ,vfda.BudgetCodeID
-- ,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
-- ,isnull(bg1.CodeName,'') as CostCategory ,isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName,isnull(CMT.ContractNo,'') as ContractNo
-- ,isnull(CMT.ContractAName,'') as ContractAName ,isnull(CMT.PartyUnit,'') as PartyUnit ,isnull(CMT.ContractAmount,'0.00') as ContractAmount
-- , 0 as SumBudgetRollingAmt
-- ,0 as StartMonthBudgetRollingAmt
-- ,0 as EndMonthBudgetRollingAmt --双月预算上报金额
-- ,0 as SumBudgetRollingissuedAmt ,0 as StartMonthBudgetRollingissuedAmt ,0 as EndMonthBudgetRollingissuedAmt --双月预算下发金额
-- ,0 as SumpayBudgetRollingAmt
-- ,0 as StartMonthpayBudgetRollingAmt
-- ,0 as EndMonthpayBudgetRollingAmt -- 可支付下发金额
-- ,0 as SumpayBudgetRollingchangesAmt ,0 as StartMonthpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as SumcumulativepayBudgetRollingAmt
-- ,0 as StartMonthcumulativepayBudgetRollingAmt
-- ,0 as EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
-- ,0 as SumHavecancelafterverificationAmt
-- ,0 as StartMonthHavecancelafterverificationAmt
-- ,0 as EndMonthHavecancelafterverificationAmt --已付款金额(核减)
-- ,0 as SumHavebeenfrozenAmt
-- ,0 as StartHavebeenfrozenAmt
-- ,case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
-- else (isnull(vfds.FreeAmt,'0.00')) end as EndMonthHavebeenfrozenAmt --申请付款金额(冻结
-- ,0 as CanpaythebalanceAmt --可用余额
-- from FM_ActualCostItemDtlFromSystemSpecialExpensesView (nolock) vfds
-- LEFT JOIN VIEW_FM_SpecialExpenses_Actual vfda ON vfds.CompanyID=vfda.CompanyID
-- AND vfds.DeptID=vfda.DeptID AND vfds.BudgetType=vfda.BudgetType
-- AND vfds.BudgetYear=vfda.BudgetYear
-- AND vfds.BudgetCodeID=vfda.BudgetCodeID AND vfds.ContractCode=vfda.MatterContractID
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=vfds.CompanyID and cy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=vfds.DEPTID and dt.RecordStatus='active'
-- inner join FM_MatterContract(nolock) CMT on vfds.ContractCode=CMT.MatterContractID and CMT.RecordStatus='active'
-- inner join FM_BudgetCode(nolock) bc on vfds.BudgetCodeID=bc.BudgetCodeID and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on bg3.BudgetCodeID=vfds.BudgetCodeID and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=vfds.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=vfds.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
-- WHERE vfds.BudgetType='SpecialExpenses'
-- and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
-- AND (vfds.CompanyID=@CompanyId or @CompanyId=0)
-- AND vfds.BudgetYear=@BudgetYear AND vfds.Monthly=@EndMonth --and vfds.ContractCode=1197
-- and( vfds.DeptID <>@DeptID or vfds.ContractCode not in (select MatterContractID from #FM_SpecialExpenses (nolock))
-- or vfds.BudgetCodeID not in(select BudgetCodeID from #FM_SpecialExpenses (nolock)))

-- insert into #FM_remainingsubjects(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
-- ,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
-- ,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
-- ,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
-- ,ContractAName ,PartyUnit ,PaymentTerms
-- ,ContractAmount ,Negotiationchange ,SettlementAmount
-- ,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
-- ,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
-- ,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
-- ,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
-- ,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
-- ,Amountpaid ,AvailableBalance)
-- SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
-- ,@StartMonth as Monthpayment,isnull(cpy.COMPANYNAME,'') as Theprojectarea,isnull(pj.ProjectName,'') as ProjectName
-- ,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,'' as Tosignsituation , null as SignTime,'' as ContractNo
-- ,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
-- ,'0.00' as ContractAmount ,'0.00' as Negotiationchange
-- ,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
-- , '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
-- ,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
-- , 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
-- ,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
-- ,0 as TwoMonthYearReportingAmt--双月预算上报金额
-- , 0 as TwoMonthYearIssuedAmt--双月预算下发金额
-- ,0 as MonthYearFirstIssuedAmt --可支付下发金额
-- , 0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
-- when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
-- when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
-- when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
-- when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
-- when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
-- when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
-- when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
-- when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
-- when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
-- when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
-- else (isnull(FBRIH.Month12,'0.00')) end as MonthYearlastIssuedAmt -- 最后一半可支付
-- ,0 as GrandtotalpayAmt
-- ,0 as ApplyPaymentAmount --申请付款金额(冻结)
-- ,0 as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from #FM_BudgetRollingHistory(nolock) FBRH
-- LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
-- INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID

-- inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
-- left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
-- WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
-- and FBRH.VersionID=@Startversionlast
-- and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'

-- AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
-- AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

-- UNION ALL --可支付第一版

-- SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
-- ,@StartMonth as Monthpayment,isnull(cpy.COMPANYNAME,'') as Theprojectarea,isnull(pj.ProjectName,'') as ProjectName
-- ,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,'' as Tosignsituation , null as SignTime,'' as ContractNo
-- ,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
-- ,'0.00' as ContractAmount ,'0.00' as Negotiationchange
-- ,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
-- , '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
-- ,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
-- , 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
-- ,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
-- ,0 as TwoMonthYearReportingAmt--双月预算上报金额
-- , 0 as TwoMonthYearIssuedAmt--双月预算下发金额
-- ,case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
-- when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
-- when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
-- when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
-- when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
-- when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
-- when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
-- when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
-- when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
-- when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
-- when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
-- else (isnull(FBRIH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
-- ,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as MonthYearlastIssuedAmt
-- ,0 as GrandtotalpayAmt -- 累计可支付金额
-- ,0 as ApplyPaymentAmount --申请付款金额(冻结)
-- ,0 as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from #FM_BudgetRollingHistory(nolock) FBRH
-- LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
-- inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
-- INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID

-- inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
-- left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
-- WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
-- and FBRH.VersionID=@Startversionfirsto
-- and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
-- AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
-- AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

-- UNION ALL --双月预算下发金额

-- SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
-- ,@StartMonth as Monthpayment,isnull(cpy.COMPANYNAME,'') as Theprojectarea,isnull(pj.ProjectName,'') as ProjectName
-- ,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,'' as Tosignsituation , null as SignTime,'' as ContractNo
-- ,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
-- ,'0.00' as ContractAmount ,'0.00' as Negotiationchange
-- ,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
-- , '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
-- ,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
-- , 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
-- ,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
-- ,0 as TwoMonthYearReportingAmt--双月预算上报金额
-- ,case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
-- when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
-- when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
-- when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
-- when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
-- when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
-- when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
-- when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
-- when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
-- when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
-- when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
-- else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
-- ,0 as MonthYearFirstIssuedAmt --可支付下发金额
-- ,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as MonthYearlastIssuedAmt
-- ,0 as GrandtotalpayAmt -- 累计可支付金额
-- ,0 as ApplyPaymentAmount --申请付款金额(冻结)
-- ,0 as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from #FM_BudgetRollingHistory(nolock) FBRH
-- LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
-- inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
-- INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID

-- inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
-- left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
-- WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
-- and FBRH.VersionID=@versionA
-- and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
-- AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
-- AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth


-- UNION ALL --双月上报

-- SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
-- ,@StartMonth as Monthpayment,isnull(cpy.COMPANYNAME,'') as Theprojectarea,isnull(pj.ProjectName,'') as ProjectName
-- ,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,'' as Tosignsituation , null as SignTime,'' as ContractNo
-- ,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
-- ,'0.00' as ContractAmount ,'0.00' as Negotiationchange
-- ,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
-- , '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
-- ,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
-- , 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
-- ,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
-- , case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
-- when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
-- when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
-- when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
-- when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
-- when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
-- when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
-- when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
-- when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
-- when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
-- when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
-- else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
-- , 0 as TwoMonthYearIssuedAmt--双月预算下发金额
-- , 0 as MonthYearFirstIssuedAmt --可支付下发金额
-- ,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as MonthYearlastIssuedAmt
-- ,0 as GrandtotalpayAmt -- 累计可支付金额
-- ,0 as ApplyPaymentAmount --申请付款金额(冻结)
-- ,0 as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from FM_BudgetRolling(nolock) FBRH
-- LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
-- inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
-- INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
-- --INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
-- inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1


-- left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
-- left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
-- WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
-- and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'

-- AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
-- AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

-- UNION ALL
-- --1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
-- SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
-- ,@StartMonth as Monthpayment,isnull(cpy.COMPANYNAME,'') as Theprojectarea,isnull(pj.ProjectName,'') as ProjectName
-- ,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,'' as Tosignsituation , null as SignTime,'' as ContractNo
-- ,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
-- ,'0.00' as ContractAmount ,'0.00' as Negotiationchange
-- ,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
-- , '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
-- ,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
-- , 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
-- ,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
-- ,0 as TwoMonthYearReportingAmt--双月预算上报金额
-- , 0 as TwoMonthYearIssuedAmt--双月预算下发金额
-- , 0 as MonthYearFirstIssuedAmt --可支付下发金额
-- ,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as MonthYearlastIssuedAmt
-- ,0 as GrandtotalpayAmt -- 累计可支付金额
-- ,case when @EndMonth=1 then (isnull(gvfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00'))
-- when @EndMonth=3 then (isnull(Avfda.sjMonth3,'0.00')+isnull(gvfda.sjMonth3,'0.00')+isnull(Avfda.sjMonth3,'0.00'))
-- when @EndMonth=5 then (isnull(Avfda.sjMonth5,'0.00')+isnull(gvfda.sjMonth5,'0.00')+isnull(Avfda.sjMonth5,'0.00'))
-- when @EndMonth=7 then (isnull(Avfda.sjMonth7,'0.00')+isnull(gvfda.sjMonth7,'0.00')+isnull(Avfda.sjMonth7,'0.00'))
-- when @EndMonth=2 then (isnull(Avfda.sjMonth2,'0.00')+isnull(gvfda.sjMonth2,'0.00')+isnull(Avfda.sjMonth2,'0.00'))
-- when @EndMonth=4 then (isnull(Avfda.sjMonth4,'0.00')+isnull(gvfda.sjMonth4,'0.00')+isnull(Avfda.sjMonth4,'0.00'))
-- when @EndMonth=6 then (isnull(Avfda.sjMonth6,'0.00')+isnull(gvfda.sjMonth6,'0.00')+isnull(Avfda.sjMonth6,'0.00'))
-- when @EndMonth=8 then (isnull(Avfda.sjMonth8,'0.00')+isnull(gvfda.sjMonth8,'0.00')+isnull(Avfda.sjMonth8,'0.00'))
-- when @EndMonth=9 then (isnull(Avfda.sjMonth9,'0.00')+isnull(gvfda.sjMonth9,'0.00')+isnull(Avfda.sjMonth9,'0.00'))
-- when @EndMonth=10 then (isnull(Avfda.sjMonth10,'0.00')+isnull(gvfda.sjMonth10,'0.00')+isnull(Avfda.sjMonth10,'0.00'))
-- when @EndMonth=11 then (isnull(Avfda.sjMonth11,'0.00')+isnull(gvfda.sjMonth11,'0.00')+isnull(Avfda.sjMonth11,'0.00'))
-- else (isnull(vfda.sjMonth12,'0.00')+isnull(gvfda.sjMonth12,'0.00')+isnull(Avfda.sjMonth12,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
-- ,case when @StartMonth=1 then (isnull(gvfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00'))
-- when @StartMonth=3 then (isnull(Avfda.sjMonth3,'0.00')+isnull(gvfda.sjMonth3,'0.00')+isnull(Avfda.sjMonth3,'0.00'))
-- when @StartMonth=5 then (isnull(Avfda.sjMonth5,'0.00')+isnull(gvfda.sjMonth5,'0.00')+isnull(Avfda.sjMonth5,'0.00'))
-- when @StartMonth=7 then (isnull(Avfda.sjMonth7,'0.00')+isnull(gvfda.sjMonth7,'0.00')+isnull(Avfda.sjMonth7,'0.00'))
-- when @StartMonth=2 then (isnull(Avfda.sjMonth2,'0.00')+isnull(gvfda.sjMonth2,'0.00')+isnull(Avfda.sjMonth2,'0.00'))
-- when @StartMonth=4 then (isnull(Avfda.sjMonth4,'0.00')+isnull(gvfda.sjMonth4,'0.00')+isnull(Avfda.sjMonth4,'0.00'))
-- when @StartMonth=6 then (isnull(Avfda.sjMonth6,'0.00')+isnull(gvfda.sjMonth6,'0.00')+isnull(Avfda.sjMonth6,'0.00'))
-- when @StartMonth=8 then (isnull(Avfda.sjMonth8,'0.00')+isnull(gvfda.sjMonth8,'0.00')+isnull(Avfda.sjMonth8,'0.00'))
-- when @StartMonth=9 then (isnull(Avfda.sjMonth9,'0.00')+isnull(gvfda.sjMonth9,'0.00')+isnull(Avfda.sjMonth9,'0.00'))
-- when @StartMonth=10 then (isnull(Avfda.sjMonth10,'0.00')+isnull(gvfda.sjMonth10,'0.00')+isnull(Avfda.sjMonth10,'0.00'))
-- when @StartMonth=11 then (isnull(vfda.sjMonth11,'0.00')+isnull(gvfda.sjMonth11,'0.00')+isnull(Avfda.sjMonth11,'0.00'))
-- else (isnull(vfda.sjMonth12,'0.00')+isnull(gvfda.sjMonth12,'0.00')+isnull(Avfda.sjMonth12,'0.00')) end as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from #FM_BudgetRollingHistory(nolock) FBRH
-- LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
-- left join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
-- INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
-- --INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
-- --inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
-- inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
-- left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
-- LEFT JOIN FM_Project(nolock) pt ON FBRH.projectid=pt.projectid

-- --实际
-- LEFT JOIN VIEW_FM_PropertyInOut_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
-- AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
-- AND FBRH.BudgetYear=vfda.BudgetYear
-- AND FBRIH.BudgetCodeID=vfda.BudgetCodeID
-- --冻结
-- LEFT JOIN FM_ActualCostItemFromSystemPropertyInOutView vfds ON FBRH.CompanyID=vfds.CompanyID
-- AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
-- AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@StartMonth
-- AND FBRIH.BudgetCodeID=vfds.BudgetCodeID


-- --实际
-- LEFT JOIN VIEW_FM_GeneralExpensest_Actual gvfda ON gvfda.CompanyID=FBRIH.CompanyID
-- AND gvfda.DeptID=FBRIH.DeptID AND gvfda.BudgetType=FBRH.BudgetType
-- AND FBRH.BudgetYear=gvfda.BudgetYear AND FBRIH.BudgetCodeID=gvfda.BudgetCodeID

-- --冻结
-- LEFT JOIN FM_ActualCostItemFromSystemGeneralExpensesView gvfds ON FBRH.CompanyID=gvfds.CompanyID
-- AND FBRH.DeptID=gvfds.DeptID AND FBRH.BudgetType=gvfds.BudgetType
-- AND FBRH.BudgetYear=gvfds.BudgetYear AND gvfds.monthly=@StartMonth
-- AND FBRIH.BudgetCodeID=gvfds.BudgetCodeID

-- --实际
-- LEFT JOIN VIEW_FM_AbsorbInvestment_Actual Avfda ON FBRH.CompanyID=Avfda.CompanyID
-- AND FBRH.DeptID=Avfda.DeptID AND FBRH.BudgetType=Avfda.BudgetType
-- AND FBRH.BudgetYear=Avfda.BudgetYear AND FBRIH.BudgetCodeID=Avfda.BudgetCodeID
-- --冻结
-- LEFT JOIN FM_ActualCostItemFromSystemAbsorbInvestmentView Avfds ON FBRH.CompanyID=Avfds.CompanyID
-- AND FBRH.DeptID=Avfds.DeptID AND FBRH.BudgetType=Avfds.BudgetType AND Avfds.monthly=@StartMonth
-- AND FBRH.BudgetYear=Avfds.BudgetYear AND FBRIH.BudgetCodeID=Avfds.BudgetCodeID

-- WHERE FBRH.RecordStatus='active' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
--AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@Startversionlast
-- and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
--AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

-- --薪酬福利
-- insert into #FM_Salaries(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
-- ,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
-- ,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
-- ,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
-- ,ContractAName ,PartyUnit ,PaymentTerms
-- ,ContractAmount ,Negotiationchange ,SettlementAmount
-- ,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
-- ,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
-- ,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
-- ,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
-- ,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
-- ,Amountpaid ,AvailableBalance)
-- SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
-- ,@StartMonth as Monthpayment ,'' as Theprojectarea, '' as ProjectName
-- ,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,'' as Tosignsituation , null as SignTime,'' as ContractNo
-- ,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
-- ,'0.00' as ContractAmount ,'0.00' as Negotiationchange
-- ,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
-- , '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
-- ,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
-- , 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
-- ,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
-- ,0 as TwoMonthYearReportingAmt--双月预算上报金额
-- , 0 as TwoMonthYearIssuedAmt--双月预算下发金额
-- ,0 as MonthYearFirstIssuedAmt --可支付下发金额
-- , 0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
-- when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
-- when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
-- when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
-- when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
-- when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
-- when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
-- when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
-- when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
-- when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
-- when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
-- else (isnull(FBRIH.Month12,'0.00')) end as MonthYearlastIssuedAmt -- 最后一半可支付
-- ,0 as GrandtotalpayAmt
-- ,0 as ApplyPaymentAmount --申请付款金额(冻结)
-- ,0 as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from #FM_BudgetRollingHistory(nolock) FBRH
-- --LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
-- --LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
-- INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
-- inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
-- left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
-- left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
-- WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
-- and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
-- and FBRH.VersionID=@Startversionlast
-- AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
-- AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

-- UNION ALL --可支付第一版

-- SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
-- ,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
-- ,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,'' as Tosignsituation , null as SignTime,'' as ContractNo
-- ,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
-- ,'0.00' as ContractAmount ,'0.00' as Negotiationchange
-- ,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
-- , '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
-- ,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
-- , 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
-- ,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
-- ,0 as TwoMonthYearReportingAmt--双月预算上报金额
-- , 0 as TwoMonthYearIssuedAmt--双月预算下发金额
-- ,case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
-- when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
-- when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
-- when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
-- when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
-- when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
-- when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
-- when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
-- when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
-- when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
-- when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
-- else (isnull(FBRIH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
-- ,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as MonthYearlastIssuedAmt
-- ,0 as GrandtotalpayAmt -- 累计可支付金额
-- ,0 as ApplyPaymentAmount --申请付款金额(冻结)
-- ,0 as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from #FM_BudgetRollingHistory(nolock) FBRH
-- --LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
-- --inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
-- INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
-- inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
-- left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
-- WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
-- and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
-- and FBRH.VersionID=@Startversionfirsto
-- AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
-- AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

-- UNION ALL --双月预算下发金额

-- SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
-- ,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
-- ,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,'' as Tosignsituation , null as SignTime,'' as ContractNo
-- ,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
-- ,'0.00' as ContractAmount ,'0.00' as Negotiationchange
-- ,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
-- , '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
-- ,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
-- , 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
-- ,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
-- ,0 as TwoMonthYearReportingAmt--双月预算上报金额
-- , case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
-- when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
-- when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
-- when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
-- when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
-- when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
-- when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
-- when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
-- when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
-- when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
-- when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
-- else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
-- ,0 as MonthYearFirstIssuedAmt --可支付下发金额
-- ,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as MonthYearlastIssuedAmt
-- ,0 as GrandtotalpayAmt -- 累计可支付金额
-- ,0 as ApplyPaymentAmount --申请付款金额(冻结)
-- ,0 as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from #FM_BudgetRollingHistory(nolock) FBRH
-- --LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
-- --inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
-- INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
-- inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
-- left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
-- WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
-- and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
-- and FBRH.VersionID=@versionA
-- AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
-- AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

-- UNION ALL --双月上报


-- SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
-- ,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
-- ,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,'' as Tosignsituation , null as SignTime,'' as ContractNo
-- ,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
-- ,'0.00' as ContractAmount ,'0.00' as Negotiationchange
-- ,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
-- , '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
-- ,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
-- , 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
-- ,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
-- , case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
-- when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
-- when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
-- when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
-- when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
-- when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
-- when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
-- when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
-- when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
-- when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
-- when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
-- else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
-- , 0 as TwoMonthYearIssuedAmt--双月预算下发金额
-- , 0 as MonthYearFirstIssuedAmt --可支付下发金额
-- ,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as MonthYearlastIssuedAmt
-- ,0 as GrandtotalpayAmt -- 累计可支付金额
-- ,0 as ApplyPaymentAmount --申请付款金额(冻结)
-- ,0 as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from FM_BudgetRolling(nolock) FBRH
-- --LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
-- --inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
-- --INNER JOIN FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
-- INNER JOIN #FM_BudgetRollingDtl(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRIH.BudgetRollingID
-- inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1


-- left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
-- left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
-- WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
-- and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
-- AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
-- AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

-- UNION ALL
-- --1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
-- SELECT FBRIH.BudgetRollingItemID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
-- ,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
-- ,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
-- , isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
-- ,'' as Tosignsituation , null as SignTime,'' as ContractNo
-- ,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
-- ,'0.00' as ContractAmount ,'0.00' as Negotiationchange
-- ,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
-- , '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
-- ,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
-- , 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
-- ,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
-- ,0 as TwoMonthYearReportingAmt--双月预算上报金额
-- , 0 as TwoMonthYearIssuedAmt--双月预算下发金额
-- , 0 as MonthYearFirstIssuedAmt --可支付下发金额
-- ,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as MonthYearlastIssuedAmt
-- ,0 as GrandtotalpayAmt -- 累计可支付金额
-- ,case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
-- when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
-- else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
-- ,case when @StartMonth=1 then ( isnull(vfda.sjMonth1,'0.00'))
-- when @StartMonth=3 then ( isnull(vfda.sjMonth3,'0.00'))
-- when @StartMonth=5 then ( isnull(vfda.sjMonth5,'0.00'))
-- when @StartMonth=7 then ( isnull(vfda.sjMonth7,'0.00'))
-- when @StartMonth=2 then ( isnull(vfda.sjMonth2,'0.00'))
-- when @StartMonth=4 then ( isnull(vfda.sjMonth4,'0.00'))
-- when @StartMonth=6 then ( isnull(vfda.sjMonth6,'0.00'))
-- when @StartMonth=8 then ( isnull(vfda.sjMonth8,'0.00'))
-- when @StartMonth=9 then ( isnull(vfda.sjMonth9,'0.00'))
-- when @StartMonth=10 then ( isnull(vfda.sjMonth10,'0.00'))
-- when @StartMonth=11 then ( isnull(vfda.sjMonth11,'0.00'))
-- else ( isnull(vfda.sjMonth12,'0.00')) end as Amountpaid --已付款金额(核减)
-- ,0 as AvailableBalance --可用余额
-- from #FM_BudgetRollingHistory(nolock) FBRH
-- --LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
-- --inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
-- --INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
-- INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
-- --inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
-- inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
-- left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
-- LEFT JOIN FM_Project(nolock) pt ON FBRH.projectid=pt.projectid

-- --实际
-- LEFT JOIN VIEW_FM_PropertyInOut_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
-- AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
-- AND FBRH.BudgetYear=vfda.BudgetYear
-- AND FBRIH.BudgetCodeID=vfda.BudgetCodeID
-- --冻结
-- LEFT JOIN FM_ActualCostItemFromSystemPropertyInOutView vfds ON FBRH.CompanyID=vfds.CompanyID
-- AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
-- AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@StartMonth
-- AND FBRIH.BudgetCodeID=vfds.BudgetCodeID
-- WHERE FBRH.RecordStatus='active' AND FBRH.BudgetType='Salaries'
--AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@Startversionfirsto
-- and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
--AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth


end
else if (isnull(@Startversionfirsto,0)>0 and isnull(@Startversionlast,0)<=0)
begin
--开的发成本
insert into #FM_Cost(NodeID ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cpy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(clib.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@Startversionfirsto
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'

UNION ALL --双月预算下发金额
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cpy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(clib.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'


UNION ALL --双月上报

SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cpy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(clib.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
,0 as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cpy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
,0 as Paymentplansthismonth,isnull(clib.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
,0 as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
,case when @StartMonth=1 then isnull(vfda.sjMonth1,'0.00')
when @StartMonth=3 then isnull(vfda.sjMonth3,'0.00')
when @StartMonth=5 then isnull(vfda.sjMonth5,'0.00')
when @StartMonth=7 then isnull(vfda.sjMonth7,'0.00')
when @StartMonth=2 then isnull(vfda.sjMonth2,'0.00')
when @StartMonth=4 then isnull(vfda.sjMonth3,'0.00')
when @StartMonth=6 then isnull(vfda.sjMonth6,'0.00')
when @StartMonth=8 then isnull(vfda.sjMonth8,'0.00')
when @StartMonth=9 then isnull(vfda.sjMonth9,'0.00')
when @StartMonth=10 then isnull(vfda.sjMonth10,'0.00')
when @StartMonth=11 then isnull(vfda.sjMonth11,'0.00')
else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
--实际
LEFT JOIN VIEW_FM_DevelopmentCost_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND vfda.BudgetYear=FBRH.BudgetYear
AND FBRDH.BudgetCodeID=vfda.BudgetCodeID AND FBRDH.ContractLibID=vfda.ContractLibID
--冻结
LEFT JOIN FM_ActualCostItemDtlFromSystemDevelopmentCostView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@StartMonth
AND FBRDH.BudgetCodeID=vfds.BudgetCodeID AND FBRDH.ContractLibID=vfds.ContractCode
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0) and FBRH.VersionID=@Startversionfirsto
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'

--专项管理费用(合同)
insert into #FM_SpecialExpenses(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea ,'' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'

left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and FBRH.VersionID=@Startversionfirsto
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

UNION ALL --双月预算下发金额
SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea ,'' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'

left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and FBRH.VersionID=@versionA
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

UNION ALL --双月上报

SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @StartMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
,case when @StartMonth=1 then ( isnull(vfda.sjMonth1,'0.00'))
when @StartMonth=3 then ( isnull(vfda.sjMonth3,'0.00'))
when @StartMonth=5 then ( isnull(vfda.sjMonth5,'0.00'))
when @StartMonth=7 then ( isnull(vfda.sjMonth7,'0.00'))
when @StartMonth=2 then ( isnull(vfda.sjMonth2,'0.00'))
when @StartMonth=4 then ( isnull(vfda.sjMonth4,'0.00'))
when @StartMonth=6 then ( isnull(vfda.sjMonth6,'0.00'))
when @StartMonth=8 then ( isnull(vfda.sjMonth8,'0.00'))
when @StartMonth=9 then ( isnull(vfda.sjMonth9,'0.00'))
when @StartMonth=10 then ( isnull(vfda.sjMonth10,'0.00'))
when @StartMonth=11 then ( isnull(vfda.sjMonth11,'0.00'))
else ( isnull(vfda.sjMonth12,'0.00')) end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
--实际
LEFT JOIN VIEW_FM_SpecialExpenses_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID AND FBRDH.MatterContractID=vfda.MatterContractID
--冻结
LEFT JOIN FM_ActualCostItemDtlFromSystemSpecialExpensesView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@StartMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID AND FBRDH.MatterContractID=vfds.ContractCode
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and FBRH.VersionID=@Startversionfirsto
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

insert into #FM_remainingsubjects(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea , isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID

inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
and FBRH.VersionID=@Startversionfirsto
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

UNION ALL --双月预算下发金额
SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID

inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
and FBRH.VersionID=@versionA
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

UNION ALL --双月上报

SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
--INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1


left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'

AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @EndMonth=1 then (isnull(gvfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00'))
when @EndMonth=3 then (isnull(Avfda.sjMonth3,'0.00')+isnull(gvfda.sjMonth3,'0.00')+isnull(Avfda.sjMonth3,'0.00'))
when @EndMonth=5 then (isnull(Avfda.sjMonth5,'0.00')+isnull(gvfda.sjMonth5,'0.00')+isnull(Avfda.sjMonth5,'0.00'))
when @EndMonth=7 then (isnull(Avfda.sjMonth7,'0.00')+isnull(gvfda.sjMonth7,'0.00')+isnull(Avfda.sjMonth7,'0.00'))
when @EndMonth=2 then (isnull(Avfda.sjMonth2,'0.00')+isnull(gvfda.sjMonth2,'0.00')+isnull(Avfda.sjMonth2,'0.00'))
when @EndMonth=4 then (isnull(Avfda.sjMonth4,'0.00')+isnull(gvfda.sjMonth4,'0.00')+isnull(Avfda.sjMonth4,'0.00'))
when @EndMonth=6 then (isnull(Avfda.sjMonth6,'0.00')+isnull(gvfda.sjMonth6,'0.00')+isnull(Avfda.sjMonth6,'0.00'))
when @EndMonth=8 then (isnull(Avfda.sjMonth8,'0.00')+isnull(gvfda.sjMonth8,'0.00')+isnull(Avfda.sjMonth8,'0.00'))
when @EndMonth=9 then (isnull(Avfda.sjMonth9,'0.00')+isnull(gvfda.sjMonth9,'0.00')+isnull(Avfda.sjMonth9,'0.00'))
when @EndMonth=10 then (isnull(Avfda.sjMonth10,'0.00')+isnull(gvfda.sjMonth10,'0.00')+isnull(Avfda.sjMonth10,'0.00'))
when @EndMonth=11 then (isnull(Avfda.sjMonth11,'0.00')+isnull(gvfda.sjMonth11,'0.00')+isnull(Avfda.sjMonth11,'0.00'))
else (isnull(vfda.sjMonth12,'0.00')+isnull(gvfda.sjMonth12,'0.00')+isnull(Avfda.sjMonth12,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
,case when @StartMonth=1 then (isnull(gvfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00'))
when @StartMonth=3 then (isnull(Avfda.sjMonth3,'0.00')+isnull(gvfda.sjMonth3,'0.00')+isnull(Avfda.sjMonth3,'0.00'))
when @StartMonth=5 then (isnull(Avfda.sjMonth5,'0.00')+isnull(gvfda.sjMonth5,'0.00')+isnull(Avfda.sjMonth5,'0.00'))
when @StartMonth=7 then (isnull(Avfda.sjMonth7,'0.00')+isnull(gvfda.sjMonth7,'0.00')+isnull(Avfda.sjMonth7,'0.00'))
when @StartMonth=2 then (isnull(Avfda.sjMonth2,'0.00')+isnull(gvfda.sjMonth2,'0.00')+isnull(Avfda.sjMonth2,'0.00'))
when @StartMonth=4 then (isnull(Avfda.sjMonth4,'0.00')+isnull(gvfda.sjMonth4,'0.00')+isnull(Avfda.sjMonth4,'0.00'))
when @StartMonth=6 then (isnull(Avfda.sjMonth6,'0.00')+isnull(gvfda.sjMonth6,'0.00')+isnull(Avfda.sjMonth6,'0.00'))
when @StartMonth=8 then (isnull(Avfda.sjMonth8,'0.00')+isnull(gvfda.sjMonth8,'0.00')+isnull(Avfda.sjMonth8,'0.00'))
when @StartMonth=9 then (isnull(Avfda.sjMonth9,'0.00')+isnull(gvfda.sjMonth9,'0.00')+isnull(Avfda.sjMonth9,'0.00'))
when @StartMonth=10 then (isnull(Avfda.sjMonth10,'0.00')+isnull(gvfda.sjMonth10,'0.00')+isnull(Avfda.sjMonth10,'0.00'))
when @StartMonth=11 then (isnull(vfda.sjMonth11,'0.00')+isnull(gvfda.sjMonth11,'0.00')+isnull(Avfda.sjMonth11,'0.00'))
else (isnull(vfda.sjMonth12,'0.00')+isnull(gvfda.sjMonth12,'0.00')+isnull(Avfda.sjMonth12,'0.00')) end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
left join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
--INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
--inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
LEFT JOIN FM_Project(nolock) pt ON FBRH.projectid=pt.projectid

--实际
LEFT JOIN VIEW_FM_PropertyInOut_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID
--冻结
LEFT JOIN FM_ActualCostItemFromSystemPropertyInOutView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@StartMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID


--实际
LEFT JOIN VIEW_FM_GeneralExpensest_Actual gvfda ON gvfda.CompanyID=FBRIH.CompanyID
AND gvfda.DeptID=FBRIH.DeptID AND gvfda.BudgetType=FBRH.BudgetType
AND FBRH.BudgetYear=gvfda.BudgetYear AND FBRIH.BudgetCodeID=gvfda.BudgetCodeID

--冻结
LEFT JOIN FM_ActualCostItemFromSystemGeneralExpensesView gvfds ON FBRH.CompanyID=gvfds.CompanyID
AND FBRH.DeptID=gvfds.DeptID AND FBRH.BudgetType=gvfds.BudgetType
AND FBRH.BudgetYear=gvfds.BudgetYear AND gvfds.monthly=@StartMonth
AND FBRIH.BudgetCodeID=gvfds.BudgetCodeID

--实际
LEFT JOIN VIEW_FM_AbsorbInvestment_Actual Avfda ON FBRH.CompanyID=Avfda.CompanyID
AND FBRH.DeptID=Avfda.DeptID AND FBRH.BudgetType=Avfda.BudgetType
AND FBRH.BudgetYear=Avfda.BudgetYear AND FBRIH.BudgetCodeID=Avfda.BudgetCodeID
--冻结
LEFT JOIN FM_ActualCostItemFromSystemAbsorbInvestmentView Avfds ON FBRH.CompanyID=Avfds.CompanyID
AND FBRH.DeptID=Avfds.DeptID AND FBRH.BudgetType=Avfds.BudgetType AND Avfds.monthly=@StartMonth
AND FBRH.BudgetYear=Avfds.BudgetYear AND FBRIH.BudgetCodeID=Avfds.BudgetCodeID

WHERE FBRH.RecordStatus='active' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@Startversionfirsto
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

--薪酬福利
insert into #FM_Salaries(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
and FBRH.VersionID=@Startversionfirsto
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
UNION ALL --双月上报
SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
,case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
UNION ALL --双月上报


SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @StartMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @StartMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @StartMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @StartMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @StartMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @StartMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @StartMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @StartMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @StartMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @StartMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @StartMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
--INNER JOIN FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRIH.BudgetRollingID
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1


left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRIH.BudgetRollingItemID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@StartMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
,case when @StartMonth=1 then isnull(vfda.sjMonth1,'0.00')
when @StartMonth=3 then isnull(vfda.sjMonth3,'0.00')
when @StartMonth=5 then isnull(vfda.sjMonth5,'0.00')
when @StartMonth=7 then isnull(vfda.sjMonth7,'0.00')
when @StartMonth=2 then isnull(vfda.sjMonth2,'0.00')
when @StartMonth=4 then isnull(vfda.sjMonth3,'0.00')
when @StartMonth=6 then isnull(vfda.sjMonth6,'0.00')
when @StartMonth=8 then isnull(vfda.sjMonth8,'0.00')
when @StartMonth=9 then isnull(vfda.sjMonth9,'0.00')
when @StartMonth=10 then isnull(vfda.sjMonth10,'0.00')
when @StartMonth=11 then isnull(vfda.sjMonth11,'0.00')
else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
--INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
--inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
LEFT JOIN FM_Project(nolock) pt ON FBRH.projectid=pt.projectid

--实际
LEFT JOIN VIEW_FM_PropertyInOut_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID
--冻结
LEFT JOIN FM_ActualCostItemFromSystemPropertyInOutView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@StartMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID
WHERE FBRH.RecordStatus='active' AND FBRH.BudgetType='Salaries'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@Startversionfirsto
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth



end
end
else if(isnull(@EndMonth,0)>0)
begin

--处理可支付最后打版
if (isnull(@endversionfirst,0)>0)
begin

--开的发成本
insert into #FM_Cost(NodeID ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull(cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull( cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms
,isnull(clib.ContractAmount,'0.00') as ContractAmount ,isnull(clib.Negotiationchange,'0.00') as Negotiationchange
,isnull(clib.SettlementAmount,'0.00') as SettlementAmount ,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue
, isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
, 0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearlastIssuedAmt -- 最后一版可支付
,0 as GrandtotalpayAmt
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
--into #FM_Cost
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@endversionlast
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'

UNION ALL --可支付第一版

SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull(cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@endversionfirst
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'

UNION ALL --双月预算下发金额

SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
UNION ALL --双月上报

SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @EndMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
, case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
when @EndMonth=4 then isnull(vfda.sjMonth4,'0.00')
when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
--实际
LEFT JOIN VIEW_FM_DevelopmentCost_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND vfda.BudgetYear=FBRH.BudgetYear
AND FBRDH.BudgetCodeID=vfda.BudgetCodeID AND FBRDH.ContractLibID=vfda.ContractLibID
--冻结
LEFT JOIN FM_ActualCostItemDtlFromSystemDevelopmentCostView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@EndMonth
AND FBRDH.BudgetCodeID=vfds.BudgetCodeID AND FBRDH.ContractLibID=vfds.ContractCode
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@endversionlast
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
--专项管理费用(合同)
insert into #FM_SpecialExpenses(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo
,isnull(CMT.ContractAName,'') as ContractAName ,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms
,isnull(CMT.ContractAmount,'0.00') as ContractAmount ,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange
,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount ,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue
, isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
, 0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearlastIssuedAmt -- 最后一半可支付
,0 as GrandtotalpayAmt
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
and FBRH.VersionID=@endversionlast
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL --可支付第一版

SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
and FBRH.VersionID=@endversionfirst
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
UNION ALL --双月预算下发金额

SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
,case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
UNION ALL --双月上报

SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @EndMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
, case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
when @EndMonth=4 then isnull(vfda.sjMonth4,'0.00')
when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
--实际
LEFT JOIN VIEW_FM_SpecialExpenses_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID AND FBRDH.MatterContractID=vfda.MatterContractID
--冻结
LEFT JOIN FM_ActualCostItemDtlFromSystemSpecialExpensesView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@EndMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID AND FBRDH.MatterContractID=vfds.ContractCode
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
and FBRH.VersionID=@endversionlast
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth


-- --预算外
-- insert into #FM_SpecialExpenses(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
-- ,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
-- ,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
-- ,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
-- ,ContractAName ,PartyUnit ,PaymentTerms
-- ,ContractAmount ,Negotiationchange ,SettlementAmount
-- ,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
-- ,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
-- ,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
-- ,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
-- ,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
-- ,Amountpaid ,AvailableBalance)
-- SELECT
-- (cast(vfda.BudgetCodeID as varchar(22))+'_'+cast(vfda.MatterContractID as varchar(22))+'_'+cast(vfda.DeptID as varchar(22))) as NodeID,
-- vfda.MatterContractID ,0 as ProjectID ,vfda.CompanyID ,vfda.BudgetCodeID
-- ,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
-- ,isnull(bg1.CodeName,'') as CostCategory ,isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName,isnull(CMT.ContractNo,'') as ContractNo
-- ,isnull(CMT.ContractAName,'') as ContractAName ,isnull(CMT.PartyUnit,'') as PartyUnit ,isnull(CMT.ContractAmount,'0.00') as ContractAmount
-- ,0 as SumBudgetRollingAmt
-- ,0 as EndMonthBudgetRollingAmt
-- ,0 as EndMonthBudgetRollingAmt --双月预算上报金额
-- ,0 as SumBudgetRollingissuedAmt ,0 as EndMonthBudgetRollingissuedAmt ,0 as EndMonthBudgetRollingissuedAmt --双月预算下发金额
-- ,0 as SumpayBudgetRollingAmt
-- ,0 as EndMonthpayBudgetRollingAmt
-- ,0 as EndMonthpayBudgetRollingAmt -- 可支付下发金额
-- ,0 as SumpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as SumcumulativepayBudgetRollingAmt
-- ,0 as EndMonthcumulativepayBudgetRollingAmt
-- ,0 as EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
-- ,0 as SumHavecancelafterverificationAmt
-- ,case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
-- when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
-- when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
-- when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
-- when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
-- when @EndMonth=4 then isnull(vfda.sjMonth4,'0.00')
-- when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
-- when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
-- when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
-- when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
-- when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
-- else isnull(vfda.sjMonth12,'0.00') end as EndMonthHavecancelafterverificationAmt
-- ,case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
-- when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
-- when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
-- when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
-- when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
-- when @EndMonth=4 then isnull(vfda.sjMonth4,'0.00')
-- when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
-- when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
-- when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
-- when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
-- when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
-- else (isnull(vfda.sjMonth12,'0.00')) end as EndMonthHavecancelafterverificationAmt -- 已付款金额(核减) --申请付款金额(冻结)
-- ,0 as SumHavebeenfrozenAmt
-- ,case when @EndMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
-- else (isnull(vfds.FreeAmt,'0.00')) end as StartHavebeenfrozenAmt
-- ,0 as EndMonthHavebeenfrozenAmt ----申请付款金额(冻结)
-- ,0 as CanpaythebalanceAmt --可用余额
-- from VIEW_FM_SpecialExpenses_Actual(nolock) vfda
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=vfda.CompanyID and cy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=vfda.DEPTID and dt.RecordStatus='active'
-- inner join FM_MatterContract(nolock) CMT on vfda.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
-- inner join FM_BudgetCode(nolock) bc on vfda.BudgetCodeID=bc.BudgetCodeID and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on bg3.BudgetCodeID=vfda.BudgetCodeID and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=vfda.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=vfda.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- LEFT JOIN FM_ActualCostItemDtlFromSystemSpecialExpensesView vfds ON vfda.CompanyID=vfds.CompanyID
-- AND vfda.DeptID=vfds.DeptID AND vfda.BudgetType=vfds.BudgetType
-- AND vfda.BudgetYear=vfds.BudgetYear AND vfds.monthly=@EndMonth
-- AND vfda.BudgetCodeID=vfds.BudgetCodeID AND vfda.MatterContractID=vfds.ContractCode

-- WHERE vfda.BudgetType='SpecialExpenses'
-- and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
-- AND (vfda.CompanyID=@CompanyId or @CompanyId=0)
-- AND vfda.BudgetYear=@BudgetYear
-- and( vfda.MatterContractID not in (select MatterContractID from #FM_SpecialExpenses (nolock))
-- or vfda.BudgetCodeID not in(select BudgetCodeID from #FM_SpecialExpenses (nolock)))
-- --select * from #FM_SpecialExpenses

-- UNION ALL

-- --1.2. 获取股份公司下发可支付版本的预算 实际 结束月冻结
-- SELECT
-- (cast(vfda.BudgetCodeID as varchar(22))+'_'+cast(vfda.MatterContractID as varchar(22))+'_'+cast(vfda.DeptID as varchar(22))) as NodeID,
-- vfda.MatterContractID ,0 as ProjectID ,vfda.CompanyID ,vfda.BudgetCodeID
-- ,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
-- ,isnull(bg1.CodeName,'') as CostCategory ,isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName,isnull(CMT.ContractNo,'') as ContractNo
-- ,isnull(CMT.ContractAName,'') as ContractAName ,isnull(CMT.PartyUnit,'') as PartyUnit ,isnull(CMT.ContractAmount,'0.00') as ContractAmount
-- ,0 as SumBudgetRollingAmt
-- ,0 as EndMonthBudgetRollingAmt
-- ,0 as EndMonthBudgetRollingAmt --双月预算上报金额
-- ,0 as SumBudgetRollingissuedAmt ,0 as EndMonthBudgetRollingissuedAmt ,0 as EndMonthBudgetRollingissuedAmt --双月预算下发金额
-- ,0 as SumpayBudgetRollingAmt
-- ,0 as EndMonthpayBudgetRollingAmt
-- ,0 as EndMonthpayBudgetRollingAmt -- 可支付下发金额
-- ,0 as SumpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as SumcumulativepayBudgetRollingAmt
-- ,0 as EndMonthcumulativepayBudgetRollingAmt
-- ,0 as EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
-- ,0 as SumHavecancelafterverificationAmt
-- ,0 as EndMonthHavecancelafterverificationAmt
-- ,0 as EndMonthHavecancelafterverificationAmt --已付款金额(核减)
-- ,0 as SumHavebeenfrozenAmt
-- ,0 as StartHavebeenfrozenAmt
-- ,case when @EndMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
-- else (isnull(vfds.FreeAmt,'0.00')) end as EndMonthHavebeenfrozenAmt --申请付款金额(冻结
-- ,0 as CanpaythebalanceAmt --可用余额
-- from VIEW_FM_SpecialExpenses_Actual(nolock) vfda
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=vfda.CompanyID and cy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=vfda.DEPTID and dt.RecordStatus='active'
-- inner join FM_MatterContract(nolock) CMT on vfda.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
-- inner join FM_BudgetCode(nolock) bc on vfda.BudgetCodeID=bc.BudgetCodeID and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on bg3.BudgetCodeID=vfda.BudgetCodeID and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=vfda.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=vfda.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- LEFT JOIN FM_ActualCostItemDtlFromSystemSpecialExpensesView vfds ON vfda.CompanyID=vfds.CompanyID
-- AND vfda.DeptID=vfds.DeptID AND vfda.BudgetType=vfds.BudgetType
-- AND vfda.BudgetYear=vfds.BudgetYear
-- AND vfda.BudgetCodeID=vfds.BudgetCodeID AND vfda.MatterContractID=vfds.ContractCode
-- WHERE vfda.BudgetType='SpecialExpenses'
-- and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
-- AND (vfda.CompanyID=@CompanyId or @CompanyId=0) AND vfds.monthly=@EndMonth
-- AND vfda.BudgetYear=@BudgetYear --AND vfda.EndMonth=@EndMonth
-- and( vfda.DeptID <>@DeptID or vfda.MatterContractID not in (select MatterContractID from #FM_SpecialExpenses (nolock))
-- or vfda.BudgetCodeID not in(select BudgetCodeID from #FM_SpecialExpenses (nolock)))

-- UNION ALL
-- SELECT
-- (cast(vfds.BudgetCodeID as varchar(22))+'_'+cast(vfds.ContractCode as varchar(22))+'_'+cast(vfds.DeptID as varchar(22))) as NodeID,
-- vfda.MatterContractID ,0 as ProjectID ,vfda.CompanyID ,vfda.BudgetCodeID
-- ,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
-- ,isnull(bg1.CodeName,'') as CostCategory ,isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName,isnull(CMT.ContractNo,'') as ContractNo
-- ,isnull(CMT.ContractAName,'') as ContractAName ,isnull(CMT.PartyUnit,'') as PartyUnit ,isnull(CMT.ContractAmount,'0.00') as ContractAmount
-- ,0 as SumBudgetRollingAmt
-- ,0 as EndMonthBudgetRollingAmt
-- ,0 as EndMonthBudgetRollingAmt --双月预算上报金额
-- ,0 as SumBudgetRollingissuedAmt ,0 as EndMonthBudgetRollingissuedAmt ,0 as EndMonthBudgetRollingissuedAmt --双月预算下发金额
-- ,0 as SumpayBudgetRollingAmt
-- ,0 as EndMonthpayBudgetRollingAmt
-- ,0 as EndMonthpayBudgetRollingAmt -- 可支付下发金额
-- ,0 as SumpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as SumcumulativepayBudgetRollingAmt
-- ,0 as EndMonthcumulativepayBudgetRollingAmt
-- ,0 as EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
-- ,0 as SumHavecancelafterverificationAmt
-- ,case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
-- when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
-- when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
-- when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
-- when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
-- when @EndMonth=4 then isnull(vfda.sjMonth4,'0.00')
-- when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
-- when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
-- when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
-- when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
-- when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
-- else isnull(vfda.sjMonth12,'0.00') end as EndMonthHavecancelafterverificationAmt
-- ,case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
-- when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
-- when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
-- when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
-- when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
-- when @EndMonth=4 then isnull(vfda.sjMonth4,'0.00')
-- when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
-- when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
-- when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
-- when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
-- when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
-- else (isnull(vfda.sjMonth12,'0.00')) end as EndMonthHavecancelafterverificationAmt -- 已付款金额(核减) --申请付款金额(冻结)
-- ,0 as SumHavebeenfrozenAmt
-- ,case when @EndMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
-- else (isnull(vfds.FreeAmt,'0.00')) end as StartHavebeenfrozenAmt
-- ,0 as EndMonthHavebeenfrozenAmt ----申请付款金额(冻结)
-- ,0 as CanpaythebalanceAmt --可用余额
-- from FM_ActualCostItemDtlFromSystemSpecialExpensesView (nolock) vfds
-- LEFT JOIN VIEW_FM_SpecialExpenses_Actual vfda ON vfda.CompanyID=vfds.CompanyID
-- AND vfda.DeptID=vfds.DeptID AND vfda.BudgetType=vfds.BudgetType
-- AND vfda.BudgetYear=vfds.BudgetYear
-- AND vfda.BudgetCodeID=vfds.BudgetCodeID AND vfda.MatterContractID=vfds.ContractCode
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=vfds.CompanyID and cy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=vfds.DEPTID and dt.RecordStatus='active'
-- inner join FM_MatterContract(nolock) CMT on vfds.ContractCode=CMT.MatterContractID and CMT.RecordStatus='active'
-- inner join FM_BudgetCode(nolock) bc on vfds.BudgetCodeID=bc.BudgetCodeID and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on bg3.BudgetCodeID=vfds.BudgetCodeID and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=vfds.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=vfds.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

-- WHERE vfds.BudgetType='SpecialExpenses'
-- and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
-- AND (vfds.CompanyID=@CompanyId or @CompanyId=0)
-- AND vfds.BudgetYear=@BudgetYear AND vfds.Monthly=@EndMonth --and vfds.ContractCode=1197
--and( vfds.DeptID <>@DeptID or vfds.ContractCode not in (select MatterContractID from #FM_SpecialExpenses (nolock))
-- or vfds.BudgetCodeID not in (select BudgetCodeID from #FM_SpecialExpenses (nolock)))
-- UNION ALL
-- --1.2. 获取股份公司下发可支付版本的预算 实际 结束月冻结
-- SELECT
-- (cast(vfds.BudgetCodeID as varchar(22))+'_'+cast(vfds.ContractCode as varchar(22))+'_'+cast(vfds.DeptID as varchar(22))) as NodeID,
-- vfda.MatterContractID ,0 as ProjectID ,vfda.CompanyID ,vfda.BudgetCodeID
-- ,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
-- ,isnull(bg1.CodeName,'') as CostCategory ,isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName,isnull(CMT.ContractNo,'') as ContractNo
-- ,isnull(CMT.ContractAName,'') as ContractAName ,isnull(CMT.PartyUnit,'') as PartyUnit ,isnull(CMT.ContractAmount,'0.00') as ContractAmount
-- , 0 as SumBudgetRollingAmt
-- ,0 as EndMonthBudgetRollingAmt
-- ,0 as EndMonthBudgetRollingAmt --双月预算上报金额
-- ,0 as SumBudgetRollingissuedAmt ,0 as EndMonthBudgetRollingissuedAmt ,0 as EndMonthBudgetRollingissuedAmt --双月预算下发金额
-- ,0 as SumpayBudgetRollingAmt
-- ,0 as EndMonthpayBudgetRollingAmt
-- ,0 as EndMonthpayBudgetRollingAmt -- 可支付下发金额
-- ,0 as SumpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
-- ,0 as SumcumulativepayBudgetRollingAmt
-- ,0 as EndMonthcumulativepayBudgetRollingAmt
-- ,0 as EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
-- ,0 as SumHavecancelafterverificationAmt
-- ,0 as EndMonthHavecancelafterverificationAmt
-- ,0 as EndMonthHavecancelafterverificationAmt --已付款金额(核减)
-- ,0 as SumHavebeenfrozenAmt
-- ,0 as StartHavebeenfrozenAmt
-- ,case when @EndMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
-- when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
-- else (isnull(vfds.FreeAmt,'0.00')) end as EndMonthHavebeenfrozenAmt --申请付款金额(冻结
-- ,0 as CanpaythebalanceAmt --可用余额
-- from FM_ActualCostItemDtlFromSystemSpecialExpensesView (nolock) vfds
-- LEFT JOIN VIEW_FM_SpecialExpenses_Actual vfda ON vfds.CompanyID=vfda.CompanyID
-- AND vfds.DeptID=vfda.DeptID AND vfds.BudgetType=vfda.BudgetType
-- AND vfds.BudgetYear=vfda.BudgetYear
-- AND vfds.BudgetCodeID=vfda.BudgetCodeID AND vfds.ContractCode=vfda.MatterContractID
-- LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=vfds.CompanyID and cy.RecordStatus='active'
-- left join CF_DEPT(nolock) dt on dt.DEPTID=vfds.DEPTID and dt.RecordStatus='active'
-- inner join FM_MatterContract(nolock) CMT on vfds.ContractCode=CMT.MatterContractID and CMT.RecordStatus='active'
-- inner join FM_BudgetCode(nolock) bc on vfds.BudgetCodeID=bc.BudgetCodeID and bc.RecordStatus='active'
-- left join FM_BudgetCode(nolock) bg3 on bg3.BudgetCodeID=vfds.BudgetCodeID and bg3.RecordStatus='active' and bg3.CodeLevel=4
-- left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=vfds.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
-- left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=vfds.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
-- left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
-- WHERE vfds.BudgetType='SpecialExpenses'
-- and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
-- AND (vfds.CompanyID=@CompanyId or @CompanyId=0)
-- AND vfds.BudgetYear=@BudgetYear AND vfds.Monthly=@EndMonth --and vfds.ContractCode=1197
-- and( vfds.DeptID <>@DeptID or vfds.ContractCode not in (select MatterContractID from #FM_SpecialExpenses (nolock))
-- or vfds.BudgetCodeID not in(select BudgetCodeID from #FM_SpecialExpenses (nolock)))

insert into #FM_remainingsubjects(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
, 0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearlastIssuedAmt -- 最后一半可支付
,0 as GrandtotalpayAmt
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID

inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
and FBRH.VersionID=@endversionlast
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'

AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL --可支付第一版

SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID

inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
and FBRH.VersionID=@endversionfirst
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
UNION ALL --双月预算下发金额

SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID

inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
and FBRH.VersionID=@endversionfirst
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
UNION ALL --双月上报

SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
--INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1


left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'

AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull( cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @EndMonth=1 then (isnull(gvfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00'))
when @EndMonth=3 then (isnull(Avfda.sjMonth3,'0.00')+isnull(gvfda.sjMonth3,'0.00')+isnull(Avfda.sjMonth3,'0.00'))
when @EndMonth=5 then (isnull(Avfda.sjMonth5,'0.00')+isnull(gvfda.sjMonth5,'0.00')+isnull(Avfda.sjMonth5,'0.00'))
when @EndMonth=7 then (isnull(Avfda.sjMonth7,'0.00')+isnull(gvfda.sjMonth7,'0.00')+isnull(Avfda.sjMonth7,'0.00'))
when @EndMonth=2 then (isnull(Avfda.sjMonth2,'0.00')+isnull(gvfda.sjMonth2,'0.00')+isnull(Avfda.sjMonth2,'0.00'))
when @EndMonth=4 then (isnull(Avfda.sjMonth4,'0.00')+isnull(gvfda.sjMonth4,'0.00')+isnull(Avfda.sjMonth4,'0.00'))
when @EndMonth=6 then (isnull(Avfda.sjMonth6,'0.00')+isnull(gvfda.sjMonth6,'0.00')+isnull(Avfda.sjMonth6,'0.00'))
when @EndMonth=8 then (isnull(Avfda.sjMonth8,'0.00')+isnull(gvfda.sjMonth8,'0.00')+isnull(Avfda.sjMonth8,'0.00'))
when @EndMonth=9 then (isnull(Avfda.sjMonth9,'0.00')+isnull(gvfda.sjMonth9,'0.00')+isnull(Avfda.sjMonth9,'0.00'))
when @EndMonth=10 then (isnull(Avfda.sjMonth10,'0.00')+isnull(gvfda.sjMonth10,'0.00')+isnull(Avfda.sjMonth10,'0.00'))
when @EndMonth=11 then (isnull(Avfda.sjMonth11,'0.00')+isnull(gvfda.sjMonth11,'0.00')+isnull(Avfda.sjMonth11,'0.00'))
else (isnull(vfda.sjMonth12,'0.00')+isnull(gvfda.sjMonth12,'0.00')+isnull(Avfda.sjMonth12,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
,case when @EndMonth=1 then (isnull(gvfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00'))
when @EndMonth=3 then (isnull(Avfda.sjMonth3,'0.00')+isnull(gvfda.sjMonth3,'0.00')+isnull(Avfda.sjMonth3,'0.00'))
when @EndMonth=5 then (isnull(Avfda.sjMonth5,'0.00')+isnull(gvfda.sjMonth5,'0.00')+isnull(Avfda.sjMonth5,'0.00'))
when @EndMonth=7 then (isnull(Avfda.sjMonth7,'0.00')+isnull(gvfda.sjMonth7,'0.00')+isnull(Avfda.sjMonth7,'0.00'))
when @EndMonth=2 then (isnull(Avfda.sjMonth2,'0.00')+isnull(gvfda.sjMonth2,'0.00')+isnull(Avfda.sjMonth2,'0.00'))
when @EndMonth=4 then (isnull(Avfda.sjMonth4,'0.00')+isnull(gvfda.sjMonth4,'0.00')+isnull(Avfda.sjMonth4,'0.00'))
when @EndMonth=6 then (isnull(Avfda.sjMonth6,'0.00')+isnull(gvfda.sjMonth6,'0.00')+isnull(Avfda.sjMonth6,'0.00'))
when @EndMonth=8 then (isnull(Avfda.sjMonth8,'0.00')+isnull(gvfda.sjMonth8,'0.00')+isnull(Avfda.sjMonth8,'0.00'))
when @EndMonth=9 then (isnull(Avfda.sjMonth9,'0.00')+isnull(gvfda.sjMonth9,'0.00')+isnull(Avfda.sjMonth9,'0.00'))
when @EndMonth=10 then (isnull(Avfda.sjMonth10,'0.00')+isnull(gvfda.sjMonth10,'0.00')+isnull(Avfda.sjMonth10,'0.00'))
when @EndMonth=11 then (isnull(vfda.sjMonth11,'0.00')+isnull(gvfda.sjMonth11,'0.00')+isnull(Avfda.sjMonth11,'0.00'))
else (isnull(vfda.sjMonth12,'0.00')+isnull(gvfda.sjMonth12,'0.00')+isnull(Avfda.sjMonth12,'0.00')) end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
left join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
--INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
--inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
LEFT JOIN FM_Project(nolock) pt ON FBRH.projectid=pt.projectid

--实际
LEFT JOIN VIEW_FM_PropertyInOut_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID
--冻结
LEFT JOIN FM_ActualCostItemFromSystemPropertyInOutView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@EndMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID


--实际
LEFT JOIN VIEW_FM_GeneralExpensest_Actual gvfda ON gvfda.CompanyID=FBRIH.CompanyID
AND gvfda.DeptID=FBRIH.DeptID AND gvfda.BudgetType=FBRH.BudgetType
AND FBRH.BudgetYear=gvfda.BudgetYear AND FBRIH.BudgetCodeID=gvfda.BudgetCodeID

--冻结
LEFT JOIN FM_ActualCostItemFromSystemGeneralExpensesView gvfds ON FBRH.CompanyID=gvfds.CompanyID
AND FBRH.DeptID=gvfds.DeptID AND FBRH.BudgetType=gvfds.BudgetType
AND FBRH.BudgetYear=gvfds.BudgetYear AND gvfds.monthly=@EndMonth
AND FBRIH.BudgetCodeID=gvfds.BudgetCodeID

--实际
LEFT JOIN VIEW_FM_AbsorbInvestment_Actual Avfda ON FBRH.CompanyID=Avfda.CompanyID
AND FBRH.DeptID=Avfda.DeptID AND FBRH.BudgetType=Avfda.BudgetType
AND FBRH.BudgetYear=Avfda.BudgetYear AND FBRIH.BudgetCodeID=Avfda.BudgetCodeID
--冻结
LEFT JOIN FM_ActualCostItemFromSystemAbsorbInvestmentView Avfds ON FBRH.CompanyID=Avfds.CompanyID
AND FBRH.DeptID=Avfds.DeptID AND FBRH.BudgetType=Avfds.BudgetType AND Avfds.monthly=@EndMonth
AND FBRH.BudgetYear=Avfds.BudgetYear AND FBRIH.BudgetCodeID=Avfds.BudgetCodeID

WHERE FBRH.RecordStatus='active' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@endversionlast
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

--薪酬福利
insert into #FM_Salaries(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
, 0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearlastIssuedAmt -- 最后一半可支付
,0 as GrandtotalpayAmt
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
and FBRH.VersionID=@endversionlast
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL --可支付第一版

SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
and FBRH.VersionID=@endversionfirst
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
UNION ALL --双月预算下发金额

SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
,case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
UNION ALL --双月上报


SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,''as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
--INNER JOIN FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRIH.BudgetRollingID
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1


left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRIH.BudgetRollingItemID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'')as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @EndMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
,case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
when @EndMonth=4 then isnull(vfda.sjMonth3,'0.00')
when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
--INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
--inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
LEFT JOIN FM_Project(nolock) pt ON FBRH.projectid=pt.projectid

--实际
LEFT JOIN VIEW_FM_PropertyInOut_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID
--冻结
LEFT JOIN FM_ActualCostItemFromSystemPropertyInOutView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@EndMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID
WHERE FBRH.RecordStatus='active' AND FBRH.BudgetType='Salaries'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@endversionlast
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

end
else if ( isnull(@endversionlast,0)>0 and isnull(@endversionfirst,0)<=0)
begin

--开的发成本
insert into #FM_Cost(NodeID ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(clib.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@endversionfirst
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
UNION ALL --双月预算下发金额
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cpy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(clib.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'

UNION ALL --双月上报

SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(clib.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
,0 as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg1.CodeName,'') as CostCategory
, isnull(bg2.CodeName,'') as TwoCodeName,'' as ThreeCodeName, '' as FourCodeName
,isnull(clib.Tosignsituation,'') as Tosignsituation , CAST(isnull(clib.SignTime,null) as datetime) as SignTime,isnull(clib.ContractNo,'') as ContractNo ,isnull(clib.ContractAName,'') as ContractAName
,isnull(clib.PartyUnit,'') as PartyUnit,isnull(clib.PaymentTerms,'') as PaymentTerms,isnull(clib.ContractAmount,'0.00') as ContractAmount
,isnull(clib.Negotiationchange,'0.00') as Negotiationchange ,isnull(clib.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(clib.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(clib.Paymentrate,'0.00') as Paymentrate
,0 as Paymentplansthismonth,isnull(clib.Remark,'') as Remark,isnull(clib.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
,0 as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @EndMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
,case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
when @EndMonth=4 then isnull(vfda.sjMonth3,'0.00')
when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bg3 on ( bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID) and bg3.RecordStatus='active'
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2

inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
left join CF_USER (nolock) ur on ur.USERID=clib.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
--实际
LEFT JOIN VIEW_FM_DevelopmentCost_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND vfda.BudgetYear=FBRH.BudgetYear
AND FBRDH.BudgetCodeID=vfda.BudgetCodeID AND FBRDH.ContractLibID=vfda.ContractLibID
--冻结
LEFT JOIN FM_ActualCostItemDtlFromSystemDevelopmentCostView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@EndMonth
AND FBRDH.BudgetCodeID=vfds.BudgetCodeID AND FBRDH.ContractLibID=vfds.ContractCode
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0) and FBRH.VersionID=@endversionfirst
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'

--专项管理费用(合同)
insert into #FM_SpecialExpenses(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea ,'' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'

left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and FBRH.VersionID=@endversionfirst
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL --双月上报
SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea ,'' as ProjectName
,isnull(cy.COMPANYNAME,'')as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'

left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and FBRH.VersionID=@versionA
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
UNION ALL --双月上报

SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @EndMonth=1 then (isnull(FBRDH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRDH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRDH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRDH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRDH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRDH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRDH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRDH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRDH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRDH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRDH.Month11,'0.00'))
else (isnull(FBRDH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRDH.BudgetRollingDtlID as NodeID,CMT.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,isnull(CMT.Tosignsituation,'') as Tosignsituation , CAST(isnull(CMT.SignTime,null) as datetime) as SignTime,isnull(CMT.ContractNo,'') as ContractNo ,isnull(CMT.ContractAName,'') as ContractAName
,isnull(CMT.PartyUnit,'') as PartyUnit,isnull(CMT.PaymentTerms,'') as PaymentTerms,isnull(CMT.ContractAmount,'0.00') as ContractAmount
,isnull(CMT.Negotiationchange,'0.00') as Negotiationchange ,isnull(CMT.SettlementAmount,'0.00') as SettlementAmount
,isnull(FBRDH.Totaloutputvalue,'0.00') as Totaloutputvalue , isnull(FBRDH.TotalpayableAmt,'0.00') as TotalpayableAmt ,isnull(CMT.ContractAmountPaid,'0.00') as ContractAmountPaid
,isnull(FBRDH.TotalpayableNoPaidAmt,'0.00') as TotalpayableNoPaidAmt ,isnull(CMT.Paymentrate,'0.00') as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRDH.Remark,'') as Remark,isnull(CMT.CreateUserID,'') as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @EndMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
,case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
when @EndMonth=4 then isnull(vfda.sjMonth3,'0.00')
when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRDH.BudgetCodeID=bc.BudgetCodeID or FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID or bg3.BudgetCodeID=FBRDH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID or bg2.BudgetCodeID=FBRDH.BudgetCodeID ) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID or bg1.BudgetCodeID=FBRDH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
left join CF_USER (nolock) ur on ur.USERID=CMT.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
--实际
LEFT JOIN VIEW_FM_SpecialExpenses_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID AND FBRDH.MatterContractID=vfda.MatterContractID
--冻结
LEFT JOIN FM_ActualCostItemDtlFromSystemSpecialExpensesView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@EndMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID AND FBRDH.MatterContractID=vfds.ContractCode
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'
and FBRH.VersionID=@endversionfirst
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

insert into #FM_remainingsubjects(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID

inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
and FBRH.VersionID=@endversionfirst
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
union all
SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
,case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID

inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
and FBRH.VersionID=@versionA
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL --双月上报

SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
--INNER JOIN #FM_BudgetRollingDtl(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1


left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'

AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT 'Item'+cast(FBRIH.BudgetRollingItemID as varchar(22)) as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,isnull( cpy.COMPANYNAME,'') as Theprojectarea ,isnull(pj.ProjectName,'') as ProjectName
,isnull(cy.COMPANYNAME,'') ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @EndMonth=1 then (isnull(gvfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00'))
when @EndMonth=3 then (isnull(Avfda.sjMonth3,'0.00')+isnull(gvfda.sjMonth3,'0.00')+isnull(Avfda.sjMonth3,'0.00'))
when @EndMonth=5 then (isnull(Avfda.sjMonth5,'0.00')+isnull(gvfda.sjMonth5,'0.00')+isnull(Avfda.sjMonth5,'0.00'))
when @EndMonth=7 then (isnull(Avfda.sjMonth7,'0.00')+isnull(gvfda.sjMonth7,'0.00')+isnull(Avfda.sjMonth7,'0.00'))
when @EndMonth=2 then (isnull(Avfda.sjMonth2,'0.00')+isnull(gvfda.sjMonth2,'0.00')+isnull(Avfda.sjMonth2,'0.00'))
when @EndMonth=4 then (isnull(Avfda.sjMonth4,'0.00')+isnull(gvfda.sjMonth4,'0.00')+isnull(Avfda.sjMonth4,'0.00'))
when @EndMonth=6 then (isnull(Avfda.sjMonth6,'0.00')+isnull(gvfda.sjMonth6,'0.00')+isnull(Avfda.sjMonth6,'0.00'))
when @EndMonth=8 then (isnull(Avfda.sjMonth8,'0.00')+isnull(gvfda.sjMonth8,'0.00')+isnull(Avfda.sjMonth8,'0.00'))
when @EndMonth=9 then (isnull(Avfda.sjMonth9,'0.00')+isnull(gvfda.sjMonth9,'0.00')+isnull(Avfda.sjMonth9,'0.00'))
when @EndMonth=10 then (isnull(Avfda.sjMonth10,'0.00')+isnull(gvfda.sjMonth10,'0.00')+isnull(Avfda.sjMonth10,'0.00'))
when @EndMonth=11 then (isnull(Avfda.sjMonth11,'0.00')+isnull(gvfda.sjMonth11,'0.00')+isnull(Avfda.sjMonth11,'0.00'))
else (isnull(vfda.sjMonth12,'0.00')+isnull(gvfda.sjMonth12,'0.00')+isnull(Avfda.sjMonth12,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
,case when @EndMonth=1 then (isnull(gvfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00')+isnull(Avfda.sjMonth1,'0.00'))
when @EndMonth=3 then (isnull(Avfda.sjMonth3,'0.00')+isnull(gvfda.sjMonth3,'0.00')+isnull(Avfda.sjMonth3,'0.00'))
when @EndMonth=5 then (isnull(Avfda.sjMonth5,'0.00')+isnull(gvfda.sjMonth5,'0.00')+isnull(Avfda.sjMonth5,'0.00'))
when @EndMonth=7 then (isnull(Avfda.sjMonth7,'0.00')+isnull(gvfda.sjMonth7,'0.00')+isnull(Avfda.sjMonth7,'0.00'))
when @EndMonth=2 then (isnull(Avfda.sjMonth2,'0.00')+isnull(gvfda.sjMonth2,'0.00')+isnull(Avfda.sjMonth2,'0.00'))
when @EndMonth=4 then (isnull(Avfda.sjMonth4,'0.00')+isnull(gvfda.sjMonth4,'0.00')+isnull(Avfda.sjMonth4,'0.00'))
when @EndMonth=6 then (isnull(Avfda.sjMonth6,'0.00')+isnull(gvfda.sjMonth6,'0.00')+isnull(Avfda.sjMonth6,'0.00'))
when @EndMonth=8 then (isnull(Avfda.sjMonth8,'0.00')+isnull(gvfda.sjMonth8,'0.00')+isnull(Avfda.sjMonth8,'0.00'))
when @EndMonth=9 then (isnull(Avfda.sjMonth9,'0.00')+isnull(gvfda.sjMonth9,'0.00')+isnull(Avfda.sjMonth9,'0.00'))
when @EndMonth=10 then (isnull(Avfda.sjMonth10,'0.00')+isnull(gvfda.sjMonth10,'0.00')+isnull(Avfda.sjMonth10,'0.00'))
when @EndMonth=11 then (isnull(vfda.sjMonth11,'0.00')+isnull(gvfda.sjMonth11,'0.00')+isnull(Avfda.sjMonth11,'0.00'))
else (isnull(vfda.sjMonth12,'0.00')+isnull(gvfda.sjMonth12,'0.00')+isnull(Avfda.sjMonth12,'0.00')) end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
--INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRDH ON FBRDH.BudgetRollingItemID=FBRIH.BudgetRollingItemID
--inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
inner join FM_BudgetCode(nolock) bc on ( FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
LEFT JOIN FM_Project(nolock) pt ON FBRH.projectid=pt.projectid

--实际
LEFT JOIN VIEW_FM_PropertyInOut_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID
--冻结
LEFT JOIN FM_ActualCostItemFromSystemPropertyInOutView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@EndMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID


--实际
LEFT JOIN VIEW_FM_GeneralExpensest_Actual gvfda ON gvfda.CompanyID=FBRIH.CompanyID
AND gvfda.DeptID=FBRIH.DeptID AND gvfda.BudgetType=FBRH.BudgetType
AND FBRH.BudgetYear=gvfda.BudgetYear AND FBRIH.BudgetCodeID=gvfda.BudgetCodeID

--冻结
LEFT JOIN FM_ActualCostItemFromSystemGeneralExpensesView gvfds ON FBRH.CompanyID=gvfds.CompanyID
AND FBRH.DeptID=gvfds.DeptID AND FBRH.BudgetType=gvfds.BudgetType
AND FBRH.BudgetYear=gvfds.BudgetYear AND gvfds.monthly=@EndMonth
AND FBRIH.BudgetCodeID=gvfds.BudgetCodeID

--实际
LEFT JOIN VIEW_FM_AbsorbInvestment_Actual Avfda ON FBRH.CompanyID=Avfda.CompanyID
AND FBRH.DeptID=Avfda.DeptID AND FBRH.BudgetType=Avfda.BudgetType
AND FBRH.BudgetYear=Avfda.BudgetYear AND FBRIH.BudgetCodeID=Avfda.BudgetCodeID
--冻结
LEFT JOIN FM_ActualCostItemFromSystemAbsorbInvestmentView Avfds ON FBRH.CompanyID=Avfds.CompanyID
AND FBRH.DeptID=Avfds.DeptID AND FBRH.BudgetType=Avfds.BudgetType AND Avfds.monthly=@EndMonth
AND FBRH.BudgetYear=Avfds.BudgetYear AND FBRIH.BudgetCodeID=Avfds.BudgetCodeID

WHERE FBRH.RecordStatus='active' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@endversionfirst
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

--薪酬福利
insert into #FM_Salaries(NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,EMPLOYEENAME ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
,case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
and FBRH.VersionID=@endversionfirst
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
UNION ALL --下发
SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearIssuedAmt--双月预算下发金额
,0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--LEFT join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL --双月上报


SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
, case when @EndMonth=1 then (isnull(FBRIH.Month1,'0.00'))
when @EndMonth=3 then (isnull(FBRIH.Month3,'0.00'))
when @EndMonth=5 then (isnull(FBRIH.Month5,'0.00'))
when @EndMonth=7 then (isnull(FBRIH.Month7,'0.00'))
when @EndMonth=2 then (isnull(FBRIH.Month2,'0.00'))
when @EndMonth=4 then (isnull(FBRIH.Month4,'0.00'))
when @EndMonth=6 then (isnull(FBRIH.Month6,'0.00'))
when @EndMonth=8 then (isnull(FBRIH.Month8,'0.00'))
when @EndMonth=9 then (isnull(FBRIH.Month9,'0.00'))
when @EndMonth=10 then (isnull(FBRIH.Month10,'0.00'))
when @EndMonth=11 then (isnull(FBRIH.Month11,'0.00'))
else (isnull(FBRIH.Month12,'0.00')) end as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,0 as ApplyPaymentAmount --申请付款金额(冻结)
,0 as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from FM_BudgetRolling(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
--INNER JOIN FM_BudgetRollingItem(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtl(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRIH.BudgetRollingID
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1


left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT FBRIH.BudgetRollingItemID as NodeID, 0 as MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,@EndMonth as Monthpayment ,'' as Theprojectarea , '' as ProjectName
,isnull(cy.COMPANYNAME,'') as Fillinregionalcompany ,isnull( dt.DeptName,'') as DeptName ,isnull(bg.CodeName,'') as CostCategory
, isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName, isnull(bg3.CodeName,'') as FourCodeName
,'' as Tosignsituation , null as SignTime,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit,'' as PaymentTerms
,'0.00' as ContractAmount ,'0.00' as Negotiationchange
,'0.00' as SettlementAmount ,'0.00' as Totaloutputvalue
, '0.00' as TotalpayableAmt ,'0.00' as ContractAmountPaid
,'0.00' as TotalpayableNoPaidAmt ,'0.00' as Paymentrate
, 0 as Paymentplansthismonth,isnull(FBRIH.Remark,'') as Remark,'' as CreateUserID
,isnull(ey.EMPLOYEENAME,'') as EMPLOYEENAME
,0 as TwoMonthYearReportingAmt--双月预算上报金额
, 0 as TwoMonthYearIssuedAmt--双月预算下发金额
, 0 as MonthYearFirstIssuedAmt --可支付下发金额
,0 as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as MonthYearlastIssuedAmt
,0 as GrandtotalpayAmt -- 累计可支付金额
,case when @EndMonth=1 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00'))
when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')) end as ApplyPaymentAmount --申请付款金额(冻结)
,case when @EndMonth=1 then isnull(vfda.sjMonth1,'0.00')
when @EndMonth=3 then isnull(vfda.sjMonth3,'0.00')
when @EndMonth=5 then isnull(vfda.sjMonth5,'0.00')
when @EndMonth=7 then isnull(vfda.sjMonth7,'0.00')
when @EndMonth=2 then isnull(vfda.sjMonth2,'0.00')
when @EndMonth=4 then isnull(vfda.sjMonth3,'0.00')
when @EndMonth=6 then isnull(vfda.sjMonth6,'0.00')
when @EndMonth=8 then isnull(vfda.sjMonth8,'0.00')
when @EndMonth=9 then isnull(vfda.sjMonth9,'0.00')
when @EndMonth=10 then isnull(vfda.sjMonth10,'0.00')
when @EndMonth=11 then isnull(vfda.sjMonth11,'0.00')
else isnull(vfda.sjMonth12,'0.00') end as Amountpaid --已付款金额(核减)
,0 as AvailableBalance --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
--LEFT join FM_Project (nolock) pj on pj.ProjectID =FBRH.ProjectID and pj.RecordStatus='active'
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.RecordStatus='active'
--inner join CF_COMPANY (nolock) cpy on cpy.CompanyID=pj.CompanyID and cpy.RecordStatus='active'
left join CF_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.RecordStatus='active'
--INNER JOIN #FM_BudgetRollingItemHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
INNER JOIN #FM_BudgetRollingDtlHistory(nolock) FBRIH ON FBRIH.BudgetRollingID=FBRH.BudgetRollingID
--inner join FM_MatterContract(nolock) CMT on FBRDH.MatterContractID=CMT.MatterContractID and CMT.RecordStatus='active'
inner join FM_BudgetCode(nolock) bc on (FBRIH.BudgetCodeID=bc.BudgetCodeID) and bc.RecordStatus='active'
left join FM_BudgetCode(nolock) bg3 on (bg3.BudgetCodeID=FBRIH.BudgetCodeID ) and bg3.RecordStatus='active' and bg3.CodeLevel=4
left join FM_BudgetCode(nolock) bg2 on ( bg2.BudgetCodeID=bg3.ParentID or bg2.BudgetCodeID=FBRIH.BudgetCodeID) and bg2.RecordStatus='active' and bg2.CodeLevel=3
left join FM_BudgetCode(nolock) bg1 on (bg1.BudgetCodeID=bg2.ParentID or bg1.BudgetCodeID=FBRIH.BudgetCodeID ) and bg1.RecordStatus='active' and bg1.CodeLevel=2
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1

left join CF_USER (nolock) ur on ur.USERID=FBRIH.CreateUserID and ur.RECORDSTATUS='active'
left join CF_EMPLOYEE (nolock) ey on ey.EMPLOYEEID=ur.EMPLOYEEID and ur.RECORDSTATUS='active'
LEFT JOIN FM_Project(nolock) pt ON FBRH.projectid=pt.projectid

--实际
LEFT JOIN VIEW_FM_PropertyInOut_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRH.ProjectID=vfda.ProjectID AND FBRH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID
--冻结
LEFT JOIN FM_ActualCostItemFromSystemPropertyInOutView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRH.ProjectID=vfds.ProjectID AND FBRH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@EndMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID
WHERE FBRH.RecordStatus='active' AND FBRH.BudgetType='Salaries'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@endversionfirst
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth
end
end

if object_id('tempdb..#FM_all') is not null drop table #FM_all

create table #FM_all(NodeID varchar(50) not null,ContractLibID int ,ProjectID int ,CompanyID int,BudgetCodeID int
,Monthpayment varchar(10) ,Theprojectarea varchar(500),ProjectName varchar(500),Fillinregionalcompany varchar(500)
,DeptName varchar(500),CostCategory varchar(500),TwoCodeName varchar(500),ThreeCodeName varchar(500)
,FourCodeName varchar(500),Tosignsituation varchar(500),SignTime datetime,ContractNo varchar(500)
,ContractAName varchar(500),PartyUnit varchar(5000),PaymentTerms varchar(max)
,ContractAmount decimal(22,2) ,Negotiationchange decimal(22,2),SettlementAmount decimal(22,2)
,Totaloutputvalue decimal(22,2),TotalpayableAmt decimal(22,2),ContractAmountPaid decimal(22,2)
,TotalpayableNoPaidAmt decimal(22,2),Paymentrate decimal(22,2),Paymentplansthismonth decimal(22,2)
,Remark varchar(max),CreateUserID int ,Applicant varchar(100),TwoMonthYearReportingAmt decimal(22,2)
,TwoMonthYearIssuedAmt decimal(22,2),MonthYearFirstIssuedAmt decimal(22,2),AdditionandchangespayAmt decimal(22,2)
,MonthYearlastIssuedAmt decimal(22,2),GrandtotalpayAmt decimal(22,2),ApplyPaymentAmount decimal(22,2)
,Amountpaid decimal(22,2),AvailableBalance decimal(22,2))
insert into #FM_all(NodeID ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,Monthpayment ,Theprojectarea ,ProjectName ,Fillinregionalcompany
,DeptName ,CostCategory ,TwoCodeName ,ThreeCodeName
,FourCodeName ,Tosignsituation ,SignTime ,ContractNo
,ContractAName ,PartyUnit ,PaymentTerms
,ContractAmount ,Negotiationchange ,SettlementAmount
,Totaloutputvalue ,TotalpayableAmt ,ContractAmountPaid
,TotalpayableNoPaidAmt ,Paymentrate,Paymentplansthismonth
,Remark ,CreateUserID ,Applicant ,TwoMonthYearReportingAmt
,TwoMonthYearIssuedAmt ,MonthYearFirstIssuedAmt ,AdditionandchangespayAmt
,MonthYearlastIssuedAmt ,GrandtotalpayAmt ,ApplyPaymentAmount
,Amountpaid ,AvailableBalance)
SELECT MAX(cs.NodeID) as NodeID,cs.ContractLibID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.Monthpayment ,cs.Theprojectarea , cs.ProjectName
,cs.Fillinregionalcompany ,cs.DeptName ,cs.CostCategory
,cs.TwoCodeName,cs.ThreeCodeName, cs.FourCodeName
,cs.Tosignsituation , cs.SignTime,cs.ContractNo ,cs.ContractAName
,cs.PartyUnit,cs.PaymentTerms,cs.ContractAmount
,isnull(max(cs.Negotiationchange) ,'0.00') as Negotiationchange,cs.SettlementAmount
,cs.Totaloutputvalue , cs.TotalpayableAmt ,cs.ContractAmountPaid
,cs.TotalpayableNoPaidAmt ,cs.Paymentrate
,sum( cs.Paymentplansthismonth) as Paymentplansthismonth,max(cs.Remark) as Remark, MAX(cs.CreateUserID)as CreateUserID
,MAX(cs.EMPLOYEENAME) as Applicant
,sum(Cs.TwoMonthYearReportingAmt) as TwoMonthYearReportingAmt--双月预算上报金额
,sum(cs.TwoMonthYearIssuedAmt)as TwoMonthYearIssuedAmt --双月预算下发金额
,sum(cs.MonthYearFirstIssuedAmt) as MonthYearFirstIssuedAmt --可支付下发金额
,SUM(cs.GrandtotalpayAmt) as GrandtotalpayAmt
,(sum( cs.MonthYearlastIssuedAmt )-sum( cs.MonthYearFirstIssuedAmt)) as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,sum(cs.MonthYearlastIssuedAmt) as AdditionandchangespayAmt -- 累计可支付金额
,sum(cs.ApplyPaymentAmount) as ApplyPaymentAmount --申请付款金额(冻结)
,sum(cs.Amountpaid ) as Amountpaid --已付款金额(核减)
,(sum(cs.MonthYearlastIssuedAmt)-sum( cs.ApplyPaymentAmount) - sum( cs.Amountpaid ))as AvailableBalance --可用余额
from #FM_Cost (nolock) cs
GROUP BY cs.ContractLibID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.Monthpayment ,cs.Theprojectarea , cs.ProjectName
,cs.Fillinregionalcompany ,cs.DeptName ,cs.CostCategory
, cs.TwoCodeName,cs.ThreeCodeName, cs.FourCodeName
,cs.Tosignsituation , cs.SignTime,cs.ContractNo ,cs.ContractAName
,cs.PartyUnit,cs.PaymentTerms,cs.ContractAmount
,cs.SettlementAmount
,cs.Totaloutputvalue , cs.TotalpayableAmt ,cs.ContractAmountPaid
,cs.TotalpayableNoPaidAmt ,cs.Paymentrate
UNION ALL
SELECT MAX(cs.NodeID) as NodeID,cs.MatterContractID as ContractLibID,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.Monthpayment ,cs.Theprojectarea , cs.ProjectName
,cs.Fillinregionalcompany ,cs.DeptName ,cs.CostCategory
, cs.TwoCodeName,cs.ThreeCodeName, cs.FourCodeName
,cs.Tosignsituation , cs.SignTime,cs.ContractNo ,cs.ContractAName
,cs.PartyUnit,cs.PaymentTerms,cs.ContractAmount
,isnull(max(cs.Negotiationchange) ,'0.00') as Negotiationchange ,cs.SettlementAmount
,cs.Totaloutputvalue , cs.TotalpayableAmt ,cs.ContractAmountPaid
,cs.TotalpayableNoPaidAmt ,cs.Paymentrate
,sum( cs.Paymentplansthismonth) as Paymentplansthismonth,max(cs.Remark) as Remark, MAX(cs.CreateUserID)as CreateUserID
,MAX(cs.EMPLOYEENAME) as Applicant
,sum(Cs.TwoMonthYearReportingAmt) as TwoMonthYearReportingAmt--双月预算上报金额
,sum(cs.TwoMonthYearIssuedAmt)as TwoMonthYearIssuedAmt --双月预算下发金额
,sum(cs.MonthYearFirstIssuedAmt) as MonthYearFirstIssuedAmt --可支付下发金额
,SUM(cs.GrandtotalpayAmt) as GrandtotalpayAmt
,(sum( cs.MonthYearlastIssuedAmt )-sum( cs.MonthYearFirstIssuedAmt)) as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,sum(cs.MonthYearlastIssuedAmt) as AdditionandchangespayAmt -- 累计可支付金额
,sum(cs.ApplyPaymentAmount) as ApplyPaymentAmount --申请付款金额(冻结)
,sum(cs.Amountpaid ) as Amountpaid --已付款金额(核减)
,(sum(cs.MonthYearlastIssuedAmt)-sum( cs.ApplyPaymentAmount) - sum( cs.Amountpaid ))as AvailableBalance --可用余额
from #FM_SpecialExpenses (nolock) cs
GROUP BY cs.MatterContractID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.Monthpayment ,cs.Theprojectarea , cs.ProjectName
,cs.Fillinregionalcompany ,cs.DeptName ,cs.CostCategory
, cs.TwoCodeName,cs.ThreeCodeName, cs.FourCodeName
,cs.Tosignsituation , cs.SignTime,cs.ContractNo ,cs.ContractAName
,cs.PartyUnit,cs.PaymentTerms,cs.ContractAmount
,cs.SettlementAmount
,cs.Totaloutputvalue , cs.TotalpayableAmt ,cs.ContractAmountPaid
,cs.TotalpayableNoPaidAmt ,cs.Paymentrate

UNION ALL
SELECT MAX(cs.NodeID) as NodeID,0 as ContractLibID,cs.ProjectID as ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.Monthpayment ,cs.Theprojectarea ,cs.ProjectName as ProjectName
,cs.Fillinregionalcompany ,cs.DeptName ,cs.CostCategory
, cs.TwoCodeName,cs.ThreeCodeName, cs.FourCodeName
,cs.Tosignsituation , cs.SignTime,cs.ContractNo ,cs.ContractAName
,cs.PartyUnit,cs.PaymentTerms,cs.ContractAmount
,isnull(max(cs.Negotiationchange) ,'0.00') as Negotiationchange ,cs.SettlementAmount
,cs.Totaloutputvalue , cs.TotalpayableAmt ,cs.ContractAmountPaid
,cs.TotalpayableNoPaidAmt ,cs.Paymentrate
,sum( cs.Paymentplansthismonth) as Paymentplansthismonth,max(cs.Remark) as Remark, MAX(cs.CreateUserID)as CreateUserID
,MAX(cs.EMPLOYEENAME) as Applicant
,sum(Cs.TwoMonthYearReportingAmt) as TwoMonthYearReportingAmt--双月预算上报金额
,sum(cs.TwoMonthYearIssuedAmt)as TwoMonthYearIssuedAmt --双月预算下发金额
,sum(cs.MonthYearFirstIssuedAmt) as MonthYearFirstIssuedAmt --可支付下发金额
,SUM(cs.GrandtotalpayAmt) as GrandtotalpayAmt
,(sum( cs.MonthYearlastIssuedAmt )-sum( cs.MonthYearFirstIssuedAmt)) as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,sum(cs.MonthYearlastIssuedAmt) as AdditionandchangespayAmt -- 累计可支付金额
,sum(cs.ApplyPaymentAmount) as ApplyPaymentAmount --申请付款金额(冻结)
,sum(cs.Amountpaid ) as Amountpaid --已付款金额(核减)
,(sum(cs.MonthYearlastIssuedAmt)-sum( cs.ApplyPaymentAmount) - sum( cs.Amountpaid ))as AvailableBalance --可用余额
from #FM_remainingsubjects (nolock) cs
GROUP BY cs.CompanyID ,cs.BudgetCodeID ,cs.ProjectID,cs.ProjectName
,cs.Monthpayment ,cs.Theprojectarea
,cs.Fillinregionalcompany ,cs.DeptName ,cs.CostCategory
, cs.TwoCodeName,cs.ThreeCodeName, cs.FourCodeName
,cs.Tosignsituation , cs.SignTime,cs.ContractNo ,cs.ContractAName
,cs.PartyUnit,cs.PaymentTerms,cs.ContractAmount
,cs.SettlementAmount
,cs.Totaloutputvalue , cs.TotalpayableAmt ,cs.ContractAmountPaid
,cs.TotalpayableNoPaidAmt ,cs.Paymentrate

UNION ALL
SELECT MAX(cs.NodeID) as NodeID,0 as ContractLibID,cs.ProjectID as ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.Monthpayment ,cs.Theprojectarea ,cs.ProjectName as ProjectName
,cs.Fillinregionalcompany ,cs.DeptName ,cs.CostCategory
, cs.TwoCodeName,cs.ThreeCodeName, cs.FourCodeName
,cs.Tosignsituation , cs.SignTime,cs.ContractNo ,cs.ContractAName
,cs.PartyUnit,cs.PaymentTerms,cs.ContractAmount
,isnull(max(cs.Negotiationchange) ,'0.00') as Negotiationchange ,cs.SettlementAmount
,cs.Totaloutputvalue , cs.TotalpayableAmt ,cs.ContractAmountPaid
,cs.TotalpayableNoPaidAmt ,cs.Paymentrate
,sum( cs.Paymentplansthismonth) as Paymentplansthismonth,max(cs.Remark) as Remark, MAX(cs.CreateUserID)as CreateUserID
,MAX(cs.EMPLOYEENAME) as Applicant
,sum(Cs.TwoMonthYearReportingAmt) as TwoMonthYearReportingAmt--双月预算上报金额
,sum(cs.TwoMonthYearIssuedAmt)as TwoMonthYearIssuedAmt --双月预算下发金额
,sum(cs.MonthYearFirstIssuedAmt) as MonthYearFirstIssuedAmt --可支付下发金额
,SUM(cs.GrandtotalpayAmt) as GrandtotalpayAmt
,(sum( cs.MonthYearlastIssuedAmt )-sum( cs.MonthYearFirstIssuedAmt)) as AdditionandchangespayAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,sum(cs.MonthYearlastIssuedAmt) as AdditionandchangespayAmt -- 累计可支付金额
,sum(cs.ApplyPaymentAmount) as ApplyPaymentAmount --申请付款金额(冻结)
,sum(cs.Amountpaid ) as Amountpaid --已付款金额(核减)
,(sum(cs.MonthYearlastIssuedAmt)-sum( cs.ApplyPaymentAmount) - sum( cs.Amountpaid ))as AvailableBalance --可用余额
from #FM_Salaries (nolock) cs
GROUP BY cs.CompanyID ,cs.BudgetCodeID ,cs.ProjectID,cs.ProjectName
,cs.Monthpayment ,cs.Theprojectarea
,cs.Fillinregionalcompany ,cs.DeptName ,cs.CostCategory
, cs.TwoCodeName,cs.ThreeCodeName, cs.FourCodeName
,cs.Tosignsituation , cs.SignTime,cs.ContractNo ,cs.ContractAName
,cs.PartyUnit,cs.PaymentTerms,cs.ContractAmount
,cs.SettlementAmount
,cs.Totaloutputvalue , cs.TotalpayableAmt ,cs.ContractAmountPaid
,cs.TotalpayableNoPaidAmt ,cs.Paymentrate
if object_id('tempdb..#FM_Cost') is not null drop table #FM_Cost
if object_id('tempdb..#FM_SpecialExpenses') is not null drop table #FM_SpecialExpenses
if object_id('tempdb..#FM_PropertyInOut') is not null drop table #FM_PropertyInOut
if object_id('tempdb..#FM_remainingsubjects') is not null drop table #FM_remainingsubjects
if object_id('tempdb..#FM_Salaries') is not null drop table #FM_Salaries
select * from #FM_all order by CompanyID,ProjectID,CostCategory,DeptName

--select distinct(NodeID) from #FM_all --order by CompanyID,ProjectID,CostCategory,DeptName

if object_id('tempdb..#FM_all') is not null drop table #FM_all
end
go

posted @ 2015-07-16 14:24  学会自信  阅读(252)  评论(0编辑  收藏  举报