public ActionResult CreateWorkOrder(WorkOrderModel model) { _log.InfoFormat("Method: CreateWorkOrder. Model ID {0}", model.Id); if (string.IsNullOrEmpty(model.Calltype)) { return(Error("Call Type 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, JCJob = model.JCJob, Contact = model.Contact, Equipment = model.Equipment, }; var result = _sageApiProxy.AddWorkOrder(workorder); result.Entity.LocationNumber = long.Parse(model.Location); if (!result.IsSucceed) { _log.ErrorFormat("Was not able to save workorder to sage. !result.IsSucceed"); return(Error(result.ErrorMassage)); } var assignment = result.RelatedAssignment; assignment.IsValid = true; if (model.Emploee == 0) { result.Entity.AssignmentId = assignment.Assignment; _repository.Add(assignment); } else { var employee = _repository.SearchFor <SageEmployee>(x => x.Employee == model.Emploee).SingleOrDefault(); assignment.EmployeeId = employee?.Employee ?? 0; assignment.Start = (model.AssignmentDate.Date).Add((model.AssignmentTime).TimeOfDay).ToString(); assignment.End = model.AssignmentDate.Date.Add((model.AssignmentTime).TimeOfDay).AddHours(assignment.EstimatedRepairHours.AsDouble() == 0 ? 1 : assignment.EstimatedRepairHours.AsDouble()).ToString(); assignment.Color = employee?.Color ?? ""; assignment.Customer = result.Entity.ARCustomer; assignment.Location = result.Entity.Location; var locations = _repository.GetAll <SageLocation>().ToArray(); var itemLocation = locations.FirstOrDefault(l => l.Name == result.Entity.Location); result.Entity.ScheduleDate = assignment.ScheduleDate; result.Entity.Latitude = itemLocation.Latitude; result.Entity.Longitude = itemLocation.Longitude; _repository.Add(assignment); _hub.CreateAssignment(new MapViewModel() { WorkOrder = result.Entity, DateEntered = assignment.ScheduleDate, Employee = employee?.Employee ?? 0, Color = employee?.Color ?? "" }); } _repository.Add(result.Entity); _log.InfoFormat("Workorder added to repository. ID: {0}, Name: {1}", workorder.Id, workorder.Name); _notification.SendNotification(string.Format("{0} was created", workorder.WorkOrder)); return(Success()); }