﻿ALTER TABLE dbo.Editor_EProject ADD PM_Planning_Approval DATETIME NULL
ALTER TABLE dbo.Editor_EProject ADD PM_Pre_Approval DATETIME NULL
GO


ALTER PROCEDURE [dbo].[ProjectSchedule] 

   @EProjectId VARCHAR(50)  

AS	
/*********首页Schedule第一部分**********/
SELECT 1 AS id,
 'Job Receive' AS MileSones,
'N.A' AS Schedule,
(CASE WHEN CONVERT(VARCHAR(10),PM_MA_JobReveive, 23) IS NOT NULL THEN CONVERT(VARCHAR(10),PM_MA_JobReveive, 23) ELSE 'N.A' END) AS Actual
FROM dbo.Editor_EProject AS eProject
WHERE eProject.EProjectId=@EProjectId

UNION
SELECT 2 AS id,
'Planning Cost Approval' AS MileSones,
 'N.A' AS Schedule,
(CASE WHEN CONVERT(VARCHAR(10),PM_Planning_Approval, 23) IS NOT NULL THEN CONVERT(VARCHAR(10),PM_Planning_Approval, 23) ELSE 'N.A' END) AS Actual
FROM dbo.Editor_EProject AS eProject
WHERE eProject.EProjectId=@EProjectId
--(CASE WHEN CONVERT(VARCHAR(10),eProject.ProjectControl_MS_ResourcePlanReceived,23) IS NOT NULL 
--THEN CONVERT(VARCHAR(10),eProject.ProjectControl_MS_ResourcePlanReceived,23) ELSE 'N.A' END) AS Actual
--FROM dbo.Editor_EProject AS eProject
--WHERE eProject.EProjectId=@EProjectId

UNION
SELECT 3 AS id,
 'Pre-approval' AS MileSones,
'N.A' AS Schedule,
(CASE WHEN CONVERT(VARCHAR(10),PM_Pre_Approval, 23) IS NOT NULL THEN CONVERT(VARCHAR(10),PM_Pre_Approval, 23) ELSE 'N.A' END) AS Actual
FROM dbo.Editor_EProject AS eProject
WHERE eProject.EProjectId=@EProjectId

UNION

SELECT 4 AS id,
'Kick-off Meeting' AS MileSones,
'N.A' AS Schedule,
(CASE WHEN CONVERT(VARCHAR(10), PM_MA_KickOffMeeting, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), PM_MA_KickOffMeeting, 23) ELSE 'N.A' END) AS Actual
FROM dbo.Editor_EProject AS eProject
WHERE eProject.EProjectId=@EProjectId

UNION 

SELECT 5 AS id,
'SR1/2' AS MileSones,
(CASE WHEN CONVERT(VARCHAR(10), ProjectControl_MS_SR, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), ProjectControl_MS_SR, 23) ELSE 'N.A' END) AS Schedule,
(CASE WHEN CONVERT(VARCHAR(10), PM_MA_SR, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), PM_MA_SR, 23) ELSE 'N.A' END) AS Actual
FROM dbo.Editor_EProject AS eProject
WHERE eProject.EProjectId=@EProjectId

UNION 

SELECT 6 AS id,
'Final Approval' AS MileSones,
(CASE WHEN CONVERT(VARCHAR(10), ProjectControl_MS_Approval, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), ProjectControl_MS_Approval, 23) ELSE 'N.A' END) AS Schedule,
(CASE WHEN CONVERT(VARCHAR(10), PM_MA_ProjectApproval, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), PM_MA_ProjectApproval, 23) ELSE 'N.A' END) AS Actual
FROM dbo.Editor_EProject AS eProject
WHERE eProject.EProjectId=@EProjectId

UNION

SELECT 7 AS id,
 'MC' AS MileSones,
(CASE WHEN CONVERT(VARCHAR(10), ProjectControl_Revised_MC_Date, 23) IS NOT NULL AND ProjectControl_Revised_MC_Date=ProjectControl_MS_MC
 THEN CONVERT(VARCHAR(10), ProjectControl_Revised_MC_Date, 23) 
 ELSE CASE WHEN CONVERT(VARCHAR(10), ProjectControl_Revised_MC_Date, 23) IS NOT NULL AND ProjectControl_Revised_MC_Date!=ProjectControl_MS_MC
 THEN CONVERT(VARCHAR(10), ProjectControl_Revised_MC_Date, 23)+'*'
 ELSE 'N.A' END END)  AS Schedule,
(CASE WHEN CONVERT(VARCHAR(10), CM_MA_MC, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), CM_MA_MC, 23)ELSE 'N.A' END)AS Actual
FROM dbo.Editor_EProject AS eProject
WHERE eProject.EProjectId=@EProjectId

