in odoo i would like to create a module that adds a tab on the product view that shows price history from purchases, landed costs. I want the displayed columns to be (source doc, date, quantity, old unit price, new unit price), the source doc should click into either the purchase order or the landed cost.
Assistant
To create a module in Odoo that adds a tab on the product view showing price history from purchases and landed costs, you'll need to:
Create a new custom module.
Define a new model to store price history records.
Extend existing models (purchase.order and stock.landed.cost) to create price history records when purchases and landed costs are validated.
Modify the product form view to add a new tab displaying the price history.
Ensure the source document is clickable, linking to the corresponding purchase order or landed cost record.
Below is a step-by-step guide to help you achieve this.
Step 1: Create a New Custom Module
First, you need to create a new custom module. We'll call it product_price_history.
a. Scaffold the Module
Using the Odoo scaffolding tool, run the following command in your terminal:
Step 3: Extend Purchase Order and Landed Cost Models
Modify the purchase.order and stock.landed.cost models to create price history records when relevant actions are performed.
a. Extend purchase.order Model
Create models/purchase_order.py and override the button_confirm method:
python
from odoo import models, api
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
def button_confirm(self):
res = super(PurchaseOrder, self).button_confirm()
price_history_obj = self.env['product.price.history']
for order in self:
for line in order.order_line:
product = line.product_id
# Get previous price from the last price history record or the standard price
prev_price = product.price_history_ids[:1].new_price_unit or product.standard_price
# Create price history record
price_history_obj.create({
'product_id': product.id,
'date': order.date_order,
'quantity': line.product_qty,
'old_price_unit': prev_price,
'new_price_unit': line.price_unit,
'source_ref': 'purchase.order,%s' % order.id,
})
return res
b. Extend stock.landed.cost Model
Create models/stock_landed_cost.py and override the button_validate method:
python
from odoo import models, api
class StockLandedCost(models.Model):
_inherit = 'stock.landed.cost'
def button_validate(self):
res = super(StockLandedCost, self).button_validate()
price_history_obj = self.env['product.price.history']
for cost in self:
for val_line in cost.valuation_adjustment_lines:
product = val_line.product_id
# Get previous price from the last price history record or the standard price
prev_price = product.price_history_ids[:1].new_price_unit or product.standard_price
additional_cost = val_line.additional_landed_cost / (val_line.quantity or 1)
new_price = prev_price + additional_cost
# Create price history record
price_history_obj.create({
'product_id': product.id,
'date': cost.date,
'quantity': val_line.quantity,
'old_price_unit': prev_price,
'new_price_unit': new_price,
'source_ref': 'stock.landed.cost,%s' % cost.id,
})
return res
Step 4: Modify the Product Form View
Add a new tab on the product form view to display the price history.
a. Create the View File
Create views/product_views.xml and define the new tab:
Display Currency Correctly: Ensure that the currency is displayed correctly by referencing the product's currency.
Optimizing Performance: Be cautious with performance when dealing with large datasets. Since the price history records are created only when purchases or landed costs are validated, it should not impact performance significantly.
Source Document Clickable: The source_ref field is a reference field that is clickable by default in Odoo's tree views. Clicking on it will open the corresponding document.
Putting It All Together
a. Update __init__.py
Ensure that all models are imported:
python
from . import models
In models/__init__.py:
python
from . import product_price_history
from . import purchase_order
from . import stock_landed_cost
b. Update __manifest__.py
Include all necessary files:
python
{
'name': 'Product Price History',
'version': '1.0',
'summary': 'Adds a price history tab on the product view',
'depends': ['purchase', 'stock_landed_costs'],
'data': [
'security/ir.model.access.csv',
'views/product_views.xml',
],
'installable': True,
'application': False,
}
c. Restart Odoo and Update Module List
After adding the module to your addons path and updating the module list, install the Product Price History module.
Testing the Module
Confirm a Purchase Order: Create and confirm a purchase order for a product. Check the product's price history tab to see if the new price history record appears.
Validate a Landed Cost: Create and validate a landed cost for a product. Verify that the price history record is created.
Click on Source Document: In the price history tab, click on the source document link to ensure it redirects to the correct record.
Conclusion
By following the steps outlined above, you've created a custom module in Odoo that adds a price history tab to the product view, displaying price changes from purchases and landed costs. The source document column is clickable, allowing users to navigate directly to the originating purchase order or landed cost record.
Note: This implementation assumes a basic understanding of Odoo module development. Adjustments may be necessary depending on your specific Odoo version and setup. Always test thoroughly in a development environment before deploying to production.