create me an odoo module to show cost history on product tab, columns are source_id, date, quantity, old_unit_cost, new_unit_cost.
These should be computed from purchase orders, manufacturing orders, and landed costs. I want it to compute when i open the view if possible
Assistant
Certainly! I'll help you create an Odoo module that adds a "Cost History" tab to the product form view. This tab will display a computed list of cost changes derived from Purchase Orders (POs), Manufacturing Orders (MOs), and Landed Costs. The columns will be:
Source ID: Reference to the PO, MO, or Landed Cost.
Date: Date of the transaction.
Quantity: Quantity involved in the transaction.
Old Unit Cost: The previous unit cost before the change.
New Unit Cost: The new unit cost after the change.
The cost history will be computed on-the-fly when you open the product form view.
Module Structure
The module will be named product_cost_history. Here's an overview of the module's structure:
Create the module directory product_cost_history and the necessary subdirectories models and views. Then, create the empty __init__.py files in the module root and models directory.
2. Define the module manifest (__manifest__.py)
python
# product_cost_history/__manifest__.py
{
'name': 'Product Cost History',
'version': '1.0',
'summary': 'Displays cost history on product form',
'description': """
Adds a tab to the product form to display the cost history,
computed from Purchase Orders, Manufacturing Orders, and Landed Costs.
""",
'category': 'Inventory',
'author': 'Your Name',
'depends': ['purchase', 'mrp', 'stock_landed_costs'],
'data': [
'views/product_cost_history_view.xml',
],
'installable': True,
'application': False,
}
3. Develop the model (product_cost_history.py)
python
# product_cost_history/models/product_cost_history.py
from odoo import models, fields, api
from odoo.tools import float_round
class ProductProduct(models.Model):
_inherit = 'product.product'
cost_history_ids = fields.One2many(
comodel_name='product.cost.history.line',
inverse_name='product_id',
string='Cost History',
compute='_compute_cost_history',
)
@api.depends('standard_price')
def _compute_cost_history(self):
for product in self:
history_lines = []
# Purchase Orders
purchase_lines = self.env['purchase.order.line'].search([
('product_id', '=', product.id),
('order_id.state', 'in', ['purchase', 'done']),
])
for line in purchase_lines:
history_lines.append((0, 0, {
'source_id': f'PO{line.order_id.name}',
'date': line.order_id.date_order.date(),
'quantity': line.product_qty,
'old_unit_cost': line.move_ids.filtered(lambda m: m.date < line.date_order).sorted('date', reverse=True)[:1].price_unit or 0.0,
'new_unit_cost': line.price_unit,
}))
# Manufacturing Orders
mo_records = self.env['mrp.production'].search([
('product_id', '=', product.id),
('state', '=', 'done'),
])
for mo in mo_records:
history_lines.append((0, 0, {
'source_id': f'MO{mo.name}',
'date': mo.date_finished.date(),
'quantity': mo.product_qty,
'old_unit_cost': mo.product_id.standard_price, # Simplification
'new_unit_cost': mo.product_id.standard_price, # Simplification
}))
# Landed Costs
landed_cost_lines = self.env['stock.landed.cost'].search([
('state', '=', 'done'),
('picking_ids.move_lines.product_id', '=', product.id),
])
for lc in landed_cost_lines:
history_lines.append((0, 0, {
'source_id': f'LC{lc.name}',
'date': lc.date,
'quantity': sum(lc.picking_ids.mapped('move_lines').filtered(lambda m: m.product_id == product).mapped('quantity_done')),
'old_unit_cost': product.standard_price, # Simplification
'new_unit_cost': product.standard_price + lc.amount_total, # Simplification
}))
product.cost_history_ids = history_lines
class ProductCostHistoryLine(models.TransientModel):
_name = 'product.cost.history.line'
_description = 'Product Cost History Line'
product_id = fields.Many2one('product.product', string='Product', required=True)
source_id = fields.Char(string='Source ID')
date = fields.Date(string='Date')
quantity = fields.Float(string='Quantity')
old_unit_cost = fields.Float(string='Old Unit Cost')
new_unit_cost = fields.Float(string='New Unit Cost')
Note: Since we cannot store the computed One2many field directly, we use a TransientModel to hold the computed lines for display purposes only.
4. Create the view (product_cost_history_view.xml)
Place the product_cost_history module in your Odoo addons directory, update the app list, and install it from the Apps menu.
Notes and Considerations
Performance: Computing cost history on-the-fly for products with many transactions may impact performance. Consider adding a button to compute the history or implementing a scheduled job if necessary.
Simplifications: The old_unit_cost and new_unit_cost in Manufacturing Orders and Landed Costs are simplified. You may need to adjust the logic based on how you calculate costs in your system.
Access Rights: Ensure that the users who need to see the cost history have the necessary access rights.
Extensibility: This module can be extended to include more detailed calculations or additional sources as needed.
Conclusion
This module will add a "Cost History" tab to your product form view, displaying the cost changes derived from Purchase Orders, Manufacturing Orders, and Landed Costs. The history is computed each time you open the product form, providing up-to-date information.
Let me know if you need further assistance or modifications!