Ejemplo n.º 1
0
        public void InsertService(Service service)
        {
            //If the service being inserted has a RecurringServiceId
            //Find any RouteTasks that have the same RecurringServiceId and Date
            //Update the ServiceId on the RouteTask with the Id of the newly created Service
            if (service.RecurringServiceId != null)
            {
                var routeTasks = this.ObjectContext.RouteTasks.Where(rt => rt.RecurringServiceId == service.RecurringServiceId && rt.Date == service.ServiceDate).ToArray();

                foreach (var routeTask in routeTasks)
                    routeTask.ServiceId = service.Id;
            }

            if ((service.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(service, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Services.AddObject(service);
            }
        }
        public void UpdateRouteTask(RouteTask currentRouteTask)
        {
            var original = this.ObjectContext.RouteTasks.Include("Service").First(rt => rt.Id == currentRouteTask.Id);
            this.ObjectContext.Detach(original);

            //User changes date of a route task in the task board
            if (original.Date != currentRouteTask.Date)
            {
                var service = this.ObjectContext.Services.FirstOrDefault(s => s.Id == original.ServiceId);

                if (service != null)
                    service.ServiceDate = currentRouteTask.Date;

                var recurringService = this.ObjectContext.RecurringServices.FirstOrDefault(rs => rs.Id == original.RecurringServiceId)
                                            ?? this.ObjectContext.RecurringServices.FirstOrDefault(rs => rs.Id == service.RecurringServiceId.Value);

                if (recurringService != null)
                {
                    var excludedDates = recurringService.ExcludedDates.ToList();
                    excludedDates.Add(original.Date);
                    recurringService.ExcludedDates = excludedDates;
                    recurringService.LastModified = DateTime.UtcNow;
                    recurringService.LastModifyingUserId = CurrentUserAccount().Id;

                    //If there wasnt previously a saved Service, make one. Otherwise, the service will get lost
                    if (service == null)
                    {
                        //Generate new service
                        var newService = new Service
                        {
                            ServiceDate = currentRouteTask.Date,
                            ClientId = recurringService.ClientId,
                            RecurringServiceId = recurringService.Id,
                            ServiceProviderId = recurringService.Client.BusinessAccount.Id,
                            ServiceTemplate = recurringService.ServiceTemplate.MakeChild(ServiceTemplateLevel.ServiceDefined),
                            CreatedDate = DateTime.UtcNow,
                            LastModified = DateTime.UtcNow,
                            LastModifyingUserId = CurrentUserAccount().Id
                        };
                        newService.Id = newService.ServiceTemplate.Id;

                        //Add the RouteTask to the Service
                        newService.RouteTasks.Add(currentRouteTask);

                        ObjectContext.Services.AddObject(newService);
                    }
                }

                //Set original date on the route task
                //Used in sql function to generate route tasks for the day
                currentRouteTask.OriginalDate = original.Date;
            }

            currentRouteTask.LastModified = DateTime.UtcNow;
            currentRouteTask.LastModifyingUserId = CurrentUserAccount().Id;

            this.ObjectContext.RouteTasks.AttachAsModified(currentRouteTask);
        }
Ejemplo n.º 3
0
        public void UpdateService(Service currentService)
        {
            var originalService = ChangeSet.GetOriginal(currentService);

            var user = ObjectContext.CurrentUserAccount();

            //If the original date was prior to today, throw an exception
            if (originalService.ServiceDate < user.Now().Date)
                throw new Exception("Cannot change the date of a Service in the past.");

            //Delete any associated route tasks (today/in the future) if the service date changed
            if (originalService.ServiceDate != currentService.ServiceDate)
            {
                var routeTasksToUpdate = this.ObjectContext.RouteTasks.Where(rt =>
                    rt.ServiceId == currentService.Id || (rt.RecurringServiceId == originalService.RecurringServiceId && rt.Date == originalService.ServiceDate)).ToArray();

                //Remove the RouteTask from its RouteDestination, change the date and set its TaskStatus to CreatedDefault
                foreach (var routeTask in routeTasksToUpdate)
                {
                    routeTask.RouteDestination = null;
                    routeTask.RouteDestinationId = null;

                    routeTask.Date = currentService.ServiceDate;

                    routeTask.TaskStatus = this.ObjectContext.TaskStatuses.FirstOrDefault(ts => ts.BusinessAccountId == routeTask.BusinessAccountId && ts.DefaultTypeInt == ((int)StatusDetail.CreatedDefault));

                    routeTask.LastModified = DateTime.UtcNow;
                    routeTask.LastModifyingUserId = CurrentUserAccount().Id;
                }
            }

            currentService.LastModified = DateTime.UtcNow;
            currentService.LastModifyingUserId = CurrentUserAccount().Id;

            this.ObjectContext.Services.AttachAsModified(currentService);
        }
Ejemplo n.º 4
0
        public void DeleteService(Service service)
        {
            if (service.Generated)
                return;

            if ((service.EntityState == EntityState.Detached))
                this.ObjectContext.Services.Attach(service);

            if (service.ServiceTemplate != null)
                this.DeleteServiceTemplate(service.ServiceTemplate);

            var routeTasksToDelete = this.ObjectContext.RouteTasks.Where(rt =>
                 rt.ServiceId == service.Id || (rt.RecurringServiceId == service.RecurringServiceId && rt.Date == service.ServiceDate)).ToArray();

            DeleteRouteTasks(routeTasksToDelete);

            if ((service.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(service, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Services.DeleteObject(service);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Called when the Service details are loaded.
        /// </summary>
        /// <param name="service">The loaded service for this ServiceHolder.</param>
        public void OnLoadServiceDetails(Service service)
        {
            ClearPreviouslyLoadedDetails();

            this.Service = service;
            _entityGraph = this.Service.EntityGraph();

            this.OccurDate = Service.ServiceDate;
            this.RecurringServiceId = Service.RecurringServiceId;

            DetailsLoaded = true;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Use this constructor to create a new Service from the passed Service.
        /// </summary>
        public ServiceHolder(Service newService)
        {
            Service = newService;
            OccurDate = newService.ServiceDate;
            ServiceName = newService.ServiceTemplate.Name;

            //The details will already be loaded from the newService
            DetailsLoaded = true;

            //When this is saved, convert it to an existing service
            newService.PropertyChanged += OnSaveConvertToExistingService;
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Services EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToServices(Service service)
 {
     base.AddObject("Services", service);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Create a new Service object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="clientId">Initial value of the ClientId property.</param>
 /// <param name="serviceProviderId">Initial value of the ServiceProviderId property.</param>
 /// <param name="serviceDate">Initial value of the ServiceDate property.</param>
 /// <param name="createdDate">Initial value of the CreatedDate property.</param>
 public static Service CreateService(global::System.Guid id, global::System.Guid clientId, global::System.Guid serviceProviderId, global::System.DateTime serviceDate, global::System.DateTime createdDate)
 {
     Service service = new Service();
     service.Id = id;
     service.ClientId = clientId;
     service.ServiceProviderId = serviceProviderId;
     service.ServiceDate = serviceDate;
     service.CreatedDate = createdDate;
     return service;
 }