当前位置:Gxlcms > 数据库问题 > 资金计划报表sql 希望大家给点意见,指出我的存储过程里面的错误的地方

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

时间:2021-07-01 10:21:17 帮助过:3人阅读

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‘))

人气教程排行