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
User
your only bringing in purchase order lines, also need landed costs.
Assistant
User
can you also make it work to click into the landed cost on the source doc
Assistant
Creating a module in Odoo to add a custom tab displaying price history for products involves several steps. You'll need to create a new module, define your models, views, and logic to fetch and display the necessary information. Below are the high-level steps to achieve this:
Step 1: Create a New Module
Module Structure: Start by setting up the basic structure for your module.
Manifest File: Update the __manifest__.py file to define your module. Ensure to add dependencies on modules like 'purchase' and 'stock_landed_costs'.
Step 2: Define Models
You may need a model to manage the price history if it requires storing additional data, but based on your requirement, the primary task is fetching and displaying existing data. You can fetch purchase order data and landed costs without defining new models.
Step 3: Create View Inheritance
XML Files: Define a QWeb template or use XML to extend the product views to include your new tab.
xml
<!-- Inherit the product.template form view to add a new page to the notebook -->
<odoo>
<record id="view_product_template_price_history_form" model="ir.ui.view">
<field name="name">product.template.form.price.history</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='general_information']" position="after">
<page string="Price History" name="price_history">
<field name="price_history_line_ids">
<tree>
<field name="source_doc" widget='url' string="Source Doc"/>
<field name="date"/>
<field name="quantity"/>
<field name="old_unit_price"/>
<field name="new_unit_price"/>
</tree>
</field>
</page>
</xpath>
</field>
</record>
</odoo>
Step 4: Add Computations in Python
In your Python files, you'll need to create a method that computes the price history.
python
from odoo import models, fields, api
class ProductTemplate(models.Model):
_inherit = 'product.template'
price_history_line_ids = fields.One2many(
comodel_name='product.price.history.line',
inverse_name='product_tmpl_id',
string='Price History',
compute='_compute_price_history',
)
@api.depends('product_variant_ids')
def _compute_price_history(self):
for product in self:
history_lines = []
# Fetch purchase order lines related to this product
po_lines = self.env['purchase.order.line'].search([('product_id', 'in', product.product_variant_ids.ids)])
for line in po_lines:
history_lines.append((0, 0, {
'source_doc': line.order_id.name,
'date': line.date_order,
'quantity': line.product_qty,
'old_unit_price': line.price_unit, # Assume old unit price logic here
'new_unit_price': line.price_unit
}))
product.price_history_line_ids = history_lines
class ProductPriceHistoryLine(models.Model):
_name = 'product.price.history.line'
_description = 'Product Price History Line'
product_tmpl_id = fields.Many2one('product.template', string='Product')
source_doc = fields.Char(string='Source Document')
date = fields.Date(string='Date')
quantity = fields.Float(string='Quantity')
old_unit_price = fields.Float(string='Old Unit Price')
new_unit_price = fields.Float(string='New Unit Price')
Step 5: Fetch Landed Costs
To display landed cost lines:
Fetch landed costs related to the product in a similar way as purchase orders.
Append to the history_lines with the results from landed cost calculations.
Step 6: Update Security and Access Rights
Create a security XML file, security/ir.model.access.csv, to define access rights for your new model.
Step 7: Test and Install the Module
Ensure that your module is loaded properly in Odoo.
Test by creating a few purchase orders and landed costs to verify that the price history is computed and displayed as expected.
These steps give you a basic framework. Depending on your specific business logic, you might need to refine computations, apply more comprehensive business rules, or handle additional edge cases.
To include landed costs in your price history tab alongside purchase order lines, you need to fetch and handle landed cost information related to the product. The landed costs can affect product cost indirectly by attributing additional costs to the products received. Let's extend the solution to include this aspect.
Step 1: Update the Model to Include Landed Costs
Modify the _compute_price_history method to fetch and incorporate landed cost lines in addition to purchase order lines.
python
from odoo import models, fields, api
class ProductTemplate(models.Model):
_inherit = 'product.template'
price_history_line_ids = fields.One2many(
comodel_name='product.price.history.line',
inverse_name='product_tmpl_id',
string='Price History',
compute='_compute_price_history',
)
@api.depends('product_variant_ids')
def _compute_price_history(self):
for product in self:
history_lines = []
# Fetch purchase order lines related to this product
po_lines = self.env['purchase.order.line'].search([('product_id', 'in', product.product_variant_ids.ids)])
for line in po_lines:
history_lines.append((0, 0, {
'source_doc': line.order_id.name,
'date': line.order_id.date_order,
'quantity': line.product_qty,
'old_unit_price': line.price_unit, # Logic for old unit price
'new_unit_price': line.price_unit
}))
# Fetch landed cost lines related to this product
lc_lines = self.env['stock.valuation.adjustment.lines'].search([
('product_id', 'in', product.product_variant_ids.ids),
('cost_id.state', '=', 'done') # Only consider confirmed landed costs
])
for lc_line in lc_lines:
history_lines.append((0, 0, {
'source_doc': lc_line.cost_id.name,
'date': lc_line.cost_id.date,
'quantity': lc_line.quantity,
'old_unit_price': lc_line.price_unit_before,
'new_unit_price': lc_line.price_unit_after
}))
product.price_history_line_ids = history_lines
class ProductPriceHistoryLine(models.Model):
_name = 'product.price.history.line'
_description = 'Product Price History Line'
product_tmpl_id = fields.Many2one('product.template', string='Product')
source_doc = fields.Char(string='Source Document')
date = fields.Date(string='Date')
quantity = fields.Float(string='Quantity')
old_unit_price = fields.Float(string='Old Unit Price')
new_unit_price = fields.Float(string='New Unit Price')
Step 2: Update View to Handle URLs
In the view XML, add logic to ensure source_doc is clickable and navigates to the correct document based on the type (purchase order or landed cost).
xml
<!-- Inherit the product.template form view to add a new page to the notebook -->
<odoo>
<record id="view_product_template_price_history_form" model="ir.ui.view">
<field name="name">product.template.form.price.history</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='general_information']" position="after">
<page string="Price History" name="price_history">
<field name="price_history_line_ids" widget="one2many_list">
<tree>
<field name="source_doc" string="Source Doc" widget="many2one"
options="{'target':'new'}" domain="[('product_id','in',product_variant_ids)]"/>
<field name="date"/>
<field name="quantity"/>
<field name="old_unit_price"/>
<field name="new_unit_price"/>
</tree>
</field>
</page>
</xpath>
</field>
</record>
</odoo>
Step 3: Testing
Create Test Purchase Orders: Ensure purchase orders are created and confirmed for the products you're working with.
Add Landed Costs: Confirm the landed costs and check that they are reflected in price history.
Testing: Verify that the data in the "Price History" tab accurately reflects the purchase orders and landed costs, with clickable links to the respective documents.
Conclusion
By incorporating these changes, you've augmented your price history functionality to consider both purchase orders and landed costs, thus providing a comprehensive view of the cost changes for products. Adjustments to the logic for calculating old and new prices may be needed based on your specific business rules or currency considerations.
To make the "Source Doc" field clickable and direct users to the appropriate document (either a Purchase Order or a Landed Cost), you'll need a slight adjustment. This involves using a Many2one field that can hold either type of document and dynamically display and link to the correct form view.
Step 1: Modify Python Model
We need to adjust the code to hold references to both purchase orders and landed costs using Many2one fields. The source_doc_id will be a computed field that references the appropriate document, and source_doc_model will differentiate between the two types.
python
from odoo import models, fields, api
class ProductPriceHistoryLine(models.Model):
_name = 'product.price.history.line'
_description = 'Product Price History Line'
product_tmpl_id = fields.Many2one('product.template', string='Product')
source_doc_id = fields.Reference(selection=[('purchase.order', 'Purchase Order'), ('stock.landed.cost', 'Landed Cost')],
string='Source Document', readonly=True)
date = fields.Date(string='Date')
quantity = fields.Float(string='Quantity')
old_unit_price = fields.Float(string='Old Unit Price')
new_unit_price = fields.Float(string='New Unit Price')
class ProductTemplate(models.Model):
_inherit = 'product.template'
price_history_line_ids = fields.One2many(
comodel_name='product.price.history.line',
inverse_name='product_tmpl_id',
string='Price History',
compute='_compute_price_history',
)
@api.depends('product_variant_ids')
def _compute_price_history(self):
for product in self:
history_lines = []
# Fetch purchase order lines related to this product
po_lines = self.env['purchase.order.line'].search([('product_id', 'in', product.product_variant_ids.ids)])
for line in po_lines:
history_lines.append((0, 0, {
'source_doc_id': f'purchase.order,{line.order_id.id}',
'date': line.order_id.date_order,
'quantity': line.product_qty,
'old_unit_price': line.price_unit, # Logic for old unit price
'new_unit_price': line.price_unit
}))
# Fetch landed cost lines related to this product
lc_lines = self.env['stock.valuation.adjustment.lines'].search(
[('product_id', 'in', product.product_variant_ids.ids),
('cost_id.state', '=', 'done')]) # Consider confirmed landed costs
for lc_line in lc_lines:
history_lines.append((0, 0, {
'source_doc_id': f'stock.landed.cost,{lc_line.cost_id.id}',
'date': lc_line.cost_id.date,
'quantity': lc_line.quantity,
'old_unit_price': lc_line.price_unit_before,
'new_unit_price': lc_line.price_unit_after
}))
product.price_history_line_ids = history_lines
Step 2: Update XML View
Make sure to adjust the XML view to make source_doc_id clickable:
Verify Clickable Links: Open an individual product's form view and navigate to the "Price History" tab. Review the list of history entries and confirm that clicking the "Source Doc" correctly opens either the Purchase Order or the Landed Cost document in a form view.
Edge Cases: Consider any edge cases when documents are deleted or if access rights are applied, and ensure that the reference fields handle these gracefully.
Conclusion
With these changes, "Source Doc" will dynamically link to either Purchase Orders or Landed Costs, giving users a seamless navigation experience. This implementation relies on the flexibility of reference fields in Odoo to manage documents of different models fluidly.