public FreightManagement RunWorkFlow(FreightManagement freightManagement, TaskManagement taskOfPrevStep, EmployeeInfo approver, EmployeeInfo currentStepApprover)
        {
            if (freightManagement == null)
            {
                return(null);
            }

            TaskManagement taskManagement = new TaskManagement();

            taskManagement.StartDate       = DateTime.Now;
            taskManagement.DueDate         = freightManagement.RequestDueDate;
            taskManagement.PercentComplete = 0;
            taskManagement.ItemId          = freightManagement.ID;
            taskManagement.ItemURL         = taskOfPrevStep.ItemURL;
            taskManagement.ListURL         = taskOfPrevStep.ListURL;
            taskManagement.TaskName        = TASK_NAME;
            taskManagement.TaskStatus      = TaskStatusList.InProgress;
            taskManagement.StepModule      = StepModuleList.FreightManagement.ToString();
            taskManagement.Department      = freightManagement.Department.LookupId > 0 ? freightManagement.Department : null;

            StepManagementDAL _stepManagementDAL = new StepManagementDAL(this.SiteUrl);
            User   assignee     = null;
            User   nextAssignee = null;
            string stepStatus   = string.Empty;

            StepManagement nextStep = null;

            if (currentStepApprover.EmployeePosition.LookupId == (int)StringConstant.EmployeePosition.DepartmentHead &&
                currentStepApprover.ADAccount.ID == freightManagement.DH.ID)
            {
                nextStep = _stepManagementDAL.GetNextStepManagement(taskOfPrevStep.StepStatus, StepModuleList.FreightManagement, freightManagement.Department.LookupId);
                if (freightManagement.HighPriority == true)
                {
                    if (nextStep != null)
                    {
                        stepStatus = nextStep.StepStatus;
                        var approverAtStep = GetApproverAtStep(freightManagement.Department.LookupId, freightManagement.Location.LookupId, StepModuleList.FreightManagement, nextStep.StepNumber);
                        if (approverAtStep != null)
                        {
                            assignee = approverAtStep.ADAccount;
                        }
                    }
                }
                else
                {
                    if (nextStep != null)
                    {
                        nextStep = _stepManagementDAL.GetNextStepManagement(nextStep.StepStatus, StepModuleList.FreightManagement, freightManagement.Department.LookupId);
                        if (nextStep != null)
                        {
                            stepStatus = nextStep.StepStatus;
                            var approverAtStep = GetApproverAtStep(freightManagement.Department.LookupId, freightManagement.Location.LookupId, StepModuleList.FreightManagement, nextStep.StepNumber);
                            if (approverAtStep != null)
                            {
                                assignee = approverAtStep.ADAccount;
                            }
                        }
                    }
                }
            }
            else
            {
                nextStep = _stepManagementDAL.GetNextStepManagement(taskOfPrevStep.StepStatus, StepModuleList.FreightManagement, freightManagement.Department.LookupId);
                if (nextStep != null)
                {
                    stepStatus = nextStep.StepStatus;
                    var approverAtStep = GetApproverAtStep(freightManagement.Department.LookupId, freightManagement.Location.LookupId, StepModuleList.FreightManagement, nextStep.StepNumber);
                    if (approverAtStep != null)
                    {
                        assignee = approverAtStep.ADAccount;
                    }
                }
            }

            // get next approver
            if (nextStep != null)
            {
                nextStep = _stepManagementDAL.GetNextStepManagement(nextStep.StepStatus, StepModuleList.FreightManagement, freightManagement.Department.LookupId);
                if (nextStep != null)
                {
                    var nextApprover = GetApproverAtStep(freightManagement.Department.LookupId, freightManagement.Location.LookupId, StepModuleList.FreightManagement, nextStep.StepNumber);
                    if (nextApprover != null)
                    {
                        nextAssignee = nextApprover.ADAccount;
                    }
                }
            }

            taskManagement.AssignedTo = assignee;
            taskManagement.NextAssign = nextAssignee;
            taskManagement.StepStatus = stepStatus;

            EmployeeInfoDAL  _employeeInfoDAL  = new EmployeeInfoDAL(this.SiteUrl);
            EmailTemplateDAL _emailTemplateDAL = new EmailTemplateDAL(this.SiteUrl);

            if (assignee == null)
            {
                freightManagement.ApprovalStatus = StringConstant.ApprovalStatus.Approved.ToString();
                this.SaveOrUpdate(freightManagement);

                EmailTemplate emailTemplate = _emailTemplateDAL.GetByKey("FreightManagement_Approve");
                EmployeeInfo  toUser        = _employeeInfoDAL.GetByID(freightManagement.Requester.LookupId);
                SendEmail(freightManagement, emailTemplate, approver, toUser, this.SiteUrl, false);
            }
            else if (assignee != null)
            {
                TaskManagementDAL taskManagementDAL = new TaskManagementDAL(this.SiteUrl);
                int retId = taskManagementDAL.SaveItem(taskManagement);

                freightManagement.ApprovalStatus = taskManagement.StepStatus;
                this.SaveOrUpdate(freightManagement);

                EmailTemplate emailTemplate = _emailTemplateDAL.GetByKey("FreightManagement_Request");
                EmployeeInfo  toUser        = _employeeInfoDAL.GetByADAccount(assignee.ID);
                SendEmail(freightManagement, emailTemplate, approver, toUser, this.SiteUrl, true);

                try
                {
                    List <EmployeeInfo> toUsers = DelegationPermissionManager.GetListOfDelegatedEmployees(toUser.ID, StringConstant.FreightManagementList.ListUrl, freightManagement.ID);
                    SendDelegationEmail(freightManagement, emailTemplate, toUsers, this.SiteUrl);
                }
                catch { }
            }

            return(freightManagement);
        }
