-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
init_hook.py
46 lines (35 loc) · 1.31 KB
/
init_hook.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
# Copyright (C) 2022 NextERP Romania
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import logging
logger = logging.getLogger(__name__)
def pre_init_hook(cr):
"""
The objective of this hook is to speed up the installation
of the module on an existing Odoo instance.
Without this script, if a database has a few hundred thousand
partners, which is not unlikely, the update will take
at least a few hours.
The pre init script writes the digits from vat fields to the
l10n_ro_vat_number so that it is not computed by the install.
"""
store_field_l10n_ro_vat_number(cr)
def store_field_l10n_ro_vat_number(cr):
cr.execute(
"""SELECT column_name
FROM information_schema.columns
WHERE table_name='res_partner' AND column_name='l10n_ro_vat_number'"""
)
if not cr.fetchone():
cr.execute(
"""
ALTER TABLE res_partner ADD COLUMN l10n_ro_vat_number varchar;
COMMENT ON COLUMN res_partner.l10n_ro_vat_number IS 'VAT number digits';
"""
)
logger.info("Computing field l10n_ro_vat_number on res.partner")
cr.execute(
r"""
UPDATE res_partner partner
SET l10n_ro_vat_number = NULLIF(regexp_replace(vat, '[^\.\d]', '', 'g'), '')
"""
)