CEL_Payroll/Payroll.Service/Leave/Service/LeaveProcessService.cs
2024-09-17 14:30:13 +06:00

804 lines
29 KiB
C#

using System;
using System.Data;
using System.Linq;
using Ease.CoreV35;
using Ease.CoreV35.Model;
using Ease.CoreV35.DataAccess;
using System.Collections.Generic;
using Payroll.BO;
using Ease.CoreV35.Caching;
namespace Payroll.Service
{
#region LeaveParameter Service
[Serializable]
public class LeaveProcessService : ServiceTemplate, ILeaveProcessService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(LeaveProcess));
#endregion
public LeaveProcessService() { }
#region Leave Process
private void MapObject(LeaveProcess oLeaveProcess, DataReader oReader)
{
base.SetObjectID(oLeaveProcess, oReader.GetID("ProcessId"));
oLeaveProcess.LeaveYearID = oReader.GetInt32("LEAVEYEARID").Value;
oLeaveProcess.ProcessDate = oReader.GetDateTime("ProcessDate").Value;
oLeaveProcess.IsYearEnd = oReader.GetBoolean("IsYearEnd").Value;
oLeaveProcess.ProcessYearDescription = oReader.GetString("ProcessYearDesc");
oLeaveProcess.PayrollTypeID = oReader.GetInt32("PayrollTypeID").Value;
this.SetObjectState(oLeaveProcess, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
LeaveProcess oLeaveProcess = new LeaveProcess();
MapObject(oLeaveProcess, oReader);
return oLeaveProcess as T;
}
private LeaveProcess CreateObject(DataReader oReader)
{
LeaveProcess oLeaveProcess = new LeaveProcess();
MapObject(oLeaveProcess, oReader);
return oLeaveProcess;
}
#region Service implementation
public LeaveProcess Get(ID id)
{
LeaveProcess oLeaveProcess = new LeaveProcess();
#region Cache Header
oLeaveProcess = (LeaveProcess)_cache["Get", id.Integer];
if (oLeaveProcess != null)
return oLeaveProcess;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.Get(tc, id.Integer));
if (oreader.Read())
{
oLeaveProcess = CreateObject(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 Process:" + e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(oLeaveProcess, "Get", id);
#endregion
return oLeaveProcess;
}
public LeaveProcess Get(int nProcessYear)
{
LeaveProcess oLeaveProcess = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetByYearID(tc, nProcessYear));
if (oreader.Read())
{
oLeaveProcess = CreateObject(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 Process : " + e.Message, e);
#endregion
}
return oLeaveProcess;
}
public LeaveProcess Get(int nLeaveProcessYear, int nPayrollTypeid)
{
LeaveProcess oLeaveProcess = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetByYearANDPayrollID(tc, nLeaveProcessYear, nPayrollTypeid));
if (oreader.Read())
{
oLeaveProcess = CreateObject(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 Process : " + e.Message, e);
#endregion
}
return oLeaveProcess;
}
public ObjectsTemplate<LeaveProcess> Get()
{
#region Cache Header
ObjectsTemplate<LeaveProcess> oLeaveProcesss = _cache["Get"] as ObjectsTemplate<LeaveProcess>;
if (oLeaveProcesss != null)
return oLeaveProcesss;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.Get(tc));
oLeaveProcesss = this.CreateObjects<LeaveProcess>(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 Process: " + e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(oLeaveProcesss, "Get");
#endregion
return oLeaveProcesss;
}
public LeaveProcess GetLastProcess()
{
LeaveProcess oLeaveProcess = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetLastProcess(tc));
if (oreader.Read())
{
oLeaveProcess = CreateObject(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 Process: " + e.Message, e);
#endregion
}
return oLeaveProcess;
}
public ID Save(LeaveProcess oLeaveProcess)
{
LeaveYearService oLYearService = new LeaveYearService();
LeaveProcessService oEmpService = new LeaveProcessService();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByProcessYearANDPayrollType(tc, oLeaveProcess.LeaveYearID, oLeaveProcess.PayrollTypeID);
int id = tc.GenerateID("LeaveProcess", "ProcessID");
base.SetObjectID(oLeaveProcess, ID.FromInteger(id));
LeaveProcessDA.Insert(tc, oLeaveProcess);
oEmpService.SaveAtProcessing(false, oLeaveProcess.ID.Integer, oLeaveProcess.EmpLeaveStatuss, tc);
oLeaveProcess.LeaveYear.IsCurrent = false;
oLeaveProcess.LeaveYear.IsEnded = true;
oLYearService.UpdateCurrYearStatus(tc, oLeaveProcess.LeaveYear);
oLYearService.UpdateYearEndValue(tc, oLeaveProcess.LeaveYear);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oLeaveProcess.ID;
}
public void Delete(ID id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.Delete(tc, id.Integer);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Leave Process: " + e.Message, e);
#endregion
}
}
public void UpadteLeaveYearStatus(LeaveProcess oLeaveProcess)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveYearService oLYearService = new LeaveYearService();
oLeaveProcess.LeaveYear.IsCurrent = true;
oLYearService.UpdateCurrYearStatus(tc, oLeaveProcess.LeaveYear);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Leave Process: " + e.Message, e);
#endregion
}
}
public void Delete(int nProcessYear)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByProcessYear(tc, nProcessYear);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Leave Process: " + e.Message, e);
#endregion
}
}
public void Delete(int nProcessYear, int nPayrollTypeID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByProcessYearANDPayrollType(tc, nProcessYear, nPayrollTypeID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Leave Process: " + e.Message, e);
#endregion
}
}
public bool IsProcessed(int nProcessYear)
{
bool res = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
res = LeaveProcessDA.IsProcessed(tc, nProcessYear);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return res;
}
public void DoYearEnd(LeaveProcess oProcess)
{
TransactionContext tc = null;
try
{
LeaveYear olYear = oProcess.LeaveYear;
olYear.IsEnded = true;
oProcess.IsYearEnd = true;
tc = TransactionContext.Begin(true);
LeaveProcessDA.DoYearEnd(tc, oProcess);
LeaveYearService oLS = new LeaveYearService();
oLS.UpdateYearEndValue(tc, olYear);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to do Year End: reason:" + e.Message, e);
#endregion
}
}
#endregion
#endregion
#region Leave Process Detail
#region Private functions and declaration
private void MapEmpLeaveStatusObject(EmpLeaveStatus oEmpLeaveStatus, DataReader oReader)
{
base.SetObjectID(oEmpLeaveStatus, oReader.GetID("TranId"));
oEmpLeaveStatus.ProcessId = oReader.GetInt32("ProcessId").Value;
oEmpLeaveStatus.EmpId = oReader.GetInt32("EmpId").Value;
oEmpLeaveStatus.LeaveYearID = oReader.GetInt32("LEAVEYEARID").Value;
oEmpLeaveStatus.LeaveId = oReader.GetInt32("LeaveId").Value;
//oEmpLeaveStatus.GradeId = oReader.GetInt32("GradeId");
//oEmpLeaveStatus.ParamId = oReader.GetInt32("ParamId");
oEmpLeaveStatus.CarryFromPrvYear = oReader.GetDouble("FORBENIFITEDYEAR").Value;
oEmpLeaveStatus.CFDays = oReader.GetDouble("CFDays").Value;
oEmpLeaveStatus.EncashDays = oReader.GetDouble("EncashDays").Value;
oEmpLeaveStatus.EncashAmount = oReader.GetDouble("EncashAmount").Value;
oEmpLeaveStatus.NormalLeaveDays = oReader.GetDouble("NormalLeaveDays").Value;
oEmpLeaveStatus.YearEndBalance = oReader.GetDouble("YearEndBalance").Value;
oEmpLeaveStatus.LeaveAvailed = oReader.GetDouble("LEAVEAVAILED").Value;
oEmpLeaveStatus.OpeningBalance = oReader.GetDouble("OPENINGBALANCE").Value;
oEmpLeaveStatus.ForfitedDays = oReader.GetDouble("FORFITEDDAYS").Value;
this.SetObjectState(oEmpLeaveStatus, Ease.CoreV35.ObjectState.Saved);
}
protected ObjectsTemplate<EmpLeaveStatus> CreateEmpLeaveStatusObjects(DataReader oReader)
{
ObjectsTemplate<EmpLeaveStatus> oEmpLeaveStatuss = new ObjectsTemplate<EmpLeaveStatus>();
while (oReader.Read())
{
EmpLeaveStatus oEmpLeaveStatus = new EmpLeaveStatus();
MapEmpLeaveStatusObject(oEmpLeaveStatus, oReader);
oEmpLeaveStatuss.Add(oEmpLeaveStatus);
}
return oEmpLeaveStatuss;
}
private EmpLeaveStatus CreateEmpLeaveStatusObject(DataReader oReader)
{
EmpLeaveStatus oEmpLeaveStatus = new EmpLeaveStatus();
MapEmpLeaveStatusObject(oEmpLeaveStatus, oReader);
return oEmpLeaveStatus;
}
#endregion
#region Service implementation
public EmpLeaveStatus GetProcessDetail(ID id)
{
EmpLeaveStatus oEmpLeaveStatus = new EmpLeaveStatus();
#region Cache Header
oEmpLeaveStatus = (EmpLeaveStatus)_cache["Get", id.Integer];
if (oEmpLeaveStatus != null)
return oEmpLeaveStatus;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetProcessDetail(tc, id.Integer));
if (oreader.Read())
{
oEmpLeaveStatus = this.CreateEmpLeaveStatusObject(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 Employee Leave Status: " + e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(oEmpLeaveStatus, "Get", id.Integer);
#endregion
return oEmpLeaveStatus;
}
public void UpdateEncashAmount(ObjectsTemplate<EmpLeaveStatus> _oEmpLeaveStatus)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
foreach (EmpLeaveStatus oStatus in _oEmpLeaveStatus)
{
LeaveProcessDA.UpdateEncashAmount(tc, oStatus);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Employee Leave Status: " + e.Message, e);
#endregion
}
}
public EmpLeaveStatus GetStatus(int empid, int leaveID, int leaveYearID)
{
EmpLeaveStatus oEmpLeaveStatus = new EmpLeaveStatus();
#region Cache Header
oEmpLeaveStatus = (EmpLeaveStatus)_cache["Get", empid, leaveID, leaveYearID];
if (oEmpLeaveStatus != null)
return oEmpLeaveStatus;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetStatus(tc, empid, leaveID, leaveYearID));
if (oreader.Read())
{
oEmpLeaveStatus = this.CreateEmpLeaveStatusObject(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 Employee Leave Status: " + e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(oEmpLeaveStatus, "Get", empid, leaveID, leaveYearID);
#endregion
return oEmpLeaveStatus;
}
public EmpLeaveStatus GetByYear(int leaveyearID, int LeaveId, int EmpId)
{
EmpLeaveStatus oEmpLeaveStatus = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(LeaveProcessDA.GetByYear(tc, leaveyearID, LeaveId, EmpId));
if (oreader.Read())
{
oEmpLeaveStatus = this.CreateEmpLeaveStatusObject(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 Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatus;
}
public ObjectsTemplate<EmpLeaveStatus> GetByYear(int leaveyearID)
{
#region Cache Header
ObjectsTemplate<EmpLeaveStatus> oEmpLeaveStatuss = _cache["Get"] as ObjectsTemplate<EmpLeaveStatus>;
if (oEmpLeaveStatuss != null)
return oEmpLeaveStatuss;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oReader = new DataReader(LeaveProcessDA.GetByYear(tc, leaveyearID));
oEmpLeaveStatuss = this.CreateEmpLeaveStatusObjects(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 Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatuss;
}
public ObjectsTemplate<EmpLeaveStatus> GetProcessDetails(int nProcessId)
{
#region Cache Header
ObjectsTemplate<EmpLeaveStatus> oEmpLeaveStatuss = _cache["Get"] as ObjectsTemplate<EmpLeaveStatus>;
if (oEmpLeaveStatuss != null)
return oEmpLeaveStatuss;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oReader = new DataReader(LeaveProcessDA.GetByProcessId(tc, nProcessId));
oEmpLeaveStatuss = this.CreateEmpLeaveStatusObjects(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 Employee Leave Status: " + e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(oEmpLeaveStatuss, "Get", nProcessId);
#endregion
return oEmpLeaveStatuss;
}
public ObjectsTemplate<EmpLeaveStatus> GetAllStatus(int empId)
{
#region Cache Header
ObjectsTemplate<EmpLeaveStatus> oEmpLeaveStatuss = _cache["Get"] as ObjectsTemplate<EmpLeaveStatus>;
if (oEmpLeaveStatuss != null)
return oEmpLeaveStatuss;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oReader = new DataReader(LeaveProcessDA.GetAllStatus(tc, empId));
oEmpLeaveStatuss = this.CreateEmpLeaveStatusObjects(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 Employee Leave Status: " + e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(oEmpLeaveStatuss, "Get", empId);
#endregion
return oEmpLeaveStatuss;
}
public ID Save(EmpLeaveStatus oEmpLeaveStatus)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oEmpLeaveStatus.IsNew)
{
this.SetObjectID(oEmpLeaveStatus, ID.FromInteger(LeaveProcessDA.GetNewDetailID(tc)));
LeaveProcessDA.Insert(tc, oEmpLeaveStatus);
}
else
LeaveProcessDA.Update(tc, oEmpLeaveStatus);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Save Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatus.ID;
}
public void SaveStatus(EmpLeaveStatus oEmpLeaveStatus)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
//if (oEmpLeaveStatus.IsNew)
//{
//LeaveProcessDA.DeleteByProcessId(tc, oEmpLeaveStatus.ProcessId);
this.SetObjectID(oEmpLeaveStatus, ID.FromInteger(LeaveProcessDA.GetNewDetailID(tc)));
LeaveProcessDA.Insert(tc, oEmpLeaveStatus);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Save Employee Leave Status: " + e.Message, e);
#endregion
}
}
public void SaveStatus(List<EmpLeaveStatus> oEmpLeaveStatus)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
foreach (EmpLeaveStatus oItem in oEmpLeaveStatus)
{
this.SetObjectID(oItem, ID.FromInteger(LeaveProcessDA.GetNewDetailID(tc)));
LeaveProcessDA.Insert(tc, oItem);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Save Employee Leave Status: " + e.Message, e);
#endregion
}
}
internal void SaveAtProcessing(bool IsUpdate, int ProcessId, ObjectsTemplate<EmpLeaveStatus> oEmpLeaveStatuss, TransactionContext tc)
{
if (oEmpLeaveStatuss != null)
{
foreach (EmpLeaveStatus oItem in oEmpLeaveStatuss)
{
if (IsUpdate) LeaveProcessDA.Update(tc, oItem);
else
{
this.SetObjectID(oItem, ID.FromInteger(LeaveProcessDA.GetNewDetailID(tc)));
oItem.ProcessId = ProcessId;
LeaveProcessDA.Insert(tc, oItem);
}
}
}
}
public void DeleteProcessDetail(ID id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteProcessDetail(tc, id.Integer);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Employee Leave Status: " + e.Message, e);
#endregion
}
}
public void DeleteProcessDetailByID(int nProcessId)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByProcessId(tc, nProcessId);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Employee Leave Status: " + e.Message, e);
#endregion
}
}
public void DeleteByPayrollType(int nProcessId, int nid, int nLeaveID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
LeaveProcessDA.DeleteByPayrollType(tc, nProcessId, nid, nLeaveID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Delete Employee Leave Status: " + e.Message, e);
#endregion
}
}
public ObjectsTemplate<EmpLeaveStatus> GetByYearType(int leaveYearID, int leaveId)
{
#region Cache Header
ObjectsTemplate<EmpLeaveStatus> oEmpLeaveStatuss = _cache["Get"] as ObjectsTemplate<EmpLeaveStatus>;
if (oEmpLeaveStatuss != null)
return oEmpLeaveStatuss;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oReader = new DataReader(LeaveProcessDA.GetByYearType(tc, leaveYearID, leaveId));
oEmpLeaveStatuss = this.CreateEmpLeaveStatusObjects(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 Employee Leave Status: " + e.Message, e);
#endregion
}
return oEmpLeaveStatuss;
}
#endregion
#endregion
}
#endregion
}