Example #2
0
        public BusinessTripManagement RunWorkFlow(BusinessTripManagement businessTripManagement, TaskManagement taskOfPrevStep, EmployeeInfo approver, EmployeeInfo currentStepApprover)
        {
            if (businessTripManagement == null)
            {
                return(null);
            }

            TaskManagement taskManagement = new TaskManagement();

            taskManagement.StartDate       = DateTime.Now;
            taskManagement.DueDate         = businessTripManagement.RequestDueDate;
            taskManagement.PercentComplete = 0;
            taskManagement.ItemId          = businessTripManagement.ID;
            taskManagement.ItemURL         = taskOfPrevStep.ItemURL;
            taskManagement.ListURL         = taskOfPrevStep.ListURL;
            taskManagement.TaskName        = TASK_NAME;
            taskManagement.TaskStatus      = TaskStatusList.InProgress;
            taskManagement.StepModule      = StepModuleList.BusinessTripManagement.ToString();
            taskManagement.Department      = businessTripManagement.CommonDepartment.LookupId > 0 ? businessTripManagement.CommonDepartment : null;

            StepManagementDAL _stepManagementDAL = new StepManagementDAL(this.SiteUrl);
            User   assignee     = null;
            User   nextAssignee = null;
            string stepStatus   = string.Empty;

            if (businessTripManagement.Domestic == true) //Domestic Business Trip
            {
                StepManagement nextStep = null;

                if (currentStepApprover.EmployeePosition.LookupId == (int)StringConstant.EmployeePosition.DepartmentHead &&
                    currentStepApprover.ADAccount.ID == businessTripManagement.DH.ID)
                {
                    nextStep = _stepManagementDAL.GetNextStepManagement(taskOfPrevStep.StepStatus, StepModuleList.BusinessTripManagement, businessTripManagement.CommonDepartment.LookupId);
                    if (businessTripManagement.TripHighPriority == true)
                    {
                        if (nextStep != null)
                        {
                            stepStatus = nextStep.StepStatus;
                            var approverAtStep = GetApproverAtStep(businessTripManagement.CommonDepartment.LookupId, businessTripManagement.CommonLocation.LookupId, StepModuleList.BusinessTripManagement, nextStep.StepNumber);
                            if (approverAtStep != null)
                            {
                                assignee = approverAtStep.ADAccount;
                            }
                        }
                    }
                    else
                    {
                        if (nextStep != null)
                        {
                            nextStep = _stepManagementDAL.GetNextStepManagement(nextStep.StepStatus, StepModuleList.BusinessTripManagement, businessTripManagement.CommonDepartment.LookupId);
                            if (nextStep != null)
                            {
                                stepStatus = nextStep.StepStatus;
                                var approverAtStep = GetApproverAtStep(businessTripManagement.CommonDepartment.LookupId, businessTripManagement.CommonLocation.LookupId, StepModuleList.BusinessTripManagement, nextStep.StepNumber);
                                if (approverAtStep != null)
                                {
                                    assignee = approverAtStep.ADAccount;
                                }
                            }
                        }
                    }
                }
                else
                {
                    nextStep = _stepManagementDAL.GetNextStepManagement(taskOfPrevStep.StepStatus, StepModuleList.BusinessTripManagement, businessTripManagement.CommonDepartment.LookupId);
                    if (nextStep != null)
                    {
                        stepStatus = nextStep.StepStatus;
                        var approverAtStep = GetApproverAtStep(businessTripManagement.CommonDepartment.LookupId, businessTripManagement.CommonLocation.LookupId, StepModuleList.BusinessTripManagement, nextStep.StepNumber);
                        if (approverAtStep != null)
                        {
                            assignee = approverAtStep.ADAccount;
                        }
                    }
                }

                // get next approver
                if (nextStep != null)
                {
                    nextStep = _stepManagementDAL.GetNextStepManagement(nextStep.StepStatus, StepModuleList.BusinessTripManagement, businessTripManagement.CommonDepartment.LookupId);
                    if (nextStep != null)
                    {
                        var nextApprover = GetApproverAtStep(businessTripManagement.CommonDepartment.LookupId, businessTripManagement.CommonLocation.LookupId, StepModuleList.BusinessTripManagement, nextStep.StepNumber);
                        if (nextApprover != null)
                        {
                            nextAssignee = nextApprover.ADAccount;
                        }
                    }
                }
            }
            else // Overseas Business Trip
            {
                StepManagement nextStep = null;
                if (currentStepApprover.ADAccount.ID == businessTripManagement.DirectBOD.ID)
                {
                    if (businessTripManagement.DirectBOD.ID != businessTripManagement.BOD.ID)
                    {
                        assignee   = businessTripManagement.BOD;
                        stepStatus = StepStatusList.BODApproval;
                        nextStep   = new StepManagement()
                        {
                            StepStatus = stepStatus, StepModule = StepModuleList.BusinessTripManagement.ToString()
                        };
                    }
                    else
                    {
                        nextStep = _stepManagementDAL.GetNextStepManagement(taskOfPrevStep.StepStatus, StepModuleList.BusinessTripManagement, businessTripManagement.CommonDepartment.LookupId);
                        if (nextStep != null)
                        {
                            stepStatus = nextStep.StepStatus;
                            var approverAtStep = GetApproverAtStep(businessTripManagement.CommonDepartment.LookupId, businessTripManagement.CommonLocation.LookupId, StepModuleList.BusinessTripManagement, nextStep.StepNumber);
                            if (approverAtStep != null)
                            {
                                assignee = approverAtStep.ADAccount;
                            }
                        }
                    }
                }
                else
                {
                    nextStep = _stepManagementDAL.GetNextStepManagement(taskOfPrevStep.StepStatus, StepModuleList.BusinessTripManagement, businessTripManagement.CommonDepartment.LookupId);
                    if (nextStep != null)
                    {
                        stepStatus = nextStep.StepStatus;
                        var approverAtStep = GetApproverAtStep(businessTripManagement.CommonDepartment.LookupId, businessTripManagement.CommonLocation.LookupId, StepModuleList.BusinessTripManagement, nextStep.StepNumber);
                        if (approverAtStep != null)
                        {
                            assignee = approverAtStep.ADAccount;
                        }
                    }
                }

                // get next approver
                if (nextStep != null)
                {
                    nextStep = _stepManagementDAL.GetNextStepManagement(nextStep.StepStatus, StepModuleList.BusinessTripManagement, businessTripManagement.CommonDepartment.LookupId);
                    if (nextStep != null)
                    {
                        var nextApprover = GetApproverAtStep(businessTripManagement.CommonDepartment.LookupId, businessTripManagement.CommonLocation.LookupId, StepModuleList.BusinessTripManagement, nextStep.StepNumber);
                        if (nextApprover != null)
                        {
                            nextAssignee = nextApprover.ADAccount;
                        }
                    }
                }
            }

            taskManagement.AssignedTo = assignee;
            taskManagement.NextAssign = nextAssignee;
            taskManagement.StepStatus = stepStatus;

            EmployeeInfoDAL  _employeeInfoDAL  = new EmployeeInfoDAL(this.SiteUrl);
            EmailTemplateDAL _emailTemplateDAL = new EmailTemplateDAL(this.SiteUrl);

            if (assignee == null)
            {
                businessTripManagement.ApprovalStatus = StringConstant.ApprovalStatus.Approved.ToString();
                this.SaveOrUpdate(businessTripManagement);

                EmailTemplate emailTemplateRequester = _emailTemplateDAL.GetByKey("BusinessTripManagement_Approve");
                EmployeeInfo  toRequester            = _employeeInfoDAL.GetByID(businessTripManagement.Requester.LookupId);
                SendEmail(businessTripManagement, emailTemplateRequester, approver, toRequester, this.SiteUrl, false);

                if (businessTripManagement.TransportationType == ResourceHelper.GetLocalizedString("BusinessTripManagement_TransportationTypeCompanyTitle", StringConstant.ResourcesFileLists, 1033))
                {
                    EmailTemplate emailTemplateDriver = _emailTemplateDAL.GetByKey("BusinessTripManagement_Driver");
                    EmployeeInfo  toDriver            = _employeeInfoDAL.GetByID(businessTripManagement.Driver.LookupId);
                    SendEmail(businessTripManagement, emailTemplateDriver, approver, toDriver, this.SiteUrl, false);
                }

                if (!string.IsNullOrEmpty(businessTripManagement.CashRequestDetail))
                {
                    EmailTemplate emailTemplateAccountant = _emailTemplateDAL.GetByKey("BusinessTripManagement_Accountant");
                    EmployeeInfo  toAccountant            = _employeeInfoDAL.GetByID(businessTripManagement.Cashier.LookupId);
                    SendEmail(businessTripManagement, emailTemplateAccountant, approver, toAccountant, this.SiteUrl, false);
                }
            }
            else if (assignee != null)
            {
                TaskManagementDAL taskManagementDAL = new TaskManagementDAL(this.SiteUrl);
                int retId = taskManagementDAL.SaveItem(taskManagement);

                businessTripManagement.ApprovalStatus = taskManagement.StepStatus;
                this.SaveOrUpdate(businessTripManagement);

                EmailTemplate emailTemplate = _emailTemplateDAL.GetByKey("BusinessTripManagement_Request");
                EmployeeInfo  toUser        = _employeeInfoDAL.GetByADAccount(assignee.ID);
                SendEmail(businessTripManagement, emailTemplate, approver, toUser, this.SiteUrl, true);

                try
                {
                    List <EmployeeInfo> toUsers = DelegationPermissionManager.GetListOfDelegatedEmployees(toUser.ID, StringConstant.BusinessTripManagementList.Url, businessTripManagement.ID);
                    SendDelegationEmail(businessTripManagement, emailTemplate, toUsers, this.SiteUrl);
                }
                catch { }
            }

            return(businessTripManagement);
        }