CEL_Payroll/Payroll.Service/Employee/Service/EmployeeCostCenterService.cs

521 lines
17 KiB
C#
Raw Permalink Normal View History

2024-09-17 14:30:13 +06:00
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 EmployeeCostCenter Service
[Serializable]
public class EmployeeCostCenterService : ServiceTemplate, IEmployeeCostCenterService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(EmployeeCostCenter));
#endregion
public EmployeeCostCenterService() { }
private void MapObject(EmployeeCostCenter oEmployeeCostCenter, DataReader oReader)
{
base.SetObjectID(oEmployeeCostCenter, oReader.GetID("ID"));
oEmployeeCostCenter.EmployeeID = oReader.GetID("employeeID");
oEmployeeCostCenter.MonthDate = oReader.GetDateTime("monthDate").Value;
oEmployeeCostCenter.CostCenterID = oReader.GetID("costCenterID");
oEmployeeCostCenter.Percentage = oReader.GetDouble("percentage").Value;
oEmployeeCostCenter.CreatedBy = oReader.GetID("CreatedBy");
oEmployeeCostCenter.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oEmployeeCostCenter.IsCurrentCC = oReader.GetBoolean("CurrentCC").Value;
oEmployeeCostCenter.ModifiedBy = oReader.GetID("ModifiedBy");
oEmployeeCostCenter.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oEmployeeCostCenter, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
EmployeeCostCenter oEmployeeCostCenter = new EmployeeCostCenter();
MapObject(oEmployeeCostCenter, oReader);
return oEmployeeCostCenter as T;
}
protected EmployeeCostCenter CreateObject(DataReader oReader)
{
EmployeeCostCenter oEmployeeCostCenter = new EmployeeCostCenter();
MapObject(oEmployeeCostCenter, oReader);
return oEmployeeCostCenter;
}
#region Service implementation
public EmployeeCostCenter Get(ID id)
{
EmployeeCostCenter oEmployeeCostCenter = new EmployeeCostCenter();
#region Cache Header
oEmployeeCostCenter = _cache["Get", id] as EmployeeCostCenter;
if (oEmployeeCostCenter != null)
return oEmployeeCostCenter;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(EmployeeCostCenterDA.Get(tc, id));
if (oreader.Read())
{
oEmployeeCostCenter = this.CreateObject<EmployeeCostCenter>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(oEmployeeCostCenter, "Get", id);
#endregion
return oEmployeeCostCenter;
}
public ObjectsTemplate<EmployeeCostCenter> GetByEmpID(ID nemployeeID)
{
#region Cache Header
ObjectsTemplate<EmployeeCostCenter> employeeCostCenters = _cache["Get"] as ObjectsTemplate<EmployeeCostCenter>;
if (employeeCostCenters != null)
return employeeCostCenters;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(EmployeeCostCenterDA.Get(tc, nemployeeID));
employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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
}
#region Cache Footer
_cache.Add(employeeCostCenters, "Get");
#endregion
return employeeCostCenters;
}
public ObjectsTemplate<EmployeeCostCenter> GetByMonthStartEnd(DateTime strt, DateTime end, int payrollTypeID)
{
ObjectsTemplate<EmployeeCostCenter> employeeCostCenters = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(EmployeeCostCenterDA.GetByMonthStartEnd(tc, strt, end, payrollTypeID));
employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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 employeeCostCenters;
}
public ObjectsTemplate<EmployeeCostCenter> GetByCCID(ID nCCID)
{
#region Cache Header
ObjectsTemplate<EmployeeCostCenter> employeeCostCenters = _cache["Get"] as ObjectsTemplate<EmployeeCostCenter>;
if (employeeCostCenters != null)
return employeeCostCenters;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(EmployeeCostCenterDA.GetByCCID(tc, nCCID));
employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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
}
#region Cache Footer
_cache.Add(employeeCostCenters, "Get");
#endregion
return employeeCostCenters;
}
public ObjectsTemplate<EmployeeCostCenter> GetByEmpIDCCID(ID nCCID, ID nEmpID)
{
#region Cache Header
ObjectsTemplate<EmployeeCostCenter> employeeCostCenters = _cache["Get"] as ObjectsTemplate<EmployeeCostCenter>;
if (employeeCostCenters != null)
return employeeCostCenters;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(EmployeeCostCenterDA.GetByEmpIDCCID(tc, nCCID,nEmpID));
employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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
}
#region Cache Footer
_cache.Add(employeeCostCenters, "Get");
#endregion
return employeeCostCenters;
}
public ObjectsTemplate<EmployeeCostCenter> Get()
{
#region Cache Header
ObjectsTemplate<EmployeeCostCenter> employeeCostCenters = _cache["Get"] as ObjectsTemplate<EmployeeCostCenter>;
if (employeeCostCenters != null)
return employeeCostCenters;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(EmployeeCostCenterDA.Get(tc));
employeeCostCenters = this.CreateObjects<EmployeeCostCenter>(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
}
#region Cache Footer
_cache.Add(employeeCostCenters, "Get");
#endregion
return employeeCostCenters;
}
public DataSet GetEmpCC(DateTime dMonthDate, string sEmpID)
{
DataSet oCC = new DataSet();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
oCC = EmployeeCostCenterDA.GetEmpCC(tc, dMonthDate, sEmpID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCC;
}
public DataSet GetEmpCCDetails(DateTime dMonthDate, string nEmpID)
{
DataSet oCC = new DataSet();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
oCC = EmployeeCostCenterDA.GetEmpCCDetails(tc, dMonthDate, nEmpID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCC;
}
public DateTime GetMaxDate(string EmpID, DateTime dMonthDate)
{
DateTime oCC = new DateTime();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
oCC = EmployeeCostCenterDA.GetMaxDate(tc, EmpID,dMonthDate);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCC;
}
public void Save(ObjectsTemplate<EmployeeCostCenter> _empCostCenters)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (_empCostCenters.Count == 0) throw new ServiceException("Empty collection is not allowed");
foreach (EmployeeCostCenter empCostCenter in _empCostCenters)
{
EmployeeCostCenterDA.DeleteByMonth(tc, empCostCenter.MonthDate, empCostCenter.EmployeeID);
EmployeeCostCenterDA.UpdateCurrentCCbeforeMonth(tc, empCostCenter.EmployeeID, false, empCostCenter.MonthDate);
}
foreach (EmployeeCostCenter empCostCenter in _empCostCenters)
{
this.SetObjectState(empCostCenter, ObjectState.New);
if (empCostCenter.IsNew)
{
int id = tc.GenerateID("EMPCOSTCENTER", "ID");
base.SetObjectID(empCostCenter, ID.FromInteger(id));
EmployeeCostCenterDA.Insert(tc, empCostCenter);
}
else
{
EmployeeCostCenterDA.Update(tc, empCostCenter);
}
}
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 Save(TransactionContext tc, ObjectsTemplate<EmployeeCostCenter> _empCostCenters)
{
try
{
if (_empCostCenters.Count == 0) throw new ServiceException("Empty collection is not allowed");
foreach (EmployeeCostCenter empCostCenter in _empCostCenters)
{
EmployeeCostCenterDA.DeleteByMonth(tc, empCostCenter.MonthDate, empCostCenter.EmployeeID);
EmployeeCostCenterDA.UpdateCurrentCCbeforeMonth(tc, empCostCenter.EmployeeID, false, empCostCenter.MonthDate);
}
foreach (EmployeeCostCenter empCostCenter in _empCostCenters)
{
this.SetObjectState(empCostCenter, ObjectState.New);
if (empCostCenter.IsNew)
{
int id = tc.GenerateID("EMPCOSTCENTER", "ID");
base.SetObjectID(empCostCenter, ID.FromInteger(id));
EmployeeCostCenterDA.Insert(tc, empCostCenter);
}
else
{
EmployeeCostCenterDA.Update(tc, empCostCenter);
}
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public ID Save(EmployeeCostCenter oEmployeeCostCenter)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oEmployeeCostCenter.IsNew)
{
int id = tc.GenerateID("EMPCOSTCENTER", "ID");
base.SetObjectID(oEmployeeCostCenter, ID.FromInteger(id));
EmployeeCostCenterDA.Insert(tc, oEmployeeCostCenter);
}
else
{
EmployeeCostCenterDA.Update(tc, oEmployeeCostCenter);
}
tc.End();
return oEmployeeCostCenter.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public ID Save( TransactionContext tc, EmployeeCostCenter oEmployeeCostCenter)
{
try
{
if (oEmployeeCostCenter.IsNew)
{
int id = tc.GenerateID("EMPCOSTCENTER", "ID");
base.SetObjectID(oEmployeeCostCenter, ID.FromInteger(id));
EmployeeCostCenterDA.Insert(tc, oEmployeeCostCenter);
}
else
{
EmployeeCostCenterDA.Update(tc, oEmployeeCostCenter);
}
return oEmployeeCostCenter.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Delete(ID id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
EmployeeCostCenterDA.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 void Delete(TransactionContext tc, ID id)
{
try
{
EmployeeCostCenterDA.Delete(tc, id);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
}
#endregion
}