diff --git a/intrastat_product/__init__.py b/intrastat_product/__init__.py index cf6083c..c120419 100644 --- a/intrastat_product/__init__.py +++ b/intrastat_product/__init__.py @@ -1,3 +1,4 @@ from . import models from . import report from . import wizards +from .hooks import pre_init_hook diff --git a/intrastat_product/__manifest__.py b/intrastat_product/__manifest__.py index 1ad9b3a..32c283d 100644 --- a/intrastat_product/__manifest__.py +++ b/intrastat_product/__manifest__.py @@ -41,4 +41,5 @@ ], "demo": ["demo/intrastat_demo.xml"], "installable": True, + "pre_init_hook": "pre_init_hook", } diff --git a/intrastat_product/hooks.py b/intrastat_product/hooks.py new file mode 100644 index 0000000..6877121 --- /dev/null +++ b/intrastat_product/hooks.py @@ -0,0 +1,35 @@ +# Copyright 2022 Stefan Rijnhart +# 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; + """ + ) diff --git a/intrastat_product/models/intrastat_product_declaration.py b/intrastat_product/models/intrastat_product_declaration.py index 43839ad..8e80a3d 100644 --- a/intrastat_product/models/intrastat_product_declaration.py +++ b/intrastat_product/models/intrastat_product_declaration.py @@ -978,9 +978,9 @@ class IntrastatProductDeclaration(models.Model): def create_xls(self): if self.env.context.get("computation_lines"): - report_file = "instrastat_transactions" + report_file = "intrastat_transactions" else: - report_file = "instrastat_declaration_lines" + report_file = "intrastat_declaration_lines" return { "type": "ir.actions.report", "report_type": "xlsx",