Exemple #1
0
def add_md():
    if (
        session["role"] != constants.ROLE_DENTIST
        and session["role"] != constants.ROLE_NURSE
        and session["role"] != constants.ROLE_ASSISTANT
    ):
        return redirect(url_for("list_md"))

    gen_info_form = MedecineDoctorGeneralInfoForm(request.form)
    address_form = forms.AddressForm(request.form)
    phone_form = forms.PhoneForm(request.form)
    mail_form = forms.MailForm(request.form)

    if request.method == "POST" and gen_info_form.validate():

        medecine_doctor = checks.is_body_already_in_database(gen_info_form, "md")
        values = {}
        values["lastname"] = gen_info_form.lastname.data
        values["firstname"] = gen_info_form.firstname.data

        new_medecine_doctor = md.MedecineDoctor(**values)
        meta.session.add(new_medecine_doctor)

        address_args = {f: getattr(address_form, f).data for f in forms.address_fields}
        if any(address_args.values()):
            new_address = contact.Address(**address_args)
            meta.session.add(new_address)
            meta.session.commit()
            new_medecine_doctor.address_id = new_address.id
        #            new_medecine_doctor.addresses.append(contact.Address(
        #                                                             **address_args))

        phone_args = {g: getattr(phone_form, f).data for f, g in forms.phone_fields}
        if any(phone_args.values()):
            new_medecine_doctor.phones.append(contact.Phone(**phone_args))

        mail_args = {f: getattr(mail_form, f).data for f in forms.mail_fields}
        if any(mail_args.values()):
            new_medecine_doctor.mails.append(contact.Mail(**mail_args))

        meta.session.commit()
        return redirect(url_for("list_md"))

    return render_template(
        "/add_md.html",
        gen_info_form=gen_info_form,
        address_form=address_form,
        phone_form=phone_form,
        mail_form=mail_form,
    )
Exemple #2
0
def add_patient(meeting_id=0):
    """ Adding a new patient in database
    """
    # the administrator don't have the right/use to create patient
    # this task should be reserve to other roles, which are
    # Secretary (when the patient first call/come, she may create a file
    # Assisant/Nurse to help the dentist
    # The dentist himself
    authorized_roles = [ constants.ROLE_DENTIST, constants.ROLE_NURSE,
                    constants.ROLE_ASSISTANT, constants.ROLE_SECRETARY ]
    if session['role'] not in authorized_roles:
        return redirect(url_for("allpatients"))
   
    # Forms used for adding a new patient : 
    # two are patient's specific :
    gen_info_form = PatientGeneralInfoForm(request.form)
    gen_info_form.title.choices = forms.get_title_choice_list()
    gen_info_form.office_id.choices = [ (office.id, office.office_name) 
                for office in meta.session.query(users.DentalOffice).all() ]
    gen_info_form.dentist_id.choices = [ (dentist.id, dentist.firstname + " " 
                                                            + dentist.lastname)
                for dentist in meta.session.query(users.OdontuxUser)
                .filter(users.OdontuxUser.role == constants.ROLE_DENTIST,
                        users.OdontuxUser.active.is_(True))
                .order_by(users.OdontuxUser.lastname)
                .all() 
    ]
    if len(gen_info_form.dentist_id.choices) > 1:
        gen_info_form.dentist_id.choices.insert( 0, (0, "") )

    hcp_patient_form = HealthCarePlanPatientForm(request.form)
    hcp_patient_form.healthcare_plans_id.choices = [ (hc.id, hc.name) 
                for hc in meta.session.query(act.HealthCarePlan)
                            .filter(act.HealthCarePlan.active.is_(True))
                            .all() ]
    if not hcp_patient_form.healthcare_plans_id.choices:
        return redirect(url_for('add_healthcare_plan'))

    # three are used for odontux_users and medecine doctors, too
    address_form = forms.AddressForm(request.form)
    phone_form = forms.PhoneForm(request.form)
    mail_form = forms.MailForm(request.form)

    if (request.method == 'POST' and gen_info_form.validate()
        and address_form.validate()
        and phone_form.validate() and mail_form.validate() ):

        # Verify patient isn't already in database
        patient = checks.is_body_already_in_database(gen_info_form, "patient")
        if patient:
            return redirect(url_for('update_patient', 
                                    body_id=patient.id,
                                    form_to_display="gen_info"))
        
        values = {f: getattr(gen_info_form, f).data 
                        for f in get_gen_info_field_list()}
        new_patient = administration.Patient(**values)
        meta.session.add(new_patient)
        meta.session.commit()
        
        for healthcare_plan_id in hcp_patient_form.healthcare_plans_id.data:
            hcp = ( meta.session.query(act.HealthCarePlan)
                        .filter(act.HealthCarePlan.id == healthcare_plan_id)
                        .one()
                )
            new_patient.hcs.append(hcp)
        meta.session.commit()

