/// <summary> /// Create a list of tasks for a visit. A workorder can have multiple visits /// Based on the WorkOrder.SelectedTasks a list is created with task attached to the actual visit it was executed /// </summary> /// <param name="visit">The visit for which the Tasks are requested</param> /// <returns></returns> public ICollection <Task> GetTasksForVisit(Visit visit) { // For a visit, all Tasks defined in WorkOrder.SelectedTasks have to be performed by a technician. // In redo scenario's, it is possible that multiple Visits are created. // Each Visit shows the same number of tasks (as defined by WorkOrder.SelectedTasks), with their corrsponding status if (visit == null) { return(null); } var tasks = new List <Task>(); foreach (var taskInfo in TaskInfos) { //Only the last task from a certain specification is shown var existingTask = Visits.SelectMany(v => v.Tasks).LastOrDefault(t => t.TaskInfo.TaskSpecificationId == taskInfo.TaskSpecificationId && !t.IsDeleted); if (existingTask == null) { existingTask = Task.Create(taskInfo, null); //Task does not yet exist on visit => Create Virtual Task to show in list existingTask.VisitId = visit.Id; existingTask.Visit = visit; existingTask.EndEdit(); existingTask.Delete(); } else { //Task => set to unmodified (Task boundary) existingTask.EndEdit(); } tasks.Add(existingTask); } return(new ObservableCollection <Task>(tasks.OrderBy(t => t.TaskInfo.SeqNo))); }