Example #1
0
        /// <summary>
        /// Set Start- and Endtime for ProductionOrderWorkSchedules for the given OrderPart excluding capacities in a forward schedule
        /// </summary>
        /// <param name="demand"></param>
        public void ForwardScheduling(IDemandToProvider demand)
        {
            var productionOrderWorkSchedules = new List <ProductionOrderWorkSchedule>();

            _context.GetWorkSchedulesFromDemand(demand, ref productionOrderWorkSchedules);
            productionOrderWorkSchedules =
                productionOrderWorkSchedules.OrderByDescending(a => a.ProductionOrderId).ThenBy(b => b.HierarchyNumber).ToList();
            foreach (var workSchedule in productionOrderWorkSchedules)
            {
                var hierarchy = -1;
                foreach (var schedule in workSchedule.ProductionOrder.ProductionOrderWorkSchedule)
                {
                    if (schedule.HierarchyNumber < workSchedule.HierarchyNumber && schedule.HierarchyNumber > hierarchy)
                    {
                        hierarchy = schedule.HierarchyNumber;
                    }
                }
                //if no hierarchy has been found
                if (hierarchy == -1)
                {
                    workSchedule.StartForward = SetForwardTimeFromChild(workSchedule);
                }
                else
                {
                    workSchedule.StartForward = _context.ProductionOrderWorkSchedules.AsNoTracking().Single(a =>
                                                                                                            (a.HierarchyNumber == hierarchy) &&
                                                                                                            (a.ProductionOrderId == workSchedule.ProductionOrderId)).EndForward;
                }
                workSchedule.EndForward = workSchedule.StartForward + workSchedule.Duration;
                _context.ProductionOrderWorkSchedules.Update(workSchedule);
                _context.SaveChanges();
            }
        }
Example #2
0
        /// <summary>
        /// Deepseach thorugh tree to Return all Workschedules Related to one Order.
        /// </summary>
        /// <param name="demandRequester"></param>
        /// <param name="productionOrderWorkSchedule"></param>
        /// <returns>List of ProductionOrderWorkSchedules - Attention May Include Dupes Through Complex Backlinks !</returns>
        public List <ProductionOrderWorkSchedule> GetWorkSchedulesFromDemand(IDemandToProvider demandRequester, ref List <ProductionOrderWorkSchedule> productionOrderWorkSchedule)
        {
            foreach (var item in demandRequester.DemandProvider.OfType <DemandProviderProductionOrder>())
            {
                var productionOrders = ProductionOrders
                                       .Include(x => x.ProductionOrderWorkSchedule)
                                       .ThenInclude(x => x.MachineGroup)
                                       .Include(x => x.ProductionOrderWorkSchedule)
                                       .ThenInclude(x => x.Machine)
                                       .Include(x => x.ProductionOrderBoms)
                                       .ThenInclude(x => x.DemandProductionOrderBoms)
                                       .ThenInclude(x => x.DemandProvider)
                                       .FirstOrDefault(x => x.Id == item.ProductionOrderId);

                productionOrderWorkSchedule.AddRange(productionOrders.ProductionOrderWorkSchedule);
                foreach (var po in productionOrders.ProductionOrderBoms)
                {
                    foreach (var dpob in po.DemandProductionOrderBoms)
                    {
                        GetWorkSchedulesFromDemand(dpob, ref productionOrderWorkSchedule);
                    }
                }
            }
            return(productionOrderWorkSchedule);
        }
