// GET: ShowBill public IActionResult ShowStolonsBill(string id) { StolonsBill bill = _context.StolonsBills.FirstOrDefault(x => x.BillNumber == id); if (bill != null) { return(View(bill)); } //Bill not found return(View(null)); }
private static StolonsBill GenerateBill(Stolon stolon, List <ConsumerBill> consumersBills, ApplicationDbContext dbContext) { StringBuilder builder = new StringBuilder(); string billNumber = stolon.ShortLabel + "_" + DateTime.Now.Year + "_" + DateTime.Now.GetIso8601WeekOfYear(); StolonsBill bill = new StolonsBill(billNumber) { Stolon = stolon, Amount = 0, ProducersFee = stolon.ProducersFee, Consumers = consumersBills.Count }; if (!consumersBills.Any()) { builder.AppendLine("Rien cette semaine !"); } else { builder.AppendLine("<h1>Commandes numéro : " + billNumber + "</h1>"); builder.AppendLine("<p>Année : " + DateTime.Now.Year); builder.AppendLine("<br>Semaine : " + DateTime.Now.GetIso8601WeekOfYear()); builder.AppendLine("<br><b>Nombre de panier : " + consumersBills.Count + "</b></p>"); builder.AppendLine("<p>Adhérents ayant un panier : <small><br>"); consumersBills.OrderBy(x => x.AdherentStolon.LocalId) .ForEach(x => builder.AppendLine("<a href=\"#" + x.AdherentStolon.LocalId + "\">" + x.AdherentStolon.GetNumberSurnameName() + "</a><br>")); builder.AppendLine("</small></p>"); builder.AppendLine("<div style=\"page-break-after:always\"></div>"); int count = 0; foreach (var consumerBill in consumersBills.OrderBy(x => x.AdherentStolon.LocalId)) { count++; builder.AppendLine("<h1 id=\"" + consumerBill.AdherentStolon.LocalId + "\">Adhérent : " + consumerBill.AdherentStolon.LocalId + " / " + consumerBill.AdherentStolon.Adherent.Surname + " / " + consumerBill.AdherentStolon.Adherent.Name + "</h1>"); builder.AppendLine("<p>Facture : " + consumerBill.BillNumber + "</p>"); builder.AppendLine("<p>Téléphone : " + consumerBill.AdherentStolon.Adherent.PhoneNumber + "</p>"); builder.AppendLine("<p>Total à régler : " + consumerBill.TotalPrice.ToString("0.00") + " €</p>"); //Create list of bill entry by product var billEntryByProducer = consumerBill.BillEntries.GroupBy(x => x.ProducerBill); builder.AppendLine("<h2>Commande par producteur</h2>"); builder.AppendLine("<table class=\"table\">"); builder.AppendLine("<tr>"); builder.AppendLine("<th>Producteur</th>"); builder.AppendLine("<th>Produit</th>"); builder.AppendLine("<th>Quantité</th>"); builder.AppendLine("</tr>"); foreach (var producerBillsEntry in billEntryByProducer.OrderBy(x => x.Key.AdherentStolon.Adherent.CompanyName)) { builder.AppendLine("<tr>"); builder.AppendLine("<td colspan=\"3\" style=\"border-top:1px solid;\">" + "<b>" + producerBillsEntry.Key.AdherentStolon.Adherent.CompanyName + "</b>" + "</td>"); builder.AppendLine("</tr>"); foreach (var billEntry in producerBillsEntry.OrderBy(x => x.ProductStock.Product.Name)) { builder.AppendLine("<tr>"); builder.AppendLine("<td></td>"); builder.AppendLine("<td>" + billEntry.ProductStock.Product.Name + "</td>"); builder.AppendLine("<td>" + billEntry.QuantityString + "</td>"); builder.AppendLine("</tr>"); } } builder.AppendLine("</table>"); if (count != consumersBills.Count) { builder.AppendLine("<div style=\"page-break-after:always\"></div>"); } } /* * foreach (ValidatedWeekBasket tempWeekBasket in consumerWeekBaskets.OrderBy(x => x.AdherentStolon.LocalId)) * { * ValidatedWeekBasket weekBasket = dbContext.ValidatedWeekBaskets.Include(x => x.BillEntries).First(x => x.Id == tempWeekBasket.Id); * weekBasket.BillEntries.ForEach(x => x = dbContext.BillEntrys.First(b => b.Id == x.Id)); * bill.Amount += weekBasket.TotalPrice; * // * builder.AppendLine("<h1>Adhérent : " + weekBasket.AdherentStolon.LocalId + " / " + weekBasket.Adherent.Surname + " / " + weekBasket.Adherent.Name + "</h1>"); * builder.AppendLine("<p>Facture : " + billNumber + "_" + weekBasket.Consumer.Id + "</p>"); * builder.AppendLine("<p>Téléphone : " + weekBasket.Consumer.PhoneNumber + "</p>"); * builder.AppendLine("<p>Total à régler : " + weekBasket.TotalPrice.ToString("0.00") + " €</p>"); * * * //Create list of bill entry by product * Dictionary<Adherent, List<BillEntry>> producersProducts = new Dictionary<Adherent, List<BillEntry>>(); * foreach (var billEntryConsumer in weekBasket.BillEntries) * { * var billEntry = dbContext.BillEntrys.Include(x => x.Product).ThenInclude(x => x.Producer).First(x => x.Id == billEntryConsumer.Id); * if (!producersProducts.ContainsKey(billEntry.Product.Producer)) * { * producersProducts.Add(billEntry.Product.Producer, new List<BillEntry>()); * } * producersProducts[billEntry.Product.Producer].Add(billEntry); * } * List<int> rowsTotal = new List<int>(); * // - Add products by producer * builder.AppendLine("<h2>Commande par producteur</h2>"); * * builder.AppendLine("<table class=\"table\">"); * builder.AppendLine("<tr>"); * builder.AppendLine("<th>Producteur</th>"); * builder.AppendLine("<th>Produit</th>"); * builder.AppendLine("<th>Quantité</th>"); * builder.AppendLine("</tr>"); * * foreach (var producer in producersProducts.Keys.OrderBy(x => x.Id)) * { * builder.AppendLine("<tr>"); * builder.AppendLine("<td colspan=\"3\" style=\"border-top:1px solid;\">" + "<b>" + producer.CompanyName + "</b>" + "</td>"); * builder.AppendLine("</tr>"); * foreach (var billEntry in producersProducts[producer].OrderBy(x => x.Product.Name)) * { * builder.AppendLine("<tr>"); * builder.AppendLine("<td></td>"); * builder.AppendLine("<td>" + billEntry.Product.Name + "</td>"); * builder.AppendLine("<td>" + billEntry.QuantityString + "</td>"); * builder.AppendLine("</tr>"); * * } * } * builder.AppendLine("</table>"); * builder.AppendLine("<divstyle=\"page-break-after:always;\">"); * } */ } builder.AddBootstrap(); builder.AddFooterAndHeaderRemoval(); bill.HtmlBillContent = builder.ToString(); //TODO envisager de créer les HtmlBillContent via des cshtml plutot que comme ca return(bill); }
private static void TriggerDeliveryAndStockUpdateMode(Stolon stolon, ApplicationDbContext dbContext) { //Consumer (create bills) List <ValidatedWeekBasket> consumerWeekBaskets = dbContext.ValidatedWeekBaskets .Include(x => x.BillEntries) .Include(x => x.AdherentStolon) .Include(x => x.AdherentStolon.Stolon) .Include(x => x.AdherentStolon.Adherent) .Where(x => x.AdherentStolon.StolonId == stolon.Id) .ToList(); foreach (var weekBasket in consumerWeekBaskets) { //Remove TempWeekBasket and linked billEntry var tempWeekBasketToRemove = dbContext.TempsWeekBaskets.FirstOrDefault(x => x.AdherentStolonId == weekBasket.AdherentStolon.Id); if (tempWeekBasketToRemove == null) { continue; } var linkedBillEntriesToRemove = dbContext.BillEntrys.Where(x => x.TempWeekBasketId == tempWeekBasketToRemove.Id).ToList(); dbContext.RemoveRange(linkedBillEntriesToRemove); dbContext.SaveChanges(); dbContext.Remove(tempWeekBasketToRemove); dbContext.SaveChanges(); //Creates new bill entryes List <BillEntry> billEntries = new List <BillEntry>(); foreach (var oldBillEntry in weekBasket.BillEntries.ToList()) { BillEntry newBillEntry = oldBillEntry.Clone(); billEntries.Add(newBillEntry); dbContext.Remove(oldBillEntry); dbContext.Add(newBillEntry); dbContext.SaveChanges(); } dbContext.Remove(weekBasket); dbContext.SaveChanges(); //Generate bill for consumer ConsumerBill consumerBill = CreateBill <ConsumerBill>(weekBasket.AdherentStolon, billEntries); consumerBill.HtmlBillContent = GenerateHtmlBillContent(consumerBill, dbContext); dbContext.Add(consumerBill); dbContext.SaveChanges(); } List <ProducerBill> producerBills = new List <ProducerBill>(); List <ConsumerBill> consumerBills = dbContext.ConsumerBills .Include(x => x.BillEntries) .ThenInclude(x => x.ProductStock) .ThenInclude(x => x.Product) .Include(x => x.AdherentStolon) .Include(x => x.AdherentStolon.Adherent) .Include(x => x.AdherentStolon.Stolon) .Where(x => x.AdherentStolon.Stolon.Id == stolon.Id && x.EditionDate.GetIso8601WeekOfYear() == DateTime.Now.GetIso8601WeekOfYear() && x.EditionDate.Year == DateTime.Now.Year) .ToList(); //Producer (creates bills) foreach (var producer in dbContext.AdherentStolons.Include(x => x.Adherent).Include(x => x.Stolon).Where(x => x.StolonId == stolon.Id && x.IsProducer).ToList()) { List <BillEntry> billEntries = new List <BillEntry>(); foreach (var consumerBill in consumerBills) { foreach (var billEntry in consumerBill.BillEntries.Where(billEntry => billEntry.ProductStock.Product.ProducerId == producer.AdherentId)) { billEntries.Add(billEntry); } } //Generate bill for producer ProducerBill bill = CreateBill <ProducerBill>(producer, billEntries); bill.HtmlBillContent = GenerateHtmlBillContent(bill, dbContext); bill.HtmlOrderContent = GenerateHtmlOrderContent(bill, dbContext); producerBills.Add(bill); if (billEntries.Any()) { dbContext.Add(bill); dbContext.SaveChanges(); } } //Stolons StolonsBill stolonsBill = GenerateBill(stolon, consumerBills, dbContext); stolonsBill.Producers = producerBills.Count; if (dbContext.StolonsBills.Any(x => x.BillNumber == stolonsBill.BillNumber)) { StolonsBill tempBill = dbContext.StolonsBills.FirstOrDefault(x => x.BillNumber == stolonsBill.BillNumber); tempBill.Amount = stolonsBill.Amount; tempBill.EditionDate = stolonsBill.EditionDate; tempBill.HtmlBillContent = stolonsBill.HtmlBillContent; tempBill.ProducersFee = stolonsBill.ProducersFee; } else { dbContext.Add(stolonsBill); } dbContext.SaveChanges(); //Move stolon's products to stock foreach (ProductStockStolon productStock in dbContext.ProductsStocks.Include(x => x.AdherentStolon).Include(x => x.Product).Where(x => x.AdherentStolon.StolonId == stolon.Id).ToList()) { if (productStock.State == ProductState.Enabled && productStock.Product.StockManagement == StockType.Week) { productStock.State = ProductState.Stock; productStock.RemainingStock = productStock.WeekStock; } } // dbContext.SaveChanges(); //For stolons try { GeneratePDF(stolonsBill.HtmlBillContent, stolonsBill.GetStolonBillFilePath()); } catch (Exception exept) { AuthMessageSender.SendEmail(stolon.Label, Configurations.Application.ContactMailAddress, "Stolons", "Erreur lors de la génération de la facture Stolons", "Message d'erreur : " + exept.Message); } // => Producer, send mails foreach (var bill in producerBills) { Task.Factory.StartNew(() => { GenerateOrderPdfAndSendEmail(bill); }); } //Bills (save bills and send mails to user) foreach (var bill in consumerBills) { Task.Factory.StartNew(() => { GenerateOrderPdfAndSendEmail(bill); }); } }
public static string GetStolonBillFilePath(this StolonsBill bill) { return(Path.Combine(Configurations.Environment.WebRootPath, Configurations.BillsStockagePath, bill.GetFileName())); }