// GET: UpdateProducerBill //validate bill payement public IActionResult UpdateProducerBill(Guid billId, int state) { ProducerBill bill = _context.ProducerBills .Include(x => x.AdherentStolon) .Include(x => x.AdherentStolon.Adherent) .First(x => x.BillId == billId); bill.State = (BillState)state; _context.Update(bill); if (bill.State == BillState.Paid) { //Transaction Transaction prodRefound = new Transaction( GetCurrentStolon(), Transaction.TransactionType.Outbound, Transaction.TransactionCategory.ProducerRefound, bill.BillAmount, "Paiement de la facture " + bill.BillNumber + " à " + bill.AdherentStolon.Adherent.CompanyName + " ( " + bill.AdherentStolon.LocalId + " )"); _context.Add(prodRefound); Transaction comitionInbound = new Transaction( GetCurrentStolon(), Transaction.TransactionType.Inbound, Transaction.TransactionCategory.ProducersFee, bill.FeeAmount, "Encaissement de la commission de la facture " + bill.BillNumber + " de " + bill.AdherentStolon.Adherent.CompanyName + " ( " + bill.AdherentStolon.LocalId + " )"); _context.Add(comitionInbound); } //Save _context.SaveChanges(); return(RedirectToAction("Index")); }
private static void GenerateOrderPdfAndSendEmail(ProducerBill bill) { try { if (bill.BillEntries.Count == 0) { AuthMessageSender.SendEmail(bill.AdherentStolon.Stolon.Label, bill.AdherentStolon.Adherent.Email, bill.AdherentStolon.Adherent.CompanyName, "Aucune commande chez " + bill.AdherentStolon.Stolon.Label + " cette semaine.", "<h3>Aucune commande chez " + bill.AdherentStolon.Stolon.Label + " cette semaine."); } else { //Generate pdf file bool hasFile = GenerateOrderPDF(bill); //Send mail to producer try { string message = bill.HtmlOrderContent; if (hasFile) { message += "<h3>En pièce jointe votre bon de commande de la semaine chez " + bill.AdherentStolon.Stolon.Label + " (Bon de commande " + bill.BillNumber + ")</h3>"; } AuthMessageSender.SendEmail(bill.AdherentStolon.Stolon.Label, bill.AdherentStolon.Adherent.Email, bill.AdherentStolon.Adherent.CompanyName, "Votre bon de commande de la semaine chez " + bill.AdherentStolon.Stolon.Label + " (Bon de commande " + bill.BillNumber + ")", message, hasFile ? File.ReadAllBytes(bill.GetOrderFilePath()) : null, "Bon de commande " + bill.GetOrderFileName()); } catch (Exception exept) { Logger.LogError("Error on sending mail " + exept); } } } catch (Exception exept) { AuthMessageSender.SendEmail(bill.AdherentStolon.Stolon.Label, Configurations.Application.ContactMailAddress, "Stolons", "Erreur lors de la génération de la facture " + bill.BillNumber + " à " + bill.AdherentStolon.Adherent.Email, "Message d'erreur : " + exept.Message); } }
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); }
public static bool GenerateOrderPDF(ProducerBill bill) { return(GeneratePDF(bill.HtmlOrderContent, bill.GetOrderFilePath())); }
public static string GenerateHtmlBillContent(ProducerBill bill, ApplicationDbContext dbContext) { //Calcul total amount decimal totalAmount = 0; foreach (var billEntry in bill.BillEntries) { totalAmount += billEntry.Price; } bill.OrderAmount = totalAmount; bill.ProducersFee = bill.AdherentStolon.Stolon.ProducersFee; //GENERATION FACTURE StringBuilder billBuilder = new StringBuilder(); //Entete de facture // Producteur billBuilder.AppendLine("<p>" + bill.AdherentStolon.Adherent.CompanyName + "<p>"); billBuilder.AppendLine("<p>" + bill.AdherentStolon.Adherent.Surname?.ToUpper() + " " + bill.AdherentStolon.Adherent.Name?.ToUpper() + "<p>"); billBuilder.AppendLine("<p>" + bill.AdherentStolon.Adherent.Address + "</p>"); billBuilder.AppendLine("<p>" + bill.AdherentStolon.Adherent.PostCode + " " + bill.AdherentStolon.Adherent.City?.ToUpper() + "</p>"); billBuilder.AppendLine("<br>"); billBuilder.AppendLine("<p>Facture n° " + bill.BillNumber + "</p>"); billBuilder.AppendLine("<p>Année : " + DateTime.Now.Year); billBuilder.AppendLine("<p>Semaine : " + DateTime.Now.GetIso8601WeekOfYear()); billBuilder.AppendLine("<br>"); billBuilder.AppendLine("<table class=\"table\">"); billBuilder.AppendLine("<tr>"); billBuilder.AppendLine("<th>Produit</th>"); billBuilder.AppendLine("<th>Quantité</th>"); billBuilder.AppendLine("<th>TVA</th>"); billBuilder.AppendLine("<th>PU HT</th>"); billBuilder.AppendLine("<th>TOTAL HT</th>"); billBuilder.AppendLine("</tr>"); //Taux tax / Total HT Dictionary <decimal, decimal> taxTotal = new Dictionary <decimal, decimal>(); decimal totalWithoutTax = 0; foreach (var productBillEntries in bill.BillEntries.GroupBy(x => x.ProductStock.Product, x => x).OrderBy(x => x.Key.Name)) { int quantity = 0; productBillEntries.ForEach(x => quantity += x.Quantity); decimal productTotalWithoutTax = Convert.ToDecimal(productBillEntries.First().UnitPriceWithoutFeeAndTax *quantity); billBuilder.AppendLine("<tr>"); billBuilder.AppendLine("<td>" + productBillEntries.Key.Name + "</td>"); billBuilder.AppendLine("<td>" + productBillEntries.Key.GetQuantityString(quantity) + "</td>"); billBuilder.AppendLine("<td>" + (productBillEntries.Key.TaxEnum == Product.TAX.None ? "NA" : productBillEntries.Key.Tax.ToString()) + " %</td>"); billBuilder.AppendLine("<td>" + (productBillEntries.Key.Type == SellType.Piece ? productBillEntries.First().UnitPriceWithoutFeeAndTax : productBillEntries.First().PriceWithoutFeeAndTax) + " €" + "</td>"); billBuilder.AppendLine("<td>" + productTotalWithoutTax.ToString("0.00") + " €" + "</td>"); billBuilder.AppendLine("</tr>"); //Si tax, on ajoute au total du taux de la tva if (productBillEntries.Key.TaxEnum != Product.TAX.None) { if (taxTotal.ContainsKey(productBillEntries.Key.Tax)) { taxTotal[productBillEntries.Key.Tax] += productTotalWithoutTax; } else { taxTotal.Add(productBillEntries.Key.Tax, productTotalWithoutTax); } } totalWithoutTax += productTotalWithoutTax; } billBuilder.AppendLine("<tr>"); billBuilder.AppendLine("<td></td>"); billBuilder.AppendLine("<td></td>"); billBuilder.AppendLine("<td></td>"); billBuilder.AppendLine("<td>Total HT</td>"); billBuilder.AppendLine("<td>" + totalWithoutTax.ToString("0.00") + " €</td>"); billBuilder.AppendLine("</tr>"); bill.TaxAmount = 0; foreach (var tax in taxTotal) { decimal taxAmount = Math.Round(tax.Value / 100m * tax.Key, 2); billBuilder.AppendLine("<tr>"); billBuilder.AppendLine("<td></td>"); billBuilder.AppendLine("<td></td>"); billBuilder.AppendLine("<td></td>"); billBuilder.AppendLine("<td>TVA " + tax.Key + "%</td>"); billBuilder.AppendLine("<td>" + taxAmount.ToString("0.00") + " €</td>"); billBuilder.AppendLine("</tr>"); bill.TaxAmount += taxAmount; } billBuilder.AppendLine("<tr>"); billBuilder.AppendLine("<td></td>"); billBuilder.AppendLine("<td></td>"); billBuilder.AppendLine("<td></td>"); billBuilder.AppendLine("<td>Net à payer</td>"); billBuilder.AppendLine("<td>" + bill.BillAmount.ToString("0.00") + " €</td>"); billBuilder.AppendLine("</tr>"); billBuilder.AppendLine("</table>"); billBuilder.AddBootstrap(); billBuilder.AddFooterAndHeaderRemoval(); return(billBuilder.ToString()); }
//PRODUCER BILL private static string GenerateHtmlOrderContent(ProducerBill bill, ApplicationDbContext dbContext) { //GENERATION COMMANDE StringBuilder orderBuilder = new StringBuilder(); //Entete de facture // Producteur orderBuilder.AppendLine("<h2> Commande n°" + bill.OrderNumber + "</h2>"); orderBuilder.AppendLine("<p>" + bill.AdherentStolon.Adherent.CompanyName + "<br>"); orderBuilder.AppendLine("<Année : " + DateTime.Now.Year + "<br>"); orderBuilder.AppendLine("Semaine : " + DateTime.Now.GetIso8601WeekOfYear() + "<br></p>"); orderBuilder.AppendLine("<br>"); #region Par produit orderBuilder.AppendLine("<h3>Commande par produit</h3>"); orderBuilder.AppendLine("<table class=\"table\">"); orderBuilder.AppendLine("<tr>"); orderBuilder.AppendLine("<th>Produit</th>"); orderBuilder.AppendLine("<th>Quantité</th>"); orderBuilder.AppendLine("</tr>"); foreach (var productBillEntries in bill.BillEntries.GroupBy(x => x.ProductStock.Product, x => x).OrderBy(x => x.Key.Name)) { int quantity = 0; productBillEntries.ForEach(x => quantity += x.Quantity); orderBuilder.AppendLine("<tr>"); orderBuilder.AppendLine("<td>" + productBillEntries.Key.Name + "</td>"); orderBuilder.AppendLine("<td>" + productBillEntries.Key.GetQuantityString(quantity) + "</td>"); orderBuilder.AppendLine("</tr>"); } orderBuilder.AppendLine("</table>"); #endregion Par produit #region Par client orderBuilder.AppendLine("<h3>Commande par client</h3>"); var billEntriesByConsumer = bill.BillEntries.GroupBy(x => x.ConsumerBill.AdherentStolon); orderBuilder.AppendLine("<table class=\"table\">"); orderBuilder.AppendLine("<tr>"); orderBuilder.AppendLine("<th>Client</th>"); orderBuilder.AppendLine("<th>Produit</th>"); orderBuilder.AppendLine("<th>Quantité</th>"); orderBuilder.AppendLine("</tr>"); foreach (var group in billEntriesByConsumer.OrderBy(x => x.Key.LocalId)) { orderBuilder.AppendLine("<tr>"); orderBuilder.AppendLine("<td colspan=\"3\" style=\"border-top:1px solid;\">" + "<b>" + group.Key.LocalId + "</b>" + "</td>"); orderBuilder.AppendLine("</tr>"); foreach (var entries in group.OrderBy(x => x.ProductStock.ProductId)) { orderBuilder.AppendLine("<tr>"); orderBuilder.AppendLine("<td></td>"); orderBuilder.AppendLine("<td>" + entries.Name + "</td>"); orderBuilder.AppendLine("<td>" + entries.QuantityString + "</td>"); orderBuilder.AppendLine("</tr>"); } } orderBuilder.AppendLine("</table>"); #endregion Par client orderBuilder.AppendLine("<h3>Facturation</h3>"); orderBuilder.AppendLine("<p>" + "Montant total de la commande " + bill.OrderAmount.ToString("0.00") + "€<br>"); orderBuilder.AppendLine("Montant total du % du Stolons " + bill.FeeAmount.ToString("0.00") + "€<br>"); orderBuilder.AppendLine("Montant total à percevoir <b>" + bill.BillAmount.ToString("0.00") + "€</b> <i>(Dont " + bill.TaxAmount.ToString("0.00") + "€ TVA)</i></p>"); orderBuilder.AddBootstrap(); orderBuilder.AddFooterAndHeaderRemoval(); return(orderBuilder.ToString()); }
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 GetOrderUrl(ProducerBill prodBill) { return(BillsStockagePath + "/" + prodBill.OrderNumber + ".pdf"); }
public static string GetOrderFileName(this ProducerBill bill) { return(bill.OrderNumber + ".pdf"); }
public static string GetOrderFilePath(this ProducerBill bill) { return(Path.Combine(Configurations.Environment.WebRootPath, Configurations.BillsStockagePath, bill.GetOrderFileName())); }