CEL_Payroll/Payroll.Service/OverTime/Service/EmployeeOverTimeService.cs

405 lines
13 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 EmployeeOverTime Service
[Serializable]
public class EmployeeOverTimeService : ServiceTemplate, IEmployeeOverTimeService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(EmployeeOverTime));
#endregion
public EmployeeOverTimeService() { }
private void MapObject(EmployeeOverTime oEmployeeOverTime, DataReader oReader)
{
base.SetObjectID(oEmployeeOverTime, oReader.GetID("EmpOverTimeID"));
oEmployeeOverTime.TermID = oReader.GetID("termID");
oEmployeeOverTime.TermParameterID = oReader.GetID("TermParameterID");
oEmployeeOverTime.EmployeeID = oReader.GetID("employeeID");
oEmployeeOverTime.MonthDate = oReader.GetDateTime("monthDate").Value;
oEmployeeOverTime.OTMonth = oReader.GetDateTime("OTMonth").Value;
oEmployeeOverTime.OTHours = oReader.GetDouble("OTHours").Value;
oEmployeeOverTime.Value = oReader.GetDouble("amount").Value;
oEmployeeOverTime.CreatedBy = oReader.GetID("CreatedBy");
oEmployeeOverTime.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oEmployeeOverTime.ModifiedBy = oReader.GetID("ModifiedBy");
oEmployeeOverTime.ModifiedDate = oReader.GetDateTime("ModifiedDate");
oEmployeeOverTime.OTMonthBasic = oReader.GetDouble("OTMonthBasic").Value;
this.SetObjectState(oEmployeeOverTime, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
EmployeeOverTime oEmployeeOverTime = new EmployeeOverTime();
MapObject(oEmployeeOverTime, oReader);
return oEmployeeOverTime as T;
}
protected EmployeeOverTime CreateObject(DataReader oReader)
{
EmployeeOverTime oEmployeeOverTime = new EmployeeOverTime();
MapObject(oEmployeeOverTime, oReader);
return oEmployeeOverTime;
}
#region Service implementation
public EmployeeOverTime Get(ID id)
{
EmployeeOverTime oEmployeeOverTime = new EmployeeOverTime();
#region Cache Header
oEmployeeOverTime = _cache["Get", id] as EmployeeOverTime;
if (oEmployeeOverTime != null)
return oEmployeeOverTime;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(EmployeeOverTimeDA.Get(tc, id));
if (oreader.Read())
{
oEmployeeOverTime = this.CreateObject<EmployeeOverTime>(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(oEmployeeOverTime, "Get", id);
#endregion
return oEmployeeOverTime;
}
public ObjectsTemplate<EmployeeOverTime> Get(DateTime dSalaryMonth,ID nPayrollTypeID)
{
#region Cache Header
ObjectsTemplate<EmployeeOverTime> empOTs = _cache["Get"] as ObjectsTemplate<EmployeeOverTime>;
if (empOTs != null)
return empOTs;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(EmployeeOverTimeDA.Get(tc, dSalaryMonth,nPayrollTypeID));
empOTs = this.CreateObjects<EmployeeOverTime>(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(empOTs, "Get");
#endregion
return empOTs;
}
public ObjectsTemplate<EmployeeOverTime> Get()
{
#region Cache Header
ObjectsTemplate<EmployeeOverTime> empOTs = _cache["Get"] as ObjectsTemplate<EmployeeOverTime>;
if (empOTs != null)
return empOTs;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(EmployeeOverTimeDA.Get(tc));
empOTs = this.CreateObjects<EmployeeOverTime>(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(empOTs, "Get");
#endregion
return empOTs;
}
public ObjectsTemplate<EmployeeOverTime> GetByEmpID(ID nEmpID, DateTime otMonth, DateTime salaryMonth)
{
#region Cache Header
ObjectsTemplate<EmployeeOverTime> empOTs = _cache["Get", nEmpID, otMonth, salaryMonth] as ObjectsTemplate<EmployeeOverTime>;
if (empOTs != null)
return empOTs;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(EmployeeOverTimeDA.GetByEmpID(tc, nEmpID, otMonth, salaryMonth ));
empOTs = this.CreateObjects<EmployeeOverTime>(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(empOTs, "Get", nEmpID, otMonth, salaryMonth);
#endregion
return empOTs;
}
public ObjectsTemplate<EmployeeOverTime> GetByEmpID(ID nEmpID, DateTime dMonthDate)
{
#region Cache Header
ObjectsTemplate<EmployeeOverTime> empOTs = _cache["Get",nEmpID,dMonthDate] as ObjectsTemplate<EmployeeOverTime>;
if (empOTs != null)
return empOTs;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(EmployeeOverTimeDA.GetByEmpID(tc,nEmpID,dMonthDate));
empOTs = this.CreateObjects<EmployeeOverTime>(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(empOTs, "Get",nEmpID,dMonthDate);
#endregion
return empOTs;
}
public ID Save(EmployeeOverTime oEmployeeOverTime)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oEmployeeOverTime.IsNew)
{
int id = tc.GenerateID("EMPOVERTIME", "EmpOverTimeID");
base.SetObjectID(oEmployeeOverTime, ID.FromInteger(id));
EmployeeOverTimeDA.Insert(tc, oEmployeeOverTime);
}
else
{
EmployeeOverTimeDA.Update(tc, oEmployeeOverTime);
}
tc.End();
return oEmployeeOverTime.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void SaveByExcel(ObjectsTemplate<EmployeeOverTime> _EmpOverTimes)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
//foreach (EmployeeOverTime oEmpOverTime in _EmpOverTimes)
// EmployeeOverTimeDA.DeleteByEmpID(tc, oEmpOverTime.EmployeeID, oEmpOverTime.MonthDate);
if (_EmpOverTimes.Count > 0) EmployeeOverTimeDA.DeleteByMonth(tc,_EmpOverTimes[0].MonthDate);
int id = tc.GenerateID("EMPOVERTIME", "EmpOverTimeID");
foreach (EmployeeOverTime oEmpOverTime in _EmpOverTimes)
{
if (oEmpOverTime.OTHours > 0)
{
base.SetObjectID(oEmpOverTime, ID.FromInteger(id));
EmployeeOverTimeDA.Insert(tc, oEmpOverTime);
id = id + 1;
}
}
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(ObjectsTemplate<EmployeeOverTime> _EmpOverTimes)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
foreach (EmployeeOverTime oEmpOverTime in _EmpOverTimes)
EmployeeOverTimeDA.DeleteByEmpID(tc, oEmpOverTime.EmployeeID, oEmpOverTime.MonthDate);
int id = tc.GenerateID("EMPOVERTIME", "EmpOverTimeID");
foreach (EmployeeOverTime oEmpOverTime in _EmpOverTimes)
{
if (oEmpOverTime.OTHours > 0)
{
base.SetObjectID(oEmpOverTime, ID.FromInteger(id));
EmployeeOverTimeDA.Insert(tc, oEmpOverTime);
id = id + 1;
}
}
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 DeleteByMonth(DateTime dOTMonth)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
EmployeeOverTimeDA.DeleteByMonth(tc, dOTMonth);
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(ID id)
//{
// TransactionContext tc = null;
// try
// {
// tc = TransactionContext.Begin(true);
// EmployeeOverTimeDA.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
// }
//}
#endregion
}
#endregion
}