/// <summary> /// Handle the send button click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSend_Click(object sender, EventArgs e) { uprgSendEmail.DisplayAfter = 0; string _OrderType = ""; TempCoffeeCheckup _TempCoffeeCheckup = new TempCoffeeCheckup(); // get all the contacts that need to be reminded List <ContactToRemindWithItems> _ContactsToRemind = _TempCoffeeCheckup.GetAllContactAndItems(); SendCheckEmailTextsData _EmailTextData = new SendCheckEmailTextsData(); CustomersTbl _Customer = new CustomersTbl(); int _RemindersSent = 0, _RemindersFailed = 0; for (int i = 0; i < _ContactsToRemind.Count; i++) { try { _EmailTextData.Header = tbxEmailIntro.Text; _EmailTextData.Footer = tbxEmailFooter.Text; if (String.Concat(_ContactsToRemind[i].EmailAddress, _ContactsToRemind[i].AltEmailAddress).Contains("@")) { // customer has an email address _ContactsToRemind[i].ReminderCount++; if (_ContactsToRemind[i].ReminderCount < CONST_MAXREMINDERS) { _EmailTextData.Body = PersonalizeBodyText(_ContactsToRemind[i]); // look through items to see if this is a reoccuring or auto order _OrderType = GetTheOrderType(_ContactsToRemind[i]); // if auto fulfil add to orders with auto add in the note if (String.IsNullOrWhiteSpace(_OrderType)) { _EmailTextData.Body += "We will only place an order on your request, no order has been added, this just a reminder."; } else { _EmailTextData.Body += "This is a" + (IsVowel(_OrderType[0]) ? "n " : " ") + _OrderType + ", please respond to cancel."; } if (_ContactsToRemind[i].ReminderCount == CONST_MAXREMINDERS - 1) { _EmailTextData.Body = String.Concat("This is your last reminder email. Next time reminders will be disabled until you order again.", _EmailTextData.Body); } // else next reminder in week*reminder count??? #if _DEBUG if (SendReminder(_EmailTextData, _ContactsToRemind[i], _OrderType, _RemindersSent)) #else if (SendReminder(_EmailTextData, _ContactsToRemind[i], _OrderType)) #endif { _RemindersSent++; } else { _RemindersFailed++; } //--- delay reminders for those that have too many, and update reminder count for all if (_ContactsToRemind[i].ReminderCount >= CONST_FORCEREMINDERDELAYCOUNT) { DateTime _ForceDate = _ContactsToRemind[i].NextPrepDate.AddDays(10 * (_ContactsToRemind[i].ReminderCount - CONST_FORCEREMINDERDELAYCOUNT + 1)); ClientUsageTbl _ClientUsage = new ClientUsageTbl(); _ClientUsage.ForceNextCoffeeDate(_ForceDate, _ContactsToRemind[i].CustomerID); } // icrement count and set last date _Customer.SetSentReminderAndIncrementReminderCount(DateTime.Now.Date, _ContactsToRemind[i].CustomerID); ////---- check the last sent date? } else { _Customer.DisableCustomer(_ContactsToRemind[i].CustomerID); _RemindersFailed++; } #if _DEBUG // if (_RemindersSent > 5) break; #endif } } catch (Exception _Ex) { // just incse we have an error we normally do not have showMessageBox _ExMsg = new showMessageBox(this.Page, "Error sending...", _Ex.Message); _RemindersFailed++; } } showMessageBox _MsgBox = new showMessageBox(this.Page, "Reminder emails status", String.Format("Reminders processed. Sent: {0}; Failed: {1}", _RemindersSent, _RemindersFailed)); string _RedirectURL = String.Format("{0}?LastSentDate={1:d}", ResolveUrl("~/Pages/SentRemindersSheet.aspx"), DateTime.Now.Date); Response.Redirect(_RedirectURL); PrepPageData(); }
private void SetListOfContactsToSendReminderTo() // ref List<string> strs, ref List<DateTime> dts) { // strs.Add("first get a list of contact that are on the reoccuringlist"); // dts.Add(DateTime.Now); // first get a list of contact that are on the reoccuringlist List <ContactToRemindWithItems> _ContactsToRemind = GetReocurringContacts(); // strs.Add("add the clients to that list that probably need in the next sechduled delivery period"); // dts.Add(DateTime.Now); // now add the clients to that list that probably need in the next sechduled delivery period AddAllContactsToRemind(ref _ContactsToRemind); //, ref strs, ref dts); _ContactsToRemind.Sort((a, b) => String.Compare(a.CompanyName, b.CompanyName)); // need to sort by name // strs.Add("write data to the table so it can be displayed"); // dts.Add(DateTime.Now); /// write data to the table so it can be displayed TempCoffeeCheckup _TempCoffeeCheckup = new TempCoffeeCheckup(); bool _CouldDelete = (_TempCoffeeCheckup.DeleteAllContactRecords()) && (_TempCoffeeCheckup.DeleteAllContactItems()); if (!_CouldDelete) { showMessageBox _smb = new showMessageBox(this.Page, "Old Temp Table delete", "Error deleting old temp tables"); } else { ItemTypeTbl _ITT = new ItemTypeTbl(); List <int> _OnlyCoffeeItems = _ITT.GetAllItemIDsofServiceType(TrackerTools.CONST_SERVTYPECOFFEE); // also the group items - since they are coffee too. _OnlyCoffeeItems.AddRange(_ITT.GetAllItemIDsofServiceType(TrackerTools.CONST_SERVTYPEGROUPITEM)); bool _AllAdded = false; bool _DoAdd = false; int j = 0; for (int i = 0; i < _ContactsToRemind.Count; i++) { // only insert client if the contain coffee item _DoAdd = false; j = 0; while ((j < _ContactsToRemind[i].ItemsContactRequires.Count) && (!_DoAdd)) { _DoAdd = _OnlyCoffeeItems.Contains(_ContactsToRemind[i].ItemsContactRequires[j].ItemID); j++; } if (_DoAdd) { _AllAdded = _TempCoffeeCheckup.InsertContacts(_ContactsToRemind[i]) || _AllAdded; foreach (ItemContactRequires _Item in _ContactsToRemind[i].ItemsContactRequires) // (int j = 0; j < _ContactsToRemind[i].ItemsContactRequires.Count; j++) { _AllAdded = _TempCoffeeCheckup.InsertContactItems(_Item) || _AllAdded; } } } if (!_AllAdded) { showMessageBox _smb = new showMessageBox(this.Page, "Not all records added to Temp Table", "Error adding some records added to Temp Table"); } } // gvCustomerCheckup.DataSource = _ContactsToRemind; // gvCustomerCheckup.DataBind(); }