/// <summary> /// Set Start- and Endtime for ProductionOrderWorkSchedules for the given OrderPart excluding capacities in a backward schedule /// </summary> /// <param name="demand"></param> public void BackwardScheduling(IDemandToProvider demand) { var productionOrderWorkSchedules = new List <ProductionOrderWorkSchedule>(); _context.GetWorkSchedulesFromDemand(demand, ref productionOrderWorkSchedules); productionOrderWorkSchedules = productionOrderWorkSchedules.OrderBy(a => a.ProductionOrderId).ThenByDescending(b => b.HierarchyNumber).ToList(); foreach (var workSchedule in productionOrderWorkSchedules) { // set transition time relating to machine group. var transitionTime = Calculations.GetTransitionTimeForWorkSchedule(workSchedule); //Set hierarchy to something high that every workSchedule found will overwrite this value var hierarchyParent = _context.GetHierarchyParent(workSchedule); //if no hierarchy has been found if (hierarchyParent == null) { if (demand.State == State.ForwardScheduleExists) { workSchedule.EndForward = SetBackwardTimeFromParent(workSchedule, demand.State); } else { workSchedule.EndBackward = SetBackwardTimeFromParent(workSchedule, demand.State); } } else { if (demand.State == State.ForwardScheduleExists) { workSchedule.EndForward = _context.ProductionOrderWorkSchedules.Single(a => (a.HierarchyNumber == hierarchyParent.HierarchyNumber) && (a.ProductionOrderId == workSchedule.ProductionOrderId)).StartForward; } else { workSchedule.EndBackward = _context.ProductionOrderWorkSchedules.Single(a => (a.HierarchyNumber == hierarchyParent.HierarchyNumber) && (a.ProductionOrderId == workSchedule.ProductionOrderId)).StartBackward; } } if (demand.State == State.ForwardScheduleExists) { workSchedule.StartForward = workSchedule.EndForward - workSchedule.Duration - transitionTime; workSchedule.EndForward = workSchedule.StartForward + workSchedule.Duration; } else { workSchedule.StartBackward = workSchedule.EndBackward - workSchedule.Duration - transitionTime; workSchedule.EndBackward = workSchedule.StartBackward + workSchedule.Duration; } } _context.ProductionOrderWorkSchedules.UpdateRange(productionOrderWorkSchedules); _context.SaveChanges(); }