Tuesday, December 17, 2019

example object in odoo

ODOO

python oop

 
In this step we will start the relational fields so we have to 
  • create 'clinic.doctor' object which will contain doctor's data
  • link the doctor object with the patient object
We can create 'clinic.doctor' object by the same way that we use to create 'clinic.patient' in the file with name 'clinic_doctor_view.xml' so we must first create 'clinic_doctor.py' just as ...

# -*- coding: utf-8 -*-
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError

class ClinicDoctor(models.Model):
    ###############################
    #Class for doctor which is contain all doctor data
    ###############################
    _name = 'clinic.doctor'
    _description = "object contain all doctor data"

    first_name = fields.Char(string='First Name', required=True)
    second_name = fields.Char(string='Second Name', required=True)
    third_name = fields.Char(string='Third Name', required=True)
    forth_name = fields.Char(string='Fourth Name', required=True)
    name = fields.Char(string="Name", readonly=True, compute='get_doctor_name')
    image = fields.Binary(string='Image', copy=False)
    gender = fields.Selection([('male','Male'),('female','Female')], string='Gender',default='male')
    birth_day = fields.Date(string='Birthday', required=True)
    age_year = fields.Integer(string="Years", compute='calculate_age')
    age_month = fields.Integer(string="Months", compute='calculate_age')
    age_day = fields.Integer(string="Days", compute='calculate_age')
    country_id = fields.Many2one('res.country', string='Country')
    phone = fields.Char(string="Phone")

    @api.constrains('phone')
    def phone_validation(self):
        """
        function to validate phone data for doctor
        """
        phone_prefix = [1,9]
        phone_type = [0,1,2,6,9]
        check_value = 0

        if self.phone and self.country_id:
            if self.country_id.code == 'SD':
                phone = self.phone
                if len(phone) != 9:
                    raise ValidationError(_("Wrong phone number , it length is not true"))
                if len(phone) == 9:
                    for index in range(0,len(phone_prefix)):
                        if phone[0] == str(phone_prefix[index]):
                            check_value = 1
                    if check_value == 0:
                        raise ValidationError(_("Wrong phone number , it start with wrong key number"))
                    check_value = 0
                    for index in range(0,len(phone_type)):
                        if phone[1] == str(phone_type[index]):
                            check_value = 1
                    if check_value == 0:
                        raise ValidationError(_("Wrong phone number , it not of real phone number"))
                    check_value = 0
                    for index in range(0,len(phone)):
                        check_value = 0
                        for number in range(0,10):
                            if phone[index] == str(number):
                                check_value = 1
                        if check_value == 0:
                            raise ValidationError(_("Wrong phone number , it must not contain charactors"))



    @api.one
    def get_doctor_name(self):
        """
        function to calculate the full name for the doctor
        """
        self.name = self.first_name+' '+self.second_name+' '+self.third_name+' '+self.forth_name


    @api.one
    def calculate_age(self):
        """
        function to calcolate the age of the user

        """
        if self.birth_day :
            birth_day = str(self.birth_day)
            current_date = str(fields.Date.today())

            birth_day_year_as_int =     int(birth_day[0]+birth_day[1]+birth_day[2]+birth_day[3])
            birth_day_month_as_int = int(birth_day[5]+birth_day[6])
            birth_day_day_as_int = int(birth_day[8]+birth_day[9])

            current_date_year_as_int = int(current_date[0]+current_date[1]+current_date[2]+current_date[3])
            current_date_month_as_int = int(current_date[5]+current_date[6])
            current_date_day_as_int = int(current_date[8]+current_date[9])

            period_years = current_date_year_as_int-birth_day_year_as_int
            period_months = current_date_month_as_int-birth_day_month_as_int
            period_days = current_date_day_as_int-birth_day_day_as_int

            months_list_1 = ['04','06','09','11']
            months_list_2 = ['01','03','05','07','08','10','12']

            if period_days < 0:
                if str(current_date_month_as_int) == '02':
                    if current_date_year_as_int%4 == 0:
                        period_days = 29+period_days
                    if current_date_year_as_int%4 != 0:
                        period_days = 28+period_days
                for index in range(0,4):
                    if current_date_month_as_int == int(months_list_1[index]):
                        period_days = 30+period_days
                for index in range(0,7):
                    if current_date_month_as_int == int(months_list_2    [index]):
                        period_days = 31+period_days
                period_months = period_months-1
            if period_months < 0:
                period_months = 12+period_months
                period_years = period_years-1

            self.age_year = period_years
            self.age_month = period_months
            self.age_day = period_days

