CEL_Payroll/Payroll.BO/Process/NotificationProcess.cs

477 lines
18 KiB
C#
Raw Normal View History

2024-09-17 14:30:13 +06:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Ease.CoreV35.Model;
namespace Payroll.BO
{
[Serializable]
public class NotificationProcess
{
#region Declaration
private ObjectsTemplate<Notification> oCurrentNotifications;
private FileStream oStream=null;
private LetterTemplte letterTemplte = null;
private String outputDir = Application.StartupPath;
private HtmlRichTextBoxBo txtBody = new HtmlRichTextBoxBo();
private String sHtml;
private static bool IsFinished = true;
private static Thread thread = null;
#endregion
#region Constuctor
public NotificationProcess()
{
IsFinished = false;
oCurrentNotifications = Notification.Get(EnumNotificationStatus.InProcess);
}
#endregion
#region Thread Functions
public static void StartThread()
{
if (!IsFinished) return;
NotificationProcess nProcess = new NotificationProcess();
IsFinished = false;
thread = new Thread(nProcess.ProcessStart);
thread.Priority = ThreadPriority.BelowNormal;
thread.Start();
}
public static void EndThread()
{
if (thread != null)
{
IsFinished = true;
thread.Join();
}
}
private void ProcessStart()
{
while (!IsFinished)
{
Process(new Object());
Thread.Sleep((60000*5));
}
}
#endregion
#region Public Functions
public void Process(object stateObject)
{
DateTime currentDateTime = DateTime.Today;
currentDateTime = currentDateTime.AddDays(1);
currentDateTime = currentDateTime.AddSeconds(-1);
foreach(Notification currentNotification in oCurrentNotifications)
{
letterTemplte = null;
oStream = null;
if (currentNotification.TagOutputID.Integer != 0)
{
List<LetterTemplte> letterTempltes = LetterTemplte.Get();
letterTemplte = letterTempltes.Where(r => r.ID.Integer == currentNotification.TagOutputID.Integer).SingleOrDefault();
letterTemplte = letterTemplte.Get(letterTemplte.ID);
oStream = (FileStream)letterTemplte.Body;
}
List<NotificationRule> sendingSMSRules = currentNotification.NotificationRules.Where(e => e.SendDate <= currentDateTime && e.SMSSendStatus ==EnumSendStatus.NotSent).ToList();
List<NotificationRule> sendingEmailRules = currentNotification.NotificationRules.Where(e => e.SendDate <= currentDateTime && e.EmailSendStatus == EnumSendStatus.NotSent).ToList();
if (sendingSMSRules.Count > 0) ProcessSMS(currentNotification, sendingSMSRules);
if (sendingEmailRules.Count > 0) ProcessEmail(currentNotification, sendingEmailRules);
//ProcessWeb(currentNotification, sendingWebRules);
Notification oNotification = Notification.Get(currentNotification.ID);
if (!oNotification.NotificationRules
.Where(ob =>
ob.SMSSendStatus == EnumSendStatus.NotSent ||
ob.EmailSendStatus == EnumSendStatus.NotSent||
ob.WebSendStatus == EnumSendStatus.NotSent)
.Any())
{
oNotification.Status = EnumNotificationStatus.Closed;
oNotification.Save();
}
}
}
//public void Process(object stateObject,out string outputString)
//{
// DateTime currentDateTime = DateTime.Today;
// currentDateTime = currentDateTime.AddDays(1);
// currentDateTime = currentDateTime.AddSeconds(-1);
// foreach (Notification currentNotification in oCurrentNotifications)
// {
// if (currentNotification.TagOutputID.Integer != 0)
// {
// List<LetterTemplte> letterTempltes = LetterTemplte.Get();
// letterTemplte = letterTempltes.Where(r => r.ID.Integer == currentNotification.TagOutputID.Integer).SingleOrDefault();
// letterTemplte = letterTemplte.Get(letterTemplte.ID);
// oStream = (FileStream)letterTemplte.Body;
// }
// List<NotificationRule> sendingSMSRules = currentNotification.NotificationRules.Where(e => e.SendDate <= currentDateTime && e.SMSSendStatus == EnumSendStatus.NotSent).ToList();
// List<NotificationRule> sendingEmailRules = currentNotification.NotificationRules.Where(e => e.SendDate <= currentDateTime && e.EmailSendStatus == EnumSendStatus.NotSent).ToList();
// //List<NotificationRule> sendingWebRules = currentNotification.NotificationRules.Where(e => e.SendDate <= currentDateTime && e.WebSendStatus == EnumSendStatus.NotSent && currentNotification.IsWebNf).ToList();
// if (sendingSMSRules.Count > 0) ProcessSMS(currentNotification, sendingSMSRules);
// if (sendingEmailRules.Count > 0) ProcessEmail(currentNotification, sendingEmailRules);
// //ProcessWeb(currentNotification, sendingWebRules);
// Notification oNotification = Notification.Get(currentNotification.ID);
// if (!oNotification.NotificationRules
// .Where(ob =>
// ob.SMSSendStatus == EnumSendStatus.NotSent ||
// ob.EmailSendStatus == EnumSendStatus.NotSent ||
// ob.WebSendStatus == EnumSendStatus.NotSent).Any())
// {
// oNotification.Status = EnumNotificationStatus.Closed;
// oNotification.Save();
// }
// }
// outputString = string.Empty;
//}
#endregion
#region Private Functions
private void GenerateFromTagOutput(NotificationRule notifyRule, Notification notification)
{
txtBody = new HtmlRichTextBoxBo();
LetterTemplte otmp = new LetterTemplte();
otmp = letterTemplte;
try
{
if (letterTemplte.Body != null && letterTemplte.FilePath == string.Empty)
{
string strPath = otmp.Generate(notification.TagOutputID, notification.ObjectID, notification.OptionalObjectID, notifyRule.PerticipantID, oStream);
txtBody.LoadFile(strPath);
}
else
{
string strPath = otmp.Generate(notification.TagOutputID, notification.ObjectID, notification.OptionalObjectID, notifyRule.PerticipantID, letterTemplte.FilePath);
txtBody.LoadFile(strPath);
}
}
catch (Exception ex)
{
throw new Exception("Failed to generate From Tag Output due to"+ex.Message);
}
}
private void ProcessEmail(Notification currentNotification, List<NotificationRule> sendingEmailRules)
{
foreach (NotificationRule emailRule in sendingEmailRules)
{
try
{
if (letterTemplte != null)
{
GenerateFromTagOutput(emailRule, currentNotification);
sHtml = txtBody.GetHTML(true, true);
}
else
{
sHtml = currentNotification.Body;
}
SendEMail(emailRule, currentNotification);
}
catch (MailSenderException ex)
{
if (ex.InnerException != null)
{
Notification.ChangeNotificationRuleRemarks(emailRule , ex.InnerException.Message , EnumNotificationMedium.SMS);
}
else
{
Notification.ChangeNotificationRuleRemarks(emailRule , ex.Message , EnumNotificationMedium.SMS);
}
continue;
}
catch (Exception ex)
{
if (ex.InnerException != null)
{
Notification.ChangeNotificationRuleRemarks(emailRule, ex.InnerException.Message, EnumNotificationMedium.SMS);
}
else
{
Notification.ChangeNotificationRuleRemarks(emailRule, ex.Message, EnumNotificationMedium.SMS);
}
continue;
}
}
}
private void ProcessSMS(Notification currentNotification, List<NotificationRule> sendingSMSRules)
{
foreach (NotificationRule smsRule in sendingSMSRules)
{
try
{
if (letterTemplte != null)
{
GenerateFromTagOutput(smsRule, currentNotification);
sHtml = txtBody.Text.Replace("\n", " ");
}
else
{
txtBody.Clear();
txtBody.AddHTML(currentNotification.Body);
sHtml = txtBody.Text.Replace("\n", " ");
}
SendSMS(smsRule, currentNotification);
}
catch (Exception ex)
{
if (ex.InnerException != null)
{
Notification.ChangeNotificationRuleRemarks(smsRule, ex.InnerException.Message, EnumNotificationMedium.SMS);
}
else
{
Notification.ChangeNotificationRuleRemarks(smsRule, ex.Message, EnumNotificationMedium.SMS);
}
continue;
}
}
}
private void SendEMail(NotificationRule emailRule, Notification currentNotification)
{
Employee tempEmp;
Vendor tempVendor;
CV tempParticipant;
EnumSendMail oSendMailNotification = EnumSendMail.None;
MailSender mailSender = new MailSender();
switch (emailRule.PerticipantType)
{
case EnumPerticipantType.Employee:
tempEmp = Employee.Get(emailRule.PerticipantID);
mailSender.To = tempEmp.EmailAddress;
break;
case EnumPerticipantType.Vendor:
tempVendor = Vendor.Get(emailRule.PerticipantID);
mailSender.To = tempVendor.EmailAddress;
break;
case EnumPerticipantType.Applicant:
tempParticipant = CV.Get(emailRule.PerticipantID);
mailSender.To = tempParticipant.EmailAddress;
break;
default:
break;
}
// This will be closed alternate to rhe switch
//mailSender.To = "hassan.imroze@gmail.com";
mailSender.Body = sHtml + currentNotification.Footer + emailRule.ExtendedBody;
mailSender.Subject = currentNotification.Subject;
if (currentNotification.Attachment.Trim() != string.Empty)
{
mailSender.AddAttachment(currentNotification.Attachment);
}
if (mailSender.To != null && mailSender.To.Trim() != string.Empty)
{
mailSender.SendMail();
Notification.ChangeNotificationRuleStatus(emailRule, EnumSendStatus.Sent, EnumNotificationMedium.Email);
}
}
private void SendSMS(NotificationRule smsRule, Notification currentNotification)
{
Employee tempEmp;
Vendor tempVendor;
CV tempParticipant;
SMSSender smsSender = new SMSSender();
switch (smsRule.PerticipantType)
{
case EnumPerticipantType.Employee:
tempEmp = Employee.Get(smsRule.PerticipantID);
//smsSender.CellNumber = tempEmp.MobileNo;
break;
case EnumPerticipantType.Vendor:
tempVendor = Vendor.Get(smsRule.PerticipantID);
//smsSender.CellNumber = tempVendor.Mobile;
break;
case EnumPerticipantType.Applicant:
tempParticipant = CV.Get(smsRule.PerticipantID);
//smsSender.CellNumber = tempParticipant.MobileNo;
break;
default:
break;
}
//This will be closed alternate to the switch
smsSender.CellNumber = "01834837704";
smsSender.SMSMessage = sHtml + currentNotification.Footer + smsRule.ExtendedBody;
//will be open after test
//smsSender.SendSMS();
Notification.ChangeNotificationRuleStatus(smsRule, EnumSendStatus.Sent, EnumNotificationMedium.SMS);
}
#endregion
//private void GenerateFromTagOutput(NotificationRule notifyRule, Notification notification)
//{
// MSWord file = new MSWord();
// txtBody = new HtmlRichTextBoxBo();
// Employee oEmp = Employee.Get(notifyRule.PerticipantID);
// LetterTemplte otmp = new LetterTemplte();
// otmp = letterTemplte;
// try
// {
// if (letterTemplte.Body != null && letterTemplte.FilePath == string.Empty)//notification.TagOutputID.Integer >= 5)
// {
// Hashtable table = new Hashtable();
// file.OriginalFile = oStream.Name;
// FileInfo ossInfo = null;
// PhotoPath oPhotoPath = PhotoPath.Get(ID.FromInteger(1));
// string sgenFilePath = oPhotoPath.LetterTempPath + "\\Generated\\" + oEmp.EmployeeNo + "-" + notification.Subject + ".doc";
// ossInfo = new FileInfo(sgenFilePath);
// table = otmp.RefreshObject(oEmp);
// if (ossInfo.Exists)
// {
// ossInfo.Delete();
// }
// if (table != null)
// {
// File.Copy(oStream.Name, sgenFilePath, true);
// file = new MSWord();
// file.OpenWordApplication();
// file.OriginalFile = oStream.Name;
// file.ReplaceWords(sgenFilePath, table);
// file.CloseWordApplication();
// }
// txtBody.LoadFile(sgenFilePath);
// //MessageBox.Show("File(s) created successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// }
// else
// {
// string strPath = otmp.Generate(notification.TagOutputID, notification.ObjectID.Integer, notification.OptionalObjectID.Integer, oEmp.ID, "");
// txtBody.LoadFile(strPath);
// //MessageBox.Show("File(s) created successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// }
// }
// catch (Exception ex)
// {
// file.CloseWordApplication();
// throw new Exception(ex.Message);
// //MessageBox.Show("Fail to create file. Because " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// //Console.WriteLine("Fail to create file. Because " + ex.Message);
// }
//}
//#region Process Web
//private void ProcessWeb(Notification currentNotification, List<NotificationRule> sendingWebRules)
//{
// if (currentNotification.TagOutputID.Integer != 0)
// {
// List<LetterTemplte> letterTempltes = LetterTemplte.Get();
// letterTemplte = letterTempltes.Where(r => r.ID.Integer == currentNotification.TagOutputID.Integer).SingleOrDefault();
// letterTemplte = letterTemplte.Get(letterTemplte.ID);
// oStream = (FileStream)letterTemplte.Body;
// }
// foreach (NotificationRule webRule in sendingWebRules)
// {
// if (currentNotification.TagOutputID.Integer != 0)
// {
// GenerateFromTagOutput(webRule,currentNotification);
// sHtml = txtBody.GetHTML(true, true);
// }
// else
// {
// sHtml = currentNotification.Body;
// }
// SendWeb(webRule, currentNotification);
// }
//}
//private void SendWeb(NotificationRule webRule, Notification currentNotification)
//{
// Employee tempEmp;
// Vendor tempVendor;
// CV tempParticipant;
// try
// {
// switch (webRule.PerticipantType)
// {
// case EnumPerticipantType.Employee:
// tempEmp = Employee.Get(webRule.PerticipantID);
// //smsSender.CellNumber = tempEmp.MobileNo;
// break;
// case EnumPerticipantType.Vendor:
// tempVendor = Vendor.Get(webRule.PerticipantID);
// //smsSender.CellNumber = tempVendor.Mobile;
// break;
// case EnumPerticipantType.Applicant:
// tempParticipant = CV.Get(webRule.PerticipantID);
// //smsSender.CellNumber = tempParticipant.MobileNo;
// break;
// default:
// break;
// }
// Notification.ChangeNotificationRuleStatus(webRule, EnumSendStatus.Sent, EnumNotificationMedium.WEB);
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
//}
//#endregion
}
}