Example #3
0
        private int GetDueTime(IDemandToProvider demand)
        {
            demand = _context.Demands.Single(a => a.Id == demand.Id);
            var latestEnd = 0;

            if (demand.State == State.ForwardScheduleExists)
            {
                /*long version of the below line
                 * foreach (var provider in demand.DemandProvider)
                 * {
                 *  if (provider.GetType() == typeof(DemandProviderProductionOrder))
                 *  {
                 *      foreach (var schedule in ((DemandProviderProductionOrder)provider).ProductionOrder.ProductionOrderWorkSchedule)
                 *      {
                 *          if (schedule.EndForward > latestEnd) latestEnd = schedule.EndForward;
                 *      }
                 *
                 *  }
                 * }*/
                latestEnd = (from provider in demand.DemandProvider where provider.GetType() == typeof(DemandProviderProductionOrder)
                             from schedule in ((DemandProviderProductionOrder)provider).ProductionOrder.ProductionOrderWorkSchedule
                             select schedule.EndForward).Concat(new[] { latestEnd }).Max();
                return(latestEnd);
            }
            demand = _context.Demands.AsNoTracking().Include(a => a.DemandRequester).Single(a => a.Id == demand.Id);
            if (demand.DemandRequesterId != null && demand.DemandRequester.GetType() == typeof(DemandOrderPart))
            {
                return(_context.OrderParts.Include(a => a.Order).Single(a => a.Id == ((DemandOrderPart)demand.DemandRequester).OrderPartId).Order.DueTime);
            }
            if (demand.DemandRequesterId == null && demand.GetType() == typeof(DemandOrderPart))
            {
                return(_context.OrderParts.Include(a => a.Order).Single(a => a.Id == ((DemandOrderPart)demand).OrderPartId).Order.DueTime);
            }
            return(999999);
        }
Example #4
0
        public PurchasePart CreatePurchase(IDemandToProvider demand, decimal amount)
        {
            var articleToPurchase = ArticleToBusinessPartners.Single(a => a.ArticleId == demand.ArticleId);
            var purchase          = new Purchase()
            {
                BusinessPartnerId = articleToPurchase.BusinessPartnerId,
                DueTime           = articleToPurchase.DueTime,
            };

            //amount to be purchased has to be raised to fit the packsize
            amount = Math.Ceiling(amount / articleToPurchase.PackSize) * articleToPurchase.PackSize;
            var purchasePart = new PurchasePart()
            {
                ArticleId = demand.ArticleId,
                Quantity  = (int)amount,
                DemandProviderPurchaseParts = new List <DemandProviderPurchasePart>(),
                PurchaseId = purchase.Id,
            };

            purchase.PurchaseParts = new List <PurchasePart>()
            {
                purchasePart
            };
            Purchases.Add(purchase);
            PurchaseParts.Add(purchasePart);
            SaveChanges();
            return(purchasePart);
        }
Example #5
0
        private void SatisfyRequest(IDemandToProvider demand, int simulationId, ProductionDomainContext evaluationContext)
        {
            var amount = demand.Quantity;

            //if anything is in stock, create demand
            amount = TryAssignStockReservation(demand, amount);

            if (amount == 0)
            {
                return;
            }

            //search for purchase
            amount = TryAssignPurchase(demand, amount);

            if (amount == 0)
            {
                return;
            }
            //find matching productionOrders
            amount = TryFindProductionOrders(demand, amount);

            if (amount != 0)
            {
                SaveContext(evaluationContext);
                amount = TryAssignStockReservation(demand, amount);
                amount = TryAssignPurchase(demand, amount);
                amount = TryFindProductionOrders(demand, amount);
                throw new NotSupportedException("logical error: still unsatisfied Requests!");
            }
            foreach (var dppo in demand.DemandProvider.OfType <DemandProviderProductionOrder>())
            {
                CallChildrenSatisfyRequest(dppo.ProductionOrder, simulationId, evaluationContext);
            }
        }
Example #6
0
        //Todo: check logic
        public void CreatePurchaseDemand(IDemandToProvider demand, decimal amount, int time)
        {
            if (NeedToRefill(demand, amount))
            {
                var providerPurchasePart = new DemandProviderPurchasePart()
                {
                    Quantity          = amount,
                    ArticleId         = demand.ArticleId,
                    DemandRequesterId = demand.Id,
                    State             = State.Created,
                };
                Demands.Add(providerPurchasePart);

                CreatePurchase(demand, amount, providerPurchasePart, time);
                Demands.Update(providerPurchasePart);
            }
            else
            {
                var providerStock = new DemandProviderStock()
                {
                    Quantity          = amount,
                    ArticleId         = demand.ArticleId,
                    DemandRequesterId = demand.Id,
                    State             = State.Created,
                    StockId           = Stocks.Single(a => a.ArticleForeignKey == demand.ArticleId).Id
                };
                Demands.Add(providerStock);
            }
            SaveChanges();
        }
