本人这几天写的一个的存储希望大家多多个点意见

IF EXISTS (SELECT * FROM sys.procedures WHERE object_id = OBJECT_ID(N'[dbo].[Proc_BudgetRollingPerformtable]'))
DROP proc [dbo].[Proc_BudgetRollingPerformtable]
GO
--执行情况表(财务汇总)


--exec [Proc_BudgetRollingPerformtable] 2,2015,0,-1,5,6
--exec [Proc_BudgetRollingPerformtable] 2,2015,0,-1,7,8
--exec [Proc_BudgetRollingPerformtable] 6342,2015,-1,0,5,6
--exec [Proc_BudgetRollingPerformtable] 2,2015,0,-1,5,6
--exec [Proc_BudgetRollingPerformtable] 6342,2015,0,27832,5,6
create proc [dbo].[Proc_BudgetRollingPerformtable]
@CompanyID int, --填报区域公司
@BudgetYear int,
@ProjectID int ,--项目ID 小于等于0的时候,查询所以的
@DeptID INT,--部门ID -1表示查所有的 小于等于0的时候,查询所以的
@StartMonth int,
@EndMonth int
as
begin
--@versionfirsto 月第一版,@versionlast月最后一版
declare @versionfirsto int , @versionlast int ,@versionA int
declare @filtersql VARCHAR(500)=''

select @versionfirsto= min(VersionID) from FM_BudgetRollingVersion where RecordStatus= 'Approved' and StartMonth=@StartMonth AND EndMonth=@EndMonth and CompanyID=@CompanyID and CheckType='B' and BudgetYear= @BudgetYear
select @versionlast= mAX(VersionID) from FM_BudgetRollingVersion where VersionID <>@versionfirsto and RecordStatus= 'Approved' and StartMonth=@StartMonth AND EndMonth=@EndMonth and CompanyID=@CompanyID and CheckType='B' and BudgetYear= @BudgetYear
select @versionA= mAX(VersionID) from FM_BudgetRollingVersion where RecordStatus= 'Approved' and StartMonth=@StartMonth AND EndMonth=@EndMonth and CheckType='A' and BudgetYear= @BudgetYear
--select @versionfirsto,@versionlast,@versionA

if(@ProjectID>0)
begin
set @filtersql= ' where ProjectID= '+ cast(@ProjectID as varchar(22))
end
if(@DeptID>0)
begin
if @ProjectID>0
set @filtersql= @filtersql + ' and DeptID= '+cast(@DeptID as varchar(22))
else
set @filtersql= @filtersql + ' where DeptID= '+ cast(@DeptID as varchar(22))
end


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= isnull(@versionfirsto,0) or rh.VersionID= isnull( @versionlast,0) or rh.VersionID= isnull( @versionA,0) )
and rh.StartMonth=isnull(@StartMonth,0) AND rh.EndMonth= isnull(@EndMonth,0) and rh.BudgetYear=@BudgetYear


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 int,ContractLibID int,BudgetMatterID int,MatterContractID int,PartnerName varchar(500),
BudgetCodeID int,ContractAmt decimal(22,2),PaidAmt decimal(22,2),YearAmt decimal(22,2),
Remark varchar(max),DeptID int,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=isnull( @versionfirsto,0) or rh.VersionID= isnull( @versionlast,0) or rh.VersionID= isnull( @versionA,0))
and (rh.StartMonth=isnull(@StartMonth,0) or rh.EndMonth= isnull(@EndMonth,0))
and rh.BudgetYear=@BudgetYear


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( @versionfirsto,0) or VersionID= isnull( @versionlast,0) or VersionID= isnull( @versionA,0))
and (StartMonth=isnull(@StartMonth,0) or EndMonth= isnull(@EndMonth,0))
and BudgetYear=@BudgetYear


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
)
insert into #FM_ContractLib(
ContractLibID,Remark,BudgetYear,ContractNo,ContractAName,PartyUnit,ContractAmount,ContractAmountPaid,
VirtualcontractId,FormalcontractId,Obsolete,SignTime)
SELECT ContractLibID,Remark,BudgetYear,ContractNo,ContractAName,PartyUnit,ContractAmount,ContractAmountPaid,
VirtualcontractId,FormalcontractId,Obsolete,SignTime
FROM FM_ContractLib (nolock)
where ContractLibName NOT LIKE '%(历史)' AND RecordStatus='Active'


--开的发成本
if object_id('tempdb..#FM_Cost') is not null drop table #FM_Cost
create table #FM_Cost(NodeID varchar(300) not null,ContractLibID int ,ProjectID int ,CompanyID int,BudgetCodeID int
,COMPANYNAME varchar(500),ProjectName varchar(500),DeptID int ,DeptName varchar(500)
,CostCategory varchar(500),TwoCodeName varchar(500),ThreeCodeName varchar(500)
,FourCodeName varchar(500),ContractNo varchar(500)
,ContractAName varchar(500),PartyUnit varchar(5000),ContractAmount decimal(22,2)
,SumBudgetRollingAmt decimal(22,2) ,StartMonthBudgetRollingAmt decimal(22,2) ,EndMonthBudgetRollingAmt decimal(22,2)
,SumBudgetRollingissuedAmt decimal(22,2) ,StartMonthBudgetRollingissuedAmt decimal(22,2) ,EndMonthBudgetRollingissuedAmt decimal(22,2)
,SumpayBudgetRollingAmt decimal(22,2) ,StartMonthpayBudgetRollingAmt decimal(22,2) ,EndMonthpayBudgetRollingAmt decimal(22,2)
,SumpayBudgetRollingchangesAmt decimal(22,2) ,StartMonthpayBudgetRollingchangesAmt decimal(22,2) ,EndMonthpayBudgetRollingchangesAmt decimal(22,2)
,SumcumulativepayBudgetRollingAmt decimal(22,2) ,StartMonthcumulativepayBudgetRollingAmt decimal(22,2) ,EndMonthcumulativepayBudgetRollingAmt decimal(22,2)
,SumHavecancelafterverificationAmt decimal(22,2) ,StartMonthHavecancelafterverificationAmt decimal(22,2) ,EndMonthHavecancelafterverificationAmt decimal(22,2)
,SumHavebeenfrozenAmt decimal(22,2) ,StartHavebeenfrozenAmt decimal(22,2) ,EndMonthHavebeenfrozenAmt decimal(22,2)
,CanpaythebalanceAmt decimal(22,2))

