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

300 lines
9.1 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;
using System.Configuration;
using System.Data.SqlClient;
using System.Reflection;
namespace Payroll.Service
{
#region PFTransaction Service
[Serializable]
public class PFTransactionService : ServiceTemplate, IPFTransactionService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(PFTransaction));
#endregion
public PFTransactionService() { }
private void MapObject(PFTransaction oPFTransaction, DataReader oReader)
{
base.SetObjectID(oPFTransaction, oReader.GetID("PFTRANID"));
oPFTransaction.EmployeeID = oReader.GetID("employeeID");
oPFTransaction.MonthDate = oReader.GetDateTime("tranDate").Value;
oPFTransaction.TranAmount = oReader.GetDouble("tranAmount").Value;
oPFTransaction.TranType = (EnumPFTranType)oReader.GetInt32("tranType").Value;
oPFTransaction.CreatedBy = oReader.GetID("CreatedBy");
oPFTransaction.CreatedDate = oReader.GetDateTime("CreationDate").Value;
this.SetObjectState(oPFTransaction, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
PFTransaction oPFTransaction = new PFTransaction();
MapObject(oPFTransaction, oReader);
return oPFTransaction as T;
}
protected PFTransaction CreateObject(DataReader oReader)
{
PFTransaction oPFTransaction = new PFTransaction();
MapObject(oPFTransaction, oReader);
return oPFTransaction;
}
#region Service implementation
public ObjectsTemplate<PFTransaction> Get()
{
#region Cache Header
ObjectsTemplate<PFTransaction> pFTransactions = _cache["Get"] as ObjectsTemplate<PFTransaction>;
if (pFTransactions != null)
return pFTransactions;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(PFTransactionDA.Get(tc));
pFTransactions = this.CreateObjects<PFTransaction>(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(pFTransactions, "Get");
#endregion
return pFTransactions;
}
public double GetPFAmount(int nEmpID)
{
double nAmount = 0.0;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
nAmount = PFTransactionDA.GetPFAmount(tc, 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 nAmount;
}
public DataSet GetPFBalance(int nEmpID, DateTime saalryMonth)
{
DataSet pfBalance = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
pfBalance = PFTransactionDA.GetPFBalance(tc, nEmpID, saalryMonth);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return pfBalance;
}
public DataSet GetPFBalance(DateTime saalryMonth)
{
DataSet pfBalance = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
pfBalance = PFTransactionDA.GetPFBalance(tc, saalryMonth);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return pfBalance;
}
public ID Save(PFTransaction oPFTransaction)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oPFTransaction.IsNew)
{
int id = tc.GenerateID("PFTransaction", "PfTranID");
base.SetObjectID(oPFTransaction, ID.FromInteger(id));
PFTransactionDA.Insert(tc, oPFTransaction);
}
else
{
PFTransactionDA.Update(tc, oPFTransaction);
}
tc.End();
return oPFTransaction.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void SaveAll(List<PFTransaction> oTrans)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
foreach (PFTransaction oItem in oTrans)
{
PFTransactionDA.Delete(tc, oItem.EmployeeID, oItem.MonthDate, oItem.TranType);
}
foreach (PFTransaction oItem in oTrans)
{
int id = tc.GenerateID("PFTransaction", "PfTranID");
base.SetObjectID(oItem, ID.FromInteger(id));
PFTransactionDA.Insert(tc, oItem);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public static DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
public ID Save(TransactionContext tc, PFTransaction oPFTransaction)
{
try
{
if (oPFTransaction.IsNew)
{
PFTransactionDA.Insert(tc, oPFTransaction);
}
else
{
PFTransactionDA.Update(tc, oPFTransaction);
}
return oPFTransaction.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 employeeId, DateTime month, EnumPFTranType type)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
PFTransactionDA.Delete(tc, employeeId, month, type);
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
}