Exemplo n.º 1
0
 private static void AddStocks(Product product, int remainingStock = 10, int weekStock = 10, Product.ProductState productState = Product.ProductState.Enabled)
 {
     foreach (var adherentStolon in product.Producer.AdherentStolons)
     {
         ProductStockStolon productStock = new ProductStockStolon(product.Id, adherentStolon.Id);
         product.ProductStocks.Add(productStock);
         productStock.RemainingStock = remainingStock;
         productStock.State          = productState;
         productStock.WeekStock      = weekStock;
     }
 }
Exemplo n.º 2
0
        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 Disable(Guid?id)
        {
            if (!AuthorizedProducer())
            {
                return(Unauthorized());
            }

            ProductStockStolon productStock = _context.ProductsStocks.FirstOrDefault(x => x.Id == id);

            if (productStock == null)
            {
                return(NotFound());
            }
            productStock.State = Product.ProductState.Disabled;
            _context.SaveChanges();
            return(RedirectToAction("Index"));
        }
Exemplo n.º 4
0
        public IActionResult AddToBasket(string weekBasketId, string productStockId)
        {
            TempWeekBasket tempWeekBasket = _context.TempsWeekBaskets.Include(x => x.AdherentStolon).Include(x => x.AdherentStolon.Adherent).Include(x => x.BillEntries).AsNoTracking().First(x => x.Id.ToString() == weekBasketId);

            if (tempWeekBasket.BillEntries.Any(x => x.ProductStockId.ToString() == productStockId))
            {
                return(JsonTmpWeekBasket());//On a déjà le produit
            }
            ProductStockStolon ProductStock = _context.ProductsStocks.Include(x => x.Product).ThenInclude(x => x.Producer).Single(x => x.Id.ToString() == productStockId);
            BillEntry          billEntry    = BillEntry.CloneFromProduct(ProductStock);

            billEntry.ProductStockId   = ProductStock.Id;
            billEntry.Quantity         = 1;
            billEntry.TempWeekBasketId = tempWeekBasket.Id;
            _context.Add(billEntry);
            _context.SaveChanges();
            return(JsonTmpWeekBasket());
        }
Exemplo n.º 5
0
        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"));
        }
Exemplo n.º 6
0
 /**
  * Updates product remaining stock with the given quantity (< 0 || > 0)
  * Manages the stock according to the sell type of the product.
  */
 private void UpdateProductStock(ProductStockStolon productStock, int qty)
 {
     productStock.RemainingStock += qty;
 }
Exemplo n.º 7
0
 public ProductStockViewModel(AdherentStolon activeAdherentStolon, ProductStockStolon productStock, int orderedQty)
 {
     ProductStock          = productStock;
     OrderedQuantityString = productStock.Product.GetQuantityString(orderedQty);
     ActiveAdherentStolon  = activeAdherentStolon;
 }