﻿
ALTER VIEW [dbo].[View_FCLAPI]
AS 
SELECT CONVERT(VARCHAR(20),fc.ID) AS Id, fc.CreateDate,fc.ModifyDate,u4.UserName AS CreatePerson,u5.UserName AS ModifyPerson,fc.FO_NO AS Fo_no,
       (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_fo,
       ft.FOType AS [Type],price.PriceScheme AS Pricing_scheme,
	   (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, 
	   fc.Vendor_NO AS Vendor_no, item.Item,fc.Original_WC AS Discipline_category,
       cur.Currency,fc.Material_Group AS Material_group, fc.Purchase_Group AS Purchase_group, fc.Cost_Element AS Cost_element, 
       t.[Type] AS Service_type,u.UserName AS Contract_admin,ch.UserName AS Cost_checker,fc.Buyer, u2.UserName AS Main_coordinator, 
	   fc.Applicant AS Mc_dept,u3.UserName AS User_representative,fc.Validate_date, fc.Expire_date, fc.FC_status,fc.Contract_Person AS Contract_person, 
	   fc.Contract_Tel AS Tel, 
	  (CASE WHEN fc.Proportion_of_FC_Definition IS NOT NULL THEN CONVERT(VARCHAR(10),CONVERT(DECIMAL(9,2),fc.Proportion_of_FC_Definition*100.0))+'%' END) AS Volume_allocation,
	  fc.Actual_Budget AS Contract_budget, fc.Connectedtransaction AS Affiliated_transaction,
	  CONVERT(DECIMAL(18,2),ISNULL(c.CheckedValue,0)) AS Checked_value,
	  CONVERT(DECIMAL(18,2),(ISNULL(fc.Actual_Budget,0)-ISNULL(c.CheckedValue,0))) AS Remaining_budget,
	  CONVERT(VARCHAR(10),CONVERT(DECIMAL(18,2),(fc.Actual_Budget-ISNULL(c.CheckedValue,0))/fc.Actual_Budget*100))+'%' AS Remaining_budget_p,
	 (CASE WHEN DATEDIFF(DAY,fc.Validate_Date,fc.Expire_Date)>0
	  THEN CONVERT(VARCHAR(10),CONVERT(DECIMAL(18,2),CONVERT(DECIMAL(18,2),DATEDIFF(DAY,GETDATE(),fc.Expire_Date))/CONVERT(DECIMAL(18,2),DATEDIFF(DAY,fc.Validate_Date,fc.Expire_Date))*100))+'%'
	  ELSE ''END) AS Remaining_duration_p,
	  fc.MainHead AS Zyhead,fc.MainHeadContact AS Zyhead_tel,
	  fc.SafetyOfficer AS Aqhead, fc.SafetyOfficerContact AS Aqhead_tel,
	  fc.ProjectManager AS Xmjl, fc.ProjectManagerContact AS Xmjl_tel,
	  fc.Email,fc.ConstRecords AS Const_records,fc.Remark,fc.ExceedLimit AS Exceed_limit
FROM FC_SESRelatedData AS fc
LEFT JOIN dbo.Base_Contractor AS con ON con.ContractorId = fc.Contractor
LEFT JOIN dbo.Base_FOType AS ft ON ft.FOTypeId = fc.FOTypeId
LEFT JOIN dbo.Base_Type AS t ON t.TypeId = fc.[Type]
LEFT JOIN dbo.Base_Item AS item ON item.ItemId = fc.Item
LEFT JOIN dbo.Base_Currency AS cur ON cur.CurrencyId = fc.Currency
LEFT JOIN dbo.Sys_User AS u ON u.UserId = fc.Contract_Admin	
LEFT JOIN dbo.Sys_User AS ch ON ch.UserId = fc.Cost_Checker 
LEFT JOIN dbo.Sys_User AS u2 ON u2.UserId = fc.Main_Coordinator
LEFT JOIN dbo.Sys_User AS u3 ON u3.UserId=fc.User_Representative
LEFT JOIN dbo.Sys_User AS u4 ON u4.UserId = fc.CreatePerson
LEFT JOIN dbo.Sys_User AS u5 ON u5.UserId=fc.ModifyPerson
LEFT JOIN dbo.Base_Discipline AS dis ON dis.DisciplineId = fc.DisciplineId
LEFT JOIN dbo.Base_PriceScheme price ON price.PriceSchemeId = fc.Pricing_Scheme
LEFT JOIN(SELECT SUM(ISNULL(SSR_Actual_cost,0)) AS CheckedValue,FO FROM dbo.FC_SESReport 
			WHERE Accepted='X' GROUP BY FO) AS c ON c.FO=fc.FO_NO	


GO


