/**
         * There initial stock levels defined in M_Stock, to avoid modelling stocks,
         * the initial stock levels are simulated as stockExchangeDemands
         */
        public static void AddInitialStockLevels(IDbTransactionData dbTransactionData)
        {
            foreach (var stock in ZppConfiguration.CacheManager.GetMasterDataCache().M_StockGetAll())
            {
                if (stock.Current > 0)
                {
                    Id articleId = new Id(stock.ArticleForeignKey);

                    Demand stockExchangeDemand =
                        StockExchangeDemand.CreateStockExchangeStockDemand(articleId,
                                                                           new DueTime(0), new Quantity(stock.Current));
                    stockExchangeDemand.SetReadOnly();
                    dbTransactionData.DemandsAdd(stockExchangeDemand);
                }
            }
        }
Exemple #2
0
        public EntityCollector CreateDependingDemands(Provider provider)
        {
            if (provider.GetQuantity().IsNull())
            {
                return(null);
            }

            if (provider.GetType() != typeof(StockExchangeProvider))
            {
                throw new MrpRunException("This can only be called for StockExchangeProviders");
            }

            // try to provide by existing demand

            // collects stockExchangeDemands, providerToDemands
            EntityCollector entityCollector =
                _openDemandManager.SatisfyProviderByOpenDemand(provider, provider.GetQuantity());

            if (entityCollector == null)
            {
                entityCollector = new EntityCollector();
            }

            Quantity remainingQuantity = entityCollector.GetRemainingQuantity(provider);

            if (remainingQuantity.IsGreaterThan(Quantity.Zero()))
            {
                LotSize.Impl.LotSize lotSizes =
                    new LotSize.Impl.LotSize(remainingQuantity, provider.GetArticleId());
                bool isLastIteration = false;
                foreach (var lotSize in lotSizes.GetLotSizes())
                {
                    if (isLastIteration || remainingQuantity.IsNegative() ||
                        remainingQuantity.IsNull())
                    {
                        throw new MrpRunException("This is one iteration too many.");
                    }

                    Demand stockExchangeDemand =
                        StockExchangeDemand.CreateStockExchangeStockDemand(provider.GetArticleId(),
                                                                           provider.GetStartTimeBackward(), lotSize);
                    entityCollector.Add(stockExchangeDemand);

                    // 3 cases
                    Quantity quantityOfNewCreatedDemandToReserve;
                    if (remainingQuantity.IsGreaterThan(lotSize))
                    {
                        quantityOfNewCreatedDemandToReserve = lotSize;
                    }
                    else if (remainingQuantity.Equals(lotSize))
                    {
                        // last iteration
                        isLastIteration = true;
                        quantityOfNewCreatedDemandToReserve = lotSize;
                    }
                    else
                    {
                        // last iteration, remaining < lotsize
                        isLastIteration = true;
                        quantityOfNewCreatedDemandToReserve = new Quantity(remainingQuantity);
                        // remember created demand as openDemand
                        _openDemandManager.AddDemand(stockExchangeDemand,
                                                     quantityOfNewCreatedDemandToReserve);
                    }

                    remainingQuantity.DecrementBy(lotSize);
                    if (quantityOfNewCreatedDemandToReserve.IsNegative() ||
                        quantityOfNewCreatedDemandToReserve.IsNull())
                    {
                        throw new MrpRunException(
                                  $"quantityOfNewCreatedDemandToReserve cannot be negative or null: " +
                                  $"{quantityOfNewCreatedDemandToReserve}");
                    }

                    T_ProviderToDemand providerToDemand = new T_ProviderToDemand(provider.GetId(),
                                                                                 stockExchangeDemand.GetId(), quantityOfNewCreatedDemandToReserve);
                    entityCollector.Add(providerToDemand);
                }
            }

            return(entityCollector);
        }