Example #7
0
        /// <summary>
        /// Run requirements planning and backward/forward termination.
        /// </summary>
        /// <param name="demand"></param>
        /// <param name="task"></param>
        /// <param name="simulationId"></param>
        public void RunRequirementsAndTermination(IDemandToProvider demand, MrpTask task, int simulationId)
        {
            if (demand.State == State.Created)
            {
                ExecutePlanning(demand, task, simulationId);
                if (demand.State != State.Finished)
                {
                    demand.State = State.ProviderExist;
                }
                _context.Update(demand);
                _context.SaveChanges();
                _messageHub.SendToAllClients("Requirements planning completed.");
            }

            if (task == MrpTask.All || task == MrpTask.Backward)
            {
                _scheduling.BackwardScheduling(demand);
                demand.State = State.BackwardScheduleExists;
                _context.Update(demand);
                _context.SaveChanges();
                _messageHub.SendToAllClients("Backward schedule exists.");
            }

            if (task == MrpTask.All && CheckNeedForward(demand, simulationId) || task == MrpTask.Forward)
            {
                //schedules forward and then backward again with the finish of the forward algorithm
                _scheduling.ForwardScheduling(demand);
                demand.State = State.ForwardScheduleExists;
                _context.Update(demand);
                _context.SaveChanges();
                _scheduling.BackwardScheduling(demand);
                _messageHub.SendToAllClients("Forward schedule exists.");
            }
            _context.SaveChanges();
        }
Example #8
0
        private void SetStartEndFromTermination(IDemandToProvider demand)
        { //Todo: replace provider.first()
            var schedules = _context.ProductionOrderWorkSchedules
                            .Include(a => a.ProductionOrder)
                            .ThenInclude(a => a.DemandProviderProductionOrders)
                            .ThenInclude(a => a.DemandRequester)
                            .Where(a => a.ProductionOrder.DemandProviderProductionOrders.First().DemandRequester.DemandRequesterId == demand.Id ||
                                   a.ProductionOrder.DemandProviderProductionOrders.First().DemandRequesterId == demand.Id)
                            .ToList();


            foreach (var schedule in schedules)
            {
                //if forward was calculated take forward, else take from backward termination
                if (schedule.EndForward - schedule.StartForward > 0)
                {
                    schedule.End   = schedule.EndForward;
                    schedule.Start = schedule.StartForward;
                }
                else
                {
                    schedule.End   = schedule.EndBackward;
                    schedule.Start = schedule.StartBackward;
                }
                _context.ProductionOrderWorkSchedules.Update(schedule);
                _context.SaveChanges();
            }
        }
Example #9
0
        public bool NeedToRefill(IDemandToProvider demand, decimal amount)
        {
            var purchasedAmount = GetAmountBought(demand.ArticleId);
            var neededAmount    = GetReserved(demand.ArticleId);
            var stockMin        = Stocks.Single(a => a.ArticleForeignKey == demand.ArticleId).Min;

            return(purchasedAmount - neededAmount - amount < stockMin);
        }
Example #10
0
        private int GetActivitySlack(IDemandToProvider demandRequester, int simulationId)
        {
            var dueTime = 999999;

            if (demandRequester.DemandProvider != null)
            {
                dueTime = _context.GetDueTimeByOrder((DemandToProvider)demandRequester);
            }
            return(dueTime - _context.SimulationConfigurations.Single(a => a.Id == simulationId).Time);
        }
Example #11
0
 public void AssignProviderToDemand(IDemandToProvider demand, DemandToProvider provider)
 {
     if (demand.DemandProvider == null)
     {
         demand.DemandProvider = new List <DemandToProvider>();
     }
     demand.DemandProvider.Add(provider);
     Update(demand);
     SaveChanges();
 }
Example #12
0
        /// <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();
        }
