EchoTex_Payroll/HRM.DA/Service/Attendance/CardOperationService.cs

559 lines
19 KiB
C#
Raw Permalink Normal View History

2024-10-14 10:01:49 +06:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using Ease.CoreV35.DataAccess;
using HRM.BO;
namespace HRM.DA
{
#region CardOperation Service
[Serializable]
public class CardOperationService : ServiceTemplate, ICardOperationService
{
public CardOperationService()
{
}
private void MapObject(CardOperation oCardOperation, DataReader oReader)
{
base.SetObjectID(oCardOperation, oReader.GetInt32("CardOperationID").Value);
oCardOperation.CardID = oReader.GetInt32("CardID", 0);
oCardOperation.CardNumber = oReader.GetString("CardNumber");
oCardOperation.Status = (EnumCardStatus)oReader.GetInt32("CardStatus").Value;
oCardOperation.AssignDate = oReader.GetDateTime("AssignDate").Value;
oCardOperation.EmployeeID = oReader.GetInt32("EmployeeID", 0);
oCardOperation.Comments = oReader.GetString("Comments");
oCardOperation.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oCardOperation.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oCardOperation.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
oCardOperation.ModifiedDate = oReader.GetDateTime("ModifiedDate");
oCardOperation.ExpiredDate = oReader.GetDateTime("ExpireDate", DateTime.MinValue);
oCardOperation.CardExpiredDate = oReader.GetDateTime("CardExpireDate", DateTime.MinValue);
this.SetObjectState(oCardOperation, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
CardOperation oCardOperation = new CardOperation();
MapObject(oCardOperation, oReader);
return oCardOperation as T;
}
#region Service implementation
public CardOperation Get(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(CardOperationDA.Get(tc, id));
CardOperation oCardOperation = null;
if (oreader.Read())
{
oCardOperation = this.CreateObject<CardOperation>(oreader);
}
oreader.Close();
tc.End();
return oCardOperation;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public CardOperation GetByCardID(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(CardOperationDA.GetByCardID(tc, id));
CardOperation oCardOperation = null;
if (oreader.Read())
{
oCardOperation = this.CreateObject<CardOperation>(oreader);
}
oreader.Close();
tc.End();
return oCardOperation;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public CardOperation GetByEmpIDAndCardID(int EmpID, int CardID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(CardOperationDA.GetByEmpIDAndCardID(tc, EmpID, CardID));
CardOperation oCardOperation = null;
if (oreader.Read())
{
oCardOperation = this.CreateObject<CardOperation>(oreader);
}
oreader.Close();
tc.End();
return oCardOperation;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public CardOperation Get(string CardNumber)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
CardOperation oCardOperation = null;
DataReader oreader = new DataReader(CardOperationDA.Get(tc, CardNumber));
if (oreader.Read())
{
oCardOperation = this.CreateObject<CardOperation>(oreader);
}
oreader.Close();
tc.End();
return oCardOperation;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public List<CardOperation> Get()
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(CardOperationDA.Get(tc));
var cardOperations = this.CreateObjects<CardOperation>(dr);
dr.Close();
tc.End();
return cardOperations;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public int Save(CardOperation oCardOperation)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oCardOperation.IsNew)
{
int id = tc.GenerateID("CardOperation", "CardOperationID");
base.SetObjectID(oCardOperation, id);
CardOperationDA.Insert(tc, oCardOperation);
}
else
{
CardOperationDA.Update(tc, oCardOperation);
}
tc.End();
return oCardOperation.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(List<CardOperation> ocardOperations, int payrollTypeId)
{
TransactionContext tc = null;
try
{
foreach(CardOperation item in ocardOperations)
{
AccessCard accessCard = new AccessCardService().Get(item.CardNumber);
item.CardID = accessCard.ID;
if (item.CardID != 0)
item.AccessCard = new AccessCardService().Get(item.CardID);
}
tc = TransactionContext.Begin(true);
var ac = new AccessCardService();
var emp = new EmployeeService();
AccessCardService accCardService = new AccessCardService();
foreach (CardOperation CardOP in ocardOperations)
{
if (CardOP.AccessCard == null)
{
throw new Exception("Card information not found");
}
if(CardOP.AccessCard.IsNew ==true)
{
new AccessCardService().Save(CardOP.AccessCard, tc);
CardOP.CardID = CardOP.AccessCard.ID;
}
//AccessCard accCard = accCardService.Get(CardOP.CardNumber, tc);
if (CardOP.AccessCard.Status == EnumCardStatus.Attached)
{
throw new Exception("Card is already astteched to another employee");
}
if (CardOP.IsNew)
{
int id = tc.GenerateID("CardOperation", "CardOperationID");
base.SetObjectID(CardOP, id);
CardOperationDA.Insert(tc, CardOP);
//ac.UpdateStatus(tc, CardOP.CardID, CardOP.Status);
ac.UpdateStatus(tc, CardOP.CardID, EnumCardStatus.Attached, payrollTypeId);
emp.UpdateCardInformation(tc, CardOP.EmployeeID, CardOP.CardID, true);
}
else
{
CardOperationDA.Update(tc, CardOP);
ac.UpdateStatus(tc, CardOP.CardID, CardOP.Status, payrollTypeId);
emp.UpdateCardInformation(tc, CardOP.EmployeeID, CardOP.CardID, true);
}
}
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(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
CardOperationDA.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
}
}
public DateTime LastCardOperationDate(int EmpID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
object obj = tc.ExecuteScalar("Select CardDate from Employee Where EmployeeID = %n", EmpID);
return obj == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(obj);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
finally
{
if (tc != null)
tc.End();
}
}
public void DetachCardFromEmployee(int EmployeeID, string CardNumber, DateTime DetachDateDate,
EnumCardStatus cardStatus)
{
TransactionContext tc = null;
try
{
EmployeeService empService = new EmployeeService();
AccessCardService accCardService = new AccessCardService();
AccessCard accCard = accCardService.Get(CardNumber);
tc = TransactionContext.Begin(true);
if (accCard != null)
{
accCard.Status = cardStatus == EnumCardStatus.Detached ? EnumCardStatus.Free : cardStatus;
accCardService.Save(accCard, tc);
empService.UpdateCardInformation(tc, EmployeeID, 0, DetachDateDate, cardStatus, true);
empService.UpdateEmpCardHistory(tc, EmployeeID, 0, DetachDateDate, accCard.CardNumber);
}
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 AssignCardToEmployee(List<CardOperation> _cardOperations)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
EmployeeService empService = new EmployeeService();
AccessCardService accCardService = new AccessCardService();
int CardOperationID = tc.GenerateID("CardOperation", "CardOperationID");
foreach (CardOperation CardOP in _cardOperations)
{
AccessCard accCard = accCardService.Get(CardOP.CardNumber, tc);
int id = 0;
if (accCard == null)
{
accCard = new AccessCard();
accCard.CardNumber = CardOP.CardNumber;
accCard.TypeID = 1; // Default Permanent
}
if (accCard.Status == EnumCardStatus.Attached)
{
throw new Exception("Card is already astteched to another employee");
}
accCard.Status = EnumCardStatus.Attached;
id = accCardService.Save(accCard, tc);
base.SetObjectID(CardOP, CardOperationID);
CardOP.CardID = id;
CardOperationDA.Insert(tc, CardOP);
empService.UpdateCardInformation(tc, CardOP.EmployeeID, CardOP.CardID, CardOP.AssignDate,
CardOP.Status, true);
empService.UpdateEmpCardHistory(tc, CardOP.EmployeeID, CardOP.CardID, CardOP.AssignDate,
CardOP.CardNumber);
CardOperationID++;
}
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 AssignCardToEmployee(int EmployeeID, string CardNumber, DateTime AssignDate, int userid)
{
TransactionContext tc = null;
try
{
EmployeeService empService = new EmployeeService();
AccessCardService accCardService = new AccessCardService();
AccessCard accCard = accCardService.Get(CardNumber);
int id = 0;
tc = TransactionContext.Begin(true);
if (accCard == null)
{
accCard = new AccessCard();
accCard.CardNumber = CardNumber;
accCard.TypeID = 1; // Default Permanent
}
if(accCard.Status == EnumCardStatus.Attached)
{
throw new Exception("Card is already astteched to another employee");
}
accCard.Status = EnumCardStatus.Attached;
id = accCardService.Save(accCard, tc);
CardOperation CardOP = new CardOperation();
base.SetObjectID(CardOP, tc.GenerateID("CardOperation", "CardOperationID"));
CardOP.CardID = id;
CardOP.CardNumber = accCard.CardNumber;
CardOP.EmployeeID = EmployeeID;
CardOP.Status = EnumCardStatus.Attached;
CardOP.AssignDate = AssignDate;
CardOP.Comments = string.Empty;
CardOP.CreatedBy = userid;
CardOP.CreatedDate = DateTime.Now;
CardOperationDA.Insert(tc, CardOP);
empService.UpdateCardInformation(tc, CardOP.EmployeeID, CardOP.CardID, AssignDate, CardOP.Status, true);
empService.UpdateEmpCardHistory(tc, CardOP.EmployeeID, CardOP.CardID, AssignDate, CardOP.CardNumber);
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(TransactionContext tc, List<CardOperation> ocardOperations)
{
try
{
EmployeeService emp = new EmployeeService();
foreach (CardOperation CardOP in ocardOperations)
{
if (CardOP.IsNew)
{
int id = tc.GenerateID("CardOperation", "CardOperationID");
base.SetObjectID(CardOP, id);
//Payroll.BO.AuditTrailBase audit = new Payroll.BO.AuditTrailBase();
CardOP.CreatedBy = 1;
CardOP.CreatedDate = DateTime.Now;
CardOperationDA.Insert(tc, CardOP);
emp.UpdateCardInformation(tc, CardOP.EmployeeID, CardOP.CardID, true);
}
else
{
CardOperationDA.Update(tc, CardOP);
emp.UpdateCardInformation(tc, CardOP.EmployeeID, CardOP.CardID, true);
}
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
//
public DataSet GetEmployeeWiseCardOperations(string sEmpIDs)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
return CardOperationDA.GetEmployeeWiseCardOperations(tc, sEmpIDs);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
finally
{
if (tc != null)
tc.End();
}
}
#endregion
}
#endregion
}