﻿INSERT INTO Sys_ButtonToMenu VALUES (NEWID(),'7E2A5BAA-1F3C-4930-9E76-0FBABBA40B20','安全业绩加分值','Add',2)
GO

CREATE TABLE [dbo].[SafetyPerformance](
	[SafetyPerformanceId] [nvarchar](50) NOT NULL,
	[ContractorId] [nvarchar](50) NULL,
	[Years] [nvarchar](10) NULL,
	[YearPrize] [nvarchar](20) NULL,
	[Bonus] [decimal](9, 2) NULL,
 CONSTRAINT [PK_SafetyPerformance] PRIMARY KEY CLUSTERED 
(
	[SafetyPerformanceId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[SafetyPerformance]  WITH CHECK ADD  CONSTRAINT [FK_SafetyPerformance_Base_Contractor] FOREIGN KEY([ContractorId])
REFERENCES [dbo].[Base_Contractor] ([ContractorId])
GO

ALTER TABLE [dbo].[SafetyPerformance] CHECK CONSTRAINT [FK_SafetyPerformance_Base_Contractor]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'承包商ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SafetyPerformance', @level2type=N'COLUMN',@level2name=N'ContractorId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'年度' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SafetyPerformance', @level2type=N'COLUMN',@level2name=N'Years'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'年度奖项' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SafetyPerformance', @level2type=N'COLUMN',@level2name=N'YearPrize'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'加分值' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SafetyPerformance', @level2type=N'COLUMN',@level2name=N'Bonus'
GO



ALTER VIEW [dbo].[View_Contractor_Evaluation]
AS
/*********框架承包商评估表**********/
SELECT t.*,(t.Spending_commitment+t.Forecasted) AS Total,
    (CASE WHEN t.YearDiff<>0 THEN CONVERT(DECIMAL(18,2),(t.Spending_commitment+t.Forecasted)/t.YearDiff) ELSE NULL END) AS YearAvg
FROM
(SELECT datas.ID, datas.FO_NO, datas.TechnicalBonus,datas.SafetyBonus,
(CASE WHEN con.Contractor IS NULL THEN con.ContractorCN
       WHEN con.ContractorCN IS NULL THEN con.Contractor ELSE con.Contractor+con.ContractorCN END)AS Contractor, 
datas.Contractor AS ContractorId,
datas.DisciplineId,
(CASE WHEN dis.Discipline IS NULL THEN dis.DisciplineCN
		WHEN dis.DisciplineCN IS NULL THEN dis.Discipline
		ELSE dis.Discipline+dis.DisciplineCN END) AS Discipline,
datas.Buyer, 
datas.BuyerId, 
datas.Main_Coordinator AS Main_CoordinatorId,
u2.UserName AS Main_Coordinator, 
datas.Contract_Admin,
datas.Validate_Date, 
datas.Expire_Date, 
datas.FC_Status,
(CASE WHEN datas.Validate_Date IS NOT NULL AND datas.Expire_Date IS NOT NULL THEN CONVERT(DECIMAL(9,2),DATEDIFF(DAY,datas.Validate_Date,datas.Expire_Date)*1.00/365) ELSE 0 END ) AS YearDiff,
v.Spending_commitment AS Spending_commitment,
(CASE WHEN datas.Proportion_of_FC_Definition IS NOT NULL 
THEN CONVERT(VARCHAR(10),CONVERT(DECIMAL(9,2),datas.Proportion_of_FC_Definition*100.0))+'%' END) AS VolumeAllocation,
ISNULL(datas.Forecasted,0) AS Forecasted,
datas.ReviewOfFC,datas.PriceEvaluation1,datas.PriceEvaluation2,datas.PriceLevel,datas.enumeration,datas.NCRIsReview,datas.NCRReviewNum,
datas.IsInquiry,datas.InquiryNum,datas.AuditResult,datas.BoQIsAudit,datas.BoQAuditComments,datas.IsOthers,ISNULL(datas.OthersNum,0) as OthersNum,datas.OtherDef,datas.Proposed

,(case when datas.IfExtend=1 
then isnull(Interview.InterviewTimes,0)+isnull(InterviewPrevious.InterviewTimes,0)
else isnull(Interview.InterviewTimes,0) end) as InterviewTimes --约谈次数
,(case when datas.IfExtend=1 
then isnull(NCR.NCRTimes,0)+ISNULL(NCRPrevious.NCRTimes,0)
else isnull(NCR.NCRTimes,0) end) as NCRTimes --NCR次数
,(case when datas.IfExtend=1 
then isnull(Rectification.RectificationTimes,0)+isnull(RectificationPrevious.RectificationTimes,0)
else isnull(Rectification.RectificationTimes,0) end) as RectificationTimes --停工整改次数

--,isnull(Interview.InterviewTimes,0) as InterviewTimes --约谈次数
--,isnull(NCR.NCRTimes,0) as NCRTimes 
--,isnull(Rectification.RectificationTimes,0) as RectificationTimes --停工整改次数
,isnull(Green.GreenTimes,0) as GreenTimes --审计绿区次数
,isnull(YellowGreen.YellowGreenTimes,0) as YellowGreenTimes --审计黄绿区次数
,isnull(Yellow.YellowTimes,0) as YellowTimes --审计黄区次数
,isnull(RedYellow.RedYellowTimes,0) as RedYellowTimes --审计红黄区次数
,isnull(Red.RedTimes,0) as RedTimes --审计红区次数
,(case when isnull(Green.GreenTimes,0)+isnull(YellowGreen.YellowGreenTimes,0)+isnull(Yellow.YellowTimes,0)+isnull(RedYellow.RedYellowTimes,0) +isnull(Red.RedTimes,0)=0 then '√'else '□' end ) as NotAudit
,(case when datas.IfExtend=1 then 'Y' else 'N' end) as IfExtend--是否为续签合同
,datas.PreviousFO --上一轮合同号

,-((case when 
		((case when datas.IfExtend=1 then isnull(Interview.InterviewTimes,0)+isnull(InterviewPrevious.InterviewTimes,0) else isnull(Interview.InterviewTimes,0) end)*0.005) +
		((case when datas.IfExtend=1 then isnull(Rectification.RectificationTimes,0)+isnull(RectificationPrevious.RectificationTimes,0) else isnull(Rectification.RectificationTimes,0) end)*0.005) < 2
then ((case when datas.IfExtend=1 then isnull(NCR.NCRTimes,0)+ISNULL(NCRPrevious.NCRTimes,0)else isnull(NCR.NCRTimes,0) end)*0.02) +
((case when datas.IfExtend=1 then isnull(Interview.InterviewTimes,0)+isnull(InterviewPrevious.InterviewTimes,0) else isnull(Interview.InterviewTimes,0) end)*0.005) +
((case when datas.IfExtend=1 then isnull(Rectification.RectificationTimes,0)+isnull(RectificationPrevious.RectificationTimes,0) else isnull(Rectification.RectificationTimes,0) end)*0.005)
else (case when datas.IfExtend=1 then isnull(NCR.NCRTimes,0)+ISNULL(NCRPrevious.NCRTimes,0)else isnull(NCR.NCRTimes,0) end)*0.02+2 end)*100) as TechnicalBonusMalus --技术减值%

FROM FC_SESRelatedData AS datas
left join FC_SESRelatedData AS previous on previous.FO_NO = datas.PreviousFO
LEFT JOIN dbo.Base_Contractor AS con ON con.ContractorId = datas.Contractor
LEFT JOIN dbo.Sys_User AS u2 ON u2.UserId = datas.Main_Coordinator
LEFT JOIN dbo.Base_Discipline AS dis ON dis.DisciplineId = datas.DisciplineId
LEFT JOIN 
(SELECT datas.FO_NO,
          (CONVERT(DECIMAL(18,2),ISNULL(c.CheckedValue,0))+CONVERT(DECIMAL(18,2),ISNULL(a.Commitment,0))) AS Spending_commitment
     FROM FC_SESRelatedData AS datas
	 LEFT JOIN dbo.Base_Discipline AS dis ON dis.DisciplineId = datas.DisciplineId
     LEFT JOIN dbo.Base_FOType AS ft ON ft.FOTypeId = datas.FOTypeId
     LEFT JOIN(SELECT SUM(ISNULL(SSR_Actual_cost,0)) AS CheckedValue,FO FROM dbo.FC_SESReport 
			   WHERE Accepted='X'  AND Deleted <> 'X' AND Blocked <> 'X'  GROUP BY FO) AS c ON c.FO=datas.FO_NO
     LEFT JOIN(SELECT SUM(ISNULL(SSR_budget,0)) AS Commitment,FO FROM dbo.FC_SESReport 
		       WHERE Accepted <> 'X'  AND Deleted <> 'X' AND Blocked <> 'X' GROUP BY FO) AS a ON a.FO=datas.FO_NO
 
)v  ON v.FO_NO = datas.FO_NO

left join (select count(*) as InterviewTimes,FC_ID from FC_ContractManagement where FileTypeId='7' group by FC_ID) as Interview on Interview.FC_ID  = datas.ID
left join (select count(*) as NCRTimes,FC_ID from FC_ContractManagement where FileTypeId='6' group by FC_ID) as NCR on NCR.FC_ID  = datas.ID
left join (select count(*) as RectificationTimes,FC_ID from FC_ContractManagement where FileTypeId='10' group by FC_ID) as Rectification on Rectification.FC_ID  = datas.ID
left join (select count(*) as GreenTimes,FC_ID from FC_ContractManagement where FileTypeId='5' and AuditResult='绿' group by FC_ID) as Green on Green.FC_ID  = datas.ID
left join (select count(*) as YellowGreenTimes,FC_ID from FC_ContractManagement where FileTypeId='5' and AuditResult='黄绿' group by FC_ID) as YellowGreen on YellowGreen.FC_ID  = datas.ID
left join (select count(*) as YellowTimes,FC_ID from FC_ContractManagement where FileTypeId='5' and AuditResult='黄' group by FC_ID) as Yellow on Yellow.FC_ID  = datas.ID
left join (select count(*) as RedYellowTimes,FC_ID from FC_ContractManagement where FileTypeId='5' and AuditResult='红黄' group by FC_ID) as RedYellow on RedYellow.FC_ID  = datas.ID
left join (select count(*) as RedTimes,FC_ID from FC_ContractManagement where FileTypeId='5' and AuditResult='红' group by FC_ID) as Red on Red.FC_ID  = datas.ID

left join (select count(*) as InterviewTimes,FC_ID from FC_ContractManagement where FileTypeId='7' group by FC_ID) as InterviewPrevious on InterviewPrevious.FC_ID  = previous.ID
left join (select count(*) as NCRTimes,FC_ID from FC_ContractManagement where FileTypeId='6' group by FC_ID) as NCRPrevious on NCRPrevious.FC_ID  = previous.ID
left join (select count(*) as RectificationTimes,FC_ID from FC_ContractManagement where FileTypeId='10' group by FC_ID) as RectificationPrevious on RectificationPrevious.FC_ID  = previous.ID


)t




GO