Example #13
0
        private int CopyDemandsAndPows(IDemandToProvider dop, TimeTable <ISimulationItem> timetable, int simulationId, int simulationNumber)
        {
            var providerlist = dop.DemandProvider.OfType <DemandProviderProductionOrder>().ToList();
            var counter      = 0;

            while (providerlist.Any())
            {
                var provider = providerlist.First();
                foreach (var pob in provider.ProductionOrder.ProductionOrderBoms.ToList())
                {
                    foreach (var dpob in pob.DemandProductionOrderBoms.ToList())
                    {
                        CopyDemandsAndPows(dpob, timetable, simulationId, simulationNumber);
                    }
                    if (!pob.DemandProductionOrderBoms.Any())
                    {
                        _context.ProductionOrderBoms.Remove(pob);
                    }
                }
                provider = _context.Demands.OfType <DemandProviderProductionOrder>().Single(a => a.Id == provider.Id);
                if (provider.ProductionOrder.DemandProviderProductionOrders.Any(a => a.Id != provider.Id))
                {
                    _context.Demands.Remove(_context.Demands.Single(a => a.Id == provider.Id));
                    var po = _context.ProductionOrders.Single(a => a.Id == provider.ProductionOrderId);
                    po.DemandProviderProductionOrders.Remove(provider);
                    _context.Update(po);
                }
                else
                {
                    var items = new List <PowsSimulationItem>();
                    foreach (var schedule in provider.ProductionOrder.ProductionOrderWorkSchedule.ToList())
                    {
                        _context.ProductionOrderWorkSchedules.Remove(schedule);
                        var itemList = timetable.Items.OfType <PowsSimulationItem>()
                                       .Where(a => a.ProductionOrderWorkScheduleId == schedule.Id).ToList();
                        if (!itemList.Any())
                        {
                            continue;
                        }
                        items.Add(itemList.First());
                        timetable.Items.Remove(itemList.First());
                        counter++;
                    }
                    FillSimulationWorkSchedules(items, simulationId, simulationNumber, 0);

                    _context.ProductionOrders.Remove(provider.ProductionOrder);
                    _context.Demands.Remove(provider);
                    _context.Demands.Remove(provider.DemandRequester);
                }
                _context.SaveChanges();
                providerlist.RemoveAt(0);
            }
            return(counter);
        }
Example #14
0
        public ProductionOrder CreateProductionOrder(IDemandToProvider demand, int duetime, int simulationId)
        {
            var productionOrder = new ProductionOrder()
            {
                ArticleId = demand.Article.Id,
                Quantity  = SimulationConfigurations.Single(a => a.Id == simulationId).Lotsize,
                Duetime   = duetime
            };

            ProductionOrders.Add(productionOrder);
            SaveChanges();
            return(productionOrder);
        }
Example #15
0
        public DemandProviderProductionOrder CreateProviderProductionOrder(IDemandToProvider demand, ProductionOrder productionOrder, decimal amount)
        {
            var dppo = new DemandProviderProductionOrder()
            {
                ArticleId         = demand.ArticleId,
                Quantity          = amount,
                ProductionOrderId = productionOrder.Id,
                DemandRequesterId = demand.DemandRequesterId,
            };

            Add(dppo);
            SaveChanges();
            return(dppo);
        }
Example #16
0
        public DemandProviderPurchasePart CreateDemandProviderPurchasePart(IDemandToProvider demand, PurchasePart purchase, decimal amount)
        {
            var dppp = new DemandProviderPurchasePart()
            {
                ArticleId         = demand.ArticleId,
                Quantity          = amount,
                PurchasePartId    = purchase.Id,
                DemandRequesterId = demand.Id,
            };

            Add(dppp);
            SaveChanges();
            return(dppp);
        }
Example #17
0
        public DemandProviderStock CreateDemandProviderStock(IDemandToProvider demand, decimal amount)
        {
            var dps = new DemandProviderStock()
            {
                ArticleId         = demand.ArticleId,
                Quantity          = amount,
                StockId           = Stocks.Single(a => a.ArticleForeignKey == demand.ArticleId).Id,
                DemandRequesterId = demand.Id,
                State             = State.Created
            };

            Add(dps);
            SaveChanges();
            return(dps);
        }
Example #18
0
        private decimal TryAssignPurchase(IDemandToProvider demand, decimal amount)
        {
            var purchaseParts         = _context.PurchaseParts.Where(a => a.State != State.Finished && a.ArticleId == demand.ArticleId).ToList();
            var amountAlreadyReserved = purchaseParts.Select(a => a.DemandProviderPurchaseParts.Sum(b => b.Quantity)).Sum();
            var amountReservable      = purchaseParts.Select(a => a.Quantity).Sum() - amountAlreadyReserved;

            if (amountReservable == 0)
            {
                return(amount);
            }
            var amountReserving = amountReservable > amount ? amount : amountReservable;

            _context.CreateDemandProviderStock(demand, amountReserving);
            amount -= amountReserving;
            return(amount);
        }
