Enable Dark Mode!
how-to-generate-qr-code-in-odoo-16.jpg
By: Mily Shajan

How to generate QR Code in Odoo 16

Technical Odoo 16

A QR code means a Quick Response Code, which is a two-dimensional one with quick readability and vast data storage capacity.

QR code helps to trigger a variety of actions. QR codes are more user-friendly than traditional barcodes, which can store large amounts of data in a two-dimensional space.

In Odoo, the QR code plays a vital role to enhance each business operations.

They can enhance the efficiency and accuracy of various business processes in Odoo by providing quick access to relevant information and facilitating seamless interactions between different stakeholders.

QR codes can be used to store information, such as the name, description, price, and availability of products. Initiating the payments in invoices and bills to track the inventory movements such as picking, etc.

This Blog describes How to Generate a QR Code in Odoo 16.

Let’s try to add a QR code to the invoice. It reads the invoice number, customer, payment amount and due details, etc.

Odoo uses the qrcode Python library to generate QR codes. The qrcode library is a Python library that allows you to easily create and manipulate QR codes. We can install this third-party library using the command pip3 install qrcode.

from odoo import models,fields
try:
  import qrcode
except ImportError:
  qrcode = None
try:
  import base64
except ImportError:
  base64 = None
from io import BytesIO

class AccountMoveInherit(models.Model):
   _inherit = 'account.move'
   qr_code = fields.Binary("QR Code", compute='generate_qr_code')
   def generate_qr_code(self):
       for rec in self:
           if qrcode and base64:
               qr = qrcode.QRCode(
                   version=1,
                 error_correction=qrcode.constants.ERROR_CORRECT_L,
                   box_size=3,
                   border=4,
               )
               qr.add_data("Invoice No : ")
               qr.add_data(rec.name)
               qr.add_data(", Customer : ")
               qr.add_data(rec.partner_id.name)
               qr.add_data(", Amount Total : ")
               qr.add_data(rec.amount_total)
               qr.make(fit=True)
               img = qr.make_image()
               temp = BytesIO()
               img.save(temp, format="PNG")
               qr_image = base64.b64encode(temp.getvalue())
               rec.update({'qr_code': qr_image})

Here inherit the account.move model and added the field qr_code, code for generating qr code is executed inside the compute function.

QRCode (): class to create a QR code object

Version: refers to the size or complexity of the code, which is determined by the number of modules (i.e., black and white squares) in the code. The version parameter can take a value from 1 to 40, where 1 is the smallest version (21 modules) and 40 is the largest version (177 modules).

Error_Correction: This Parameter helps to ensure that the QR code can be read correctly even if it is damaged or distorted.

There are four levels of error correction for QR codes, each with a different amount of redundancy and error correction capability. The four levels are:

ERROR_CORRECT_L :(Low): recovers up to 7% of data

ERROR_CORRECT_M: (Medium): recovers up to 15% of data

ERROR_CORRECT_Q: (Quartile): recovers up to 25% of data

ERROR_CORRECT_H: (High): recovers up to 30% of data

Box_size: The box size refers to the size (in pixels) of each module (i.e., black or white square) in the QR code. Here it is three, which means the qrcode contains 3 pixels tall and 3 pixels wide.

Border: border refers to the number of blank (white) modules that surround the QR code. Here the border is set as ‘4’, which means a border of four blank modules will be added around the QR code.

Add_data: This is a method that is used to add data to the QR code. It takes the data to be encoded as a parameter and adds it to the QR code object. The data can be any type of string (ASCII, Unicode, etc.), as long as it is not too long for the specified version and error correction level.

Here using add_data() added the fields like invoice number, partner name, and the total amount to the QR code.

All values are added to the qr and it is associated with .make() function.

Make () : This method is used to generate the QR code based on the data and settings provided. The .make() method takes a fit parameter which is set to False by default. If fit is set to True, the QR code will be automatically resized to fit the data.

After making the qrcode, it is rendered as an image in PNG format, which is then encoded to base 64.

  <record id="account_move_form" model="ir.ui.view">
  <field name="name">account.move.form</field>
  <field name="model">account.move</field>
  <field name="inherit_id" ref="account.view_move_form" />
  <field name="arch" type="xml">
     <xpath expr="//field[@name='invoice_date']" position="after">
        <field name="qr_code" widget='image' class="oe_avatar"/>
     </xpath>
  </field>
</record>

The XML Code depicts inside the account.view_move_form qr_code is added as in an image form. Where which is stored as PNG Format.

how-to-generate-qr-code-in-odoo-16-1.png

When we scan the QR code, it gives the results as follows how-to-generate-qr-code-in-odoo-16-2

Invoice No: INV/2023/00002, Customer: Deco Addict, Amount Total: 48012.5

With these steps, you can easily generate QR codes in Odoo 16 for various purposes.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message