Then we will it view in 'clinic_doctor_view.xml' just as

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>

        <!-- """Create Form View""" -->

        <record id="clinic_doctor_form_view" model="ir.ui.view">
            <field name="name">clinic.doctor.form</field>
            <field name="model">clinic.doctor</field>
            <field name="arch" type="xml">
                <form string="Doctor">
                    <sheet>
                        <div style="width:100%;">
                            <div style="width:20%;float:left;">
                                <field name="image" widget="image"/>
                            </div>
                            <div style="width:78%;float:right;">
                                <group colspan="2" col="2">
                                    <field name="name" nolabel="1" style="font-size:32px;"/>
                                </group>
                                <table style="width:100%;">
                                    <tr style="width:100%;">
                                        <td style="width:25%;font-size:13px;padding:2px;">
                                            <field name="first_name" placeholder="First Name"/>
                                        </td>
                                        <td style="width:25%;font-size:13px;padding:2px;">
                                            <field name="second_name" placeholder="Second Name"/>
                                        </td>
                                        <td style="width:25%;font-size:13px;padding:2px;">
                                            <field name="third_name" placeholder="Third Name"/>
                                        </td>
                                        <td style="width:25%;font-size:13px;padding:2px;">
                                            <field name="forth_name" placeholder="Fourth Name"/>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                        <br></br><br></br>
                        <group colspan="4" col="4">
                            <field name="gender"/>
                            <field name="birth_day"/>
                            <field name="country_id"/>
                            <field name="phone"/>
                        </group>
                        <table style="width:50%;">
                            <tr>
                                <td style="width:40%;font-size:13px;padding:2px;">
                                </td>
                                <td style="width:20%;padding:2px;">
                                    <center>
                                        years
                                    </center>
                                </td>
                                <td style="width:20%;padding:2px;">
                                    <center>
                                        months
                                    </center>
                                </td>
                                <td style="width:20%;padding:2px;">
                                    <center>
                                        days
                                    </center>
                                </td>
                            </tr>
                            <tr>
                                <td style="width:40%;font-size:13px;padding:2px;">
                                    <b>
                                        Age
                                    </b>
                                </td>
                                <td style="width:20%;padding:2px;">
                                    <center>
                                        <field name="age_year" />
                                    </center>
                                </td>
                                <td style="width:20%;padding:2px;">
                                    <center>
                                        <field name="age_month" />
                                    </center>
                                </td>
                                <td style="width:20%;padding:2px;">
                                    <center>
                                        <field name="age_day" />
                                    </center>
                                </td>
                            </tr>
                        </table>
                    </sheet>
                </form>
            </field>
        </record>

        <!-- """Create Tree View""" -->

        <record id="clinic_doctor_tree_view" model="ir.ui.view">
            <field name="name">clinic.doctor.tree</field>
            <field name="model">clinic.doctor</field>
            <field name="arch" type="xml">
                <tree string="doctors">
                    <field name="name"/>
                    <field name="gender"/>
                    <field name="age_year"/>
                    <field name="phone"/>
                </tree>
            </field>
        </record>

        <!-- """Create Action View""" -->

        <record id="clinic_doctor_action_view" model="ir.actions.act_window">
            <field name="name">doctors</field>
            <field name="res_model">clinic.doctor</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="view_id" ref="clinic_doctor_tree_view"/>
        </record>

        <!-- """Create the Configration menu """ -->

        <menuitem id="clinic_doctor_main_menu"
            name="Doctors" sequence="10"
            parent="clinic_root_menu"
        />

        <!-- """Create the Company's Projects menu""" -->

        <menuitem
            name="Doctors"
            parent="clinic_doctor_main_menu"
            action="clinic_doctor_action_view"
            id="doctors_action_menu"
            sequence="15"
        />

    </data>
</odoo>

Then we will create the relation between the patient and the doctor , an we can say ' one doctor will take care of more than one patient ' so it will be 'One2many' form doctor to patient , and also we can say ' many patients have taken care by one doctor' so it will be 'Many2one' from patient to doctor 

Now we can code that by adding many2one field into 'clinic.patient' object as 

doctor_id = fields.Many2one('clinic.doctor', string='Doctor') 

then we will add this field to patient object view file 

Then we must go to 'clinic.doctor' object and add One2many field for patients just as bellow ...

patient_id = fields.One2many('clinic.patient', 'doctor_id', string='Patients')

Then we go to doctor object view and add it  as bellow 

<group colspan="4" col="4" string="Patients">
         <field name="patient_id" nolabel="1"/>

</group> 

Watch On YouTube



PREVIOUS STEP            NEXT STEP

No comments:

Post a Comment

Odoo Invoice Qr code issues

There are two main issues must of us facing with the QR code in Odoo invoice & these issues are 1/ QR code displayed as broken image w...