// GET: WeekBasket/Index/id public IActionResult Index() { AdherentStolon adherentStolon = GetActiveAdherentStolon(); if (adherentStolon == null) { return(NotFound()); } TempWeekBasket tempWeekBasket = _context.TempsWeekBaskets.Include(x => x.AdherentStolon).Include(x => x.AdherentStolon.Adherent).Include(x => x.BillEntries).FirstOrDefault(x => x.AdherentStolon.Id == adherentStolon.Id); ValidatedWeekBasket validatedWeekBasket = _context.ValidatedWeekBaskets.Include(x => x.AdherentStolon).Include(x => x.AdherentStolon.Adherent).Include(x => x.BillEntries).FirstOrDefault(x => x.AdherentStolon.Id == adherentStolon.Id); if (tempWeekBasket == null) { //Il n'a pas encore de panier de la semaine, on lui en crée un tempWeekBasket = new TempWeekBasket { AdherentStolon = adherentStolon, BillEntries = new List <BillEntry>() }; _context.Add(tempWeekBasket); _context.SaveChanges(); } return(View(new WeekBasketViewModel(adherentStolon, tempWeekBasket, validatedWeekBasket, _context))); }
static public bool?IsBasketValidated(TempWeekBasket tempBasket, ApplicationDbContext context) { ValidatedWeekBasket validatedBasket = context.ValidatedWeekBaskets .Include(x => x.AdherentStolon) .Include(x => x.BillEntries) .AsNoTracking() .FirstOrDefault(x => x.AdherentStolon.AdherentId == tempBasket.AdherentStolon.AdherentId); if (validatedBasket == null) { return(false); } if (validatedBasket.BillEntries.Count != tempBasket.BillEntries.Count) { return(false); } foreach (BillEntry billEntry in tempBasket.BillEntries.ToList()) { BillEntry validatedEntry = validatedBasket.BillEntries.FirstOrDefault(x => x.ProductStockId == billEntry.ProductStockId); if (validatedEntry == null) { return(false); } if (billEntry.Quantity != validatedEntry.Quantity) { return(false); } } return(true); }
public WeekBasketViewModel(AdherentStolon adherentStolon, TempWeekBasket tempWeekBasket, ValidatedWeekBasket validatedWeekBasket, ApplicationDbContext context) { TempWeekBasket = tempWeekBasket; ValidatedWeekBasket = validatedWeekBasket; AdherentStolon = adherentStolon; ProductsStocks = context.ProductsStocks.Include(x => x.Product).Include(x => x.AdherentStolon).Where(x => x.AdherentStolon.Id == AdherentStolon.Stolon.Id).Where(x => x.Product.IsAvailable).Where(x => x.State == Product.ProductState.Enabled).ToList(); ProductTypes = context.ProductTypes.Include(x => x.ProductFamilly).ToList(); }
private TempWeekBasket AddProductQuantity(string weekBasketId, string productStockId, int quantity) { TempWeekBasket tempWeekBasket = _context.TempsWeekBaskets.Include(x => x.AdherentStolon).Include(x => x.AdherentStolon.Adherent).Include(x => x.BillEntries).First(x => x.Id.ToString() == weekBasketId); //tempWeekBasket.RetrieveProducts(_context); ValidatedWeekBasket validatedWeekBasket = _context.ValidatedWeekBaskets.Include(x => x.AdherentStolon).Include(x => x.BillEntries).AsNoTracking().FirstOrDefault(x => x.AdherentStolon.AdherentId == tempWeekBasket.AdherentStolon.AdherentId); int validatedQuantity = 0; if (validatedWeekBasket != null) { BillEntry validatedEntry = validatedWeekBasket.BillEntries.FirstOrDefault(x => x.ProductStockId.ToString() == productStockId); if (validatedEntry != null) { validatedQuantity = validatedEntry.Quantity; } } BillEntry billEntry = tempWeekBasket.BillEntries.FirstOrDefault(x => x.ProductStockId.ToString() == productStockId); ProductStockStolon productStock = _context.ProductsStocks.Include(x => x.Product).FirstOrDefault(x => x.Id.ToString() == productStockId); decimal stepStock = productStock.RemainingStock; if (productStock.Product.Type != Product.SellType.Piece) { stepStock = (productStock.RemainingStock * 1000.0M) / productStock.Product.QuantityStep; } bool proceed = false; if (productStock.Product.StockManagement == Product.StockType.Unlimited) { proceed = true; } if (!(quantity > 0 && stepStock < (billEntry.Quantity - validatedQuantity) + quantity)) { proceed = true; } if (proceed == true) { billEntry.Quantity = billEntry.Quantity + quantity; if (billEntry.Quantity <= 0) { //La quantite est 0 on supprime le produit tempWeekBasket.BillEntries.Remove(billEntry); _context.Remove(billEntry); } _context.SaveChanges(); } return(tempWeekBasket); }
public IActionResult ValidatedBasket() { Stolon stolon = GetCurrentStolon(); var adherentStolon = GetActiveAdherentStolon(); if (adherentStolon == null) { return(null); } ValidatedWeekBasket validatedWeekBasket = _context.ValidatedWeekBaskets.Include(x => x.AdherentStolon).Include(x => x.AdherentStolon.Adherent).Include(x => x.BillEntries).FirstOrDefault(x => x.AdherentStolon.Id == adherentStolon.Id); ValidationSummaryViewModel validationSummaryViewModel = new ValidationSummaryViewModel(GetActiveAdherentStolon(), validatedWeekBasket, new List <BillEntry>()) { Total = GetBasketPrice(validatedWeekBasket) }; return(View("ValidatedBasket", validationSummaryViewModel)); }
public IActionResult JsonValidatedWeekBasket() { var adherentStolon = GetActiveAdherentStolon(); if (adherentStolon == null) { return(null); } ValidatedWeekBasket validatedWeekBasket = _context.ValidatedWeekBaskets .Include(x => x.AdherentStolon) .Include(x => x.AdherentStolon.Adherent) .Include(x => x.BillEntries) .AsNoTracking() .FirstOrDefault(x => x.AdherentStolon.Id == adherentStolon.Id); if (validatedWeekBasket != null) { validatedWeekBasket.RetrieveProducts(_context); } return(Json(validatedWeekBasket)); }
public IActionResult ResetBasket(string basketId) { TempWeekBasket tempWeekBasket = _context.TempsWeekBaskets.Include(x => x.AdherentStolon).Include(x => x.AdherentStolon.Adherent).First(x => x.Id.ToString() == basketId); ValidatedWeekBasket validatedWeekBasket = _context.ValidatedWeekBaskets.Include(x => x.AdherentStolon).Include(x => x.BillEntries).FirstOrDefault(x => x.AdherentStolon.AdherentId == tempWeekBasket.AdherentStolon.AdherentId); if (validatedWeekBasket == null) { tempWeekBasket.BillEntries = new List <BillEntry>(); } else { tempWeekBasket.BillEntries = new List <BillEntry>(); foreach (BillEntry billEntry in validatedWeekBasket.BillEntries.ToList()) { tempWeekBasket.BillEntries.Add(billEntry.Clone()); } } tempWeekBasket.Validated = true; _context.SaveChanges(); tempWeekBasket.RetrieveProducts(_context); return(Json(tempWeekBasket)); }
public IActionResult ValidateBasket(string basketId) { var adherentStolon = GetActiveAdherentStolon(); Stolon stolon = GetCurrentStolon(); if (stolon.GetMode() == Stolon.Modes.DeliveryAndStockUpdate) { return(Redirect("Index")); } //TempWeekBasket tempWeekBasket = _context.TempsWeekBaskets.Include(x => x.BillEntries).Include(x => x.AdherentStolon).AsNoTracking().FirstOrDefault(x => x.Id.ToString() == basketId); //tempWeekBasket.RetrieveProducts(_context); ValidatedWeekBasket validatedWeekBasket = _context.ValidatedWeekBaskets.Include(x => x.AdherentStolon).ThenInclude(x => x.Adherent).Include(x => x.BillEntries).FirstOrDefault(x => x.AdherentStolonId == adherentStolon.Id); if (validatedWeekBasket == null) { //First validation of the week validatedWeekBasket = new ValidatedWeekBasket { BillEntries = new List <BillEntry>(), AdherentStolon = adherentStolon }; _context.Add(validatedWeekBasket); _context.SaveChanges(); } else { validatedWeekBasket.RetrieveProducts(_context); } TempWeekBasket tempWeekBasket = _context.TempsWeekBaskets.Include(x => x.BillEntries).Include(x => x.AdherentStolon).FirstOrDefault(x => x.Id.ToString() == basketId); tempWeekBasket.RetrieveProducts(_context); //TODO LOCK to prevent multi insert at this moment ? if (tempWeekBasket.BillEntries.Any()) { List <BillEntry> rejectedEntries = new List <BillEntry>(); //Sauvegarde des produits déja validés List <BillEntry> previousBillEntries = validatedWeekBasket.BillEntries; //On met le panier validé dans le même état que le temporaire validatedWeekBasket.BillEntries = new List <BillEntry>(); foreach (BillEntry billEntry in tempWeekBasket.BillEntries.ToList()) { validatedWeekBasket.BillEntries.Add(billEntry.Clone()); } //Gestion de la suppression et du changement de quantité sur des billEntry existantes foreach (BillEntry prevEntry in previousBillEntries) { BillEntry newEntry = validatedWeekBasket.BillEntries.FirstOrDefault(x => x.ProductStockId == prevEntry.ProductStockId); ProductStockStolon productStock = _context.ProductsStocks.Include(x => x.Product).Include(x => x.AdherentStolon).Single(x => x.Id == prevEntry.ProductStockId); if (newEntry == null) { //produit supprimé du panier UpdateProductStock(productStock, prevEntry.Quantity); } else { int qtyDiff = newEntry.Quantity - prevEntry.Quantity; decimal stepStock = productStock.RemainingStock; if (productStock.Product.Type != Product.SellType.Piece) { //Actual remaining stock in terms of quantity step Kg/L for weight type products stepStock = (productStock.RemainingStock / productStock.Product.QuantityStep) * 1000.0M; } if (stepStock < qtyDiff && productStock.Product.StockManagement != Product.StockType.Unlimited) { //Stock insuffisant, on supprime la nouvelle ligne et on garde l'ancienne validatedWeekBasket.BillEntries.Remove(newEntry); validatedWeekBasket.BillEntries.Add(prevEntry); rejectedEntries.Add(newEntry); } else { UpdateProductStock(productStock, -qtyDiff); //On supprime la bill entry précédente ( ancienne bill entry) _context.BillEntrys.Remove(prevEntry); } } } //Gestion de l'ajout de produits foreach (BillEntry newEntry in validatedWeekBasket.BillEntries.ToList()) { BillEntry prevEntry = previousBillEntries.FirstOrDefault(x => x.ProductStockId == newEntry.ProductStockId); if (prevEntry == null) { //Nouveau produit ProductStockStolon productStock = _context.ProductsStocks.Include(x => x.Product).Include(x => x.AdherentStolon).Single(x => x.Id == newEntry.ProductStockId); decimal stepStock = productStock.RemainingStock; if (productStock.Product.Type != Product.SellType.Piece) { stepStock = (productStock.RemainingStock / productStock.Product.QuantityStep) * 1000.0M; } if (newEntry.Quantity <= stepStock || productStock.Product.StockManagement == Product.StockType.Unlimited) { //product.RemainingStock -= newEntry.Quantity; UpdateProductStock(productStock, -newEntry.Quantity); } else { validatedWeekBasket.BillEntries.Remove(newEntry); rejectedEntries.Add(newEntry); } } } _context.SaveChanges(); //On supprime toute les BillEntry du tempWeekBasket _context.BillEntrys.RemoveRange(_context.BillEntrys.Where(x => x.TempWeekBasketId == tempWeekBasket.Id).ToList()); _context.SaveChanges(); //On met le panier temporaire dans le même état que le validé foreach (BillEntry entry in validatedWeekBasket.BillEntries) { tempWeekBasket.BillEntries.Add(entry.Clone()); } tempWeekBasket.Validated = true; _context.SaveChanges(); //END LOCK TODO //Recuperation du detail produit pour utilisation dans la Vue validatedWeekBasket.RetrieveProducts(_context); //Send email to user string subject; if (rejectedEntries.Count == 0) { subject = "Validation de votre panier de la semaine"; } else { subject = "Validation partielle de votre panier de la semaine"; } ValidationSummaryViewModel validationSummaryViewModel = new ValidationSummaryViewModel(adherentStolon, validatedWeekBasket, rejectedEntries) { Total = GetBasketPrice(validatedWeekBasket) }; Services.AuthMessageSender.SendEmail(adherentStolon.Stolon.Label, validatedWeekBasket.AdherentStolon.Adherent.Email, validatedWeekBasket.AdherentStolon.Adherent.Name, subject, base.RenderPartialViewToString("Templates/ValidatedBasketTemplate", validationSummaryViewModel)); //Return view return(View("ValidatedBasket", validationSummaryViewModel)); } else { //On annule tout le contenu du panier foreach (BillEntry entry in validatedWeekBasket.BillEntries) { ProductStockStolon productStock = _context.ProductsStocks.Include(x => x.Product).Include(x => x.AdherentStolon).Single(x => x.Id == entry.ProductStockId); UpdateProductStock(productStock, entry.Quantity); //entry.Product.RemainingStock += entry.Quantity; } _context.Remove(tempWeekBasket); _context.Remove(validatedWeekBasket); _context.SaveChanges(); //Il ne commande rien du tout //On lui signale Services.AuthMessageSender.SendEmail(stolon.Label, validatedWeekBasket.AdherentStolon.Adherent.Email, validatedWeekBasket.AdherentStolon.Adherent.Name, "Panier de la semaine annulé", base.RenderPartialViewToString("Templates/ValidatedBasketTemplate", null)); } return(View("ValidatedBasket")); }
public ValidationSummaryViewModel(AdherentStolon activeAdherentStolon, ValidatedWeekBasket validatedWeekBasket, List <BillEntry> unvalidBillEntry) { ValidatedWeekBasket = validatedWeekBasket; UnValidBillEntry = unvalidBillEntry; ActiveAdherentStolon = activeAdherentStolon; }