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

583 lines
18 KiB
C#
Raw Permalink Normal View History

2024-10-14 10:01:49 +06:00
using HRM.BO;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
namespace HRM.DA
{
#region Leave Year Service
public class LeaveYearService : ServiceTemplate, ILeaveYearService
{
#region Private functions and declaration
public LeaveYearService()
{
}
private void MapObject(LeaveYear oLeaveYear, DataReader oReader)
{
base.SetObjectID(oLeaveYear, oReader.GetInt32("LeaveYearID").Value);
oLeaveYear.Name = oReader.GetString("Name");
oLeaveYear.StartDate = oReader.GetDateTime("StartDate").Value;
oLeaveYear.EndDate = oReader.GetDateTime("EndDate").Value;
oLeaveYear.IsCurrent = oReader.GetBoolean("IsCurrent").Value;
oLeaveYear.IsEnded = oReader.GetBoolean("IsEnded").Value;
oLeaveYear.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oLeaveYear.Sequence = oReader.GetInt32("SequenceNo").Value;
oLeaveYear.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value;
oLeaveYear.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oLeaveYear.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value;
oLeaveYear.ModifiedDate = oReader.GetDateTime("ModifiedDate");
oLeaveYear.PayrollTypeId = oReader.GetInt32("PAYROLLTYPEID").Value;
var eStartDate = oReader.GetDateTime("EncashmentStartDate", true, DateTime.MinValue);
if (eStartDate != DateTime.MinValue)
oLeaveYear.EncashmentStartDate = oReader.GetDateTime("EncashmentStartDate");
else oLeaveYear.EncashmentStartDate = null;
var eEndDate = oReader.GetDateTime("EncashmentEndDate", true, DateTime.MinValue);
if (eEndDate != DateTime.MinValue)
oLeaveYear.EncashmentEndDate = oReader.GetDateTime("EncashmentEndDate");
else oLeaveYear.EncashmentEndDate = null;
oLeaveYear.IsEncashmentActive = oReader.GetBoolean("IsEncashmentActive",true, false);
this.SetObjectState(oLeaveYear, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
LeaveYear oLeaveYear = new LeaveYear();
MapObject(oLeaveYear, oReader);
return oLeaveYear as T;
}
LeaveYear CreateObject(DataReader oReader)
{
LeaveYear oLeaveYear = new LeaveYear();
MapObject(oLeaveYear, oReader);
return oLeaveYear;
}
#endregion
#region Service Implementation
public LeaveYear Get(int id)
{
LeaveYear oLeaveYear = new LeaveYear();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveYearDA.Get(tc, id));
if (oreader.Read())
{
oLeaveYear = this.CreateObject<LeaveYear>(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 Year: " + ex.Message, ex);
#endregion
}
return oLeaveYear;
}
public List<LeaveYear> Get()
{
List<LeaveYear> oLeaveYears = new List<LeaveYear>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveYearDA.Get(tc));
oLeaveYears = this.CreateObjects<LeaveYear>(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 oLeaveYears;
}
public List<LeaveYear> GetByPayrollTypeId(int id)
{
List<LeaveYear> oLeaveYears = new List<LeaveYear>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveYearDA.GetByPayrollType(tc, id));
oLeaveYears = this.CreateObjects<LeaveYear>(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 oLeaveYears;
}
public List<LeaveYear> Get(EnumStatus status)
{
List<LeaveYear> leaveYears = new List<LeaveYear>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveYearDA.Get(tc, status));
leaveYears = this.CreateObjects<LeaveYear>(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 leaveYears;
}
public List<LeaveYear> Get(EnumStatus status, int payrollTypeId)
{
List<LeaveYear> leaveYears = new List<LeaveYear>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveYearDA.Get(tc, status, payrollTypeId));
leaveYears = this.CreateObjects<LeaveYear>(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 leaveYears;
}
public LeaveYear GetIDByName(string sLeaveYear)
{
LeaveYear oLeaveYear = new LeaveYear();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveYearDA.GetIDByName(tc, sLeaveYear));
if (oreader.Read())
{
oLeaveYear = this.CreateObject<LeaveYear>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave: " + e.Message, e);
#endregion
}
return oLeaveYear;
}
public LeaveYear GetCurrentYear(int payrolltypeid)
{
LeaveYear oLeaveYear = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveYearDA.GetCurrentYear(tc, payrolltypeid));
if (oreader.Read())
{
oLeaveYear = this.CreateObject<LeaveYear>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Year: " + e.Message, e);
#endregion
}
return oLeaveYear;
}
public LeaveYear GetCurrentYear(string year)
{
LeaveYear oLeaveYear = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveYearDA.GetCurrentYear(tc, year));
if (oreader.Read())
{
oLeaveYear = this.CreateObject<LeaveYear>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Year: " + e.Message, e);
#endregion
}
return oLeaveYear;
}
public LeaveYear GetLastYear(int payrolltypeid)
{
try
{
List<LeaveYear> leaveYears = Get(EnumStatus.Active, payrolltypeid);
LeaveYear currentYear = leaveYears.FirstOrDefault(x => x.IsCurrent == true);
if(currentYear ==null)
{
throw new Exception("Current year didn't find");
}
foreach (LeaveYear oItem in leaveYears)
{
if (oItem.EndDate.AddDays(1) == currentYear.StartDate)
{
return oItem;
}
}
}
catch (Exception e)
{
#region Handle Exception
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Year: " + e.Message, e);
#endregion
}
return null;
}
public LeaveYear LastLeaveYear(LeaveYear oCurrYear)
{
try
{
List<LeaveYear> leaveYears = Get();
foreach (LeaveYear oItem in leaveYears)
{
if (oItem.EndDate.AddDays(1) == oCurrYear.StartDate)
{
return oItem;
}
}
}
catch (Exception e)
{
#region Handle Exception
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Year: " + e.Message, e);
#endregion
}
return null;
}
public LeaveYear GetLastLeaveYear(LeaveYear oCurrYear, int payrollTypeId)
{
TransactionContext tc = null;
LeaveYear lasteLeaveYear = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveYearDA.GetLastLeaveYear(tc, oCurrYear, payrollTypeId));
if (oreader.Read())
{
lasteLeaveYear = this.CreateObject<LeaveYear>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Year: " + e.Message, e);
#endregion
}
return lasteLeaveYear;
}
public void UpdateYearEndValue(TransactionContext tc, LeaveYear leaveYear)
{
try
{
LeaveYearDA.UpdateYearEndStatus(tc, leaveYear);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Insert Leave Year: " + e.Message, e);
#endregion
}
}
public void UpdateCurrYearStatus(TransactionContext tc, LeaveYear leaveYear)
{
try
{
LeaveYearDA.UpdateCurrYearStatus(tc, leaveYear);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to update current year status; reason:" + e.Message, e);
#endregion
}
}
internal void UpdateLeaveBalance(TransactionContext tc, int employeeID, int payrollTypeID, int currentPayrollid)
{
try
{
List<LeaveYear> olFirstPayYears = new List<LeaveYear>();
LeaveYear lFirstPayPrev = new LeaveYear();
List<LeaveYear> olSecondPayYears = new List<LeaveYear>();
LeaveYear lSecPayPrev = new LeaveYear();
DataReader dr = new DataReader(LeaveYearDA.Get(tc, currentPayrollid));
olFirstPayYears = this.CreateObjects<LeaveYear>(dr);
dr.Close();
DataReader dr1 = new DataReader(LeaveYearDA.Get(tc, payrollTypeID));
olSecondPayYears = this.CreateObjects<LeaveYear>(dr1);
dr1.Close();
LeaveYear oFirstL = (from p in olFirstPayYears where p.IsCurrent == true select p).FirstOrDefault();
LeaveYear oSecondL = (from p in olSecondPayYears where p.IsCurrent == true select p).FirstOrDefault();
if (oFirstL != null && oSecondL != null)
{
foreach (LeaveYear oItem in olFirstPayYears)
{
if (oItem.EndDate.AddDays(1) == oFirstL.StartDate)
{
lFirstPayPrev = oItem;
break;
}
}
foreach (LeaveYear oItem in olSecondPayYears)
{
if (oItem.EndDate.AddDays(1) == oSecondL.StartDate)
{
lSecPayPrev = oItem;
break;
}
}
LeaveYearDA.UpdateLeaveStatusID(tc, lFirstPayPrev, lSecPayPrev, oFirstL, oSecondL, employeeID, payrollTypeID, currentPayrollid);
}
}
catch (Exception ex)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(ex);
throw new ServiceException("Failed to Update Leave Transfer Information: " + ex.Message, ex);
#endregion
}
}
public int Save(LeaveYear oLeaveYear)
{
TransactionContext tc = null;
try
{
if(oLeaveYear.Name == "")
{
oLeaveYear.Name = oLeaveYear.StartDate.ToString("dd MMM yy") + "-" + oLeaveYear.EndDate.ToString("dd MMM yy");
}
tc = TransactionContext.Begin(true);
if (oLeaveYear.IsNew)
{
int id = tc.GenerateID("LeaveYear", "LeaveYearID");
base.SetObjectID(oLeaveYear, id);
int seqNo = tc.GenerateID("LeaveYear", "SequenceNO");
oLeaveYear.Sequence = seqNo;
LeaveYearDA.Insert(tc, oLeaveYear);
}
else
{
LeaveYearDA.Update(tc, oLeaveYear);
}
tc.End();
return oLeaveYear.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Insert Leave Year: " + e.Message, e);
#endregion
}
}
public void Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveYearDA.Delete(tc, id);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Leave Year: " + e.Message, e);
#endregion
}
}
public int CountCurrentYear()
{
int res = 0;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
res = LeaveYearDA.CountCurrentYear(tc);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Leave Process Information: " + e.Message, e);
#endregion
}
return res;
}
#endregion
}
#endregion
}