forked from OCA/stock-logistics-warehouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.py
67 lines (61 loc) · 2.6 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
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import logging
from odoo.tools.sql import column_exists, create_column
_logger = logging.getLogger(__name__)
def pre_init_hook(cr):
"""Pre init create volume column on stock.picking and stock.move"""
if not column_exists(cr, "stock_move", "volume"):
create_column(cr, "stock_move", "volume", "double precision")
# First we compute the reserved qty by move_id
# the reserved qty is the sum of the reserved qty of the move lines
# linked to the move
# Then we update the volume of the moves not in state done or cancel
# If the move is in state partially available, or assigned, the volume
# is the reserved qty * the product volume
# else the volume is the move quantity * the product volume
cr.execute(
"""
with reserved_qty_by_move as (
select
move_id,
product_id,
sum(reserved_qty) as reserved_qty
from stock_move_line
group by move_id, product_id
)
update stock_move
set volume =
CASE
WHEN state in ('partially_available', 'assigned') THEN
reserved_qty * pp.volume
ELSE
product_uom_qty * pp.volume
END
from reserved_qty_by_move
join product_product pp on pp.id = reserved_qty_by_move.product_id
where
stock_move.id = reserved_qty_by_move.move_id
and state not in ('done', 'cancel')
"""
)
_logger.info(f"{cr.rowcount} rows updated in stock_move")
if not column_exists(cr, "stock_picking", "volume"):
create_column(cr, "stock_picking", "volume", "double precision")
# we recompute the volume of the pickings not in state done or cancel
# the volume is the sum of the volume of the moves linked to the picking
# that are not in state done or cancel
cr.execute(
"""
update stock_picking
set volume = (
select sum(volume)
from stock_move
where
stock_move.picking_id = stock_picking.id
and state not in ('done', 'cancel')
)
where state not in ('done', 'cancel')
"""
)
_logger.info(f"{cr.rowcount} rows updated in stock_picking")