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); }
public decimal GetAmountBought(int articleId) { var purchaseParts = PurchaseParts.Where(a => a.ArticleId == articleId && a.State != State.Finished); var purchasedAmount = 0; foreach (var purchasePart in purchaseParts) { purchasedAmount += purchasePart.Quantity; } return(purchasedAmount); }
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); }