[IMP] intrastat_product: prepopulate stored computed fields

This commit is contained in:
Stefan Rijnhart
2022-06-22 15:44:41 +02:00
parent 2cec5b4f23
commit 1ad7b55a80
3 changed files with 37 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
from . import models
from . import report
from . import wizards
from .hooks import pre_init_hook

View File

@@ -41,4 +41,5 @@
],
"demo": ["demo/intrastat_demo.xml"],
"installable": True,
"pre_init_hook": "pre_init_hook",
}

View File

@@ -0,0 +1,35 @@
# Copyright 2022 Stefan Rijnhart <stefan@opener.amsterdam>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import logging
def pre_init_hook(cr):
"""Prepopulate stored computed fields for faster installation"""
logger = logging.getLogger(__name__)
logger.info("Prepopulating stored computed fields")
cr.execute(
"""
alter table account_move
add column if not exists src_dest_country_id integer;
"""
)
cr.execute(
"""
with countries as (
select am.id as move_id,
coalesce(
rps.country_id,
rp.country_id,
rpc.country_id
) as country_id
from account_move am
left join res_partner rps on rps.id = am.partner_shipping_id
join res_partner rp on rp.id = am.partner_id
join res_company rc on rc.id = am.company_id
join res_partner rpc on rpc.id = rc.partner_id
)
update account_move am
set src_dest_country_id = countries.country_id
from countries where am.id = countries.move_id;
"""
)