public static void RunBirthdaysWithoutSMSorEmail(bool includeValidation = true)
    {
        bool   enableEmails = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendEmail").Value) == 1;
        string emailAddress = SystemVariableDB.GetByDescr("BirthdayNotificationEmail_EmailAddress").Value;

        bool incPatientsWithMobile = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_IncPatientsWithMobile").Value) == 1;
        bool incPatientsWithEmail  = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_IncPatientsWithEmail").Value) == 1;

        bool sendMondays    = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendMondays").Value) == 1;
        bool sendTuesdays   = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendTuesdays").Value) == 1;
        bool sendWednesdays = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendWednesdays").Value) == 1;
        bool sendThursdays  = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendThursdays").Value) == 1;
        bool sendFridays    = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendFridays").Value) == 1;
        bool sendSaturdays  = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendSaturdays").Value) == 1;
        bool sendSundays    = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendSundays").Value) == 1;

        int fromDaysAheadMondays     = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendFromDaysAhead_Mondays").Value);
        int untilDaysAheadMondays    = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendUntilDaysAhead_Mondays").Value);
        int fromDaysAheadTuesdays    = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendFromDaysAhead_Tuesdays").Value);
        int untilDaysAheadTuesdays   = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendUntilDaysAhead_Tuesdays").Value);
        int fromDaysAheadWednesdays  = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendFromDaysAhead_Wednesdays").Value);
        int untilDaysAheadWednesdays = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendUntilDaysAhead_Wednesdays").Value);
        int fromDaysAheadThursdays   = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendFromDaysAhead_Thursdays").Value);
        int untilDaysAheadThursdays  = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendUntilDaysAhead_Thursdays").Value);
        int fromDaysAheadFridays     = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendFromDaysAhead_Fridays").Value);
        int untilDaysAheadFridays    = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendUntilDaysAhead_Fridays").Value);
        int fromDaysAheadSaturdays   = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendFromDaysAhead_Saturdays").Value);
        int untilDaysAheadSaturdays  = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendUntilDaysAhead_Saturdays").Value);
        int fromDaysAheadSundays     = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendFromDaysAhead_Sundays").Value);
        int untilDaysAheadSundays    = Convert.ToInt32(SystemVariableDB.GetByDescr("BirthdayNotificationEmail_SendUntilDaysAhead_Sundays").Value);



        if (includeValidation && !enableEmails)
        {
            return;
        }
        if (!Utilities.IsValidEmailAddresses(emailAddress, false))
        {
            return;
        }

        if (includeValidation && DateTime.Today.DayOfWeek == DayOfWeek.Monday && !sendMondays)
        {
            return;
        }
        if (includeValidation && DateTime.Today.DayOfWeek == DayOfWeek.Tuesday && !sendTuesdays)
        {
            return;
        }
        if (includeValidation && DateTime.Today.DayOfWeek == DayOfWeek.Wednesday && !sendWednesdays)
        {
            return;
        }
        if (includeValidation && DateTime.Today.DayOfWeek == DayOfWeek.Thursday && !sendThursdays)
        {
            return;
        }
        if (includeValidation && DateTime.Today.DayOfWeek == DayOfWeek.Friday && !sendFridays)
        {
            return;
        }
        if (includeValidation && DateTime.Today.DayOfWeek == DayOfWeek.Saturday && !sendSaturdays)
        {
            return;
        }
        if (includeValidation && DateTime.Today.DayOfWeek == DayOfWeek.Sunday && !sendSundays)
        {
            return;
        }

        if (fromDaysAheadMondays > untilDaysAheadMondays)
        {
            return;
        }
        if (fromDaysAheadTuesdays > untilDaysAheadTuesdays)
        {
            return;
        }
        if (fromDaysAheadWednesdays > untilDaysAheadWednesdays)
        {
            return;
        }
        if (fromDaysAheadThursdays > untilDaysAheadThursdays)
        {
            return;
        }
        if (fromDaysAheadFridays > untilDaysAheadFridays)
        {
            return;
        }
        if (fromDaysAheadSaturdays > untilDaysAheadSaturdays)
        {
            return;
        }
        if (fromDaysAheadSundays > untilDaysAheadSundays)
        {
            return;
        }



        int fromDaysAhead = 0, untilDaysAhead = 0;

        if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
        {
            fromDaysAhead = fromDaysAheadMondays;    untilDaysAhead = untilDaysAheadMondays;
        }
        if (DateTime.Today.DayOfWeek == DayOfWeek.Tuesday)
        {
            fromDaysAhead = fromDaysAheadTuesdays;   untilDaysAhead = untilDaysAheadTuesdays;
        }
        if (DateTime.Today.DayOfWeek == DayOfWeek.Wednesday)
        {
            fromDaysAhead = fromDaysAheadWednesdays; untilDaysAhead = untilDaysAheadWednesdays;
        }
        if (DateTime.Today.DayOfWeek == DayOfWeek.Thursday)
        {
            fromDaysAhead = fromDaysAheadThursdays;  untilDaysAhead = untilDaysAheadThursdays;
        }
        if (DateTime.Today.DayOfWeek == DayOfWeek.Friday)
        {
            fromDaysAhead = fromDaysAheadFridays;    untilDaysAhead = untilDaysAheadFridays;
        }
        if (DateTime.Today.DayOfWeek == DayOfWeek.Saturday)
        {
            fromDaysAhead = fromDaysAheadSaturdays;  untilDaysAhead = untilDaysAheadSaturdays;
        }
        if (DateTime.Today.DayOfWeek == DayOfWeek.Sunday)
        {
            fromDaysAhead = fromDaysAheadSundays;    untilDaysAhead = untilDaysAheadSundays;
        }

        DateTime start = DateTime.Now.AddDays(fromDaysAhead);
        DateTime end   = DateTime.Now.AddDays(untilDaysAhead);

        System.Data.DataTable dt;
        try
        {
            dt = PatientDB.GetBirthdays_DataTable(start.Month, start.Day, end.Month, end.Day);
        }
        catch (CustomMessageException ex)
        {
            Logger.LogException(ex);
            return;
        }


        // get their mobile and emails to filter

        Patient[] patients = new Patient[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            patients[i]              = PatientDB.Load(dt.Rows[i]);
            patients[i].Person       = PersonDB.Load(dt.Rows[i]);
            patients[i].Person.Title = IDandDescrDB.Load(dt.Rows[i], "t_title_id", "t_descr");
        }

        Hashtable patientContactPhoneNbrHash = GetPatientPhoneNbrCache(patients);
        Hashtable patientContactEmailHash    = GetPatientEmailCache(patients);

        dt.Columns.Add("mobile", typeof(string));
        dt.Columns.Add("email", typeof(string));
        for (int i = dt.Rows.Count - 1; i >= 0; i--)
        {
            string phoneNumPatient = GetPhoneNbr(patientContactPhoneNbrHash, patients[i].Person.EntityID, true);
            string emailPatient    = GetEmail(patientContactEmailHash, patients[i].Person.EntityID);

            if ((!incPatientsWithMobile && (phoneNumPatient != null && phoneNumPatient.Length > 0)) ||
                (!incPatientsWithEmail && (emailPatient != null && emailPatient.Length > 0)))
            {
                dt.Rows.RemoveAt(i);
                continue;
            }

            dt.Rows[i]["mobile"] = phoneNumPatient == null ? "" : phoneNumPatient;
            dt.Rows[i]["email"]  = emailPatient == null ? "" : emailPatient;
        }



        // put in file to email

        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        sb.Append("\"" + "D.O.B." + "\"").Append(",");
        sb.Append("\"" + "Name" + "\"").Append(",");
        sb.Append("\"" + "Clinic Patient" + "\"").Append(",");
        sb.Append("\"" + "Mobile" + "\"").Append(",");
        sb.Append("\"" + "Email" + "\"").Append(",");

        sb.AppendLine();


        bool tblEmpty = (dt.Rows.Count == 1 && dt.Rows[0][0] == DBNull.Value);

        if (!tblEmpty)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sb.Append("\"" + Convert.ToDateTime(dt.Rows[i]["dob"]).ToString("d MMMMM, yyyy") + "\"").Append(",");
                sb.Append("\"" + dt.Rows[i]["firstname"].ToString() + " " + dt.Rows[i]["surname"].ToString() + (dt.Rows[i]["t_title_id"] == DBNull.Value || Convert.ToInt32(dt.Rows[i]["t_title_id"]) == 0 ? "" :  " (" + dt.Rows[i]["t_descr"] + ")") + "\"").Append(",");
                sb.Append("\"" + (Convert.ToBoolean(dt.Rows[i]["is_clinic_patient"]) ? "Yes" : "No") + "\"").Append(",");
                sb.Append("\"" + dt.Rows[i]["mobile"].ToString() + "\"").Append(",");
                sb.Append("\"" + dt.Rows[i]["email"].ToString() + "\"").Append(",");
                sb.AppendLine();
            }
        }


        // put in file, then email it

        string tmpLettersDirectory = Letter.GetTempLettersDirectory();

        if (!System.IO.Directory.Exists(tmpLettersDirectory))
        {
            throw new CustomMessageException("Temp letters directory doesn't exist");
        }

        string tmpDir = FileHelper.GetTempDirectoryName(tmpLettersDirectory);

        System.IO.Directory.CreateDirectory(tmpDir);
        string tmpFileName = tmpDir + "Birthdays.csv";

        System.IO.File.WriteAllText(tmpFileName, sb.ToString());

        Emailer.SimpleEmail(emailAddress, "Upcoming Birthdays", "Please find attached all birthdays from " + fromDaysAhead + " days ahead until " + untilDaysAhead + " days ahead.<br /><br />Regards,<br />Mediclinic", true, new string[] { tmpFileName }, null);

        System.IO.File.Delete(tmpFileName);
        System.IO.Directory.Delete(tmpDir);
    }