-- --专项管理费用(合同)
if object_id('tempdb..#FM_SpecialExpenses') is not null drop table #FM_SpecialExpenses
create table #FM_SpecialExpenses(NodeID varchar(300) not null,MatterContractID int ,ProjectID int ,CompanyID int,BudgetCodeID int
,COMPANYNAME varchar(500),ProjectName varchar(500),DeptID int ,DeptName varchar(500)
,CostCategory varchar(500),TwoCodeName varchar(500),ThreeCodeName varchar(500)
,FourCodeName varchar(500),ContractNo varchar(500)
,ContractAName varchar(500),PartyUnit varchar(5000),ContractAmount decimal(22,2)
,SumBudgetRollingAmt decimal(22,2) ,StartMonthBudgetRollingAmt decimal(22,2) ,EndMonthBudgetRollingAmt decimal(22,2)
,SumBudgetRollingissuedAmt decimal(22,2) ,StartMonthBudgetRollingissuedAmt decimal(22,2) ,EndMonthBudgetRollingissuedAmt decimal(22,2)
,SumpayBudgetRollingAmt decimal(22,2) ,StartMonthpayBudgetRollingAmt decimal(22,2) ,EndMonthpayBudgetRollingAmt decimal(22,2)
,SumpayBudgetRollingchangesAmt decimal(22,2) ,StartMonthpayBudgetRollingchangesAmt decimal(22,2) ,EndMonthpayBudgetRollingchangesAmt decimal(22,2)
,SumcumulativepayBudgetRollingAmt decimal(22,2) ,StartMonthcumulativepayBudgetRollingAmt decimal(22,2) ,EndMonthcumulativepayBudgetRollingAmt decimal(22,2)
,SumHavecancelafterverificationAmt decimal(22,2) ,StartMonthHavecancelafterverificationAmt decimal(22,2) ,EndMonthHavecancelafterverificationAmt decimal(22,2)
,SumHavebeenfrozenAmt decimal(22,2) ,StartHavebeenfrozenAmt decimal(22,2) ,EndMonthHavebeenfrozenAmt decimal(22,2)
,CanpaythebalanceAmt decimal(22,2))
--剩余的科目 管理费用,经营收支,投融资流出
if object_id('tempdb..#FM_remainingsubjects') is not null drop table #FM_remainingsubjects
create table #FM_remainingsubjects(NodeID varchar(300) not null,ContractLibID int ,ProjectID int ,CompanyID int,BudgetCodeID int
,COMPANYNAME varchar(500),ProjectName varchar(500),DeptID int ,DeptName varchar(500)
,CostCategory varchar(500),TwoCodeName varchar(500),ThreeCodeName varchar(500)
,FourCodeName varchar(500),ContractNo varchar(500)
,ContractAName varchar(500),PartyUnit varchar(5000),ContractAmount decimal(22,2)
,SumBudgetRollingAmt decimal(22,2) ,StartMonthBudgetRollingAmt decimal(22,2) ,EndMonthBudgetRollingAmt decimal(22,2)
,SumBudgetRollingissuedAmt decimal(22,2) ,StartMonthBudgetRollingissuedAmt decimal(22,2) ,EndMonthBudgetRollingissuedAmt decimal(22,2)
,SumpayBudgetRollingAmt decimal(22,2) ,StartMonthpayBudgetRollingAmt decimal(22,2) ,EndMonthpayBudgetRollingAmt decimal(22,2)
,SumpayBudgetRollingchangesAmt decimal(22,2) ,StartMonthpayBudgetRollingchangesAmt decimal(22,2) ,EndMonthpayBudgetRollingchangesAmt decimal(22,2)
,SumcumulativepayBudgetRollingAmt decimal(22,2) ,StartMonthcumulativepayBudgetRollingAmt decimal(22,2) ,EndMonthcumulativepayBudgetRollingAmt decimal(22,2)
,SumHavecancelafterverificationAmt decimal(22,2) ,StartMonthHavecancelafterverificationAmt decimal(22,2) ,EndMonthHavecancelafterverificationAmt decimal(22,2)
,SumHavebeenfrozenAmt decimal(22,2) ,StartHavebeenfrozenAmt decimal(22,2) ,EndMonthHavebeenfrozenAmt decimal(22,2)
,CanpaythebalanceAmt decimal(22,2))

-- 薪酬福利
if object_id('tempdb..#FM_Salaries') is not null drop table #FM_Salaries
create table #FM_Salaries(NodeID varchar(300) not null,ContractLibID int ,ProjectID int ,CompanyID int,BudgetCodeID int
,COMPANYNAME varchar(500),ProjectName varchar(500),DeptID int ,DeptName varchar(500)
,CostCategory varchar(500),TwoCodeName varchar(500),ThreeCodeName varchar(500)
,FourCodeName varchar(500),ContractNo varchar(500)
,ContractAName varchar(500),PartyUnit varchar(5000),ContractAmount decimal(22,2)
,SumBudgetRollingAmt decimal(22,2) ,StartMonthBudgetRollingAmt decimal(22,2) ,EndMonthBudgetRollingAmt decimal(22,2)
,SumBudgetRollingissuedAmt decimal(22,2) ,StartMonthBudgetRollingissuedAmt decimal(22,2) ,EndMonthBudgetRollingissuedAmt decimal(22,2)
,SumpayBudgetRollingAmt decimal(22,2) ,StartMonthpayBudgetRollingAmt decimal(22,2) ,EndMonthpayBudgetRollingAmt decimal(22,2)
,SumpayBudgetRollingchangesAmt decimal(22,2) ,StartMonthpayBudgetRollingchangesAmt decimal(22,2) ,EndMonthpayBudgetRollingchangesAmt decimal(22,2)
,SumcumulativepayBudgetRollingAmt decimal(22,2) ,StartMonthcumulativepayBudgetRollingAmt decimal(22,2) ,EndMonthcumulativepayBudgetRollingAmt decimal(22,2)
,SumHavecancelafterverificationAmt decimal(22,2) ,StartMonthHavecancelafterverificationAmt decimal(22,2) ,EndMonthHavecancelafterverificationAmt decimal(22,2)
,SumHavebeenfrozenAmt decimal(22,2) ,StartHavebeenfrozenAmt decimal(22,2) ,EndMonthHavebeenfrozenAmt decimal(22,2)
,CanpaythebalanceAmt decimal(22,2))