UNION

SELECT 8 AS id,
'Close' AS MileSones,
(CASE WHEN CONVERT(VARCHAR(10), ProjectControl_MS_Close, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), ProjectControl_MS_Close, 23) ELSE 'N.A' END) AS Schedule,
(CASE WHEN CONVERT(VARCHAR(10), ProjectControl_ClosedDate, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), ProjectControl_ClosedDate, 23) ELSE 'N.A' END)as Actual

FROM dbo.Editor_EProject AS eProject
WHERE eProject.EProjectId=@EProjectId 

UNION

SELECT 9 AS id,
'Cancel' AS MileSones,
(CASE WHEN CONVERT(VARCHAR(10), ProjectControl_PC_CancelDate, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), ProjectControl_PC_CancelDate, 23) ELSE 'N.A' END) AS Schedule,
'N.A' AS Actual
FROM dbo.Editor_EProject AS eProject
WHERE eProject.EProjectId=@EProjectId

GO



ALTER VIEW [dbo].[View_ProjectListReport]
AS
/*********报表：项目列表  佘春生修改于2023.03.01*********/
SELECT eproject.EProjectId,
eproject.ProjectControl_BUCode,--BU. 	
eproject.ProjectControl_JobNo,--Job No	
eproject.ProjectControl_JobType,--Job Type	
eproject.ProjectControl_LeadByName,--Lead By	
eproject.ProjectControl_JobTitle,--Job Title	
ISNULL(eproject.ProjectControl_OrginalBudget,0) AS OrginalBudget,--Org. Budget

