Files
manufacture/purchase_mrp_distribution/models/stock_move.py
Carlos Roca 0ea4e392c8 [ADD] purchase_mrp_distribution: New module to distribute qty of move in diferent products
This module aims to address the need to receive a
generic product, but until the moment of reception,
we do not know the quantity that we might receive
of its specific products.

Using kits was not suitable for us, because to
account for a unit of the kit product, the quantity
specified in the BoM must be received. Therefore,
we opted to define a new type of BoM called
distribution.

In this way, upon receiving a unit of any product
added to the BoM, it will be accounted for as a
unit of the generic product and will be reflected
in the svls as separate products, but they will be
accounted for in the purchase line as part of the
product.
2024-11-11 08:35:57 +01:00

51 lines
2.0 KiB
Python

# Copyright 2024 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import _, fields, models
class StockMove(models.Model):
_inherit = "stock.move"
distribution_bom_id = fields.Many2one("mrp.bom")
def action_open_distribution_wizard(self):
return {
"name": _("Distribution for %s") % self.product_id.display_name,
"type": "ir.actions.act_window",
"view_mode": "form",
"res_model": "stock.move.distribution.wiz",
"target": "new",
"context": {"active_model": "stock.move", "active_id": self.id},
}
def _action_done(self, cancel_backorder=False):
distribution_moves = self.filtered(lambda m: m.distribution_bom_id)
new_moves = self.env["stock.move"]
for move in distribution_moves:
cancel_move = False
for sml in move.move_line_ids.filtered(
lambda ml, move=move: ml.product_id != move.product_id
):
# Create new moves with the corresponding product and the linked sml
new_moves += self.env["stock.move"].create(
{
"picking_id": sml.picking_id.id,
"product_id": sml.product_id.id,
"name": sml.product_id.display_name,
"purchase_line_id": move.purchase_line_id.id,
"product_uom": sml.product_uom_id.id,
"state": "assigned",
"location_id": sml.location_id.id,
"location_dest_id": sml.location_dest_id.id,
"price_unit": move.price_unit,
"move_line_ids": [(4, sml.id)],
}
)
cancel_move = True
if cancel_move:
move.state = "cancel"
return super(StockMove, self + new_moves)._action_done(
cancel_backorder=cancel_backorder
)