--SELECT * FROM #FM_BudgetRollingDtlHistory WHERE contractlibid=7318
--SELECT @versionfirsto,@versionlast
--处理可支付最后打版
if ( @versionfirsto >0 )
begin
if (@versionlast>0)
begin
--开的发成本
insert into #FM_Cost(NodeID ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt )
SELECT
FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN isnull(bg1.CodeName,'') ELSE isnull(bg.CodeName,'') end as CostCategory
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN isnull(bg2.CodeName,'') ELSE isnull(bg1.CodeName,'') end as TwoCodeName
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN '' ELSE isnull(bg2.CodeName,'') end as ThreeCodeName
,'' as FourCodeName
,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit ,isnull(clib.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
,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 StartMonthcumulativepayBudgetRollingAmt
,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 EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
,0 as SumHavecancelafterverificationAmt ,0 as StartMonthHavecancelafterverificationAmt ,0 as EndMonthHavecancelafterverificationAmt --申请付款金额(冻结)
,0 as SumHavebeenfrozenAmt ,0 as StartHavebeenfrozenAmt ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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=pj.CompanyID and cy.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
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg.RecordStatus='active' and bg.CodeLevel=1
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@versionlast AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth 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
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN isnull(bg1.CodeName,'') ELSE isnull(bg.CodeName,'') end as CostCategory
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN isnull(bg2.CodeName,'') ELSE isnull(bg1.CodeName,'') end as TwoCodeName
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN '' ELSE isnull(bg2.CodeName,'') end as ThreeCodeName
,'' as FourCodeName
,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit ,isnull(clib.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
,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 StartMonthpayBudgetRollingAmt
,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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=pj.CompanyID and cy.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
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg.RecordStatus='active' and bg.CodeLevel=1
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@versionfirsto
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth 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
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN isnull(bg1.CodeName,'') ELSE isnull(bg.CodeName,'') end as CostCategory
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN isnull(bg2.CodeName,'') ELSE isnull(bg1.CodeName,'') end as TwoCodeName
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN '' ELSE isnull(bg2.CodeName,'') end as ThreeCodeName
,'' as FourCodeName
,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit ,isnull(clib.ContractAmount,'0.00') as ContractAmount
, 0 as SumBudgetRollingAmt
, 0 as StartMonthBudgetRollingAmt
,0 as EndMonthBudgetRollingAmt --双月预算上报金额
,0 as SumBudgetRollingissuedAmt
,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 StartMonthBudgetRollingissuedAmt
, 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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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=pj.CompanyID and cy.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
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg1.RecordStatus='active' and bg.CodeLevel=1
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth and FBRH.EndMonth=@EndMonth

UNION ALL

--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结

SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN isnull(bg1.CodeName,'') ELSE isnull(bg.CodeName,'') end as CostCategory
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN isnull(bg2.CodeName,'') ELSE isnull(bg1.CodeName,'') end as TwoCodeName
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN '' ELSE isnull(bg2.CodeName,'') end as ThreeCodeName
,'' as FourCodeName
,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit ,isnull(clib.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_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=pj.CompanyID and cy.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
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg.RecordStatus='active' and bg.CodeLevel=1
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
--实际
LEFT JOIN VIEW_FM_DevelopmentCost_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRDH.ProjectID=vfda.ProjectID AND FBRDH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID AND FBRDH.ContractLibID=vfda.ContractLibID
--冻结
LEFT JOIN FM_ActualCostItemDtlFromSystemDevelopmentCostView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRDH.ProjectID=vfds.ProjectID AND FBRDH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@StartMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID AND FBRDH.ContractLibID=vfds.ContractCode
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@versionlast
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth 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
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN isnull(bg1.CodeName,'') ELSE isnull(bg.CodeName,'') end as CostCategory
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN isnull(bg2.CodeName,'') ELSE isnull(bg1.CodeName,'') end as TwoCodeName
,CASE WHEN bg2.fullbudgetcode LIKE '03.%' THEN '' ELSE isnull(bg2.CodeName,'') end as ThreeCodeName
,'' as FourCodeName
,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit ,isnull(clib.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 @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_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=pj.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
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg.RecordStatus='active' and bg.CodeLevel=1
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.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=@versionlast
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0) and FBRH.StartMonth=@StartMonth
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
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt )
SELECT --最后一版
FBRDH.BudgetRollingDtlID as NodeID, isnull(FBRDH.MatterContractID,0) as MatterContractID,isnull(FBRH.ProjectID,0) as ProjectID ,isnull(FBRH.CompanyID,0)as CompanyID , isnull(FBRDH.BudgetCodeID ,0) as BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,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.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
,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 StartMonthcumulativepayBudgetRollingAmt
,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 EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
,0 as SumHavecancelafterverificationAmt ,0 as StartMonthHavecancelafterverificationAmt ,0 as EndMonthHavecancelafterverificationAmt --申请付款金额(冻结)
,0 as SumHavebeenfrozenAmt,0 as StartHavebeenfrozenAmt ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
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'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses' --and FBRIH.RecordStatus='active' and FBRDH.RecordStatus='active'
and FBRH.VersionID=@versionlast
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth and FBRH.EndMonth=@EndMonth

UNION ALL --可支付第一版

SELECT
FBRDH.BudgetRollingDtlID as NodeID, isnull(FBRDH.MatterContractID,0) as MatterContractID,isnull(FBRH.ProjectID,0) as ProjectID ,isnull(FBRH.CompanyID,0)as CompanyID , isnull(FBRDH.BudgetCodeID ,0) as BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,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.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
,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 StartMonthpayBudgetRollingAmt
,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
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'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses' --and FBRIH.RecordStatus='active' and FBRDH.RecordStatus='active'
and FBRH.VersionID=@versionfirsto
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth and FBRH.EndMonth=@EndMonth

UNION ALL --双月下发
select
FBRDH.BudgetRollingDtlID as NodeID, isnull(FBRDH.MatterContractID,0) as MatterContractID,isnull(FBRH.ProjectID,0) as ProjectID ,isnull(FBRH.CompanyID,0)as CompanyID , isnull(FBRDH.BudgetCodeID ,0) as BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,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.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
,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 StartMonthBudgetRollingissuedAmt
,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
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'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses' --and FBRIH.RecordStatus='active' and FBRDH.RecordStatus='active'
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 and FBRH.EndMonth=@EndMonth


-- UNION ALL --双月上报

-- -- SELECT
-- -- FBRDH.BudgetRollingDtlID as NodeID, FBRDH.MatterContractID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.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
-- -- , 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 StartMonthBudgetRollingAmt
-- -- ,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
-- -- ,0 as CanpaythebalanceAmt --可用余额
-- -- 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 ) 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'
-- --WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses'-- and FBRIH.RecordStatus='active' and FBRDH.RecordStatus='active'
-- -- AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
-- -- and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
-- --AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT
FBRDH.BudgetRollingDtlID as NodeID, isnull(FBRDH.MatterContractID,0) as MatterContractID,isnull(FBRH.ProjectID,0) as ProjectID ,isnull(FBRH.CompanyID,0)as CompanyID , isnull(FBRDH.BudgetCodeID ,0) as BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,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.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_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 ) 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 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 FBRIH.RecordStatus='active' and FBRDH.RecordStatus='active'
and FBRH.VersionID=@versionlast
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, isnull(FBRDH.MatterContractID,0) as MatterContractID,isnull(FBRH.ProjectID,0) as ProjectID ,isnull(FBRH.CompanyID,0)as CompanyID , isnull(FBRDH.BudgetCodeID ,0) as BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,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.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_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 ) 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 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 FBRIH.RecordStatus='active' and FBRDH.RecordStatus='active'
and FBRH.VersionID=@versionlast
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth

--SELECT * FROM #FM_SpecialExpenses

--预算外
insert into #FM_SpecialExpenses( NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt)
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(bg.CodeName,'') as CostCategory ,isnull(bg1.CodeName,'') as TwoCodeName,isnull(bg2.CodeName,'') as ThreeCodeName
, isnull(bg3.CodeName,'') 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 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.DeptID <>@DeptID or 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 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 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.StartMonth=@StartMonth
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 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.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 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 ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt )

SELECT ( cast(FBRIH.BudgetRollingItemID as varchar(22)) +'-'+cast( FBRH.BudgetRollingID as varchar(22)) )as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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
,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 StartMonthcumulativepayBudgetRollingAmt
,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 EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
,0 as SumHavecancelafterverificationAmt ,0 as StartMonthHavecancelafterverificationAmt ,0 as EndMonthHavecancelafterverificationAmt --申请付款金额(冻结)
,0 as SumHavebeenfrozenAmt,0 as StartHavebeenfrozenAmt ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut') --and FBRIH.RecordStatus='active'
and FBRH.VersionID=@versionlast
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 ( cast(FBRIH.BudgetRollingItemID as varchar(22)) +'-'+cast( FBRH.BudgetRollingID as varchar(22)) ) as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME ,isnull(pj.ProjectName,'') as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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
,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 StartMonthpayBudgetRollingAmt
,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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_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

WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut') --and FBRIH.RecordStatus='active'
and FBRH.VersionID=@versionfirsto
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 ( cast(FBRIH.BudgetRollingItemID as varchar(22)) +'-'+cast( FBRH.BudgetRollingID as varchar(22)) ) as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'0.00' as ContractAmount
,0 as SumBudgetRollingAmt ,0 as StartMonthBudgetRollingAmt ,0 as EndMonthBudgetRollingAmt --双月预算上报金额
,0 as SumBudgetRollingissuedAmt
,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 StartMonthBudgetRollingissuedAmt
,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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_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

WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut') --and FBRIH.RecordStatus='active'
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 ( cast(FBRIH.BudgetRollingItemID as varchar(22)) +'-'+cast( FBRH.BudgetRollingID as varchar(22)) ) as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
--,isnull( cy.COMPANYNAME,'') as COMPANYNAME , pj.ProjectName as ProjectName
--,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'0.00' as ContractAmount
-- ,0 as SumBudgetRollingAmt
-- ,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 StartMonthBudgetRollingAmt
-- ,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
-- ,0 as CanpaythebalanceAmt --可用余额
-- 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_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

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

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT ( cast(FBRIH.BudgetRollingItemID as varchar(22)) +'-'+cast( FBRH.BudgetRollingID as varchar(22)) ) as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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(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 StartMonthHavecancelafterverificationAmt

,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 EndMonthHavecancelafterverificationAmt --已付款金额(核减)
,0 as SumHavebeenfrozenAmt
,
case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00')) end as StartHavebeenfrozenAmt --申请付款金额(冻结)
,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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_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 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='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@versionlast
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth -- and FBRH.EndMonth=@EndMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT ( cast(FBRIH.BudgetRollingItemID as varchar(22)) +'-'+cast( FBRH.BudgetRollingID as varchar(22)) ) as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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 @EndMonth =1 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00')) end as EndMonthHavebeenfrozenAmt --已付款金额(冻结)
,0 as CanpaythebalanceAmt --可用余额
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_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 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 FM_ActualCostItemFromSystemGeneralExpensesView gvfds ON gvfds.CompanyID=FBRIH.CompanyID
AND gvfds.DeptID=FBRIH.DeptID AND gvfds.BudgetType=FBRH.BudgetType AND gvfds.monthly=@EndMonth
AND gvfds.BudgetYear=FBRH.BudgetYear AND FBRIH.BudgetCodeID=gvfds.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='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@versionlast
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth --and FBRH.StartMonth=@StartMonth


-- 薪酬福利
insert into #FM_Salaries(NodeID ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt )
SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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
,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 StartMonthcumulativepayBudgetRollingAmt
,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 EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
,0 as SumHavecancelafterverificationAmt ,0 as StartMonthHavecancelafterverificationAmt ,0 as EndMonthHavecancelafterverificationAmt --申请付款金额(冻结)
,0 as SumHavebeenfrozenAmt,0 as StartHavebeenfrozenAmt ,0 as EndMonthHavebeenfrozenAmt
,0 as CanpaythebalanceAmt --可用余额
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
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries' --and FBRIH.RecordStatus='active'
and FBRH.VersionID=@versionlast
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 --可支付第一版
SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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
,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 StartMonthpayBudgetRollingAmt
,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries' --and FBRIH.RecordStatus='active'
and FBRH.VersionID=@versionfirsto
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 --双月预算下发金额
SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'0.00' as ContractAmount
,0 as SumBudgetRollingAmt ,0 as StartMonthBudgetRollingAmt ,0 as EndMonthBudgetRollingAmt --双月预算上报金额
,0 as SumBudgetRollingissuedAmt
,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 StartMonthBudgetRollingissuedAmt
,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries' --and FBRIH.RecordStatus='active'
and FBRH.VersionID=@versionA
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary' and bc.CodeType not in('Income')
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth and FBRH.EndMonth=@EndMonth

-- UNION ALL --双月上报

--SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
--,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName
--,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'0.00' as ContractAmount
-- ,0 as SumBudgetRollingAmt
-- ,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 StartMonthBudgetRollingAmt
-- ,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
-- ,0 as CanpaythebalanceAmt --可用余额
-- 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
--WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries'-- and FBRIH.RecordStatus='active'
-- AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
--AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth and FBRH.EndMonth=@EndMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结

SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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.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 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_BudgetRollingHistory(nolock) FBRH
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.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 VIEW_FM_Salaries_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
--冻结
LEFT JOIN FM_ActualCostItemFromSystemSalariesView 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
WHERE FBRH.RecordStatus='active' AND FBRH.BudgetType='Salaries'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@versionlast
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary' and bc.CodeType not in('Income')
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 结束月冻结

SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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 @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_BudgetRollingHistory(nolock) FBRH
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.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 FM_ActualCostItemFromSystemSalariesView 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
WHERE FBRH.RecordStatus='active' AND FBRH.BudgetType='Salaries'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@versionlast
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary' and bc.CodeType not in('Income')
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

end
end
if (@versionlast<0 or @versionlast is null)
begin

--开的发成本
insert into #FM_Cost(NodeID ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt )
--可支付第一版
SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN isnull(bg1.CodeName,'') ELSE isnull(bg.CodeName,'') end as CostCategory
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN isnull(bg2.CodeName,'') ELSE isnull(bg1.CodeName,'') end as TwoCodeName
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN '' ELSE isnull(bg2.CodeName,'') end as ThreeCodeName
,'' as FourCodeName
,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit ,isnull(clib.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
,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 StartMonthpayBudgetRollingAmt
,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 EndMonthpayBudgetRollingAmt -- 可支付下发金额
,0 as SumpayBudgetRollingchangesAmt ,0 as StartMonthpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as SumcumulativepayBudgetRollingAmt
,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 StartMonthcumulativepayBudgetRollingAmt
,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 EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
,0 as SumHavecancelafterverificationAmt ,0 as StartMonthHavecancelafterverificationAmt ,0 as EndMonthHavecancelafterverificationAmt --申请付款金额(冻结)
,0 as SumHavebeenfrozenAmt ,0 as StartHavebeenfrozenAmt ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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=pj.CompanyID and cy.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
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg.RecordStatus='active' and bg.CodeLevel=1
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@versionfirsto
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth 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
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN isnull(bg1.CodeName,'') ELSE isnull(bg.CodeName,'') end as CostCategory
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN isnull(bg2.CodeName,'') ELSE isnull(bg1.CodeName,'') end as TwoCodeName
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN '' ELSE isnull(bg2.CodeName,'') end as ThreeCodeName
,'' as FourCodeName
,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit ,isnull(clib.ContractAmount,'0.00') as ContractAmount
, 0 as SumBudgetRollingAmt
, 0 as StartMonthBudgetRollingAmt
,0 as EndMonthBudgetRollingAmt --双月预算上报金额
,0 as SumBudgetRollingissuedAmt
,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 StartMonthBudgetRollingissuedAmt
, 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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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=pj.CompanyID and cy.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
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg.RecordStatus='active' and bg.CodeLevel=1
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and bg3.CodeCategory='Cost' and bg3.CodeType='Expenditure'
and FBRH.VersionID=@versionA
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth and FBRH.EndMonth=@EndMonth

UNION ALL

--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结

SELECT FBRDH.BudgetRollingDtlID as NodeID,clib.ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN isnull(bg1.CodeName,'') ELSE isnull(bg.CodeName,'') end as CostCategory
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN isnull(bg2.CodeName,'') ELSE isnull(bg1.CodeName,'') end as TwoCodeName
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN '' ELSE isnull(bg2.CodeName,'') end as ThreeCodeName
,'' as FourCodeName
,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit ,isnull(clib.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_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=pj.CompanyID and cy.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
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg.RecordStatus='active' and bg.CodeLevel=1
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.ContractLibID
--实际
LEFT JOIN VIEW_FM_DevelopmentCost_Actual vfda ON FBRH.CompanyID=vfda.CompanyID
AND FBRDH.ProjectID=vfda.ProjectID AND FBRDH.DeptID=vfda.DeptID AND FBRH.BudgetType=vfda.BudgetType
AND FBRH.BudgetYear=vfda.BudgetYear
AND FBRIH.BudgetCodeID=vfda.BudgetCodeID AND FBRDH.ContractLibID=vfda.ContractLibID
--冻结
LEFT JOIN FM_ActualCostItemDtlFromSystemDevelopmentCostView vfds ON FBRH.CompanyID=vfds.CompanyID
AND FBRDH.ProjectID=vfds.ProjectID AND FBRDH.DeptID=vfds.DeptID AND FBRH.BudgetType=vfds.BudgetType
AND FBRH.BudgetYear=vfds.BudgetYear AND vfds.monthly=@StartMonth
AND FBRIH.BudgetCodeID=vfds.BudgetCodeID AND FBRDH.ContractLibID=vfds.ContractCode
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='DevelopmentCost'
and FBRH.VersionID=@versionfirsto
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth 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
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN isnull(bg1.CodeName,'') ELSE isnull(bg.CodeName,'') end as CostCategory
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN isnull(bg2.CodeName,'') ELSE isnull(bg1.CodeName,'') end as TwoCodeName
,CASE WHEN bg2.fullbudgetcode LIKE '03%' THEN '' ELSE isnull(bg2.CodeName,'') end as ThreeCodeName
,'' as FourCodeName
,isnull(clib.ContractNo,'') as ContractNo
,isnull(clib.ContractAName,'') as ContractAName ,isnull(clib.PartyUnit,'') as PartyUnit ,isnull(clib.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 @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_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=pj.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
left join FM_BudgetCode(nolock) bg on bg.BudgetCodeID=bg1.ParentID and bg.RecordStatus='active' and bg.CodeLevel=1
inner join #FM_ContractLib(nolock) clib on FBRDH.ContractLibID=clib.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=@versionfirsto
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0) and FBRH.StartMonth=@StartMonth
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
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt )
--可支付第一版

SELECT
FBRDH.BudgetRollingDtlID as NodeID, isnull(FBRDH.MatterContractID,0) as MatterContractID,isnull(FBRH.ProjectID,0) as ProjectID ,isnull(FBRH.CompanyID,0)as CompanyID , isnull(FBRDH.BudgetCodeID ,0) as BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,isnull(bg.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
,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 StartMonthpayBudgetRollingAmt
,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 EndMonthpayBudgetRollingAmt -- 可支付下发金额
,0 as SumpayBudgetRollingchangesAmt ,0 as StartMonthpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as SumcumulativepayBudgetRollingAmt
,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 StartMonthcumulativepayBudgetRollingAmt
,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 EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
,0 as SumHavecancelafterverificationAmt ,0 as StartMonthHavecancelafterverificationAmt ,0 as EndMonthHavecancelafterverificationAmt --申请付款金额(冻结)
,0 as SumHavebeenfrozenAmt,0 as StartHavebeenfrozenAmt ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
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'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses' --and FBRIH.RecordStatus='active' and FBRDH.RecordStatus='active'
and FBRH.VersionID=@versionfirsto
and bc.CodeCategory='SpecialExpenses' and bc.CodeType='Expenditure'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth and FBRH.EndMonth=@EndMonth

UNION ALL --双月下发

SELECT
FBRDH.BudgetRollingDtlID as NodeID, isnull(FBRDH.MatterContractID,0) as MatterContractID,isnull(FBRH.ProjectID,0) as ProjectID ,isnull(FBRH.CompanyID,0)as CompanyID , isnull(FBRDH.BudgetCodeID ,0) as BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,isnull(bg.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
,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 StartMonthBudgetRollingissuedAmt
,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
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'
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='SpecialExpenses' --and FBRIH.RecordStatus='active' and FBRDH.RecordStatus='active'
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 and FBRH.EndMonth=@EndMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT
FBRDH.BudgetRollingDtlID as NodeID, isnull(FBRDH.MatterContractID,0) as MatterContractID,isnull(FBRH.ProjectID,0) as ProjectID ,isnull(FBRH.CompanyID,0)as CompanyID , isnull(FBRDH.BudgetCodeID ,0) as BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,isnull(bg.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_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 ) 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 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 FBRIH.RecordStatus='active' and FBRDH.RecordStatus='active'
and FBRH.VersionID=@versionfirsto
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, isnull(FBRDH.MatterContractID,0) as MatterContractID,isnull(FBRH.ProjectID,0) as ProjectID ,isnull(FBRH.CompanyID,0)as CompanyID , isnull(FBRDH.BudgetCodeID ,0) as BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName,dt.DEPTID as DeptID,isnull( dt.DeptName,'') as DeptName
,isnull(bg.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_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 ) 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 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 FBRIH.RecordStatus='active' and FBRDH.RecordStatus='active'
and FBRH.VersionID=@versionfirsto
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_SpecialExpenses( NodeID ,MatterContractID ,ProjectID ,CompanyID ,BudgetCodeID
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt)
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 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 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.DeptID <>@DeptID or 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 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 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.StartMonth=@StartMonth
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,
vfds.ContractCode ,0 as ProjectID ,vfds.CompanyID ,vfds.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.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,vfds.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 ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt )
--可支付第一版
SELECT ( cast(FBRIH.BudgetRollingItemID as varchar(22)) +'-'+cast( FBRIH.BudgetRollingID as varchar(22))) as NodeID, 0 as ContractLibID , isnull(FBRIH.ProjectID,0) ,isnull(FBRIH.CompanyID,0) ,isnull(FBRIH.BudgetCodeID ,0)
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,isnull(FBRIH.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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
,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 StartMonthpayBudgetRollingAmt
,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 EndMonthpayBudgetRollingAmt -- 可支付下发金额
,0 as SumpayBudgetRollingchangesAmt ,0 as StartMonthpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as SumcumulativepayBudgetRollingAmt
,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 StartMonthcumulativepayBudgetRollingAmt
,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 EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
,0 as SumHavecancelafterverificationAmt ,0 as StartMonthHavecancelafterverificationAmt ,0 as EndMonthHavecancelafterverificationAmt --申请付款金额(冻结)
,0 as SumHavebeenfrozenAmt,0 as StartHavebeenfrozenAmt ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
from #FM_BudgetRollingHistory(nolock) FBRH
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_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_DEPT(nolock) dt on dt.DEPTID=FBRH.DEPTID and dt.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
WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut') --and FBRIH.RecordStatus='active'
and FBRH.VersionID=@versionfirsto and FBRIH.VersionID= @versionfirsto
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth and FBRH.EndMonth=@EndMonth

--select * from #FM_remainingsubjects
--select distinct * from #FM_BudgetRollingItemHistory where VersionID=@versionfirsto
UNION ALL -- 双月预算下发金额

SELECT ( cast(FBRIH.BudgetRollingItemID as varchar(22)) +'-'+cast( FBRH.BudgetRollingID as varchar(22)) ) as NodeID, 0 as ContractLibID ,isnull (FBRH.ProjectID,0) as ProjectID, isnull(FBRH.CompanyID,0) as CompanyID,isnull(FBRIH.BudgetCodeID,0) as BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'0.00' as ContractAmount
,0 as SumBudgetRollingAmt ,0 as StartMonthBudgetRollingAmt ,0 as EndMonthBudgetRollingAmt --双月预算上报金额
,0 as SumBudgetRollingissuedAmt
,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 StartMonthBudgetRollingissuedAmt
,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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_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

WHERE FBRH.RecordStatus='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut') --and FBRIH.RecordStatus='active'
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
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT ( cast(FBRIH.BudgetRollingItemID as varchar(22)) +'-'+cast( FBRH.BudgetRollingID as varchar(22)) ) as NodeID, 0 as ContractLibID ,isnull(FBRH.ProjectID,0) ,isnull(FBRH.CompanyID,0) ,isnull(FBRIH.BudgetCodeID ,0)
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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(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 StartMonthHavecancelafterverificationAmt

,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 EndMonthHavecancelafterverificationAmt --已付款金额(核减)
,0 as SumHavebeenfrozenAmt
,
case when @StartMonth=1 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=3 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=5 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=7 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=2 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=4 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=6 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=8 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=9 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=10 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @StartMonth=11 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00')) end as StartHavebeenfrozenAmt --申请付款金额(冻结)
,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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_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 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='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@versionfirsto and FBRIH.VersionID=@versionfirsto
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth -- and FBRH.EndMonth=@EndMonth

UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结
SELECT ( cast(FBRIH.BudgetRollingItemID as varchar(22)) +'-'+cast( FBRH.BudgetRollingID as varchar(22)) ) as NodeID, 0 as ContractLibID ,isnull(FBRH.ProjectID,0) ,isnull(FBRH.CompanyID,0) ,isnull(FBRIH.BudgetCodeID ,0)
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , isnull(pj.ProjectName,'') as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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 @EndMonth =1 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=3 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=5 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=7 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=2 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=4 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=6 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=8 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=9 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=10 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
when @EndMonth=11 then (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00'))
else (isnull(vfds.FreeAmt,'0.00')+isnull(gvfds.FreeAmt,'0.00')+isnull(Avfds.FreeAmt,'0.00')) end as EndMonthHavebeenfrozenAmt --已付款金额(冻结)
,0 as CanpaythebalanceAmt --可用余额
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_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 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 FM_ActualCostItemFromSystemGeneralExpensesView gvfds ON gvfds.CompanyID=FBRIH.CompanyID
AND gvfds.DeptID=FBRIH.DeptID AND gvfds.BudgetType=FBRH.BudgetType AND gvfds.monthly=@EndMonth
AND gvfds.BudgetYear=FBRH.BudgetYear AND FBRIH.BudgetCodeID=gvfds.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='Approved' AND (FBRH.BudgetType ='GeneralExpenses' or FBRH.BudgetType ='AbsorbInvestment' or FBRH.BudgetType ='PropertyInOut')
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@versionfirsto
and (bc.CodeCategory='CashFlow' OR bc.CodeCategory='OtherInOut' OR bc.CodeCategory='GeneralExpenses' ) AND bc.CodeType='Expenditure'
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth --and FBRH.StartMonth=@StartMonth

--薪酬福利
insert into #FM_Salaries(NodeID ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt )
--可支付第一版
SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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
,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 StartMonthpayBudgetRollingAmt
,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 EndMonthpayBudgetRollingAmt -- 可支付下发金额
,0 as SumpayBudgetRollingchangesAmt ,0 as StartMonthpayBudgetRollingchangesAmt ,0 as EndMonthpayBudgetRollingchangesAmt --可支付增减变化金额 最后一次减去第一版(如果存在多版本可支付)
,0 as SumcumulativepayBudgetRollingAmt
,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 StartMonthcumulativepayBudgetRollingAmt
,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 EndMonthcumulativepayBudgetRollingAmt --累计可支付预算 最后一版可支付
,0 as SumHavecancelafterverificationAmt ,0 as StartMonthHavecancelafterverificationAmt ,0 as EndMonthHavecancelafterverificationAmt --申请付款金额(冻结)
,0 as SumHavebeenfrozenAmt,0 as StartHavebeenfrozenAmt ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries' --and FBRIH.RecordStatus='active'
and FBRH.VersionID=@versionfirsto
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 --双月预算下发金额
SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'0.00' as ContractAmount
,0 as SumBudgetRollingAmt ,0 as StartMonthBudgetRollingAmt ,0 as EndMonthBudgetRollingAmt --双月预算上报金额
,0 as SumBudgetRollingissuedAmt
,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 StartMonthBudgetRollingissuedAmt
,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 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 ,0 as EndMonthHavebeenfrozenAmt --已付款金额(核减)
,0 as CanpaythebalanceAmt --可用余额
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
WHERE FBRH.RecordStatus='Approved' AND FBRH.BudgetType='Salaries' --and FBRIH.RecordStatus='active'
and FBRH.VersionID=@versionA
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary' and bc.CodeType not in('Income')
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0)
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth and FBRH.EndMonth=@EndMonth


UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 开始月冻结

SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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.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 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_BudgetRollingHistory(nolock) FBRH
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.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 VIEW_FM_Salaries_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
--冻结
LEFT JOIN FM_ActualCostItemFromSystemSalariesView 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
WHERE FBRH.RecordStatus='active' AND FBRH.BudgetType='Salaries'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@versionfirsto
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary' and bc.CodeType not in('Income')
AND FBRH.BudgetYear=@BudgetYear AND FBRH.StartMonth=@StartMonth
UNION ALL
--1.2. 获取股份公司下发可支付版本的预算 实际 结束月冻结

SELECT FBRIH.BudgetRollingDtlID as NodeID, 0 as ContractLibID ,FBRH.ProjectID ,FBRH.CompanyID ,FBRIH.BudgetCodeID
,isnull( cy.COMPANYNAME,'') as COMPANYNAME , '' as ProjectName
,isnull(dt.DeptID,'0') as DeptID ,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 ContractNo ,'' as ContractAName ,'' as PartyUnit ,'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 @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_BudgetRollingHistory(nolock) FBRH
LEFT join CF_COMPANY (nolock) cy on cy.CompanyID=FBRH.CompanyID and cy.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 FM_ActualCostItemFromSystemSalariesView 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
WHERE FBRH.RecordStatus='active' AND FBRH.BudgetType='Salaries'
AND (FBRH.CompanyID=@CompanyId or @CompanyId=0 ) and FBRH.VersionID=@versionfirsto
and bc.FullBudgetCode NOT LIKE '05.03%' AND bc.CodeCategory ='Salary' and bc.CodeType not in('Income')
AND FBRH.BudgetYear=@BudgetYear AND FBRH.EndMonth=@EndMonth

 

end



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

create table #FM_all(NodeID varchar(500) not null,ContractLibID int ,ProjectID int ,CompanyID int,BudgetCodeID int
,COMPANYNAME varchar(500),ProjectName varchar(500),DeptID int ,DeptName varchar(500)
,CostCategory varchar(500),TwoCodeName varchar(500),ThreeCodeName varchar(500)
,FourCodeName varchar(500),ContractNo varchar(500)
,ContractAName varchar(500),PartyUnit varchar(5000),ContractAmount decimal(22,2)
,SumBudgetRollingAmt decimal(22,2) ,StartMonthBudgetRollingAmt decimal(22,2) ,EndMonthBudgetRollingAmt decimal(22,2)
,SumBudgetRollingissuedAmt decimal(22,2) ,StartMonthBudgetRollingissuedAmt decimal(22,2) ,EndMonthBudgetRollingissuedAmt decimal(22,2)
,SumpayBudgetRollingAmt decimal(22,2) ,StartMonthpayBudgetRollingAmt decimal(22,2) ,EndMonthpayBudgetRollingAmt decimal(22,2)
,SumpayBudgetRollingchangesAmt decimal(22,2) ,StartMonthpayBudgetRollingchangesAmt decimal(22,2) ,EndMonthpayBudgetRollingchangesAmt decimal(22,2)
,SumcumulativepayBudgetRollingAmt decimal(22,2) ,StartMonthcumulativepayBudgetRollingAmt decimal(22,2) ,EndMonthcumulativepayBudgetRollingAmt decimal(22,2)
,SumHavecancelafterverificationAmt decimal(22,2) ,StartMonthHavecancelafterverificationAmt decimal(22,2) ,EndMonthHavecancelafterverificationAmt decimal(22,2)
,SumHavebeenfrozenAmt decimal(22,2) ,StartHavebeenfrozenAmt decimal(22,2) ,EndMonthHavebeenfrozenAmt decimal(22,2)
,CanpaythebalanceAmt decimal(22,2))
insert into #FM_all(
NodeID ,ContractLibID ,ProjectID ,CompanyID ,BudgetCodeID
,COMPANYNAME,ProjectName ,DeptID ,DeptName
,CostCategory ,TwoCodeName ,ThreeCodeName ,FourCodeName ,ContractNo
,ContractAName ,PartyUnit ,ContractAmount
,SumBudgetRollingAmt ,StartMonthBudgetRollingAmt ,EndMonthBudgetRollingAmt
,SumBudgetRollingissuedAmt ,StartMonthBudgetRollingissuedAmt ,EndMonthBudgetRollingissuedAmt
,SumpayBudgetRollingAmt ,StartMonthpayBudgetRollingAmt ,EndMonthpayBudgetRollingAmt
,SumpayBudgetRollingchangesAmt ,StartMonthpayBudgetRollingchangesAmt ,EndMonthpayBudgetRollingchangesAmt
,SumcumulativepayBudgetRollingAmt ,StartMonthcumulativepayBudgetRollingAmt ,EndMonthcumulativepayBudgetRollingAmt
,SumHavecancelafterverificationAmt ,StartMonthHavecancelafterverificationAmt ,EndMonthHavecancelafterverificationAmt
,SumHavebeenfrozenAmt ,StartHavebeenfrozenAmt ,EndMonthHavebeenfrozenAmt
,CanpaythebalanceAmt )
SELECT cast(MAX(cs.NodeID) as varchar(22)) as NodeID,cs.ContractLibID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.COMPANYNAME,cs.ProjectName ,cs.DeptID ,cs.DeptName
,cs.CostCategory ,cs.TwoCodeName ,cs.ThreeCodeName ,cs.FourCodeName ,cs.ContractNo
,cs.ContractAName ,cs.PartyUnit ,cs.ContractAmount
, (isnull(sum (StartMonthBudgetRollingAmt),'0.00') +isnull(sum(EndMonthBudgetRollingAmt),'0.00')) as SumBudgetRollingAmt
,isnull(sum (StartMonthBudgetRollingAmt),'0.00') as StartMonthBudgetRollingAmt
,isnull(sum(EndMonthBudgetRollingAmt),'0.00') as EndMonthBudgetRollingAmt

,(isnull(sum (StartMonthBudgetRollingissuedAmt),'0.00') +isnull(sum(EndMonthBudgetRollingissuedAmt),'0.00')) as SumBudgetRollingissuedAmt
,isnull(sum (StartMonthBudgetRollingissuedAmt),'0.00') as StartMonthBudgetRollingissuedAmt
,isnull(sum(EndMonthBudgetRollingissuedAmt),'0.00') as EndMonthBudgetRollingissuedAmt
,(isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthpayBudgetRollingAmt),'0.00')) as SumpayBudgetRollingAmt
,isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') as StartMonthBudgetRollingAmt
,isnull(sum(EndMonthpayBudgetRollingAmt),'0.00') as EndMonthBudgetRollingAmt

