Wednesday, December 11, 2019

api decorators in odoo

 

ODOO

 

api

 

 
In this step we will
  • explain api decorators
  • add age fields for patient object
  • calculate age from birthday as example for writing function in odoo

What is api decorators ?

odoo.api.one(method)
odoo.api.multi(method)
Where self is defined as record-set (some records that have related with each other)
EG: model.method(cr, uid, ids, args, context=context)
odoo.api.model(method)
Where self is defined as record-set but its contents is not relevant (some records that have no related with each other)
EG: model.method(cr, uid, args, context=context)
odoo.api.depends(*args)
odoo.api.constrains(*args)
odoo.api.onchange(*args)
odoo.api.returns(model, downgrade=None, upgrade=None)


  • add age fields for patient object
we will calculate the age of patient as years, months and days so we must add three fields for age (age_year, age_month, age_day) which will be computed fields that get their values from one function “calculate_age()” so to do that we have to go to ‘clinic.patient’ object and add the code bellow

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')

@api.one
def calculate_age(self):
"""
function to calcolate the age of he 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 deign view for these field by adding the code bellow to ‘clinic_patient_view.xml’ file

<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>


Watch On YouTube


PREVIOUS STEP            NEXT STEP

Tuesday, December 10, 2019

create report in odoo 10



      In this blog we will explain how we can create report in odoo 10 (report generate by wizard)

      So to to that we have to follow the step blow , we will take payments report as an example

1/ create wizard python file in which we will add all fields that we want to use as filler to generate the report 

class paymentsReportWiz(models.TransientModel):
    _name = "payments.report.wiz"
    _description = "Payments Report"


    def get_year_range(self):
        year_list = []
        for i in range(2015, (fields.Date.from_string(fields.Date.today()).year)+5):
            year_list.append((str(i), str(i)))
        return year_list

    target_move = fields.Selection([('posted', 'All Posted Entries'),
        ('all', 'All Entries')], string='Target Moves', required=True, default='posted')
    year = fields.Selection(get_year_range, string='Year', default=str(fields.Date.from_string(fields.Date.today()).year))
 
@api.multi
    def print_report(self):
        """
            function to print the report
        """
        data = {}
        move_ids = self.get_move_ids()
        data['year'] = self.year
        data['move_ids'] = move_ids
        return self.env['report'].with_context({'portrait': 1}).get_action(self, 'module_name.payments_report_template', data=data)

    
    def get_move_ids(self):
        """
            Function to retrun moves base on selected moves type
        """
        move_object = self.env['account.move']
        move_ids = []
        if self.target_move == 'posted':
            move_ids = move_object.search([('state','=','posted')])
        else:
            move_ids = move_object.search([])
        return move_ids.ids



class PaymnetsReportDetails(models.AbstractModel):
    _name = "report.module_name.payments_report_template"

    @api.model
    def render_html(self, docids, data=None):
        """
            function to render the report
        """
        docids = docids
        current_company = self.env.user.company_id
        data['current_company'] = current_company
        return self.env['report'].render('module_name.payments_report_template', data)
 

2/ create the wizard form view file  , as bellow 


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

        <!-- """Create Wizard View""" -->

        <record id="view_payments_report_wiz_form" model="ir.ui.view">
            <field name="name">payments.report.wiz.form</field>
            <field name="model">payments.report.wiz</field>
            <field name="arch" type="xml">
                <form >
                    <sheet>
                        <group colspan="4" col="4">
                            <field name="target_move" widget="radio"/>
                            <field name="year" required="1"/>
                        </group>

                        <footer>
                            <button name="print_report" string="Print" type="object" default_focus="1" class="oe_highlight"/>
                            <button string="Cancel" special="cancel"/>
                        </footer>
                    </sheet>
                </form>
            </field>
        </record>



3/ create the report template as bellow 
 

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

  <!-- ...Define Template... -->

  <template id="payments_report_template">
    <t t-call="report.html_container">
      <div class="page">
        <b style="font-size:24px;">
          <span t-esc="current_company.name"/>
        </b>
        <b style="float:right;font-size:24px;color:#3a5d9c">
          Payments Report
        </b>
      </div>
    </t>
  </template>

<report
    id="payments_report_action"
    string="Payments Report"
    model="account.move.line"
    report_type="qweb-html"
    name="module_name.payments_report_template"
    menu="False"
    />

  </odoo>


Monday, December 9, 2019

how to edit view in odoo

ODOO

views

 

In this step we will continue with patient object by adding some new fields and design their view , so we have two steps today
  • add the fields 
          'name' which will contain the full name for the patient that will be concatenation of the four name , so 'name' field will be computed field
          'image' which is binary field that will contain the image of patient
          'gender' contain the gender of patient and it is selection field contain two values [male and female]
          'birth_day' the birthdate for the patient
          'met_doctor' which is boolean field that will be true if the patient met the doctor before 
          'last_meeting' the date of last meeting 

So we can add them as 

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

class ClinicPatient(models.Model):
    ###############################
    #Class for patient which is contain all patient data
    ###############################
    _name = 'clinic.patient'
    _description = "object contain all patient 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_patient_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)
    met_doctor = fields.Boolean(string='Met Doctor')
    last_meeting = fields.Date(string='Last Meeting')

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

  • Edit view file as bellow 
<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>

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

        <record id="clinic_patient_form_view" model="ir.ui.view">
            <field name="name">clinic.patient.form</field>
            <field name="model">clinic.patient</field>
            <field name="arch" type="xml">
                <form string="Patient">
                    <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="met_doctor"/>
                            <field name="last_meeting" attrs="{'invisible':[('met_doctor','=',False)],'required':[('met_doctor','=',True)]}"/>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

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

        <record id="clinic_patient_tree_view" model="ir.ui.view">
            <field name="name">clinic.patient.tree</field>
            <field name="model">clinic.patient</field>
            <field name="arch" type="xml">
                <tree string="Patients">
                    <field name="first_name"/>
                    <field name="second_name"/>
                    <field name="third_name"/>
                    <field name="forth_name"/>
                </tree>
            </field>
        </record>

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

        <record id="clinic_patient_action_view" model="ir.actions.act_window">
            <field name="name">Patients</field>
            <field name="res_model">clinic.patient</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="view_id" ref="clinic_patient_tree_view"/>
        </record>


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

        <menuitem id="clinic_root_menu"
            name="Clinic Patient"
            sequence="1"
        />

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

        <menuitem id="clinic_patient_main_menu"
            name="Patients" sequence="2"
            parent="clinic_root_menu"
        />

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

        <menuitem
            name="Patients"
            parent="clinic_patient_main_menu"
            action="clinic_patient_action_view"
            id="patients_action_menu"
            sequence="5"
        />

    </data>
</odoo>


Watch on YouTube


  PREVIOUS STEP            NEXT STEP

Saturday, December 7, 2019

object with simple view in odoo

ODOO

 

views

 

On this step we will create object with simple view
    As we decided we are going to design clinic erp system as today we will start to create patient model so as we know in our module structure we create models folder which is contain python files , also we will create views folder which will contain xml files so let us start …
First : create patient object in python file by name “clinic_patient.py” inside models folder which we must create inside our module just as bellow …

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

class ClinicPatient(models.Model):
    _name = 'clinic.patient'
    _description = "object contain all patient 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)


   Now we created new object with name "clinic.patient" which will create new table in the database by name "clinic_patient" which is also contain four fields (names) , then we must create view file for this model by name “clinic_patient_view.xml” inside views folder which we create inside our module as bellow …
 First : create new file inside view folder and save it by name "
clinic_patient_view.xml"
second : open it and and edit it as bellow ...

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

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

        <record id="clinic_patient_form_view" model="ir.ui.view">
            <field name="name">clinic.patient.form</field>
            <field name="model">clinic.patient</field>
            <field name="arch" type="xml">
                <form string="Patient">
                    <sheet>
                        <table style="width:100%;">
                            <tr style="width:100%;">
                                <td style="width:20%;">
                                    <b style="font-size:13px;">
                                        Name
                                    </b>
                                </td>
                                <td style="width:20%;font-size:13px;">
                                    <field name="first_name" placeholder="First Name"/>
                                </td>
                                <td style="width:20%;font-size:13px;">
                                    <field name="second_name" placeholder="Second Name"/>
                                </td>
                                <td style="width:20%;font-size:13px;">
                                    <field name="third_name" placeholder="Third Name"/>
                                </td>
                                <td style="width:20%;font-size:13px;">
                                    <field name="forth_name" placeholder="Fourth Name"/>
                                </td>
                            </tr>
                        </table>
                    </sheet>
                </form>
            </field>
        </record>

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

        <record id="clinic_patient_tree_view" model="ir.ui.view">
            <field name="name">clinic.patient.tree</field>
            <field name="model">clinic.patient</field>
            <field name="arch" type="xml">
                <tree string="Patients">
                    <field name="first_name"/>
                    <field name="second_name"/>
                    <field name="third_name"/>
                    <field name="forth_name"/>
                </tree>
            </field>
        </record>

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

        <record id="clinic_patient_action_view" model="ir.actions.act_window">
            <field name="name">Patients</field>
            <field name="res_model">clinic.patient</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="view_id" ref="clinic_patient_tree_view"/>
        </record>


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

        <menuitem id="clinic_root_menu"
            name="Clinic Patient"
            sequence="1"
        />

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

        <menuitem id="clinic_patient_main_menu"
            name="Patients" sequence="2"
            parent="clinic_root_menu"
        />

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

        <menuitem
            name="Patients"
            parent="clinic_patient_main_menu"
            action="clinic_patient_action_view"
            id="patients_action_menu"
            sequence="5"
        />

    </data>
</odoo> 

 Whatch On YouTube 


 
  PREVIOUS STEP         NEXT STEP

Wednesday, December 4, 2019

View types in odoo

ODOO

 

Views



In this step we will explain
  • View types in odoo
  • How to create your first python class (python class structure)


View types :
 
Form view : show editable form for your object just like in the image bellow

List view (tree view) : show existing records for your models in the database as list with some fields , just like bellow

Kanban view : show existing records in card view just like in the image bellow 
 
Calendar view : view to display and edit information about dates and durations in your records in a visual way
 
Graph view : show your data in graph views as bar chart, line chart and pie chart
Pivot view : show your data in dynamic views
Search view : design search view with some attributes like (group by ,filters …) to make search more easy for the user 

Now how to create your first python class ?

We can define the class (object) structure as bellow
Import packages : here we will import all packages that we will use
Class class_name.. : here we define our class (class name and type)
Class attributes : here we define the attributes for the class
Class attributes :
_name = ‘the object name’ … eg : _name = ‘clinic.patient’
_description = ‘object description’
_order = ‘field_name’ … to order the records by this field
_rec_name = ‘field_name’… to show record in relational fields by this field
_inherit = ‘object.object’ … write the inherited here


Class body : will define as
Fields

Constraints

Functions

Watch On YouTube 



PREVIOUS STEP          NEXT STEP

Module's standard structure in odoo

ODOO

odoo module

 

On this step we will explain :
  • Field’s attributes
  • Module's standard structure 
     
To see field’s attributes click here

Module’s standard structure is just as bellow 
 
 module
    __init__.py
    __manifest__.py
    >< controllers
    >< models
    >< data
    >< i18n
    >< views
    >< security
    >< demo
    >< static
    >< wizard
    >< report
    >< doc
    >< test
Now let us explain them one by one :
Controllers : contains the code files for the website controllers, for modules which are providing that kind of feature .

Models : contains the backend code files, creating the Models and their business logic (contain the python files which create models)

Data : contains other data files with module initial data 

I18n : is where Odoo will look for the translation (contain translation file for many languages)

Views : contains the XML files for the user interface, with the actions, forms, lists, and so on

Security : contains the data files defining access control lists (contain files which define permissions)

Demo : contains data files with demonstration data

Static : is where all web assets are expected to be placed (contain web assets files such as css, js … files)

Wizard : Wizards describe stateful interactive sessions with the user through dynamic forms (contain all wizard files)

Report : contain reports files
 
Doc : contain documentation files

Test : contain the test files which are python file have names start with test_ "eg: test_patient.py"

Watch on YouTube 




PREVIOUS STEP           NEXT STEP

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...