public string RegenerateOrders() { var stolon = GetCurrentStolon(); var consumersBills = _context.ConsumerBills .Include(x => x.AdherentStolon) .Include(x => x.AdherentStolon.Adherent) .Include(x => x.AdherentStolon.Stolon) .Where(x => x.State == BillState.Pending && x.AdherentStolon.StolonId == stolon.Id) .OrderBy(x => x.AdherentStolon.Adherent.Id) .ToList(); var producersBills = _context.ProducerBills .Include(x => x.AdherentStolon) .Include(x => x.AdherentStolon.Adherent) .Include(x => x.AdherentStolon.Stolon) .Where(x => x.State != BillState.Paid && x.AdherentStolon.StolonId == stolon.Id) .OrderBy(x => x.AdherentStolon.Adherent.Id) .ToList(); var weekStolonBill = _context.StolonsBills .Include(x => x.Stolon) .Where(x => x.StolonId == stolon.Id) .ToList() .FirstOrDefault(x => x.BillNumber.EndsWith(DateTime.Now.Year + "_" + DateTime.Now.GetIso8601WeekOfYear())); DateTime start = DateTime.Now; StringBuilder report = new StringBuilder(); report.Append("Rapport de génération des commandes : "); report.AppendLine("- stolon : " + stolon.Label); report.AppendLine("- année : " + DateTime.Now.Year); report.AppendLine("- semaine : " + DateTime.Now.GetIso8601WeekOfYear()); report.AppendLine(); Dictionary <ConsumerBill, bool> consumers = new Dictionary <ConsumerBill, bool>(); consumersBills.ForEach(x => consumers.Add(x, BillGenerator.GenerateBillPDF(x))); Dictionary <ProducerBill, bool> producers = new Dictionary <ProducerBill, bool>(); producersBills.ForEach(x => producers.Add(x, BillGenerator.GenerateOrderPDF(x))); bool stolonBill = BillGenerator.GeneratePDF(weekStolonBill.HtmlBillContent, weekStolonBill.FilePath); report.AppendLine("RESUME : "); report.AppendLine("Commandes consomateurs générées : " + consumers.Count(x => x.Value == true) + "/" + consumers.Count); report.AppendLine("Commandes producteurs générées : " + producers.Count(x => x.Value == true) + "/" + producers.Count); report.AppendLine("Commandes stolons générées : " + stolonBill); report.AppendLine("DETAILS : "); report.AppendLine("-- Consomateurs ok : "); consumers.Where(x => x.Value).ToList().ForEach(consumer => report.AppendLine(consumer.Key.AdherentStolon.LocalId + " " + consumer.Key.AdherentStolon.Adherent.Name.ToUpper() + " " + consumer.Key.AdherentStolon.Adherent.Surname)); report.AppendLine("-- Consomateurs nok : "); consumers.Where(x => x.Value == false).ToList().ForEach(consumer => report.AppendLine(consumer.Key.AdherentStolon.LocalId + " " + consumer.Key.AdherentStolon.Adherent.Name.ToUpper() + " " + consumer.Key.AdherentStolon.Adherent.Surname)); report.AppendLine("-- Producteurs ok : "); producers.Where(x => x.Value).ToList().ForEach(producer => report.AppendLine(producer.Key.AdherentStolon.LocalId + " " + producer.Key.AdherentStolon.Adherent.Name.ToUpper() + " " + producer.Key.AdherentStolon.Adherent.Surname)); report.AppendLine("-- Producteurs nok : "); producers.Where(x => x.Value == false).ToList().ForEach(producer => report.AppendLine(producer.Key.AdherentStolon.LocalId + " " + producer.Key.AdherentStolon.Adherent.Name.ToUpper() + " " + producer.Key.AdherentStolon.Adherent.Surname)); report.AppendLine(); report?.AppendLine(" --Temps d'execution de la génération : " + (DateTime.Now - start)); return(report.ToString()); }
public bool UpdateBillCorrection(VmBillCorrection billCorrection) { billCorrection.Reason = "Modification le : " + DateTime.Now.ToString() + "\n\rRaison : " + billCorrection.Reason + "\n\r\n\r"; ProducerBill bill = _context.ProducerBills.Include(x => x.BillEntries).Include(x => x.AdherentStolon).First(x => x.BillId == billCorrection.ProducerBillId); bill.ModificationReason = billCorrection.Reason; bill.HasBeenModified = true; List <Guid?> modifiedBills = new List <Guid?>(); foreach (var billQuantity in billCorrection.NewQuantities) { var billEntry = bill.BillEntries.First(x => x.Id == billQuantity.BillId); billEntry.Quantity = billQuantity.Quantity; billEntry.HasBeenModified = true; if (!modifiedBills.Any(x => x == billEntry.ConsumerBillId)) { modifiedBills.Add(billEntry.ConsumerBillId); } } _context.SaveChanges(); bill = _context.ProducerBills.Include(x => x.BillEntries).Include(x => x.AdherentStolon).Include(x => x.AdherentStolon.Stolon).Include(x => x.AdherentStolon.Adherent).First(x => x.BillId == billCorrection.ProducerBillId); bill.BillEntries.ForEach(x => x.ProductStock = _context.ProductsStocks.Include(y => y.Product).First(stock => stock.Id == x.ProductStockId)); bill.HtmlBillContent = BillGenerator.GenerateHtmlBillContent(bill, _context); BillGenerator.GenerateBillPDF(bill); foreach (var billId in modifiedBills) { var billToModify = _context.ConsumerBills.Include(x => x.AdherentStolon.Adherent).First(x => x.BillId == billId); billToModify.ModificationReason += billCorrection.Reason; billToModify.HasBeenModified = true; //Envoie mail user BillGenerator.GenerateBillPDF(billToModify); AuthMessageSender.SendEmail(billToModify.AdherentStolon.Stolon.Label, billToModify.AdherentStolon.Adherent.Email, billToModify.AdherentStolon.Adherent.CompanyName, "Votre bon de commande de la semaine chez " + billToModify.AdherentStolon.Stolon.Label + "a été modifié (Bon de commande " + bill.BillNumber + ")", "Oops, petit problème, malheureusement tout vos produits ne pourront pas être disponible. Votre commande a été modifié. En voici la raison : " + billCorrection.Reason + "\n\n" + bill.HtmlBillContent); } _context.SaveChanges(); return(true); }