protected void CreateLogin(string email)
    {
        email = email.Replace("'", "''");

        //string curDbName = Session["DB"].ToString();

        try
        {
            List <Tuple <string, Patient, bool> > list = new List <Tuple <string, Patient, bool> >();


            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 (databaseName == "Mediclinic_0001")
                //    continue;

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

                Session["DB"] = databaseName;
                Session["SystemVariables"] = SystemVariableDB.GetAll();


                bool allowPatientLogins            = ((SystemVariables)Session["SystemVariables"])["AllowPatientLogins"].Value == "1";
                bool allowPatientsToCreateOwnLogin = ((SystemVariables)Session["SystemVariables"])["AllowPatientsToCreateOwnLogin"].Value == "1";

                if (!allowPatientLogins || !allowPatientsToCreateOwnLogin)
                {
                    continue;
                }


                int[] entityIDs;
                if (Utilities.GetAddressType().ToString() == "Contact")
                {
                    entityIDs = ContactDB.GetByAddrLine1(null, email, 27).Select(r => r.EntityID).ToArray();
                }
                else if (Utilities.GetAddressType().ToString() == "ContactAus")
                {
                    entityIDs = ContactAusDB.GetByAddrLine1(null, email, 27).Select(r => r.EntityID).ToArray();
                }
                else
                {
                    throw new Exception("Unknown AddressType in config: " + Utilities.GetAddressType().ToString().ToString());
                }


                foreach (int entityID in entityIDs)
                {
                    Patient patient = PatientDB.GetByEntityID(entityID);
                    if (patient == null || patient.IsDeceased || patient.IsDeleted)
                    {
                        continue;
                    }

                    bool hasLoginDetails = patient.Login.Length > 0;
                    if (!hasLoginDetails)
                    {
                        string login    = Regex.Replace(patient.Person.Firstname, @"[^A-Za-z]+", "").ToLower() + Regex.Replace(patient.Person.Surname, @"[^A-Za-z]+", "").ToLower();
                        string loginTry = login;

                        Random rnd = new Random();
                        int    nbr = rnd.Next(11, 999);

                        do
                        {
                            bool loginUsed = (!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"]) && UserDatabaseMapperDB.UsernameExists(loginTry)) ||
                                             (PatientDB.LoginExists(loginTry));

                            if (!loginUsed)
                            {
                                patient.Login = loginTry;
                                patient.Pwd   = loginTry == login ? login + nbr : loginTry;

                                PatientDB.UpdateLoginPwd(patient.PatientID, patient.Login, patient.Pwd);
                                if (!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"]))
                                {
                                    UserDatabaseMapperDB.Insert(loginTry, Session["DB"].ToString());
                                }

                                break;
                            }

                            nbr++;
                            loginTry = login + nbr;
                        } while (true);
                    }

                    SendPasswordRetrievalEmail(patient.Login, patient.Pwd, email);
                    list.Add(new Tuple <string, Patient, bool>(databaseName, patient, hasLoginDetails));
                }

                Session.Remove("DB");
                Session.Remove("SystemVariables");
            }


            System.Text.StringBuilder finalOutput = new System.Text.StringBuilder();
            foreach (Tuple <string, Patient, bool> item in list)
            {
                finalOutput.Append("<tr><td>" + item.Item1 + "</td><td>" + item.Item2.Person.FullnameWithoutMiddlename + "</td><td>" + item.Item3 + "</td><td>" + item.Item2.Login + " | " + item.Item2.Pwd + "</td></tr>");
            }


            //FailureText.Text = "Count: " + list.Count + "<br /><table border=\"1\" class=\"block_center padded-table-2px\">" + finalOutput.ToString() + "</table>";


            if (list.Count == 0)
            {
                throw new CustomMessageException("No patients found with this email");
            }

            this.FailureText.Text = "An email has been sent with new login details";
        }
        catch (CustomMessageException cmEx)
        {
            this.FailureText.Text = cmEx.Message;
        }
        finally
        {
            //Session["DB"] = curDbName;
            //Session["SystemVariables"] = SystemVariableDB.GetAll();
            Session.Remove("DB");
            Session.Remove("SystemVariables");
        }
    }
Esempio n. 2
0
    protected bool ExistsAndCreatedLogin_FromEmail(int orgID, string phoneNumber, string email, int siteID, ref int register_patient_id, ref int phone_id, ref int email_id)
    {
        bool patientAlreadyExists = false;


        int[] entityIDs;
        if (Utilities.GetAddressType().ToString() == "Contact")
        {
            entityIDs = ContactDB.GetByAddrLine1(null, email, 27).Select(r => r.EntityID).ToArray();
        }
        else if (Utilities.GetAddressType().ToString() == "ContactAus")
        {
            entityIDs = ContactAusDB.GetByAddrLine1(null, email, 27).Select(r => r.EntityID).ToArray();
        }
        else
        {
            throw new Exception("Unknown AddressType in config: " + Utilities.GetAddressType().ToString().ToString());
        }

        foreach (int entityID in entityIDs)
        {
            Patient patient = PatientDB.GetByEntityID(entityID);
            if (patient == null || patient.IsDeceased || patient.IsDeleted)
            {
                continue;
            }

            // if no login set, create it

            bool hasLoginDetails = patient.Login.Length > 0;
            if (!hasLoginDetails)
            {
                string login    = txtLogin.Text;
                string loginTry = login;

                Random rnd = new Random();
                int    nbr = rnd.Next(11, 999);

                do
                {
                    bool loginUsed = (!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"]) && UserDatabaseMapperDB.UsernameExists(loginTry)) ||
                                     (PatientDB.LoginExists(loginTry));


                    if (loginUsed)
                    {
                        throw new CustomMessageException("Login name in use. Please choose another");
                    }

                    if (!loginUsed)
                    {
                        patient.Login = loginTry;
                        patient.Pwd   = txtPwd.Text;

                        PatientDB.UpdateLoginPwd(patient.PatientID, patient.Login, patient.Pwd);
                        if (!Convert.ToBoolean(ConfigurationManager.AppSettings["UseConfigDB"]))
                        {
                            UserDatabaseMapperDB.Insert(loginTry, Session["DB"].ToString());
                        }

                        break;
                    }

                    nbr++;
                    loginTry = login + nbr;
                } while (true);
            }


            // add phone number if different from existing
            phone_id = AddPhoneNbrIfNotExists(patient, siteID, phoneNumber);

            // add clinic if different from existing
            register_patient_id = AddOrgIfNotExists(patient, siteID, orgID);


            SendInfoEmail(email, patient.Login, patient.Pwd);
            patientAlreadyExists = true;
        }

        return(patientAlreadyExists);
    }
Esempio n. 3
0
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (GetUrlParamType() == UrlParamType.View)
        {
            maintable.Visible = false; // hide this so that we don't send all the page data (all suburbs, etc) to display before it redirects
            Response.Redirect(UrlParamModifier.AddEdit(Request.RawUrl, "type", "edit"));
        }
        else if (GetUrlParamType() == UrlParamType.Edit)
        {
            try
            {
                UrlParamCreditType urlParamCreditType = GetUrlParamCreditType();

                if (urlParamCreditType != UrlParamCreditType.Add)
                {
                    throw new CustomMessageException("Can no edit a '" + GetUrlParamCreditType().ToString() + "'");
                }

                if (!ddlExpiryValidateAllOrNoneSet.IsValid)
                {
                    return;
                }

                Credit credit = CreditDB.GetByID(GetFormID());

                /*
                 * txtAmount.Text = txtAmount.Text.Trim();
                 * if (txtAmount.Text.StartsWith("$")) txtAmount.Text = txtAmount.Text.Substring(1);
                 * decimal amount;
                 * if (!decimal.TryParse(txtAmount.Text, out amount))
                 *  throw new CustomMessageException("Amount must be a valid amount.");
                 */

                if (urlParamCreditType == UrlParamCreditType.Add)
                {
                    CreditDB.Update(credit.CreditID, credit.CreditType.ID, credit.EntityID, credit.Amount, txtDescr.Text.Trim(), GetExpiryFromForm(), credit.VoucherCredit == null ? -1 : credit.VoucherCredit.CreditID, credit.InvoiceID, credit.TyroPaymentPendingID, Convert.ToInt32(Session["StaffID"]));
                }


                Response.Redirect(UrlParamModifier.AddEdit(Request.RawUrl, "type", "view"));
            }
            catch (Exception ex)
            {
                SetErrorMessage(ex.Message);
            }
        }
        else if (GetUrlParamType() == UrlParamType.Add)
        {
            try
            {
                UrlParamCreditType urlParamCreditType = GetUrlParamCreditType();

                if (urlParamCreditType != UrlParamCreditType.Add)
                {
                    throw new CustomMessageException("Can no add a '" + GetUrlParamCreditType().ToString() + "'");
                }

                if (!ddlExpiryValidateAllOrNoneSet.IsValid)
                {
                    return;
                }

                int entityID = GetFormID();

                txtAmount.Text = txtAmount.Text.Trim();
                if (txtAmount.Text.StartsWith("$"))
                {
                    txtAmount.Text = txtAmount.Text.Substring(1);
                }
                decimal amount;
                if (!decimal.TryParse(txtAmount.Text, out amount))
                {
                    throw new CustomMessageException("Amount must be a valid amount.");
                }

                int credit_type_id = -1;
                if (urlParamCreditType == UrlParamCreditType.Add)
                {
                    credit_type_id = 1;
                }
                else if (urlParamCreditType == UrlParamCreditType.Use)
                {
                    credit_type_id = 2;
                }
                else if (urlParamCreditType == UrlParamCreditType.CashoutTyroToMC)
                {
                    credit_type_id = 3;
                }
                else if (urlParamCreditType == UrlParamCreditType.CashoutMCtoPT)
                {
                    credit_type_id = 4;
                }
                else
                {
                    throw new CustomMessageException("Invalid URL Field ct");
                }


                bool refresh_on_close = Request.QueryString["refresh_on_close"] != null && Request.QueryString["refresh_on_close"] == "1";

                if (urlParamCreditType == UrlParamCreditType.Add)
                {
                    int creditID = CreditDB.Insert_AddVoucher(entityID, amount, txtDescr.Text.Trim(), GetExpiryFromForm(), Convert.ToInt32(Session["StaffID"]));

                    // need non booking org .. to put on invoice .....
                    // so need to put it in gui .. only for adding type 1

                    Patient patient       = PatientDB.GetByEntityID(entityID);
                    int     invID         = InvoiceDB.Insert(108, -1, 0, patient.PatientID, Convert.ToInt32(ddlClinic.SelectedValue), "", "", Convert.ToInt32(Session["StaffID"]), Convert.ToInt32(Session["SiteID"]), amount, 0, false, false, false, DateTime.MinValue);
                    int     invoiceLineID = InvoiceLineDB.Insert(invID, patient.PatientID, -1, creditID, 1, amount, 0, "", "", -1);

                    System.Drawing.Size size = Receipt.GetPopupWindowAddSize();
                    size = new System.Drawing.Size(size.Width + 15, size.Height + 60);
                    Response.Redirect("~/Invoice_ReceiptAndCreditNoteAddV2.aspx?id=" + invID + "&returnValue=false&window_size=" + size.Width + "_" + size.Height + (refresh_on_close ? "&refresh_on_close=1" : ""), false);
                    return;
                }



                // close this window

                maintable.Visible = false;

                if (refresh_on_close)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>window.opener.location.href=window.opener.location.href;self.close();</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>window.returnValue=false;self.close();</script>");
                }
            }
            catch (Exception ex)
            {
                SetErrorMessage(ex.Message);
            }
        }
        else
        {
            HideTableAndSetErrorMessage("", "Invalid URL Parameters");
        }
    }