/// <summary> /// This method runs all validation rules and returns a summary of the results & rules /// </summary> /// <param name="model">The model containing already retrieved results like Author details, Post details etc. used to run validation on.</param> /// <param name="vars">The validation variables being used to run the validation on the data</param> /// <returns>Returns the model containing validation results</returns> public ValidationSummaryViewModel RunValidation(CurationDetailsViewModel model, ValidationVariables vars) { var result = new ValidationSummaryViewModel(); var validationItems = new List <ValidationItemViewModel>(); try { validationItems.Add(ValidationHelper.ValidatePostCreateDateRule(model, vars)); validationItems.Add(ValidationHelper.ValidatePostMaxPendingPayoutRule(model, vars)); validationItems.Add(ValidationHelper.ValidateTotalMaxPendingPayoutRule(model, vars)); validationItems.Add(ValidationHelper.ValidateMaxPostPayoutRule(model, vars)); validationItems.Add(ValidationHelper.ValidateAuthorReputationRule(model, vars)); validationItems.Add(ValidationHelper.ValidateMinPostsRule(model, vars)); validationItems.Add(ValidationHelper.ValidateMinCommentsRule(model, vars)); var upvoteAccountDetails = GetAccountDetails(vars.UpvoteAccount.Replace("@", "")); validationItems.Add(ValidationHelper.ValidateUpvoteAccountMinVPRule(upvoteAccountDetails, vars)); validationItems.Add(ValidationHelper.ValidateMinDaysSinceLastUpvoteFromUpvoteAccount(model.Author.Details.name, model.UpvoteAccountVotes, vars)); } catch (Exception ex) { _log.Error(ex); } result.Items = validationItems; return(result); }
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 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")); }