﻿
ALTER TABLE dbo.EMC_Punishment ADD IsUserEvaluated BIT NULL
GO
ALTER TABLE dbo.EMC_Punishment ADD IsDepEvaluated BIT NULL
GO

ALTER TABLE dbo.SyncDataUserLogs ADD Josn NVARCHAR(MAX)
GO

ALTER VIEW [dbo].[FC_SESReportView]
AS
SELECT 
a.SES_No,
a.FO AS FO_NO,
a.Short_Descrption,
CONVERT(VARCHAR(50),a.Start_Date,23) AS Start_Date,
CONVERT(VARCHAR(50),a.End_Date,23) AS End_Date,
CONVERT(VARCHAR(50),a.TECO_Date,23) AS TECO_Date,
CONVERT(VARCHAR(50),a.SES_CONF_Format,23) AS SES_CONF_Format,
CONVERT(VARCHAR(50),a.Claim_sheets_receive,23) AS Claim_sheets_receive,
(CASE WHEN a.Claim_sheets_receive IS NULL THEN a.TECO_Date ELSE a.Claim_sheets_receive END) AS Claim_TECO_Date,
YEAR(CASE WHEN a.Claim_sheets_receive IS NULL THEN a.TECO_Date ELSE a.Claim_sheets_receive END) AS dateYear,
MONTH(CASE WHEN a.Claim_sheets_receive IS NULL THEN a.TECO_Date ELSE a.Claim_sheets_receive END) AS dateMonth,
a.Requisitioner,
u1.Account AS MainCoordinatorUser,
u2.Account AS UserRepresentativeUser,
u3.Account AS ContractAdminUser,
b.DisciplineId,
dis.Discipline AS Discipline_Eng,
dis.DisciplineCN AS Discipline_CN,
(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,
(SELECT TOP 1 p.ViolationDegree FROM dbo.EMC_Punishment p 
  WHERE p.FO_NO=a.FO AND p.ViolationRelatedSes=a.SES_No AND p.Flag='1' 
         AND (p.IsUserEvaluated IS NULL OR IsDepEvaluated IS NULL )) AS ViolationDegree,
(SELECT TOP 1 p.ViolationDegree FROM dbo.EMC_Punishment p 
  WHERE p.FO_NO=a.FO AND p.ViolationRelatedSes=a.SES_No AND p.Flag='1') AS ShowViolationDegree  
FROM FC_SESReport AS a
LEFT JOIN FC_SESRelatedData AS b ON a.FO=b.FO_NO
LEFT JOIN Base_Discipline AS dis ON dis.DisciplineId = b.DisciplineId
LEFT JOIN Sys_User AS u1 ON u1.UserId=b.Main_Coordinator
LEFT JOIN Sys_User AS u2 ON u2.UserId=b.User_Representative
LEFT JOIN Sys_User AS u3 ON u3.UserId=b.Contract_Admin
WHERE YEAR(CASE WHEN a.Claim_sheets_receive IS NULL THEN a.TECO_Date ELSE a.Claim_sheets_receive END)=(CASE MONTH(GETDATE()) WHEN 1 THEN YEAR(GETDATE())-1 ELSE YEAR(GETDATE()) END)
AND MONTH(CASE WHEN a.Claim_sheets_receive IS NULL THEN a.TECO_Date ELSE a.Claim_sheets_receive END)=(CASE MONTH(GETDATE()) WHEN 1 THEN 12 ELSE MONTH(GETDATE())-1 END)
--and a.SES_No not in (select SES_No from Score_JournalEvaluation)
AND (a.Claim_sheets_receive IS NOT NULL OR a.TECO_Date IS NOT NULL)


GO




