protected void EmailAllUsers(bool previewOnly, string DB = null) { bool IsDebug = Utilities.IsDev(); string curDbName = Session["DB"].ToString(); try { txtSubject.Text = txtSubject.Text.Trim(); if (txtSubject.Text.Length == 0) { lblEmailErrorMessage.Text = "<br />No Subject Entered<br /><br />"; return; } if (FreeTextBox1.Text.Trim().Length == 0) { lblEmailErrorMessage.Text = "<br />No Email Text Entered<br /><br />"; return; } string fromEmail = IsDebug ? "*****@*****.**" : ((SystemVariables)System.Web.HttpContext.Current.Session["SystemVariables"])["Email_FromEmail"].Value; string fromName = ((SystemVariables)Session["SystemVariables"])["Email_FromName"].Value; DataTable tblAllStaff = null; ArrayList emailInfoList = new ArrayList(); // list of Tuple<site, person, email> System.Data.DataTable tbl = DBBase.ExecuteQuery("EXEC sp_databases;", "master").Tables[0]; for (int i = 0; i < tbl.Rows.Count; i++) { string databaseName = tbl.Rows[i][0].ToString(); if (!Regex.IsMatch(databaseName, @"Mediclinic_\d{4}")) { continue; } if (chkIgnore0001.Checked && databaseName == @"Mediclinic_0001") { continue; } if (DB != null && databaseName != DB) { continue; } Session["DB"] = databaseName; Session["SystemVariables"] = SystemVariableDB.GetAll(); string clientSiteName = ((SystemVariables)Session["SystemVariables"])["Site"].Value; DataTable tblStaff = StaffDB.GetDataTable(); // get entity ID's so we can get all emails into a hashtable in one db query int[] entityIDs = new int[tblStaff.Rows.Count]; for (int j = 0; j < tblStaff.Rows.Count; j++) { entityIDs[j] = Convert.ToInt32(tblStaff.Rows[j]["person_entity_id"]); } Hashtable emailHash = PatientsContactCacheDB.GetBullkEmail(entityIDs, -1); // add the emails to the datatable - as comma-seperated string tblStaff.Columns.Add("database_name", typeof(String)); tblStaff.Columns.Add("emails", typeof(String)); tblStaff.Columns.Add("site", typeof(String)); for (int j = 0; j < tblStaff.Rows.Count; j++) { Staff s = StaffDB.LoadAll(tblStaff.Rows[j]); if (chkMasterAdminOnly.Checked && !s.IsMasterAdmin) { continue; } if (emailHash[s.Person.EntityID] != null) { if (Utilities.GetAddressType().ToString() == "Contact") { if (emailHash[s.Person.EntityID] != null) { foreach (Contact c in (Contact[])emailHash[s.Person.EntityID]) { if (Utilities.IsValidEmailAddress(c.AddrLine1.Trim())) { emailInfoList.Add(new Tuple <string, string, string>(clientSiteName, s.Person.FullnameWithoutMiddlename, c.AddrLine1.Trim())); } } } } else if (Utilities.GetAddressType().ToString() == "ContactAus") { if (emailHash[s.Person.EntityID] != null) { foreach (ContactAus c in (ContactAus[])emailHash[s.Person.EntityID]) { if (Utilities.IsValidEmailAddress(c.AddrLine1.Trim())) { emailInfoList.Add(new Tuple <string, string, string>(clientSiteName, s.Person.FullnameWithoutMiddlename, c.AddrLine1.Trim())); } } } } else { throw new Exception("Unknown AddressType in config: " + Utilities.GetAddressType().ToString().ToString()); } } } // if first table, set alldb's to this, else add this to alldb's list if (tblAllStaff == null) { tblAllStaff = tblStaff; } else { tblAllStaff.Merge(tblStaff); } Session.Remove("DB"); Session.Remove("SystemVariables"); } // send the email string output = "<h4>" + (previewOnly ? "Message Preview" : "Message Sent") + "</h4>" + Environment.NewLine + "<table style=\"min-width:400px;border:1px solid black;text-align:center;background-color:#FFFFFF;border-spacing:2px;border-collapse:separate;\"><tr><td><b>" + txtSubject.Text + "</b></td></tr></table><div style=\"height:10px;\"></div>" + Environment.NewLine + "<table style=\"min-width:500px;border:1px solid black;text-align:left;background-color:#FFFFFF;border-spacing:10px;border-collapse:separate;\"><tr style=\"min-height:200px;\"><td>" + (FreeTextBox1.Text.Length == 0 ? " " : FreeTextBox1.Text) + "</td></tr></table><br />"; output += "<h4>" + (previewOnly ? "Will Be Sent To" : "Sent To") + "</h4><table border=\"1\" class=\"table table-bordered table-striped table-grid table-grid-top-bottum-padding-normal auto_width block_center\" >"; string emails = string.Empty; foreach (Tuple <string, string, string> emailInfo in emailInfoList) { output += "<tr><td style=\"padding-left:4px;padding-right:4px;text-align:left !important;\">" + emailInfo.Item1 + "</td><td style=\"padding-left:4px;padding-right:4px;text-align:left !important;\">" + emailInfo.Item2 + "</td><td style=\"padding-left:4px;padding-right:4px;text-align:left !important;\">" + emailInfo.Item3 + "</td></tr>"; emails = (emails.Length == 0 ? "" : ",") + emailInfo.Item3; } if (emailInfoList.Count == 0) { output += "<tr><td style=\"padding-left:4px;padding-right:4px;text-align:left !important;\"> No Staff Have Emails In The Selected Site(s) </td></tr>"; } output += "</table>"; if (!previewOnly && emails.Length > 0) { // email: put to addresss as from address // email: put all emails in BCC EmailerNew.SimpleEmail( fromEmail, fromName, fromEmail, txtSubject.Text.Trim(), FreeTextBox1.Text, true, null, false, null, IsDebug ? "*****@*****.**" : emails ); } lblEmailOutput.Text = output; } finally { Session["DB"] = curDbName; Session["SystemVariables"] = SystemVariableDB.GetAll(); Page.ClientScript.RegisterStartupScript(this.GetType(), "download", @"<script language=javascript>addLoadEvent(function () { window.location.hash = ""emailing_tag""; });</script>"); } }
protected void GrdOrgInvoicesOutstanding_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Email") { int organisationID = Convert.ToInt32(e.CommandArgument); Organisation org = OrganisationDB.GetByID(organisationID); string[] emails = ContactDB.GetEmailsByEntityID(org.EntityID); if (emails.Length == 0) { SetErrorMessage("No email address set for '" + org.Name + "'. Please set one to email invoices to them."); return; } else { DataTable dt = Session["orginvoicesoutstanding_data"] as DataTable; DataRow row = dt.Select("organisation_id = " + organisationID)[0]; string invoiceIDsCommaSep = (string)row["invoice_ids_comma_sep"]; int[] invoiceIDs = Array.ConvertAll <string, int>(invoiceIDsCommaSep.Split(','), Convert.ToInt32); string tmpLettersDirectory = Letter.GetTempLettersDirectory(); string originalFile = Letter.GetLettersDirectory() + (!UserView.GetInstance().IsAgedCareView ? @"OverdueInvoiceTemplate.docx" : @"OverdueInvoiceTemplateAC.docx"); string tmpDir = FileHelper.GetTempDirectoryName(tmpLettersDirectory); System.IO.Directory.CreateDirectory(tmpDir); string outputFile = tmpDir + "OverdueInvoices.pdf"; try { Letter.GenerateOutstandingInvoices(originalFile, outputFile, invoiceIDs, -1, organisationID); EmailerNew.SimpleEmail( ((SystemVariables)System.Web.HttpContext.Current.Session["SystemVariables"])["Email_FromEmail"].Value, ((SystemVariables)System.Web.HttpContext.Current.Session["SystemVariables"])["Email_FromName"].Value, string.Join(",", emails), "Overdue Invoices", "Pease find your Invoices attached. <br/>Please call us if you do not agree with the Invoice amount stated.<br /><br />Thank you.", true, new string[] { outputFile }, false, null ); SetErrorMessage("Invoices Emailed to " + org.Name + " (" + string.Join(",", emails) + ")"); } catch (CustomMessageException cmEx) { SetErrorMessage(cmEx.Message); } catch (Exception ex) { SetErrorMessage("", ex.ToString()); } finally { try { if (System.IO.File.Exists(outputFile)) { System.IO.File.Delete(outputFile); } } catch (Exception) { } // delete temp dir if (tmpDir != null) { try { System.IO.Directory.Delete(tmpDir, true); } catch (Exception) { } } } } } if (e.CommandName == "Print") { int organisationID = Convert.ToInt32(e.CommandArgument); DataTable dt = Session["orginvoicesoutstanding_data"] as DataTable; DataRow row = dt.Select("organisation_id = " + organisationID)[0]; SetErrorMessage("Org ID: " + row["organisation_id"] + "<br />Invoices: " + row["invoice_ids_comma_sep"]); string invoiceIDsCommaSep = (string)row["invoice_ids_comma_sep"]; int[] invoiceIDs = Array.ConvertAll <string, int>(invoiceIDsCommaSep.Split(','), Convert.ToInt32); Letter.GenerateOutstandingInvoicesToPrint(Response, invoiceIDs, -1, organisationID, !UserView.GetInstance().IsAgedCareView); } if (e.CommandName == "SetAllPaid" || e.CommandName == "SetAllWiped") { try { int organisationID = Convert.ToInt32(e.CommandArgument); DataTable dt = Session["orginvoicesoutstanding_data"] as DataTable; DataRow row = dt.Select("organisation_id = " + organisationID)[0]; string invoiceIDsCommaSep = (string)row["invoice_ids_comma_sep"]; int[] invoiceIDs = Array.ConvertAll <string, int>(invoiceIDsCommaSep.Split(','), Convert.ToInt32); foreach (int invoiceID in invoiceIDs) { Invoice invoice = InvoiceDB.GetByID(invoiceID); if (invoice == null || invoice.IsPaID) { continue; } if (e.CommandName.Equals("SetAllPaid")) { ReceiptDB.Insert(null, 362, invoice.InvoiceID, invoice.TotalDue, Convert.ToDecimal(0.00), false, false, DateTime.MinValue, Convert.ToInt32(Session["StaffID"])); InvoiceDB.UpdateIsPaid(null, invoice.InvoiceID, true); } else if (e.CommandName.Equals("SetAllWiped")) { CreditNoteDB.Insert(invoice.InvoiceID, invoice.TotalDue, string.Empty, Convert.ToInt32(Session["StaffID"])); InvoiceDB.UpdateIsPaid(null, invoice.InvoiceID, true); } } SetErrorMessage("Invoices Set As " + (e.CommandName.Equals("SetAllPaid") ? "Paid" : "Wiped") + " : " + row["invoice_ids_comma_sep"]); GrdOrgInvoicesOutstanding_FillGrid(); } catch (CustomMessageException cmEx) { SetErrorMessage(cmEx.Message); } catch (Exception ex) { SetErrorMessage("", ex.ToString()); } } }
protected static string SendEmailToPT(bool incSending, DataTable dt, Patient patient, Site site) { DataRow row = dt.Select("patient_id = " + patient.PatientID)[0]; string daysOverdueCommaSep = (string)row["days_overdue_comma_sep"]; string[] emails = ContactDB.GetEmailsByEntityID(patient.Person.EntityID); if (emails.Length == 0) { return(string.Empty + "[" + daysOverdueCommaSep + "]"); } if (incSending) { string invoiceIDsCommaSep = (string)row["invoice_ids_comma_sep"]; int[] invoiceIDs = Array.ConvertAll <string, int>(invoiceIDsCommaSep.Split(','), Convert.ToInt32); bool isClinics = site.SiteType.ID != 2; string tmpLettersDirectory = Letter.GetTempLettersDirectory(); string originalFile = Letter.GetLettersDirectory() + (isClinics ? @"OverdueInvoiceTemplate.docx" : @"OverdueInvoiceTemplateAC.docx"); string tmpDir = FileHelper.GetTempDirectoryName(tmpLettersDirectory); System.IO.Directory.CreateDirectory(tmpDir); string outputFile = tmpDir + "OverdueInvoices.pdf"; try { Letter.GenerateOutstandingInvoices(originalFile, outputFile, invoiceIDs, patient.PatientID, -1); EmailerNew.SimpleEmail( ((SystemVariables)System.Web.HttpContext.Current.Session["SystemVariables"])["Email_FromEmail"].Value, ((SystemVariables)System.Web.HttpContext.Current.Session["SystemVariables"])["Email_FromName"].Value, IsDebug ? "*****@*****.**" : string.Join(",", emails), "Overdue Invoices", "Pease find your Invoices attached. <br />Please call us if you do not agree with the Invoice amount stated.<br /><br />Thank you.", true, new string[] { outputFile }, false, null, "*****@*****.**" ); } finally { try { if (System.IO.File.Exists(outputFile)) { System.IO.File.Delete(outputFile); } } catch (Exception) { } // delete temp dir if (tmpDir != null) { try { System.IO.Directory.Delete(tmpDir, true); } catch (Exception) { } } } } return(string.Join(", ", emails) + "[" + daysOverdueCommaSep + "]"); }
protected void btnEmailToFac_Click(object sender, EventArgs e) { Booking booking = BookingDB.GetByID(GetFormBooking()); string[] emails = ContactDB.GetEmailsByEntityID(booking.Organisation.EntityID); if (emails.Length == 0) { SetErrorMessage("No email address set for '" + booking.Organisation.Name + "'. Please set one to email treatment list to them."); return; } else { string tmpLettersDirectory = FileHelper.GetTempDirectoryName(Letter.GetTempLettersDirectory()); Directory.CreateDirectory(tmpLettersDirectory); string originalFile = Letter.GetLettersDirectory() + @"ACTreatmentList.docx"; string tmpOutputFile = tmpLettersDirectory + "TreatmentList.pdf"; if (!File.Exists(originalFile)) { SetErrorMessage("Template File 'ACTreatmentList.docx' does not exist."); return; } MergeFile(true, originalFile, tmpOutputFile); try { EmailerNew.SimpleEmail( ((SystemVariables)System.Web.HttpContext.Current.Session["SystemVariables"])["Email_FromEmail"].Value, ((SystemVariables)System.Web.HttpContext.Current.Session["SystemVariables"])["Email_FromName"].Value, (Utilities.IsDev() ? "*****@*****.**" : string.Join(",", emails)), "Treatement List", "Pease find the <u>Residents Treated List</u> for " + booking.DateStart.ToString("d MMM, yyyy") + " at " + booking.Organisation.Name + "<br /><br />Regards,<br />" + ((SystemVariables)System.Web.HttpContext.Current.Session["SystemVariables"])["Email_FromName"].Value, true, new string[] { tmpOutputFile }, false, null ); SetErrorMessage("Emailed to " + booking.Organisation.Name + " (" + string.Join(", ", emails) + ")"); } catch (CustomMessageException cmEx) { SetErrorMessage(cmEx.Message); } catch (Exception ex) { SetErrorMessage("", ex.ToString()); } finally { try { if (System.IO.File.Exists(tmpOutputFile)) { System.IO.File.Delete(tmpOutputFile); } } catch (Exception) { } // delete temp dir if (tmpLettersDirectory != null) { try { System.IO.Directory.Delete(tmpLettersDirectory, true); } catch (Exception) { } } } } }