public void ExecutePlanning(IDemandToProvider demand, MrpTask task, int simulationId) { //creates Provider for the needs var productionOrders = _demandForecast.NetRequirement(demand, task, simulationId); if (demand.State != State.Finished) { demand.State = State.ProviderExist; } var articleBoms = _context.ArticleBoms .Include(a => a.ArticleChild) .ThenInclude(a => a.ArticleBoms) .ToList(); //If there was enough in stock this does not have to be produced if (!productionOrders.Any()) { return; } foreach (var productionOrder in productionOrders) { //if the ProductionOrder was just created, initialize concrete WorkSchedules for the ProductionOrders if (productionOrder.ProductionOrderWorkSchedule == null || !productionOrder.ProductionOrderWorkSchedule.Any()) { _context.CreateProductionOrderWorkSchedules(productionOrder); } var children = articleBoms.Where(a => a.ArticleParentId == productionOrder.ArticleId) .ToList(); if (!children.Any()) { return; } foreach (var child in children) { //create Requester var dpob = _context.CreateDemandProductionOrderBom(child.ArticleChildId, productionOrder.Quantity * (int)child.Quantity); //create Production-BOM _context.TryCreateProductionOrderBoms(dpob, productionOrder, simulationId); //call this method recursively for a depth-first search ExecutePlanning(dpob, task, simulationId); } } }