diff --git a/procurement_auto_create_group_by_product/models/__init__.py b/procurement_auto_create_group_by_product/models/__init__.py index 02c83c38a889..c3325f776739 100644 --- a/procurement_auto_create_group_by_product/models/__init__.py +++ b/procurement_auto_create_group_by_product/models/__init__.py @@ -1,3 +1,4 @@ from . import stock_rule +from . import stock_move from . import procurement_group from . import product_product diff --git a/procurement_auto_create_group_by_product/models/stock_move.py b/procurement_auto_create_group_by_product/models/stock_move.py new file mode 100644 index 000000000000..5c0b5ec88e22 --- /dev/null +++ b/procurement_auto_create_group_by_product/models/stock_move.py @@ -0,0 +1,27 @@ +# Copyright 2023 Jacques-Etienne Baudoux (BCIM) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from itertools import groupby + +from odoo import fields, models + + +class StockMove(models.Model): + _inherit = "stock.move" + + def _merge_moves(self, merge_into=False): + sorted_moves_by_rule = sorted(self, key=lambda m: m.rule_id.id) + res_moves = self.browse() + for rule, move_list in groupby(sorted_moves_by_rule, key=lambda m: m.rule_id.id): + moves = self.browse(m.id for m in move_list) + res_moves |= super(StockMove, moves)._merge_moves(merge_into=merge_into) + return res_moves + + def _prepare_merge_moves_distinct_fields(self): + result = super()._prepare_merge_moves_distinct_fields() + if self.rule_id.auto_create_group_by_product: + # Allow to merge moves on a pick operation having different + # deadlines + if "date_deadline" in result: + result.remove("date_deadline") + return result