EchoTex_Payroll/HRM.DA/Service/Leave/LeaveExceptionService.cs
2024-10-14 10:01:49 +06:00

225 lines
7.3 KiB
C#

using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using HRM.BO;
namespace HRM.DA
{
public class LeaveExceptionService : ServiceTemplate, ILeaveExceptionService
{
#region Private functions and declaration
public LeaveExceptionService()
{
}
private void MapObject(LeaveException oLeaveException, DataReader oReader)
{
//base.SetObjectID(oLeaveException, oReader.GetInt32("LeaveExceptionID").Value);
oLeaveException.EmployeeID = oReader.GetInt32("EmployeeID").Value;
oLeaveException.StartDate = oReader.GetDateTime("StartDate").Value;
oLeaveException.EndDate = oReader.GetDateTime("EndDate").Value;
oLeaveException.LeaveID = oReader.GetInt16("LeaveID").Value;
oLeaveException.OpeningBalance = oReader.GetDouble("OpeningBalance").Value;
oLeaveException.MaxDays = oReader.GetDouble("MaxDays").Value;
oLeaveException.CFDays = oReader.GetDouble("CFDays").Value;
this.SetObjectState(oLeaveException, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
LeaveException oLeave = new LeaveException();
MapObject(oLeave, oReader);
return oLeave as T;
}
private LeaveException CreateObject(DataReader oReader)
{
LeaveException oLeave = new LeaveException();
MapObject(oLeave, oReader);
return oLeave;
}
#endregion
public List<LeaveException> Get()
{
List<Employee> employees = new List<Employee>();
employees = new EmployeeService().GetAllEmps();
List<Leave> leaves = new LeaveService().GetAll();
List<LeaveException> oLeaveExceptions = new List<LeaveException>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveExceptionDA.Get(tc));
oLeaveExceptions = this.CreateObjects<LeaveException>(dr);
dr.Close();
tc.End();
foreach (LeaveException item in oLeaveExceptions)
{
Employee employee = employees.Find(x => x.ID == item.EmployeeID);
item.EmployeeNo = employee != null ? employee.EmployeeNo : string.Empty;
item.EmployeeName = employee != null ? employee.Name : string.Empty;
Leave leave = leaves.Find(x => x.ID == item.LeaveID);
item.LeaveName = leave != null ? leave.Description : string.Empty;
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oLeaveExceptions;
}
public List<LeaveException> Get(DateTime dFrom, DateTime dTo)
{
List<LeaveException> oLeaves = new List<LeaveException>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveExceptionDA.Get2(tc, dFrom, dTo));
oLeaves = this.CreateObjects<LeaveException>(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 oLeaves;
}
public List<LeaveException> GetByEmpID(int nLeaveID, int nEmpID)
{
List<Employee> employees = new List<Employee>();
employees = new EmployeeService().GetAllEmps();
List<Leave> leaves = new LeaveService().GetAll();
List<LeaveException> oLeaveExceptions = new List<LeaveException>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(LeaveExceptionDA.GetByEmpID(tc, nLeaveID, nEmpID));
oLeaveExceptions = this.CreateObjects<LeaveException>(dr);
dr.Close();
tc.End();
foreach (LeaveException item in oLeaveExceptions)
{
Employee employee = employees.Find(x => x.ID == item.EmployeeID);
item.EmployeeNo = employee != null ? employee.EmployeeNo : string.Empty;
item.EmployeeName = employee != null ? employee.Name : string.Empty;
Leave leave = leaves.Find(x => x.ID == item.LeaveID);
item.LeaveName = leave != null ? leave.Description : string.Empty;
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oLeaveExceptions;
}
public int Save(LeaveException oLeave)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveExceptionDA.Delete(tc, oLeave.EmployeeID);
LeaveExceptionDA.Insert(tc, oLeave);
tc.End();
return oLeave.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
//throw new ServiceException("Failed to Get Leave", e);
throw new Exception("Failed to Insert Leave. Because " + e.Message, e);
#endregion
}
}
public void DeleteItem(int nID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveExceptionDA.DeleteItem(tc, nID);
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 DeleteByEmpID(int empID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveExceptionDA.Delete(tc, empID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
}
}