using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using HRM.BO; using HRM.DA; using NPOI.OpenXmlFormats.Dml.Diagram; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using static iTextSharp.text.pdf.AcroFields; namespace HRM.Service { #region LeaveEncashmentService [Serializable] public class LeaveEncashmentService : ServiceTemplate, ILeaveEncashmentService { #region Private functions and declaration public LeaveEncashmentService() { } private void MapObject(LeaveEncashment oLeaveEncashment, DataReader oReader) { base.SetObjectID(oLeaveEncashment, oReader.GetInt32("LeaveEncashmentID").Value); oLeaveEncashment.LeaveYearID = oReader.GetInt32("LeaveYearID").Value; oLeaveEncashment.EmployeeID = oReader.GetInt32("EmployeeID").Value; oLeaveEncashment.LeaveID = oReader.GetInt32("LeaveID").Value; oLeaveEncashment.TaxParamID = oReader.GetInt32("TaxParamID").HasValue ? oReader.GetInt32("TaxParamID").Value: 0; oLeaveEncashment.EncashmentDays = oReader.GetDouble("EncashmentDays").Value; oLeaveEncashment.LeaveBalance = oReader.GetDouble("LeaveBalance").Value; oLeaveEncashment.GrossSalary = oReader.GetDouble("GrossSalary").Value; oLeaveEncashment.Amount = oReader.GetDouble("Amount").Value; oLeaveEncashment.TaxAmount = oReader.GetDouble("TaxAmount").Value; oLeaveEncashment.IsFromWeb = oReader.GetBoolean("IsFromWeb").Value; oLeaveEncashment.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value; oLeaveEncashment.CreatedDate = oReader.GetDateTime("CreatedDate").Value; oLeaveEncashment.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value; oLeaveEncashment.ModifiedDate = oReader.GetDateTime("ModifiedDate"); oLeaveEncashment.BasicSalary = oReader.GetDouble("BasicSalary").Value; oLeaveEncashment.ESSSubmittedDays = oReader.GetDouble("ESSSubmittedDays").Value; this.SetObjectState(oLeaveEncashment, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { LeaveEncashment oLeaveEncashment = new LeaveEncashment(); MapObject(oLeaveEncashment, oReader); return oLeaveEncashment as T; } LeaveEncashment CreateObject(DataReader oReader) { LeaveEncashment oLeaveEncashment = new LeaveEncashment(); MapObject(oLeaveEncashment, oReader); return oLeaveEncashment; } #endregion #region Service Implementation public int Save(LeaveEncashment oLeaveEncashment) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oLeaveEncashment.IsNew) { int id = tc.GenerateID("LeaveEncashment", "LeaveEncashmentID"); base.SetObjectID(oLeaveEncashment, id); LeaveEncashmentDA.Insert(tc, oLeaveEncashment); } else { LeaveEncashmentDA.Update(tc, oLeaveEncashment); } tc.End(); return oLeaveEncashment.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Save Leave Year: " + e.Message, e); #endregion } } public void Save(List _editedLeaveEncashments) { TransactionContext tc = null; IncomeTaxService osvr = new IncomeTaxService(); try { tc = TransactionContext.Begin(true); foreach (LeaveEncashment oLeaveEncashment in _editedLeaveEncashments) { if (oLeaveEncashment.IsNew) { int id = tc.GenerateID("LeaveEncashment", "LeaveEncashmentID"); base.SetObjectID(oLeaveEncashment, id); LeaveEncashmentDA.Insert(tc, oLeaveEncashment); } else { LeaveEncashmentDA.Update(tc, oLeaveEncashment); } if (oLeaveEncashment.IncomeTaxcoll != null && oLeaveEncashment.IncomeTaxcoll.Count > 0) osvr.Save(tc, oLeaveEncashment.IncomeTaxcoll, EnumIncomeTaxDataFrom.ProcessTempData); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Save Leave Year: " + e.Message, e); #endregion } } public LeaveEncashment Get(int leaveYearID, int employeeID) { LeaveEncashment oLeaveEncashment = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveEncashmentDA.Get(tc, leaveYearID, employeeID)); if (oreader.Read()) { oLeaveEncashment = this.CreateObject(oreader); } oreader.Close(); tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException("Failed to Get Leave Encashment: " + ex.Message, ex); #endregion } return oLeaveEncashment; } public List Get(int leaveYearID) { List oLeaveEncashments = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveEncashmentDA.Get(tc, leaveYearID)); oLeaveEncashments = this.CreateObjects(oreader); oreader.Close(); tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException("Failed to Get Leave Encashment: " + ex.Message, ex); #endregion } return oLeaveEncashments; } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LeaveEncashmentDA.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 } } public DataTable GetData(int nYearID) { DataSet attenDataset = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); attenDataset = LeaveEncashmentDA.GetData(tc, nYearID); tc.End(); return attenDataset.Tables[0]; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public DataTable GetSalaryData(DateTime dt) { DataSet attenDataset = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); attenDataset = LeaveEncashmentDA.GetSalaryData(tc, dt); tc.End(); return attenDataset.Tables[0]; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public DataTable GetSalaryDataExpat(DateTime dt) { DataSet attenDataset = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); attenDataset = LeaveEncashmentDA.GetSalaryDataExpat(tc, dt); tc.End(); return attenDataset.Tables[0]; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Delete(LeaveEncashment leaveencash) { TransactionContext tc = null; IncomeTaxService osvr = new IncomeTaxService(); try { tc = TransactionContext.Begin(true); if (leaveencash.IncomeTaxcoll.Count > 0) { osvr.DeleteEncashTax(tc, leaveencash.IncomeTaxcoll,EnumIncomeTaxDataFrom.ProcessTempData); IncomeTax oTax = leaveencash.IncomeTaxcoll.Where(x => x.Description == "Earned Leave").FirstOrDefault(); if (oTax != null) { leaveencash.IncomeTaxcoll.Remove(oTax); } osvr.Save(tc, leaveencash.IncomeTaxcoll, EnumIncomeTaxDataFrom.ProcessTempData); } LeaveEncashmentDA.Delete(tc, leaveencash.ID); 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 GetByEmpIDs(string empIDs, DateTime FirstDateOfYear, DateTime LastDateOfYear) { #region Cache Header List employees = new List(); #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveEncashmentDA.GetByEmpIDs(tc, empIDs, FirstDateOfYear, LastDateOfYear)); employees = 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 employees; } public void CalculateTax(List oProcessedItems, DateTime nextPayProcess, int taxParaId, int payrollTypeID = 0) { if (taxParaId == null) throw new ServiceException("Current year Tax paramter setup not found."); TaxParameter oparamter = new TaxParameterService().Get(taxParaId); double nTaxAmount = 0; List oEmployees = new EmployeeService().GetAllEmps(); List currentYearTax = new List(); List empIdIntList = new List(); foreach (LeaveEncashment item in oProcessedItems) empIdIntList.Add(item.EmployeeID); string empIdString = string.Join(",", empIdIntList); List currentYearTaxs = new IncomeTaxService().GetByEmployeeIds(empIdString, EnumIncomeTaxDataFrom.ProcessTempData); foreach (LeaveEncashment item in oProcessedItems) { TaxCalculator oCalculator = new TaxCalculator(taxParaId, payrollTypeID); oCalculator.TaxParameter = oparamter; oCalculator.Employee = oEmployees.Find(x => x.ID == item.EmployeeID); //currentYearTax = new IncomeTaxService().Get(item.EmployeeID, EnumIncomeTaxDataFrom.ProcessTempData); currentYearTax = currentYearTaxs.FindAll(x => x.EmployeeID == item.EmployeeID); List oTaxRowItems = new List(); IncomeTax encashItem = new IncomeTaxService().Get(currentYearTax, item.EmployeeID, EnumIncomeTaxItemGroup.Earned_Leave,(int) EnumIncomeTaxItemGroup.Earned_Leave); if (encashItem != null) { oTaxRowItems.Add(TaxRawItem.Create("Earned Leave", encashItem.ThisMonthAmount, enumIncomeTaxItemType.Earned_Leave, (int)EnumIncomeTaxItemGroup.Earned_Leave)); item.TaxParamID = oparamter.ID; encashItem.ThisMonthAmount = item.Amount; } //encashItem.ThisMonthAmount = item.Amount; //if (item.Amount > 0) //{ // List oTaxRowItems = new List(); // //IncomeTax encashItem = IncomeTax.Get(currentYearTax, item.EmployeeID, EnumIncomeTaxItemGroup.Earned_Leave, 100); // oTaxRowItems.Add(TaxRawItem.Create("Earned Leave", item.Amount, enumIncomeTaxItemType.Earned_Leave, // (int)EnumIncomeTaxItemGroup.Earned_Leave)); // nTaxAmount = 0; // item.IncomeTaxcoll = // oCalculator.CalculateEncashAmount(oTaxRowItems, ref nTaxAmount, nextPayProcess); // item.TaxAmount = nTaxAmount; // item.TaxParamID = oparamter.ID; //} } } #endregion public List rmgLeaveEncashmentCalculation(int month, string syears, string empids, int payrolltypeid, int leaveid) { List returnItems = new List(); int SelectedMonthValue = month; string[] years = syears.Split(','); PayrollType opType = new PayrollTypeService().Get(payrolltypeid); int nYear = DateTime.Now.Year; nYear = Convert.ToInt32( years[0]); //foreach (SearchEmployee item in fEmpSearch.SelectedEmployees) //{ // if (item.JoiningDate.Month == cboMonth.SelectedIndex + 1 // && years.Where(x => x.ToString() == item.JoiningDate.Year.ToString()).Any() // && Years(item.JoiningDate, DateTime.Today) >= 2) // { // oSearchEmployees.Add(item); // } //} DateTime SelectedMonth = new DateTime(nYear, SelectedMonthValue, 1).LastDateOfMonth(); string empIDs = empids;//oSearchEmployees.Aggregate(new StringBuilder(), (sb, emp) => sb.AppendFormat("{0},", emp.EmployeeID), sb => sb.ToString().Trim(',')); LeaveYear oleaveYear = new LeaveYearService().GetCurrentYear(payrolltypeid); if (oleaveYear.StartDate.Year != DateTime.Today.Year) { throw new Exception(@"Leave year does not match with current year."); } List _Employees = new EmployeeService().GetByEmpIDs(empIDs); List oEmps = new List(); foreach (Employee item in _Employees) { if (item.JoiningDate.Month == month && years.Where(x => x.ToString().Trim() == item.JoiningDate.Year.ToString()).Any() && Years(item.JoiningDate, DateTime.Today) >= 2) { oEmps.Add(item); } } if(oEmps.Count ==0) { throw new Exception("selected employees are not completed 2 years service length."); } _Employees = oEmps; DateTime FirstDateofYear = GlobalFunctions.FirstDateOfMonth( SelectedMonth); DateTime LastDateOfYear = GlobalFunctions.LastDateOfMonth( SelectedMonth); List les = new LeaveEncashmentService().GetByEmpIDs(empIDs, FirstDateofYear, LastDateOfYear); string empIDLE = les.Aggregate(new StringBuilder(), (sb, emp) => sb.AppendFormat("{0},", emp.EmployeeID), sb => sb.ToString().Trim(',')); List selectedMonthSalaris = new List(); if (!string.IsNullOrEmpty(empIDs)) { // SelectedMonth = SelectedMonth.AddMonths(-1).LastDateOfMonth(); DateTime oSalaryMonth = opType.LastPayProcessDate; selectedMonthSalaris =new SalaryMonthlyService().Get(empIDs, oSalaryMonth, payrolltypeid); } if (!string.IsNullOrEmpty(empIDLE)) { List result = empIDLE.Split(',').ToList(); foreach (string item in result) { Employee emp = _Employees.Where(x => x.ID.ToString() == item).FirstOrDefault(); if (emp != null) { _Employees.Remove(emp); } } } int rowindex = 0; double firstYearPresentDays = 0; double secondYearPresentDays = 0; double firstYearAbsentDays = 0; double firstYearHoliDays = 0; double firstYearLeaveBalance = 0; double secondYearLeaveBalance = 0; double secondYearLeaveAvailed = 0; List oDAP = new List(); List oLE = new List(); double eligibleDays = 0; double remainingLeaveDays = 0; // DataTable attnStatusCount = new DailyAttnProcessService().GetStatusCount(FirstYearStartDate, FirstYearEndDate) foreach (Employee item in _Employees) { SalaryMonthly previousMonthSalary = selectedMonthSalaris.Where(x => x.EmployeeID == item.ID).FirstOrDefault(); if (previousMonthSalary == null) { continue; } DateTime JoiningDateBefore2YearsFromCurrentYear = new DateTime(SelectedMonth.Year, SelectedMonth.Month, item.JoiningDate.Day); //new DateTime(SelectedMonth.Year, SelectedMonth.Month, item.JoiningDate.Day).AddYears(-2);// new DateTime(DateTime.Today.Year- 2, item.JoiningDate.Month, item.JoiningDate.Day);// SelectedMonth.FirstDateOfMonth().AddYears(-2); DateTime FirstYearStartDate = JoiningDateBefore2YearsFromCurrentYear; DateTime FirstYearEndDate = JoiningDateBefore2YearsFromCurrentYear.AddYears(1).AddDays(-1); DateTime LastYearStartDate = JoiningDateBefore2YearsFromCurrentYear.AddYears(1); DateTime LastYearEndDate = JoiningDateBefore2YearsFromCurrentYear.AddYears(2).AddDays(-1); firstYearPresentDays = new DailyAttnProcessService().GetStatusCount(FirstYearStartDate, FirstYearEndDate, EnumAttendanceType.Present, payrolltypeid, item.ID, null ); firstYearLeaveBalance = firstYearPresentDays > 0 ? firstYearPresentDays / 18 : 0; secondYearLeaveBalance = secondYearPresentDays > 0 ? secondYearPresentDays / 18 : 0; secondYearLeaveAvailed =new LeaveEntryService().GetAmountOnFromDate(item.ID,leaveid, EnumLeaveStatus.Approved, LastYearStartDate, LastYearEndDate); firstYearLeaveBalance = Math.Round(firstYearLeaveBalance); eligibleDays = firstYearLeaveBalance - secondYearLeaveAvailed; if (eligibleDays <= 0) { continue; } eligibleDays = Math.Round(eligibleDays, 2); LeaveEncashment editedEncash = new LeaveEncashment(); editedEncash.Amount = eligibleDays * GlobalFunctions.Round((previousMonthSalary.ThisMonthGross / 30)); editedEncash.EncashmentDays = eligibleDays; editedEncash.EmployeeID = item.ID; editedEncash.LeaveYearID = oleaveYear.ID - 2; editedEncash.GrossSalary = previousMonthSalary.ThisMonthGross; editedEncash.presentDays = firstYearPresentDays; editedEncash.holiDays = firstYearHoliDays; editedEncash.LeaveBalance = firstYearLeaveBalance; editedEncash.LeaveID = leaveid; editedEncash.IsFromWeb = false; editedEncash.enjoyedLeave = secondYearLeaveAvailed; editedEncash.encashMonth = SelectedMonth.Date; editedEncash.encashmentFromDate = FirstYearStartDate; editedEncash.encashmentToDate = FirstYearEndDate; editedEncash.employeeNo = item.EmployeeNo; editedEncash.employeeName = item.Name; returnItems.Add(editedEncash); rowindex++; } return returnItems; } //public void calculateTaxForLeaveEncashment(List oProcessedItems, CurrentUser currentUser, int taxParaId, int payrollTypeId) //{ // List oCurrentYearTaxes = new List(); // double _nTaxAmount; // TaxParameter taxParam = new TaxParameterService().Get(taxParaId); // List oEmployees = new EmployeeService().GetAllEmps(); // oCurrentYearTaxes = new IncomeTaxService().Get(EnumIncomeTaxDataFrom.ProcessTempData, payrollTypeId); // foreach (LeaveEncashment le in oProcessedItems) // { // _nTaxAmount = 0; // TaxRawItem taxRawItem = new TaxRawItem(); // taxRawItem = TaxRawItem.Create("Leave Encash", le.Amount, enumIncomeTaxItemType.Earned_Leave, 27); // List otaxRawItems = new List(); // otaxRawItems.Add(taxRawItem); // TaxCalculator otaxCal = new TaxCalculator(); // otaxCal.TaxParameter = taxParam; // otaxCal.Employee = oEmployees.Find(x => x.ID == le.EmployeeID); // le.IncomeTaxcoll = otaxCal.CalculateEncashTax(otaxRawItems, oCurrentYearTaxes, currentUser, ref _nTaxAmount); // le.TaxAmount = _nTaxAmount; // le.Amount -= le.TaxAmount; // } //} int Years(DateTime start, DateTime end) { return (end.Year - start.Year - 1) + (((end.Month > start.Month) || ((end.Month == start.Month) && (end.Day >= start.Day))) ? 1 : 0); } } #endregion }