,((isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')) - (isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthpayBudgetRollingAmt),'0.00'))) as SumpayBudgetRollingchangesAmt
,(isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00')- isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') )as StartMonthpayBudgetRollingchangesAmt
,(isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')-isnull(sum(EndMonthpayBudgetRollingAmt),'0.00')) as EndMonthpayBudgetRollingchangesAmt

,(isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')) as SumcumulativepayBudgetRollingAmt
,isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') as StartMonthcumulativepayBudgetRollingAmt
,isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00') as EndMonthcumulativepayBudgetRollingAmt

,(isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') +isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00')) as SumHavecancelafterverificationAmt
,isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') as StartMonthHavecancelafterverificationAmt
,isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00') as EndMonthHavecancelafterverificationAmt
,(isnull(sum (StartHavebeenfrozenAmt),'0.00') +isnull(sum(EndMonthHavebeenfrozenAmt),'0.00')) as SumHavebeenfrozenAmt
,isnull(sum (StartHavebeenfrozenAmt),'0.00') as StartHavebeenfrozenAmt
,isnull(sum(EndMonthHavebeenfrozenAmt),'0.00') as EndMonthHavecancelafterverificationAmt
,((isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00'))
-(isnull(sum (StartHavebeenfrozenAmt),'0.00') +isnull(sum(EndMonthHavebeenfrozenAmt),'0.00'))
-(isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') +isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00')))
from #FM_Cost (nolock) cs
GROUP BY cs.ContractLibID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.COMPANYNAME,cs.ProjectName ,cs.DeptID ,cs.DeptName
,cs.CostCategory ,cs.TwoCodeName ,cs.ThreeCodeName ,cs.FourCodeName ,cs.ContractNo
,cs.ContractAName ,cs.PartyUnit ,cs.ContractAmount
UNION ALL
SELECT cast(MAX(cs.NodeID) as varchar(22)) as NodeID,cs.MatterContractID as ContractLibID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.COMPANYNAME,cs.ProjectName ,cs.DeptID ,cs.DeptName
,cs.CostCategory ,cs.TwoCodeName ,cs.ThreeCodeName ,cs.FourCodeName ,cs.ContractNo
,cs.ContractAName ,cs.PartyUnit ,cs.ContractAmount
, (isnull(sum (StartMonthBudgetRollingAmt),'0.00') +isnull(sum(EndMonthBudgetRollingAmt),'0.00')) as SumBudgetRollingAmt
,isnull(sum (StartMonthBudgetRollingAmt),'0.00') as StartMonthBudgetRollingAmt
,isnull(sum(EndMonthBudgetRollingAmt),'0.00') as EndMonthBudgetRollingAmt

,(isnull(sum (StartMonthBudgetRollingissuedAmt),'0.00') +isnull(sum(EndMonthBudgetRollingissuedAmt),'0.00')) as SumBudgetRollingissuedAmt
,isnull(sum (StartMonthBudgetRollingissuedAmt),'0.00') as StartMonthBudgetRollingissuedAmt
,isnull(sum(EndMonthBudgetRollingissuedAmt),'0.00') as EndMonthBudgetRollingissuedAmt
,(isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthpayBudgetRollingAmt),'0.00')) as SumpayBudgetRollingAmt
,isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') as StartMonthBudgetRollingAmt
,isnull(sum(EndMonthpayBudgetRollingAmt),'0.00') as EndMonthBudgetRollingAmt

