CEL_Payroll/Payroll.Service/Basic/Service/BankSevice.cs

313 lines
9.6 KiB
C#
Raw 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 Bank Service
[Serializable]
public class BankService : ServiceTemplate, IBankService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(Bank));
#endregion
public BankService() { }
private void MapObject(Bank oBank, DataReader oReader)
{
//oBank.ID = oReader.GetID("BankID");
base.SetObjectID(oBank, ID.FromInteger(oReader.GetInt32("BankID").Value));
oBank.Code = oReader.GetString("Code");
oBank.Name = oReader.GetString("Name");
oBank.Accountingformat = oReader.GetString("ACCOUNTNOFORMAT");
oBank.AccountNo = oReader.GetString("AccountNo");
oBank.Sequence = oReader.GetInt32("SequenceNO").Value;
oBank.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oBank.CreatedBy = oReader.GetID("CreatedBy");
oBank.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oBank.ModifiedBy = oReader.GetID("ModifiedBy");
oBank.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oBank,Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
Bank oBank = new Bank();
MapObject(oBank, oReader);
return oBank as T;
}
protected Bank CreateObject(DataReader oReader)
{
Bank oBank = new Bank();
MapObject(oBank, oReader);
return oBank;
}
#region Service implementation
public string GetNextCode()
{
TransactionContext tc = null;
string _code = "";
try
{
tc = TransactionContext.Begin();
_code = GlobalFunctionService.GetMaxCode(tc, "bank", "codeautogenerate", "Banks", "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 Bank Get(ID id)
{
Bank oBank = new Bank();
#region Cache Header
oBank = _cache["Get", id] as Bank;
if (oBank != null)
return oBank;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(BankDA.Get(tc, id));
if (oreader.Read())
{
oBank = this.CreateObject<Bank>(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(oBank, "Get", id);
#endregion
return oBank;
}
public Bank GetByBrancID(ID id)
{
Bank oBank = new Bank();
#region Cache Header
oBank = _cache["GetByBrancID", id] as Bank;
if (oBank != null)
return oBank;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(BankDA.GetByBrancID(tc, id));
if (oreader.Read())
{
oBank = this.CreateObject<Bank>(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(oBank, "GetByBrancID", id);
#endregion
return oBank;
}
public ObjectsTemplate<Bank> Get()
{
#region Cache Header
ObjectsTemplate<Bank> banks = _cache["Get"] as ObjectsTemplate<Bank>;
if (banks != null)
return banks;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(BankDA.Get(tc));
banks = this.CreateObjects<Bank>(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(banks, "Get");
#endregion
return banks;
}
public ObjectsTemplate<Bank> Get(EnumStatus status)
{
#region Cache Header
ObjectsTemplate<Bank> banks = _cache["Get", status] as ObjectsTemplate<Bank>;
if (banks != null)
return banks;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(BankDA.Get(tc, status));
banks = this.CreateObjects<Bank>(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(banks, "Get", status);
#endregion
return banks;
}
public ID Save(Bank oBank)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oBank.IsNew)
{
int id = tc.GenerateID("Banks", "BankID");
base.SetObjectID(oBank, ID.FromInteger(id));
int seqNo = tc.GenerateID("Banks", "SequenceNO");
oBank.Sequence = seqNo;
BankDA.Insert(tc, oBank);
}
else
{
BankDA.Update(tc, oBank);
}
tc.End();
return oBank.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);
BankDA.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
//for Excel Upload
public static void SaveForUpload(TransactionContext tc, ObjectsTemplate<Bank> banks)
{
try
{
foreach (Bank oBank in banks)
{
if (oBank.IsNew)
{
int seqNo = tc.GenerateID("Banks", "SequenceNO");
oBank.Sequence = seqNo;
bool isAutoGenerated = ConfigurationManager.GetBoolValue("bank", "codeautogenerate", EnumConfigurationType.Logic);
if (isAutoGenerated == true)
oBank.Code = GlobalFunctionService.GetMaxCode(tc, "bank", "codeautogenerate", "banks", "Code");
oBank.CreatedBy = User.CurrentUser.ID;
oBank.CreatedDate = DateTime.Now;
BankDA.Insert(tc, oBank);
}
else
{
oBank.ModifiedBy = User.CurrentUser.ID;
oBank.ModifiedDate = DateTime.Now;
BankDA.Update(tc, oBank);
}
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
}
#endregion
}