public static HealthCardAction[] GetReceivedActionsByPatientID(int patient_id)
    {
        string sql = @"
            SELECT
                     hca.health_card_action_id, hca.health_card_id, hca.health_card_action_type_id, hca.action_date, type.descr
            FROM
                     HealthCardAction AS hca 
                     INNER JOIN HealthCardActionType AS type ON type.health_card_action_type_id = hca.health_card_action_type_id
                     INNER JOIN HealthCard AS hc ON hc.health_card_id = hca.health_card_id

            WHERE
                     hc.patient_id = " + patient_id + @" AND hca.health_card_action_type_id = 0
            ORDER BY
                     hca.action_date DESC, hca.health_card_action_id DESC";

        DataTable tbl = DBBase.ExecuteQuery(sql).Tables[0];

        HealthCardAction[] list = new HealthCardAction[tbl.Rows.Count];
        for (int i = 0; i < tbl.Rows.Count; i++)
        {
            list[i] = Load(tbl.Rows[i]);
        }

        return(list);
    }
    // use this if using a bulk data retrival of HealthCardActions to pass in for less db calls
    public static DateTime GetEPCDateSigned(HealthCardAction[] healthCardActions, DateTime booking_date)
    {
        /*
         * go through history (HealthCardAction) list for a patient
         * get all that is type Received
         * order by date DESC
         * find "first" where  booking_date >= date of that history item
        */
        for (int i = 0; i < healthCardActions.Length; i++)
        {
            if (booking_date >= healthCardActions[i].ActionDate.Date)
                return healthCardActions[i].ActionDate.Date;
        }

        return DateTime.MinValue; // not found
    }
    public static Hashtable GetReceivedActionsByPatientIDs(int[] patient_ids)
    {
        string sql = @"
            SELECT
                     hca.health_card_action_id, hca.health_card_id, hca.health_card_action_type_id, hca.action_date, type.descr,   hc.patient_id
            FROM
                     HealthCardAction AS hca 
                     INNER JOIN HealthCardActionType AS type ON type.health_card_action_type_id = hca.health_card_action_type_id
                     INNER JOIN HealthCard AS hc ON hc.health_card_id = hca.health_card_id

            WHERE
                     " + (patient_ids != null && patient_ids.Length > 0 ? " hc.patient_id IN (" + string.Join(",", patient_ids) + @")" : "1 <> 1") + @" AND hca.health_card_action_type_id = 0
            ORDER BY
                     hca.action_date DESC, hca.health_card_action_id DESC";

        DataTable tbl = DBBase.ExecuteQuery(sql).Tables[0];

        Hashtable hash = new Hashtable();

        for (int i = 0; i < tbl.Rows.Count; i++)
        {
            HealthCardAction hca = Load(tbl.Rows[i]);
            int patient_id       = Convert.ToInt32(tbl.Rows[i]["patient_id"]);
            if (hash[patient_id] == null)
            {
                hash[patient_id] = new System.Collections.ArrayList();
            }
            ((System.Collections.ArrayList)hash[patient_id]).Add(hca);
        }


        // convert from arraylists to arrays
        ArrayList keys = new ArrayList();

        foreach (System.Collections.DictionaryEntry de in hash)
        {
            keys.Add(de.Key);
        }
        foreach (int key in keys)
        {
            hash[key] = (HealthCardAction[])((ArrayList)hash[key]).ToArray(typeof(HealthCardAction));
        }
        ;

        return(hash);
    }
 public LetterPrintHistory(int letter_print_history_id, int letter_id, int letter_print_history_send_method_id,
                           int booking_id, int patient_id, int organisation_id, int register_referrer_id,
                           int staff_id, int health_card_action_id, DateTime date, string doc_name, bool has_doc)
 {
     this.letter_print_history_id = letter_print_history_id;
     this.letter                  = letter_id       == -1 ? null : new Letter(letter_id);
     this.send_method             = new IDandDescr(letter_print_history_send_method_id);
     this.booking                 = new Booking(booking_id);
     this.patient                 = patient_id           == -1 ? null : new Patient(patient_id);
     this.organisation            = organisation_id      ==  0 ? null : new Organisation(organisation_id);
     this.register_referrer       = register_referrer_id == -1 ? null : new RegisterReferrer(register_referrer_id);
     this.staff                   = staff_id             == -1 ? null : new Staff(staff_id);
     this.health_card_action      = new HealthCardAction(health_card_action_id);
     this.date                    = date;
     this.doc_name                = doc_name;
     this.has_doc                 = has_doc;
 }
