private MailsSendedReport SendMail(IEnumerable <IAdherent> users, MailMessage mailMessage) { var activeAdherentStolon = GetActiveAdherentStolon(); MailsSendedReport report = new MailsSendedReport(); foreach (Adherent user in users) { if (!String.IsNullOrWhiteSpace(user.Email)) { try { AuthMessageSender.SendEmail(activeAdherentStolon.Stolon.Label, user.Email, user.Name, mailMessage.Title, mailMessage.Message); report.MailsSended++; } catch (Exception ex) { DotnetHelper.GetLogger <MailsController>().LogError(ex.ToString()); report.MailsNotSended++; } } } return(report); }
public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (!ModelState.IsValid) { return(View(model)); } Adherent stolonsUser = _context.Adherents.FirstOrDefault(x => model.Email.Equals(x.Email, StringComparison.CurrentCultureIgnoreCase)); if (stolonsUser == null) { return(View("ForgotPasswordConfirmation")); } ApplicationUser appUser = await _userManager.FindByEmailAsync(stolonsUser.Email.ToString()); string resetPasswordToken = await _userManager.GeneratePasswordResetTokenAsync(appUser); resetPasswordToken = System.Net.WebUtility.UrlEncode(resetPasswordToken); string link = "http://" + Configurations.SiteUrl + "/Account/ResetPassword?token=" + resetPasswordToken + "&mail=" + stolonsUser.Email; //Send mail ForgotPasswordEmailViewModel vmodel = new ForgotPasswordEmailViewModel(stolonsUser, link); AuthMessageSender.SendEmail(Configurations.Application.StolonsLabel, stolonsUser.Email, stolonsUser.Name, "Stolons: Oubli de votre mot de passe", base.RenderPartialViewToString("ResetPasswordEmailTemplate", vmodel), null, null); return(View("ForgotPasswordConfirmation")); }
public async Task <IActionResult> ChangePassword(ChangePasswordViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await GetCurrentAppUserAsync(); if (user != null) { var result = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); AuthMessageSender.SendEmail(GetCurrentStolon().Label, user.Email, user.Email, "Confirmation de changement de mot de passe", "<h3>Confirmation du changement de mot de passe :</h3><p> Nouveau mot de passe : " + model.NewPassword + "</p>"); return(RedirectToAction(nameof(Index), new { Message = ManageMessageId.ChangePasswordSuccess })); } AddErrors(result); return(View(model)); } return(RedirectToAction(nameof(Index), new { Message = ManageMessageId.Error })); }
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); } }
private static void GenerateOrderPdfAndSendEmail(ConsumerBill bill) { try { //Generate pdf file bool hasFile = GenerateBillPDF(bill); //Send mail to consumer string message = "<h3>" + bill.AdherentStolon.Stolon.OrderDeliveryMessage + "</h3>"; message += "<br/>"; message += "<h4>Votre comande de la semaine (Commande " + bill.BillNumber + ")" + ")</h4>"; message += bill.HtmlBillContent; if (hasFile) { message += "<br/><h4>Retrouver aussi en pièce jointe votre commande de la semaine (Commande " + bill.BillNumber + ")</h4>"; } if (bill.AdherentStolon.Token > 0) { message += "<p>Vous avez " + bill.AdherentStolon.Token + "Ṩ, pensez à payer avec vos stols lors de la récupération de votre commande.</p>"; } try { AuthMessageSender.SendEmail(bill.AdherentStolon.Stolon.Label, bill.AdherentStolon.Adherent.Email, bill.AdherentStolon.Adherent.Name, "Votre commande de la semaine (Commande " + bill.BillNumber + ")", message, hasFile ? File.ReadAllBytes(bill.GetBillFilePath()) : null, "Commande " + bill.GetBillFileName()); } 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 commande " + 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); }
private static void TriggerOrderMode(Stolon stolon, ApplicationDbContext dbContext) { 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.Stock) { productStock.State = ProductState.Disabled; } } dbContext.SaveChanges(); //Send email to all adherent that have subscribe to product by mail string htmlMessage = "Bonjour, les commandes sont ouverte chez " + stolon.Label + ". Vous pouvez dès maintenant et jusqu'à " + stolon.DeliveryAndStockUpdateDayStartDate.ToFrench() + " " + stolon.DeliveryAndStockUpdateDayStartDateHourStartDate + ":" + stolon.DeliveryAndStockUpdateDayStartDateMinuteStartDate + " commander vos produits sur " + Configurations.Application.StolonsUrl; //TODO générer un jolie message avec la liste des produits foreach (var adherentStolon in dbContext.AdherentStolons.Include(x => x.Adherent).Where(x => x.StolonId == stolon.Id && x.Adherent.ReceivedProductListByEmail).AsNoTracking()) { AuthMessageSender.SendEmail(stolon.Label, adherentStolon.Adherent.Email, adherentStolon.Adherent.Name + " " + adherentStolon.Adherent.Surname, "Commande ouverte chez " + stolon.Label, htmlMessage); } }
public IActionResult Send(Participant participant, int id, int[] contactId) { // Grant label to company var company = _companyRepository.GetBy(id); var group = _groupRepository.GetBy(participant.Group.GroupId); try { group.Grant(company); _companyRepository.SaveChanges(); } catch (GoedBezigException e) { TempData["error"] = e.Message; return(RedirectToAction("Show", new { id })); } if (group.Context.CurrentState is GrantedState) { //Grant label worked var sender = new AuthMessageSender(); //TODO: only has one contact atm. Multiple form submissions are the solution but I don't know how this will play out in this controller. foreach (var cId in contactId) { var contact = company.Contacts.First(co => co.ContactId == cId); sender.SendEmail(company.Name, contact.Email, group.Organization.Name, group.Motivation.MotivationText); } } TempData["success"] = $"Het label werd toegekend aan de organisatie."; return(RedirectToAction("Dashboard", "Group")); }
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); }); } }