EchoTex_Payroll/HRM.DA/Service/Settlement/SettlementClearanceService.cs

681 lines
24 KiB
C#
Raw Permalink Normal View History

2024-10-14 10:01:49 +06:00
using HRM.BO;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using HRM.BO.Configuration;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Threading;
using System.Linq;
using System.Data;
namespace HRM.DA
{
#region SettlementClearance Service
public class SettlementClearanceService : ServiceTemplate, ISettlementClearanceService
{
#region Private functions and declaration
public SettlementClearanceService()
{
}
private void MapObject(SettlementClearance oSettlementClearance, DataReader oReader)
{
base.SetObjectID(oSettlementClearance, oReader.GetInt32("SettlementClearanceID").Value);
oSettlementClearance.UserID = oReader.GetInt32("UserID").Value;
oSettlementClearance.EmployeeID = oReader.GetInt32("EmployeeID").Value;
oSettlementClearance.SentDate = oReader.GetDateTime("SentDate").HasValue ? oReader.GetDateTime("SentDate").Value : DateTime.MinValue;
oSettlementClearance.ClearanceDate = oReader.GetDateTime("ClearanceDate").HasValue ? oReader.GetDateTime("ClearanceDate").Value : DateTime.MinValue;
oSettlementClearance.SettlementClearanceStatus = (EnumSettlementClearanceStatus)oReader.GetInt32("SettlementClearanceStatus").Value;
oSettlementClearance.CreatedBy = oReader.GetInt32("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value;
oSettlementClearance.CreatedDate = oReader.GetDateTime("CreatedDate").HasValue ? oReader.GetDateTime("CreatedDate").Value : DateTime.MinValue;
oSettlementClearance.ModifiedBy = oReader.GetInt32("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value;
oSettlementClearance.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue ? oReader.GetDateTime("ModifiedDate").Value : DateTime.MinValue;
oSettlementClearance.EmpClearanceID = oReader.GetInt32("EmpCleranceID").Value;
oSettlementClearance.ResignationRequestID = oReader.GetInt32("ResignationRequestID").Value;
this.SetObjectState(oSettlementClearance, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
SettlementClearance oSettlementClearance = new SettlementClearance();
MapObject(oSettlementClearance, oReader);
return oSettlementClearance as T;
}
private SettlementClearance CreateObject(DataReader oReader)
{
SettlementClearance oSettlementClearance = new SettlementClearance();
MapObject(oSettlementClearance, oReader);
return oSettlementClearance;
}
#endregion
#region Service implementation
public List<SettlementClearance> Get()
{
List<SettlementClearance> oSettlementClearance = new List<SettlementClearance>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SettlementClearanceDA.Get(tc));
oSettlementClearance = this.CreateObjects<SettlementClearance>(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
}
return oSettlementClearance;
}
public SettlementClearance Get(int id)
{
SettlementClearance oSettlementClearance = new SettlementClearance();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(SettlementClearanceDA.Get(tc, id));
if (oreader.Read())
{
oSettlementClearance = this.CreateObject<SettlementClearance>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get SettlementClearance", e);
#endregion
}
return oSettlementClearance;
}
public SettlementClearance Get(TransactionContext tc, int notifiedEmpID, int empResignationID)
{
SettlementClearance oSettlementClearance = new SettlementClearance();
// TransactionContext tc = null;
try
{
//tc = TransactionContext.Begin();
DataReader oreader = new DataReader(SettlementClearanceDA.Get(tc, notifiedEmpID, empResignationID));
if (oreader.Read())
{
oSettlementClearance = this.CreateObject<SettlementClearance>(oreader);
}
oreader.Close();
// tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get SettlementClearance", e);
#endregion
}
return oSettlementClearance;
}
public int Save(SettlementClearance oSettlementClearance)
{
TransactionContext tc = null;
try
{
bool isMailSent = SendEmail(oSettlementClearance.NotificationEmployee, oSettlementClearance.ResignationEmployee);
if (isMailSent == false)
{
new Exception("Mail has not sent");
}
if (isMailSent == true)
{
SettlementClearance prvItem = this.Get(tc, oSettlementClearance.EmployeeID, oSettlementClearance.EmpClearanceID);
if (prvItem != null)
{
oSettlementClearance.ID = prvItem.ID;
}
tc = TransactionContext.Begin(true);
if (oSettlementClearance.IsNew)
{
int id = tc.GenerateID("SettlementClearance", "SettlementClearanceID");
base.SetObjectID(oSettlementClearance, id);
SettlementClearanceDA.Insert(tc, oSettlementClearance);
}
else
{
SettlementClearanceDA.Update(tc, oSettlementClearance);
}
tc.End();
}
return oSettlementClearance.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new Exception("Failed to Insert SettlementClearance. Because " + e.Message, e);
#endregion
}
}
public void SaveAll(string employeeIds, CurrentUser? currentUser, int empresignationId)
{
TransactionContext tc = null;
int id = 0;
try
{
List<int> empIds = employeeIds.Split(',').Select(int.Parse).ToList();
empIds = empIds.Distinct().ToList();
List<Employee> emps = new EmployeeService().GetByEmpIDs(employeeIds, (int)currentUser.PayrollTypeID);
EmpResignationRequest rg = new ResignationRequestService().Get(empresignationId);
Employee regEmp = new EmployeeService().Get(rg.EmployeeID);
if (regEmp == null)
{
throw new Exception("No Resignation Employee Found");
}
tc = TransactionContext.Begin(true);
if (empIds != null && empIds.Count > 0)
{
foreach (var item in empIds)
{
SettlementClearance sc = new SettlementClearance();
SettlementClearance prvItem = this.Get(tc, item, regEmp.ID);
if (prvItem != null)
{
sc = prvItem;
sc.ID = prvItem.ID;
}
if (sc.IsNew)
{
sc.NotificationEmployee = emps.FirstOrDefault(x => x.ID == item);
sc.EmployeeID = item;
sc.UserID = currentUser.UserID;
sc.EmpClearanceID = regEmp.ID;
sc.SentDate = DateTime.Today;
sc.ClearanceDate = null;
sc.CreatedDate = DateTime.Today;
sc.CreatedBy = currentUser.UserID;
sc.ResignationEmployee = regEmp;
sc.ResignationRequestID = empresignationId;
sc.SettlementClearanceStatus = EnumSettlementClearanceStatus.Pending;
id = tc.GenerateID("SettlementClearance", "SettlementClearanceID");
base.SetObjectID(sc, id);
SettlementClearanceDA.Insert(tc, sc);
SendEmail(sc.NotificationEmployee, sc.ResignationEmployee);
//if (isMailSent == false)
//{
// throw new Exception("Mail has not been sent. Inavlid Email Address");
//}
SettlementClearanceDA.UpdateResignationStatus(tc, empresignationId, EnumResignStatus.Clearance_initiated);
}
else
{
if(sc.SettlementClearanceStatus != EnumSettlementClearanceStatus.Completed)
{
sc.NotificationEmployee = emps.FirstOrDefault(x => x.ID == item);
sc.ResignationEmployee = regEmp;
sc.ModifiedDate = DateTime.Now;
sc.ModifiedBy= currentUser.UserID;
sc.ClearanceDate = null;
// SettlementClearanceDA.Update(tc, sc);
SendEmail(sc.NotificationEmployee, sc.ResignationEmployee);
}
}
}
}
else
{
throw new Exception("Employee is not valid");
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public bool SendEmail(Employee mailEmployee, Employee regEmployee)
{
var isMailSent = false;
MailSender sender = null;
EmailSettings st = new EmailSettings();
try
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
IConfiguration Configuration = builder.Build();
Configuration.GetSection("EmailSettings").Bind(st);
sender = new MailSender();
Thread.Sleep(200);
string sbody = "";
if (mailEmployee.EmailAddress == string.Empty || mailEmployee.EmailAddress == null)
{
throw new Exception("no mail address found");
}
var description = "Notification of Final Settlement Clearance";
sender = new MailSender();
sender.AddTo(mailEmployee.EmailAddress.Trim());
sbody = "Dear " + mailEmployee.Name + "," + "<br/><br/>" + " Please Complete Settlements for Employee No:" + regEmployee.EmployeeNo + " , Employee Name :" + regEmployee.Name;
sender.Body = sbody;
sender.Link = st.WebAddress;
sender.Subject = description;
sender.ThreadSendMail(st);
isMailSent = true;
}
catch (Exception ex)
{
//throw new ServiceException(ex.Message);
// throw new CustomException(EnumExceptionType.Informational, "Successfully submitted but system could not send E-mail. You do not need to submit it again.");
}
return isMailSent;
}
public void Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
SettlementClearanceDA.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 SettlementClearance GetByEmpID(int empID)
{
SettlementClearance oSettlementClearance = new SettlementClearance();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(SettlementClearanceDA.GetByEmpID(tc, empID));
if (oreader.Read())
{
oSettlementClearance = this.CreateObject<SettlementClearance>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get SettlementClearance", e);
#endregion
}
return oSettlementClearance;
}
public SettlementClearance GetByUserID(int userID)
{
SettlementClearance oSettlementClearance = new SettlementClearance();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(SettlementClearanceDA.GetByUserID(tc, userID));
if (oreader.Read())
{
oSettlementClearance = this.CreateObject<SettlementClearance>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get SettlementClearance", e);
#endregion
}
return oSettlementClearance;
}
public List<SettlementClearance> GetByStatus(EnumSettlementClearanceStatus scStatus)
{
List<SettlementClearance> oSettlementClearance = new List<SettlementClearance>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SettlementClearanceDA.GetByStatus(tc, scStatus));
oSettlementClearance = this.CreateObjects<SettlementClearance>(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
}
return oSettlementClearance;
}
public List<SettlementClearance> GetByResignationRequestId(TransactionContext tc, int ResignationRequestID)
{
List<SettlementClearance> oSettlementClearance = new List<SettlementClearance>();
//TransactionContext tc = null;
try
{
// tc = TransactionContext.Begin();
DataReader dr = new DataReader(SettlementClearanceDA.GetByResignationRequestId(tc, ResignationRequestID));
oSettlementClearance = this.CreateObjects<SettlementClearance>(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
}
return oSettlementClearance;
}
public List<SettlementClearance> GetBySentDate(DateTime sentDate)
{
List<SettlementClearance> oSettlementClearance = new List<SettlementClearance>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SettlementClearanceDA.GetBySentDate(tc, sentDate));
oSettlementClearance = this.CreateObjects<SettlementClearance>(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
}
return oSettlementClearance;
}
public List<SettlementClearance> GetBySentDateRange(DateTime fromSentDate, DateTime toSentDate)
{
List<SettlementClearance> oSettlementClearance = new List<SettlementClearance>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SettlementClearanceDA.GetBySentDateRange(tc, fromSentDate, toSentDate));
oSettlementClearance = this.CreateObjects<SettlementClearance>(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
}
return oSettlementClearance;
}
public List<SettlementClearance> GetByClearanceDate(DateTime clearanceDate)
{
List<SettlementClearance> oSettlementClearance = new List<SettlementClearance>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SettlementClearanceDA.GetByClearanceDate(tc, clearanceDate));
oSettlementClearance = this.CreateObjects<SettlementClearance>(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
}
return oSettlementClearance;
}
public List<SettlementClearance> GetByRegEmp(int regEmpId, int employeeid)
{
List<SettlementClearance> oSettlementClearance = new List<SettlementClearance>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SettlementClearanceDA.GetByRegEmpID(tc, regEmpId, employeeid));
oSettlementClearance = this.CreateObjects<SettlementClearance>(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
}
return oSettlementClearance;
}
public List<SettlementClearance> GetByResignationId(int resignationId)
{
List<SettlementClearance> oSettlementClearance = new List<SettlementClearance>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SettlementClearanceDA.GetByResignationRequestId(tc, resignationId));
oSettlementClearance = this.CreateObjects<SettlementClearance>(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
}
return oSettlementClearance;
}
public bool IsClearanceEmp(int empid)
{
bool bvalue = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
bvalue = SettlementClearanceDA.IsClearanceEmp(tc, empid);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return bvalue;
}
public DataTable GetClearanceEmployee()
{
DataTable items =null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
items = SettlementClearanceDA.GetClearanceEmployee(tc);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return items;
}
public List<SettlementClearance> GetByClearanceDateRange(DateTime fromClearanceDate, DateTime toClearanceDate)
{
List<SettlementClearance> oSettlementClearance = new List<SettlementClearance>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(SettlementClearanceDA.GetByClearanceDateRange(tc, fromClearanceDate, toClearanceDate));
oSettlementClearance = this.CreateObjects<SettlementClearance>(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
}
return oSettlementClearance;
}
#endregion
}
#endregion
}