using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using HRM.BO; using Microsoft.AspNetCore.Http.HttpResults; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HRM.DA { public class UploadedComponentService : ServiceTemplate, IUploadedComponentService { #region Sevice Head private void MapUploadedComponentObject(UploadedComponent component, DataReader oReader) { base.SetObjectID(component, oReader.GetInt32("UploadedComponentID", true, 0)); component.SalaryMonth = oReader.GetDateTime("SalaryMonth", true, DateTime.MinValue); component.ComponentID = oReader.GetInt32("ComponentID", true, 0); component.ComponentType = (EnumComponentType)oReader.GetInt32("ComponentType", true, 0); component.UploadedBy = oReader.GetInt32("UploadedBy", true, 0); component.UploadedDate = oReader.GetDateTime("UploadedDate", true, DateTime.MinValue); component.ApprovedBy = oReader.GetInt32("ApprovedBy", true, 0); component.ApprovedDate = oReader.GetDateTime("ApprovedDate", true, DateTime.MinValue); component.IsDamageManagement = oReader.GetBoolean("IsDamageManagement", true, false); this.SetObjectState(component, Ease.Core.ObjectState.Saved); } private void MapUploadedComponentEmpObject(UploadedComponentEmp componentEmp, DataReader oReader) { base.SetObjectID(componentEmp, oReader.GetInt32("UploadedComponentEmpID", true, 0)); componentEmp.UploadedComponentID = oReader.GetInt32("UploadedComponentID", true, 0); componentEmp.EmployeeID = oReader.GetInt32("EmployeeID", true, 0); componentEmp.Amount = oReader.GetDouble("Amount", true, 0); componentEmp.Reason = oReader.GetString("Reason", true, string.Empty); componentEmp.TotalAmount = oReader.GetDouble("TotalAmount", true, 0); componentEmp.DeductionMonth = oReader.GetDateTime("DeductionMonth", true, DateTime.MinValue); componentEmp.DeductionAmount = oReader.GetDouble("DeductionAmount", true, 0); componentEmp.Remarks = oReader.GetString("Remarks", true, string.Empty); componentEmp.CreatedDate = oReader.GetDateTime("CreatedDate", true, DateTime.MinValue); componentEmp.ModifiedDate = oReader.GetDateTime("ModifiedDate", true, DateTime.MinValue); this.SetObjectState(componentEmp, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { UploadedComponent component = new UploadedComponent(); MapUploadedComponentObject(component, oReader); return component as T; } private UploadedComponent CreateObject(DataReader oReader) { UploadedComponent component = new UploadedComponent(); MapUploadedComponentObject(component, oReader); return component; } private List CreateChilds(DataReader oReader) { List components = new List(); UploadedComponentEmp component; while (oReader.Read()) { component = new UploadedComponentEmp(); component = CreateChild(oReader); components.Add(component); } return components; } private UploadedComponentEmp CreateChild(DataReader oReader) { UploadedComponentEmp component = new UploadedComponentEmp(); MapUploadedComponentEmpObject(component, oReader); return component; } #endregion #region Service implementation public void Save(UploadedComponent component) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (component.IsNew) { int uploadedComponentID; int uploadedComponentEmpID; uploadedComponentID = tc.GenerateID("UploadedComponent", "UploadedComponentID"); base.SetObjectID(component, uploadedComponentID); UploadedComponentDA.Insert(tc, component); foreach (UploadedComponentEmp item in component.UploadedComponentEmps) { uploadedComponentEmpID = tc.GenerateID("UploadedComponentEmp", "UploadedComponentEmpID"); base.SetObjectID(item, uploadedComponentEmpID); item.CreatedDate = DateTime.Today; item.UploadedComponentID = component.ID; UploadedComponentDA.InsertUploadedComponentEmp(tc, item); } } else { UploadedComponentDA.Update(tc, component); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void SaveTC(UploadedComponent component, TransactionContext tc) { try { if (component.IsNew) { int uploadedComponentID; int uploadedComponentEmpID; uploadedComponentID = tc.GenerateID("UploadedComponent", "UploadedComponentID"); base.SetObjectID(component, uploadedComponentID); UploadedComponentDA.Insert(tc, component); foreach (UploadedComponentEmp item in component.UploadedComponentEmps) { uploadedComponentEmpID = tc.GenerateID("UploadedComponentEmp", "UploadedComponentEmpID"); base.SetObjectID(item, uploadedComponentEmpID); item.CreatedDate = DateTime.Today; item.UploadedComponentID = component.ID; UploadedComponentDA.InsertUploadedComponentEmp(tc, item); } } else { UploadedComponentDA.Update(tc, component); } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } /// The ADParameter of the allowance/deduction that covers the grade; 0 when there is none. private static int GetADParameterID(int gradeID, int allowDeductID) { int id = 0; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(UploadedComponentDA.GetADParamByGradeAndAD(tc, gradeID, allowDeductID)); if (dr.Read()) id = dr.GetInt32("ADPARAMETERID", true, 0); 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 id; } public void SaveWithADParameter(UploadedComponent component, DateTime salaryMonth) { TransactionContext tc = null; try { List employees = new EmployeeService().GetAllLive(); List _aDParameterEmployees = new List(); ADParameterEmployee aDParameterEmployee = null; foreach (UploadedComponentEmp item in component.UploadedComponentEmps) { aDParameterEmployee = new ADParameterEmployee(); int empGradeID = (int)employees.Where(e => e.ID == item.EmployeeID).Select(g => g.GradeID).First(); aDParameterEmployee.ADParameterID = GetADParameterID(empGradeID, component.ComponentID); aDParameterEmployee.Periodicity = EnumPeriodicity.Monthly; aDParameterEmployee.FormDate = GlobalFunctions.FirstDateOfMonth(salaryMonth); aDParameterEmployee.TillDate = GlobalFunctions.LastDateOfMonth(salaryMonth); aDParameterEmployee.MonthlyAmount = item.Amount; aDParameterEmployee.Arreartype = EnumArrearType.NotPresent; aDParameterEmployee.EmployeeID = item.EmployeeID; aDParameterEmployee.AllowDeductID = component.ComponentID; aDParameterEmployee.CreatedBy = (int)component.ApprovedBy; aDParameterEmployee.CreatedDate = DateTime.Today; // approving the upload is the approval of these individual amounts aDParameterEmployee.ApprovalStatus = EnumAuthStatus.Approved; _aDParameterEmployees.Add(aDParameterEmployee); } if (component.UploadedComponentEmps.Count == _aDParameterEmployees.Count) { tc = TransactionContext.Begin(true); this.SaveTC(component, tc); foreach (ADParameterEmployee item in _aDParameterEmployees) { base.SetObjectID(item, tc.GenerateID("ADParameterEmployee", "ADPARAMETEREMPID")); ADParameterEmployeeDA.Insert(tc, item); } tc.End(); } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public List Get(DateTime fromDate, DateTime toDate) { List uploadedComponents = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(UploadedComponentDA.Get(tc, fromDate, toDate)); uploadedComponents = 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 uploadedComponents; } public DataTable GetByID(int uploadedComponentID) { DataTable dtUploadedComponent = new DataTable(); List uploadedComponents = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataSet ds = UploadedComponentDA.GetByID_DT(tc, uploadedComponentID); dtUploadedComponent = (ds != null) && (ds.Tables != null && ds.Tables.Count > 0) ? ds.Tables[0] : new DataTable(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return dtUploadedComponent; } public DataTable GetByIDandUploaderID(int uploadedComponentID, int uploaderId) { DataTable dtUploadedComponent = new DataTable(); List uploadedComponents = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataSet ds = UploadedComponentDA.GetByIDandUploaderID_DT(tc, uploadedComponentID, uploaderId); dtUploadedComponent = (ds != null) && (ds.Tables != null && ds.Tables.Count > 0) ? ds.Tables[0] : new DataTable(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return dtUploadedComponent; } public List Get(int componentID, int uploaderID, DateTime salaryMonth) { List components = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(UploadedComponentDA.Get(tc, componentID, uploaderID, salaryMonth)); components = this.CreateObjects(oreader); oreader.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 components; } public List Get(int uploaderID, DateTime salaryMonth) { List components = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(UploadedComponentDA.Get(tc, uploaderID, salaryMonth)); components = this.CreateObjects(oreader); oreader.Close(); tc.End(); if (components.Any()) { foreach (UploadedComponent uc in components) { if (uc.UploadedComponentEmps == null) uc.UploadedComponentEmps = GetUploadedComponentEmp(uc.ID); } } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return components; } public List GetByApprover(int approverID, DateTime salaryMonth) { List components = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(UploadedComponentDA.GetByApprover(tc, approverID, salaryMonth)); components = this.CreateObjects(oreader); oreader.Close(); tc.End(); if (components.Any()) { foreach (UploadedComponent uc in components) { if (uc.UploadedComponentEmps == null) uc.UploadedComponentEmps = GetUploadedComponentEmp(uc.ID); } } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return components; } public List GetByApprover(int approverID) { List components = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(UploadedComponentDA.GetByApprover(tc, approverID)); components = this.CreateObjects(oreader); oreader.Close(); tc.End(); if (components.Any()) { foreach (UploadedComponent uc in components) { if (uc.UploadedComponentEmps == null) uc.UploadedComponentEmps = GetUploadedComponentEmp(uc.ID); } } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return components; } public DataTable GetByUploaderDT(int empID) { DataTable dtUploadedComponent = new DataTable(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataSet ds = UploadedComponentDA.GetByUploaderDT(tc, empID); dtUploadedComponent = (ds != null) && (ds.Tables != null && ds.Tables.Count > 0) ? ds.Tables[0] : new DataTable(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return dtUploadedComponent; } public List GetUploadedComponentEmp(int componentID) { List componentEmps = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(UploadedComponentDA.GetUploadedComponentEmp(tc, componentID)); componentEmps = this.CreateChilds(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 componentEmps; } public UploadedComponent GetExistingComponent(int uploaderID, int componentID, EnumComponentType componentType, DateTime salaryMonth) { UploadedComponent componentSetup = new UploadedComponent(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(UploadedComponentDA.GetExistingComponent(tc, uploaderID, componentID, componentType, salaryMonth)); if (dr.Read()) { componentSetup = this.CreateObject(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 componentSetup; } public void Delete(int uploadedComponentID) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); UploadedComponentDA.DeleteUploadedComponentEmp(tc, uploadedComponentID); UploadedComponentDA.Delete(tc, uploadedComponentID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void DeleteUploadedComponentEmp(int uploadedComponentID) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); UploadedComponentDA.DeleteUploadedComponentEmp(tc, uploadedComponentID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } #endregion } }