Example #1
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 #2
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 #3
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 #4
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 #5
0
        /// <summary>
        /// Creates providers for the demands through stock, productionOrders or purchases
        /// </summary>
        /// <param name="demand"></param>
        /// <param name="task"></param>
        /// <param name="simulationId"></param>
        /// <returns>ProductionOrder to fulfill the demand, ProductionOrder is null if there was enough in stock</returns>
        public List <ProductionOrder> NetRequirement(IDemandToProvider demand, MrpTask task, int simulationId)
        {
            //Todo: search for available POs
            var stock = _context.Stocks.Include(a => a.DemandStocks)
                        .Single(a => a.ArticleForeignKey == demand.ArticleId);
            var plannedStock     = _context.GetPlannedStock(stock, demand);
            var productionOrders = new List <ProductionOrder>();
            var stockProvider    = _context.TryCreateStockReservation(demand);
            var time             = _context.SimulationConfigurations.Single(a => a.Id == simulationId).Time;
            //if the article has no children it has to be purchased
            var children = _context.ArticleBoms.Where(a => a.ArticleParentId == demand.ArticleId).ToList();

            if (children.Any())
            {
                //if the plannedStock is below zero, articles have to be produced for its negative amount
                if (plannedStock < 0)
                {
                    var fittingProductionOrders = _context.CheckForProductionOrders(demand, -plannedStock, _context.SimulationConfigurations.Single(a => a.Id == simulationId).Time);
                    var amount = -plannedStock;
                    if (fittingProductionOrders != null)
                    {
                        foreach (var fittingProductionOrder in fittingProductionOrders)
                        {
                            var availableAmount = _context.GetAvailableAmountFromProductionOrder(fittingProductionOrder);
                            var provider        = _context.CreateDemandProviderProductionOrder(demand, fittingProductionOrder,
                                                                                               availableAmount < -plannedStock ? availableAmount : -plannedStock);
                            //productionOrders.Add(fittingProductionOrder);
                            _context.AssignProductionOrderToDemandProvider(fittingProductionOrder, provider);
                            amount -= availableAmount < -plannedStock ? availableAmount : -plannedStock;
                            if (amount == 0)
                            {
                                return(productionOrders);
                            }
                        }
                    }
                    if (amount > 0)
                    {
                        var pos = _context.CreateChildProductionOrders(demand, amount, simulationId);
                        productionOrders.AddRange(pos);
                    }
                }
            }
            else if (plannedStock < stock.Min)
            {
                _context.CreatePurchaseDemand(demand, stock.Max - stock.Min, _context.SimulationConfigurations.Single(a => a.Id == simulationId).Time);
            }
            if (stock.Min <= 0 || plannedStock >= stock.Min || demand.GetType() == typeof(DemandStock))
            {
                return(productionOrders);
            }

            if (_context.Demands.OfType <DemandStock>()
                .Any(a => a.ArticleId == demand.ArticleId &&
                     a.State != State.Finished))
            {
                return(productionOrders);
            }

            var demandStock = plannedStock < 0 ? _context.CreateStockDemand(demand, stock, stock.Min) : _context.CreateStockDemand(demand, stock, stock.Min - plannedStock);

            //call processMrp to plan and schedule the stockDemand
            _processMrp.RunRequirementsAndTermination(demandStock, task, simulationId);
            return(productionOrders);
        }
Example #6
0
        private bool CheckNeedForward(IDemandToProvider demand, int simulationId)
        {
            var pows = new List <ProductionOrderWorkSchedule>();

            return(demand.GetType() == typeof(DemandStock) || _context.GetWorkSchedulesFromDemand(demand, ref pows).Any(a => a.StartBackward < _context.SimulationConfigurations.Single(b => b.Id == simulationId).Time));
        }