Example #19
0
        private decimal TryAssignStockReservation(IDemandToProvider demand, decimal amount)
        {
            var articleStock = _context.Stocks.AsNoTracking().Single(a => a.ArticleForeignKey == demand.ArticleId).Current;

            if (articleStock <= 0)
            {
                return(amount);
            }
            var provider = _context.TryCreateStockReservation(demand);

            if (provider != null)
            {
                amount -= provider.Quantity;
            }
            return(amount);
        }
Example #20
0
        public DemandStock CreateStockDemand(IDemandToProvider demand, Stock stock, decimal amount)
        {
            var demandStock = new DemandStock()
            {
                Quantity       = amount,
                Article        = demand.Article,
                ArticleId      = demand.ArticleId,
                State          = State.Created,
                DemandProvider = new List <DemandToProvider>(),
                StockId        = stock.Id,
            };

            Demands.Add(demandStock);
            SaveChanges();
            return(demandStock);
        }
Example #21
0
        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);
                }
            }
        }
Example #22
0
        public List <ProductionOrder> CheckForProductionOrders(IDemandToProvider demand, decimal amount, int timer)
        {
            var possibleProductionOrders       = new List <ProductionOrder>();
            var perfectFittingProductionOrders = new List <ProductionOrder>();
            var pos = ProductionOrders.Include(b => b.ProductionOrderWorkSchedule)
                      .Include(a => a.DemandProviderProductionOrders)
                      .Where(a => a.ArticleId == demand.ArticleId &&
                             (GetLatestEndFromProductionOrder(a) == null ||
                              GetLatestEndFromProductionOrder(a) == 0 ||
                              GetLatestEndFromProductionOrder(a) >= timer)).ToList();

            foreach (var po in pos)
            {
                var availableAmount = GetAvailableAmountFromProductionOrder(po);
                if (availableAmount == amount)
                {
                    perfectFittingProductionOrders.Add(po);
                }
                else if (availableAmount > 0)
                {
                    possibleProductionOrders.Add(po);
                }
            }
            if (!possibleProductionOrders.Any())
            {
                return(null);
            }
            if (perfectFittingProductionOrders.Any())
            {
                return new List <ProductionOrder>()
                       {
                           GetEarliestProductionOrder(perfectFittingProductionOrders)
                       }
            }
            ;
            var list = new List <ProductionOrder>();

            while (amount > 0 && possibleProductionOrders.Any())
            {
                list.Add(GetEarliestProductionOrder(possibleProductionOrders));

                possibleProductionOrders.Remove(list.Last());
                amount -= GetAvailableAmountFromProductionOrder(list.Last());
            }
            return(list);
        }
Example #23
0
        /***
         * Requirements
         * */
        /*
         * /// <summary>
         * /// Creates stock reservation if possible
         * /// </summary>
         * /// <param name="stock"></param>
         * /// <param name="demand"></param>
         * public void TryCreateStockReservation(Stock stock, IDemandToProvider demand)
         * {
         * var stockReservations = GetReserved(demand.ArticleId);
         * var bought = GetAmountBought(demand.ArticleId);
         * //get the current amount of free available articles
         * var current = stock.Current + bought - stockReservations;
         * decimal quantity;
         * //either reserve all that are in stock or the amount needed
         * quantity = demand.Quantity > current ? current : demand.Quantity;
         * if (quantity == 0) return;
         * var demandProviderStock = new DemandProviderStock()
         * {
         *     ArticleId = stock.ArticleForeignKey,
         *     Quantity = quantity,
         *     DemandRequesterId = demand.DemandRequesterId ?? demand.Id,
         *     StockId = stock.Id
         * };
         * Demands.Add(demandProviderStock);
         * SaveChanges();
         * }*/

        public List <ProductionOrder> CreateChildProductionOrders(IDemandToProvider demand, decimal amount, int simulationId)
        {
            ProductionOrderBom bom = null;

            if (demand.GetType() == typeof(DemandProductionOrderBom))
            {
                bom = ProductionOrderBoms.FirstOrDefault(a => a.Id == ((DemandProductionOrderBom)demand).ProductionOrderBomId);
            }

            var lotsize          = SimulationConfigurations.Single(a => a.Id == simulationId).Lotsize;
            var productionOrders = new List <ProductionOrder>();

            /*decimal bomQuantity;
             * if (bom != null)
             *  bomQuantity = ArticleBoms.AsNoTracking().Single(a => a.ArticleChildId == demand.ArticleId &&
             *      a.ArticleParentId == bom.ProductionOrderParent.ArticleId).Quantity * lotsize;
             * else
             *  bomQuantity = ArticleBoms.AsNoTracking().Single(a =>a.ArticleChildId == demand.ArticleId &&
             *      a.ArticleParentId == null).Quantity * lotsize;
             */
            while (amount > 0)// || bomQuantity > 0)
            {
                var productionOrder = CreateProductionOrder(demand, GetDueTimeByOrder(demand), simulationId);
                if (amount > 0)
                {
                    var demandProviderProductionOrder = CreateDemandProviderProductionOrder(demand, productionOrder,
                                                                                            amount > lotsize ? lotsize : amount);

                    //if the article has a parent create a relationship
                    if (bom != null)
                    {
                        demandProviderProductionOrder.DemandRequesterId = demand.Id;
                        Demands.Update(demandProviderProductionOrder);
                    }
                }
                SaveChanges();
                amount -= lotsize;
                //bomQuantity -= lotsize;
                productionOrders.Add(productionOrder);
            }

            return(productionOrders);
        }