,((isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')) - (isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthpayBudgetRollingAmt),'0.00'))) as SumpayBudgetRollingchangesAmt
,(isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00')- isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') )as StartMonthpayBudgetRollingchangesAmt
,(isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')-isnull(sum(EndMonthpayBudgetRollingAmt),'0.00')) as EndMonthpayBudgetRollingchangesAmt

,(isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')) as SumcumulativepayBudgetRollingAmt
,isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') as StartMonthcumulativepayBudgetRollingAmt
,isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00') as EndMonthcumulativepayBudgetRollingAmt

,(isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') +isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00')) as SumHavecancelafterverificationAmt
,isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') as StartMonthHavecancelafterverificationAmt
,isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00') as EndMonthHavecancelafterverificationAmt
,(isnull(sum (StartHavebeenfrozenAmt),'0.00') +isnull(sum(EndMonthHavebeenfrozenAmt),'0.00')) as SumHavebeenfrozenAmt
,isnull(sum (StartHavebeenfrozenAmt),'0.00') as StartHavebeenfrozenAmt
,isnull(sum(EndMonthHavebeenfrozenAmt),'0.00') as EndMonthHavecancelafterverificationAmt
,((isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00'))
-(isnull(sum (StartHavebeenfrozenAmt),'0.00') +isnull(sum(EndMonthHavebeenfrozenAmt),'0.00'))
-(isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') +isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00')))

from #FM_SpecialExpenses (nolock) cs
GROUP BY cs.MatterContractID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.COMPANYNAME,cs.ProjectName ,cs.DeptID ,cs.DeptName
,cs.CostCategory ,cs.TwoCodeName ,cs.ThreeCodeName ,cs.FourCodeName ,cs.ContractNo
,cs.ContractAName ,cs.PartyUnit ,cs.ContractAmount

UNION ALL
SELECT MAX(cs.NodeID) as NodeID,0 as ContractLibID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.COMPANYNAME,cs.ProjectName ,cs.DeptID ,cs.DeptName
,cs.CostCategory ,cs.TwoCodeName ,cs.ThreeCodeName ,cs.FourCodeName ,'' as ContractNo
,'' as ContractAName ,'' as PartyUnit ,'0.00' as ContractAmount
, (isnull(sum (StartMonthBudgetRollingAmt),'0.00') +isnull(sum(EndMonthBudgetRollingAmt),'0.00')) as SumBudgetRollingAmt
,isnull(sum (StartMonthBudgetRollingAmt),'0.00') as StartMonthBudgetRollingAmt
,isnull(sum(EndMonthBudgetRollingAmt),'0.00') as EndMonthBudgetRollingAmt

,(isnull(sum (StartMonthBudgetRollingissuedAmt),'0.00') +isnull(sum(EndMonthBudgetRollingissuedAmt),'0.00')) as SumBudgetRollingissuedAmt
,isnull(sum (StartMonthBudgetRollingissuedAmt),'0.00') as StartMonthBudgetRollingissuedAmt
,isnull(sum(EndMonthBudgetRollingissuedAmt),'0.00') as EndMonthBudgetRollingissuedAmt
,(isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthpayBudgetRollingAmt),'0.00')) as SumpayBudgetRollingAmt
,isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') as StartMonthBudgetRollingAmt
,isnull(sum(EndMonthpayBudgetRollingAmt),'0.00') as EndMonthBudgetRollingAmt

