Introduction
Managing inventory with serial number tracking often requires printing barcode labels that include the serial number and other custom information like vendor part numbers. In Odoo 16, the standard barcode label printing may not directly support these fields, but with a few customizations, you can generate labels that meet your needs. This guide walks you through creating a server action to generate barcode labels from the stock.lot model and designing a QWeb report to display the required data.
Understanding the Requirement
In the original forum post, the user wants to:
- Print barcode labels with the serial number (stored in
stock.lot.name). - Include additional fields such as the internal reference (AT number) and vendor part number.
- Avoid manual steps like exporting data to third-party label software (e.g., Dymo).
Odoo’s built-in barcode label printing works for products and stock moves, but for serial numbers (lots), you need a custom report action.
Step-by-Step Implementation
1. Create a Server Action to Generate Barcode Labels
First, we need to add a button or server action on the stock.lot model that triggers the barcode report. Add the following Python method to your custom module (e.g., models/stock_lot.py):
from odoo import models, api
class StockLot(models.Model):
_inherit = 'stock.lot'
def generate_barcode(self):
self.ensure_one()
data = {
'response': self.name, # Serial number
'product_code': self.product_id.default_code,
'vendor_part': self.product_id.vendor_part_number if hasattr(self.product_id, 'vendor_part_number') else '',
}
return self.env.ref('your_module.action_report_barcode').report_action(self, data=data)
If you need to include the vendor part number, ensure that field exists on the product or lot. You can extend the model to add a vendor_part_number field if necessary.
2. Define the Report Action in XML
Create a report action in your module’s data/ folder (e.g., data/report_barcode.xml):
<record id="action_report_barcode" model="ir.actions.report">
<field name="name">Lot Barcode Label</field>
<field name="model">stock.lot</field>
<field name="report_type">qweb-pdf</field>
<field name="report_name">your_module.report_lot_barcode</field>
<field name="report_file">your_module.report_lot_barcode</field>
<field name="binding_model_id" ref="stock.model_stock_lot"/>
<field name="binding_type">report</field>
<field name="multi">True</field>
</record>
This action will appear as a “Print” button on the lot form view.
3. Create the QWeb Report Template
Design a QWeb report that displays the barcode and the required fields. Create a report template in views/report_lot_barcode.xml:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="report_lot_barcode">
<t t-call="web.basic_layout">
<div class="page">
<div class="barcode">
<img t-att-src="'/report/barcode/?type=%s&value=%s&width=%s&height=%s' % ('Code128', doc.name, 600, 200)" alt="Barcode"/>
</div>
<div class="info">
<p><strong>Serial Number:</strong> <span t-esc="doc.name"/></p>
<p><strong>Product:</strong> <span t-esc="doc.product_id.display_name"/></p>
<p><strong>Internal Reference:</strong> <span t-esc="doc.product_id.default_code"/></p>
<p><strong>Vendor Part:</strong> <span t-esc="doc.product_id.vendor_part_number or ''"/></p>
</div>
</div>
</t>
</template>
</odoo>
This template uses Odoo’s built-in barcode rendering service to generate a Code128 barcode from the lot name. You can adjust the barcode type and dimensions as needed.
4. Add a Button on the Lot Form View
To make the action easily accessible, add a button to the lot form view. In your module’s views/stock_lot_views.xml:
<record id="view_stock_lot_form_inherit" model="ir.ui.view">
<field name="name">stock.lot.form.inherit</field>
<field name="model">stock.lot</field>
<field name="inherit_id" ref="stock.view_stock_lot_form"/>
<field name="arch" type="xml">
<xpath expr="//header" position="inside">
<button name="generate_barcode" type="object" string="Print Barcode" class="btn-primary"/>
</xpath>
</field>
</record>
Alternative Approach: Using Server Actions
If you prefer not to modify the model, you can create a server action that generates the report. Go to Settings > Technical > Actions > Server Actions and create a new action with:
- Model: stock.lot
- Action Type: Python Code
- Code:
action = env.ref('your_module.action_report_barcode').report_action(records, data={'response': records.mapped('name')})
Then add a button on the lot form view that calls this server action.
Conclusion
By implementing a custom report action and QWeb template, you can generate barcode labels directly from Odoo 16 without relying on external tools. This approach is scalable, maintainable, and can be extended to include any additional fields. The code provided serves as a solid foundation; customize the report template to match your label size and layout preferences.