EchoTex_Payroll/HRM.DA/Service/Leave/LeaveEncashmentService.cs

611 lines
24 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
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<T>(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<LeaveEncashment> _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<LeaveEncashment>(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<LeaveEncashment> Get(int leaveYearID)
{
List<LeaveEncashment> oLeaveEncashments = new List<LeaveEncashment>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveEncashmentDA.Get(tc, leaveYearID));
oLeaveEncashments = this.CreateObjects<LeaveEncashment>(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<LeaveEncashment> GetByEmpIDs(string empIDs, DateTime FirstDateOfYear, DateTime LastDateOfYear)
{
#region Cache Header
List<LeaveEncashment> employees = new List<LeaveEncashment>();
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveEncashmentDA.GetByEmpIDs(tc, empIDs, FirstDateOfYear, LastDateOfYear));
employees = this.CreateObjects<LeaveEncashment>(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<LeaveEncashment> 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<Employee> oEmployees = new EmployeeService().GetAllEmps();
List<IncomeTax> currentYearTax = new List<IncomeTax>();
List<int> empIdIntList = new List<int>();
foreach (LeaveEncashment item in oProcessedItems)
empIdIntList.Add(item.EmployeeID);
string empIdString = string.Join(",", empIdIntList);
List<IncomeTax> 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<TaxRawItem> oTaxRowItems = new List<TaxRawItem>();
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<TaxRawItem> oTaxRowItems = new List<TaxRawItem>();
// //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<LeaveEncashment> rmgLeaveEncashmentCalculation(int month, string syears,
string empids,
int payrolltypeid, int leaveid)
{
List<LeaveEncashment> returnItems = new List<LeaveEncashment>();
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<Employee> _Employees = new EmployeeService().GetByEmpIDs(empIDs);
List<Employee> oEmps = new List<Employee>();
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<LeaveEncashment> 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<SalaryMonthly> selectedMonthSalaris = new List<SalaryMonthly>();
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<string> 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<DailyAttnProcess> oDAP = new List<DailyAttnProcess>();
List<LeaveEntry> oLE = new List<LeaveEntry>();
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<LeaveEncashment> oProcessedItems, CurrentUser currentUser, int taxParaId, int payrollTypeId)
//{
// List<IncomeTax> oCurrentYearTaxes = new List<IncomeTax>();
// double _nTaxAmount;
// TaxParameter taxParam = new TaxParameterService().Get(taxParaId);
// List<Employee> 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<TaxRawItem> otaxRawItems = new List<TaxRawItem>();
// 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
}