﻿ALTER VIEW [dbo].[View_EprojectAPI]
AS

SELECT pro.EProjectId AS Id, u4.UserName AS Create_by,pro.CreateDate AS Create_time,u5.UserName AS Update_by,pro.ModifyDate AS Update_time,
       pro.ProjectControl_BUCode AS Bu_code,pro.ProjectControl_JobNo AS Job_no,pro.ProjectControl_JobType AS Job_type,pro.ProjectControl_LeadByName AS Lead_by,   
	   pro.ProjectControl_JobTitle AS Job_title,pro.ProjectControl_OrginalBudget AS Budget,pro.ProjectControl_ProjectManager AS Project_manager,
	   pro.ProjectControl_ConstManager AS Construction_manager,pro.ProjectControl_EMManager AS Engineering_manager,
	   CONVERT(NVARCHAR(10),pro.PM_MA_ProjectApproval,111) AS Approval_date,
	   CONVERT(NVARCHAR(10),pro.ProjectControl_MS_MC,111) AS Mc_planed,
	   
	   (CASE WHEN pro.CM_CC_RevisedEnd IS NULL  THEN CONVERT(NVARCHAR(10),pro.CM_CM_RevisedEnd,23)
	         WHEN pro.CM_CM_RevisedEnd IS NULL  THEN CONVERT(NVARCHAR(10),pro.CM_CC_RevisedEnd,23)
	         WHEN pro.CM_CC_RevisedEnd IS NOT NULL AND pro.CM_CM_RevisedEnd IS NOT NULL AND pro.CM_CC_RevisedEnd >= pro.CM_CM_RevisedEnd
	              THEN CONVERT(NVARCHAR(10),pro.CM_CC_RevisedEnd,23)
	         WHEN pro.CM_CC_RevisedEnd IS NOT NULL AND pro.CM_CM_RevisedEnd IS NOT NULL AND pro.CM_CC_RevisedEnd <= pro.CM_CM_RevisedEnd
	         THEN CONVERT(NVARCHAR(10),pro.CM_CM_RevisedEnd,23) END)  AS Mc_revised,

	   pro.PM_General_Priority AS Priority,pro.PM_General_Category AS Category,
	   pro.ProjectControl_JobStatus AS Job_status,
	   (CASE WHEN DATEDIFF(DAY,pro.PM_MA_ProjectApproval,pro.ProjectControl_CM_SchEnd)>0
			THEN (CASE WHEN (DATEDIFF(DAY,pro.ProjectControl_CM_SchEnd,pro.CM_CM_RevisedEnd)/DATEDIFF(DAY,pro.PM_MA_ProjectApproval,pro.ProjectControl_CM_SchEnd))>0.1 THEN 'C'
					   WHEN (DATEDIFF(DAY,pro.ProjectControl_CM_SchEnd,pro.CM_CM_RevisedEnd)/DATEDIFF(DAY,pro.PM_MA_ProjectApproval,pro.ProjectControl_CM_SchEnd))<=0.1 AND (DATEDIFF(DAY,pro.ProjectControl_CM_SchEnd,pro.CM_CM_RevisedEnd)/DATEDIFF(DAY,pro.PM_MA_ProjectApproval,pro.ProjectControl_CM_SchEnd))>0.05 THEN 'B'
					   WHEN (DATEDIFF(DAY,pro.ProjectControl_CM_SchEnd,pro.CM_CM_RevisedEnd)/DATEDIFF(DAY,pro.PM_MA_ProjectApproval,pro.ProjectControl_CM_SchEnd))<=0.05 THEN 'A'
			ELSE '' END)
		ELSE ''END) AS Schedule,--Sche.	 AS Schedule,
		(CASE WHEN (SELECT TOP 1 OrginalBudget+ ChangedBudget FROM dbo.Editor_CostReport WHERE EProjectId= pro.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=pro.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=pro.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=pro.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=pro.EProjectId ORDER BY Monthly DESC)<=0.05 THEN 'A'  ELSE '' END)
        ELSE '' END) AS Cost,
		(CASE WHEN pro.PM_SC_PendingQty>0 THEN 'C'
              WHEN (pro.PM_SC_PendingQty=0 OR pro.PM_SC_PendingQty IS NULL) AND PM_SC_ApprovedQty>0 THEN 'B'
              WHEN (pro.PM_SC_ApprovedQty=0 OR pro.PM_SC_ApprovedQty IS NULL) AND (pro.PM_SC_PendingQty=0 OR pro.PM_SC_PendingQty IS NULL) THEN 'A'
         ELSE '' END) AS Scope,
	   CONVERT(NVARCHAR(10),pro.PM_MA_JobReveive,111) AS Received_date,
	   (CASE WHEN (SELECT TOP 1 ISNULL(ActualCost,0)+ISNULL(CommittedPRPO,0)+ISNULL(CommittedSSRs,0)+ISNULL(CostToComplete,0) FROM dbo.Editor_CostReport WHERE EProjectId=pro.EprojectId ORDER BY Monthly DESC) IS NOT NULL
	         THEN (SELECT TOP 1 ISNULL(ActualCost,0)+ISNULL(CommittedPRPO,0)+ISNULL(CommittedSSRs,0)+ISNULL(CostToComplete,0) FROM dbo.Editor_CostReport WHERE EProjectId=pro.EprojectId ORDER BY Monthly DESC)
             ELSE 0 END) AS Estimated_fianl_cost,
	   CONVERT(NVARCHAR(10),CM_MA_MC,111) AS Mc_actual,
	   CONVERT(NVARCHAR(10),(SELECT TOP 1 ReceiveHC FROM dbo.Editor_TDC WHERE EProjectId=pro.EProjectId AND [Type]='D11_MC'  ORDER BY ReceiveHC DESC),111) AS MC_Signed,
	   CONVERT(NVARCHAR(10),(SELECT TOP 1 ReceiveHC FROM dbo.Editor_TDC WHERE Type='D12_RFSU'  AND ReceiveHC IS NOT NULL AND EProjectId=pro.EProjectId ORDER BY ReceiveHC DESC),111) as Rfsu,
	   CONVERT(NVARCHAR(10), pro.CM_Punch_CKilledDate,111) AS  Punch_c_killed,
	   CONVERT(NVARCHAR(10),(SELECT TOP 1 ReceiveHC FROM dbo.Editor_TDC WHERE EProjectId=pro.EProjectId AND [Type]='D30_FC' ORDER BY ReceiveHC DESC),111) AS Fc_signed,
	   CONVERT(NVARCHAR(10),pro.ProjectControl_BC_CloseDate,111) AS  Business_closed,
	   CONVERT(NVARCHAR(10),(SELECT TOP 1 ReceiveHC FROM dbo.Editor_TDC WHERE EProjectId=pro.EProjectId AND [Type]='D01_IFC' AND Disc='00_ALL'),111) AS Ifc_received,
	   CONVERT(NVARCHAR(10),(SELECT TOP 1 ReceiveHC FROM dbo.Editor_TDC WHERE EProjectId=pro.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='00_ALL'),111) AS As_built_received,
	   CONVERT(NVARCHAR(10),(SELECT TOP 1 ReceivePDF FROM dbo.Editor_TDC WHERE EProjectId=pro.EProjectId AND [Type]='D03_MD' ORDER BY ReceivePDF DESC),111) AS Md_received,
	   CONVERT(NVARCHAR(10),pro.CM_Date_of_Registration,23) AS Date_registeration,pro.ProjectControl_CostEffectvitity AS Cost_effectivity,
	   pro.ProjectControl_PVIPrediction AS PVI,
       CONVERT(NVARCHAR(10),pro.ProjectControl_PC_CancelDate,111) AS Cancel_date,pro.ProjectControl_Account AS Account_no,
	   pro.ProjectControl_NetworkNo AS Network_no,pro.StudyWo AS Study_wo,pro.CTEInvolved AS Cte_involved, pro.CAPEXPlanNo AS Capex_plan_no,
	   pro.PM_General_CDI AS CDI
FROM dbo.Editor_EProject pro
LEFT JOIN dbo.Sys_User AS u4 ON u4.UserId = pro.CreatePerson
LEFT JOIN dbo.Sys_User AS u5 ON u5.UserId=pro.ModifyPerson


GO


