using HRM.BO; using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; using static iTextSharp.text.pdf.AcroFields; namespace HRM.DA { #region OpiParameter Service public class OpiParameterService : ServiceTemplate, IOpiParameterService { #region Private functions and declaration public OpiParameterService() { } private void MapObject(OpiParameter oOpiParameter, DataReader oReader) { base.SetObjectID(oOpiParameter, oReader.GetInt32("OpiParameterID").Value); oOpiParameter.OpiType = (EnumOpiType)oReader.GetInt32("OpiType").Value; oOpiParameter.AmountToDistribute = oReader.GetDouble("AmountToDistribute").Value; oOpiParameter.AvgMonth = oReader.GetInt32("AvgMonth").Value; oOpiParameter.FlatAmount = oReader.GetDouble("FlatAmount").Value; oOpiParameter.ProvisionAmount = oReader.GetDouble("ProvisionAmount").Value; oOpiParameter.MinAmount = oReader.GetDouble("MinAmount") == null ? 0.0 : oReader.GetDouble("MinAmount").Value; oOpiParameter.MaxAmount = oReader.GetDouble("MaxAmount") == null ? 0.0 : oReader.GetDouble("MaxAmount").Value; oOpiParameter.FromDate = oReader.GetDateTime("FromDate") == null ? DateTime.MinValue : oReader.GetDateTime("FromDate").Value; oOpiParameter.ToDate = oReader.GetDateTime("ToDate") == null ? DateTime.MinValue : oReader.GetDateTime("ToDate").Value; oOpiParameter.Gender = (EnumGender)oReader.GetInt32("Gender"); oOpiParameter.Status = (EnumStatus)oReader.GetInt32("Status"); //oOpiParameter.Sequence = oReader.GetInt32("Sequence").Value; oOpiParameter.IsActive = oReader.GetBoolean("IsActive").Value; oOpiParameter.IsEarnedBasic = oReader.GetBoolean("IsEarnedBasic").Value; oOpiParameter.IsTaxable = oReader.GetBoolean("IsTaxable").Value; oOpiParameter.IsConfirmed = oReader.GetBoolean("IsConfirmed").Value; oOpiParameter.IsFractionate = oReader.GetBoolean("IsFractionate").Value; oOpiParameter.IsIAApplicable = oReader.GetBoolean("IsIAApplicable").Value; oOpiParameter.IsWithException = oReader.GetBoolean("IsWithException").Value; oOpiParameter.OpiItemID = oReader.GetString("OpiItemID") == null ? 0 : oReader.GetInt32("OpiItemID").Value; oOpiParameter.OpiPeriodicity = (EnumOpiPeriodicity)oReader.GetInt32("OpiPeriodicity"); oOpiParameter.PercentOfBasic = oReader.GetDouble("PercentOfBasic").Value; oOpiParameter.PercentOfGross = oReader.GetDouble("PercentOfGross").Value; oOpiParameter.EntitleType = (EnumEntitleType)oReader.GetInt32("EntitleType").Value; oOpiParameter.OpiAvgPayType = (EnumOpiAvgPayType)oReader.GetInt32("OpiAvgPayType").Value; oOpiParameter.OpiAvgPayItemID = oReader.GetString("OpiAvgPayItemID") == null ? 0 : oReader.GetInt32("OpiAvgPayItemID").Value; oOpiParameter.NoOfMonth = oReader.GetInt32("NoOfMonth").Value; oOpiParameter.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value; oOpiParameter.CreatedDate = oReader.GetDateTime("CreationDate").Value; oOpiParameter.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value; oOpiParameter.ModifiedDate = oReader.GetDateTime("ModifiedDate"); //oOpiParameter.GFFlatAmount = oReader.GetDouble("GFFlatAmount").Value; //oOpiParameter.GFPercentOfBasic = oReader.GetDouble("GFPercentOfBasic").Value; //oOpiParameter.GFPercentOfGross = oReader.GetDouble("GFPercentOfGross").Value; oOpiParameter.PayrollTypeID = oReader.GetString("PayrollTypeID") == null ? 0 : oReader.GetInt32("PayrollTypeID").Value; this.SetObjectState(oOpiParameter, Ease.Core.ObjectState.Saved); } private void MapParameterGradeObject(OpiParameterGrade oGrade, DataReader oReader) { base.SetObjectID(oGrade, oReader.GetInt32("OPIPARAMETERGradeID").Value); oGrade.OpiParameterId = oReader.GetString("OpiParameterID") == null ? 0 : oReader.GetInt32("OpiParameterID").Value; oGrade.GradeId = oReader.GetString("GradeID") == null ? 0 : oReader.GetInt32("GradeID").Value; oGrade.OpiItemId = oReader.GetString("OpiItemId") == null ? 0 : oReader.GetInt32("OpiItemId").Value; oGrade.PayrollTypeID = oReader.GetString("PayrollTypeID") == null ? 0 : oReader.GetInt32("PayrollTypeID").Value; this.SetObjectState(oGrade, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { OpiParameter oOpiParameter = new OpiParameter(); MapObject(oOpiParameter, oReader); return oOpiParameter as T; } protected OpiParameter CreateObject(DataReader oReader) { OpiParameter oOpiParameter = new OpiParameter(); MapObject(oOpiParameter, oReader); return oOpiParameter; } protected List CreateParameterGradeObject(DataReader oReader) { List oGrades = new List(); while (oReader.Read()) { OpiParameterGrade oGrade = new OpiParameterGrade(); MapParameterGradeObject(oGrade, oReader); oGrades.Add(oGrade); } return oGrades; } #endregion #region Service implementation public OpiParameter Get(int id, int payrollTypeID) { OpiParameter oOpiParameter = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(OpiParameterDA.Get(tc, id, payrollTypeID)); if (oreader.Read()) { oOpiParameter = this.CreateObject(oreader); } oreader.Close(); if (oOpiParameter != null) { DataReader dr = new DataReader(OpiParameterDA.GetGrades(tc, oOpiParameter.ID, payrollTypeID)); oOpiParameter.OpiParameterGrades = this.CreateParameterGradeObject(dr); dr.Close(); oOpiParameter.OpiParameterIndividuals = new OpiParameterIndividualService().GetByParameterID(tc, oOpiParameter.ID); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oOpiParameter; } public List Get(int payrollTypeID) { List oOpiParameters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(OpiParameterDA.Get(tc, payrollTypeID)); oOpiParameters = 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 oOpiParameters; } public List GetGrades(int opiParameterID, int payrollTypeID) { List oGrade = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(OpiParameterDA.GetGrades(tc, opiParameterID, payrollTypeID)); oGrade = this.CreateParameterGradeObject(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 oGrade; } public List GetGrades(int payrollTypeID) { List oGrades = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(OpiParameterDA.GetGrades(tc, payrollTypeID)); oGrades = this.CreateParameterGradeObject(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 oGrades; } public List Get(EnumStatus status, int payrollTypeID) { List opiParameters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(OpiParameterDA.Get(tc, status, payrollTypeID)); opiParameters = this.CreateObjects(dr); dr.Close(); if(opiParameters.Count > 0) { List ogrades = this.GetGrades(payrollTypeID); opiParameters.ForEach(x => { x.OpiParameterGrades = ogrades.FindAll(y => y.OpiParameterId == x.ID); }); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return opiParameters; } public List Get(EnumStatus status, EnumOpiType nType, int payrollTypeID) { List opiParameters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(OpiParameterDA.Get(tc, status, nType, payrollTypeID)); opiParameters = this.CreateObjects(dr); foreach (OpiParameter oparam in opiParameters) { oparam.OpiParameterGrades = this.GetGrades(oparam.ID, payrollTypeID); } 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 opiParameters; } public int Save(OpiParameter oOpiParameter) { TransactionContext tc = null; int id = 0; try { tc = TransactionContext.Begin(true); if (oOpiParameter.IsNew) { id = tc.GenerateID("OpiParameter", "OpiParameterID"); base.SetObjectID(oOpiParameter, id); OpiParameterDA.Insert(tc, oOpiParameter); } else { OpiParameterDA.Update(tc, oOpiParameter); OpiParameterDA.DeleteGrades(tc, oOpiParameter.ID); OpiParameterIndividualDA.DeletebyOpiParamId(tc, oOpiParameter.ID); } id = tc.GenerateID("OpiParameterIndividual", "OpiParameterIndividualID"); foreach (OpiParameterIndividual opiIndv in oOpiParameter.OpiParameterIndividuals) { id = id + 1; opiIndv.OpiParameterID = oOpiParameter.ID; base.SetObjectID(opiIndv, id); OpiParameterIndividualDA.Insert(tc, opiIndv); } id = tc.GenerateID("OPIPARAMETERGRADE", "OPIPARAMETERGRADEID"); foreach (OpiParameterGrade oGrade in oOpiParameter.OpiParameterGrades) { id = id + 1; base.SetObjectID(oGrade, id); oGrade.OpiParameterId = oOpiParameter.ID; oGrade.OpiItemId = oOpiParameter.OpiItemID; OpiParameterDA.InsertGrade(tc, oGrade); } tc.End(); return oOpiParameter.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); OpiParameterDA.Delete(tc, id); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } #endregion #region IOpiParameterService Members public List Get(EnumStatus enumStatus, EnumOpiType enumOpiType, EnumEntitleType enumEntitleType, int payrollTypeID) { List opiParameters = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(OpiParameterDA.Get(tc, enumStatus, enumOpiType, enumEntitleType, payrollTypeID)); opiParameters = this.CreateObjects(dr); foreach (OpiParameter oparam in opiParameters) { oparam.OpiParameterGrades = this.GetGrades(oparam.ID, payrollTypeID); } 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 opiParameters; } public List GetUsedGrades(int alllowdeductid) { List aDParameterGrade = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(OpiParameterDA.GetUsedGrades(tc, alllowdeductid)); aDParameterGrade = this.CreateParameterGradeObject(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 aDParameterGrade; } public void ChangeStatus(int id, EnumStatus enumStatus) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); OpiParameterDA.ChangeStatus(tc, id, enumStatus); 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(EnumStatus enumStatus, EnumEntitleType enumEntitleType, int payrollTypeID) { List opiParameters = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(OpiParameterDA.Get(tc, enumStatus, enumEntitleType, payrollTypeID)); opiParameters = this.CreateObjects(dr); foreach (OpiParameter oparam in opiParameters) { oparam.OpiParameterGrades = this.GetGrades(oparam.ID, payrollTypeID); } 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 opiParameters; } #endregion public List GetApplicableParameters(Employee employee, int graId, List parameters) { List applicableParams = parameters.FindAll(delegate (OpiParameter param) { return IsApplicable(param, graId, employee); }); return applicableParams; } public OpiParameter GetApplicableParameter(Employee employee, int graId, List parameters, int itemid) { foreach (OpiParameter item in parameters) { if (item.OpiItemID == itemid) { if (IsApplicable(item, graId, employee) == true) return item; } } return null; } private bool IsApplicable(OpiParameter param, int graid, Employee employee) { bool isApplicable = false; foreach (OpiParameterGrade adgrade in param.OpiParameterGrades) { if (adgrade.GradeId == graid) { isApplicable = true; } } if (!isApplicable) return false; if (param.Gender != EnumGender.None && param.Gender != employee.Gender) { return false; } if (param.IsConfirmed && !employee.IsConfirmed) { return false; } return true; } public List ApplicableParameters(Employee emlpoyee, OPIProcessDetail detail, List allAdparameters ,List gradesalaries) { List oparameters = new List(); foreach (EmployeeGradeSalary gs in gradesalaries) { List lparameter = this.GetApplicableParameters(emlpoyee, gs.GradeID, allAdparameters); foreach (OpiParameter item in lparameter) { if (oparameters.Contains(item) == false) oparameters.Add(item); } } return oparameters; //foreach (EmployeeGradeSalary gs in salary.ArrearPaidGradeSalaries) //{ // List aplparameter = this.GetApplicableParameters(emlpoyee, gs.GradeID, allAdparameters); // gs.ADParameters = new List(); // foreach (ADParameter item in aplparameter) // gs.ADParameters.Add(item); //} } public double GetAmount(Employee oEmp, OpiParameter opiParam) { OpiParameterIndividual adEmp = null; double nAmount = 0.0; switch (opiParam.EntitleType) { case EnumEntitleType.Grade: if (opiParam.PercentOfBasic > 0) nAmount = oEmp.BasicSalary * opiParam.PercentOfBasic / 100.0 + opiParam.FlatAmount; else if (opiParam.PercentOfGross > 0) nAmount = oEmp.GrossSalary * opiParam.PercentOfBasic / 100.0 + opiParam.FlatAmount; else nAmount = opiParam.FlatAmount; break; case EnumEntitleType.Individual: adEmp = new OpiParameterIndividualService().Get(oEmp.ID, opiParam.OpiItemID, opiParam.ID); if (adEmp != null) nAmount = adEmp.Value; else { if (opiParam.PercentOfBasic > 0) nAmount = oEmp.BasicSalary * opiParam.PercentOfBasic / 100.0 + opiParam.FlatAmount; else if (opiParam.PercentOfGross > 0) nAmount = oEmp.GrossSalary * opiParam.PercentOfBasic / 100.0 + opiParam.FlatAmount; else nAmount = opiParam.FlatAmount; } break; } if (opiParam.MinAmount != 0 || opiParam.MaxAmount != 0) { if (nAmount < opiParam.MinAmount) { nAmount = opiParam.MinAmount; } if (nAmount > opiParam.MaxAmount) { nAmount = opiParam.MaxAmount; } } return nAmount; } public double GetIndividualAmount(Employee employee, DateTime fortheMonth, OpiParameterIndividual ademployee, double basicSalary, double grossSalary, OpiParameter opiParam, DateTime nextProcessDate) { double nAmount = 0.0; switch (opiParam.EntitleType) { case EnumEntitleType.Grade: throw new ServiceException("You have choosed the wrong overloaded function to calculate" + " Grade defined Allowance"); case EnumEntitleType.Individual: if (ademployee.ArrearType == EnumArrearType.NotPresent) { DateTime fromDate = GlobalFunctions.FirstDateOfMonth(nextProcessDate); DateTime toDate = nextProcessDate; if (ademployee.FromDate < GlobalFunctions.FirstDateOfMonth(fortheMonth)) fromDate = GlobalFunctions.FirstDateOfMonth(fortheMonth); else fromDate = ademployee.FromDate; if (ademployee.ToDate == null || ademployee.ToDate == DateTime.MinValue) toDate = GlobalFunctions.LastDateOfMonth(fortheMonth); else toDate = (DateTime)ademployee.ToDate; if (toDate > GlobalFunctions.LastDateOfMonth(fortheMonth)) toDate = GlobalFunctions.LastDateOfMonth(fortheMonth); if (ademployee.OpiPeriodicity != EnumOpiPeriodicity.OnceOff) if (employee.EndOfContractDate != null || employee.EndOfContractDate != DateTime.MinValue) if (employee.EndOfContractDate < toDate) toDate = (DateTime)employee.EndOfContractDate; if (ademployee.ValueType == EnumValueType.Other) { //while percent of basic then not required to do fractionate of date basis //if(this.PercentOfBasic >0) // nAmount = (basicSalary * ademployee.MonthlyAmount)/100 * PayrollPayrollGlobalFunctions.GetFraction(fromDate, toDate); //else if (this.PercentOfGross>0) // nAmount = (grossSalary * ademployee.MonthlyAmount)/100 * PayrollPayrollGlobalFunctions.GetFraction(fromDate, toDate); if (opiParam.PercentOfBasic > 0) nAmount = (basicSalary * ademployee.Value) / 100 * GlobalFunctions.GetFraction(fromDate, toDate); else if (opiParam.PercentOfGross > 0) nAmount = (grossSalary * ademployee.Value) / 100 * GlobalFunctions.GetFraction(fromDate, toDate); } else nAmount = ademployee.Value * GlobalFunctions.GetFraction(fromDate, toDate);//PayrollPayrollGlobalFunctions.GetFraction(fromDate, toDate); } else if (ademployee.ArrearType == EnumArrearType.ToCalculate) { if (ademployee.ValueType == EnumValueType.Other) { if (opiParam.PercentOfBasic > 0) nAmount = (basicSalary * ademployee.Value) / 100 * GlobalFunctions.GetFraction(ademployee.FromDate, (DateTime)ademployee.ToDate); else if (opiParam.PercentOfGross > 0) nAmount = (grossSalary * ademployee.Value) / 100 * GlobalFunctions.GetFraction(ademployee.FromDate, (DateTime)ademployee.ToDate); } else nAmount = ademployee.Value * GlobalFunctions.GetFraction(ademployee.FromDate, (DateTime)ademployee.ToDate); } break; } return nAmount; } public double GetGradeDefinedAmount(Employee employee, List gradeSalarires, OpiParameter param) { double nAmount = 0; foreach (EmployeeGradeSalary item in gradeSalarires) { nAmount = nAmount + this.GetGradeDefinedAmount(employee, item.BasicSalary, item.GrossSalary, item.FractionofFromTo, param); } return nAmount; } public double GetGradeDefinedAmount(Employee employee, EmployeeGradeSalary gradeSalary, OpiParameter param) { return this.GetGradeDefinedAmount(employee, gradeSalary.BasicSalary, gradeSalary.GrossSalary, gradeSalary.FractionofFromTo , param); } public double GetGradeDefinedAmount(Employee employee, double basicSalary, double grossSalary, double fractionate, OpiParameter opiParam) { OpiParameterIndividual opiIndividual = null; double nAmount = 0.0; switch (opiParam.EntitleType) { case EnumEntitleType.Grade: if (opiParam.PercentOfBasic + opiParam.GFPercentOfBasic + opiParam.FlatAmount + opiParam.GFFlatAmount > 0) { // if (this.PercentOfBasic > 0) // //nAmount = basicSalary * this.PercentOfBasic / 100.0 + this.FlatAmount; // if(this.IsFractionate) // nAmount = nAmount + this.FlatAmount * fractionate + (basicSalary * this.PercentOfBasic * fractionate) / 100.0; // else // nAmount = nAmount + ((basicSalary * this.PercentOfBasic / 100.0) * fractionate); //else if (this.PercentOfGross > 0) // //nAmount = grossSalary * this.PercentOfGross / 100.0 + this.FlatAmount; // if (this.IsFractionate) // nAmount = nAmount + this.FlatAmount * fractionate + (basicSalary * this.PercentOfBasic * fractionate) / 100.0; // else // nAmount = nAmount + ((basicSalary * this.PercentOfBasic / 100.0) * fractionate); //else // nAmount = this.FlatAmount * fractionate; if (opiParam.IsFractionate) { nAmount = nAmount + opiParam.FlatAmount * fractionate + (basicSalary * opiParam.PercentOfBasic * fractionate) / 100.0; } else { nAmount = nAmount + opiParam.FlatAmount + ((basicSalary * opiParam.PercentOfBasic / 100.0) * fractionate); } } else if (opiParam.PercentOfGross + opiParam.GFPercentOfGross > 0) //nAmount = grossSalary * this.PercentOfGross / 100.0 + this.FlatAmount; if (opiParam.IsFractionate) { nAmount = nAmount + opiParam.FlatAmount * fractionate + (basicSalary * opiParam.PercentOfBasic * fractionate) / 100.0; } else { nAmount = nAmount + ((basicSalary * opiParam.PercentOfBasic / 100.0) * fractionate); } else { nAmount = opiParam.FlatAmount * fractionate; } if (opiParam.NotApplicable.Exists(delegate (OpiParameterIndividual adEmployee) { return adEmployee.EmployeeId == employee.ID; })) { nAmount = 0; } else { OpiParameterIndividual adEmp = opiParam.Exception.Find(delegate (OpiParameterIndividual adEmployee) { return adEmployee.EmployeeId == employee.ID; }); if (adEmp != null) { switch (adEmp.ValueType) { case EnumValueType.Amount: nAmount = adEmp.Value; break; case EnumValueType.Other: if (opiParam.PercentOfBasic > 0) nAmount = basicSalary * adEmp.Value / 100.0 * fractionate; //+ this.FlatAmount; // nAmount = basicSalary * adEmp.Value / 100.0 + this.FlatAmount; else if (opiParam.PercentOfGross > 0) { nAmount = grossSalary * adEmp.Value / 100.0 * fractionate; //+ this.FlatAmount; //nAmount = grossSalary * adEmp.Value / 100.0 + this.FlatAmount; } break; } } } //if (this.IsFlatAmount == true) nAmount = PayrollPayrollGlobalFunctions.Round(nAmount * fractionate); //nAmount = PayrollPayrollGlobalFunctions.Round(nAmount * fractionate); break; case EnumEntitleType.Individual: throw new ServiceException("You have choosed the wrong overloaded function to calculate" + " Individual Allowance"); } if (opiParam.MinAmount != 0 || opiParam.MaxAmount != 0) { if (nAmount < opiParam.MinAmount) { nAmount = opiParam.MinAmount; } if (nAmount > opiParam.MaxAmount) { nAmount = opiParam.MaxAmount; } } return nAmount; } } #endregion }