,((isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')) - (isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthpayBudgetRollingAmt),'0.00'))) as SumpayBudgetRollingchangesAmt
,(isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00')- isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') )as StartMonthpayBudgetRollingchangesAmt
,(isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')-isnull(sum(EndMonthpayBudgetRollingAmt),'0.00')) as EndMonthpayBudgetRollingchangesAmt

,(isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')) as SumcumulativepayBudgetRollingAmt
,isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') as StartMonthcumulativepayBudgetRollingAmt
,isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00') as EndMonthcumulativepayBudgetRollingAmt

,(isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') +isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00')) as SumHavecancelafterverificationAmt
,isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') as StartMonthHavecancelafterverificationAmt
,isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00') as EndMonthHavecancelafterverificationAmt
,(isnull(sum (StartHavebeenfrozenAmt),'0.00') +isnull(sum(EndMonthHavebeenfrozenAmt),'0.00')) as SumHavebeenfrozenAmt
,isnull(sum (StartHavebeenfrozenAmt),'0.00') as StartHavebeenfrozenAmt
,isnull(sum(EndMonthHavebeenfrozenAmt),'0.00') as EndMonthHavecancelafterverificationAmt
,((isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00'))
-(isnull(sum (StartHavebeenfrozenAmt),'0.00') +isnull(sum(EndMonthHavebeenfrozenAmt),'0.00'))
-(isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') +isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00')))
from #FM_remainingsubjects (nolock) cs
GROUP BY cs.ContractLibID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.COMPANYNAME,cs.ProjectName ,cs.DeptID ,cs.DeptName
,cs.CostCategory ,cs.TwoCodeName ,cs.ThreeCodeName ,cs.FourCodeName ,cs.ContractNo
,cs.ContractAName ,cs.PartyUnit ,cs.ContractAmount
UNION ALL
SELECT cast(MAX(cs.NodeID) as varchar(22)) as NodeID,cs.ContractLibID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.COMPANYNAME,cs.ProjectName ,cs.DeptID ,cs.DeptName
,cs.CostCategory ,cs.TwoCodeName ,cs.ThreeCodeName ,cs.FourCodeName ,cs.ContractNo
,cs.ContractAName ,cs.PartyUnit ,cs.ContractAmount
, (isnull(sum (StartMonthBudgetRollingAmt),'0.00') +isnull(sum(EndMonthBudgetRollingAmt),'0.00')) as SumBudgetRollingAmt
,isnull(sum (StartMonthBudgetRollingAmt),'0.00') as StartMonthBudgetRollingAmt
,isnull(sum(EndMonthBudgetRollingAmt),'0.00') as EndMonthBudgetRollingAmt

,(isnull(sum (StartMonthBudgetRollingissuedAmt),'0.00') +isnull(sum(EndMonthBudgetRollingissuedAmt),'0.00')) as SumBudgetRollingissuedAmt
,isnull(sum (StartMonthBudgetRollingissuedAmt),'0.00') as StartMonthBudgetRollingissuedAmt
,isnull(sum(EndMonthBudgetRollingissuedAmt),'0.00') as EndMonthBudgetRollingissuedAmt
,(isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthpayBudgetRollingAmt),'0.00')) as SumpayBudgetRollingAmt
,isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') as StartMonthBudgetRollingAmt
,isnull(sum(EndMonthpayBudgetRollingAmt),'0.00') as EndMonthBudgetRollingAmt