eproject.ProjectControl_ChangedBudget AS ChangedBudget,
eproject.PM_General_CostToComplete AS CostToComplete,
(ISNULL(eproject.ProjectControl_Actual,0)+ISNULL(eproject.ProjectControl_CommittedPRPO,0)+
ISNULL(eproject.ProjectControl_CommittedSSRs,0)+ISNULL(eproject.PM_General_CostToComplete,0)) AS FinalCost,
--(CASE WHEN (SELECT TOP 1 ISNULL(cost.ChangedBudget,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC) IS NOT NULL
--THEN (SELECT TOP 1 ISNULL(cost.ChangedBudget,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC) 
--ELSE 0 END) AS ChangedBudget,--Chd. Budget	

--(CASE WHEN (SELECT TOP 1 ISNULL(cost.CostToComplete,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC) IS NOT NULL
--THEN (SELECT TOP 1 ISNULL(cost.CostToComplete,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC)
--ELSE 0 END) AS CostToComplete,--Cost.Comp	

--((CASE WHEN (SELECT TOP 1 ISNULL(cost.ActualCost,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC) IS NOT NULL
--THEN (SELECT TOP 1 ISNULL(cost.ActualCost,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC)
--ELSE 0 END)+
--(CASE WHEN (SELECT TOP 1 ISNULL(cost.CommittedPRPO,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC) IS NOT NULL
--THEN (SELECT TOP 1 ISNULL(cost.CommittedPRPO,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC)
--ELSE 0 END)+
--(CASE WHEN (SELECT TOP 1 ISNULL(cost.CommittedSSRs,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC) IS NOT NULL
--THEN (SELECT TOP 1 ISNULL(cost.CommittedSSRs,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC)
--ELSE 0 END)+
--(CASE WHEN (SELECT TOP 1 ISNULL(cost.CostToComplete,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC) IS NOT NULL
--THEN (SELECT TOP 1 ISNULL(cost.CostToComplete,0) FROM dbo.Editor_CostReport AS cost WHERE cost.EProjectId=eproject.EProjectId ORDER BY cost.Monthly DESC)
--ELSE 0 END)) AS FinalCost,--Final Cost	

eproject.ProjectControl_ProjectManager,--Proj. Manager	
eproject.ProjectControl_ConstManager,--Const. Manager
eproject.ProjectControl_EMManager,	
CONVERT(NVARCHAR(10),eproject.PM_MA_JobReveive,23) AS RECEIVES,--RECEIVE	
CONVERT(NVARCHAR(10),eproject.ProjectControl_MS_Approval,23) AS ProjectControl_MS_Approval,--Appr Date(Sch)	
CONVERT(NVARCHAR(10),eproject.PM_MA_ProjectApproval,23) AS PM_MA_ProjectApproval,--Appr Date(Act)	
CONVERT(NVARCHAR(10),eproject.ProjectControl_MS_MC,23) AS ProjectControl_MS_MC,--MC (Plan)	
CONVERT(NVARCHAR(10),eproject.CM_MA_MC,23) AS CM_MA_MC,--MC (Act)	
CONVERT(NVARCHAR(10),eproject.ProjectControl_MS_Close,23) AS ProjectControl_MS_Close,--CLOSE(Sch)	
CONVERT(NVARCHAR(10),eproject.ProjectControl_ClosedDate,23)  AS CloseAct,--CLOSE(Act) 代码中处理
CONVERT(NVARCHAR(10),eproject.ProjectControl_PC_CancelDate,23) AS ProjectControl_PC_CancelDate,--Cancel Date	
CONVERT(NVARCHAR(10),eproject.ProjectControl_BC_CloseDate,23) AS ProjectControl_BC_CloseDate,--B. Close	
eproject.PM_General_Priority,--Priority	
eproject.ProjectControl_JobStatus,--Job Status	
eproject.PM_Critical,
(SELECT TOP 1 ConstText FROM dbo.Sys_Const WHERE ConstValue=eproject.PM_ReasonForCritical AND GroupId='ReasonForCritical') AS PM_ReasonForCritical,
(CASE WHEN DATEDIFF(DAY,eproject.PM_MA_ProjectApproval,eproject.ProjectControl_CM_SchEnd)>0
			THEN (CASE WHEN (DATEDIFF(DAY,eproject.ProjectControl_CM_SchEnd,eproject.CM_CM_RevisedEnd)/DATEDIFF(DAY,eproject.PM_MA_ProjectApproval,eproject.ProjectControl_CM_SchEnd))>0.1 THEN 'C'
					   WHEN (DATEDIFF(DAY,eproject.ProjectControl_CM_SchEnd,eproject.CM_CM_RevisedEnd)/DATEDIFF(DAY,eproject.PM_MA_ProjectApproval,eproject.ProjectControl_CM_SchEnd))<=0.1 AND (DATEDIFF(DAY,eproject.ProjectControl_CM_SchEnd,eproject.CM_CM_RevisedEnd)/DATEDIFF(DAY,eproject.PM_MA_ProjectApproval,eproject.ProjectControl_CM_SchEnd))>0.05 THEN 'B'
					   WHEN (DATEDIFF(DAY,eproject.ProjectControl_CM_SchEnd,eproject.CM_CM_RevisedEnd)/DATEDIFF(DAY,eproject.PM_MA_ProjectApproval,eproject.ProjectControl_CM_SchEnd))<=0.05 THEN 'A'
			ELSE '' END)
		ELSE ''END) AS Schedule,--Sche.	

(CASE WHEN (SELECT TOP 1 OrginalBudget+ ChangedBudget FROM dbo.Editor_CostReport WHERE EProjectId= eproject.EProjectId ORDER BY Monthly DESC)>0
THEN (CASE WHEN (SELECT TOP 1 (ActualCost+CommittedPRPO+CommittedSSRs+CostToComplete-OrginalBudget-ChangedBudget)/(OrginalBudget+ChangedBudget) FROM dbo.Editor_CostReport WHERE EProjectId=eproject.EProjectId ORDER BY Monthly DESC)>0.1 THEN 'C'
		   WHEN (SELECT TOP 1 (ActualCost+CommittedPRPO+CommittedSSRs+CostToComplete-OrginalBudget-ChangedBudget)/(OrginalBudget+ChangedBudget) FROM dbo.Editor_CostReport WHERE EProjectId=eproject.EProjectId ORDER BY Monthly DESC)>0.05 AND (SELECT TOP 1 (ActualCost+CommittedPRPO+CommittedSSRs+CostToComplete-OrginalBudget-ChangedBudget)/(OrginalBudget+ChangedBudget) FROM dbo.Editor_CostReport WHERE EProjectId=eproject.EProjectId ORDER BY Monthly DESC)<=0.1 THEN 'B' 
		   WHEN (SELECT TOP 1 (ActualCost+CommittedPRPO+CommittedSSRs+CostToComplete-OrginalBudget-ChangedBudget)/(OrginalBudget+ChangedBudget) FROM dbo.Editor_CostReport WHERE EProjectId=eproject.EProjectId ORDER BY Monthly DESC)<=0.05 THEN 'A'  ELSE '' END)
ELSE '' END) AS Cost,--Cost

(CASE WHEN eproject.PM_SC_PendingQty>0 THEN 'C'
     WHEN (eproject.PM_SC_PendingQty=0 OR eproject.PM_SC_PendingQty IS NULL) AND PM_SC_ApprovedQty>0 THEN 'B'
     WHEN (eproject.PM_SC_ApprovedQty=0 OR eproject.PM_SC_ApprovedQty IS NULL) AND (eproject.PM_SC_PendingQty=0 OR eproject.PM_SC_PendingQty IS NULL) THEN 'A'
     ELSE '' END) AS Scope,--Scope	

eproject.ProjectControl_MOCFormNo,--MOC No.	
eproject.ProjectControl_Account,--Account	
ISNULL(eproject.ProjectControl_PVIPrediction,0) AS ProjectControl_PVIPrediction,--PVI Pre.	
eproject.ProjectControl_CostEffectvitity,--Cost Eff.	
eproject.ProjectControl_OperationRep,--Oper. Rep.
eproject.ProjectControl_CI_ProcessName,--Process Eng.	
eproject.ProjectControl_CI_EquipmentName,--Equip. Eng.	
eproject.ProjectControl_CI_InstrumentName,--Instr. Eng.	
eproject.ProjectControl_CI_ElectricalName,--Elec. Eng.	
eproject.ProjectControl_CI_CivilName,--Civil Eng.	
eproject.PM_General_Category,--CATEGORY	
eproject.PM_General_CDI,--PROJECT_CDI	

CONVERT(NVARCHAR(10),(SELECT TOP 1 ReceiveHC FROM dbo.Editor_TDC WHERE [Type]='D01_IFC' AND Disc='00_ALL' AND EProjectId = eproject.EProjectId),23) AS IFC_Received,--IFC Received
CONVERT(NVARCHAR(10),(SELECT TOP 1 InputDate FROM dbo.Editor_TDC WHERE [Type]='D11_MC' AND Disc='00_ALL' AND EProjectId = eproject.EProjectId),23) AS MC_ReceivedDate,--MC Received Date

s.Detail_Eng_Civil_Sch_Start,--PROJ_DE_CIVIL_SCH_START_DATE	
s.Detail_Eng_Civil_Sch_End,--PROJ_DE_CIVIL_SCH_END_DATE	
s.PROJ_DE_CIVIL_ACU_START_DATE,--PROJ_DE_CIVIL_ACU_START_DATE
s.PROJ_DE_CIVIL_ACU_END_DATE,--PROJ_DE_CIVIL_ACU_END_DATE

s.Detail_Eng_Mech_EI_Sch_Start,--PROJ_DE_ME_SCH_START_DATE
s.PROJ_DE_ME_SCH_END_DATE,--PROJ_DE_ME_SCH_END_DATE	
s.Detail_Eng_Mech_EI_Sch_End,--PROJ_DE_ME_ACU_START_DATE	
s.PROJ_DE_ME_ACU_END_DATE, --PROJ_DE_ME_ACU_END_DATE

s.ProjectControl_LP_SchStart,--PROJ_DE_LLI_SCH_START_DATE	
s.ProjectControl_LP_SchEnd,--PROJ_DE_LLI_SCH_END_DATE	
s.PM_LLEP_RevisedStart,--PROJ_DE_LLI_ACU_START_DATE	
s.PM_LLEP_RevisedEnd,--PROJ_DE_LLI_ACU_END_DATE

s.ProjectControl_CC_SchStart,--PROJ_CN_CIVIL_SCH_START_DATE	
s.ProjectControl_CC_SchEnd,--PROJ_CN_CIVIL_SCH_END_DATE	
s.CM_CC_RevisedStart,--PROJ_CN_CIVIL_ACU_START_DATE	
s.CM_CC_RevisedEnd,--PROJ_CN_CIVIL_ACU_END_DATE	

s.ProjectControl_CM_SchStart,--PROJ_CN_ME_SCH_START_DATE	
s.ProjectControl_CM_SchEnd,--PROJ_CN_ME_SCH_END_DATE	
s.CM_CM_RevisedStart,--PROJ_CN_ME_ACU_START_DATE	
s.CM_CM_RevisedEnd,--PROJ_CN_ME_ACU_END_DATE	

CONVERT(VARCHAR(10),eproject.CM_KickOffMetting,23) AS CM_KickOffMetting,
eproject.ProjectControl_Revised_MC_Date,--Revised MC Date	
eproject.ProjectControl_Remarks, --Remarks
eproject.OutSourceType,
eproject.CM_Date_of_Registration,
eproject.CM_Punch_CKilledDate,
eproject.StudyWo,
eproject.CTEInvolved,
eproject.CAPEXPlanNo,
ISNULL(eproject.PM_SC_ApprovedQty,0) AS PM_SC_ApprovedQty,
ISNULL(eproject.PM_SC_ApprovedCost,0) AS PM_SC_ApprovedCost,
CONVERT(VARCHAR(10),eproject.PM_Planning_Approval,23) AS PM_Planning_Approval,
CONVERT(VARCHAR(10),eproject.PM_Pre_Approval,23) AS PM_Pre_Approval

FROM dbo.Editor_EProject AS eproject
LEFT JOIN dbo.View_ScheduleDate s ON s.EProjectId = eproject.EProjectId


GO
