mirror of
https://github.com/OCA/manufacture.git
synced 2025-01-28 16:37:15 +02:00
[ADD] mrp_packaging_default: product packaging data in MRP
This module allows and encourages the use of packaging within MRP, both to manufacture products or to create kits. @moduon MT-4506
This commit is contained in:
2
mrp_packaging_default/models/__init__.py
Normal file
2
mrp_packaging_default/models/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from . import mrp_bom_line
|
||||
from . import stock_move
|
||||
67
mrp_packaging_default/models/mrp_bom_line.py
Normal file
67
mrp_packaging_default/models/mrp_bom_line.py
Normal file
@@ -0,0 +1,67 @@
|
||||
# Copyright 2023 Moduon Team S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class MrpBomLine(models.Model):
|
||||
_inherit = "mrp.bom.line"
|
||||
|
||||
product_packaging_id = fields.Many2one(
|
||||
comodel_name="product.packaging",
|
||||
string="Packaging",
|
||||
compute="_compute_product_packaging",
|
||||
store=True,
|
||||
readonly=False,
|
||||
domain="[('product_id', '=', product_id)]",
|
||||
check_company=True,
|
||||
)
|
||||
product_packaging_qty = fields.Float(
|
||||
string="Packaging Qty.",
|
||||
compute="_compute_product_packaging",
|
||||
digits="Product Unit of Measure",
|
||||
store=True,
|
||||
readonly=False,
|
||||
)
|
||||
|
||||
@api.depends("product_id", "product_qty", "product_uom_id")
|
||||
def _compute_product_packaging(self):
|
||||
"""Set the appropriate packaging for the product qty."""
|
||||
for one in self:
|
||||
one.product_packaging_id = (
|
||||
one.product_id.packaging_ids._find_suitable_product_packaging(
|
||||
one.product_qty, one.product_uom_id
|
||||
)
|
||||
)
|
||||
if not one.product_packaging_id:
|
||||
one.product_packaging_qty = 0
|
||||
continue
|
||||
uom_qty_per_package = (
|
||||
one.product_packaging_id.product_uom_id._compute_quantity(
|
||||
one.product_packaging_id.qty, one.product_uom_id
|
||||
)
|
||||
)
|
||||
one.product_packaging_qty = (
|
||||
one.product_packaging_id._check_qty(one.product_qty, one.product_uom_id)
|
||||
/ uom_qty_per_package
|
||||
)
|
||||
|
||||
@api.onchange("product_packaging_id", "product_packaging_qty")
|
||||
def _onchange_product_packaging_set_qty(self):
|
||||
"""When interactively setting a new packaging, set default qty values."""
|
||||
if not self.product_packaging_id:
|
||||
return
|
||||
self.product_qty = (
|
||||
self.product_packaging_qty
|
||||
* self.product_uom_id._compute_quantity(
|
||||
self.product_packaging_id.qty,
|
||||
self.product_packaging_id.product_uom_id,
|
||||
)
|
||||
)
|
||||
|
||||
@api.onchange("product_id")
|
||||
def _onchange_product_set_qty_from_packaging(self):
|
||||
"""When interactively setting a new product, set default packaging values."""
|
||||
default_packaging = self.product_id.packaging_ids[:1]
|
||||
if default_packaging:
|
||||
self.product_uom_id = default_packaging.product_uom_id
|
||||
self.product_qty = default_packaging.qty
|
||||
36
mrp_packaging_default/models/stock_move.py
Normal file
36
mrp_packaging_default/models/stock_move.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# Copyright 2023 Moduon Team S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)
|
||||
|
||||
|
||||
from odoo import api, models
|
||||
|
||||
|
||||
class StockMove(models.Model):
|
||||
_inherit = "stock.move"
|
||||
|
||||
@api.model
|
||||
def _packaging_vals_from_bom_line(self, vals):
|
||||
"""Fill vals with packaging info from BoM line."""
|
||||
try:
|
||||
bom_line = self.env["mrp.bom.line"].browse(vals["bom_line_id"])
|
||||
except KeyError:
|
||||
# No BoM line, nothing to do
|
||||
return
|
||||
vals.update(
|
||||
{
|
||||
"product_packaging_id": bom_line.product_packaging_id.id,
|
||||
"product_packaging_qty": bom_line.product_packaging_qty,
|
||||
}
|
||||
)
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
"""Inherit packaging from BoM line."""
|
||||
for vals in vals_list:
|
||||
self._packaging_vals_from_bom_line(vals)
|
||||
return super().create(vals_list)
|
||||
|
||||
def write(self, vals):
|
||||
"""Inherit packaging from BoM line."""
|
||||
self._packaging_vals_from_bom_line(vals)
|
||||
return super().write(vals)
|
||||
Reference in New Issue
Block a user