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
Assistant
Creating an Odoo module to display a Cost History on the product tab involves several steps:
Module Structure: Define the module structure with necessary files.
Models: Create models to store cost history and extend the product model.
Views: Design the user interface to display the cost history.
Logic: Implement the logic to compute cost history from Purchase Orders, Manufacturing Orders, and Landed Costs.
Below is a comprehensive guide to creating the product_cost_history module.
1. Module Structure
Create the following directory structure for your custom module:
Note: Depending on the Odoo version, the exact view inheritance might vary. Ensure you adjust the inherit_id to match your Odoo version's product form view.
7. Implement Logic to Populate Cost History
To track cost changes from Purchase Orders, Manufacturing Orders, and Landed Costs, you need to extend these models to create cost history records when relevant actions occur.
7.1 From Purchase Orders
Extend the purchase.order and purchase.order.line models to capture cost changes.
python
# product_cost_history/models/cost_history.py (continued)
from odoo.exceptions import UserError
class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
@api.model
def create(self, vals):
order_line = super(PurchaseOrderLine, self).create(vals)
order_line._create_cost_history()
return order_line
def write(self, vals):
res = super(PurchaseOrderLine, self).write(vals)
self._create_cost_history()
return res
def _create_cost_history(self):
for line in self:
product = line.product_id
if line.price_unit and line.product_qty:
old_cost = product.standard_price
new_cost = (line.price_unit * line.product_qty + product.standard_price * product.qty_available) / (product.qty_available + line.product_qty)
# Create cost history record
self.env['product.cost.history'].create({
'product_id': product.id,
'source_model': 'purchase.order',
'source_id': line.order_id.id,
'date': line.order_id.date_order,
'quantity': line.product_qty,
'old_unit_cost': old_cost,
'new_unit_cost': new_cost,
'change_reason': 'purchase'
})
# Update product cost
product.standard_price = new_cost
7.2 From Manufacturing Orders
Extend the mrp.production model to capture cost changes.
python
# product_cost_history/models/cost_history.py (continued)
class MrpProduction(models.Model):
_inherit = 'mrp.production'
@api.model
def create(self, vals):
production = super(MrpProduction, self).create(vals)
production._create_cost_history()
return production
def write(self, vals):
res = super(MrpProduction, self).write(vals)
if 'state' in vals and vals['state'] in ['done']:
self._create_cost_history()
return res
def _create_cost_history(self):
for production in self:
for line in production.move_finished_ids:
product = line.product_id
old_cost = product.standard_price
# Assuming BOM's total cost affects the new cost
bom = production.bom_id
if bom:
new_cost = bom.total_cost # You need to define how to calculate total_cost
# Create cost history record
self.env['product.cost.history'].create({
'product_id': product.id,
'source_model': 'mrp.production',
'source_id': production.id,
'date': production.date_planned_start or fields.Date.today(),
'quantity': line.product_uom_qty,
'old_unit_cost': old_cost,
'new_unit_cost': new_cost,
'change_reason': 'manufacturing'
})
# Update product cost
product.standard_price = new_cost
Note: The calculation of new_cost from BOM should be defined according to your specific costing method.
7.3 From Landed Costs
Extend the stock.landed.cost model to capture cost changes.
python
# product_cost_history/models/cost_history.py (continued)
class StockLandedCost(models.Model):
_inherit = 'stock.landed.cost'
def _create_account_moves(self, cost_lines, cost, company_currency, landed_cost_id):
res = super(StockLandedCost, self)._create_account_moves(cost_lines, cost, company_currency, landed_cost_id)
for cost_line in cost_lines:
product = cost_line.product_id
old_cost = product.standard_price
new_cost = old_cost + (cost_line.value / cost_line.qty)
# Create cost history record
self.env['product.cost.history'].create({
'product_id': product.id,
'source_model': 'stock.landed.cost',
'source_id': landed_cost_id.id,
'date': landed_cost_id.date,
'quantity': cost_line.qty,
'old_unit_cost': old_cost,
'new_unit_cost': new_cost,
'change_reason': 'landed_cost'
})
# Update product cost
product.standard_price = new_cost
return res
Note: Ensure that the method _create_account_moves is the correct hook point in your Odoo version for capturing landed cost allocations. Adjust accordingly if the method signature differs.
8. Install and Test the Module
Update App List: Go to Apps > Update App List.
Install the Module: Search for Product Cost History and install it.
Verify:
Navigate to any product form.
You should see a new tab named Cost History displaying the cost changes with columns: Source Record, Date, Quantity, Old Unit Cost, New Unit Cost, and Change Reason.
9. Additional Considerations
Performance: If your database has a large number of transactions, consider optimizing the cost history creation logic or using scheduled jobs.
Security: Ensure that only authorized users can view or modify cost history records.
Customization: Depending on your specific business logic, you might need to adjust how old_unit_cost and new_unit_cost are calculated.
Testing: Rigorously test the module in a development environment before deploying it to production to ensure that cost calculations are accurate and no data integrity issues arise.
By following this guide, you'll have a functional Odoo module that tracks and displays the cost history of products, derived from Purchase Orders, Manufacturing Orders, and Landed Costs. Adjust and extend the logic as needed to fit your specific business requirements.