Exemple #2
0
    protected void FillGrid()
    {
        DataTable dt;

        try
        {
            dt = PatientDB.GetBirthdays_DataTable(Convert.ToInt32(ddlStartDate_Month.SelectedValue), Convert.ToInt32(ddlStartDate_Day.SelectedValue),
                                                  Convert.ToInt32(ddlEndDate_Month.SelectedValue), Convert.ToInt32(ddlEndDate_Day.SelectedValue));
        }
        catch (CustomMessageException ex)
        {
            SetErrorMessage(ex.Message);
            return;
        }


        // get their mobile and emails

        Patient[] patients = new Patient[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            patients[i]              = PatientDB.Load(dt.Rows[i]);
            patients[i].Person       = PersonDB.Load(dt.Rows[i]);
            patients[i].Person.Title = IDandDescrDB.Load(dt.Rows[i], "t_title_id", "t_descr");
        }

        Hashtable patientContactPhoneNbrHash = GetPatientPhoneNbrCache(patients);
        Hashtable patientContactEmailHash    = GetPatientEmailCache(patients);

        ArrayList patientIDs = new ArrayList();

        dt.Columns.Add("mobile", typeof(string));
        dt.Columns.Add("email", typeof(string));
        for (int i = dt.Rows.Count - 1; i >= 0; i--)
        {
            string phoneNumPatient = GetPhoneNbr(patientContactPhoneNbrHash, patients[i].Person.EntityID, true);
            string emailPatient    = GetEmail(patientContactEmailHash, patients[i].Person.EntityID);

            if ((!chkIncWithMobile.Checked && (phoneNumPatient != null && phoneNumPatient.Length > 0)) ||
                (!chkIncWithEmail.Checked && (emailPatient != null && emailPatient.Length > 0)))
            {
                dt.Rows.RemoveAt(i);
                continue;
            }

            dt.Rows[i]["mobile"] = phoneNumPatient == null ? "" : phoneNumPatient;
            dt.Rows[i]["email"]  = emailPatient == null ? ""  : emailPatient;

            patientIDs.Add(patients[i].PatientID);
        }


        hiddenPatientIDs.Value = string.Join(",", (int[])patientIDs.ToArray(typeof(int)));

        Session["data_bookingswithoutsmsoremail"] = dt;

        if (!IsPostBack)
        {
            chkUsePaging.Checked = dt.Rows.Count > 50;
        }

        this.GrdSummaryReport.AllowPaging = chkUsePaging.Checked;

        if (dt.Rows.Count > 0)
        {
            if (IsPostBack && Session["sortExpression_bookingswithoutsmsoremail"] != null && Session["sortExpression_bookingswithoutsmsoremail"].ToString().Length > 0)
            {
                DataView dataView = new DataView(dt);
                dataView.Sort = Session["sortExpression_bookingswithoutsmsoremail"].ToString();
                GrdSummaryReport.DataSource = dataView;
            }
            else
            {
                GrdSummaryReport.DataSource = dt;
            }


            try
            {
                GrdSummaryReport.DataBind();
                GrdSummaryReport.PagerSettings.FirstPageText = "1";
                GrdSummaryReport.PagerSettings.LastPageText  = GrdSummaryReport.PageCount.ToString();
                GrdSummaryReport.DataBind();
            }
            catch (Exception ex)
            {
                SetErrorMessage(ex.ToString());
            }
        }
        else
        {
            dt.Rows.Add(dt.NewRow());
            GrdSummaryReport.DataSource = dt;
            GrdSummaryReport.DataBind();

            int TotalColumns = GrdSummaryReport.Rows[0].Cells.Count;
            GrdSummaryReport.Rows[0].Cells.Clear();
            GrdSummaryReport.Rows[0].Cells.Add(new TableCell());
            GrdSummaryReport.Rows[0].Cells[0].ColumnSpan = TotalColumns;
            GrdSummaryReport.Rows[0].Cells[0].Text       = "No Record Found";
        }
    }