From fee437312c471dc4c79ed79ad25888417c36b116 Mon Sep 17 00:00:00 2001 From: Marie Lejeune Date: Tue, 18 Feb 2025 15:22:28 +0100 Subject: [PATCH] [IMP] sale_purchase_force_vendor: propagate on stock rules See related issue #2574 --- sale_purchase_force_vendor/README.rst | 16 ++++----- .../models/stock_move.py | 36 ++++++++++++++++--- .../static/description/index.html | 4 +-- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/sale_purchase_force_vendor/README.rst b/sale_purchase_force_vendor/README.rst index a35af541b1d..a0d5553fe7e 100644 --- a/sale_purchase_force_vendor/README.rst +++ b/sale_purchase_force_vendor/README.rst @@ -51,9 +51,9 @@ To configure this module, you need to: 5. Go to *Sale -> Products -> Products*. 6. Create a new product with the following options: - - [Puchase tab] \`Vendors\`: Set different vendors (Vendor A + - Vendor B). - - [Iventory tab] \`Routes\`: Buy and MTO + - [Puchase tab] \`Vendors\`: Set different vendors (Vendor A + Vendor + B). + - [Iventory tab] \`Routes\`: Buy and MTO Usage ===== @@ -61,8 +61,8 @@ Usage 1. Go to *Sale -> Orders -> Quotations* and create a new Quotation. 2. Create a new line with the following options: - - \`Route\`: MTO. - - \`Vendor\`: Vendor B. + - \`Route\`: MTO. + - \`Vendor\`: Vendor B. 3. Confirm sale order. 4. A new purchase order will have been created to Vendor B. @@ -91,10 +91,10 @@ Authors Contributors ------------ -- `Tecnativa `__: +- `Tecnativa `__: - - Víctor Martínez - - Pedro M. Baeza + - Víctor Martínez + - Pedro M. Baeza Maintainers ----------- diff --git a/sale_purchase_force_vendor/models/stock_move.py b/sale_purchase_force_vendor/models/stock_move.py index 84a38e72dfa..009e5f091dd 100644 --- a/sale_purchase_force_vendor/models/stock_move.py +++ b/sale_purchase_force_vendor/models/stock_move.py @@ -1,11 +1,26 @@ # Copyright 2022 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import models +from odoo import api, fields, models class StockMove(models.Model): _inherit = "stock.move" + propagated_sale_line_id = fields.Many2one( + comodel_name="sale.order.line", + string="Propagated SOL", + compute="_compute_propagated_sale_line_id", + store=True, + help="Propagate the sale order line on the chained moves " + "to get it on the purchase move and " + "be able to retrieve the forced vendor", + ) + + @api.depends("sale_line_id") + def _compute_propagated_sale_line_id(self): + for rec in self: + rec.propagated_sale_line_id = rec.sale_line_id + def _prepare_procurement_values(self): """Inject the preferred vendor in case of an MTO that first creates the OUT move. @@ -13,10 +28,21 @@ def _prepare_procurement_values(self): res = super()._prepare_procurement_values() # Get all chained moves to get sale line moves = self.browse(list(self._rollup_move_dests({self.id}))) - move_sale = moves.filtered("sale_line_id")[:1] - if move_sale.sale_line_id.vendor_id: - res_order_line = move_sale.sale_line_id._prepare_procurement_values( - group_id=move_sale.group_id + # Propagate the sale line from the dest moves + if any(m.propagated_sale_line_id for m in moves.move_dest_ids): + moves.write( + { + "propagated_sale_line_id": moves.move_dest_ids.mapped( + "propagated_sale_line_id" + )[0].id + } + ) + move_sale = moves.filtered("propagated_sale_line_id")[:1] + if move_sale.propagated_sale_line_id.vendor_id: + res_order_line = ( + move_sale.propagated_sale_line_id._prepare_procurement_values( + group_id=move_sale.group_id + ) ) res.update({"supplierinfo_id": res_order_line["supplierinfo_id"]}) return res diff --git a/sale_purchase_force_vendor/static/description/index.html b/sale_purchase_force_vendor/static/description/index.html index 2c5646fd3d7..d4b1abfc9ea 100644 --- a/sale_purchase_force_vendor/static/description/index.html +++ b/sale_purchase_force_vendor/static/description/index.html @@ -399,8 +399,8 @@

Configuration

“Unarchived” MTO route.
  • Go to Sale -> Products -> Products.
  • Create a new product with the following options:
      -
    • [Puchase tab] `Vendors`: Set different vendors (Vendor A + -Vendor B).
    • +
    • [Puchase tab] `Vendors`: Set different vendors (Vendor A + Vendor +B).
    • [Iventory tab] `Routes`: Buy and MTO