Automating report printing in Odoo 15 through QR code scanning significantly enhances operational efficiency by reducing manual tasks and ensuring timely documentation. This comprehensive guide details the steps required to integrate QR code functionalities with Odoo's reporting and printing capabilities, enabling automatic report generation when a QR code is scanned using a mobile device.
To automate report printing based on QR code scans, it's essential to generate QR codes within Odoo for the relevant records, such as sales orders, invoices, or product details. This involves either using Odoo's built-in barcode features or leveraging custom modules designed for QR code generation.
Odoo provides inherent barcode functionalities that can be extended to include QR codes. By adding a QR code field to the desired model, QR codes can be generated and displayed within Odoo records and reports.
Here's how to add a QR code field to a custom model:
from odoo import models, fields
import qrcode
from io import BytesIO
from base64 import b64encode
class YourModel(models.Model):
_name = 'your.model'
name = fields.Char(string='Name')
qr_code = fields.Binary(string='QR Code', readonly=True)
def generate_qr_code(self):
for record in self:
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(record.name)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
buffered = BytesIO()
img.save(buffered, format="PNG")
img_str = b64encode(buffered.getvalue())
record.qr_code = img_str
This code defines a QR code field and a method to generate a QR code based on the record's name.
Several community-developed modules enhance QR code functionalities in Odoo. Modules such as sh_qrcode_in_reports
and allinone_print_qrcode_app
facilitate embedding QR codes into various Odoo reports, including sales orders, purchase orders, invoices, and more.
To install a custom module:
sh_qrcode_in_reports
).Once installed, configure the module to select which reports should include QR codes.
After generating QR codes, the next step is to embed them into the relevant Odoo reports. This ensures that whenever a report is generated, it includes the corresponding QR code, which can later be scanned to trigger automatic printing.
Here's an example of how to embed a QR code in a QWeb report template:
<template id="report_your_model_document">
<t t-call="web.html_container">
<div class="page">
<div>
<h2>Your Model Report</h2>
<p>Name: <span t-field="doc.name"/></p>
<img t-att-src="'data:image/png;base64,%s' % (doc.qr_code)" alt="QR Code"/>
</div>
</div>
</t>
</template>
This template includes the QR code image within the report, ensuring it's visible when the report is generated.
Choosing the right QR code scanner app is crucial for seamless integration with Odoo. The scanner should be capable of scanning QR codes and sending the scanned data to Odoo's API or webhook endpoint. Options include:
Once a QR code is scanned, the next step is to send the scanned data to Odoo. This can be done through various methods:
Example of a custom Odoo controller to handle QR code scans:
from odoo import http
from odoo.http import request
class QRCodeController(http.Controller):
@http.route('/qr/scan', type='json', auth='public', methods=['POST'])
def handle_qr_scan(self, **kwargs):
scanned_data = kwargs.get('data')
if scanned_data:
record = request.env['your.model'].sudo().search([('qr_code', '=', scanned_data)], limit=1)
if record:
# Trigger report printing
record.print_report()
return {'status': 'success', 'message': f"Report generated for {record.name}"}
return {'status': 'error', 'message': 'Record not found'}
This controller listens for POST requests at the /qr/scan
endpoint and processes the scanned QR code data accordingly.
Odoo's Automated Actions allow the system to perform specific tasks automatically when certain conditions are met. To trigger report printing upon QR code scanning, set up an automated action that listens for updates to the QR code field.
Steps to Create an Automated Action:
Access Automated Actions: Navigate to Settings > Technical > Automation > Automated Actions.
Create a New Action: Click on Create and fill in the details:
Define the Action: If choosing "Execute Python Code," input the Python code that triggers the report generation. For example:
record.print_report()
To handle the report generation and printing, develop custom server actions that can be called by the automated actions. This involves writing Python methods within your Odoo models.
Example of a server action method to print a report:
from odoo import models, api
class YourModel(models.Model):
_name = 'your.model'
@api.model
def print_report(self):
report = self.env.ref('your_module.report_your_model_document')
return report.report_action(self)
Custom controllers in Odoo handle incoming HTTP requests, such as those sent by a QR code scanner app. These controllers process the scanned data, identify the relevant record, and trigger report generation and printing.
Example controller code:
from odoo import http
from odoo.http import request
class QRCodeController(http.Controller):
@http.route('/qr/scan', type='json', auth='public', methods=['POST'])
def handle_qr_scan(self, **kwargs):
scanned_data = kwargs.get('data')
if scanned_data:
record = request.env['your.model'].sudo().search([('qr_code', '=', scanned_data)], limit=1)
if record:
# Trigger report printing
record.print_report()
return {'status': 'success', 'message': f"Report generated for {record.name}"}
return {'status': 'error', 'message': 'Record not found'}
The Common Unix Printing System (CUPS) enables the Odoo server to send print jobs directly to a designated printer. Configuring CUPS involves setting up the printer and ensuring that the Odoo server can communicate with it.
Example Python code to send a print job via CUPS:
import os
def print_pdf(pdf_content):
with open('/tmp/report.pdf', 'wb') as f:
f.write(pdf_content)
os.system("lp /tmp/report.pdf") # Sends the PDF to the default printer
This function writes the PDF content to a temporary file and uses the lp
command to send it to the default printer.
Modules like Odoo Direct Print PRO simplify the process of automatically printing reports by providing out-of-the-box integrations with various printers. These modules handle the communication between Odoo and the printer, ensuring that reports are printed promptly when triggered.
To use Odoo Direct Print PRO:
Install the Module: Navigate to Apps, search for "Odoo Direct Print PRO," and install it.
Configure Printer Settings: Access the module's settings to add and configure your printer. Ensure that the printer is accessible from the Odoo server.
Set Print Triggers: Define which reports should be printed automatically and under what conditions (e.g., immediately after generation).
Example of triggering a print action within a method:
def print_report(self):
report = self.env.ref('your_module.report_your_model_document')
pdf_content, _ = report.render_qweb_pdf([self.id])
self.env['report.printer'].print_report(pdf_content)
This method generates the report and sends the PDF content to the configured printer.
For environments with network printers, ensure that the printers are properly configured to accept print jobs from the Odoo server. This may involve setting up IP addresses, port configurations, and ensuring that the Odoo server has the necessary permissions to communicate with the printers.
Creating a network printer entry in CUPS:
# Access the CUPS web interface
http://localhost:631/
# Add a new printer with the network details
lpadmin -p printer_name -E -v ipp://printer_ip_address/ipp/print -m everywhere
This command adds a network printer to CUPS, making it accessible for print jobs.
Ensure that QR codes are correctly generated and embedded within the Odoo records and reports. Verify that the QR code contains the accurate data necessary to identify the corresponding record.
Steps to test QR code generation:
Test the QR code scanning functionality to confirm that scanned data is correctly sent to Odoo and processed appropriately.
Steps to validate QR code scanning:
Ensure that the automated actions set up in Odoo are functioning as intended, triggering report generation and printing upon receiving the scanned QR code data.
Steps to verify automated actions:
Implement robust error handling mechanisms to manage potential issues during the QR code scanning and report printing processes. This includes handling invalid QR codes, failed API calls, and printer connectivity issues.
Common issues and solutions:
Error | Possible Cause | Solution |
---|---|---|
Invalid QR Code Data | The QR code contains incorrect or malformed data. | Ensure that the QR code generation script encodes the correct data and that the scanner app correctly reads it. |
Record Not Found | The scanned QR code does not correspond to any existing record in Odoo. | Verify that the QR code generation is linked to the correct records and that the scan sends the appropriate data to Odoo. |
API Communication Failure | Issues with the network or incorrect API endpoint configurations prevent data from reaching Odoo. | Check network connectivity, verify API endpoint URLs, and ensure that firewalls or security settings are not blocking the requests. |
Printer Connectivity Issues | The Odoo server cannot communicate with the printer due to network or configuration problems. | Ensure that the printer is online, properly connected to the network, and configured correctly in CUPS or the printing module. |
Implement logging within your custom controllers and automated actions to capture detailed error information, facilitating quicker diagnosis and resolution of issues.
Automating the printing of Odoo 15 reports through QR code scanning enhances efficiency by minimizing manual intervention and ensuring timely documentation. By integrating QR code functionalities, setting up automated actions, and configuring appropriate printing solutions, businesses can create a streamlined workflow that responds swiftly to real-time data inputs. Comprehensive testing and robust error handling further ensure the reliability and effectiveness of the automated system.