EchoTex_Payroll/HRM.MailNotificationProcess/SchedularSetupProcess.cs
2024-10-14 10:01:49 +06:00

1023 lines
51 KiB
C#

using System;
using System.Collections.Generic;
using HRM.BO;
using HRM.DA;
using HRM.Report;
using HRM.BO.Configuration;
using System.Data;
using System.IO;
using System.Threading;
using NPOI.SS.Formula.Functions;
using System.Windows.Forms;
using System.Linq;
using Microsoft.Extensions.Configuration;
namespace HRM.MailNotificationProcess
{
public class SchedularSetupProcess
{
#region Global Properties
private EmailSettings emailSettings;
private MailNotificationProcessConfiguration mailNotificationProcessConfiguration;
#endregion
#region Constractors
public SchedularSetupProcess()
{
this.emailSettings = null;
this.mailNotificationProcessConfiguration = null;
}
public SchedularSetupProcess(EmailSettings pEmailSettings, MailNotificationProcessConfiguration oMailNotificationProcessConfiguration)
{
this.emailSettings = pEmailSettings;
this.mailNotificationProcessConfiguration = oMailNotificationProcessConfiguration;
}
#endregion
#region wait the thread
public bool waitTheThread(DateTime startAt)
{
if (!mailNotificationProcessConfiguration.IsTest)
{
double timeLeft = startAt.Subtract(DateTime.Now).TotalMilliseconds;
if (timeLeft < 0)
{
return false;
}
else
{
// Thread.Sleep(Convert.ToInt32(timeLeft));
return true;
}
}
else
{
return true;
}
}
#endregion
#region CreateAndSendMail
private void CreateAndSendMail(SchedularSetup pSchedularSetup, string toEmail, string pdfFileLocation = "")
{
try
{
MailSender mailSender = new MailSender();
mailSender.AddTo(toEmail);
mailSender.Subject = pSchedularSetup.MailSubject;
mailSender.Body = pSchedularSetup.MailBody.Replace("<<EmployeeName>>", "Sir/Madam");
if(pSchedularSetup.Type == EnumSchedularType.Yesterday_Absent_Notification_Self)
{
mailSender.Body = mailSender.Body.Replace("<<Date>>", DateTime.Today.AddDays(-1).ToLongDateString());
}
if (pdfFileLocation != "")
{
mailSender.AddAttachment(pdfFileLocation);
}
mailSender.SendMail(emailSettings);
}
catch (Exception)
{
throw;
}
}
private void CreateAndSendMailForMonthlyAttendanceSummary(SchedularSetup pSchedularSetup, DateTime date, string toEmail, string pdfFileLocation = "")
{
try
{
MailSender mailSender = new MailSender();
mailSender.AddTo(toEmail);
mailSender.Subject = pSchedularSetup.MailSubject;
mailSender.Body = pSchedularSetup.MailBody.Replace("<<EmployeeName>>", "Sir/Madam").Replace("<<Date>>", date.ToString("MMMM yyyy"));
if (pdfFileLocation != "")
{
mailSender.AddAttachment(pdfFileLocation);
}
mailSender.SendMail(emailSettings);
}
catch (Exception)
{
throw;
}
}
private void CreateAndSendMail(SchedularSetup pSchedularSetup,
string toEmail,List<string> cc, string pdfFileLocation = "")
{
MailSender mailSender = new MailSender();
mailSender.AddTo(toEmail);
cc.ForEach(x =>
{
mailSender.AddCC(x);
});
mailSender.Subject = pSchedularSetup.MailSubject;
mailSender.Body = pSchedularSetup.MailBody.Replace("<<EmployeeName>>", "Sir/Madam");
if (pSchedularSetup.Type == EnumSchedularType.Yesterday_Absent_Notification_Self)
{
mailSender.Body = mailSender.Body.Replace("<<Date>>", DateTime.Today.AddDays(-1).ToLongDateString());
}
if (pdfFileLocation != "")
{
mailSender.AddAttachment(pdfFileLocation);
}
mailSender.SendMail(emailSettings);
}
private void CreateAndSendMailSelfOnError(SchedularSetup pSchedularSetup, string ErrorMsg, List<string> cc, string pdfFileLocation = "")
{
MailSender mailSender = new MailSender();
if (pSchedularSetup.OnErrorMail != null)
{
pSchedularSetup.OnErrorMail.ForEach(x =>
{
mailSender.AddTo(x);
});
}
if (cc != null)
{
cc.ForEach(x =>
{
mailSender.AddCC(x);
});
}
mailSender.Subject = "Error - " + pSchedularSetup.MailSubject;
mailSender.Body = "Looks like an error occured. Details: " + ErrorMsg;
if (pSchedularSetup.Type == EnumSchedularType.Yesterday_Absent_Notification_Self)
{
mailSender.Body = mailSender.Body.Replace("<<Date>>", DateTime.Today.AddDays(-1).ToLongDateString());
mailSender.Subject = mailSender.Subject.Replace("<<Date>>", DateTime.Today.AddDays(-1).ToLongDateString());
}
if (pdfFileLocation != "")
{
mailSender.AddAttachment(pdfFileLocation);
}
mailSender.SendMail(emailSettings);
}
private void CreateAndSendMailSelf(SchedularSetup pSchedularSetup,
Employee emp, List<string> cc, string pdfFileLocation = "")
{
MailSender mailSender = new MailSender();
mailSender.AddTo(emp.EmailAddress);
cc.ForEach(x =>
{
mailSender.AddCC(x);
});
mailSender.Subject = pSchedularSetup.MailSubject;
mailSender.Body = pSchedularSetup.MailBody.Replace("<<EmployeeName>>", emp.Name);
if (pSchedularSetup.Type == EnumSchedularType.Yesterday_Absent_Notification_Self)
{
mailSender.Body = mailSender.Body.Replace("<<Date>>", DateTime.Today.AddDays(-1).ToLongDateString());
mailSender.Subject = mailSender.Subject.Replace("<<Date>>", DateTime.Today.AddDays(-1).ToLongDateString());
}
if (pdfFileLocation != "")
{
mailSender.AddAttachment(pdfFileLocation);
}
mailSender.SendMail(emailSettings);
}
private void CreateAndSendMailForLM(SchedularSetup pSchedularSetup, string toEmail, string empString)
{
try
{
MailSender mailSender = new MailSender();
mailSender.AddTo(toEmail);
mailSender.Subject = pSchedularSetup.MailSubject;
mailSender.Body = pSchedularSetup.MailBody.Replace("<<EmployeeName>>", "Sir/Madam")
.Replace("<<AbsentEmployeeName>>", empString)
.Replace("<<Date>>", DateTime.Today.AddDays(-1).ToLongDateString());
mailSender.SendMail(emailSettings);
}
catch (Exception)
{
throw;
}
}
#endregion
#region functionality for Schedular
#region AnniversaryNofication
public void AnniversaryNofication(SchedularSetup pSchedularSetup)
{
if (waitTheThread(pSchedularSetup.StartAt))
{
List<Employee> employeeList = new EmployeeService().GetAllHREmpsWorkAniversary(mailNotificationProcessConfiguration.PayrollTypeForMailSchedular);
List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
MailNotificationHistory tempMailNotificationHistory = null;
foreach (Employee emp in employeeList)
{
tempMailNotificationHistory = new MailNotificationHistory(emp.EmployeeNo.ToString(), pSchedularSetup.Type);
try
{
if (emp.EmailAddress != null && emp.EmailAddress.Trim() == string.Empty)
{
CreateAndSendMail(pSchedularSetup, emp.EmailAddress);
}
}
catch (Exception ex)
{
string ErrorMsg = ex.Message;
CreateAndSendMailSelfOnError(pSchedularSetup, ErrorMsg, [], String.Empty);
tempMailNotificationHistory.Description = ex.Message;
tempMailNotificationHistory.Status = false;
}
finally
{
oMailNotificationHistoryList.Add(tempMailNotificationHistory);
}
}
if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
{
new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
}
}
}
#endregion
#region BirthdayNofication
public void BirthdayNofication(SchedularSetup pSchedularSetup)
{
if (waitTheThread(pSchedularSetup.StartAt))
{
List<Employee> employeeList = new EmployeeService().GetAllHREmpsBirthday(mailNotificationProcessConfiguration.PayrollTypeForMailSchedular);
List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
MailNotificationHistory tempMailNotificationHistory = null;
foreach (Employee emp in employeeList)
{
tempMailNotificationHistory = new MailNotificationHistory(emp.EmployeeNo.ToString(), pSchedularSetup.Type);
try
{
if (emp.EmailAddress != null && emp.EmailAddress.Trim() == string.Empty)
{
CreateAndSendMail(pSchedularSetup, emp.EmailAddress);
}
}
catch (Exception ex)
{
string ErrorMsg = ex.Message;
CreateAndSendMailSelfOnError(pSchedularSetup, ErrorMsg, [], String.Empty);
tempMailNotificationHistory.Description = ex.Message;
tempMailNotificationHistory.Status = false;
}
finally
{
oMailNotificationHistoryList.Add(tempMailNotificationHistory);
}
}
if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
{
new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
}
}
}
#endregion
#region DailyAttendanceNofication
//public void DailyAttendanceNofication(SchedularSetup pSchedularSetup)
//{
// if (waitTheThread(pSchedularSetup.StartAt))
// {
// byte[] report = null;
// string fileFormate = Enum.GetName(pSchedularSetup.ReportFormat.GetType(), pSchedularSetup.ReportFormat);
// string fileExtention = EnumHelper.GetEnumDescription(pSchedularSetup.ReportFormat);
// MailNotificationHistory tempMailNotificationHistory = null;
// List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
// DateTime fromDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
// DateTime toDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, pSchedularSetup.StartAt.Hour, pSchedularSetup.StartAt.Minute, pSchedularSetup.StartAt.Second);
// if (pSchedularSetup.ToEmailAddress != null && pSchedularSetup.ToEmailAddress.Count > 0)
// {
// foreach (string toEmail in pSchedularSetup.ToEmailAddress)
// {
// try
// {
// tempMailNotificationHistory = new MailNotificationHistory(toEmail, pSchedularSetup.Type);
// report = report == null ? new AttendanceReport().GetDailyAttnReport(fromDate, toDate, EnumAttendanceType.None, mailNotificationProcessConfiguration.PayrollTypeForMailSchedular, fileFormate, true) : report;
// string pdfFileLocation = Directory.GetCurrentDirectory() + "\\CurrentDayAttnReport" + "(" + fromDate.ToString("dd MMM yyyy") + ")" + fileExtention;
// File.WriteAllBytes(pdfFileLocation, report);
// CreateAndSendMail(pSchedularSetup, toEmail, pdfFileLocation);
// }
// catch (Exception ex)
// {
// tempMailNotificationHistory.Description = ex.Message;
// tempMailNotificationHistory.Status = false;
// }
// finally
// {
// oMailNotificationHistoryList.Add(tempMailNotificationHistory);
// }
// }
// }
// if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
// {
// new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
// }
// }
//}
#endregion
#region PreviousDayAttendanceNofication
//public void PreviousDayAttendanceNofication(SchedularSetup pSchedularSetup)
//{
// if (waitTheThread(pSchedularSetup.StartAt))
// {
// byte[] report = null;
// string fileFormate = Enum.GetName(pSchedularSetup.ReportFormat.GetType(), pSchedularSetup.ReportFormat);
// string fileExtention = EnumHelper.GetEnumDescription(pSchedularSetup.ReportFormat);
// MailNotificationHistory tempMailNotificationHistory = null;
// List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
// DateTime fromDate = DateTime.Today.AddDays(-1);
// DateTime toDate = new DateTime(fromDate.Year, fromDate.Month, fromDate.Day, 23, 59, 0);
// if (pSchedularSetup.ToEmailAddress != null && pSchedularSetup.ToEmailAddress.Count > 0)
// {
// foreach (string toEmail in pSchedularSetup.ToEmailAddress)
// {
// try
// {
// tempMailNotificationHistory = new MailNotificationHistory(toEmail, pSchedularSetup.Type);
// report = report == null ? new AttendanceReport().GetDailyAttnReport(fromDate, toDate, EnumAttendanceType.None, mailNotificationProcessConfiguration.PayrollTypeForMailSchedular, fileFormate) : report;
// string pdfFileLocation = Directory.GetCurrentDirectory() + "\\PreviousDayAttnReport" + "(" + fromDate.ToString("dd MMM yyyy") + ")" + fileExtention;
// File.WriteAllBytes(pdfFileLocation, report);
// CreateAndSendMail(pSchedularSetup, toEmail, pdfFileLocation);
// }
// catch (Exception ex)
// {
// tempMailNotificationHistory.Description = ex.Message;
// tempMailNotificationHistory.Status = false;
// }
// finally
// {
// oMailNotificationHistoryList.Add(tempMailNotificationHistory);
// }
// }
// }
// if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
// {
// new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
// }
// }
//}
#endregion
#region PreviousDayAbsentNotification
public void PreviousDayAbsentNotificationForEmp(SchedularSetup pSchedularSetup)
{
List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
try
{
EnumSchedularType notificationtype = pSchedularSetup.Type;
List<Employee> employeeList = new EmployeeService().GetAllAbsentOnYesterdayEmps(mailNotificationProcessConfiguration.PayrollTypeForMailSchedular);
List<Employee> allemps = new EmployeeService().GetAllEmps();
List<EmployeeCordinator> empCord = new EmployeeCordinatorService().Get();
Employee _employeeCordinator =new Employee();
List<string> ccemail = new List<string>();
foreach (Employee emp in employeeList)
{
ccemail = new List<string>();
try
{
#region check CoOrdinator and line manager
EmployeeCordinator cordinatorCheck = empCord.Find(x => x.EmployeeID == emp.ID);
if (cordinatorCheck != null)
{
pSchedularSetup.IsCCCordinator = true;
}
else
{
pSchedularSetup.IsCCCordinator = false;
}
if (emp.LineManagerID != null)
{
pSchedularSetup.IsCCLM = true;
}
else
{
pSchedularSetup.IsCCLM = false;
}
#endregion
if (emp.EmailAddress == "")
{
MailNotificationHistory own = new MailNotificationHistory();
own.Description = "Employee Email address not found";
own.EmployeeNo = emp.EmployeeNo;
own.Status = false;
own.NotificationType = notificationtype;
oMailNotificationHistoryList.Add(own);
continue;
}
if (pSchedularSetup.IsCCLM == true) // line email address not found
{
Employee linemanager = new Employee();
linemanager = allemps.Find(x => x.ID == emp.LineManagerID);
if (linemanager == null)
{
MailNotificationHistory cc = new MailNotificationHistory();
cc.EmployeeNo = emp.EmployeeNo;
cc.NotificationType = notificationtype;
cc.Status = false;
cc.Description = "Line Manager not found for the employee: " + emp.EmployeeNo;
oMailNotificationHistoryList.Add(cc);
}
else
{
if (pSchedularSetup.IsCCLM == true && linemanager.EmailAddress == "")
{
MailNotificationHistory cc = new MailNotificationHistory();
cc.EmployeeNo = emp.EmployeeNo;
cc.NotificationType = notificationtype;
cc.Status = false;
cc.Description = "Line Manager Email address not found for the employee" + emp.EmployeeNo;
oMailNotificationHistoryList.Add(cc);
}
else
{
ccemail.Add(linemanager.EmailAddress);
}
}
}
if (pSchedularSetup.IsCCCordinator == true) // line email address not found
{
MailNotificationHistory crd = new MailNotificationHistory();
EmployeeCordinator coordinator = empCord.Find(x => x.EmployeeID == emp.ID);
if(coordinator == null)
{
crd.Status = false;
crd.NotificationType = notificationtype;
crd.EmployeeNo = emp.EmployeeNo;
crd.Description = "Cordinator not found for themployee:" + emp.EmployeeNo + ' ' + emp.Name;
oMailNotificationHistoryList.Add(crd);
}
else
{
_employeeCordinator.ID = coordinator.CordinatorID;
Employee tempCordinator = allemps.Find(x => x.ID == _employeeCordinator.ID);
if (tempCordinator != null && tempCordinator.EmailAddress == "")
{
crd.Status = false;
crd.NotificationType = notificationtype;
crd.EmployeeNo = tempCordinator.EmployeeNo;
crd.Description = "CoOrdinator " + tempCordinator.EmployeeNo + tempCordinator.Name + "Email address not found";
oMailNotificationHistoryList.Add(crd);
}
else
ccemail.Add(tempCordinator.EmailAddress);
}
//allemps.
}
try
{
CreateAndSendMailSelf(pSchedularSetup, emp, ccemail, String.Empty);
Thread.Sleep(100);
MailNotificationHistory success = new MailNotificationHistory();
success.EmployeeNo = emp.EmployeeNo;
success.Status = true;
success.NotificationType = notificationtype;
success.Description = "Successful for " + emp.EmployeeNo;
oMailNotificationHistoryList.Add(success);
}
catch (Exception ex)
{
string ErrorMsg = ex.Message;
CreateAndSendMailSelfOnError(pSchedularSetup, ErrorMsg, ccemail, String.Empty);
Thread.Sleep(100);
MailNotificationHistory mailFailed = new MailNotificationHistory();
mailFailed.NotificationType = notificationtype;
mailFailed.EmployeeNo = emp.EmployeeNo;
mailFailed.Status = false;
mailFailed.Description = ex.Message.Length > 256 ? ex.Message.Substring(0,256):ex.Message;
oMailNotificationHistoryList.Add(mailFailed);
}
}
catch (Exception ex)
{
MailNotificationHistory mailFailed = new MailNotificationHistory();
mailFailed.EmployeeNo = emp.EmployeeNo;
mailFailed.Description = ex.Message.Length > 256 ? ex.Message.Substring(0, 256) : ex.Message;
mailFailed.NotificationType = notificationtype;
mailFailed.Status = false;
}
//finally
//{
// oMailNotificationHistoryList.Add(tempMailNotificationHistory);
//}
}
if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
{
new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
}
}
catch (Exception ex)
{
pSchedularSetup.Remarks = ex.Message.Length > 256 ? ex.Message.Substring(0, 256) : ex.Message;
}
finally
{
pSchedularSetup.LastExecuteDateTime = DateTime.Now;
SchedularSetupService oSchedularSetupService = new SchedularSetupService();
oSchedularSetupService.Save(pSchedularSetup);
}
}
#endregion
#region MonthlyAttendanceSummaryForLM
public void MonthlyAttendanceSummaryForLM(SchedularSetup pSchedularSetup)
{
if (waitTheThread(pSchedularSetup.StartAt) && (mailNotificationProcessConfiguration.IsTest || DateTime.Today == GlobalFunctions.FirstDateOfMonth(DateTime.Today)))
{
List<MailNotificationHistory> oMailNotificationHistoryList = new();
pSchedularSetup.Type = EnumSchedularType.MonthlyAttendanceSummaryLM;
byte[] report = null;
string fileFormate = Enum.GetName(pSchedularSetup.ReportFormat.GetType(), pSchedularSetup.ReportFormat);
string fileExtention = EnumHelper.GetEnumDescription(pSchedularSetup.ReportFormat);
List<EmployeeCordinator> empCord = new EmployeeCordinatorService().Get();
//string fileExtention = "pdf";
List<Employee> allemps = new EmployeeService().GetAllEmps();
//List<Employee> linemanagers = new List<Employee>();
//allemps.ForEach(x =>{
// if(x.LineManagerID != null )
// {
// if (linemanagers.FindIndex(y => y.ID == (int)x.LineManagerID) != -1)
// linemanagers.Add(x);
// }
//});
foreach (Employee emp in allemps) //Here, Emp is the linemanager/Co Ordinators and _employees are the subordinates!!
{
List<Employee> _employees = new EmployeeService().GetSubordinatesByLineManager(emp.ID);
MailNotificationHistory tempMailNotificationHistory = null;
DateTime fromDate = GlobalFunctions.FirstDateOfMonth(DateTime.Now.AddMonths(-24));
DateTime toDate = GlobalFunctions.LastDateOfMonth(DateTime.Now.AddMonths(-1));
EmployeeCordinator cordinatorCheck = empCord.Find(x => x.CordinatorID == emp.ID);
if (cordinatorCheck != null)
{
if (emp.EmailAddress != null)
{
try
{
tempMailNotificationHistory = new MailNotificationHistory(emp.EmployeeNo, pSchedularSetup.Type);
//report = new AttendanceReport().GetMonthlyAttendanceSummaryCordinator(emp.ID, fromDate, toDate, EnumAttendanceType.None, mailNotificationProcessConfiguration.PayrollTypeForMailSchedular, fileFormate);
string pdfFileLocation = Directory.GetCurrentDirectory() + "\\PreviousMonthAttnReportToCordinator" + "(" + emp.EmployeeNo + ")" + "(" + fromDate.ToString("dd MMM yyyy") + "-" + toDate.ToString("dd MMM yyyy") + ")" + fileExtention;
File.WriteAllBytes(pdfFileLocation, report);
//CreateAndSendMail(pSchedularSetup, emp.EmailAddress, pdfFileLocation);
CreateAndSendMailForMonthlyAttendanceSummary(pSchedularSetup, fromDate, emp.EmailAddress, pdfFileLocation);
MailNotificationHistory emnotfound = new MailNotificationHistory(emp.EmailAddress, pSchedularSetup.Type);
emnotfound.EmployeeNo = emp.EmployeeNo;
emnotfound.Description = "successfully sent monthly attendance report for the Cordinator:" + emp.EmployeeNo + " " + emp.Name + "";
oMailNotificationHistoryList.Add(emnotfound);
}
catch (Exception ex)
{
tempMailNotificationHistory.Description = ex.Message;
tempMailNotificationHistory.Status = false;
oMailNotificationHistoryList.Add(tempMailNotificationHistory);
}
//finally
//{
// oMailNotificationHistoryList.Add(tempMailNotificationHistory);
//}
}
else
{
MailNotificationHistory emnotfound = new MailNotificationHistory(emp.EmailAddress, pSchedularSetup.Type);
emnotfound.EmployeeNo = emp.EmployeeNo;
emnotfound.Description = "Line manager:" + emp.EmployeeNo + " " + emp.Name + " email not found";
oMailNotificationHistoryList.Add(emnotfound);
}
}
if (_employees.Count != 0)
{
if (emp.EmailAddress != null)
{
try
{
tempMailNotificationHistory = new MailNotificationHistory(emp.EmployeeNo, pSchedularSetup.Type);
//report = report == null ? new AttendanceReport().GetMonthlyAttendanceSummary(emp.ID, fromDate, toDate, EnumAttendanceType.None, mailNotificationProcessConfiguration.PayrollTypeForMailSchedular, fileFormate) : report;
//report = new AttendanceReport().GetMonthlyAttendanceSummary(emp.ID, fromDate, toDate, EnumAttendanceType.None, mailNotificationProcessConfiguration.PayrollTypeForMailSchedular, fileFormate);
//string pdfFileLocation = Directory.GetCurrentDirectory() + "\\PreviousMonthAttnReport(Line Manager " + emp.ID + ")" + "(" + fromDate.ToString("dd MMM yyyy") + "-" + toDate.ToString("dd MMM yyyy") + ")" + fileExtention;
string pdfFileLocation = Directory.GetCurrentDirectory() + "\\PreviousMonthAttnReportToLineManager" + "(" + emp.EmployeeNo + ")" + "(" + fromDate.ToString("dd MMM yyyy") + "-" + toDate.ToString("dd MMM yyyy") + ")" + fileExtention;
File.WriteAllBytes(pdfFileLocation, report);
//CreateAndSendMail(pSchedularSetup, emp.EmailAddress, pdfFileLocation);
CreateAndSendMailForMonthlyAttendanceSummary(pSchedularSetup, fromDate, emp.EmailAddress, pdfFileLocation);
MailNotificationHistory emnotfound = new MailNotificationHistory(emp.EmailAddress, pSchedularSetup.Type);
emnotfound.EmployeeNo = emp.EmployeeNo;
emnotfound.Description = "successfully sent monthly attendance report for the Line manager:" + emp.EmployeeNo + " " + emp.Name + "";
oMailNotificationHistoryList.Add(emnotfound);
}
catch (Exception ex)
{
string ErrorMsg = ex.Message;
CreateAndSendMailSelfOnError(pSchedularSetup, ErrorMsg, [], String.Empty);
tempMailNotificationHistory.Description = ex.Message;
tempMailNotificationHistory.Status = false;
oMailNotificationHistoryList.Add(tempMailNotificationHistory);
}
//finally
//{
// oMailNotificationHistoryList.Add(tempMailNotificationHistory);
//}
}
else
{
MailNotificationHistory emnotfound = new MailNotificationHistory(emp.EmailAddress, pSchedularSetup.Type);
emnotfound.EmployeeNo = emp.EmployeeNo;
emnotfound.Description = "Line manager:" + emp.EmployeeNo + " " + emp.Name + " email not found";
oMailNotificationHistoryList.Add(emnotfound);
}
}
}
if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
{
new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
}
}
}
#endregion
#region PreviousDayAbsentNotificationLM
public void PreviousDayAbsentNotificationForLM(SchedularSetup pSchedularSetup)
{
if (waitTheThread(pSchedularSetup.StartAt))
{
List<Employee> employeeList = new EmployeeService().GetAllAbsentOnYesterdayEmps(mailNotificationProcessConfiguration.PayrollTypeForMailSchedular);
List<Employee> allemp = new EmployeeService().GetAllEmps();
List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
MailNotificationHistory tempMailNotificationHistory = null;
foreach (Employee emp in employeeList)
{
if (emp.LineManagerID != null)
{
Employee lm = allemp.FirstOrDefault(x => x.ID == emp.ID); //new EmployeeService().GetAllAbsentOnYesterdayEmpLM((int)emp.LineManagerID, mailNotificationProcessConfiguration.PayrollTypeForMailSchedular);
tempMailNotificationHistory = new MailNotificationHistory(lm.EmployeeNo.ToString(), pSchedularSetup.Type);
string EmpString = emp.Name + " [" + emp.EmployeeNo + "]";
try
{
if (lm.EmailAddress != null && lm.EmailAddress.Trim() != string.Empty)
{
CreateAndSendMailForLM(pSchedularSetup, lm.EmailAddress, EmpString);
} else
{
tempMailNotificationHistory.Description = "Email address not found for line manager";
tempMailNotificationHistory.Status = false;
}
}
catch (Exception ex)
{
string ErrorMsg = ex.Message;
CreateAndSendMailSelfOnError(pSchedularSetup, ErrorMsg, [], String.Empty);
tempMailNotificationHistory.Description = ex.Message;
tempMailNotificationHistory.Status = false;
}
finally
{
oMailNotificationHistoryList.Add(tempMailNotificationHistory);
}
}
else
{
tempMailNotificationHistory = new MailNotificationHistory(emp.EmployeeNo.ToString(), pSchedularSetup.Type);
tempMailNotificationHistory.Description = "Line manager not found";
tempMailNotificationHistory.Status = false;
oMailNotificationHistoryList.Add(tempMailNotificationHistory);
}
}
if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
{
new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
}
}
}
#endregion
#region PreviousWeekAbsentNotification
//public void PreviousWeekAttendanceNotification(SchedularSetup pSchedularSetup)
//{
// if (waitTheThread(pSchedularSetup.StartAt) && (mailNotificationProcessConfiguration.IsTest || DateTime.Today.DayOfWeek == DayOfWeek.Sunday))
// {
// byte[] report = null;
// string fileFormate = Enum.GetName(pSchedularSetup.ReportFormat.GetType(), pSchedularSetup.ReportFormat);
// string fileExtention = EnumHelper.GetEnumDescription(pSchedularSetup.ReportFormat);
// MailNotificationHistory tempMailNotificationHistory = null;
// List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
// //DateTime fromDate = DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek - 7); // previous week Sunday
// //DateTime toDate = DateTime.Now.AddDays((int)DateTime.Now.DayOfWeek - 7); // recent Sturday
// // as it will fire on Sunday
// DateTime fromDate = DateTime.Now.AddDays(-7); // Sunday
// DateTime toDate = DateTime.Now.AddDays(-1); // recent Sturday
// if (pSchedularSetup.ToEmailAddress != null && pSchedularSetup.ToEmailAddress.Count > 0)
// {
// foreach (string toEmail in pSchedularSetup.ToEmailAddress)
// {
// try
// {
// tempMailNotificationHistory = new MailNotificationHistory(toEmail, pSchedularSetup.Type);
// report = report == null ? new AttendanceReport().GetWeeklyAttnReport(fromDate, toDate, EnumAttendanceType.None, mailNotificationProcessConfiguration.PayrollTypeForMailSchedular, fileFormate) : report;
// string pdfFileLocation = Directory.GetCurrentDirectory() + "\\PreviousWeekAttnReport" + "(" + fromDate.ToString("dd MMM yyyy") + "-" + toDate.ToString("dd MMM yyyy") + ")" + fileExtention;
// File.WriteAllBytes(pdfFileLocation, report);
// CreateAndSendMail(pSchedularSetup, toEmail, pdfFileLocation);
// }
// catch (Exception ex)
// {
// tempMailNotificationHistory.Description = ex.Message;
// tempMailNotificationHistory.Status = false;
// }
// finally
// {
// oMailNotificationHistoryList.Add(tempMailNotificationHistory);
// }
// }
// }
// if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
// {
// new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
// }
// }
//}
#endregion
#region PreviousMonthAbsentNotification
//public void PreviousMonthAttendanceNotification(SchedularSetup pSchedularSetup)
//{
// if (waitTheThread(pSchedularSetup.StartAt) && (mailNotificationProcessConfiguration.IsTest || DateTime.Today == GlobalFunctions.FirstDateOfMonth(DateTime.Today)))
// {
// byte[] report = null;
// string fileFormate = Enum.GetName(pSchedularSetup.ReportFormat.GetType(), pSchedularSetup.ReportFormat);
// string fileExtention = EnumHelper.GetEnumDescription(pSchedularSetup.ReportFormat);
// MailNotificationHistory tempMailNotificationHistory = null;
// List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
// DateTime fromDate = GlobalFunctions.FirstDateOfMonth(DateTime.Now.AddMonths(-1));
// DateTime toDate = GlobalFunctions.LastDateOfMonth(DateTime.Now.AddMonths(-1));
// if (pSchedularSetup.ToEmailAddress != null && pSchedularSetup.ToEmailAddress.Count > 0)
// {
// foreach (string toEmail in pSchedularSetup.ToEmailAddress)
// {
// try
// {
// tempMailNotificationHistory = new MailNotificationHistory(toEmail, pSchedularSetup.Type);
// report = report == null ? new AttendanceReport().GetMonthlyAttnReport(fromDate, toDate, EnumAttendanceType.None, mailNotificationProcessConfiguration.PayrollTypeForMailSchedular, fileFormate) : report;
// string pdfFileLocation = Directory.GetCurrentDirectory() + "\\PreviousMonthAttnReport" + "(" + fromDate.ToString("dd MMM yyyy") + "-" + toDate.ToString("dd MMM yyyy") + ")" + fileExtention;
// File.WriteAllBytes(pdfFileLocation, report);
// CreateAndSendMail(pSchedularSetup, toEmail, pdfFileLocation);
// }
// catch (Exception ex)
// {
// tempMailNotificationHistory.Description = ex.Message;
// tempMailNotificationHistory.Status = false;
// }
// finally
// {
// oMailNotificationHistoryList.Add(tempMailNotificationHistory);
// }
// }
// }
// if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
// {
// new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
// }
// }
//}
#endregion
#region PendingListNotification
public void PendingListNotification(SchedularSetup pSchedularSetup)
{
if (waitTheThread(pSchedularSetup.StartAt) && (mailNotificationProcessConfiguration.IsTest || DateTime.Today.DayOfWeek == DayOfWeek.Sunday))
{
DataSet linemanagerPendingList = new EmployeeService().GetLinemanagerPendingList();
List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
MailNotificationHistory tempMailNotificationHistory = null;
if (linemanagerPendingList != null &&
linemanagerPendingList.Tables != null &&
linemanagerPendingList.Tables.Count > 0 &&
linemanagerPendingList.Tables[0].Rows.Count > 0)
{
foreach (DataRow emp in linemanagerPendingList.Tables[0].Rows)
{
tempMailNotificationHistory = new MailNotificationHistory(emp["EMPLOYEENO"].ToString(), pSchedularSetup.Type);
try
{
CreateAndSendMail(pSchedularSetup, emp["EMAILADDRESS"].ToString());
}
catch (Exception ex)
{
string ErrorMsg = ex.Message;
CreateAndSendMailSelfOnError(pSchedularSetup, ErrorMsg, [], String.Empty);
tempMailNotificationHistory.Description = ex.Message;
tempMailNotificationHistory.Status = false;
}
finally
{
oMailNotificationHistoryList.Add(tempMailNotificationHistory);
}
}
}
if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
{
new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
}
}
}
#endregion
#region PreviousDayApprovedLeaveNofication
public void PreviousDayApprovedLeaveNofication(SchedularSetup pSchedularSetup)
{
if (waitTheThread(pSchedularSetup.StartAt))
{
byte[] report = null;
string fileFormate = Enum.GetName(pSchedularSetup.ReportFormat.GetType(), pSchedularSetup.ReportFormat);
string fileExtention = EnumHelper.GetEnumDescription(pSchedularSetup.ReportFormat);
MailNotificationHistory tempMailNotificationHistory = null;
List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
DateTime leaveApprovedDate = DateTime.Today.AddDays(-1);
if (pSchedularSetup.ToEmailAddress != null && pSchedularSetup.ToEmailAddress.Count > 0)
{
foreach (string toEmail in pSchedularSetup.ToEmailAddress)
{
try
{
tempMailNotificationHistory = new MailNotificationHistory(toEmail, pSchedularSetup.Type);
report = report == null ? new rptLeave().GetPreviousDayApprovedLeaveReport(toEmail, leaveApprovedDate, EnumLeaveStatus.Approved, fileFormate) : report;
string pdfFileLocation = Directory.GetCurrentDirectory() + "\\PreviousDayApprovedLeaveReport" + "(" + leaveApprovedDate.ToString("dd MMM yyyy") + ")" + fileExtention;
File.WriteAllBytes(pdfFileLocation, report);
CreateAndSendMail(pSchedularSetup, toEmail, pdfFileLocation);
}
catch (Exception ex)
{
string ErrorMsg = ex.Message;
CreateAndSendMailSelfOnError(pSchedularSetup, ErrorMsg, [], String.Empty);
tempMailNotificationHistory.Description = ex.Message;
tempMailNotificationHistory.Status = false;
}
finally
{
oMailNotificationHistoryList.Add(tempMailNotificationHistory);
}
}
}
if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
{
new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
}
}
}
#endregion
#region Attendance Auto Process
public void AttendanceAutoProcess(SchedularSetup pSchedularSetup)
{
EnumSchedularType notificationtype = pSchedularSetup.Type;
AttendanceAutoProcessConfiguration oAttendanceAutoProcessConfiguration = new AttendanceAutoProcessConfiguration();
List<MailNotificationHistory> oMailNotificationHistoryList = new List<MailNotificationHistory>();
//StreamWriter ofile = null;
//string basepath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//string loc = basepath + "\\log.txt";
//if (!File.Exists(loc))
//{
// File.Create(loc).Dispose();
//}
var configurationBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
configurationBuilder.GetSection("AttendanceAutoProcess").Bind(oAttendanceAutoProcessConfiguration);
// List<PayrollType> oPtypes = new PayrollTypeService().Get();
string[] ptypeid = mailNotificationProcessConfiguration.PayrollTypeForMailSchedular.Split(",");
foreach (string ptype in ptypeid)
{
try
{
new AttendanceProcess().Process(DateTime.Today, EnumProcessMode.Auto, Convert.ToInt32(ptype), 1);
Thread.Sleep(5000);
MailNotificationHistory success = new MailNotificationHistory();
success.Description = "Date & Time : " + DateTime.Now.ToString() + ", Attendence Process Completed Successfully For " + DateTime.Today.AddDays(-oAttendanceAutoProcessConfiguration.NumberOfDaysToProcess).Date.ToString("dd MMM yyyy") + ".";
success.NotificationType = notificationtype;
success.Status = true;
oMailNotificationHistoryList.Add(success);
}
catch (Exception ex)
{
string ErrorMsg = ex.Message;
CreateAndSendMailSelfOnError(pSchedularSetup, ErrorMsg, [], String.Empty);
MailNotificationHistory failed = new MailNotificationHistory();
failed.Description = "Date & Time : " + DateTime.Now.ToString() + ", Error While Processing Attendence Data For " + DateTime.Today.AddDays(-oAttendanceAutoProcessConfiguration.NumberOfDaysToProcess).Date.ToString("dd MMM yyyy") + ". Reason: " + ex.Message;
failed.NotificationType = notificationtype;
failed.Status = false;
oMailNotificationHistoryList.Add(failed);
}
}
if (oMailNotificationHistoryList != null && oMailNotificationHistoryList.Count > 0)
{
new SchedularSetupService().SaveMailHistory(oMailNotificationHistoryList);
}
pSchedularSetup.LastExecuteDateTime = DateTime.Now;
SchedularSetupService oSchedularSetupService = new SchedularSetupService();
oSchedularSetupService.Save(pSchedularSetup);
}
#endregion
#endregion
}
}