CEL_Payroll/Payroll.Service/Basic/Service/BranchService.cs

327 lines
10 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 Branch Service
[Serializable]
public class BranchService : ServiceTemplate, IBranchService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(Branch));
#endregion
public BranchService() { }
private void MapObject(Branch oBranch, DataReader oReader)
{
//oBranch.ID = oReader.GetID("BranchID");
base.SetObjectID(oBranch, ID.FromInteger(oReader.GetInt32("BranchID").Value));
oBranch.Code = oReader.GetString("Code");
oBranch.Name = oReader.GetString("Name");
oBranch.Address = oReader.GetString("Address");
oBranch.Telephone = oReader.GetString("Telephone");
oBranch.BankID = ID.FromInteger(oReader.GetInt32("BankID").Value);
oBranch.Sequence = oReader.GetInt32("SequenceNO").GetValueOrDefault();
oBranch.Status = (EnumStatus)oReader.GetInt32("Status").GetValueOrDefault();
oBranch.CreatedBy = oReader.GetID("CreatedBy");
oBranch.CreatedDate = oReader.GetDateTime("CreationDate").GetValueOrDefault();
oBranch.ModifiedBy = oReader.GetID("ModifiedBy");
oBranch.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oBranch, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
Branch oBranch = new Branch();
MapObject(oBranch, oReader);
return oBranch as T;
}
protected Branch CreateObject(DataReader oReader)
{
Branch oBranch = new Branch();
MapObject(oBranch, oReader);
return oBranch;
}
#region Service implementation
public string GetNextCode()
{
TransactionContext tc = null;
string _code = "";
try
{
tc = TransactionContext.Begin();
_code = GlobalFunctionService.GetMaxCode(tc, "branch", "codeautogenerate", "BRANCHES", "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 Branch Get(ID id)
{
Branch oBranch = new Branch();
#region Cache Header
oBranch = _cache["Get", id] as Branch;
if (oBranch != null)
return oBranch;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(BranchDA.Get(tc, id));
if (oreader.Read())
{
oBranch = this.CreateObject<Branch>(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(oBranch, "Get", id);
#endregion
return oBranch;
}
public ObjectsTemplate<Branch> GetChild(ID bankID)
{
#region Cache Header
ObjectsTemplate<Branch> branchs = _cache["GetChild", bankID] as ObjectsTemplate<Branch>;
if (branchs != null)
return branchs;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(BranchDA.GetChild(tc, bankID));
branchs = this.CreateObjects<Branch>(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(branchs, "GetChild", bankID);
#endregion
return branchs;
}
public ObjectsTemplate<Branch> Get(EnumStatus status)
{
#region Cache Header
ObjectsTemplate<Branch> branchs = _cache["Get", status] as ObjectsTemplate<Branch>;
if (branchs != null)
return branchs;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(BranchDA.Get(tc, status));
branchs = this.CreateObjects<Branch>(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(branchs, "Get", status);
#endregion
return branchs;
}
public Branch GetOldBranchByID(ID branchID)
{
#region Cache Header
Branch branch = _cache["GetOldBranchByID"] as Branch;
if (branch != null)
return branch;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(BranchDA.GetOldBranchByID(tc, branchID));
if (dr.Read())
branch = this.CreateObject<Branch>(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(branch, "GetOldBranchByID");
#endregion
return branch;
}
public ID Save(Branch oBranch)
{
TransactionContext tc = null;
try
{
int nID = oBranch.BankID.Integer;
tc = TransactionContext.Begin(true);
if (oBranch.IsNew)
{
int id = tc.GenerateID("Branches", "BranchID");
base.SetObjectID(oBranch, ID.FromInteger(id));
int seqNo = tc.GenerateID("Branches", "SequenceNO");
oBranch.Sequence = seqNo;
BranchDA.Insert(tc, oBranch);
}
else
{
BranchDA.Update(tc, oBranch);
}
tc.End();
return oBranch.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);
BranchDA.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<Branch> branches)
{
try
{
foreach (Branch oBranch in branches)
{
if (oBranch.IsNew)
{
int seqNo = tc.GenerateID("Branches", "SequenceNO");
oBranch.Sequence = seqNo;
bool isAutoGenerated = ConfigurationManager.GetBoolValue("branch", "codeautogenerate", EnumConfigurationType.Logic);
if (isAutoGenerated == true)
oBranch.Code = GlobalFunctionService.GetMaxCode(tc, "branch", "codeautogenerate", "Branches", "Code");
oBranch.CreatedBy = User.CurrentUser.ID;
oBranch.CreatedDate = DateTime.Now;
BranchDA.Insert(tc, oBranch);
}
else
{
oBranch.ModifiedBy = User.CurrentUser.ID;
oBranch.ModifiedDate = DateTime.Now;
BranchDA.Update(tc, oBranch);
}
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
}
#endregion
}