forked from OCA/sale-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.py
69 lines (58 loc) · 1.75 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
# Copyright 2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from odoo import SUPERUSER_ID
from odoo.api import Environment
_logger = logging.getLogger(__name__)
def pre_init_hook(cr):
_logger.info("Create discount columns in database")
# Use IF NOT EXISTS to alter table because when the module is already
# installed (v9.0 for example) and you migrate to this 13.0, the column
# already exists but the module is considered as new.
# So this hook is triggered and it raise an exception.
cr.execute(
"""
ALTER TABLE sale_order
ADD COLUMN IF NOT EXISTS price_total_no_discount numeric;
"""
)
cr.execute(
"""
ALTER TABLE sale_order
ADD COLUMN IF NOT EXISTS discount_total numeric;
"""
)
cr.execute(
"""
ALTER TABLE sale_order_line
ADD COLUMN IF NOT EXISTS price_total_no_discount
numeric;
"""
)
cr.execute(
"""
ALTER TABLE sale_order_line
ADD COLUMN IF NOT EXISTS discount_total numeric;
"""
)
def post_init_hook(cr, registry):
_logger.info("Compute discount columns")
env = Environment(cr, SUPERUSER_ID, {})
query = """
update sale_order_line
set price_total_no_discount = price_total
where discount = 0.0
"""
cr.execute(query)
query = """
update sale_order
set price_total_no_discount = amount_total
"""
cr.execute(query)
query = """
select distinct order_id from sale_order_line where discount > 0.0;
"""
cr.execute(query)
order_ids = cr.fetchall()
orders = env["sale.order"].search([("id", "in", order_ids)])
orders.mapped("order_line")._update_discount_display_fields()