How to generate QR code in Odoo 15 ERP

How to generate QR code in Odoo 15 ERP

3 minutes, 41 seconds Read

QR Code is a two-dimensional pictographic code with a quick response, faster readability, and ample storage. It looks like a white background covered with a  group of black squares in a square-like pattern. Which contains a large number of data that can be obtained by a simple imaging device.

The QR Code consists of information that can be encoded in different kinds of data like alphanumeric and kanji symbols.

In odoo, we need QR codes under different conditions. It helps to store data on many records on a single thing. Basically, it can store information about a product, invoice, and picking in Odoo.

In this blog, it discusses How to Generate a QR code in Odoo 15

An example of a QR code with product information is as follows. Code reads product details like name, category, cost, unit of measure, etc…

try:
   import qrcode
except ImportError:
   qrcode = None
try:
   import base64
except ImportError:
   base64 = None
from io import BytesIO
from odoo import models, fields, api, _
from odoo.exceptions import UserError
class Product(models.Model):
   """ inherit Invoice to add report settings """
   _inherit = "product.template"
   qr_code = fields.Binary('QRcode', compute="_generate_qr")

   def _generate_qr(self):
       "method to generate QR code"
       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("Product : ")
               qr.add_data(rec.name)
               qr.add_data(", Reference : ")
               qr.add_data(rec.default_code)
               qr.add_data(", Price : ")
               qr.add_data(rec.list_price)
               qr.add_data(", Quantity : ")
               qr.add_data(rec.qty_available)
               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})
           else:
               raise UserError(_('Necessary Requirements To Run This Operation Is Not Satisfied'))

The python library QRcode is used for generating QRcode. Also, base64 is used to extract it as an image.

For generating the QRcode, we need to install the QRcode library using the command pip install QRcode or from the pycharm install the same. This library automates the process of QRcode generation.

When we create a QRcode only the content to be encoded is required, and the other parameters are version,error_correction,box_size, and border

Version: It is a parameter which is an Integer numbering from 1 to 40 which controls the size of the QRcode, smallest one among these is 1 where a 21 * 21 Matrix.

Error_Correction: This Parameter controls the error_correction used for qrcode.

ERROR_CORRECT_L: It corrects up to 7 % or less numb of errors.

ERROR_CORRECT_M: It corrects about 15 % or less of the errors

ERROR_CORRECT_Q: It corrects about 25 % or less of the errors

ERROR_CORRECT_H: It corrects about 30 % of the errors

Box_size: This parameter controls the number of pixels in the box, which is the size of the box here it is  3

Border: This parameter controls the number of boxes with thick borders should be, default = 4 which is a minimum value

In the above python code example, it inherits product.template model and added a new field for QRcode import the python libraries for the QRcode image generation.

add_data: The function used for adding the fields, and data into the QRcode

Here it adds product name, price, Reference, and quantity using this add_data function then the data renders it to an image which is then encoded into base 64. And corresponding QRcode is updated to that field 

The XML code is as follows

<record id="product_inherit" model="ir.ui.view">
          <field name="name">product.qrcode</field>
          <field name="model">product.template</field>
          <field name="inherit_id" ref="product.product_template_only_form_view"/>
          <field name="arch" type="xml">
              <xpath expr="//field[@name = 'barcode']" position="after">
                  <field name="qr_code" widget='image' class="oe_avatar"
                   />
              </xpath>
   </field>
</record>

The XML code depicts which inherits the product_template form and adds the QRcode image to the view of the product. The image can be saved in either PNG, JPEG, or SVG format and can appear in the form using the widget image and oe_avatar class.

Using this method we can generate QRcode to store

how-to-generate-qr-code-in-odoo-15-erp-1-cybrosys

The QR CODE of the product is obtained like this, which scans the product name, Reference, price, and quantity available.

how-to-generate-qr-code-in-odoo-15-erp-1-cybrosys

This Blog helps you to generate a QRcode in odoo 15 for each record.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *