Example #1
0
        public decimal GetReserved(int articleId)
        {
            var demands = Demands.OfType <DemandProviderStock>()
                          .Where(a => a.State != State.Finished && a.ArticleId == articleId).Sum(a => a.Quantity);

            return(demands);
        }
Example #2
0
        /// <summary>
        /// returns the OrderIds for the ProductionOrder
        /// </summary>
        /// <param name="po"></param>
        /// <returns></returns>
        public List <int> GetOrderIdsFromProductionOrder(ProductionOrder po)
        {
            po = ProductionOrders.Include(a => a.DemandProviderProductionOrders).ThenInclude(b => b.DemandRequester).Single(a => a.Id == po.Id);
            var ids       = new List <int>();
            var requester = (from provider in po.DemandProviderProductionOrders
                             select provider.DemandRequester).ToList();

            if (!requester.Any() || requester.First() == null)
            {
                return(ids);
            }
            foreach (var singleRequester in requester)
            {
                if (singleRequester.GetType() == typeof(DemandProductionOrderBom))
                {
                    ids.AddRange(GetOrderIdsFromProductionOrder(
                                     ((DemandProductionOrderBom)singleRequester).ProductionOrderBom.ProductionOrderParent));
                }
                else if (singleRequester.GetType() == typeof(DemandOrderPart))
                {
                    var dop = Demands.OfType <DemandOrderPart>().Include(a => a.OrderPart).Single(a => a.Id == singleRequester.Id);
                    ids.Add(dop.OrderPart.OrderId);
                }
            }
            return(ids);
        }
Example #3
0
        public List <ProductionOrderWorkSchedule> GetBomParents(ProductionOrderWorkSchedule plannedSchedule)
        {
            var provider = plannedSchedule.ProductionOrder.DemandProviderProductionOrders;

            if (provider == null || provider.ToList().Any(dppo => dppo.DemandRequester == null))
            {
                return(new List <ProductionOrderWorkSchedule>());
            }
            var requester = (from demandProviderProductionOrder in provider
                             select demandProviderProductionOrder.DemandRequester into req
                             select req).ToList();


            var pows = new List <ProductionOrderWorkSchedule>();

            foreach (var singleRequester in requester)
            {
                if (singleRequester.GetType() == typeof(DemandOrderPart) || singleRequester.GetType() == typeof(DemandStock))
                {
                    return(null);
                }
                var demand = Demands.OfType <DemandProductionOrderBom>().Include(a => a.ProductionOrderBom)
                             .ThenInclude(b => b.ProductionOrderParent).ThenInclude(c => c.ProductionOrderWorkSchedule)
                             .Single(a => a.Id == singleRequester.Id);
                var schedules = demand.ProductionOrderBom.ProductionOrderParent.ProductionOrderWorkSchedule;
                pows.Add(schedules.Single(a => a.HierarchyNumber == schedules.Min(b => b.HierarchyNumber)));
            }
            return(pows);

            /*return (from demandProviderProductionOrder in provider
             * select demandProviderProductionOrder.DemandRequester into requester
             * where requester.GetType() == typeof(DemandProductionOrderBom)
             * select ((DemandProductionOrderBom)requester).ProductionOrderBom.ProductionOrderParent.ProductionOrderWorkSchedule into schedules
             * select schedules.Single(a => a.HierarchyNumber == schedules.Min(b => b.HierarchyNumber))).ToList();*/
        }
Example #4
0
        public List <IDemandToProvider> UpdateStateDemandRequester(List <IDemandToProvider> provider)
        {
            var changedRequester = new List <IDemandToProvider>();

            foreach (var singleProvider in provider)
            {
                var prov = Demands.Include(a => a.DemandRequester).ThenInclude(b => b.DemandProvider).Single(a => a.Id == singleProvider.Id);
                if (prov.DemandRequester.DemandProvider.Any(a => a.State != State.Finished))
                {
                    if (!TryUpdateStockProvider(prov.DemandRequester))
                    {
                        continue;
                    }
                }
                //check for PO then set ready if it has started and taken out the articles from the stock
                if (prov.DemandRequester.GetType() == typeof(DemandProductionOrderBom))
                {
                    var dpob = Demands.OfType <DemandProductionOrderBom>().Single(a => a.Id == prov.DemandRequester.Id);
                    if (dpob.ProductionOrderBom.ProductionOrderParent.ProductionOrderWorkSchedule.All(a => a.ProducingState == ProducingState.Created))
                    {
                        continue;
                    }
                }

                prov.DemandRequester.State = State.Finished;
                Update(prov.DemandRequester);
                SaveChanges();
                changedRequester.Add(prov.DemandRequester);
            }
            var test  = Demands.OfType <DemandProductionOrderBom>().Where(a => a.State != State.Finished);
            var test2 = test.Where(a => a.DemandProvider.All(b => b.State == State.Finished));

            Debug.WriteLine(test2.Count() + " items waiting to be concluded");
            Debug.WriteLine(test2.Count(a => a.ProductionOrderBom.ProductionOrderParent.ProductionOrderWorkSchedule.First().Name == "Wedding"));

            var dpobs = Demands.OfType <DemandProductionOrderBom>().Where(a => a.State != State.Finished &&
                                                                          a.ProductionOrderBom.ProductionOrderParent.ProductionOrderWorkSchedule.Any(c => c.ProducingState != ProducingState.Created));

            foreach (var dpob in dpobs)
            {
                dpob.State = State.Finished;
                changedRequester.Add(dpob);
            }
            return(changedRequester);
        }
Example #5
0
        public List <IDemandToProvider> UpdateStateDemandProviderPurchaseParts()
        {
            var changedDemands = new List <IDemandToProvider>();
            var provider       = Demands.OfType <DemandProviderPurchasePart>().Include(a => a.PurchasePart).ThenInclude(b => b.Purchase).Where(a => a.State != State.Finished).ToList();

            foreach (var singleProvider in provider)
            {
                if (singleProvider.PurchasePart.State != State.Finished)
                {
                    continue;
                }
                singleProvider.State = State.Finished;
                changedDemands.Add(singleProvider);
                Update(singleProvider);
                SaveChanges();
            }
            return(changedDemands);
        }
Example #6
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 #7
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 #8
0
        /// <summary>
        /// returns the Production Order Work Schedules for a given Order
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public Task <List <ProductionOrderWorkSchedule> > GetProductionOrderWorkScheduleByOrderId(int orderId)
        {
            return(Task.Run(() =>
            {
                // get the corresponding Order Parts to Order
                var demands = Demands.OfType <DemandOrderPart>()
                              .Include(x => x.OrderPart)
                              .Where(o => o.OrderPart.OrderId == orderId)
                              .ToList();

                // ReSharper Linq
                var demandboms = demands.SelectMany(demand => Demands.OfType <DemandProductionOrderBom>()
                                                    .Where(a => a.DemandRequesterId == demand.Id)).ToList();

                // get Demand Providers for this Order
                var demandProviders = new List <DemandProviderProductionOrder>();
                foreach (var order in (Demands.OfType <DemandProviderProductionOrder>()
                                       .Join(demands, c => c.DemandRequesterId, d => ((IDemandToProvider)d).Id, (c, d) => c)))
                {
                    demandProviders.Add(order);
                }

                var demandBomProviders = (Demands.OfType <DemandProviderProductionOrder>()
                                          .Join(demandboms, c => c.DemandRequesterId, d => d.Id, (c, d) => c)).ToList();

                // get ProductionOrderWorkSchedule for
                var pows = ProductionOrderWorkSchedules.Include(x => x.ProductionOrder).Include(x => x.Machine).Include(x => x.MachineGroup).AsNoTracking();
                var powDetails = pows.Join(demandProviders, p => p.ProductionOrderId, dp => dp.ProductionOrderId,
                                           (p, dp) => p).ToList();

                var powBoms = (from p in pows
                               join dbp in demandBomProviders on p.ProductionOrderId equals dbp.ProductionOrderId
                               select p).ToList();

                powDetails.AddRange(powBoms);
                return powDetails;
            }));
        }
Example #9
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);
     }
 }