QR Code Generation in odoo15

HARSHAD
June 14, 2022

A QR Code is a two-dimensional pictographic code with a quick response, rapid readability, and ample storage. It appears as a white background covered with a pattern of black squares. It contains a large amount of data that a simple imaging device can retrieve.

QR Codes contain different data, such as alphanumeric and kanji symbols.

With Odoo, we use QR codes in different ways. It allows us to store data on many records on a single object. Basically, about a product stores information, invoices, and picking. In this blog, it discusses How to Generate a QR code in Odoo

Request Your Free Quote

Odoo QR Code Generation

As an example, here is a QR code for product information. It reads product details like name, category, cost, unit of measure, etc.

class QRCodeInvoice(models.Model): 
   _inherit = 'account.invoice'

   qr_image = fields.Binary("QR Code", compute='_generate_qr_code')
 
  def _generate_qr_code(self):

      for each in self:

           text"Seller Name:" +          
str(each.env.user.company_id.name)+"n" + 
                   "Seller VAT Number:" + 
str(each.env.user.company_id.vat)+"n" + 
                   "Customer Name :" + str(each.partner_id.name)+"n" + 
                   "Customer VAT Number:" + str(each.partner_id.vat)+"n"+ 

                   "Invoice Number and Date:" + str(each.number)+" ,"+
 str(each.date_invoice)+"n"+ 
                   "VAT Amount : " + str(each.amount_tax)+ "n"+
                   "Total Invoice: " + str(each.amount_total)+ "n"
                
           each.qr_image = generate_qr_code(text)

For generating QR codes, the python library QR code is used. As well as for extracting it as an image, base64 was used.

Install the QR code library using the command pip install QR code or pycharm install the same. This library automates the QR code generation process.

The content to be encoded is the only parameter and also the other parameters are version, error_correction, box_size, and border

Version:

This parameter is an integer number ranging from 1 to 40 and which determines the size of a QR code, the smallest being 1 where a 21 * 21 matrix used

Box_size:

It controls the number of pixels, which is the size of the box. Here, it is 3.

Border:

The parameter controls how many boxes with thick borders there should be; the default is 4, which is a minimum value

The above python code example inherits the product. template model and adds a QR code field by using Python libraries for the QR code image generation.

Here, the product name, price, reference, and quantity are added using the add_data function. then rendered the data to an image, which is then encoded into base 64. The QR code for this product also updated. 

The XML code is:

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

It inherits the product template form and adds the QR code image to the view of the product. Using the widget image and the oe avatar class, the image can be saved in either PNG, JPEG, or SVG format and appear in the form.

Using this method, we can generate a QR Code

qr code generation

Request Your Free Quote

"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

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