public MessageResult ApproveVehicle(VehicleApprovalModel vehicleApprovalModel)
        {
            MessageResult msgResult = new MessageResult();

            try
            {
                SPWeb spWeb = SPContext.Current.Web;

                if (vehicleApprovalModel.Id > 0)
                {
                    Biz.Models.VehicleManagement vehicleManagement = _vehicleManagementDAL.GetByID(vehicleApprovalModel.Id);
                    string currentApprovalStatus = vehicleManagement.ApprovalStatus.ToLower();
                    if (currentApprovalStatus == ApprovalStatus.Approved.ToLower() || currentApprovalStatus == ApprovalStatus.Cancelled.ToLower() || currentApprovalStatus == ApprovalStatus.Rejected.ToLower())
                    {
                        return(new MessageResult {
                            Code = (int)VehicleErrorCode.CannotApprove, Message = MessageResultHelper.GetRequestStatusMessage(currentApprovalStatus), ObjectId = 0
                        });
                    }

                    string requestExpiredMsg = MessageResultHelper.GetRequestExpiredMessage(vehicleManagement.RequestDueDate);
                    if (!string.IsNullOrEmpty(requestExpiredMsg))
                    {
                        return(new MessageResult {
                            Code = (int)VehicleErrorCode.CannotApprove, Message = requestExpiredMsg, ObjectId = 0
                        });
                    }

                    bool            hasApprovalPermission = HasApprovalPermission(vehicleApprovalModel.Id.ToString());
                    DelegationModel delegationModel       = GetDelegatedTaskInfo(vehicleApprovalModel.Id.ToString());
                    bool            isDelegated           = (delegationModel != null && delegationModel.Requester.LookupId > 0) ? true : false;
                    if (hasApprovalPermission == false && isDelegated == false)
                    {
                        return(msgResult);
                    }

                    EmployeeInfoDAL _employeeInfoDAL = new EmployeeInfoDAL(spWeb.Url);
                    EmployeeInfo    approverInfo     = _employeeInfoDAL.GetByADAccount(spWeb.CurrentUser.ID);

                    int assigneeId = hasApprovalPermission == true ? approverInfo.ADAccount.ID : (isDelegated == true ? delegationModel.FromEmployee.ID : 0);

                    TaskManagementDAL      _taskManagementDAL       = new TaskManagementDAL(spWeb.Url);
                    IList <TaskManagement> taskManagementCollection = _taskManagementDAL.GetRelatedTasks(vehicleManagement.ID, StepModuleList.VehicleManagement.ToString());
                    if (taskManagementCollection != null && taskManagementCollection.Count > 0)
                    {
                        TaskManagement        taskOfOriginalAssignee = _taskManagementDAL.GetTaskByAssigneeId(taskManagementCollection, assigneeId);
                        List <TaskManagement> relatedTasks           = taskManagementCollection.Where(t => t.ID != taskOfOriginalAssignee.ID).ToList();
                        User nextAssignee = taskOfOriginalAssignee.NextAssign;

                        if (hasApprovalPermission == true)
                        {
                            taskOfOriginalAssignee.TaskStatus      = TaskStatusList.Completed.ToString();
                            taskOfOriginalAssignee.PercentComplete = 1;
                            taskOfOriginalAssignee.TaskOutcome     = TaskOutcome.Approved.ToString();
                            taskOfOriginalAssignee.Description     = vehicleApprovalModel.Comment;
                            _taskManagementDAL.CloseTasks(relatedTasks);
                            _taskManagementDAL.SaveItem(taskOfOriginalAssignee);
                        }
                        else if (isDelegated == true)
                        {
                            TaskManagement clonedTask = _taskManagementDAL.CloneTask(taskOfOriginalAssignee);
                            clonedTask.AssignedTo      = approverInfo.ADAccount;
                            clonedTask.TaskStatus      = TaskStatusList.Completed.ToString();
                            clonedTask.PercentComplete = 1;
                            clonedTask.TaskOutcome     = TaskOutcome.Approved.ToString();
                            clonedTask.Description     = vehicleApprovalModel.Comment;
                            relatedTasks.Add(taskOfOriginalAssignee);
                            _taskManagementDAL.CloseTasks(relatedTasks);
                            _taskManagementDAL.SaveItem(clonedTask);
                        }

                        if (!string.IsNullOrEmpty(vehicleApprovalModel.Comment))
                        {
                            vehicleManagement.CommonComment = vehicleManagement.CommonComment.BuildComment(string.Format("{0}: {1}", approverInfo.FullName, vehicleApprovalModel.Comment));
                        }

                        if (nextAssignee == null)
                        {
                            vehicleManagement.ApprovalStatus = StringConstant.ApprovalStatus.Approved.ToString();
                            _vehicleManagementDAL.SaveOrUpdate(spWeb, vehicleManagement);

                            EmailTemplateDAL _emailTemplateDAL = new EmailTemplateDAL(spWeb.Url);
                            EmailTemplate    emailTemplate     = _emailTemplateDAL.GetByKey("VehicleManagement_Approve");
                            EmployeeInfo     toUser            = _employeeInfoDAL.GetByID(vehicleManagement.Requester.LookupId);
                            _vehicleManagementDAL.SendEmail(vehicleManagement, emailTemplate, approverInfo, toUser, VehicleTypeOfEmail.Approve, spWeb.Url);
                        }
                        else if (nextAssignee != null && taskManagementCollection != null && taskManagementCollection.Count > 0)
                        {
                            _vehicleManagementDAL.RunWorkFlow(vehicleManagement, taskOfOriginalAssignee);

                            EmailTemplateDAL _emailTemplateDAL = new EmailTemplateDAL(spWeb.Url);
                            EmailTemplate    emailTemplate     = _emailTemplateDAL.GetByKey("VehicleManagement_Request");
                            EmployeeInfo     toUser            = _employeeInfoDAL.GetByADAccount(nextAssignee.ID);
                            _vehicleManagementDAL.SendEmail(vehicleManagement, emailTemplate, approverInfo, toUser, VehicleTypeOfEmail.Request, spWeb.Url);

                            try
                            {
                                List <EmployeeInfo> toUsers = DelegationPermissionManager.GetListOfDelegatedEmployees(toUser.ID, StringConstant.VehicleManagementList.ListUrl, vehicleManagement.ID);
                                _vehicleManagementDAL.SendDelegationEmail(vehicleManagement, emailTemplate, toUsers, spWeb.Url);
                            }
                            catch { }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                msgResult.Code    = (int)VehicleErrorCode.Unexpected;
                msgResult.Message = ex.Message;
            }

            return(msgResult);
        }
Example #2
0
        public MessageResult RejectBusinessTrip(BusinessTripManagementModel businessTripManagementModel)
        {
            MessageResult msgResult = new MessageResult();

            try
            {
                SPWeb spWeb = SPContext.Current.Web;

                if (businessTripManagementModel.Id > 0)
                {
                    Biz.Models.BusinessTripManagement businessTripManagement = _businessTripManagementDAL.GetByID(businessTripManagementModel.Id);
                    string currentApprovalStatus = businessTripManagement.ApprovalStatus.ToLower();
                    if (currentApprovalStatus == ApprovalStatus.Approved.ToLower() || currentApprovalStatus == ApprovalStatus.Cancelled.ToLower() || currentApprovalStatus == ApprovalStatus.Rejected.ToLower())
                    {
                        return(new MessageResult {
                            Code = (int)BusinessTripErrorCode.CannotReject, Message = MessageResultHelper.GetRequestStatusMessage(currentApprovalStatus), ObjectId = 0
                        });
                    }

                    string requestExpiredMsg = MessageResultHelper.GetRequestExpiredMessage(businessTripManagement.RequestDueDate);
                    if (!string.IsNullOrEmpty(requestExpiredMsg))
                    {
                        return(new MessageResult {
                            Code = (int)BusinessTripErrorCode.CannotReject, Message = requestExpiredMsg, ObjectId = 0
                        });
                    }

                    bool            hasApprovalPermission = HasApprovalPermission(businessTripManagementModel.Id.ToString());
                    DelegationModel delegationModel       = GetDelegatedTaskInfo(businessTripManagementModel.Id.ToString());
                    bool            isDelegated           = (delegationModel != null && delegationModel.Requester.LookupId > 0) ? true : false;
                    if (hasApprovalPermission == false && isDelegated == false)
                    {
                        return(msgResult);
                    }

                    EmployeeInfoDAL _employeeInfoDAL = new EmployeeInfoDAL(spWeb.Url);
                    EmployeeInfo    approverInfo     = _employeeInfoDAL.GetByADAccount(spWeb.CurrentUser.ID);

                    int assigneeId = hasApprovalPermission == true ? approverInfo.ADAccount.ID : (isDelegated == true ? delegationModel.FromEmployee.ID : 0);

                    TaskManagementDAL      _taskManagementDAL       = new TaskManagementDAL(spWeb.Url);
                    IList <TaskManagement> taskManagementCollection = _taskManagementDAL.GetRelatedTasks(businessTripManagement.ID, StepModuleList.BusinessTripManagement.ToString());
                    if (taskManagementCollection != null && taskManagementCollection.Count > 0)
                    {
                        TaskManagement        taskOfOriginalAssignee = _taskManagementDAL.GetTaskByAssigneeId(taskManagementCollection, assigneeId);
                        List <TaskManagement> relatedTasks           = taskManagementCollection.Where(t => t.ID != taskOfOriginalAssignee.ID).ToList();

                        if (hasApprovalPermission == true)
                        {
                            taskOfOriginalAssignee.TaskStatus      = TaskStatusList.Completed.ToString();
                            taskOfOriginalAssignee.PercentComplete = 1;
                            taskOfOriginalAssignee.TaskOutcome     = TaskOutcome.Rejected.ToString();
                            taskOfOriginalAssignee.Description     = businessTripManagementModel.Comment;
                            _taskManagementDAL.CloseTasks(relatedTasks);
                            _taskManagementDAL.SaveItem(taskOfOriginalAssignee);
                        }
                        else if (isDelegated == true)
                        {
                            TaskManagement clonedTask = _taskManagementDAL.CloneTask(taskOfOriginalAssignee);
                            clonedTask.AssignedTo      = approverInfo.ADAccount;
                            clonedTask.TaskStatus      = TaskStatusList.Completed.ToString();
                            clonedTask.PercentComplete = 1;
                            clonedTask.TaskOutcome     = TaskOutcome.Rejected.ToString();
                            clonedTask.Description     = businessTripManagementModel.Comment;
                            relatedTasks.Add(taskOfOriginalAssignee);
                            _taskManagementDAL.CloseTasks(relatedTasks);
                            _taskManagementDAL.SaveItem(clonedTask);
                        }
                    }

                    if (!string.IsNullOrEmpty(businessTripManagementModel.Comment))
                    {
                        businessTripManagement.Comment = businessTripManagement.Comment.BuildComment(string.Format("{0}: {1}", approverInfo.FullName, businessTripManagementModel.Comment));
                    }

                    businessTripManagement.ApprovalStatus = ApprovalStatus.Rejected.ToString();
                    _businessTripManagementDAL.SaveOrUpdate(spWeb, businessTripManagement);

                    EmailTemplateDAL _emailTemplateDAL = new EmailTemplateDAL(spWeb.Url);
                    EmailTemplate    emailTemplate     = _emailTemplateDAL.GetByKey("BusinessTripManagement_Reject");
                    EmployeeInfo     toUser            = _employeeInfoDAL.GetByID(businessTripManagement.Requester.LookupId);
                    _businessTripManagementDAL.SendEmail(businessTripManagement, emailTemplate, approverInfo, toUser, spWeb.Url, false);
                }
            }
            catch (Exception ex)
            {
                msgResult.Code    = (int)BusinessTripErrorCode.Unexpected;
                msgResult.Message = ex.Message;
            }

            return(msgResult);
        }