EchoTex_Payroll/HRM.DA/Service/Workflow/WorkFlowEng.cs

906 lines
40 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
using Ease.Core.DataAccess;
using HRM.BO;
using HRM.BO.Configuration;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
namespace HRM.DA
{
// do not change any code discuss shamim
public class WFManager<T> where T : IworkflowInterface
{
#region class variable
private Employee _employee = null;
private WFRule _wfSetup = null;
private WFMovementTran _MoveTran = null;
private T _wfinterface;
private object _sourceObject;
private WFType _wfType = null;
#endregion class variable
public WFManager()
{
}
public WFManager(object sourceObject)
{
this._sourceObject = sourceObject;
}
#region constructor
private T ConvertToT<T>(IworkflowInterface oTran) where T : IworkflowInterface
{
return (T)oTran;
}
#endregion constructor
public WFRule InitiatorRuleSelection(TransactionContext tc, int employeeid, WFType wftype)
{
List<WFRule> moduleRules = new WFRuleService().GetRules(tc, wftype.ID);
List<WFRule> selectedRules = new List<WFRule>();
List<WFRule.WFStep.WFStepActor> otherPositionTypeActors = new List<WFRule.WFStep.WFStepActor>();
foreach (WFRule orule in moduleRules)
{
int stepNo = orule.Steps.Select(x => x.StepNo).Min();
WFRule.WFStep step = orule.Steps.FirstOrDefault(x => x.StepNo == stepNo);
foreach (WFRule.WFStep.WFStepActor actor in step.Actors)
{
switch (actor.WFActorType)
{
case EnumWFActorType.AnyEmployee:
selectedRules.Add(orule);
break;
case EnumWFActorType.LineManager:
if (_employee.LineManagerID != null)
{
actor.ObjectID = (int)_employee.LineManagerID;
selectedRules.Add(orule);
}
break;
case EnumWFActorType.Employee:
if (actor.ObjectID == employeeid)
selectedRules.Add(orule);
break;
case EnumWFActorType.OrganogramNode:
break;
case EnumWFActorType.PositionType:
{
actor.ObjectID = 0;
List<Employee> emps = new List<Employee>();
if (emps.Count > 1)
{
for (int i = 0; i < emps.Count; i++)
{
if (emps[i].DepartmentID == _employee.DepartmentID)
{
actor.ObjectID = emps[i].ID;
break;
}
}
if (actor.ObjectID == 0)
throw new Exception("Didn't find Position type approver");
}
else if (emps.Count == 1)
actor.ObjectID = emps[0].ID;
else throw new Exception("Didn't find Position type approver");
}
break;
case EnumWFActorType.ListField:
// string objid = this.GetListObjectValue(actor.ListFieldName, 0);
// if (objid != "") actor.ObjectID = Convert.ToInt32(objid);
break;
default:
break;
}
}
}
WFRule sorule = null;
if (selectedRules.Count > 0)
{
int weight = selectedRules.Max(x => x.Weightage);
sorule = selectedRules.First(x => x.Weightage == weight);
}
return sorule;
}
private void Actionresult(WFRule.WFStep ostep, ref bool Iscomplete, ref int gotoStep, ref int recomendationEmpID)
{
Iscomplete = false;
gotoStep = 0;
recomendationEmpID = 0;
if (ostep.Action == EnumwfLogicAction.Complete)
Iscomplete = true;
if (ostep.Action == EnumwfLogicAction.GoToStep)
gotoStep = ostep.GoToStepNo;
if (ostep.Action == EnumwfLogicAction.Recomendation)
{
recomendationEmpID = (int)ostep.RecomendationEmpID;
}
}
public void ExecuteLogic(WFRule orule, int employeeid, int Stepindex,
ref bool Iscomplete, ref int gotoStep, ref int recomendationEmpID)
{
if (orule.Steps.Count == Stepindex)
{
Iscomplete = true;
return;
}
WFRule.WFStep ostep = orule.Steps[Stepindex];
if (ostep == null) throw new Exception((" Er:" + "Invalid Step No"));
if (ostep.Amount != 0)
{
double listAmount = 0;
switch (ostep.Operator)
{
case EnumwfLogicOperator.No_Operator:
break;
case EnumwfLogicOperator.Equal:
if (listAmount == ostep.Amount && ostep.Action == EnumwfLogicAction.Complete)
this.Actionresult(ostep, ref Iscomplete, ref gotoStep, ref recomendationEmpID);
break;
case EnumwfLogicOperator.Greater:
if (listAmount > ostep.Amount && ostep.Action == EnumwfLogicAction.Complete)
this.Actionresult(ostep, ref Iscomplete, ref gotoStep, ref recomendationEmpID);
break;
case EnumwfLogicOperator.Less:
if (listAmount < ostep.Amount && ostep.Action == EnumwfLogicAction.Complete)
this.Actionresult(ostep, ref Iscomplete, ref gotoStep, ref recomendationEmpID);
break;
case EnumwfLogicOperator.LessOrEqual:
if (listAmount <= ostep.Amount && ostep.Action == EnumwfLogicAction.Complete)
this.Actionresult(ostep, ref Iscomplete, ref gotoStep, ref recomendationEmpID);
break;
case EnumwfLogicOperator.Not_Null:
{
object val = null;
if (val != null && ostep.Action == EnumwfLogicAction.Complete)
this.Actionresult(ostep, ref Iscomplete, ref gotoStep, ref recomendationEmpID);
break;
}
case EnumwfLogicOperator.Null:
{
object val = null;
if (val == null && ostep.Action == EnumwfLogicAction.Complete)
this.Actionresult(ostep, ref Iscomplete, ref gotoStep, ref recomendationEmpID);
break;
}
default:
break;
}
}
}
public List<WFRule.WFStep.WFStepActor> GetApprovers(TransactionContext tc, WFRule orule, int employeeid, int stepIndex)
{
List<WFRule.WFStep.WFStepActor> approvers = new List<WFRule.WFStep.WFStepActor>();
WFRule.WFStep ostep = orule.Steps[stepIndex];
foreach (WFRule.WFStep.WFStepActor actor in ostep.Actors)
{
switch (actor.WFActorType)
{
case EnumWFActorType.AnyEmployee:
throw new Exception((" Er:" + "Approver can't be Any-Employee"));
case EnumWFActorType.LineManager:
if (_employee.LineManagerID != null)
{
actor.ObjectID = Convert.ToInt32(_employee.LineManagerID);
approvers.Add(actor);
}
else throw new Exception(("Line manager not defined in the system, please contact to system Administrator"));
break;
case EnumWFActorType.Employee:
approvers.Add(actor);
break;
case EnumWFActorType.OrganogramNode:
break;
case EnumWFActorType.PositionType:
switch ((EnumOGPositionType)actor.ObjectID)
{
case EnumOGPositionType.CEO:
List<OrganogramBasicTemp> CEOtemp = new OrganogramEmployeeService().getTopHiararchy(tc, employeeid);
if (CEOtemp == null || CEOtemp.Count == 0) throw new Exception("CEO not found in the organogram");
OrganogramBasicTemp ceohead = CEOtemp.FirstOrDefault(x => x.positionType == EnumOGPositionType.CEO);
if (ceohead == null) throw new Exception("CEO not found for the Organogram");
if (ceohead.EmployeeID == null)
throw new Exception("CEO Node/Post is vacant");
else
{
actor.ObjectID = Convert.ToInt32(ceohead.EmployeeID);
approvers.Add(actor);
}
break;
case EnumOGPositionType.HOHR:
List<OrganogramEmployee> ogemps = new OrganogramEmployeeService().GetByPositionType(tc, EnumOGPositionType.HOHR);
if (ogemps == null || ogemps.Count == 0)
throw new Exception("'Head of HR' not found for the Organogram");
OrganogramEmployee ogemp = ogemps[0];
actor.ObjectID = Convert.ToInt32(ogemp.EmployeeID);
approvers.Add(actor);
break;
case EnumOGPositionType.DH:
List<OrganogramBasicTemp> temp = new OrganogramEmployeeService().getTopHiararchy(tc, employeeid);
if (temp == null || temp.Count == 0) throw new Exception("Department Head not found for the initiated employee");
OrganogramBasicTemp head = temp.FirstOrDefault(x => x.positionType == EnumOGPositionType.DH);
if (head == null) throw new Exception("Department Head not found for the initiated employee");
if (head.EmployeeID == null)
throw new Exception("Department Head Post/Node is Vacant");
else
{
actor.ObjectID = Convert.ToInt32(head.EmployeeID);
approvers.Add(actor);
}
break;
case EnumOGPositionType.BM:
break;
case EnumOGPositionType.Man_COM:
break;
case EnumOGPositionType.Others:
break;
case EnumOGPositionType.Trusty:
break;
default:
break;
}
break;
case EnumWFActorType.ListField:
var appid = new WFMovementTranService().GetListFieldApprover(tc, orule.WFTypeID, _MoveTran.ObjectID, actor.ListFieldName);
if(appid ==null)
{
throw new Exception("Failed to get approver from the source");
}
actor.ObjectID = Convert.ToInt32(appid);
approvers.Add(actor);
break;
default:
break;
}
}
return approvers;
}
/// <summary>
///
/// </summary>
/// <param name="tc">connection string</param>
/// <param name="empid">initiator employee-id</param>
/// <param name="wftypeid">workflow type id</param>
/// <param name="objectID">object primary key</param>
/// <param name="sRemarks">Next approver notification, optional</param>
/// <param name="subject"> mail body</param>
// do not change following function code without discuss shamim
public void InitiateProcess(TransactionContext tc, int empid, int wftypeid, int objectID, string sRemarks, string subject)
{
// do not change following function code without discuss shamim
try
{
WFRule oRule = null;
WFType owftype = new WFTypeService().Get(tc, wftypeid);
this._employee = new EmployeeService().Get(tc, empid);
WFMovementTran oTran = new WFMovementTran();
oTran.Status = EnumwfStatus.Received;
oTran.FromEmployeeID = empid;
oTran.Tier = 1;
WFMovementNext initiator = new WFMovementNext();
initiator.EmployeeID = empid;
initiator.Status = EnumwfStatus.Initiate;
initiator.Remarks = sRemarks;
oTran.ObjectDescription = subject;
oTran.WFMNexts = new List<WFMovementNext>();
oTran.WFMNexts.Add(initiator);
oTran.WFTypeID = wftypeid;
oRule = this.InitiatorRuleSelection(tc, empid, owftype);
oTran.WFRuleID = oRule.ID;
// do not change following function code without discuss shamim
oTran.ObjectID = objectID;
_MoveTran = oTran;
oTran.WFMNexts.InsertRange(1, this.GetNextApprovers(tc, empid, oRule, sRemarks, 0));
if (oTran.WFMNexts.Count == 2) oTran.Tier = this.checkSameApproverInNext(tc, oRule, oTran.WFMNexts[1].EmployeeID, oTran.Tier);
oTran.WFMTasks = this.MoveMentTasks(oTran, 0, subject);
oTran.UniqueNumber = new WFMovementTranService().GetUniqueNumber(tc);
new WFMovementTranService().Save(tc, _MoveTran);
this._MoveTran.WFMTasks.ForEach(x =>
{
//for mail purpose
if (x.TasksType == EnumWFNotifyType.Email && x.employee == null)
x.employee = new EmployeeService().Get(tc, x.EmployeeID);
});
this.ThreadSendMail(_MoveTran);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public WFMovementTran GetCurrentMovemenTran()
{
return _MoveTran;
}
// do not change following function code without discuss shamim
public int checkSameApproverInNext(TransactionContext tc, WFRule orule, int approverEmpID, int stepinstepIndex)
{
int nextTier = stepinstepIndex;
if (orule.Steps.Count - 1 > stepinstepIndex + 1)
{
for (int i = stepinstepIndex + 1; i < orule.Steps.Count; i++)
{
if (orule.Steps[i].Actors.Count == 1 &&
orule.Steps[i].Actors[0].WFActorType == EnumWFActorType.PositionType)
{
List<WFRule.WFStep.WFStepActor> pactor = new List<WFRule.WFStep.WFStepActor>();
pactor = this.GetApprovers(tc, orule, approverEmpID, i);
if (pactor.Count == 1 && pactor[0].ObjectID == approverEmpID)
{
stepinstepIndex = i;
}
break;
}
}
}
return stepinstepIndex;
}
// do not change following function code without discuss shamim
private List<WFMovementNext> GetNextApprovers(TransactionContext tc, int aproverpOfEmpID, WFRule oRule, string sRemarks, int stepIndex)
{
bool isComplete = false;
int gotoStep = 0;
int recomendationid = 0;
int employeeid = aproverpOfEmpID;
List<WFMovementNext> approvers = new List<WFMovementNext>();
this.ExecuteLogic(oRule, aproverpOfEmpID, stepIndex, ref isComplete, ref gotoStep, ref recomendationid);
if (recomendationid != 0)
{
WFMovementNext Recomendation = new WFMovementNext();
Recomendation.EmployeeID = recomendationid;
Recomendation.Status = EnumwfStatus.Received;
Recomendation.Remarks = sRemarks;
approvers.Add(Recomendation);
}
else
{
int stepNo = stepIndex + 1;
//if (gotoStep != 0)
//{
// stepNo = gotoStep;
// otran.RuleStepNo = stepNo;
//}
//else otran.RuleStepNo = otran.RuleStepNo + 1;
List<WFRule.WFStep.WFStepActor> oSteps = new List<WFRule.WFStep.WFStepActor>();
oSteps = this.GetApprovers(tc, oRule, employeeid, stepNo);
foreach (WFRule.WFStep.WFStepActor item in oSteps)
{
WFMovementNext gtStep = new WFMovementNext();
gtStep.EmployeeID = (int)item.ObjectID;
gtStep.Status = EnumwfStatus.Received;
//gtStep.Viewed = true;
//gtStep.Remarks = sRemarks; //prev
gtStep.Remarks = ""; //new Added
approvers.Add(gtStep);
}
}
return approvers;
}
//private string GetListObjectValue(string listObjectFiledName, long objectID)
//{
// #region Get Source Object
// if (_sourceObject == null)
// {
// object nstatus = null;
// Type ty = null;
// ty = System.Reflection.Assembly.GetCallingAssembly().GetType("Ease.PPIC.BO" + this._wfType.SourceObjectName.Trim());
// object sourceObject = System.Activator.CreateInstance(ty);
// if (sourceObject == null) throw new Exception("unable to create object instance from object name:" + this._wfType.SourceObjectName
// + ". please check availability of the object in the Payroll.Report assembly.");
// System.Type[] pararemterTypes = new System.Type[1];
// pararemterTypes[0] = typeof(long);
// System.Reflection.MethodInfo methodInfoDel = (sourceObject.GetType()).GetMethod(this._wfType.PKGetFunctionName, pararemterTypes);
// object[] parameterValue = new object[1];
// parameterValue[0] = objectID;
// if (methodInfoDel != null)
// sourceObject = methodInfoDel.Invoke(sourceObject, parameterValue);
// else throw new Exception("function: " + this._wfType.PKGetFunctionName + "( long PKID)"
// + "is not exist in the object:" + this._wfType.PKGetFunctionName);
// // parameterValue[0] = this.SetSourceObjectStatus;
// System.Reflection.MethodInfo sourceStatusMenthod = (sourceObject.GetType()).GetMethod("GetStatusbyWf");
// if (sourceStatusMenthod != null)
// nstatus = sourceStatusMenthod.Invoke(sourceObject, parameterValue);
// else throw new Exception("function: GetStatusbyWf is not exist in the object:" + this._wfType.SourceObjectName);
// // this._wfType.SourceObjectStatus = (int)nstatus;
// }
// #endregion
// Type myType = this._sourceObject.GetType();
// // Get the PropertyInfo object by passing the property name.
// PropertyInfo myPropInfo = myType.GetProperty(listObjectFiledName);
// if (myPropInfo != null)
// return myPropInfo.GetValue(_sourceObject, null).ToString();
// return "";
//}
public void SetSourceObjectStatus()
{
//object nstatus = null;
//Type ty = null;
//ConfigurationManager omanager = new ConfigurationManager();
//ty = System.Reflection.Assembly.GetCallingAssembly().GetType("Payroll.BO." + this.WFSetup.WFType.SourceObjectName.Trim());
//object sourceObject = System.Activator.CreateInstance(ty);
//if (sourceObject == null) throw new ServiceException("unable to create object instance from object name:" + this.WFSetup.WFType.SourceObjectName
// + ". please check availability of the object in the Payroll.Report assembly.");
//System.Type[] pararemterTypes = new System.Type[1];
//pararemterTypes[0] = ID.GetType();
//System.Reflection.MethodInfo methodInfoDel = (sourceObject.GetType()).GetMethod("Get", pararemterTypes);
//object[] parameterValue = new object[1];
//parameterValue[0] = this.ObjectID;
//if (methodInfoDel != null)
// sourceObject = methodInfoDel.Invoke(sourceObject, parameterValue);
//else throw new ServiceException("function: Get(ID) is not exist in the object:" + this.WFSetup.WFType.SourceObjectName);
//parameterValue[0] = this.Status;
//System.Reflection.MethodInfo sourceStatusMenthod = (sourceObject.GetType()).GetMethod("GetStatusbyWf");
//if (sourceStatusMenthod != null)
// nstatus = sourceStatusMenthod.Invoke(sourceObject, parameterValue);
//else throw new ServiceException("function: GetStatusbyWf is not exist in the object:" + this.WFSetup.WFType.SourceObjectName);
//this.WFSetup.WFType.SourceObjectStatus = (int)nstatus;
}
// do not change following function code without discuss shamim
public WFMovementTran Approve(TransactionContext tc, int nTranID, int approveEmpID,
string Remarks, string subject)
{
WFRule owfrulesrv = new WFRule();
WFMovementTranService tranService = new WFMovementTranService();
WFMovementTran NextTran = null;
WFMovementTran LastTran = tranService.Get(tc, nTranID);
WFRule oRule = new WFRuleService().Get(tc, LastTran.WFRuleID);
WFRule.WFStep currentStep = oRule.Steps.FirstOrDefault(x => x.StepSequeceNo == LastTran.Tier);
//if (currentStep.ReqAppAll == true && LastTran.WFMNexts.Count(x => x.Status == EnumwfStatus.Approve)
// != LastTran.WFMNexts.Count)
//{
// WFMovementNext onext = _MoveTran.WFMNexts.FirstOrDefault(x => x.EmployeeID == approveEmpID);
// onext.Status = EnumwfStatus.Approve;
// new WFMovementTranService().u(tc, LastTran);
//}
bool isComplete = false;
int gotoStep = 0;
int recomendationid = 0;
this.ExecuteLogic(oRule, approveEmpID, LastTran.Tier + 1, ref isComplete, ref gotoStep, ref recomendationid);
if (isComplete == true)
{
LastTran.ObjectDescription = subject;
LastTran.Status = EnumwfStatus.End;
LastTran.WFMNexts.ForEach(c =>
{
if (c.EmployeeID == approveEmpID)
{
c.Status = EnumwfStatus.Approve;
c.StatusUpdateTime = DateTime.Now;
c.Remarks = Remarks; // new added
}
});
int employeeid = LastTran.FromEmployeeID;
if (LastTran.Tier != 1)
{
employeeid = new WFMovementTranService().InitiatorEmpID(tc, LastTran.UniqueNumber);
}
LastTran.WFMTasks = new List<WFMovementTask>();
LastTran.WFMTasks.Add(RefreshMovementTaskObj(employeeid, 0, EnumWFNotifyType.Email, false, subject, enumMailSendType.To));
LastTran.WFMTasks.Add(RefreshMovementTaskObj(employeeid, 0, EnumWFNotifyType.SysNotification, false, subject, enumMailSendType.To));
new WFMovementTranService().Save(tc, LastTran);
LastTran.WFMTasks.ForEach(x =>
{
//for mail purpose
if (x.TasksType == EnumWFNotifyType.Email && x.employee == null)
x.employee = new EmployeeService().Get(tc, x.EmployeeID);
});
this.ThreadSendMail(LastTran);
return LastTran;
}
LastTran.Status = EnumwfStatus.Approve;
LastTran.WFMNexts.ForEach(c =>
{
if (c.EmployeeID == approveEmpID)
{
c.Status = EnumwfStatus.Approve;
c.StatusUpdateTime = DateTime.Now;
c.Remarks = Remarks;// new added
}
});
this._employee = new EmployeeService().Get(tc, approveEmpID);
_MoveTran = NextTran;
NextTran = new WFMovementTran();
NextTran.Tier = LastTran.Tier + 1;
NextTran.ObjectDescription = LastTran.ObjectDescription;
NextTran.WFRuleID = LastTran.WFRuleID;
NextTran.UniqueNumber = LastTran.UniqueNumber;
NextTran.FromEmployeeID = approveEmpID;
NextTran.ObjectID = LastTran.ObjectID;
NextTran.WFTypeID = LastTran.WFTypeID;
NextTran.Status = EnumwfStatus.Received;
NextTran.WFMNexts = this.GetNextApprovers(tc, approveEmpID, oRule, Remarks, LastTran.Tier);
if (NextTran.WFMNexts.Count == 1) NextTran.Tier = this.checkSameApproverInNext(tc, oRule, (int)NextTran.WFMNexts[0].EmployeeID, NextTran.Tier);
int empid = new WFMovementTranService().InitiatorEmpID(tc, LastTran.UniqueNumber);
LastTran.WFMTasks.Add(RefreshMovementTaskObj(empid, 0, EnumWFNotifyType.Email, false, subject, enumMailSendType.To));
LastTran.WFMTasks.Add(RefreshMovementTaskObj(empid, 0, EnumWFNotifyType.SysNotification, false, subject, enumMailSendType.To));
NextTran.WFMTasks = new List<WFMovementTask>();
foreach (WFMovementNext item in NextTran.WFMNexts)
{
NextTran.WFMTasks.Add(RefreshMovementTaskObj(item.EmployeeID, item.NodeID, EnumWFNotifyType.Email, false, NextTran.ObjectDescription, enumMailSendType.To));
NextTran.WFMTasks.Add(RefreshMovementTaskObj(item.EmployeeID, item.NodeID, EnumWFNotifyType.SysNotification, false, NextTran.ObjectDescription, enumMailSendType.To));
}
new WFMovementTranService().Submit(tc, LastTran, NextTran);
NextTran.WFMTasks.ForEach(x =>
{
//for mail purpose
if (x.TasksType == EnumWFNotifyType.Email && x.employee == null)
x.employee = new EmployeeService().Get(tc, x.EmployeeID);
});
this.ThreadSendMail(NextTran);
return NextTran;
}
public void Reject(TransactionContext tc, int nTranID, int rejectEmpID, string Remarks, string subject)
{
try
{
WFMovementTranService tranService = new WFMovementTranService();
WFMovementTran LastTran = tranService.Get(tc, nTranID);
LastTran.Status = EnumwfStatus.Reject;
List<WFMovementNext> onexts = new List<WFMovementNext>();
LastTran.WFMNexts.ForEach(c =>
{
if (c.EmployeeID == rejectEmpID)
{
c.Status = EnumwfStatus.Reject;
c.Remarks = Remarks;
onexts.Add(c);
}
});
LastTran.WFMNexts = onexts;
LastTran.ObjectDescription = subject;
LastTran.WFMTasks = new List<WFMovementTask>();
int employeeid = LastTran.FromEmployeeID;
if (LastTran.Tier != 1)
employeeid = new WFMovementTranService().InitiatorEmpID(tc, LastTran.UniqueNumber);
LastTran.WFMTasks.Add(RefreshMovementTaskObj(employeeid, 0, EnumWFNotifyType.Email, false, subject, enumMailSendType.To));
LastTran.WFMTasks.Add(RefreshMovementTaskObj(employeeid, 0, EnumWFNotifyType.SysNotification, false, subject, enumMailSendType.To));
new WFMovementTranService().Save(tc, LastTran);
LastTran.WFMTasks.ForEach(x =>
{
//for mail purpose
if (x.TasksType == EnumWFNotifyType.Email && x.employee == null)
x.employee = new EmployeeService().Get(tc, x.EmployeeID);
});
this.ThreadSendMail(LastTran);
}
catch (Exception Ex)
{
throw new Exception(Ex.Message);
}
}
public void Revert(TransactionContext tc, int nTranID, int revertEmpID, string Remarks, string subject)
{
try
{
WFMovementTranService tranService = new WFMovementTranService();
WFMovementTran LastTran = tranService.Get(tc, nTranID);
LastTran.Status = EnumwfStatus.Revert;
LastTran.WFMNexts.ForEach(c =>
{
if (c.EmployeeID == revertEmpID)
{
c.Status = EnumwfStatus.Revert;
c.Remarks = Remarks;
}
});
WFMovementTran oNextApprovar = new WFMovementTran();
oNextApprovar.FromEmployeeID = _employee.ID;
oNextApprovar.Remarks = Remarks; //_employee.Name + ": " + Remarks;
oNextApprovar.WFTypeID = LastTran.WFTypeID;
oNextApprovar.WFRuleID = LastTran.WFRuleID;
oNextApprovar.UniqueNumber = LastTran.UniqueNumber;
/*For Initiator Tier will be one*/
oNextApprovar.Tier = LastTran.Tier > workflowConstants.WF_Initiator_Tier ? LastTran.Tier - 1 : LastTran.Tier;
oNextApprovar.ObjectID = LastTran.ObjectID;
WFMovementNext onext = new WFMovementNext();
onext.EmployeeID = LastTran.FromEmployeeID;
onext.Remarks = subject;
onext.ReceiveStatus = EnumWFReceiveStatus.NOT_YET_OPEN;
onext.Status = EnumwfStatus.Received;
oNextApprovar.WFMNexts = new List<WFMovementNext>();
oNextApprovar.WFMNexts.Add(onext);
// oNextApprovar.WFMTasks = this.MoveMentTasks(oNextApprovar.WFMNexts, oRule, _MoveTran.Tier, _MoveTran.Uniquenumber, enumwfStatus.Revert, sTaskRemrks);
oNextApprovar.Status = EnumwfStatus.Received;
oNextApprovar.ObjectDescription = subject;// _wfinterface.ObjectDescription; // get the object description from interface
oNextApprovar.WFMTasks = this.MoveMentTasks(oNextApprovar, 0, subject);
new WFMovementTranService().Submit(tc, LastTran, oNextApprovar);
this.ThreadSendMail(oNextApprovar);
//if (Notification != null)
//{
// Notification(oNextApprovar);
//}
//SendMail(oNextApprovar);
}
catch (Exception Ex)
{
throw new Exception(Ex.Message);
}
}
private WFMovementTask RefreshMovementTaskObj(int empID, int nodeID, EnumWFNotifyType notifyType, bool status, string Remarks, enumMailSendType mailSendType)
{
WFMovementTask oDTask = new WFMovementTask();
oDTask.EmployeeID = empID;
oDTask.NodeID = nodeID;
oDTask.TasksType = notifyType;
oDTask.Status = status;
oDTask.Description = Remarks;
oDTask.MailSendType = mailSendType;
oDTask.SentTime = DateTime.Today;
return oDTask;
}
private List<WFMovementTask> MoveMentTasks(WFMovementTran otran, int CurrentStepNo, string subject)
{
List<WFMovementTask> oWFTasks = new List<WFMovementTask>();
foreach (WFMovementNext item in otran.WFMNexts)
{
if (otran.FromEmployeeID == item.EmployeeID) continue;
oWFTasks.Add(RefreshMovementTaskObj(item.EmployeeID, item.NodeID, EnumWFNotifyType.Email, false, subject, enumMailSendType.To));
oWFTasks[oWFTasks.Count - 1].employee = item.employee;
oWFTasks.Add(RefreshMovementTaskObj(item.EmployeeID, item.NodeID, EnumWFNotifyType.SysNotification, false, subject, enumMailSendType.To));
oWFTasks[oWFTasks.Count - 1].employee = item.employee;
}
return oWFTasks;
}
public string ApproveEmpsNames(int nTranID, int employeeid)
{
List<SearchEmployee> items = this.ApproveEmps(nTranID, employeeid);
string names = "";
names = "After your approval workflow will be completed";
if (items != null && items.Count > 0)
{
names = "";
items.ForEach(x =>
{
names = names + x.EmployeeNo + "-" + x.Name + ", ";
});
if (names.Length > 0) names = names.Substring(0, names.Length - 2);
}
return names;
}
public void ThreadSendMail(WFMovementTran oTran)
{
Thread myNewThread = new Thread(() => SendMail(oTran));
myNewThread.Start();
// new WFMovementTranService().UpdateSysNotification(next.ID, true);
}
private void IsCCApplicable(ref MailSender oMailSender, IConfiguration Configuration, int empId)
{
if (Configuration.GetSection("SalesEmpEmail").Value != null)
{
string companyCode = "SP_1";
string listOfSalesEmail = Configuration.GetSection("SalesEmpEmail").Value;
if (listOfSalesEmail != string.Empty)
{
string[] salesEmail = listOfSalesEmail.Split(','); // first one is for sales head, others are cc
bool result = new EmployeeService().IsCCApplicable(empId, companyCode, salesEmail[0]);
if (result)
{
oMailSender.AddCC(listOfSalesEmail);
}
}
}
}
public void SendMail(WFMovementTran oTran)
{
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();
//Send Email With Link
foreach (WFMovementTask next in oTran.WFMTasks)
{
Thread.Sleep(200);
string sbody = "";
if (next.employee == null)
{
// it is another process, so a new TC will need to create
next.employee = new EmployeeService().Get(next.EmployeeID);
}
if (next.employee.EmailAddress == string.Empty || next.employee.EmailAddress == null) continue;
if (next.TasksType == EnumWFNotifyType.SysNotification) continue;
sender = new MailSender();
sender.AddTo(next.employee.EmailAddress.Trim());
sbody = "Dear " + next.employee.Name + "," + "<br/><br/>" + next.Description + "";
sender.Body = sbody;
sender.Link = st.WebAddress;
sender.Subject = next.Description;
// IsCCApplicable(ref sender, Configuration, next.employee.ID); it was write for sanofi
sender.SendMail(st);
// new WFMovementTranService().UpdateSysNotification(next.ID, true);
// Mail Logger for Mail
MailLog("Mail Sent to " + sender.To + ", From: " + sender.From + ", CC: " + sender.CC + ". Subject: " + sender.Subject + ". Body: " + sender.Body);
}
}
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.");
}
}
private void MailLog(string message)
{
using (StreamWriter writer = File.AppendText("Maillog.txt"))
{
writer.WriteLine("[" + DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss tt") + "] - " + message);
writer.Flush();
writer.Close();
}
}
public List<SearchEmployee> ApproveEmps(int nTranID, int employeeid)
{
WFMovementTranService tranService = new WFMovementTranService();
WFMovementTran LastTran = tranService.Get(nTranID);
WFRule oRule = new WFRuleService().Get(LastTran.WFRuleID);
List<SearchEmployee> approvars = new List<SearchEmployee>();
try
{
bool isComplete = false;
int gotoStep = 0;
int recomendationid = 0;
this.ExecuteLogic(oRule, employeeid, LastTran.Tier + 1, ref isComplete, ref gotoStep, ref recomendationid);
this._employee = new EmployeeService().Get(employeeid);
if (isComplete == true) return null;
TransactionContext tc = TransactionContext.Begin();
List<WFMovementNext> nexts = this.GetNextApprovers(tc, employeeid, oRule, "", LastTran.Tier);
tc.End();
//WFMovementNext nx = nexts.FirstOrDefault(x => x.EmployeeID == employeeid);
//if(nx !=null) // next approver and current approver are same
//{
// this.ExecuteLogic(oRule, nx.EmployeeID, LastTran.Tier + 2, ref isComplete, ref gotoStep, ref recomendationid);
// if (isComplete == true) return null;
// else
// {
// this._employee = new EmployeeService().Get(employeeid);
// nexts = this.GetNextApprovers(nx.EmployeeID, oRule, "", LastTran.Tier + 1);
// }
//}
foreach (WFMovementNext item in nexts)
{
SearchEmployee s = new SearchEmployeeService().get(item.EmployeeID);
if (s != null)
approvars.Add(s);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return approvars;
}
}
}