forked from OCA/stock-logistics-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.py
102 lines (90 loc) · 3.06 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
95
96
97
98
99
100
101
102
# Copyright 2022 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
import logging
from openupgradelib import openupgrade
from odoo import SUPERUSER_ID, api
from odoo.tools.sql import column_exists, create_column
logger = logging.getLogger(__name__)
def setup_move_line_progress(cr):
"""Update the 'progress' column for move lines."""
table = "stock_move_line"
column = "progress"
if column_exists(cr, table, column):
logger.info("%s already exists on %s, skipping setup", column, table)
return
env = api.Environment(cr, SUPERUSER_ID, {})
logger.info("creating %s on table %s", column, table)
field_spec = [
(
"progress",
"stock.move.line",
"stock_move_line",
"float",
"float",
"stock_picking_progress",
100.0,
)
]
openupgrade.add_fields(env, field_spec)
logger.info("filling up %s on %s", column, table)
cr.execute("""UPDATE stock_move_line SET progress =0 WHERE state = 'cancel'""")
cr.execute(
"""
UPDATE stock_move_line SET progress =(qty_done / reserved_qty) * 100
WHERE state not in ('done', 'cancel') AND reserved_qty > 0.0
"""
)
def setup_move_progress(cr):
"""Update the 'progress' column for not-started or already done moves."""
table = "stock_move"
column = "progress"
if column_exists(cr, table, column):
logger.info("%s already exists on %s, skipping setup", column, table)
return
env = api.Environment(cr, SUPERUSER_ID, {})
logger.info("creating %s on table %s", column, table)
field_spec = [
(
"progress",
"stock.move",
"stock_move",
"float",
"float",
"stock_picking_progress",
100.0,
)
]
openupgrade.add_fields(env, field_spec)
logger.info("filling up %s on %s", column, table)
cr.execute("""UPDATE stock_move SET progress =0 WHERE state = 'cancel'""")
cr.execute(
"""
UPDATE stock_move SET progress =(quantity_done / product_uom_qty) * 100
WHERE state not in ('done', 'cancel') AND product_uom_qty > 0.0
"""
)
def setup_picking_progress(cr):
table = "stock_picking"
column = "progress"
_type = "float"
if column_exists(cr, table, column):
logger.info("%s already exists on %s, skipping setup", column, table)
return
logger.info("creating %s on table %s", column, table)
create_column(cr, table, column, _type)
fill_column_query = """
UPDATE stock_picking p
SET progress = subquery.avg_progress
FROM (
SELECT sm.picking_id, avg(sm.progress) as avg_progress
FROM stock_move sm
GROUP BY sm.picking_id
) as subquery
WHERE p.id = subquery.picking_id;
"""
logger.info("filling up %s on %s", column, table)
cr.execute(fill_column_query)
def pre_init_hook(cr):
setup_move_line_progress(cr)
setup_move_progress(cr)
setup_picking_progress(cr)