Beispiel #5
0
    protected static void MergeDocument(string sourceTemplatePath, string outputDocPath,
                    Booking booking, Patient patient, Contact patientAddress, ContactAus patientAddressAus, Contact patientPhone, ContactAus patientPhoneAus, HealthCard medicareCard, HealthCard dvaCard, Site site,
                    Organisation org, Contact orgAddress, ContactAus orgAddressAus, Contact orgPhone, ContactAus orgPhoneAus, Contact orgFax, ContactAus orgFaxAus, Contact orgWeb, ContactAus orgWebAus, Contact orgEmail, ContactAus orgEmailAus,
                    Staff staff, RegisterReferrer regReferrer, Contact referrerAddress, ContactAus referrerAddressAus, Contact referrerPhone, ContactAus referrerPhoneAus, Contact referrerFax, ContactAus referrerFaxAus, HealthCardAction healthCardAction,

                    DateTime pt_last_bk_date, DateTime epc_expire_date, int      epc_count_remaining, 

                    bool isDoubleSidedPrinting, string[] extraPages,
                    bool keepHistoryInDB, bool keepHistoryInFile, int letterPrintHistorySendMethodID, string historyDir, string historyFileName, int letterID)
    {
        HealthCard activeHcCard = (medicareCard != null && medicareCard.IsActive) ? medicareCard : ((dvaCard != null && dvaCard.IsActive) ? dvaCard : null);

        DataSet sourceDataSet = new DataSet();
        sourceDataSet.Tables.Add("MergeIt");

        sourceDataSet.Tables[0].Columns.Add("curr_date");

        sourceDataSet.Tables[0].Columns.Add("pt_name");
        sourceDataSet.Tables[0].Columns.Add("pt_title");
        sourceDataSet.Tables[0].Columns.Add("pt_firstname");
        sourceDataSet.Tables[0].Columns.Add("pt_middlename");
        sourceDataSet.Tables[0].Columns.Add("pt_surname");
        sourceDataSet.Tables[0].Columns.Add("pt_gender");
        sourceDataSet.Tables[0].Columns.Add("pt_dob");
        sourceDataSet.Tables[0].Columns.Add("pt_dob_day_month_only");

        sourceDataSet.Tables[0].Columns.Add("pt_conditions");

        sourceDataSet.Tables[0].Columns.Add("pt_addr");
        sourceDataSet.Tables[0].Columns.Add("pt_addr_tabbedx1");
        //sourceDataSet.Tables[0].Columns.Add("pt_addr_line1");
        //sourceDataSet.Tables[0].Columns.Add("pt_addr_line2");
        //sourceDataSet.Tables[0].Columns.Add("pt_addr_street");
        //sourceDataSet.Tables[0].Columns.Add("pt_addr_suburb");
        //sourceDataSet.Tables[0].Columns.Add("pt_addr_postcode");
        //sourceDataSet.Tables[0].Columns.Add("pt_addr_country");
        sourceDataSet.Tables[0].Columns.Add("pt_phone");

        sourceDataSet.Tables[0].Columns.Add("pt_last_bk_date");

        sourceDataSet.Tables[0].Columns.Add("pt_hc_card_nbr");
        sourceDataSet.Tables[0].Columns.Add("pt_hc_card_name");
        sourceDataSet.Tables[0].Columns.Add("pt_hc_card_refsigneddate");
        sourceDataSet.Tables[0].Columns.Add("pt_epc_expire_date");
        sourceDataSet.Tables[0].Columns.Add("pt_epc_count_remaining");

        sourceDataSet.Tables[0].Columns.Add("pt_mc_card_nbr");
        sourceDataSet.Tables[0].Columns.Add("pt_mc_card_name");
        sourceDataSet.Tables[0].Columns.Add("pt_mc_card_refsigneddate");

        sourceDataSet.Tables[0].Columns.Add("pt_dvacard_nbr");
        sourceDataSet.Tables[0].Columns.Add("pt_dvacard_name");
        sourceDataSet.Tables[0].Columns.Add("pt_dvacard_refsigneddate");

        sourceDataSet.Tables[0].Columns.Add("org_name");
        sourceDataSet.Tables[0].Columns.Add("org_abn");
        sourceDataSet.Tables[0].Columns.Add("org_acn");
        sourceDataSet.Tables[0].Columns.Add("org_bpay_account");

        sourceDataSet.Tables[0].Columns.Add("org_addr");
        sourceDataSet.Tables[0].Columns.Add("org_addr_tabbedx1");
        //sourceDataSet.Tables[0].Columns.Add("org_addr_line1");
        //sourceDataSet.Tables[0].Columns.Add("org_addr_line2");
        //sourceDataSet.Tables[0].Columns.Add("org_addr_street");
        //sourceDataSet.Tables[0].Columns.Add("org_addr_suburb");
        //sourceDataSet.Tables[0].Columns.Add("org_addr_postcode");
        //sourceDataSet.Tables[0].Columns.Add("org_addr_country");
        sourceDataSet.Tables[0].Columns.Add("org_phone");
        sourceDataSet.Tables[0].Columns.Add("org_office_fax");
        sourceDataSet.Tables[0].Columns.Add("org_web");
        sourceDataSet.Tables[0].Columns.Add("org_email");

        sourceDataSet.Tables[0].Columns.Add("ref_name");
        sourceDataSet.Tables[0].Columns.Add("ref_title");
        sourceDataSet.Tables[0].Columns.Add("ref_firstname");
        sourceDataSet.Tables[0].Columns.Add("ref_middlename");
        sourceDataSet.Tables[0].Columns.Add("ref_surname");
        //sourceDataSet.Tables[0].Columns.Add("ref_gender");
        //sourceDataSet.Tables[0].Columns.Add("ref_dob");

        sourceDataSet.Tables[0].Columns.Add("ref_addr");
        sourceDataSet.Tables[0].Columns.Add("ref_addr_tabbedx1");
        //sourceDataSet.Tables[0].Columns.Add("ref_addr_line1");
        //sourceDataSet.Tables[0].Columns.Add("ref_addr_line2");
        //sourceDataSet.Tables[0].Columns.Add("ref_addr_street");
        //sourceDataSet.Tables[0].Columns.Add("ref_addr_suburb");
        //sourceDataSet.Tables[0].Columns.Add("ref_addr_postcode");
        //sourceDataSet.Tables[0].Columns.Add("ref_addr_country");
        sourceDataSet.Tables[0].Columns.Add("ref_phone");
        sourceDataSet.Tables[0].Columns.Add("ref_fax");

        sourceDataSet.Tables[0].Columns.Add("bk_date");
        sourceDataSet.Tables[0].Columns.Add("bk_time");
        sourceDataSet.Tables[0].Columns.Add("bk_length");

        sourceDataSet.Tables[0].Columns.Add("bk_prov_name");
        sourceDataSet.Tables[0].Columns.Add("bk_prov_title");
        sourceDataSet.Tables[0].Columns.Add("bk_prov_firstname");
        sourceDataSet.Tables[0].Columns.Add("bk_prov_middlename");
        sourceDataSet.Tables[0].Columns.Add("bk_prov_surname");
        sourceDataSet.Tables[0].Columns.Add("bk_prov_number");
        //sourceDataSet.Tables[0].Columns.Add("bk_prov_gender");
        //sourceDataSet.Tables[0].Columns.Add("bk_prov_dob");

        sourceDataSet.Tables[0].Columns.Add("bk_treatment_notes");

        sourceDataSet.Tables[0].Columns.Add("bk_offering_name");
        sourceDataSet.Tables[0].Columns.Add("bk_offering_short_name");
        sourceDataSet.Tables[0].Columns.Add("bk_offering_descr");

        string bookingNotes = string.Empty;
        if (booking != null)
        {
            if (booking.Patient != null)
            {
                foreach (Note note in booking.GetTreatmentNotes())
                    bookingNotes += "Treatment Note:" + Environment.NewLine + Environment.NewLine + note.Text + Environment.NewLine + Environment.NewLine;
            }
            else if (patient != null) // group bookings - but need patientID
            {
                BookingPatient bp = BookingPatientDB.GetByBookingAndPatientID(booking.BookingID, patient.PatientID);
                if (bp != null)
                    foreach (Note note in NoteDB.GetByEntityID(bp.EntityID, "252"))
                        bookingNotes += "Treatment Note:" + Environment.NewLine + Environment.NewLine + note.Text + Environment.NewLine + Environment.NewLine;
            }

        }

        /*  -- for testing
            string s1 = patient == null ? "No patient found" : patient.Person.FullnameWithTitleWithoutMiddlename;
            string s2 = patient == null ? "No patient found" : (patient.Person.Title.ID == 0 ? "" : patient.Person.Title.Descr);
            string s3 = patient == null ? "" : patient.Person.Firstname;
            string s4 = patient == null ? "" : patient.Person.Middlename;
            string s5 = patient == null ? "" : patient.Person.Surname;
            string s6 = patient == null ? "" : patient.Person.Gender;
            string s7 = patient == null ? "" : patient.Person.Dob == DateTime.MinValue ? "" : patient.Person.Dob.ToString("d MMMM, yyyy");

            string s8 = patientAddress == null ? "No address found" : patientAddress.AddrLine1;
            string s9 = patientAddress == null ? "" : patientAddress.AddrLine2;
            string s10 = patientAddress == null || patientAddress.AddressChannel == null ? "" : (patientAddress.AddressChannel.AddressChannelID == 1 ? "" : patientAddress.AddressChannel.DisplayName);
            string s11 = patientAddress == null ? "" : patientAddress.Suburb.Name;
            string s12 = patientAddress == null ? "" : patientAddress.Suburb.Postcode;
            string s13 = patientAddress == null || patientAddress.Country == null ? "" : patientAddress.Country.Descr;

            string s14 = activeHcCard == null ? "No hc card found" : activeHcCard.CardNbr + " - " + activeHcCard.CardFamilyMemberNbr;
            string s15 = activeHcCard == null ? "" : activeHcCard.CardName;
            string s16 = activeHcCard == null ? "" : activeHcCard.DateReferralSigned.ToString("d MMMM, yyyy");

            string s17 = medicareCard == null ? "No medicare card found" : medicareCard.CardNbr + " - " + medicareCard.CardFamilyMemberNbr;
            string s18 = medicareCard == null ? "" : medicareCard.CardName;
            string s19 = medicareCard == null ? "" : medicareCard.DateReferralSigned.ToString("d MMMM, yyyy");

            string s20 = dvaCard == null ? "No dva card found" : dvaCard.CardNbr;
            string s21 = dvaCard == null ? "" : dvaCard.CardName;
            string s22 = dvaCard == null ? "" : dvaCard.DateReferralSigned.ToString("d MMMM, yyyy");

            string s23 = org != null ? org.Name        : site.Name;
            string s24 = org != null ? org.Abn         : site.Abn;
            string s25 = org != null ? org.Acn         : site.Acn;
            string s26 = org != null ? org.BpayAccount : site.BankBpay;

            string s27 = orgAddress == null ? "No address found" : orgAddress.AddrLine1;
            string s28 = orgAddress == null ? "" : orgAddress.AddrLine2;
            string s29 = orgAddress == null ? "" : (orgAddress.AddressChannel.AddressChannelID == 1 ? "" : orgAddress.AddressChannel.DisplayName);
            string s30 = orgAddress == null ? "" : orgAddress.Suburb.Name;
            string s31 = orgAddress == null ? "" : orgAddress.Suburb.Postcode;
            string s32 = orgAddress == null || orgAddress.Country == null ? "" : orgAddress.Country.Descr;

            string s33 = regReferrer == null ? "No referrer found" : regReferrer.Referrer.Person.FullnameWithTitleWithoutMiddlename;
            string s34 = regReferrer == null ? "No referrer found" : (regReferrer.Referrer.Person.Title.ID == 0 ? "" : regReferrer.Referrer.Person.Title.Descr);
            string s35 = regReferrer == null ? "" : regReferrer.Referrer.Person.Firstname;
            string s36 = regReferrer == null ? "" : regReferrer.Referrer.Person.Middlename;
            string s37 = regReferrer == null ? "" : regReferrer.Referrer.Person.Surname;
            string s38 = regReferrer == null ? "" : regReferrer.Referrer.Person.Gender;
            string s39 = regReferrer == null ? "" : regReferrer.Referrer.Person.Dob.ToString("d MMMM, yyyy");

            string s40 = referrerAddress == null ? "No address found" : referrerAddress.AddrLine1;
            string s41 = referrerAddress == null ? "" : referrerAddress.AddrLine2;
            string s42 = referrerAddress == null ? "" : (referrerAddress.AddressChannel.AddressChannelID == 1 ? "" : referrerAddress.AddressChannel.DisplayName);
            string s43 = referrerAddress == null ? "" : referrerAddress.Suburb.Name;
            string s44 = referrerAddress == null ? "" : referrerAddress.Suburb.Postcode;
            string s45 = referrerAddress == null || referrerAddress.Country == null ? "" : referrerAddress.Country.Descr;

            string s46 = booking == null ? "" : booking.DateStart.ToString("d MMMM, yyyy");
            string s47 = booking == null ? "" : booking.DateStart.ToString("HH:mm");

            string s48 = booking == null || booking.Provider == null ? "" : booking.Provider.Person.FullnameWithTitleWithoutMiddlename;
            string s49 = booking == null || booking.Provider == null ? "" : (booking.Provider.Person.Title.ID == 0 ? "" : booking.Provider.Person.Title.Descr);
            string s50 = booking == null || booking.Provider == null ? "" : booking.Provider.Person.Firstname;
            string s51 = booking == null || booking.Provider == null ? "" : booking.Provider.Person.Middlename;
            string s52 = booking == null || booking.Provider == null ? "" : booking.Provider.Person.Surname;
            string s53 = booking == null || booking.Provider == null ? "" : booking.Provider.Person.Gender;
            string s54 = booking == null || booking.Provider == null ? "" : booking.Provider.Person.Dob.ToString("d MMMM, yyyy");

            string s55 = booking == null ? "" : bookingNotes;

            string s56 = booking == null || booking.Offering == null ? "" : booking.Offering.Name;
            string s57 = booking == null || booking.Offering == null ? "" : booking.Offering.ShortName;
            string s58 = booking == null || booking.Offering == null ? "" : booking.Offering.Descr;
        */

        string patientAddressText  , patientAddressTabbedText  , patientPhoneText;
        string orgAddressText      , orgAddressTabbedText      , orgPhoneText       , orgFaxText, orgWebText, orgEmailText;
        string referrerAddressText , referrerAddressTabbedText , referrerPhoneText, referrerFaxText;
        if (Utilities.GetAddressType().ToString() == "Contact")
        {
            patientAddressText        = patientAddress == null    ? "No address found"      : patientAddress.GetFormattedAddress("No address found");
            patientAddressTabbedText  = patientAddress == null    ? "No address found"      : patientAddress.GetFormattedAddress("No address found", 1);
            orgAddressText            = orgAddress     == null    ? "No address found"      : orgAddress.GetFormattedAddress("No address found");
            orgAddressTabbedText      = orgAddress     == null    ? "No address found"      : orgAddress.GetFormattedAddress("No address found", 1);
            referrerAddressText       = orgAddress     == null    ? "No address found"      : referrerAddress.GetFormattedAddress("No address found");
            referrerAddressTabbedText = orgAddress     == null    ? "No address found"      : referrerAddress.GetFormattedAddress("No address found", 1);
            patientPhoneText          = patientPhone   == null    ? "No phone number found" : patientPhone.GetFormattedPhoneNumber("No phone number found");
            orgPhoneText              = orgPhone       == null    ? "No phone number found" : orgPhone.GetFormattedPhoneNumber("No phone number found");
            referrerPhoneText         = referrerPhone  == null    ? "No phone number found" : referrerPhone.GetFormattedPhoneNumber("No phone number found");
            referrerFaxText           = referrerFax    == null    ? "No fax number found"   : referrerFax.GetFormattedPhoneNumber("No fax number found");
            orgFaxText                = orgFax         == null    ? "No fax number found"   : orgFax.GetFormattedPhoneNumber("No fax number found");
            orgWebText                = orgWeb         == null    ? "No website found"      : orgWeb.AddrLine1;
            orgEmailText              = orgEmail       == null    ? "No email found"        : orgEmail.AddrLine1;
        }
        else if (Utilities.GetAddressType().ToString() == "ContactAus")
        {
            patientAddressText        = patientAddressAus  == null ? "No address found"      : patientAddressAus.GetFormattedAddress("No address found");
            patientAddressTabbedText  = patientAddressAus  == null ? "No address found"      : patientAddressAus.GetFormattedAddress("No address found", 1);
            orgAddressText            = orgAddressAus      == null ? "No address found"      : orgAddressAus.GetFormattedAddress("No address found");
            orgAddressTabbedText      = orgAddressAus      == null ? "No address found"      : orgAddressAus.GetFormattedAddress("No address found", 1);
            referrerAddressText       = referrerAddressAus == null ? "No address found"      : referrerAddressAus.GetFormattedAddress("No address found");
            referrerAddressTabbedText = referrerAddressAus == null ? "No address found"      : referrerAddressAus.GetFormattedAddress("No address found", 1);
            patientPhoneText          = patientPhoneAus    == null ? "No phone number found" : patientPhoneAus.GetFormattedPhoneNumber("No phone number found");
            orgPhoneText              = orgPhoneAus        == null ? "No phone number found" : orgPhoneAus.GetFormattedPhoneNumber("No phone number found");
            referrerPhoneText         = referrerPhoneAus   == null ? "No phone number found" : referrerPhoneAus.GetFormattedPhoneNumber("No phone number found");
            referrerFaxText           = referrerFaxAus     == null ? "No fax number found"   : referrerFaxAus.GetFormattedPhoneNumber("No fax number found");
            orgFaxText                = orgFaxAus          == null ? "No fax number found"   : orgFaxAus.GetFormattedPhoneNumber("No fax number found");
            orgWebText                = orgWebAus          == null ? "No website found"      : orgWebAus.AddrLine1;
            orgEmailText              = orgEmailAus        == null ? "No email found"        : orgEmailAus.AddrLine1;
        }
        else
            throw new Exception("Unknown AddressType in config: " + Utilities.GetAddressType().ToString().ToString());

        string    ptConditionsText   = string.Empty;
        Hashtable selectedConditions = PatientConditionDB.GetHashtable_ByPatientID(patient.PatientID, false);
        foreach (Condition condition in ConditionDB.GetAll())
            if (selectedConditions[condition.ConditionID] != null)
                ptConditionsText += (ptConditionsText.Length == 0 ? "" : Environment.NewLine) + " • " + ((PatientCondition)selectedConditions[condition.ConditionID]).Condition.Descr;
        if (ptConditionsText == string.Empty)
            ptConditionsText = " • None";

        string bk_prov_number = string.Empty;
        if (booking != null && booking.Provider != null && booking.Organisation != null)
        {
            RegisterStaff regStaff = RegisterStaffDB.GetByStaffIDAndOrganisationID(booking.Provider.StaffID, booking.Organisation.OrganisationID);
            bk_prov_number = (regStaff == null) ? string.Empty : regStaff.ProviderNumber;
        }

        TimeSpan bkTime = booking != null ? booking.DateEnd.Subtract(booking.DateStart) : TimeSpan.Zero;
        string bkTimeLength;
        if (bkTime.Minutes > 0 && bkTime.Hours > 0)
            bkTimeLength = bkTime.Hours + (bkTime.Hours > 1 ? "hrs " : "hr ") + bkTime.Minutes + " minutes";
        else if (bkTime.Hours > 0)
            bkTimeLength = bkTime.Hours + (bkTime.Hours > 1 ? "hours " : "hour ");
        else if (bkTime.Minutes > 0)
            bkTimeLength = bkTime.Minutes + bkTime.Minutes + " minutes";
        else
            bkTimeLength = bkTime.Minutes + " minutes";

        sourceDataSet.Tables[0].Rows.Add(

            DateTime.Now.ToString("d MMMM, yyyy"),

            patient == null ? "No patient found" : patient.Person.FullnameWithTitleWithoutMiddlename,
            patient == null ? "No patient found" : (patient.Person.Title.ID == 0 ? "" : patient.Person.Title.Descr),
            patient == null ? "" : patient.Person.Firstname,
            patient == null ? "" : patient.Person.Middlename,
            patient == null ? "" : patient.Person.Surname,
            patient == null ? "" : patient.Person.Gender,
            patient == null ? "" : patient.Person.Dob == DateTime.MinValue ? (object)DBNull.Value : patient.Person.Dob.ToString("d MMMM, yyyy"),
            patient == null ? "" : patient.Person.Dob == DateTime.MinValue ? (object)DBNull.Value : patient.Person.Dob.ToString("MMMM ") + Utilities.GetDateOrdinal(patient.Person.Dob.Day),

            ptConditionsText,

            patientAddressText,
            patientAddressTabbedText,
            //patientAddress == null                                          ? "No address found" : patientAddress.AddrLine1,
            //patientAddress == null                                          ? "" : patientAddress.AddrLine2,
            //patientAddress == null || patientAddress.AddressChannel == null ? "" : (patientAddress.AddressChannel.AddressChannelID == 1 ? "" : patientAddress.AddressChannel.DisplayName),
            //patientAddress == null || patientAddress.Suburb  == null        ? "" : patientAddress.Suburb.Name,
            //patientAddress == null || patientAddress.Suburb  == null        ? "" : patientAddress.Suburb.Postcode,
            //patientAddress == null || patientAddress.Country == null        ? "" : patientAddress.Country.Descr,
            patientPhoneText,

            pt_last_bk_date == DateTime.MinValue ? "No previous bookings found" : pt_last_bk_date.ToString("d MMMM, yyyy"),

            activeHcCard == null ? "No hc card found" : activeHcCard.CardNbr + " - " + activeHcCard.CardFamilyMemberNbr,
            activeHcCard == null ? "" : activeHcCard.CardName,
            activeHcCard == null ? (object)"" : activeHcCard.DateReferralSigned.ToString("d MMMM, yyyy"),
            epc_expire_date == DateTime.MinValue ? (object)"" : epc_expire_date.ToString("d MMMM, yyyy"),
            epc_expire_date == DateTime.MinValue ? (object)"" : epc_count_remaining,

            medicareCard == null ? "No medicare card found" : medicareCard.CardNbr + " - " + medicareCard.CardFamilyMemberNbr,
            medicareCard == null ? " " : medicareCard.CardName,
            medicareCard == null ? (object)" " : medicareCard.DateReferralSigned.ToString("d MMMM, yyyy"),

            dvaCard == null ? "No dva card found" : dvaCard.CardNbr,
            dvaCard == null ? "" : dvaCard.CardName,
            dvaCard == null ? (object)"" : dvaCard.DateReferralSigned.ToString("d MMMM, yyyy"),

            org != null ? org.Name        : site.Name,
            org != null ? org.Abn         : site.Abn,
            org != null ? org.Acn         : site.Acn,
            org != null ? org.BpayAccount : site.BankBpay,

            orgAddressText,
            orgAddressTabbedText,
            //orgAddress == null ? "No address found" : orgAddress.AddrLine1,
            //orgAddress == null ? "" : orgAddress.AddrLine2,
            //orgAddress == null ? "" : (orgAddress.AddressChannel.AddressChannelID == 1 ? "" : orgAddress.AddressChannel.DisplayName),
            //orgAddress == null ? "" : orgAddress.Suburb.Name,
            //orgAddress == null ? "" : orgAddress.Suburb.Postcode,
            //orgAddress == null || orgAddress.Country == null ? "" : orgAddress.Country.Descr,
            orgPhoneText,
            orgFaxText,
            orgWebText,
            orgEmailText,

            regReferrer == null ? "No referrer found" : regReferrer.Referrer.Person.FullnameWithTitleWithoutMiddlename,
            regReferrer == null ? "No referrer found" : (regReferrer.Referrer.Person.Title.ID == 0 ? "" : regReferrer.Referrer.Person.Title.Descr),
            regReferrer == null ? "" : regReferrer.Referrer.Person.Firstname,
            regReferrer == null ? "" : regReferrer.Referrer.Person.Middlename,
            regReferrer == null ? "" : regReferrer.Referrer.Person.Surname,
            //regReferrer == null ? "" : regReferrer.Referrer.Person.Gender,
            //regReferrer == null ? (object)"" : regReferrer.Referrer.Person.Dob.ToString("d MMMM, yyyy"),

            referrerAddressText,
            referrerAddressTabbedText,
            //referrerAddress == null ? "No address found" : referrerAddress.AddrLine1,
            //referrerAddress == null ? "" : referrerAddress.AddrLine2,
            //referrerAddress == null ? "" : (referrerAddress.AddressChannel.AddressChannelID == 1 ? "" : referrerAddress.AddressChannel.DisplayName),
            //referrerAddress == null ? "" : referrerAddress.Suburb.Name,
            //referrerAddress == null ? "" : referrerAddress.Suburb.Postcode,
            //referrerAddress == null || referrerAddress.Country == null ? "" : referrerAddress.Country.Descr,
            referrerPhoneText,
            referrerFaxText,

            booking == null ? "" : booking.DateStart.ToString("d MMMM, yyyy"),
            booking == null ? "" : booking.DateStart.ToString("HH:mm"),
            booking == null ? "" : bkTimeLength,

            booking == null || booking.Provider == null ? "" : booking.Provider.Person.FullnameWithTitleWithoutMiddlename,
            booking == null || booking.Provider == null ? "" : (booking.Provider.Person.Title.ID == 0 ? "" : booking.Provider.Person.Title.Descr),
            booking == null || booking.Provider == null ? "" : booking.Provider.Person.Firstname,
            booking == null || booking.Provider == null ? "" : booking.Provider.Person.Middlename,
            booking == null || booking.Provider == null ? "" : booking.Provider.Person.Surname,
            bk_prov_number,
            //booking == null || booking.Provider == null ? "" : booking.Provider.Person.Gender,
            //booking == null || booking.Provider == null ? "" : booking.Provider.Person.Dob.ToString("d MMMM, yyyy"),

            booking == null ? "" : bookingNotes,

            booking == null || booking.Offering == null ? "" : booking.Offering.Name,
            booking == null || booking.Offering == null ? "" : booking.Offering.ShortName,
            booking == null || booking.Offering == null ? "" : booking.Offering.Descr

            );

        //now merge
        string errorString = string.Empty;
        //if (!OfficeInterop.MailMerge.MergeDataWithWordTemplate(sourceTemplatePath, outputDocPath, sourceDataSet, isDoubleSidedPrinting, extraPages, out errorString))
        //    throw new CustomMessageException("Error:" + errorString);
        if (!WordMailMerger.Merge(sourceTemplatePath, outputDocPath, sourceDataSet, null, 0, false, isDoubleSidedPrinting, extraPages, true, null, out errorString))
            throw new CustomMessageException("Error:" + errorString);

        if (!historyDir.EndsWith(@"\"))
            historyDir += @"\";

        //
        // create pdf file to be stored because it's much smaller in size
        //
        string tmpLettersDirectory = Letter.GetTempLettersDirectory();
        if (!System.IO.Directory.Exists(tmpLettersDirectory))
            throw new CustomMessageException("Temp letters directory doesn't exist");
        string outputDocPathPDF = FileHelper.GetTempFileName(tmpLettersDirectory + Path.ChangeExtension(historyFileName, ".pdf"));
        string _errorString = string.Empty;
        if (keepHistoryInDB || keepHistoryInFile)
            OfficeInterop.FormatConverter.WordToPDF(outputDocPath, outputDocPathPDF, out _errorString);

        if (keepHistoryInDB)
        {
            byte[] doc_contents = System.IO.File.ReadAllBytes(outputDocPathPDF);
            int letterHistoryID = LetterPrintHistoryDB.Insert(letterID, letterPrintHistorySendMethodID, booking == null ? -1 : booking.BookingID, patient == null ? -1 : patient.PatientID, org != null ? org.OrganisationID : 0, regReferrer == null ? -1 : regReferrer.RegisterReferrerID, staff.StaffID, -1, historyFileName, doc_contents);

            if (keepHistoryInFile)
            {
                string filePath = historyDir + letterHistoryID + System.IO.Path.GetExtension(outputDocPathPDF);
                if (System.IO.File.Exists(filePath))
                    throw new CustomMessageException("File already exists: " + filePath);
                if (!Directory.Exists(historyDir))
                    Directory.CreateDirectory(historyDir);
                System.IO.File.Copy(outputDocPathPDF, filePath);
            }
        }
        else if (keepHistoryInFile)
        {
            int letterHistoryID = LetterPrintHistoryDB.Insert(letterID, letterPrintHistorySendMethodID, booking == null ? -1 : booking.BookingID, patient == null ? -1 : patient.PatientID, org != null ? org.OrganisationID : 0, regReferrer == null ? -1 : regReferrer.RegisterReferrerID, staff == null ? -1 : staff.StaffID, healthCardAction == null ? -1 : healthCardAction.HealthCardActionID, historyFileName, null);

            string filePath = historyDir + letterHistoryID + System.IO.Path.GetExtension(outputDocPathPDF);
            if (System.IO.File.Exists(filePath))
                throw new CustomMessageException("File already exists: " + filePath);
            if (!Directory.Exists(historyDir))
                Directory.CreateDirectory(historyDir);
            System.IO.File.Copy(outputDocPathPDF, filePath);
        }

        if (keepHistoryInDB || keepHistoryInFile)
            File.Delete(outputDocPathPDF);
    }
    public static HealthCardAction[] GetReceivedActionsByPatientID(int patient_id)
    {
        string sql = @"
            SELECT
                     hca.health_card_action_id, hca.health_card_id, hca.health_card_action_type_id, hca.action_date, type.descr
            FROM
                     HealthCardAction AS hca
                     INNER JOIN HealthCardActionType AS type ON type.health_card_action_type_id = hca.health_card_action_type_id
                     INNER JOIN HealthCard AS hc ON hc.health_card_id = hca.health_card_id

            WHERE
                     hc.patient_id = " + patient_id + @" AND hca.health_card_action_type_id = 0
            ORDER BY
                     hca.action_date DESC, hca.health_card_action_id DESC";

        DataTable tbl = DBBase.ExecuteQuery(sql).Tables[0];

        HealthCardAction[] list = new HealthCardAction[tbl.Rows.Count];
        for (int i = 0; i < tbl.Rows.Count; i++)
            list[i] = Load(tbl.Rows[i]);

        return list;
    }