A QR code is a quick response code that looks similar to barcodes. Which is frequently used day by day for tracking information. It consists of many black squares arranged in a square grid with a white background where we can embed the data which is readable by an imaging device.
In odoo, QR Code plays a vital role in reporting, data analysis, etc… It is quickly readable and helps to store an enormous amount of data. It can be used to track product Information, stock moves, invoices, sale,s and purchase details in Odoo.
This Blog describes how to generate a QR code in odoo 14. Here it illustrates an example of a QR code for a particular invoice. QR code reads the details of an invoice order with the sequence, customer, and amount details.
try:
import qrcode
except ImportError:
qrcode = None
try:
import base64
except ImportError:
base64 = None
from io import BytesIO
class ReportInvoice(models.Model):
""" inherit Invoice to add report settings """
_inherit = "account.move"
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(rec.company_id.name)
qr.add_data(", Payment Reference : ")
qr.add_data(rec.payment_reference)
qr.add_data(", Customer : ")
qr.add_data(rec.partner_id.name)
qr.add_data(",Invoice Date : ")
qr.add_data(rec.invoice_date)
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})
QR code generation works with a python library qr code, which helps for the QR code image generation. For that, first, we need to install the package using the command in the terminal Pip3 install QRcode.
Above it illustrates the code example of creating a QR code in invoices which depicts the invoice details like payment reference, customer and Invoice date, etc…
In the inherited python file, imported the QRcode and base64 library for an image if and simply added a compute field of binary type for QRcode.
The corresponding XML File is as follows
<odoo>
<data>
<record id="res_invoice_inherit" model="ir.ui.view">
<field name="name">account.move.report</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 = 'payment_reference']" position="after">
<field name="qr_code" widget='image' class="oe_avatar"
/>
</xpath>
</field>
</record>
</data>
</odoo>
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=3,
border=4,
)
Version: It is a parameter is an integer from 1 to 40 that controls the size of the QR code (the smallest, version 1, is a 21 * 21 Matrix)
Error_correction: The parameter controls the error correction used for the QR code.
Box_size: This parameter controls how many pixels on each box are needed to accumulate
Border: It controls the number of the boxes and which thick the border should be(default=4)
.add_data parameter can be used to add data to the QRcode, In this example creating the QRcode of an invoice company_name, reference of the payment, customer and salesperson details. And using all parameters the QR will make an image in the make_image function.
The corresponding image can be saved as either SVG or PNG format; here it is saved as PNG format and then encoded the value and assigned to a particular value in the field in the invoice.
When the QRcode is scanned the output obtained as
Using this method we can generate a QR code that stores data from different records in Odoo 14.