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

234 lines
7.8 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 OPIPayment Service
[Serializable]
public class OPIPaymentService : ServiceTemplate, IOPIPaymentService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(OPIPayment));
public OPIPaymentService() { }
private void MapObject(OPIPayment oOPIPayment, DataReader oReader)
{
base.SetObjectID(oOPIPayment, oReader.GetID("OPIPaymentID"));
oOPIPayment.OPIItemID = oReader.GetID("OPIItemID");
oOPIPayment.EmployeeID = oReader.GetID("EmployeeID");
oOPIPayment.PaymentDate = oReader.GetDateTime("PaymentDate").Value;
oOPIPayment.Amount = oReader.GetDouble("Amount").Value;
oOPIPayment.TaxAmount = oReader.GetDouble("TaxAmount").Value;
oOPIPayment.Remarks = oReader.GetString("Remarks");
oOPIPayment.CreatedBy = oReader.GetID("CreatedBy");
oOPIPayment.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oOPIPayment.ModifiedBy = oReader.GetID("ModifiedBy");
oOPIPayment.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oOPIPayment, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
OPIPayment oOPIPayment = new OPIPayment();
MapObject(oOPIPayment, oReader);
return oOPIPayment as T;
}
protected OPIPayment CreateObject(DataReader oReader)
{
OPIPayment oOPIPayment = new OPIPayment();
MapObject(oOPIPayment, oReader);
return oOPIPayment;
}
#endregion
#region Service implementation
public OPIPayment Get(ID id)
{
OPIPayment oOPIPayment = new OPIPayment();
#region Cache Header
oOPIPayment = (OPIPayment)_cache["Get", id] as OPIPayment;
if (oOPIPayment != null)
return oOPIPayment;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oReader = new DataReader(OPIPaymentDA.Get(tc, id));
if (oReader.Read())
{
oOPIPayment = this.CreateObject(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(oOPIPayment, "Get", id);
#endregion
return oOPIPayment;
}
public string GetNextCode()
{
TransactionContext tc = null;
string _code = "";
try
{
tc = TransactionContext.Begin();
_code = GlobalFunctionService.GetMaxCode(tc, "opipaymentitem", "codeautogenerate", "OPIPayment", "Code");
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return _code;
}
public ObjectsTemplate<OPIPayment> Get()
{
#region Cache Header
ObjectsTemplate<OPIPayment> oOPIPayments = _cache["Get"] as ObjectsTemplate<OPIPayment>;
if (oOPIPayments != null)
return oOPIPayments;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(OPIPaymentDA.Get(tc));
oOPIPayments = this.CreateObjects<OPIPayment>(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(oOPIPayments, "Get");
#endregion
return oOPIPayments;
}
public DataSet GetDataSetOfPaymentEmployees(ID nOpiItemID, EnumOpiType nOpiType)
{
DataSet dsEmployees = new DataSet();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
dsEmployees = OPIPaymentDA.GetDataSetOfPaymentEmployees(tc, nOpiItemID, nOpiType);
tc.End();
}
catch (Exception e)
{
throw new ServiceException("Failed to Get Employee ", e);
}
return dsEmployees;
}
public ID Save(OPIPayment oOPIPayment)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oOPIPayment.IsNew)
{
int id = tc.GenerateID("OPIPayment", "OPIPaymentID");
base.SetObjectID(oOPIPayment, ID.FromInteger(id));
OPIPaymentDA.Insert(tc, oOPIPayment);
}
else
{
OPIPaymentDA.Update(tc, oOPIPayment);
}
tc.End();
return oOPIPayment.ID;
}
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<OPIPayment> opiPayments)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
foreach (OPIPayment oPayment in opiPayments)
{
int id = tc.GenerateID("OPIPayment", "OPIPaymentID");
base.SetObjectID(oPayment, ID.FromInteger(id));
oPayment.CreatedBy = User.CurrentUser.ID;
oPayment.CreatedDate = DateTime.Now;
OPIPaymentDA.Insert(tc, oPayment);
}
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);
OPIPaymentDA.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
}