#        # A family is define in odontux mostly by the fact that
#        # they live together. 
#        family_field_list = ['family_id']
#        for f in family_field_list:
#            # If patient in a family that is already in database, just tell 
#            # which family he's in.
#            if getattr(gen_info_form, f).data:
#                value[f] = int(getattr(gen_info_form, f).data)
#            else:
#                # Create new family and put the new patient in it.
#                new_family = administration.Family()
#                meta.session.add(new_family)
#                meta.session.commit()
#                new_patient.family_id = new_family.id
#                meta.session.commit()
                # Give the patient, member of family an address 
        address_args = {f: getattr(address_form, f).data 
                                for f in forms.address_fields}
        if any(address_args.values()):
            new_address = contact.Address(**address_args)
            meta.session.add(new_address)
            meta.session.commit()
            new_patient.address_id = new_address.id
            meta.session.commit()

#        # Now, we'll see if patient will pay for himself and for his family ;
#        # if not, it must be someone from his family who'll pay.
#        if gen_info_form.payer.data:
#            # Mark the patient as a payer
#            new_payer = administration.Payer(**{'id': new_patient.id})
#            meta.session.add(new_payer)
#            meta.session.commit()
#            # precise that in his family, he pays.
#            new_patient.family.payers.append(new_payer)
#            meta.session.commit()
#            
        # Phone number, in order to contact him.
        
        phone_args = {g: getattr(phone_form, f).data
                      for f,g in forms.phone_fields}

        if any(phone_args.values()):
            new_patient.phones.append(contact.Phone(**phone_args))
            meta.session.commit()

        # Mail
        mail_args = {f: getattr(mail_form, f).data for f in forms.mail_fields}
        if any(mail_args.values()):
            new_patient.mails.append(contact.Mail(**mail_args))
            meta.session.commit()

#        ##################################
#        # The Social Security Number : SSN
#        ##################################
#        # The social security info may have been entered via the 
#        # socialsecurity_id, encountered in gen_info_form, and which
#        # is used usually while insering children of already known patients
#        # in database.
#        if SSN_form.SSN.data:
#            # Verify if number already in database, for example children who
#            # are under parent's social security.
#            try:
#                SSN_id = meta.session.query(SocialSecurityLocale)\
#                    .filter(SocialSecurityLocale.number\
#                            == SSN_form.SSN.data).one().id
#
#            # If the number is new to database :
#            except sqlalchemy.orm.exc.NoResultFound:
#                SSN_args = {g: getattr(SSN_form, f).data 
#                                    for f,g in get_SSN_field_list() }
#                new_SSN = SocialSecurityLocale(**SSN_args)
#                meta.session.add(new_SSN)
#                meta.session.commit()
#                SSN_id = new_SSN.id
#        
#        else:
#            # If there weren't any SSNumber given, try anyway to add "cmu"
#            # and insurance hypothetic values into database
#            SSN_args = {g: getattr(SSN_form, f).data 
#                                    for f,g in get_SSN_field_list() }
#            new_SSN = SocialSecurityLocale(**SSN_args)
#            meta.session.add(new_SSN)
#            meta.session.commit()
#            SSN_id = new_SSN.id
#
#        # Tell Patient class the SSN he is related to.
#        new_patient.socialsecurity_id = SSN_id
#        meta.session.commit()

        #Add the new patient to gnucash !
        comptability = gnucash_handler.GnuCashCustomer(new_patient.id, 
                                                 new_patient.dentist_id)
        customer = comptability.add_customer()        

        return redirect(url_for('add_patient_appointment', 
                                            body_id=new_patient.id,
                                            meeting_id=meeting_id))

    return render_template("add_patient.html",
                            gen_info_form=gen_info_form,
                            address_form=address_form,
                            phone_form=phone_form,
                            mail_form=mail_form,
                            hcp_patient_form=hcp_patient_form,
                            meeting_id=meeting_id)