Exemplo n.º 1
0
 public WeekBasketViewModel(Consumer consumer, TempWeekBasket tempWeekBasket,ValidatedWeekBasket validatedWeekBasket, ApplicationDbContext context)
 {
     TempWeekBasket = tempWeekBasket;
     ValidatedWeekBasket = validatedWeekBasket;
     Consumer = consumer;
     Products = context.Products.Include(x=>x.Producer).Where(x => x.State == Product.ProductState.Enabled).ToList();
     ProductTypes = context.ProductTypes.Include(x => x.ProductFamilly).ToList();
 }
Exemplo n.º 2
0
        public IActionResult ValidateBasket(string basketId)
        {
            Stolon stolon = GetCurrentStolon() ;
            if (stolon.GetMode() == Stolon.Modes.DeliveryAndStockUpdate)
                return Redirect("Index");

            TempWeekBasket tempWeekBasket = _context.TempsWeekBaskets.Include(x => x.Products).Include(x => x.Consumer).First(x => x.Id.ToString() == basketId);
            tempWeekBasket.RetrieveProducts(_context);
            ValidatedWeekBasket validatedWeekBasket = _context.ValidatedWeekBaskets.Include(x => x.Consumer).Include(x => x.Products).FirstOrDefault(x => x.Consumer.Id == tempWeekBasket.Consumer.Id);

            if (validatedWeekBasket == null)
            {
                //First validation of the week
                validatedWeekBasket = new ValidatedWeekBasket();
                validatedWeekBasket.Products = new List<BillEntry>();
                validatedWeekBasket.Consumer = tempWeekBasket.Consumer;
                _context.Add(validatedWeekBasket);
            }
            else
            {
                validatedWeekBasket.RetrieveProducts(_context);
            }
            //LOCK to prevent multi insert at this momment
            if (tempWeekBasket.Products.Any())
            {
                List<BillEntry> rejectedEntries = new List<BillEntry>();
                //Sauvegarde des produits déja validés
                List<BillEntry> previousBillEntries = validatedWeekBasket.Products;
                //On met le panier validé dans le même état que le temporaire
                validatedWeekBasket.Products = new List<BillEntry>();
                foreach (BillEntry billEntry in tempWeekBasket.Products.ToList())
                {
                    validatedWeekBasket.Products.Add(billEntry.Clone());
                }

                //Gestion de la suppression et du changement de quantité sur des billEntry existantes
                foreach (BillEntry prevEntry in previousBillEntries)
                {
                    BillEntry newEntry = validatedWeekBasket.Products.FirstOrDefault(x => x.ProductId == prevEntry.ProductId);
                    Product product = _context.Products.First(x => x.Id == prevEntry.ProductId);

                    if (newEntry == null)
                    {
                        //produit supprimé du panier
                        UpdateProductStock(product, prevEntry.Quantity);
                    }
                    else
                    {
                        int qtyDiff = newEntry.Quantity - prevEntry.Quantity;
                        decimal stepStock = product.RemainingStock;
                        if (product.Type != Product.SellType.Piece)
                        {
                            stepStock = (product.RemainingStock / product.QuantityStep) * 1000.0M;
                        }
                        if (stepStock < qtyDiff)
                        {
                            //Stock insuffisant, on supprime la nouvelle ligne et on garde l'ancienne
                            validatedWeekBasket.Products.Remove(newEntry);
                            validatedWeekBasket.Products.Add(prevEntry);
                            rejectedEntries.Add(newEntry);
                        }
                        else
                        {
                            UpdateProductStock(product, -qtyDiff);
                        }
                    }
                }

                //Gestion de l'ajout de produits
                foreach (BillEntry newEntry in validatedWeekBasket.Products.ToList())
                {
                    BillEntry prevEntry = previousBillEntries.FirstOrDefault(x => x.ProductId == newEntry.ProductId);

                    if (prevEntry == null)
                    {
                        //Nouveau produit
                        Product product = _context.Products.First(x => x.Id == newEntry.ProductId);
                        decimal stepStock = product.RemainingStock;
                        if (product.Type != Product.SellType.Piece)
                        {
                            stepStock = (product.RemainingStock / product.QuantityStep) * 1000.0M;
                        }
                        if (newEntry.Quantity <= stepStock)
                        {
                            //product.RemainingStock -= newEntry.Quantity;
                            UpdateProductStock(product, -newEntry.Quantity);
                        }
                        else
                        {
                            validatedWeekBasket.Products.Remove(newEntry);
                            rejectedEntries.Add(newEntry);
                        }
                    }
                }

                tempWeekBasket.Products = new List<BillEntry>();
                //On met le panier temporaire dans le même état que le validé
                foreach (BillEntry entry in validatedWeekBasket.Products)
                {
                    tempWeekBasket.Products.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(validatedWeekBasket, rejectedEntries) { Total = GetBasketPrice(validatedWeekBasket) };
                Services.AuthMessageSender.SendEmail(validatedWeekBasket.Consumer.Email, validatedWeekBasket.Consumer.Name, subject, base.RenderPartialViewToString("Templates/ValidatedBasketTemplate", validationSummaryViewModel));
                //Return view
                return View("ValidateBasket", validationSummaryViewModel);
            }
            else
            {
                //On annule tout le contenu du panier
                foreach (BillEntry entry in validatedWeekBasket.Products)
                {
                    Product product = _context.Products.First(x => x.Id == entry.ProductId);
                    UpdateProductStock(entry.Product, 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(validatedWeekBasket.Consumer.Email, validatedWeekBasket.Consumer.Name, "Panier de la semaine annulé", base.RenderPartialViewToString("ValidateBasket", null));
            }
            return View("ValidateBasket");
        }
Exemplo n.º 3
0
 public ValidationSummaryViewModel(ValidatedWeekBasket validatedWeekBasket, List<BillEntry> unvalidBillEntry)
 {
     ValidatedWeekBasket = validatedWeekBasket;
     UnValidBillEntry = unvalidBillEntry;
 }