Esempio n. 1
0
    protected bool ExistsAndCreatedLogin_FromNameAndDOB(int orgID, string phoneNumber, string email, string firstname, string surname, DateTime DOB, int siteID, ref int register_patient_id, ref int phone_id, ref int email_id)
    {
        bool patientAlreadyExists = false;

        Patient[] matchingPatients = PatientDB.GetByFirstnameSurnameDOB(firstname, surname, DOB);
        foreach (Patient patient in matchingPatients)
        {
            if (patient == null || patient.IsDeceased || patient.IsDeleted)
            {
                continue;
            }

            if (patient.Person.Firstname != firstname ||
                patient.Person.Surname != surname ||
                patient.Person.Dob == DateTime.MinValue ||
                patient.Person.Dob != DOB)
            {
                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 email if different from existing
            email_id = AddEmailIfNotExists(patient, siteID, email);

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


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

        return(patientAlreadyExists);
    }