From b737eb6868caa4f041c9d751804c25a1a4197650 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Mon, 27 Apr 2020 14:03:01 +0200 Subject: [PATCH] Fix warning in stock_move_common_dest In this module: https://github.com/OCA/stock-logistics-warehouse/pull/808 A computed field uses "common_dest_move_ids" as dependency: @api.depends( "picking_type_id.display_completion_info", "move_lines.common_dest_move_ids.state", ) def _compute_completion_info(self): Which has the following effect when the ORM triggers changes: odoo.osv.expression: Non-stored field stock.move.common_dest_move_ids cannot be searched. Implement a search method to prevent this. --- stock_move_common_dest/models/stock_move.py | 32 +++++++++++++++---- .../tests/test_move_common_dest.py | 18 +++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/stock_move_common_dest/models/stock_move.py b/stock_move_common_dest/models/stock_move.py index 395c77f83..4cf44b567 100644 --- a/stock_move_common_dest/models/stock_move.py +++ b/stock_move_common_dest/models/stock_move.py @@ -10,17 +10,12 @@ class StockMove(models.Model): common_dest_move_ids = fields.Many2many( "stock.move", compute="_compute_common_dest_move_ids", + search="_search_compute_dest_move_ids", help="All the stock moves having a chained destination move sharing the" " same picking as the actual move's destination move", ) - @api.depends( - "move_dest_ids", - "move_dest_ids.picking_id", - "move_dest_ids.picking_id.move_lines", - "move_dest_ids.picking_id.move_lines.move_orig_ids", - ) - def _compute_common_dest_move_ids(self): + def _common_dest_move_query(self): sql = """SELECT smmr.move_orig_id move_id , array_agg(smmr2.move_orig_id) common_move_dest_ids FROM stock_move_move_rel smmr @@ -36,6 +31,16 @@ class StockMove(models.Model): AND smmr.move_orig_id IN %s GROUP BY smmr.move_orig_id; """ + return sql + + @api.depends( + "move_dest_ids", + "move_dest_ids.picking_id", + "move_dest_ids.picking_id.move_lines", + "move_dest_ids.picking_id.move_lines.move_orig_ids", + ) + def _compute_common_dest_move_ids(self): + sql = self._common_dest_move_query() self.env.cr.execute(sql, (tuple(self.ids),)) res = { row.get("move_id"): row.get("common_move_dest_ids") @@ -47,3 +52,16 @@ class StockMove(models.Model): move.common_dest_move_ids = [(6, 0, common_move_ids)] else: move.common_dest_move_ids = [(5, 0, 0)] + + def _search_compute_dest_move_ids(self, operator, value): + moves = self.search([("id", operator, value)]) + if not moves: + return [("id", "=", 0)] + sql = self._common_dest_move_query() + self.env.cr.execute(sql, (tuple(moves.ids),)) + res = [ + move_dest_id + for row in self.env.cr.dictfetchall() + for move_dest_id in row.get("common_move_dest_ids") or [] + ] + return [("id", "in", res)] diff --git a/stock_move_common_dest/tests/test_move_common_dest.py b/stock_move_common_dest/tests/test_move_common_dest.py index c32132f36..9af07ff05 100644 --- a/stock_move_common_dest/tests/test_move_common_dest.py +++ b/stock_move_common_dest/tests/test_move_common_dest.py @@ -154,3 +154,21 @@ class TestCommonMoveDest(SavepointCase): self.assertEqual(pack_move_1b.common_dest_move_ids, pack_move_1a) self.assertFalse(ship_move_1a.common_dest_move_ids) self.assertFalse(ship_move_1b.common_dest_move_ids) + self.assertEqual( + self.env["stock.move"].search( + [("common_dest_move_ids", "=", pick_move_1b.id)] + ), + pick_move_1a, + ) + self.assertEqual( + self.env["stock.move"].search( + [("common_dest_move_ids", "=", pick_move_1a.id)] + ), + pick_move_1b, + ) + self.assertEqual( + self.env["stock.move"].search( + [("common_dest_move_ids", "in", (pick_move_1a | pick_move_1b).ids)] + ), + pick_move_1a | pick_move_1b, + )