-
-
Notifications
You must be signed in to change notification settings - Fork 848
Expand file tree
/
Copy pathstock_move.py
More file actions
76 lines (69 loc) · 2.71 KB
/
Copy pathstock_move.py
File metadata and controls
76 lines (69 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import api, fields, models
from odoo.osv.expression import FALSE_DOMAIN
class StockMove(models.Model):
_inherit = "stock.move"
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",
)
def _flush_common_dest_move_query(self):
# flush is necessary before a SELECT
self.flush_recordset(["move_orig_ids", "move_dest_ids"])
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
, stock_move sm_dest
, stock_picking sp
, stock_move sm_pick
, stock_move_move_rel smmr2
WHERE smmr.move_dest_id = sm_dest.id
AND sm_dest.picking_id = sp.id
AND sp.id = sm_pick.picking_id
AND sm_pick.id = smmr2.move_dest_id
AND smmr.move_orig_id != smmr2.move_orig_id
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_ids",
"move_dest_ids.picking_id.move_ids.move_orig_ids",
)
def _compute_common_dest_move_ids(self):
if not self.ids:
self.common_dest_move_ids = [(5, 0, 0)]
return
self._flush_common_dest_move_query()
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")
for row in self.env.cr.dictfetchall()
}
for move in self:
common_move_ids = res.get(move.id)
if common_move_ids:
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 FALSE_DOMAIN
self._flush_common_dest_move_query()
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)]