Beispiel #1
0
        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());
        }
Beispiel #2
0
        public bool CerateAssignment(AssignmentViewModel model)
        {
            _log.InfoFormat("Method: CreateAssignment. Model ID {0}", model.Id);
            var databaseAssignment = _repository.SearchFor <SageAssignment>(x => x.WorkOrder == model.WorkOrder).Single();

            var employee = _repository.SearchFor <SageEmployee>(x => x.Employee == model.Employee).SingleOrDefault();

            databaseAssignment.Employee             = employee?.Name ?? "";
            databaseAssignment.ScheduleDate         = model.ScheduleDate;
            databaseAssignment.WorkOrder            = model.WorkOrder;
            databaseAssignment.EstimatedRepairHours = model.EstimatedRepairHours;
            databaseAssignment.StartTime            = model.ScheduleDate;
            databaseAssignment.Enddate = model.EndDate;
            databaseAssignment.Endtime = model.EndDate;

            var edited = _sageApiProxy.EditAssignment(databaseAssignment);

            if (edited.IsSucceed == false)
            {
                _log.ErrorFormat("edited == null. Error.");
                return(false);
            }

            databaseAssignment.EmployeeId = employee != null ? employee.Employee : 0;
            databaseAssignment.Start      = ((DateTime)edited.Entity.ScheduleDate).Add(((DateTime)edited.Entity.StartTime).TimeOfDay).ToString();
            databaseAssignment.End        = ((DateTime)edited.Entity.ScheduleDate).Add(((DateTime)edited.Entity.StartTime).TimeOfDay).AddHours(edited.Entity.EstimatedRepairHours.AsDouble()).ToString();
            databaseAssignment.Color      = employee?.Color ?? "";

            var workorder = _repository.SearchFor <SageWorkOrder>(w => w.WorkOrder == model.WorkOrder).SingleOrDefault();

            databaseAssignment.Customer = workorder.ARCustomer;
            databaseAssignment.Location = workorder.Location;
            var locations    = _repository.GetAll <SageLocation>().ToArray();
            var itemLocation = locations.FirstOrDefault(l => l.Name == workorder.Location);

            if (itemLocation == null)
            {
                itemLocation = new SageLocation();
                _locationService.ResolveLocation(itemLocation);
            }
            else if (itemLocation.Longitude == 0 || itemLocation.Longitude == 0)
            {
                _locationService.ResolveLocation(itemLocation);
            }
            workorder.ScheduleDate = databaseAssignment.ScheduleDate;
            workorder.Latitude     = itemLocation.Latitude;
            workorder.Longitude    = itemLocation.Longitude;
            workorder.Color        = databaseAssignment.Color;
            workorder.EmployeeId   = databaseAssignment.EmployeeId;
            workorder.Employee     = databaseAssignment.Employee;

            _repository.Update(databaseAssignment);
            workorder.AssignmentId = 0;
            _repository.Update(workorder);
            _repository.Update(itemLocation);


            _hub.CreateAssignment(new MapViewModel()
            {
                WorkOrder   = workorder,
                DateEntered = databaseAssignment.ScheduleDate,
                Employee    = employee?.Employee ?? 0,
                Color       = employee?.Color ?? ""
            });

            _notification.SendNotification(string.Format("Workorder {0} assigned to {1}", workorder.Name, employee.Name));
            _log.InfoFormat("DatabaseAssignment added to repository. Employee {0}, Employee ID {1}", databaseAssignment.Employee, databaseAssignment.EmployeeId);

            return(true);
        }