forked from OCA/product-attribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.py
119 lines (114 loc) · 4.05 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright 2023 ACSONE SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
import logging
from odoo.tools import sql
_logger = logging.getLogger(__name__)
def pre_init_hook(cr): # pragma: nocover
"""Recompute the volume and weight column on product and template
by converting the value from the uom defined on the product to the default uom
"""
if sql.column_exists(cr, "product_template", "volume_uom_id"):
_logger.info("Recompute volume on product.product")
# get default m3 uom
cr.execute(
"""
SELECT res_id
FROM ir_model_data
WHERE module = 'uom' AND name = 'product_uom_cubic_meter'
"""
)
m3_uom_id = cr.fetchone()[0]
# get uom factor
cr.execute(
"""
SELECT factor
FROM uom_uom
WHERE id = %s
""",
(m3_uom_id,),
)
m3_uom_factor = cr.fetchone()[0]
# update volume where volume_uom_id is not null and not m3
cr.execute(
"""
UPDATE product_product
SET volume = product_product.volume / product_uom.factor * %s
FROM uom_uom product_uom,
product_template pt
WHERE product_uom.id = volume_uom_id
AND pt.id = product_product.product_tmpl_id
AND volume_uom_id IS NOT NULL AND pt.volume_uom_id != %s
""",
(m3_uom_factor, m3_uom_id),
)
_logger.info(f"{cr.rowcount} product_product rows updated")
# update product_template with 1 product_product
cr.execute(
"""
UPDATE product_template
SET Volume = unique_product.volume
FROM (
SELECT product_tmpl_id, volume
FROM product_product
WHERE volume is not null
GROUP BY product_tmpl_id, volume
HAVING COUNT(*) = 1
) unique_product
WHERE product_template.id = unique_product.product_tmpl_id
AND product_template.volume_uom_id != %s
""",
(m3_uom_id,),
)
_logger.info(f"{cr.rowcount} product_template rows updated")
if sql.column_exists(cr, "product_template", "weight_uom_id"):
_logger.info("Recompute weight on product.product")
# get default kg uom
cr.execute(
"""
SELECT res_id
FROM ir_model_data
WHERE module = 'uom' AND name = 'product_uom_kgm'
"""
)
kg_uom_id = cr.fetchone()[0]
# get uom factor
cr.execute(
"""
SELECT factor
FROM uom_uom
WHERE id = %s
""",
(kg_uom_id,),
)
kg_uom_factor = cr.fetchone()[0]
# update weight where weight_uom_id is not null and not kg
cr.execute(
"""
UPDATE product_product
SET weight = product_product.weight / product_uom.factor * %s
FROM uom_uom product_uom, product_template pt
WHERE product_uom.id = weight_uom_id
AND pt.id = product_product.product_tmpl_id
AND weight_uom_id IS NOT NULL AND pt.weight_uom_id != %s
""",
(kg_uom_factor, kg_uom_id),
)
_logger.info(f"{cr.rowcount} product_product rows updated")
# update product_template with 1 product_product
cr.execute(
"""
UPDATE product_template
SET weight = unique_product.weight
FROM (
SELECT product_tmpl_id, weight
FROM product_product
WHERE volume is not null
GROUP BY product_tmpl_id, weight
HAVING COUNT(*) = 1
) unique_product
WHERE product_template.id = unique_product.product_tmpl_id
AND product_template.weight_uom_id != %s
""",
(kg_uom_id,),
)
_logger.info(f"{cr.rowcount} product_template rows updated")