using DocumentFormat.OpenXml.Office2010.Excel;
using Ease.Core.DataAccess;
using HRM.BO;
using NPOI.OpenXmlFormats.Dml.Chart;
using System;
using System.Data;
using System.Data.SqlClient;
namespace HRM.DA
{
public class ObjectiveDA
{
internal static IDataReader Get(TransactionContext tc)
{
string sql = SQLParser.MakeSQL("SELECT * FROM Objective");
return tc.ExecuteReader(sql);
}
internal static IDataReader Get(TransactionContext tc, int id)
{
string sql = SQLParser.MakeSQL("SELECT * FROM Objective WHERE ObjectiveID = %n", id);
return tc.ExecuteReader(sql);
}
internal static IDataReader GetObjectiveSet(TransactionContext tc, int ObjectiveSetId)
{
string sql = SQLParser.MakeSQL("SELECT * FROM Objective WHERE ObjectiveSetID = %n", ObjectiveSetId);
return tc.ExecuteReader(sql);
}
internal static void Insert(TransactionContext tc, Objective item)
{
string sql = SQLParser.MakeSQL(
"Insert Into Objective(ObjectiveID, ObjectiveSetID, PMPYearID, Description, Activities, MeasuresOfSuccess, Weightages, EmployeeID, EmployeeNodeID, ObjectiveDate, LMID, LMNodeID, LMRatingID, RagingDate, MYEmployeeComments, MYLMComments, IsDraft,ObjectiveType,YEEmployeeComments, YELMComments,IsOldObjective,PriorityPercent, StartDate, EndDate ,MYAssessRating,MYLMAssessRating,MYStatus,YEStatus,KPIType,yeempmark,yelmmark,GroupID,Tier, Title, MASTERKPIID, ProgressStatus, ObjectiveStatus, PayrollTypeID, ObjectiveSettingRemarksLM, ObjectiveSettingRemarksLM2) " +
"Values(%n, %n, %n, %s, %s, %s, %n, %n, %n, %d, %n, %n, %n, %d, %s, %s, %b,%n,%s,%s, %b,%n, %d, %d,%n,%n,%n,%n,%n,%n,%n,%n,%s,%s, %n, %n, %n, %n, %s, %s)",
item.ID, DataReader.GetNullValue(item.ObjectiveSetID), DataReader.GetNullValue(item.PMPYearID),
item.Description, item.Activities, item.MeasuresOfSuccess, item.Weightages,
DataReader.GetNullValue(item.EmployeeID), DataReader.GetNullValue(item.EmployeeNodeID),
DataReader.GetNullValue(item.ObjectiveDate), DataReader.GetNullValue(item.LMID), DataReader.GetNullValue(item.LMNodeID),
item.LMRatingID, DataReader.GetNullValue(item.RagingDate), item.MYEmployeeComments, item.MYLMComments, item.IsDraft,
(int)item.ObjectiveType, item.YEEmployeeComments, item.YELMComments, item.IsOldObjective,
item.PriorityPercent, item.StartDate, item.EndDate,
item.MYAssessRating, item.MYLMAssessRating, (int)item.MYStatus, (int)item.YEStatus, item.KPIType,
item.YEEmpMark, item.YELMMark, DataReader.GetNullValue(item.GroupID), item.Tier, item.Title, item.MasterKpiId, item.ProgressStatus, (int)item.ObjectiveStatus, item.PayrollTypeID,
item.ObjectiveSettingRemarksLM, item.ObjectiveSettingRemarksLM2);
tc.ExecuteNonQuery(sql);
}
internal static int InsertHistory(TransactionContext tc, EnumOperationalStatus changestatus, ObjectiveSet objectiveSet, Objective oldObjective, Objective newObjective, bool outsideOfSettings = false)
{
DateTime? newFromDate = newObjective == null ? null : DataReader.GetNullValue(newObjective.StartDate);
DateTime? newToDate = newObjective == null ? null : DataReader.GetNullValue(newObjective.EndDate);
double? newWeightage = newObjective == null ? null : DataReader.GetNullValue(newObjective.Weightages);
// TOP 1 ObjectiveHistoryID rather than the original COUNT(*): still the same existence
// check (any row returned = exists), but now also hands back the id of that row so
// callers (e.g. staged attachment linkage) can act on the exact matched history row
// instead of only knowing whether one exists.
Object obj = null;
if (newObjective != null)
// ISNULL, not a plain "=": pre-migration rows have OutsideOfSettings = NULL and must
// still be treated as "false" so the legacy correction-mode dedup keeps matching them.
obj = tc.ExecuteScalar("Select TOP 1 ObjectiveHistoryID FROM ObjectiveHistory WHERE " +
" objectivesetid=%n and objectiveid=%n and employeeid=%n and issubmitted =%b and ISNULL(OutsideOfSettings, 0) = %b ",
objectiveSet.ID, newObjective.ID, newObjective.EmployeeID, false, outsideOfSettings);
int? existingHistoryId = (obj == null || obj == DBNull.Value) ? (int?)null : Convert.ToInt32(obj);
if (existingHistoryId.HasValue)
{
ObjectiveDA.updatetHistory(tc, changestatus, oldObjective, newObjective, outsideOfSettings);
return existingHistoryId.Value;
}
else
{
string sql = SQLParser.MakeSQL(
@"Insert Into ObjectiveHistory
(
ObjectiveSetID,
EmployeeID,
ObjectiveID,
ChangeType,
ObjectiveTypeID,
Title,
Target,
FromDate,
ToDate,
Weightage,
NewTitle,
NewTarget,
NewFromDate,
NewToDate,
NewWeightage,
Remarks,
UserID,
CreatedDate,
OutsideOfSettings,
PMPYearID
)
Values
(
%n, %n, %n, %n, %n, %s, %s, %D, %D, %n,
%s, %s, %D, %D, %n, %s, %n, %D, %b, %n
)",
objectiveSet.ID,
objectiveSet.EmployeeID,
oldObjective.ID,
changestatus,
oldObjective.ObjectiveType,
oldObjective.Title,
oldObjective.MeasuresOfSuccess,
oldObjective.StartDate,
oldObjective.EndDate,
oldObjective.Weightages,
newObjective == null ? null : (oldObjective.Title == newObjective.Title) ? null : newObjective.Title,
newObjective == null ? null : (oldObjective.MeasuresOfSuccess == newObjective.MeasuresOfSuccess) ? null : newObjective.MeasuresOfSuccess,
newObjective == null ? null : (oldObjective.StartDate == newObjective.StartDate) ? null : newObjective.StartDate,
newObjective == null ? null : (oldObjective.EndDate == newObjective.EndDate) ? null : newObjective.EndDate,
newObjective == null ? null : (oldObjective.Weightages == newObjective.Weightages) ? null : newObjective.Weightages,
null,
objectiveSet.CreatedBy,
DateTime.Today,
outsideOfSettings,
objectiveSet.PMPYearID
);
object newIdObj = tc.ExecuteScalar(sql + "; SELECT CAST(SCOPE_IDENTITY() AS INT)");
return (newIdObj == null || newIdObj == DBNull.Value) ? 0 : Convert.ToInt32(newIdObj);
}
}
///
/// Stages a brand-new (not-yet-existing) KPI proposed outside of settings. Nothing is written
/// to the Objective table here — the proposal lives only in ObjectiveHistory (ObjectiveID is
/// left NULL) until a line manager approves it (ObjectiveService.ApplyOutsideOfSettingsHistory).
/// Title/Target/FromDate/ToDate/Weightage (the "old value" columns) are NOT NULL in the
/// existing schema, built around every row being a change to something that already exists -
/// there's no real "old" value for a KPI that doesn't exist yet, so the proposed values are
/// written into both the old and new columns rather than widening the schema further.
///
internal static int InsertHistoryForNewObjective(TransactionContext tc, ObjectiveSet objectiveSet, Objective newObjective)
{
string sql = SQLParser.MakeSQL(
@"Insert Into ObjectiveHistory
(
ObjectiveSetID,
EmployeeID,
ObjectiveID,
ChangeType,
ObjectiveTypeID,
Title,
Target,
FromDate,
ToDate,
Weightage,
NewTitle,
NewTarget,
NewFromDate,
NewToDate,
NewWeightage,
UserID,
CreatedDate,
OutsideOfSettings,
PMPYearID
)
Values
(
%n, %n, %n, %n, %n, %s, %s, %D, %D, %n, %s, %s, %D, %D, %n, %n, %D, %b, %n
)",
objectiveSet.ID,
objectiveSet.EmployeeID,
null,
(int)EnumOperationalStatus.Add,
newObjective.ObjectiveType,
newObjective.Title,
newObjective.MeasuresOfSuccess,
newObjective.StartDate,
newObjective.EndDate,
newObjective.Weightages,
newObjective.Title,
newObjective.MeasuresOfSuccess,
newObjective.StartDate,
newObjective.EndDate,
newObjective.Weightages,
objectiveSet.CreatedBy,
DateTime.Today,
true,
objectiveSet.PMPYearID
);
object newIdObj = tc.ExecuteScalar(sql + "; SELECT CAST(SCOPE_IDENTITY() AS INT)");
return (newIdObj == null || newIdObj == DBNull.Value) ? 0 : Convert.ToInt32(newIdObj);
}
///
/// All pending (still-submitted, not yet decided) outside-of-settings history rows for an
/// objective set — the set a LM approve/reject action processes as one batch. pmpYearId is a
/// belt-and-suspenders check on top of ObjectiveSetID (which already implies a single year) -
/// it stops an approve/reject from ever touching a row from the wrong PMP year. IsApproved IS
/// NULL is a second one: a row already decided must never be re-applied/re-rejected even if
/// something else (e.g. a later unrelated resubmit) ever flips IsSubmitted back to true.
///
internal static IDataReader GetPendingOutsideOfSettingsHistory(TransactionContext tc, int objectiveSetId, int pmpYearId)
{
string sql = SQLParser.MakeSQL(
@"SELECT ObjectiveHistoryID, ObjectiveID, EmployeeID, ChangeType, ObjectiveTypeID,
NewTitle, NewTarget, NewFromDate, NewToDate, NewWeightage
FROM ObjectiveHistory
WHERE ObjectiveSetID = %n AND PMPYearID = %n AND IsSubmitted = %b AND OutsideOfSettings = %b
AND IsApproved IS NULL",
objectiveSetId, pmpYearId, true, true);
return tc.ExecuteReader(sql);
}
internal static void ApplyHistoryEditToObjective(TransactionContext tc, int objectiveId, string newTitle,
string newTarget, DateTime? newFromDate, DateTime? newToDate, double? newWeightage)
{
string sql = SQLParser.MakeSQL(
@"Update Objective Set
Title = COALESCE(%s, Title),
MeasuresOfSuccess = COALESCE(%s, MeasuresOfSuccess),
StartDate = COALESCE(%D, StartDate),
EndDate = COALESCE(%D, EndDate),
Weightages = COALESCE(%n, Weightages),
ObjectiveStatus = %n
Where ObjectiveID = %n",
newTitle, newTarget, newFromDate, newToDate, newWeightage, (int)EnumObjectiveStatus.No_Change, objectiveId);
tc.ExecuteNonQuery(sql);
}
internal static void SetObjectiveStatus(TransactionContext tc, int objectiveId, EnumObjectiveStatus status)
{
string sql = SQLParser.MakeSQL(
@"Update Objective Set ObjectiveStatus = %n Where ObjectiveID = %n", (int)status, objectiveId);
tc.ExecuteNonQuery(sql);
}
internal static void UpdateHistoryObjectiveID(TransactionContext tc, int objectiveHistoryId, int objectiveId)
{
string sql = SQLParser.MakeSQL(
@"Update ObjectiveHistory Set ObjectiveID = %n Where ObjectiveHistoryID = %n", objectiveId, objectiveHistoryId);
tc.ExecuteNonQuery(sql);
}
internal static void SetHistoryDecision(TransactionContext tc, int objectiveHistoryId, bool isApproved, string remarks, int userId)
{
string sql = SQLParser.MakeSQL(
@"Update ObjectiveHistory Set IsSubmitted = %b, IsApproved = %b, LMActionRemarks = %s,
LMActionDate = %D, UserID = %n
Where ObjectiveHistoryID = %n",
false, isApproved, remarks, DateTime.Today, userId, objectiveHistoryId);
tc.ExecuteNonQuery(sql);
}
///
/// Rows the employee still needs to see: pending outside-of-settings changes (awaiting LM
/// decision) plus rejected ones not yet dismissed by the employee.
///
internal static DataTable GetObjectiveHistoryStatus(TransactionContext tc, int objectiveSetId)
{
string sql = SQLParser.MakeSQL(@"
SELECT
oh.ObjectiveHistoryID,
oh.ObjectiveID,
oh.ChangeType,
CASE oh.ChangeType
WHEN 1 THEN 'New'
WHEN 2 THEN 'Modify'
WHEN 3 THEN 'Delete'
ELSE 'Unknown'
END AS ActionType,
COALESCE(o.description, ot.name) as Area,
COALESCE(oh.Title, oh.NewTitle) as Title,
oh.NewTitle,
oh.Target,
oh.NewTarget,
oh.FromDate,
oh.NewFromDate,
oh.ToDate,
oh.NewToDate,
oh.Weightage,
oh.NewWeightage,
oh.IsSubmitted,
oh.IsApproved,
oh.LMActionRemarks as ActionRemarks,
oh.LMActionDate as ActionDate,
oh.PMPYearID
FROM ObjectiveHistory oh
LEFT JOIN Objective o ON oh.ObjectiveID = o.OBJECTIVEID
LEFT JOIN ObjectiveType ot ON oh.ObjectiveTypeID = ot.ObjectiveTypeID
WHERE oh.ObjectiveSetID = %n
AND oh.OutsideOfSettings = %b
AND (oh.IsApproved IS NULL OR oh.IsApproved = %b);",
objectiveSetId, true, false);
return tc.ExecuteDataTable(sql);
}
///
/// Outside-of-settings history rows that still count toward the KPI weightage total: ones
/// still awaiting an LM decision plus ones already approved. Rejected/dismissed rows are
/// excluded since they never take (or no longer take) effect.
///
internal static DataTable GetObjectiveHistoryWeightage(TransactionContext tc, int objectiveSetId)
{
string sql = SQLParser.MakeSQL(@"
SELECT
oh.ObjectiveHistoryID,
oh.ObjectiveID,
oh.NewWeightage
FROM ObjectiveHistory oh
WHERE oh.ObjectiveSetID = %n
AND oh.OutsideOfSettings = %b
AND (oh.IsApproved IS NULL OR oh.IsApproved = %b);",
objectiveSetId, true, true);
return tc.ExecuteDataTable(sql);
}
///
/// Clears a single rejected row's pending flag once the employee has seen it - scoped to one
/// ObjectiveHistoryID (not the whole set) so dismissing one KPI's rejection notice doesn't
/// also clear a sibling KPI's.
///
internal static void DismissObjectiveHistory(TransactionContext tc, int objectiveHistoryId)
{
string sql = SQLParser.MakeSQL(
@"Update ObjectiveHistory Set OutsideOfSettings = %b
Where ObjectiveHistoryID = %n AND OutsideOfSettings = %b AND IsSubmitted = %b AND IsApproved = %b",
false, objectiveHistoryId, true, false, false);
tc.ExecuteNonQuery(sql);
}
internal static void updatetHistory(TransactionContext tc, EnumOperationalStatus changestatus, Objective oldObjective, Objective newObjective, bool outsideOfSettings = false)
{
//string sql = SQLParser.MakeSQL(
// @"update ObjectiveHistory
// set
// NewTitle =%s,
// NewTarget=%s,
// NewFromDate =%d,
// NewToDate=%d,
// NewWeightage=%n,
// UserID=%n,
// CreatedDate=%d
// where objectivesetid=%n and objectiveid=%n and employeeid=%n
// ",
// (oldObjective.Title == newObjective.Title) ? null : newObjective.Title,
// (oldObjective.MeasuresOfSuccess == newObjective.MeasuresOfSuccess) ? null : newObjective.MeasuresOfSuccess,
// (oldObjective.StartDate == newObjective.StartDate) ? null : newObjective.StartDate,
// (oldObjective.EndDate == newObjective.EndDate) ? null : newObjective.EndDate,
// (oldObjective.Weightages == newObjective.Weightages) ? null : newObjective.Weightages,
// oldObjective.CreatedBy,
// DateTime.Today,
// newObjective.ObjectiveSetID,
// newObjective.ID,
// newObjective.EmployeeID
//);
//tc.ExecuteNonQuery(sql);
var setClauses = new System.Collections.Generic.List();
var parameters = new System.Collections.Generic.List