Example #24
0
        public DemandProviderStock TryCreateStockReservation(IDemandToProvider demand)
        {
            var stock             = Stocks.Single(a => a.ArticleForeignKey == demand.ArticleId);
            var stockReservations = GetReserved(demand.ArticleId);
            var bought            = GetAmountBought(demand.ArticleId);
            var justProduced      = Demands.OfType <DemandProductionOrderBom>()
                                    .Where(a => (a.State != State.Finished || a.ProductionOrderBom.ProductionOrderParent.ProductionOrderWorkSchedule.All(b => b.ProducingState == ProducingState.Created || b.ProducingState == ProducingState.Waiting)) &&
                                           a.ArticleId == demand.ArticleId &&
                                           a.DemandProvider.Any() &&
                                           a.DemandProvider.All(b => b.State == State.Finished)).Sum(a => a.Quantity);

            //get the current amount of free available articles
            var     current = stock.Current + bought - stockReservations - justProduced;
            decimal quantity;

            //either reserve all that are in stock or the amount needed
            quantity = demand.Quantity > current ? current : demand.Quantity;

            return(quantity <= 0 ? null : CreateDemandProviderStock(demand, quantity));
        }
Example #25
0
        public DemandProviderProductionOrder CreateDemandProviderProductionOrder(IDemandToProvider demand, ProductionOrder productionOrder, decimal amount)
        {
            var demandProviderProductionOrder = new DemandProviderProductionOrder()
            {
                Quantity          = amount,
                Article           = demand.Article,
                ArticleId         = demand.ArticleId,
                ProductionOrderId = productionOrder.Id,
                DemandRequesterId = demand.Id
            };

            Demands.Add(demandProviderProductionOrder);
            if (productionOrder.DemandProviderProductionOrders == null)
            {
                productionOrder.DemandProviderProductionOrders = new List <DemandProviderProductionOrder>();
            }
            productionOrder.DemandProviderProductionOrders.Add(demandProviderProductionOrder);
            SaveChanges();

            return(demandProviderProductionOrder);
        }
Example #26
0
        public decimal GetPlannedStock(Stock stock, IDemandToProvider demand)
        {
            var amountReserved = GetReserved(demand.ArticleId);
            var amountBought   = 0;
            var articlesBought = PurchaseParts.Where(a => a.ArticleId == demand.ArticleId && a.State != State.Finished);

            foreach (var articleBought in articlesBought)
            {
                amountBought += articleBought.Quantity;
            }
            //just produced articles have a reason and parents they got produced for so they cannot be reserved by another requester
            var amountJustProduced = Demands.OfType <DemandProductionOrderBom>()
                                     .Where(a => (a.State != State.Finished || a.ProductionOrderBom.ProductionOrderParent.ProductionOrderWorkSchedule.All(b => b.ProducingState == ProducingState.Created || b.ProducingState == ProducingState.Waiting)) &&
                                            a.ArticleId == demand.ArticleId &&
                                            a.DemandProvider.Any() &&
                                            a.DemandProvider.All(b => b.State == State.Finished)).Sum(a => a.Quantity);
            //plannedStock is the amount of this article in stock after taking out the amount needed
            var plannedStock = stock.Current + amountBought - demand.Quantity - amountReserved - amountJustProduced;

            return(plannedStock);
        }
