| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 | 
							- using Comal.Classes;
 
- using InABox.Core;
 
- namespace Comal.Stores
 
- {
 
-     public class JobStore : ScheduleActionStore<Job>
 
-     {
 
-         protected override void BeforeSave(Job entity)
 
-         {
 
-             base.BeforeSave(entity);
 
-             if (entity.ID == Guid.Empty)
 
-             {
 
-                 var defstatus = Provider.Query(
 
-                     new Filter<JobStatus>(x => x.Default).IsEqualTo(true),
 
-                     Columns.None<JobStatus>().Add(x => x.ID)
 
-                 ).Rows.FirstOrDefault()?.ToObject<JobStatus>();
 
-                 if (defstatus != null)
 
-                     entity.JobStatus.CopyFrom(defstatus);
 
-             }
 
-             if (!entity.Account.IsValid() && entity.Customer.IsValid())
 
-             {
 
-                 Customer final = null;
 
-                 var customer = Provider.Load(new Filter<Customer>(x => x.ID).IsEqualTo(entity.Customer.ID)).FirstOrDefault();
 
-                 if (customer != null)
 
-                 {
 
-                     final = customer;
 
-                     if (customer.Account.IsValid())
 
-                     {
 
-                         var account = Provider.Load(new Filter<Customer>(x => x.ID).IsEqualTo(customer.Account.ID)).FirstOrDefault();
 
-                         if (account != null)
 
-                             final = account;
 
-                     }
 
-                 }
 
-                 entity.Account.Synchronise(final ?? new Customer());
 
-             }
 
-             StoreUtils.Geocode(entity.SiteAddress);
 
-         }
 
-         protected override void AfterSave(Job entity)
 
-         {
 
-             base.AfterSave(entity);
 
-             if(entity.HasOriginalValue(x => x.ID))
 
-                 CreateDefaultScope(entity);
 
-         }
 
-         private void CreateDefaultScope(Job job)
 
-         {
 
-             var scope = new JobScope
 
-             {
 
-                 Description = "Default Scope",
 
-                 Type = JobScopeType.Contract
 
-             };
 
-             scope.Job.ID = job.ID;
 
-             
 
-             FindSubStore<JobScope>().Save(scope, "Created automatically on job creation.");
 
-             // Save the job again, with a default scope set. We create a lighter job so we're not saving all the same properties over again.
 
-             var lightJob = new Job().SetID(job.ID);
 
-             lightJob.DefaultScope.ID = scope.ID;
 
-             Provider.Save(lightJob);
 
-             // Update default scope for sending the entity back to the client.
 
-             job.DefaultScope.ID = scope.ID;
 
-         }
 
-         protected override void AfterDelete(Job entity)
 
-         {
 
-             base.AfterDelete(entity);
 
-             var setoutstore = FindSubStore<Setout>();
 
-             var setouts = setoutstore.Query(
 
-                 new Filter<Setout>(x => x.JobLink.ID).IsEqualTo(entity.ID),
 
-                 null).ToObjects<Setout>();
 
-             setoutstore.Delete(setouts, "Cascaded Delete from Job");
 
-         }
 
-     }
 
- }
 
 
  |