Example #1
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);
        }