public override void ItemAdded(SPItemEventProperties properties) { try { base.ItemAdded(properties); var siteURL = properties.WebUrl; var vehicleManagementDAL = new VehicleManagementDAL(siteURL); var ItemID = properties.ListItemId; var currentItem = vehicleManagementDAL.GetByID(ItemID); taskManagementDAL = new TaskManagementDAL(siteURL); TaskManagement taskManagement = new TaskManagement(); taskManagement.StartDate = DateTime.Now; taskManagement.DueDate = currentItem.RequestDueDate; taskManagement.PercentComplete = 0; taskManagement.ItemId = currentItem.ID; taskManagement.ItemURL = properties.List.DefaultDisplayFormUrl + "?ID=" + properties.ListItemId; taskManagement.ListURL = properties.List.DefaultViewUrl; taskManagement.TaskName = TASK_NAME; taskManagement.TaskStatus = TaskStatusList.InProgress; taskManagement.StepModule = StepModuleList.VehicleManagement.ToString(); taskManagement.Department = currentItem.CommonDepartment; taskManagement.AssignedTo = currentItem.DepartmentHead; employeeInfoDAL = new EmployeeInfoDAL(siteURL); EmployeeInfo requesterInfo = employeeInfoDAL.GetByID(currentItem.Requester.LookupId); if ((int)Convert.ToDouble(requesterInfo.EmployeeLevel.LookupValue, CultureInfo.InvariantCulture.NumberFormat) == (int)StringConstant.EmployeeLevel.DepartmentHead) { taskManagement.StepStatus = StepStatusList.BODApproval; } else { taskManagement.StepStatus = StepStatusList.DHApproval; } DepartmentDAL _departmentDAL = new DepartmentDAL(siteURL); Department departmentHR = _departmentDAL.GetByCode("HR"); if (departmentHR.ID == currentItem.CommonDepartment.LookupId) { taskManagement.NextAssign = null; } else { EmployeeInfo deptHeadOfHR = employeeInfoDAL.GetByPositionDepartment(StringConstant.EmployeePosition.DepartmentHead, requesterInfo.FactoryLocation.LookupId, departmentHR.ID).FirstOrDefault(); if (deptHeadOfHR != null) { taskManagement.NextAssign = deptHeadOfHR.ADAccount; } } taskManagementDAL.SaveItem(taskManagement); var mailDAL = new EmailTemplateDAL(siteURL); var emailTemplate = mailDAL.GetByKey("VehicleManagement_Request"); EmployeeInfo assigneeInfo = employeeInfoDAL.GetByADAccount(taskManagement.AssignedTo.ID); currentItem.ApprovalStatus = taskManagement.StepStatus; vehicleManagementDAL.SaveOrUpdate(currentItem); vehicleManagementDAL.SendEmail(currentItem, emailTemplate, null, assigneeInfo, VehicleTypeOfEmail.Request, siteURL); try { List <EmployeeInfo> toUsers = DelegationPermissionManager.GetListOfDelegatedEmployees(siteURL, assigneeInfo.ID, StringConstant.VehicleManagementList.ListUrl, currentItem.ID); vehicleManagementDAL.SendDelegationEmail(currentItem, emailTemplate, toUsers, siteURL); } catch { } } catch (Exception ex) { ULSLogging.Log(new SPDiagnosticsCategory("STADA - Transportation Event Receiver - ItemAdded fn", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, string.Format(CultureInfo.InvariantCulture, "{0}:{1}", ex.Message, ex.StackTrace)); } }
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); }
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); }