In the dynamic world of business, real-time data is not just a luxury; it’s a necessity. For businesses leveraging Odoo 18, gaining instant visibility into inventory levels directly from sales and purchase orders can be a game-changer. This comprehensive guide will show you exactly how to Add Stock Odoo 18 to your sales and purchase order lines, empowering your teams with crucial information at their fingertips.
This tutorial is inspired by and expands upon the valuable insights shared in the video: How to Add Stock Available Field in Sale Order Line and Purchase Order Line in Odoo 18. We’ll dive deep into the technical steps, provide clear code examples, and explain the ‘why’ behind each action, ensuring you can confidently implement this powerful enhancement.
The Critical Need for Real-Time Stock Visibility in Odoo 18
Imagine a sales representative promising a customer 100 units of a product, only to discover later that only 50 are currently in stock. Or a procurement manager ordering a large batch of raw materials, unaware that there’s already an excess in the warehouse. These common scenarios lead to delays, customer dissatisfaction, increased carrying costs, and ultimately, lost revenue.
This is precisely where the ability to Add Stock Odoo 18 directly to your sales and purchase order lines becomes invaluable. By displaying the current on-hand quantity of a product alongside the ordered or purchased quantity, Odoo users gain immediate insight into inventory availability. This transparency enables:
- Sales Managers: To make accurate promises, avoid over-selling, and prioritize orders based on immediate availability. They can quickly see if an item is available for immediate delivery or if it needs to be backordered, leading to more realistic customer expectations and improved service.
- Purchase Managers & Procurement Teams: To optimize purchasing decisions, prevent overstocking, and ensure timely replenishment. They can instantly assess if a product needs to be procured or if current stock levels are sufficient, streamlining the purchasing process and reducing unnecessary expenditure.
- Enhanced Operational Efficiency: By reducing the need to navigate back and forth between different modules (Sales, Purchase, Inventory) to check stock, workflows become smoother and faster. This saves time and reduces potential errors, contributing to a more efficient overall operation.
Implementing a solution to Add Stock Odoo 18 functionality directly into transaction lines eliminates guesswork and fosters more informed decision-making across your organization. It transforms your Odoo system into a more powerful and proactive inventory management tool.
Understanding Odoo’s Core Inventory Management
Before we dive into customization, let’s briefly touch upon how Odoo manages stock. Odoo’s robust inventory module inherently tracks product quantities through various fields. The most relevant for our purpose is qty_available
, which is a computed field on the product.product
model (the actual product variant) that reflects the current quantity on hand in your default warehouse. This field is automatically updated by Odoo with every stock movement (sales, purchases, internal transfers, adjustments).
Our goal is not to reinvent the wheel, but rather to extend the visibility of this existing qty_available
field from the product master data directly into the transactional lines of sales and purchase orders. This way, the sales and procurement teams can access this critical Odoo 18 stock information without leaving their current workflow.
Preparing for Customization: Essential Prerequisites
To successfully Add Stock Odoo 18 fields, you’ll need a few things in place:
- Odoo 18 Installation: Ensure you have Odoo 18 (Community or Enterprise edition) up and running.
- Basic Odoo Module Development Knowledge: While this guide provides detailed steps, a foundational understanding of Odoo’s module structure, Python, and XML will be beneficial for troubleshooting or further customization.
- Developer Mode Activated: This is crucial for accessing technical features, including external IDs of views, which we’ll need for XML inheritance.
- To activate: Go to Settings > Developer Tools (usually at the bottom left) and click “Activate the developer mode.”
- Access to Odoo Server Files: You’ll need to be able to create and modify files within your Odoo server’s custom add-ons path.
Best Practices for Odoo Customization:
- Always use custom modules: Never modify Odoo’s core files directly. This ensures your changes are upgrade-safe and maintainable.
- Version Control: Use Git or a similar system to track your custom module’s development.
- Test in a Staging Environment: Before deploying to a production environment, thoroughly test all changes in a dedicated staging instance.
Step-by-Step Guide to Add Stock Odoo 18 to Order Lines
We will create a new Odoo module, define new fields, and modify existing views to display the available stock.
1. Module Creation: The Foundation for Customizing Odoo 18 Stock Fields
First, create the basic structure for your new Odoo module. We’ll name it available_stock
.
- Navigate to your Odoo custom add-ons path (e.g.,
odoo/custom_addons/
). - Create a new directory:
available_stock
. - Inside
available_stock
, create the following subdirectories and files:
available_stock/
├── __init__.py
├── __manifest__.py
├── models/
│ ├── __init__.py
│ ├── purchase_order.py
│ └── sale_order.py
└── views/
├── purchase_order_view.xml
└── sale_order_view.xml
Now, let’s populate these files.
a. Define the Module Manifest (__manifest__.py
):
This file tells Odoo about your module.
{
'name': 'Available Stock in Sales/Purchases',
'version': '1.0',
'summary': 'Adds available stock field to sale and purchase order lines for Odoo 18.',
'description': """
This module enhances Odoo 18 by adding a computed field to display the available stock quantity
(on-hand inventory) directly within sale order and purchase order lines.
This provides real-time inventory visibility for sales and procurement teams,
improving decision-making and operational efficiency.
A powerful tool to better Add Stock Odoo 18 management to your daily operations.
""",
'author': 'Your Name or Company',
'website': 'https://www.yourcompany.com', # Replace with your website
'category': 'Inventory/Sales/Purchase',
'depends': ['sale_management', 'purchase', 'stock'], # Crucial dependencies
'data': [
'views/sale_order_view.xml',
'views/purchase_order_view.xml',
],
'installable': True,
'application': False,
'auto_install': False,
'license': 'LGPL-3', # Or another appropriate license
}
Explanation:
depends
: It’s crucial to listsale_management
,purchase
, andstock
here. These are the core modules whose models and views we’ll be extending.data
: Specifies the XML files that Odoo should load to apply our view modifications.
b. Setup __init__.py
files:
available_stock/__init__.py
:from . import models
This line tells Odoo to load themodels
directory.available_stock/models/__init__.py
:python from . import purchase_order from . import sale_order
This loads our specific Python files where we’ll define the new fields.
2. Extending Models: Implementing Available Stock in Sale Order Lines
We will inherit the sale.order.line
model to add our new qty_available
field. This field will be computed, meaning its value will be calculated dynamically based on the associated product’s qty_available
.
- Create/Open
available_stock/models/sale_order.py
:
from odoo import models, fields, api
class SaleOrderLine(models.Model):
_inherit = 'sale.order.line' # Inherit the existing Sale Order Line model
# Define a new computed field to show available stock
qty_available = fields.Float(
string='Available Stock',
compute='_compute_qty_available', # Method to calculate the field's value
store=False, # Do not store this field in the database, calculate on the fly
digits='Product Unit of Measure', # Use standard Odoo precision for quantities
help="Current quantity of this product available in stock."
)
@api.depends('product_id') # Recalculate if the product changes
def _compute_qty_available(self):
"""
Computes the available stock for each sale order line product.
Retrieves the 'qty_available' directly from the linked product.
"""
for line in self:
if line.product_id:
# Odoo's product.product model already has a qty_available field
line.qty_available = line.product_id.qty_available
else:
line.qty_available = 0.0 # Default to 0 if no product is selected
Explanation:
_inherit = 'sale.order.line'
: This tells Odoo we’re extending the existingsale.order.line
model.qty_available = fields.Float(...)
: We define a new field of type Float, suitable for quantities.compute='_compute_qty_available'
: This is the core. It tells Odoo that the value ofqty_available
will be determined by the_compute_qty_available
method.store=False
: We setstore=False
becauseqty_available
is a dynamic value that can change frequently. Storing it in the database would lead to outdated information unless constantly updated, which Odoo’sproduct.product.qty_available
already handles efficiently.@api.depends('product_id')
: This decorator ensures that whenever theproduct_id
on a sale order line changes, the_compute_qty_available
method is re-executed for that line, keeping the available stock display up-to-date.line.product_id.qty_available
: This is the crucial part. Odoo’sproduct.product
model (whichproduct_id
links to) already has aqty_available
field that represents the current on-hand quantity. We are simply accessing and displaying this existing data.
3. Extending Models: Implementing Available Stock in Purchase Order Lines
Similar to sales, we’ll inherit the purchase.order.line
model.
- Create/Open
available_stock/models/purchase_order.py
:
from odoo import models, fields, api
class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line' # Inherit the existing Purchase Order Line model
# Define a new computed field to show available stock
qty_available = fields.Float(
string='Available Stock',
compute='_compute_qty_available',
store=False,
digits='Product Unit of Measure',
help="Current quantity of this product available in stock."
)
@api.depends('product_id') # Recalculate if the product changes
def _compute_qty_available(self):
"""
Computes the available stock for each purchase order line product.
Retrieves the 'qty_available' directly from the linked product.
"""
for line in self:
if line.product_id:
line.qty_available = line.product_id.qty_available
else:
line.qty_available = 0.0
Explanation:
The logic here is identical to the SaleOrderLine
model, but applied to purchase.order.line
. This ensures consistency when you Add Stock Odoo 18 visibility across both sales and purchasing modules.
4. Modifying Views: Displaying the New Stock Field in Odoo 18
Now that our field is defined in the models, we need to make it visible in the Odoo user interface. We’ll use XML inheritance to add qty_available
to the tree (list) views of sale and purchase order lines.
a. Add Field to Sale Order Line View (views/sale_order_view.xml
):
<odoo>
<data>
<record id="sale_order_line_available_stock_view" model="ir.ui.view">
<field name="name">Sale Order Line Available Stock (Extension)</field>
<field name="model">sale.order.line</field>
<!-- Inherit the tree view of sale order lines -->
<field name="inherit_id" ref="sale.sale_order_line_view_tree"/>
<field name="arch" type="xml">
<!-- Use XPath to insert our field after the 'product_uom_qty' (ordered quantity) field -->
<xpath expr="//field[@name='product_uom_qty']" position="after">
<field name="qty_available" widget="float_time"/> <!-- Use float_time widget for better display if needed, or remove -->
</xpath>
</field>
</record>
</data>
</odoo>
Explanation:
inherit_id="sale.sale_order_line_view_tree"
: This is crucial. It points to the external ID of the original tree view for sale order lines that we want to modify. You can find these IDs in developer mode by inspecting the view.xpath expr="//field[@name='product_uom_qty']" position="after"
: This XPath expression precisely locates theproduct_uom_qty
field (the ordered quantity field) within the original view’s XML structure and tells Odoo to insert our newqty_available
field directly after it. Theposition="after"
attribute is key for placement.widget="float_time"
: This is optional, but can sometimes make float numbers display more cleanly. You can remove it if you prefer Odoo’s default float rendering.
b. Add Field to Purchase Order Line View (views/purchase_order_view.xml
):
<odoo>
<data>
<record id="purchase_order_line_available_stock_view" model="ir.ui.view">
<field name="name">Purchase Order Line Available Stock (Extension)</field>
<field name="model">purchase.order.line</field>
<!-- Inherit the tree view of purchase order lines -->
<field name="inherit_id" ref="purchase.purchase_order_line_tree"/>
<field name="arch" type="xml">
<!-- Use XPath to insert our field after the 'product_qty' (ordered quantity) field -->
<xpath expr="//field[@name='product_qty']" position="after">
<field name="qty_available" widget="float_time"/>
</xpath>
</field>
</record>
</data>
</odoo>
Explanation:
The logic is identical to the sale order line view, but we inherit purchase.purchase_order_line_tree
and position our field after product_qty
(the quantity field on purchase lines). This completes the setup to Add Stock Odoo 18 visibility for purchasing teams.
5. Deployment and Verification: Activating Your New Odoo 18 Stock Feature
With all the files in place, it’s time to deploy your module.
- Restart Your Odoo Server: After making changes to Python or XML files in your custom module, you must restart your Odoo server for the changes to be picked up.
- Linux:
sudo systemctl restart odoo
(or your specific Odoo service name) - Docker: Restart your Odoo container.
- Manual: If running from command line, stop and restart the process.
- Linux:
- Install or Upgrade the Module in Odoo:
- Log in to your Odoo instance as an administrator.
- Go to Apps.
- Click on “Update Apps List” to ensure Odoo recognizes your new module.
- Search for “Available Stock in Sales/Purchases” (or whatever you named your module in
__manifest__.py
). - Click the Install button. If you previously installed an earlier version of your module, click Upgrade instead.
- Verify the New Field:
- Sales Orders:
- Navigate to Sales > Orders > Quotations/Orders.
- Open any existing sale order or create a new one.
- Go to the “Order Lines” tab. You should now see an “Available Stock” column next to the “Ordered Quantity” column.
- Add a product to a line and observe the
Available Stock
value. Change the product to see the value update dynamically.
- Purchase Orders:
- Navigate to Purchase > Orders > Requests for Quotation/Purchase Orders.
- Open any existing purchase order or create a new one.
- Go to the “Products” tab. You should now see an “Available Stock” column next to the “Quantity” column.
- Add a product to a line and verify the
Available Stock
value.
- Sales Orders:
Congratulations! You have successfully configured Odoo 18 to Add Stock Odoo 18 visibility directly onto your sales and purchase order lines. This seemingly small change brings significant benefits to your inventory management workflow.
Practical Tips and Best Practices for Odoo Stock Management
While adding this field is a great step, comprehensive inventory management involves more. Here are some tips to maximize your Odoo 18 stock capabilities:
- Regular Inventory Counts: Even with real-time digital tracking, physical inventory counts (using Odoo’s inventory adjustments or cycle counting features) are essential to reconcile discrepancies and ensure the accuracy of your
qty_available
data. - Leverage Odoo’s Built-in Inventory Reports: Explore reports like
Inventory Valuation
,Stock Analysis
, andProduct Moves
under the Inventory module to gain deeper insights into your stock performance. - Master Product Categories: Properly categorizing your products helps in organizing your inventory, applying specific costing methods, and generating more granular reports.
- Understand Lead Times and Safety Stock: For effective procurement, configure vendor lead times on products and consider implementing safety stock rules in Odoo to prevent stockouts for critical items.
- Multi-Warehouse Management: If you operate multiple warehouses, ensure your Odoo setup reflects this accurately. The
qty_available
field we used usually reflects stock in all locations by default, but you can create more complex computed fields if you need location-specific available quantities visible on order lines. - External Resource: For more in-depth knowledge on Odoo Inventory, refer to the official Odoo documentation: Odoo 18 Inventory Documentation.
- Internal Link: To optimize your sales processes further, consider exploring Odoo Sales Module Best Practices (Hypothetical internal link).
Beyond Add Stock Odoo 18: Advanced Customizations
Once you’ve mastered adding the basic available stock, you might consider further enhancements:
- Available Quantity per Location: Modify the computed field to show
qty_available
from a specific warehouse or location if your business requires this level of detail on order lines. - Quantity in Sales/Purchase UoM: If your sales or purchase unit of measure differs from the product’s default unit of measure, you might want a computed field that converts and displays the available quantity in the respective UoM.
- Quantity On Order/Forecasted: Extend the idea to also show “Quantity on Sales Order” or “Quantity on Purchase Order” on the product form or even directly on the order line to get a fuller picture of future stock movements.
- Visual Cues: For advanced users, you could add conditional formatting to the
qty_available
field (e.g., turn red ifqty_available
is less thanordered_qty
) to provide immediate visual alerts. - Internal Link: For more complex customization ideas, refer to our guide on Advanced Odoo Customization Techniques (Hypothetical internal link).
- External Resource: The Odoo developer documentation is an invaluable resource for advanced topics: Odoo 18 Developer Documentation.
Conclusion
Empowering your Odoo 18 users with instant inventory visibility directly on sales and purchase order lines is a significant step towards greater operational efficiency and informed decision-making. By following this step-by-step tutorial, you’ve learned how to confidently Add Stock Odoo 18 functionality to your system, leveraging Odoo’s powerful customization capabilities. This enhancement streamlines workflows, reduces errors, and ultimately contributes to better customer satisfaction and optimized inventory management.
Don’t hesitate to implement this improvement in your Odoo 18 instance. Experience the benefits of having real-time inventory insights precisely where and when your sales and procurement teams need them most. If you encounter any challenges or have further customization ideas, feel free to comment below!
Discover more from teguhteja.id
Subscribe to get the latest posts sent to your email.