Example #27
0
        public ProductionOrderBom TryCreateProductionOrderBoms(IDemandToProvider demand, ProductionOrder parentProductionOrder, int simulationId)
        {
            if (parentProductionOrder == null)
            {
                return(null);
            }
            var lotsize  = SimulationConfigurations.Single(a => a.Id == simulationId).Lotsize;
            var quantity = demand.Quantity > lotsize ? lotsize : demand.Quantity;
            var pob      = new ProductionOrderBom()
            {
                Quantity = quantity,
                ProductionOrderParentId = parentProductionOrder.Id
            };

            Add(pob);
            SaveChanges();
            if (demand.GetType() == typeof(DemandProductionOrderBom))
            {
                AssignDemandProviderToProductionOrderBom((DemandProductionOrderBom)demand, pob);
            }
            return(pob);
        }
Example #28
0
        private decimal TryFindProductionOrders(IDemandToProvider demand, decimal amount)
        {
            var possibleMatchingProductionOrders = _context.ProductionOrders.Where(a => a.ArticleId == demand.ArticleId && a.ProductionOrderWorkSchedule.Any(b => b.ProducingState != ProducingState.Finished)).ToList();

            while (amount > 0 && possibleMatchingProductionOrders.Any())
            {
                var earliestProductionOrder            = _context.GetEarliestProductionOrder(possibleMatchingProductionOrders);
                var availableAmountFromProductionOrder = _context.GetAvailableAmountFromProductionOrder(earliestProductionOrder);
                if (availableAmountFromProductionOrder == 0)
                {
                    possibleMatchingProductionOrders.Remove(possibleMatchingProductionOrders.Find(a => a.Id == earliestProductionOrder.Id));
                    continue;
                }
                var provider = _context.CreateProviderProductionOrder(demand, earliestProductionOrder, amount >= availableAmountFromProductionOrder ? availableAmountFromProductionOrder : amount);
                var duetime  = _context.GetDueTimeByOrder(demand);
                if (provider.ProductionOrder.DemandProviderProductionOrders.Count == 1)
                {
                    provider.ProductionOrder.Duetime = duetime;
                }
                else if (provider.ProductionOrder.Duetime > duetime)
                {
                    provider.ProductionOrder.Duetime = duetime;
                }
                _context.AssignProviderToDemand(demand, provider);
                _context.AssignProductionOrderToDemandProvider(earliestProductionOrder, provider);
                if (amount > availableAmountFromProductionOrder)
                {
                    amount -= availableAmountFromProductionOrder;
                }
                else
                {
                    amount = 0;
                }
                possibleMatchingProductionOrders.ToList().Remove(earliestProductionOrder);
            }
            return(amount);
        }
Example #29
0
 public int GetDueTimeByOrder(IDemandToProvider demand)
 {
     if (demand.GetType() == typeof(DemandOrderPart))
     {
         demand = Demands.OfType <DemandOrderPart>().Include(a => a.OrderPart).ThenInclude(b => b.Order).ToList().Single(a => a.Id == demand.Id);
         return(((DemandOrderPart)demand).OrderPart.Order.DueTime);
     }
     if (demand.GetType() == typeof(DemandStock))
     {
         return(int.MaxValue);
     }
     if (demand.GetType() != typeof(DemandProductionOrderBom))
     {
         return(int.MaxValue);
     }
     {
         demand =
             Demands.AsNoTracking().OfType <DemandProductionOrderBom>()
             .Include(a => a.ProductionOrderBom)
             .ThenInclude(b => b.ProductionOrderParent)
             .Single(c => c.Id == demand.Id);
         return(((DemandProductionOrderBom)demand).ProductionOrderBom.ProductionOrderParent.Duetime);
     }
 }
Example #30
0
 public bool HasChildren(IDemandToProvider demand)
 {
     return(ArticleBoms.Any(a => a.ArticleParentId == demand.ArticleId));
 }