Enable Dark Mode!
how-to-generate-a-qr-code-in-odoo-18.jpg
By: Afra MP

How to Generate a QR Code in Odoo 18

Technical Odoo 18 Odoo Enterprises Odoo Community Odoo Sh

Success in the fast-paced commercial world of today depends critically on efficiency and smooth integration. Odoo, an all-inclusive suite of open-source company management software, has revolutionized operations for companies looking to optimize their processes. The platform continues to empower businesses with cutting-edge capabilities, such as the ability to create Quick Response (QR) codes from within the Odoo environment, with the release of Odoo 18.

In the current digital age, QR codes are commonplace and provide a rapid and effective means of information sharing. These two-dimensional barcodes streamline the data access procedure for everything from product details to contact details. The functionality of creating QR codes in Odoo 18 will be examined in this post, along with how companies may use this tool to improve workflows.

Decoding the Significance of QR Codes

Before delving into the intricacies of generating QR codes in Odoo 18, let's first understand the essence of these versatile codes and their significance in modern business operations.

What are QR Codes?

A two-dimensional barcode known as a QR code, or Quick Response code, is capable of storing a large amount of data in a small, machine-readable format. Text, URLs, contact details, and even small multimedia files can all be encoded using QR codes, in contrast to standard barcodes, which have a limited capacity.

Generating QR Codes in Odoo 18: A Step-by-Step Guide

Odoo 18 provides a seamless integration with the Python library qrcode, enabling users to generate QR codes dynamically within the Odoo environment. This section will guide you through the step-by-step process of generating QR codes for various business scenarios.

Generating QR Codes for Sales Orders

In this example, we will explore how to generate QR codes for sales orders, enabling customers to quickly access order details by scanning the code.

Python Code:

# -*- coding: utf-8 -*-
from odoo import models,fields
try:
  import qrcode
except ImportError:
  qrcode = None
try:
  import base64
except ImportError:
  base64 = None
from io import BytesIO
class SaleOrder(models.Model):
    _inherit = 'sale.order'
    qr_code = fields.Binary("QR Code", compute='compute_qr_code')
    def compute_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("Sale 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})

Let's break down the code:

* The code inherits from the sale.order model, allowing us to extend its functionality.

* A new field qr_code is defined as a Binary field, which will store the generated QR code.

* The compute_qr_code method is responsible for generating the QR code. It iterates over each record (rec) in the model.

* Inside the method, a QRCode object is created with specific settings, such as version, error correction level, box size, and border.

* The add_data method is used to add data to the QR code, including the sale order number, customer name, and total amount.

* The make method generates the QR code based on the provided data.

* The generated QR code is converted into a PNG image and encoded as a base64 string.

* Lastly, the base64-encoded QR code is added to the current record's qr_code field.

XML Code:

<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
     <record id="account_move_form" model="ir.ui.view">
          <field name="name">sale.order.form</field>
          <field name="model">sale.order</field>
          <field name="inherit_id" ref="sale.view_order_form"/>
          <field name="arch" type="xml">
              <xpath expr="//field[@name='payment_term_id']" position="after">
                  <field name="qr_code" widget='image' class="oe_avatar"/>
              </xpath>
          </field>
     </record>
</odoo>

The XML code defines a new view for the sale.order model, inheriting from the existing view sale.view_order_form. It adds the qr_code field as an image widget with the oe_avatar class, which styles the QR code for a visually appealing display.

How to Generate a QR Code in Odoo 18-cybrosys

When you scan the generated QR code, it will provide the following information:

Sale No: S00021, Customer: Gemini Furniture, Amount Total: 140.0

This example demonstrates how Odoo 18 allows you to generate QR codes dynamically for sales orders, providing customers with a convenient way to access order details by simply scanning the code.

To read more about How to Generate a QR Code in Odoo 17 ERP, refer to our blog How to Generate a QR Code in Odoo 17 ERP.


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