public void SaveFormRequest(IFormRequestViewModel viewModel, string formData, bool changingStatus = false) { FormRequest formRequest; var xmlForm = FormXmlService.LoadXmlForm(viewModel.FormId); var historyLog = string.Empty; var isNewRequest = viewModel.Id.Equals(0); if (isNewRequest) { formRequest = new FormRequest(); formRequest.UserAssigned = UserUtil.DisplayUserName; if (xmlForm.SelectSingleNode("//body/referenceCode") != null) { formRequest.Reference = GetNextReferenceCode(xmlForm.SelectSingleNode("//body/referenceCode").InnerXml); FormXmlService.SetValue(ref formData, "Reference", formRequest.Reference); } historyLog = "Form request created."; } else { formRequest = DataContext.FormRequests.First(x => x.ID.Equals(viewModel.Id)); historyLog = "Form request updated."; } formRequest.StatusId = viewModel.StatusId; formRequest.FormId = viewModel.FormId; formRequest.JSonFormData = formData; formRequest.CreatedBy = viewModel.CreatedBy; if (viewModel.Id == 0) { DataContext.FormRequests.Add(formRequest); } else { formRequest.UserAssigned = viewModel.UserAssigned ?? UserUtil.DisplayUserName; } DataContext.SaveChanges(); viewModel.Id = formRequest.ID; if (viewModel.Attachments != null && viewModel.Attachments.Any()) { AttachmentService.SaveAttachments(viewModel); } if (!changingStatus || isNewRequest) { HistoryService.SaveHistory(viewModel.Id, historyLog); } }
public void ChangeStatus(string formData, ChangeStatusViewModel viewModel, UrlHelper url, string scheme) { FormRequestViewModel.Id = viewModel.RequestFormId; FormRequestViewModel.Reference = viewModel.Reference; FormRequestViewModel.StatusId = viewModel.NewStatus; FormRequestViewModel.FormId = viewModel.FormId; FormRequestViewModel.JSonFormData = formData; FormRequestViewModel.CreatedBy = viewModel.CreatedBy; FormXmlService.SetValue(ref formData, "statusId", viewModel.NewStatus); FormXmlService.SetValue(ref formData, "status", viewModel.NewStatusDisplayName); if (viewModel.RequestFormId == 0) { FormRequestViewModel.UserAssigned = UserUtil.DisplayUserName; FormXmlService.SetValue(ref formData, "userAssigned", UserUtil.DisplayUserName); } else { var xmlForm = FormXmlService.LoadXmlForm(viewModel.FormId); var userAssigned = FormXmlService.ParseFields(formData, xmlForm.SelectSingleNode("//body/statuses/status[name='" + viewModel.NewStatus + "']/userAssigned").InnerXml); FormRequestViewModel.UserAssigned = userAssigned; FormXmlService.SetValue(ref formData, "userAssigned", userAssigned); } SaveFormRequest(FormRequestViewModel, formData, true); if (!string.IsNullOrEmpty(viewModel.Action)) { var log = FormXmlService.ParseFields(formData, viewModel.Action); HistoryService.SaveHistory(FormRequestViewModel.Id, log, viewModel.Comment); } if (!string.IsNullOrEmpty(viewModel.SendEmailNotification)) { var emailTemplates = viewModel.SendEmailNotification.Split(','); foreach (var template in emailTemplates) { viewModel.SendEmailNotification = template; EmailService.SendEmail(formData, viewModel, url, scheme); } } }