,((isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')) - (isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthpayBudgetRollingAmt),'0.00'))) as SumpayBudgetRollingchangesAmt
,(isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00')- isnull(sum (StartMonthpayBudgetRollingAmt),'0.00') )as StartMonthpayBudgetRollingchangesAmt
,(isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')-isnull(sum(EndMonthpayBudgetRollingAmt),'0.00')) as EndMonthpayBudgetRollingchangesAmt

,(isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00')) as SumcumulativepayBudgetRollingAmt
,isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') as StartMonthcumulativepayBudgetRollingAmt
,isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00') as EndMonthcumulativepayBudgetRollingAmt

,(isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') +isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00')) as SumHavecancelafterverificationAmt
,isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') as StartMonthHavecancelafterverificationAmt
,isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00') as EndMonthHavecancelafterverificationAmt
,(isnull(sum (StartHavebeenfrozenAmt),'0.00') +isnull(sum(EndMonthHavebeenfrozenAmt),'0.00')) as SumHavebeenfrozenAmt
,isnull(sum (StartHavebeenfrozenAmt),'0.00') as StartHavebeenfrozenAmt
,isnull(sum(EndMonthHavebeenfrozenAmt),'0.00') as EndMonthHavecancelafterverificationAmt
,((isnull(sum (StartMonthcumulativepayBudgetRollingAmt),'0.00') +isnull(sum(EndMonthcumulativepayBudgetRollingAmt),'0.00'))
-(isnull(sum (StartHavebeenfrozenAmt),'0.00') +isnull(sum(EndMonthHavebeenfrozenAmt),'0.00'))
-(isnull(sum (StartMonthHavecancelafterverificationAmt),'0.00') +isnull(sum(EndMonthHavecancelafterverificationAmt),'0.00')))
as CanpaythebalanceAmt
from #FM_Salaries (nolock) cs
GROUP BY cs.ContractLibID ,cs.ProjectID ,cs.CompanyID ,cs.BudgetCodeID
,cs.COMPANYNAME,cs.ProjectName ,cs.DeptID ,cs.DeptName
,cs.CostCategory ,cs.TwoCodeName ,cs.ThreeCodeName ,cs.FourCodeName ,cs.ContractNo
,cs.ContractAName ,cs.PartyUnit ,cs.ContractAmount
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
exec(' select * from #FM_all '+ @filtersql+' order by ProjectID,CostCategory,DeptID,TwoCodeName ,ThreeCodeName ,FourCodeName ')
--select distinct (NodeID) from #FM_all
if object_id('tempdb..#FM_all') is not null drop table #FM_all
end

GO

 

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