public ActionResult SaveWorkOrder(WorkOrderModel model) { _log.InfoFormat("Method: SaveWorkOrder. Model ID {0}", model.Id); if (model.Emploee == 0) { return(Error("Employee is required")); } if (model.Location == null) { return(Error("Location is required")); } if (model.Problem == null) { return(Error("Problem is required")); } var workorder = new SageWorkOrder() { ARCustomer = model.Customer, Location = model.Location, CallType = model.Calltype, CallDate = model.Calldate.GetLocalDate(), Problem = model.Problem, RateSheet = model.Ratesheet, Employee = model.Emploee.ToString(), EstimatedRepairHours = Convert.ToDecimal(model.Estimatehours), NottoExceed = model.Nottoexceed, Comments = model.Locationcomments, CustomerPO = model.Customerpo, PermissionCode = model.Permissiocode, PayMethod = model.Paymentmethods, WorkOrder = model.WorkOrder, Id = model.Id, Status = WorkOrderStatus.ById(model.Status), JCJob = model.JCJob, Contact = model.Contact, Equipment = model.Equipment }; var workOrderResult = _sageApiProxy.EditWorkOrder(workorder); workOrderResult.Entity.LocationNumber = long.Parse(model.Location); if (!workOrderResult.IsSucceed) { _log.ErrorFormat("Was not able to update workorder to sage. !result.IsSucceed"); return(Error("Was not able to update workorder to sage")); } if (model.Emploee != 0) { var assignmentDb = _repository.SearchFor <SageAssignment>(x => x.WorkOrder == model.WorkOrder).Single(); var editedAssignment = new AssignmentViewModel(); editedAssignment.Employee = model.Emploee; editedAssignment.EndDate = model.AssignmentDate.Date.Add((model.AssignmentTime).TimeOfDay).AddHours(assignmentDb.EstimatedRepairHours.AsDouble() == 0 ? 1 : assignmentDb.EstimatedRepairHours.AsDouble()); editedAssignment.EstimatedRepairHours = workorder.EstimatedRepairHours.ToString(); editedAssignment.Id = assignmentDb.Id; editedAssignment.ScheduleDate = model.AssignmentDate.Date.Add((model.AssignmentTime).TimeOfDay); editedAssignment.WorkOrder = assignmentDb.WorkOrder; _scheduleService.CerateAssignment(editedAssignment); var locations = _repository.GetAll <SageLocation>().ToArray(); var itemLocation = locations.FirstOrDefault(l => l.Name == workOrderResult.Entity.Location); workOrderResult.Entity.ScheduleDate = model.AssignmentDate.Date.Add((model.AssignmentTime).TimeOfDay); workOrderResult.Entity.Latitude = itemLocation.Latitude; workOrderResult.Entity.Longitude = itemLocation.Longitude; } this.ResolveWorkOrderItems(model); this.ResolveWorkOrderNotes(model); workOrderResult.Entity.Status = WorkOrderStatus.ById(model.Status); workOrderResult.Entity.Id = workorder.Id; workOrderResult.Entity.IsValid = true; workOrderResult.Entity.WorkOrderItems = _sageApiProxy.GetWorkorderItemsByWorkOrderId(workorder.WorkOrder).Entities; workOrderResult.Entity.WorkNotes = this._sageApiProxy.GetNotes(workorder.WorkOrder).Entities; _repository.Update(workOrderResult.Entity); _log.InfoFormat("Repository update workorder. Name {0}, ID {1}", workorder.Name, workorder.Id); _hub.UpdateWorkOrder(model); if (model.Status == WorkOrderStatus.WorkCompleteId) { _hub.ShowAlert(new SweetAlertModel() { Message = $"Workorder #{model.WorkOrder} closed", Title = "Workorder completed", Type = "success" }); } return(Success()); }