forked from OCA/stock-logistics-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.py
94 lines (85 loc) · 3.2 KB
/
hooks.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright 2019 Tecnativa - David
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from odoo import SUPERUSER_ID, api
_logger = logging.getLogger(__name__)
def pre_init_hook(cr):
"""Speed up the installation of the module on an existing Odoo instance"""
cr.execute(
"""
SELECT column_name
FROM information_schema.columns
WHERE table_name='stock_move' AND
column_name='qty_returnable'
"""
)
if not cr.fetchone():
_logger.info("Creating field qty_returnable on stock_move")
cr.execute(
"""
ALTER TABLE stock_move ADD COLUMN qty_returnable float;
"""
)
cr.execute(
"""
UPDATE stock_move SET qty_returnable = 0
WHERE state IN ('draft', 'cancel')
"""
)
cr.execute(
"""
UPDATE stock_move SET qty_returnable = product_uom_qty
WHERE state = 'done'
"""
)
def post_init_hook(cr, registry):
"""Set moves returnable qty on hand"""
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
moves_draft = env["stock.move"].search([("state", "in", ["draft", "cancel"])])
moves_no_return_pendant = env["stock.move"].search(
[
("returned_move_ids", "=", False),
("state", "not in", ["draft", "cancel", "done"]),
]
)
moves_by_reserved_availability = {}
for move in moves_no_return_pendant:
moves_by_reserved_availability.setdefault(move.reserved_availability, [])
moves_by_reserved_availability[move.reserved_availability].append(move.id)
for qty, ids in moves_by_reserved_availability.items():
cr.execute(
"UPDATE stock_move SET qty_returnable = %s " "WHERE id IN %s",
(qty, tuple(ids)),
)
moves_no_return_done = env["stock.move"].search(
[
("returned_move_ids", "=", False),
("state", "=", "done"),
]
)
# Recursively solve quantities
updated_moves = moves_no_return_done + moves_draft + moves_no_return_pendant
remaining_moves = env["stock.move"].search(
[
("returned_move_ids", "!=", False),
("state", "=", "done"),
]
)
while remaining_moves:
_logger.info("{} moves left...".format(len(remaining_moves)))
remaining_moves, updated_moves = update_qty_returnable(
cr, remaining_moves, updated_moves
)
def update_qty_returnable(cr, remaining_moves, updated_moves):
for move in remaining_moves:
if all([x in updated_moves for x in move.returned_move_ids]):
quantity_returned = sum(move.returned_move_ids.mapped("qty_returnable"))
quantity = move.product_uom_qty - quantity_returned
cr.execute(
"UPDATE stock_move SET qty_returnable = %s " "WHERE id = %s",
(quantity, move.id),
)
remaining_moves -= move
updated_moves += move
return remaining_moves, updated_moves