EchoTex_Payroll/HRM.DA/Service/Leave/ShortLeaveService.cs

558 lines
19 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
using Ease.Core;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using HRM.BO;
using HRM.BO.Configuration;
using Microsoft.Extensions.Configuration;
using NPOI.SS.Formula.Eval;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static iTextSharp.text.pdf.AcroFields;
namespace HRM.DA
{
public class ShortLeaveService : ServiceTemplate , IShortLeaveService
{
#region Private functions and declaration
protected override T CreateObject<T>(DataReader oReader)
{
ShortLeave oShortLeave = new ShortLeave();
MapObject(oShortLeave, oReader);
return oShortLeave as T;
}
private void MapObject(ShortLeave oShortLeave, DataReader oReader)
{
base.SetObjectID(oShortLeave, oReader.GetInt32("ShortLeaveId").Value);
oShortLeave.TelephoneNo = oReader.GetString("TelephoneNo");
oShortLeave.ALTEMPID = oReader.GetString("ALTEMPID");
oShortLeave.EntryDate = oReader.GetDateTime("EntryDate").Value;
oShortLeave.TotalHour = oReader.GetDouble("TotalHour").Value;
oShortLeave.DepartureTime = oReader.GetTime("DepartureTime").Value;
oShortLeave.DepartureEntryByID = oReader.GetInt32("DepartureEntryByID", true, 0);
oShortLeave.ReturnTime = oReader.GetTime("ReturnTime", true, TimeSpan.FromMinutes(0));
oShortLeave.ReturnEntryByID = oReader.GetInt32("ReturnEntryByID", true, 0);
oShortLeave.ActualInTime = oReader.GetTime("ActualInTime", true, TimeSpan.FromMinutes(0));
oShortLeave.ActualOutTime = oReader.GetTime("ActualOutTime", true, TimeSpan.FromMinutes(0));
oShortLeave.ExpectedReturnTime = oReader.GetTime("ExpectedReturnTime", true, TimeSpan.FromMinutes(0));
oShortLeave.GatePassStatus = (EnumGatePassStatus)oReader.GetInt32("GatePassStatus").Value;
oShortLeave.LineManagerID = oReader.GetInt32("LineManagerID").Value;
oShortLeave.ApprovedBy = oReader.GetInt32("ApprovedBy", true, 0);
oShortLeave.ApprovedDate = oReader.GetDateTime("ApprovedDate").Value;
oShortLeave.EmployeeID = oReader.GetInt32("EmployeeID").Value;
oShortLeave.PayrollTypeID = oReader.GetInt32("PayrollTypeID").Value;
oShortLeave.Remarks = oReader.GetString("Remarks");
oShortLeave.PlaceOfVisit = oReader.GetString("PlaceOfVisit");
oShortLeave.PurposeOfVisit = oReader.GetString("PurposeOfVisit");
}
private ShortLeave CreateObject(DataReader oReader)
{
ShortLeave oShortLeave = new ShortLeave();
MapObject(oShortLeave, oReader);
return oShortLeave;
}
#endregion
#region Service implementation
#region Get Functions
public ShortLeave Get(int id)
{
ShortLeave oShortLeave = new ShortLeave();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(ShortLeaveDA.Get(tc, id));
if (oreader.Read())
{
oShortLeave = this.CreateObject<ShortLeave>(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 Short Leave", e);
#endregion
}
return oShortLeave;
}
public ShortLeave GetByDate(int id, DateTime date)
{
ShortLeave oShortLeave = new ShortLeave();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(ShortLeaveDA.GetByDate(tc, id, date));
if (oreader.Read())
{
oShortLeave = this.CreateObject<ShortLeave>(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 Short Leave", e);
#endregion
}
return oShortLeave;
}
public List<ShortLeave> GetListByDate(int id, DateTime date)
{
List<ShortLeave> oShortLeaves = new List<ShortLeave>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(ShortLeaveDA.GetByDate(tc, id, date));
oShortLeaves = this.CreateObjects<ShortLeave>(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 Short Leave", e);
#endregion
}
return oShortLeaves;
}
public DataTable Get(int empid, int payrollTypeID, EnumGatePassStatus status, DateTime fromDate, DateTime toDate)
{
TransactionContext tc = null;
DataTable dt = null;
try
{
tc = TransactionContext.Begin();
//fromDate = fromDate.FirstDateOfMonth();
//toDate = toDate.LastDateOfMonth();
dt = ShortLeaveDA.Get(tc, empid, payrollTypeID, status, fromDate, toDate);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Short Leave Information" + e.Message, e);
#endregion
}
return dt;
}
public DataTable GetAppliedSortLeave(int lmId, EnumGatePassStatus status)
{
TransactionContext tc = null;
DataTable dt = null;
try
{
tc = TransactionContext.Begin();
dt = ShortLeaveDA.GetAppliedSortLeave(tc, lmId, status);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Applied Short Leave Data" + e.Message, e);
#endregion
}
return dt;
}
public DataTable GetApprovedShortLeave(int lmId, EnumGatePassStatus status, DateTime fromDate, DateTime toDate)
{
TransactionContext tc = null;
DataTable dt = null;
try
{
tc = TransactionContext.Begin();
dt = ShortLeaveDA.GetApprovedShortLeave(tc, lmId, status, fromDate, toDate);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Applied Short Leave Data" + e.Message, e);
#endregion
}
return dt;
}
public void ApproveOrRejectShortLeave(int shortLeaveid, EnumGatePassStatus status, int approver)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
ShortLeaveDA.ApproveOrRejectShortLeave(tc, shortLeaveid, status, approver);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new Exception("Failed to Approve Or Reject Short Leave. Because " + e.Message, e);
#endregion
}
}
public void ApproveMultipleShortLeave (string shortLeaveid, EnumGatePassStatus status, int approver)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
ShortLeaveDA.ApproveMultipleShortLeave(tc, shortLeaveid, status, approver);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new Exception("Failed to Approve Or Reject Short Leave. Because " + e.Message, e);
#endregion
}
}
public DataTable getGatemanShortLeave(EnumGatePassStatus status, DateTime fromDate, DateTime toDate)
{
TransactionContext tc = null;
DataTable dt = null;
try
{
tc = TransactionContext.Begin();
//fromDate = fromDate.FirstDateOfMonth();
//toDate = toDate.LastDateOfMonth();
dt = ShortLeaveDA.getGatemanShortLeave(tc, status, fromDate, toDate);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException("Failed to Get Short Leave Information" + e.Message, e);
#endregion
}
return dt;
}
#endregion
#region Save
public int Save(ShortLeave oShortLeave)
{
TransactionContext tc = null;
try
{
if (oShortLeave == null)
{
oShortLeave = new ShortLeaveService().Get(oShortLeave.ID);
}
if (oShortLeave.Employee == null)
{
oShortLeave.Employee = new EmployeeService().Get(oShortLeave.EmployeeID);
}
List<ShortLeave> oSL = new List<ShortLeave>();
int currentoShortLeaveID = oShortLeave.ID;
oSL = this.GetListByDate(oShortLeave.EmployeeID, oShortLeave.EntryDate);
oSL = oSL.Where(x => x.GatePassStatus != EnumGatePassStatus.Rejected //&& x.GatePassStatus != EnumGatePassStatus.Approved
&& x.ID != currentoShortLeaveID).ToList();
if (oSL.Count>0)
{
throw new Exception("Overlaps with previously applied Short leaves.");
}
else
{
tc = TransactionContext.Begin(true);
Save(oShortLeave, tc);
tc.End();
}
//if (isNew == true && oShortLeave.GatePassStatus == EnumGatePassStatus.Applied)
//{
// string subject = oShortLeave.Employee.EmployeeNo + "-" + oShortLeave.Employee.Name + ", has applied " + oShortLeave.PurposeOfVisit + " Short Leave on "
// + oShortLeave.EntryDate.ToString("dd MMM yyyy")
// + " at " + oShortLeave.EntryDate.Add(oShortLeave.DepartureTime).ToString("hh:mm tt")
// //" to " + oShortLeave.AppliedToDate.ToString("dd MMM yyyy") +
// //" days:" + oShortLeave.AppliedTotalDays.ToString()
// + ". Your Approval is required";
// WFManager<IworkflowInterface> om = new WFManager<IworkflowInterface>(oShortLeave);
// om.InitiateProcess(tc, oShortLeave.EmployeeID, oShortLeave.SetupID, oShortLeave.ID, "", subject);
//}
//else
//{
// tc = TransactionContext.Begin(true);
// if (oShortLeave.IsNew)
// {
// int id = tc.GenerateID("ShortLeave", "ShortLeaveID");
// base.SetObjectID(oShortLeave, id);
// ShortLeaveDA.Insert(tc, oShortLeave);
// }
// else
// {
// ShortLeaveDA.Update(tc, oShortLeave);
// }
// tc.End();
//}
//else if (oLeaveEntry.LeaveStatus == EnumLeaveStatus.Approved)
//{
// new DailyAttnProcessService().UpdateLeave(tc, oLeaveEntry.EmpID, oLeaveEntry.LeaveID, oLeaveEntry.CreatedBy, oLeaveEntry.ApprovedFromDate, oLeaveEntry.ApprovedToDate);
//}
return oShortLeave.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new Exception("Failed to Apply Short Leave. Because " + e.Message, e);
#endregion
}
}
public int Save(ShortLeave oShortLeave, TransactionContext tc)
{
try
{
bool isNew = oShortLeave.IsNew;
if (oShortLeave.IsNew)
{
int id = tc.GenerateID("ShortLeave", "ShortLeaveID");
base.SetObjectID(oShortLeave, id);
ShortLeaveDA.Insert(tc, oShortLeave);
}
else
{
ShortLeaveDA.Update(tc, oShortLeave);
}
if (isNew == true && oShortLeave.GatePassStatus == EnumGatePassStatus.Applied)
{
SendMail(oShortLeave);
}
//if (isNew == true && oShortLeave.GatePassStatus == EnumGatePassStatus.Applied)
//{
// string subject = oShortLeave.Employee.EmployeeNo + "-" + oShortLeave.Employee.Name + ", has applied " + oShortLeave.PurposeOfVisit + " Shortleave at "
// + oShortLeave.EntryDate.ToString("dd MMM yyyy")
// + ". Your Approval is required";
// WFManager<IworkflowInterface> om = new WFManager<IworkflowInterface>(oShortLeave);
// om.InitiateProcess(tc, oShortLeave.EmployeeID, oLeaveEntry.SetupID, oShortLeave.ID, "", subject);
//}
//else if (oLeaveEntry.LeaveStatus == EnumLeaveStatus.Approved)
//{
//}
return oShortLeave.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void UpdateInAndOutTime(ShortLeave oShortleave, Employee oEmp)
{
TransactionContext tc = null;
try
{
ShortLeave oSL = new ShortLeaveService().Get(oShortleave.ID);
if(oShortleave.EntryDate != DateTime.Now.Date)
{
throw new Exception("Incorrect Entry: Select employee applied today.");
}
if (oShortleave.ActualOutTime >= oSL.ActualOutTime && oSL.ActualOutTime != TimeSpan.FromMinutes(0)
&& oShortleave.ActualInTime == TimeSpan.FromMinutes(0) )
{
throw new Exception("The Departure Entry is Already recorded.");
}
if (oShortleave.ActualInTime >= oSL.ActualInTime && oSL.ActualInTime != TimeSpan.FromMinutes(0) )
{
throw new Exception("The Return Entry is Already recorded.");
}
tc = TransactionContext.Begin(true);
ShortLeaveDA.UpdateInAndOutTime(tc, oShortleave, oEmp);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
#region Delete
public void Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
ShortLeaveDA.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
#endregion
#region private methods
private void SendMail(ShortLeave shortLeave)
{
try
{
//string Name = shortLeave.Employee.EmployeeNo + "-" + shortLeave.Employee.Name;
Employee lm = new EmployeeService().Get(shortLeave.LineManagerID);
string Name = lm.Name;
string Email = lm.EmailAddress;
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
EmailSettings emailSettings = new EmailSettings();
IConfiguration Configuration = builder.Build();
Configuration.GetSection("EmailSettings").Bind(emailSettings);
MailSender mailSender = new MailSender();
mailSender.AddTo(Email)
;
mailSender.Subject = "Notification of Short Leave request (" + DateTime.Now.ToString("dd MMM yyyy") + ")";
mailSender.Body = $@"<p>Dear {Name},</p>
<p>A Short Leave request has been submitted for Employee ID-{shortLeave.Employee.EmployeeNo}</p>
<p>Your Approval is required.</p>
<p>Thanks {Name}</p>";
//<p>Regards,</p>
//<p>{shortLeave.Employee.Name}</p>";
mailSender.ThreadSendMail(emailSettings);
}
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.");
}
}
#endregion
}
}