public static Job Create(DiscoDataContext Database, Device device, User user, JobType type, List<JobSubType> subTypes, User initialTech, bool addAutoQueues = true) { Job j = new Job() { JobType = type, OpenedTechUserId = initialTech.UserId, OpenedTechUser = initialTech, OpenedDate = DateTime.Now }; // Device if (device != null) { j.Device = device; j.DeviceSerialNumber = device.SerialNumber; } // User if (user != null) { j.User = user; j.UserId = user.UserId; } // Sub Types List<JobSubType> jobSubTypes = subTypes.ToList(); j.JobSubTypes = jobSubTypes; Database.Jobs.Add(j); // Job Queues if (addAutoQueues) { var queues = from st in subTypes from jq in st.JobQueues group st by jq into g select new { queue = g.Key, subTypes = g }; foreach (var queue in queues) { var commentBuilder = new StringBuilder("Automatically added by:").AppendLine(); foreach (var subType in queue.subTypes) { commentBuilder.AppendLine().Append("* ").Append(subType.Description); } var jqj = new JobQueueJob() { JobQueueId = queue.queue.Id, Job = j, AddedDate = DateTime.Now, AddedUserId = initialTech.UserId, AddedComment = commentBuilder.ToString(), SLAExpiresDate = queue.queue.DefaultSLAExpiry.HasValue ? (DateTime?)DateTime.Now.AddMinutes(queue.queue.DefaultSLAExpiry.Value) : null, Priority = JobQueuePriority.Normal }; Database.JobQueueJobs.Add(jqj); } } switch (type.Id) { case JobType.JobTypeIds.HWar: Database.JobMetaWarranties.Add(new JobMetaWarranty() { Job = j }); break; case JobType.JobTypeIds.HNWar: Database.JobMetaNonWarranties.Add(new JobMetaNonWarranty() { Job = j }); if (device != null) { // Add Job Components var components = Database.DeviceComponents.Include("JobSubTypes").Where(c => !c.DeviceModelId.HasValue || c.DeviceModelId == j.Device.DeviceModelId); var addedComponents = new List<DeviceComponent>(); foreach (var c in components) { if (c.JobSubTypes.Count == 0) { // No Filter addedComponents.Add(c); } else { foreach (var st in c.JobSubTypes) { foreach (var jst in jobSubTypes) { if (st.JobTypeId == jst.JobTypeId && st.Id == jst.Id) { addedComponents.Add(c); break; } } if (addedComponents.Contains(c)) break; } } } foreach (var c in addedComponents) Database.JobComponents.Add(new JobComponent() { Job = j, TechUserId = initialTech.UserId, Cost = c.Cost, Description = c.Description }); } break; } return j; }
private static bool CanCloseNever(this Job j, JobQueueJob IgnoreJobQueueJob = null) { if (!UserService.CurrentAuthorization.Has(Claims.Job.Actions.Close)) return true; if (j.ClosedDate.HasValue) return true; // Job already Closed if (j.DeviceHeld.HasValue && !j.DeviceReturnedDate.HasValue) return true; // Device not returned to User if (j.WaitingForUserAction.HasValue) return true; // Job waiting on User Action if (j.JobQueues != null) { if (IgnoreJobQueueJob == null) { if (j.JobQueues.Any(jqj => !jqj.RemovedDate.HasValue)) return true; // Job associated with a Job Queue } else { if (j.JobQueues.Any(jqj => jqj.Id != IgnoreJobQueueJob.Id && !jqj.RemovedDate.HasValue)) return true; // Job associated with a Job Queue } } return false; }
private void UpdatePriority(JobQueueJob jobQueueJob, string Priority) { if (!jobQueueJob.CanEditPriority()) throw new InvalidOperationException("Editing Priority for job queue job is Denied"); JobQueuePriority priority; if (!Enum.TryParse<JobQueuePriority>(Priority, out priority)) throw new ArgumentException("Invalid Priority Value", "Priority"); jobQueueJob.OnEditPriority(priority); Database.SaveChanges(); }
private void UpdateSla(JobQueueJob jobQueueJob, string Sla) { if (!jobQueueJob.CanEditSla()) throw new InvalidOperationException("Editing SLA for job queue job is Denied"); if (!string.IsNullOrEmpty(Sla)) { DateTime SLADate; if (DateTime.TryParse(Sla, out SLADate)) { jobQueueJob.OnEditSla(SLADate); Database.SaveChanges(); } else { throw new ArgumentException("Unable to Parse SLA Date", "SLA"); } } else { jobQueueJob.OnEditSla(null); Database.SaveChanges(); } }
private void UpdateRemovedComment(JobQueueJob jobQueueJob, string RemovedComment) { if (!jobQueueJob.CanEditRemovedComment()) throw new InvalidOperationException("Editing removed comment for job queue job is Denied"); jobQueueJob.OnEditRemovedComment(RemovedComment); Database.SaveChanges(); }
public static JobQueueJob OnAddQueue(this Job j, DiscoDataContext Database, JobQueue jq, User Technician, string Comment, DateTime? SLAExpires, JobQueuePriority Priority) { if (!j.CanAddQueue(jq)) throw new InvalidOperationException("Adding job to queue is denied"); if (SLAExpires.HasValue && SLAExpires.Value < DateTime.Now) throw new ArgumentException("The SLA Date must be greater than the current time", "SLAExpires"); var jqj = new JobQueueJob() { JobQueueId = jq.Id, JobId = j.Id, AddedDate = DateTime.Now, AddedUserId = Technician.UserId, AddedComment = string.IsNullOrWhiteSpace(Comment) ? null : Comment.Trim(), SLAExpiresDate = SLAExpires, Priority = Priority }; Database.JobQueueJobs.Add(jqj); return jqj; }