using System; using System.Data; using Ease.Core.Model; using Ease.Core.DataAccess; using Ease.Core; using System.Collections.Generic; using Ease.Core.Utility; using HRM.BO; using System.Linq; namespace HRM.DA { public class HeadCountApprovalRequestService : ServiceTemplate, IHeadCountApprovalRequestService { #region CV Object Mapping private void MapObject(HeadCountApprovalRequest ob, DataReader oReader) { this.SetObjectID(ob, oReader.GetInt32("HeadCountApprovalRequestID").Value); ob.Year = oReader.GetInt32("Year", 0); ob.Designation = oReader.GetString("Designation") != null ? oReader.GetString("Designation") : string.Empty; ob.CategoryID = oReader.GetInt32("CategoryID", 0); ob.Position = oReader.GetString("Position") != null ? oReader.GetString("Position") : string.Empty; ob.RcID = oReader.GetInt32("RcID", 0); ob.RequestDate = oReader.GetString("RequestDate") != null ? oReader.GetDateTime("RequestDate").Value : DateTime.MinValue; ob.RequestType = oReader.GetString("RequestType") != null ? (EnumHeadCountRequestType)oReader.GetInt32("RequestType").Value : EnumHeadCountRequestType.None; ob.Reason = oReader.GetString("Reason") != null ? oReader.GetString("Reason") : string.Empty; ob.UnitID = oReader.GetInt32("DepartmentID", 0); ob.BudgetedManPower = oReader.GetInt32("BudgetedManPower", 0); ob.AdditionalManPower = oReader.GetInt32("AdditionalManPower", 0); ob.WfStatus = oReader.GetString("WfStatus") != null ? (EnumwfStatus)oReader.GetInt32("WfStatus").Value : EnumwfStatus.Initiate; ob.IsMonthly = oReader.GetString("IsMonthly") != null ? oReader.GetBoolean("IsMonthly").Value : false; ob.Remarks = oReader.GetString("Remarks") != null ? oReader.GetString("Remarks") : string.Empty; ob.Month1 = oReader.GetString("Month1") != null ? oReader.GetInt32("Month1").Value : 0; ob.Month2 = oReader.GetString("Month2") != null ? oReader.GetInt32("Month2").Value : 0; ob.Month3 = oReader.GetString("Month3") != null ? oReader.GetInt32("Month3").Value : 0; ob.Month4 = oReader.GetString("Month4") != null ? oReader.GetInt32("Month4").Value : 0; ob.Month5 = oReader.GetString("Month5") != null ? oReader.GetInt32("Month5").Value : 0; ob.Month6 = oReader.GetString("Month6") != null ? oReader.GetInt32("Month6").Value : 0; ob.Month7 = oReader.GetString("Month7") != null ? oReader.GetInt32("Month7").Value : 0; ob.Month8 = oReader.GetString("Month8") != null ? oReader.GetInt32("Month8").Value : 0; ob.Month9 = oReader.GetString("Month9") != null ? oReader.GetInt32("Month9").Value : 0; ob.Month10 = oReader.GetString("Month10") != null ? oReader.GetInt32("Month10").Value : 0; ob.Month11 = oReader.GetString("Month11") != null ? oReader.GetInt32("Month11").Value : 0; ob.Month12 = oReader.GetString("Month12") != null ? oReader.GetInt32("Month12").Value : 0; ob.BudgetedYaer = oReader.GetString("BudgetYear") != null ? oReader.GetInt32("BudgetYear").Value : 0; ob.YearTotal = oReader.GetString("YearToal") != null ? oReader.GetInt32("YearToal").Value : 0; ob.DesignationID = oReader.GetString("DesignationID") != null ? oReader.GetInt32("DesignationID").Value : 0; if (oReader.GetDateTime("CreationDate") == null) { ob.CreatedDate = DateTime.MinValue; } else { ob.CreatedDate = oReader.GetDateTime("CreationDate").Value; } if (oReader.GetDateTime("ModificationDate") == null) { ob.ModifiedDate = DateTime.MinValue; } else { ob.ModifiedDate = oReader.GetDateTime("ModificationDate").Value; } //this.SetObjectState(ob, ObjectState.Saved); } private void MapChild(HeadCountRequestEmp ob, DataReader oReader) { this.SetObjectID(ob, oReader.GetInt32("HeadCountRequestEmpID").Value); ob.HeadCountApprovalRequestID = oReader.GetInt32("HeadCountApprovalRequestID", 0); ob.EmployeeID = oReader.GetInt32("EmployeeID", 0); ob.Department = oReader.GetString("Department") != null ? oReader.GetString("Department") : string.Empty; ob.Designation = oReader.GetString("Designation") != null ? oReader.GetString("Designation") : string.Empty; } protected override T CreateObject(DataReader oReader) { HeadCountApprovalRequest obCv = new HeadCountApprovalRequest(); MapObject(obCv, oReader); return obCv as T; } private List CreateChildObjects(DataReader oReader) { List allExperiences = new List(); while (oReader.Read()) { HeadCountRequestEmp obExp = new HeadCountRequestEmp(); MapChild(obExp, oReader); allExperiences.Add(obExp); } return allExperiences; } #endregion #region Service implementation public HeadCountApprovalRequest Get(int id) { HeadCountApprovalRequest ob = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader oreader = new DataReader(HeadCountApprovalRequestDA.Get(tc, id)); if (oreader.Read()) { ob = this.CreateObject(oreader); } oreader.Close(); if (ob != null) { ob.HeadCountRequestEmps = GetChilds(tc, ob.ID); } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } return ob; } private List GetChilds(TransactionContext tc, int id) { List obs = new List(); try { DataReader oreader = new DataReader(HeadCountApprovalRequestDA.GetChilds(tc, id)); obs = this.CreateChildObjects(oreader); oreader.Close(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } return obs; } public List Get() { List obs = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader oreader = new DataReader(HeadCountApprovalRequestDA.Get(tc)); obs = this.CreateObjects(oreader); oreader.Close(); if (obs != null && obs.Count > 0) { string sEmpIDs = string.Empty; foreach (HeadCountApprovalRequest item in obs) { item.HeadCountRequestEmps = GetChilds(tc, item.ID); if (item.HeadCountRequestEmps != null && item.HeadCountRequestEmps.Count > 0) { foreach (HeadCountRequestEmp itm in item.HeadCountRequestEmps) { sEmpIDs += itm.EmployeeID + ","; } } } sEmpIDs = sEmpIDs.Trim(','); List oEmps = null; if (sEmpIDs != string.Empty) oEmps = new EmployeeService().GetByEmpNos(tc, sEmpIDs); if (oEmps != null && oEmps.Count > 0) { foreach (HeadCountApprovalRequest item in obs) { foreach (HeadCountRequestEmp he in item.HeadCountRequestEmps) { Employee emp = oEmps.Find(x => x.ID == he.EmployeeID); if (emp != null) he.Employee = emp; } } } } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } return obs; } public void Save(HeadCountApprovalRequest ob) { TransactionContext tc = null; int oID = 0; try { tc = TransactionContext.Begin(true); if (ob.IsNew) { int id = tc.GenerateID("HeadCountApprovalRequest", "HeadCountApprovalRequestID"); oID = (id); base.SetObjectID(ob, (id)); HeadCountApprovalRequestDA.Insert(tc, ob); } else { oID = ob.ID; ob.ModifiedBy = 1; ob.ModifiedDate = DateTime.Today; HeadCountApprovalRequestDA.Update(tc, ob); } HeadCountApprovalRequestDA.DeleteChild(tc, ob.ID); foreach (HeadCountRequestEmp item in ob.HeadCountRequestEmps) { int id = tc.GenerateID("HeadCountRequestEmp", "HeadCountRequestEmpID"); item.HeadCountApprovalRequestID = oID; base.SetObjectID(item, (id)); ExperienceDA.InsertChild(item, tc); } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); if (ex.Message.Contains("UK_HeadCountApprovalRequest")) { throw new ServiceException("Cannot insert duplicate data", ex); } else { throw new ServiceException(ex.Message, ex); } #endregion } } public int GetBudgetedHeadCount(int departmentId, int designationId,DateTime positionDate) { List obs = null; TransactionContext tc = null; int? headCount = 0; try { tc = TransactionContext.Begin(); int year = positionDate.Year; DataReader dr = new DataReader(HeadCountApprovalRequestDA.GetBudgetedHeadCount(tc, departmentId, designationId, year)); obs = this.CreateObjects(dr); dr.Close(); tc.End(); if(obs != null && obs.Count > 0) { headCount = obs.Sum(x => x.BudgetedYaer); } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return headCount ?? 0; } public List GetBudgetedHeadCountByrequisitionID(DateTime positionDate) { List obs = new List(); TransactionContext tc = null; int? headCount = 0; try { tc = TransactionContext.Begin(); int year = positionDate.Year; DataReader dr = new DataReader(HeadCountApprovalRequestDA.GetBudgetedHeadCount(tc,0,0, year)); obs = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return obs; } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); HeadCountApprovalRequestDA.Delete(tc, id); tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } public void SaveAuto(List items) { TransactionContext tc = null; int oID = 0; try { tc = TransactionContext.Begin(true); foreach (HeadCountApprovalRequest ob in items) { if (ob.IsNew) { int id = tc.GenerateID("HeadCountApprovalRequest", "HeadCountApprovalRequestID"); oID = (id); base.SetObjectID(ob, (id)); ob.RequestDate = Ease.Core.Utility.Global.DateFunctions.LastDateOfYear(ob.RequestDate); HeadCountApprovalRequestDA.Insert(tc, ob); } else { oID = ob.ID; ob.ModifiedBy = 1; ob.ModifiedDate = DateTime.Today; HeadCountApprovalRequestDA.Update(tc, ob); } } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); if (ex.Message.Contains("UK_HeadCountApprovalRequest")) { throw new ServiceException("Cannot insert duplicate data",ex); } else { throw new ServiceException(ex.Message, ex); } #endregion } } public List Get(int year, int departmentID) { List obs = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader oreader = new DataReader(HeadCountApprovalRequestDA.Get(tc, year, departmentID)); obs = this.CreateObjects(oreader); oreader.Close(); if (obs != null && obs.Count > 0) { string sEmpIDs = string.Empty; foreach (HeadCountApprovalRequest item in obs) { item.HeadCountRequestEmps = GetChilds(tc, item.ID); if (item.HeadCountRequestEmps != null && item.HeadCountRequestEmps.Count > 0) { foreach (HeadCountRequestEmp itm in item.HeadCountRequestEmps) { sEmpIDs += itm.EmployeeID + ","; } } } sEmpIDs = sEmpIDs.Trim(','); List oEmps = null; if (sEmpIDs != string.Empty) oEmps = new EmployeeService().GetByEmpNos(tc, sEmpIDs); if (oEmps != null && oEmps.Count > 0) { foreach (HeadCountApprovalRequest item in obs) { foreach (HeadCountRequestEmp he in item.HeadCountRequestEmps) { Employee emp = oEmps.Find(x => x.ID == he.EmployeeID); if (emp != null) he.Employee = emp; } } } } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } return obs; } #endregion } }