mrp_packaging_qty/models/stock_move.py

23 lines
1.2 KiB
Python

from odoo import api, models
class StockMove(models.Model):
_inherit = 'stock.move'
@api.depends('product_uom_qty', 'raw_material_production_id', 'raw_material_production_id.product_qty', 'raw_material_production_id.qty_producing')
def _compute_unit_factor(self):
super()._compute_unit_factor()
for move in self:
mo = move.raw_material_production_id or move.production_id
# Prevent 3-decimal rounding drift if the move comes from a BOM Line!
if mo and mo.bom_id and move.bom_line_id:
# Use the exact, unrounded mathematical ratio directly from BOM line and UoMs.
bom_line = move.bom_line_id
bom = mo.bom_id
# Fetch quantities directly converting to product matching formats without DB truncation
line_qty = bom_line.product_uom_id._compute_quantity(bom_line.product_qty, bom_line.product_id.uom_id, round=False)
bom_qty = bom.product_uom_id._compute_quantity(bom.product_qty, bom.product_tmpl_id.uom_id, round=False)
if bom_qty:
move.unit_factor = line_qty / bom_qty