From a46398f9c114f8f2a249166e6a29199ea8892e9c Mon Sep 17 00:00:00 2001 From: Josep-s73 Date: Tue, 9 Jul 2024 10:28:30 +0200 Subject: [PATCH] [MIG] l10n_es_intrastat_report: Migration to 17.0 --- l10n_es_intrastat_report/__manifest__.py | 2 +- .../data/account_fiscal_position_template.xml | 11 -- l10n_es_intrastat_report/hooks.py | 5 +- .../migrations/16.0.1.0.0/post-migration.py | 113 ------------------ .../migrations/16.0.1.3.0/post-migration.py | 98 --------------- l10n_es_intrastat_report/models/__init__.py | 1 + .../l10n_es_intrastat_product_declaration.py | 4 +- .../models/template_es_common.py | 15 +++ .../tests/test_l10n_es_intrastat_report.py | 7 +- .../views/l10n_es_intrastat_product.xml | 28 ++--- .../wizards/l10n_es_intrastat_code_import.py | 7 +- 11 files changed, 34 insertions(+), 257 deletions(-) delete mode 100644 l10n_es_intrastat_report/data/account_fiscal_position_template.xml delete mode 100644 l10n_es_intrastat_report/migrations/16.0.1.0.0/post-migration.py delete mode 100644 l10n_es_intrastat_report/migrations/16.0.1.3.0/post-migration.py create mode 100644 l10n_es_intrastat_report/models/template_es_common.py diff --git a/l10n_es_intrastat_report/__manifest__.py b/l10n_es_intrastat_report/__manifest__.py index ed555a32352..5294cef624f 100644 --- a/l10n_es_intrastat_report/__manifest__.py +++ b/l10n_es_intrastat_report/__manifest__.py @@ -16,11 +16,11 @@ "website": "https://github.com/OCA/l10n-spain", "depends": ["intrastat_product", "l10n_es_aeat"], "conflicts": ["report_intrastat"], + "external_dependencies": {"python": ["xlrd==1.2.0"]}, "data": [ "security/l10n_es_intrastat_report_security.xml", "security/ir.model.access.csv", "data/intrastat_transaction.xml", - "data/account_fiscal_position_template.xml", "views/l10n_es_intrastat_product.xml", "wizards/l10n_es_intrastat_code_import.xml", ], diff --git a/l10n_es_intrastat_report/data/account_fiscal_position_template.xml b/l10n_es_intrastat_report/data/account_fiscal_position_template.xml deleted file mode 100644 index 16971ee2787..00000000000 --- a/l10n_es_intrastat_report/data/account_fiscal_position_template.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - b2c - - - b2b - - diff --git a/l10n_es_intrastat_report/hooks.py b/l10n_es_intrastat_report/hooks.py index 86c345cd577..b3b3ee5816a 100644 --- a/l10n_es_intrastat_report/hooks.py +++ b/l10n_es_intrastat_report/hooks.py @@ -1,14 +1,11 @@ # Copyright 2020 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import SUPERUSER_ID, api - -def post_init_hook(cr, registry): +def post_init_hook(env): """Set the intrastat field of the private fiscal position records. This is necessary for the intrastat report generation """ - env = api.Environment(cr, SUPERUSER_ID, {}) items = env["ir.model.data"].search( [ ("model", "=", "account.fiscal.position"), diff --git a/l10n_es_intrastat_report/migrations/16.0.1.0.0/post-migration.py b/l10n_es_intrastat_report/migrations/16.0.1.0.0/post-migration.py deleted file mode 100644 index f67a349285f..00000000000 --- a/l10n_es_intrastat_report/migrations/16.0.1.0.0/post-migration.py +++ /dev/null @@ -1,113 +0,0 @@ -# Copyright 2023 Studio73 - Roger Amorós -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from openupgradelib import openupgrade - -from odoo.tools.sql import column_exists, create_column - -intr_prod_dec_fields = [ - "message_main_attachment_id", - "company_id", - "company_country_code", - "state", - "note", - "year", - "month", - "year_month", - "declaration_type", - "action", - "revision", - "num_decl_lines", - "total_amount", - "reporting_level", - "xml_attachment_id", -] -intr_prod_comp_lin_fields = [ - "invoice_line_id", - "src_dest_country_id", - "src_dest_country_code", - "hs_code_id", - "weight", - "suppl_unit_qty", - "amount_company_currency", - "amount_accessory_cost_company_currency", - "transaction_id", - "region_id", - "product_origin_country_id", - "product_origin_country_code", - "vat", - "partner_vat", - "incoterm_id", - "transport_id", - "intrastat_state_id", -] -intr_prod_dec_lin_fields = [ - "src_dest_country_id", - "src_dest_country_code", - "hs_code_id", - "suppl_unit_qty", - "transaction_id", - "region_id", - "product_origin_country_id", - "product_origin_country_code", - "vat", - "incoterm_id", - "transport_id", - "intrastat_state_id", - "weight", - "amount_company_currency", - "partner_vat", -] - - -@openupgrade.migrate() -def migrate(env, version): - if not column_exists(env.cr, "intrastat_product_declaration", "old_l10n_es_id"): - create_column( - env.cr, "intrastat_product_declaration", "old_l10n_es_id", "numeric" - ) - if not column_exists( - env.cr, "intrastat_product_declaration_line", "old_l10n_es_id" - ): - create_column( - env.cr, "intrastat_product_declaration_line", "old_l10n_es_id", "numeric" - ) - if not column_exists( - env.cr, "intrastat_product_computation_line", "old_l10n_es_id" - ): - create_column( - env.cr, "intrastat_product_computation_line", "old_l10n_es_id", "numeric" - ) - new_fields = ", ".join(f'"{f}"' for f in ["old_l10n_es_id"] + intr_prod_dec_fields) - old_fields = ", ".join(f'"{f}"' for f in ["id"] + intr_prod_dec_fields) - query = f"""INSERT INTO intrastat_product_declaration ({new_fields}) - SELECT {old_fields} FROM l10n_es_intrastat_product_declaration - """ - openupgrade.logged_query(env.cr, query) - - new_fields = ", ".join( - f"{f}" for f in ["old_l10n_es_id"] + intr_prod_dec_lin_fields - ) - old_fields = ", ".join(f"leipd.{f}" for f in ["id"] + intr_prod_dec_lin_fields) - query = f"""INSERT INTO intrastat_product_declaration_line (parent_id, {new_fields}) - SELECT ipd.id, {old_fields} - FROM l10n_es_intrastat_product_declaration_line AS leipd - INNER JOIN intrastat_product_declaration - AS ipd ON ipd.old_l10n_es_id = leipd.parent_id - """ - openupgrade.logged_query(env.cr, query) - - new_fields = ", ".join( - f"{f}" for f in ["old_l10n_es_id"] + intr_prod_comp_lin_fields - ) - old_fields = ", ".join(f"leipc.{f}" for f in ["id"] + intr_prod_comp_lin_fields) - query = f"""INSERT INTO intrastat_product_computation_line - (parent_id, declaration_line_id, {new_fields}) - SELECT ipd.id, leipd.id, {old_fields} - FROM l10n_es_intrastat_product_computation_line AS leipc - INNER JOIN intrastat_product_declaration - AS ipd ON ipd.old_l10n_es_id = leipc.parent_id - INNER JOIN intrastat_product_declaration_line - AS leipd ON leipd.old_l10n_es_id = leipc.declaration_line_id - """ - openupgrade.logged_query(env.cr, query) diff --git a/l10n_es_intrastat_report/migrations/16.0.1.3.0/post-migration.py b/l10n_es_intrastat_report/migrations/16.0.1.3.0/post-migration.py deleted file mode 100644 index 4c45fa0e9b1..00000000000 --- a/l10n_es_intrastat_report/migrations/16.0.1.3.0/post-migration.py +++ /dev/null @@ -1,98 +0,0 @@ -# Copyright 2023 Tecnativa - Víctor Martínez -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from openupgradelib import openupgrade, openupgrade_merge_records - -_xmlids_merge_records = [ - ( - "l10n_es_intrastat_report.intrastat_transaction_11", - "intrastat_product.intrastat_transaction_11", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_12", - "intrastat_product.intrastat_transaction_12", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_21", - "intrastat_product.intrastat_transaction_21", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_22", - "intrastat_product.intrastat_transaction_22", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_23", - "intrastat_product.intrastat_transaction_23", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_31", - "intrastat_product.intrastat_transaction_31", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_32", - "intrastat_product.intrastat_transaction_32", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_33", - "intrastat_product.intrastat_transaction_33", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_34", - "intrastat_product.intrastat_transaction_34", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_41", - "intrastat_product.intrastat_transaction_41", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_42", - "intrastat_product.intrastat_transaction_42", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_51", - "intrastat_product.intrastat_transaction_51", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_52", - "intrastat_product.intrastat_transaction_52", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_71", - "intrastat_product.intrastat_transaction_71", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_72", - "intrastat_product.intrastat_transaction_72", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_80", - "intrastat_product.intrastat_transaction_80", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_91", - "intrastat_product.intrastat_transaction_91", - ), - ( - "l10n_es_intrastat_report.intrastat_transaction_99", - "intrastat_product.intrastat_transaction_99", - ), -] - - -@openupgrade.migrate() -def migrate(env, version): - """Merge records from intrastat.transaction. - Existing records from l10n_en_intrastat_report will be merged into - intrastat_product records. - Remove obsolote transactions (previously archived).""" - for item in _xmlids_merge_records: - old_record = env.ref(item[0], raise_if_not_found=False) - new_record = env.ref(item[1], raise_if_not_found=False) - if old_record and new_record: - openupgrade_merge_records.merge_records( - env=env, - model_name="intrastat.transaction", - record_ids=old_record.ids, - target_record_id=new_record.id, - method="sql", - ) diff --git a/l10n_es_intrastat_report/models/__init__.py b/l10n_es_intrastat_report/models/__init__.py index b33449f0bbd..cedbeda537a 100644 --- a/l10n_es_intrastat_report/models/__init__.py +++ b/l10n_es_intrastat_report/models/__init__.py @@ -1,3 +1,4 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from . import l10n_es_intrastat_product_declaration from . import stock_location +from . import template_es_common diff --git a/l10n_es_intrastat_report/models/l10n_es_intrastat_product_declaration.py b/l10n_es_intrastat_report/models/l10n_es_intrastat_product_declaration.py index 706209830a0..e69af554c73 100644 --- a/l10n_es_intrastat_report/models/l10n_es_intrastat_product_declaration.py +++ b/l10n_es_intrastat_report/models/l10n_es_intrastat_product_declaration.py @@ -197,8 +197,8 @@ def _prepare_grouped_fields(self, fields_to_sum): vals["partner_vat"] = self.partner_vat return vals - def _prepare_declaration_line(self): - vals = super()._prepare_declaration_line() + def _prepare_declaration_line(self, line_number): + vals = super()._prepare_declaration_line(line_number) # Avoid rounding in weight and fiscal value vals["weight"] = 0.0 vals["amount_company_currency"] = 0.0 diff --git a/l10n_es_intrastat_report/models/template_es_common.py b/l10n_es_intrastat_report/models/template_es_common.py new file mode 100644 index 00000000000..b718c41f0d0 --- /dev/null +++ b/l10n_es_intrastat_report/models/template_es_common.py @@ -0,0 +1,15 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo import models + +from odoo.addons.account.models.chart_template import template + + +class AccountChartTemplate(models.AbstractModel): + _inherit = "account.chart.template" + + @template("es_common", model="account.fiscal.position") + def _get_fiscal_position(self): + return { + "fp_intra_private": {"intrastat": "b2c"}, + "fp_intra": {"intrastat": "b2b"}, + } diff --git a/l10n_es_intrastat_report/tests/test_l10n_es_intrastat_report.py b/l10n_es_intrastat_report/tests/test_l10n_es_intrastat_report.py index f4ab5438923..6d84722c413 100644 --- a/l10n_es_intrastat_report/tests/test_l10n_es_intrastat_report.py +++ b/l10n_es_intrastat_report/tests/test_l10n_es_intrastat_report.py @@ -57,10 +57,7 @@ def _create_declaration(cls, declaration_type): @classmethod def setUpClass(cls, chart_template_ref=None): - chart_template_ref = ( - "l10n_es.account_chart_template_common" or chart_template_ref - ) - super().setUpClass(chart_template_ref=chart_template_ref) + super().setUpClass(chart_template_ref="es_full") cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT)) # Set current company to Spanish intrastat_transport = cls.env["intrastat.transport_mode"].search([], limit=1) @@ -157,7 +154,7 @@ def test_post_init_hook(self): "res_id": fp.id, } ) - post_init_hook(self.env.cr, None) + post_init_hook(self.env) fp = self.env["account.fiscal.position"].browse(item.res_id) self.assertTrue(fp.intrastat) diff --git a/l10n_es_intrastat_report/views/l10n_es_intrastat_product.xml b/l10n_es_intrastat_report/views/l10n_es_intrastat_product.xml index e241e71a3ac..2f118919603 100644 --- a/l10n_es_intrastat_report/views/l10n_es_intrastat_product.xml +++ b/l10n_es_intrastat_report/views/l10n_es_intrastat_product.xml @@ -1,4 +1,4 @@ - + intrastat.product.computation.line.form intrastat.product.computation.line - primary + primary - - {'required': [('reporting_level', '=', 'extended')], 'invisible': [('reporting_level', '!=', 'extended')]} + reporting_level != 'extended' + reporting_level == 'extended' @@ -44,9 +42,7 @@ - - 1/> - + @@ -65,15 +61,13 @@ /> - - {'required': [('reporting_level', '=', 'extended')], 'invisible': [('reporting_level', '!=', 'extended')]} + reporting_level != 'extended' + reporting_level == 'extended' @@ -112,9 +106,7 @@ name="string" >Spanish Intrastat Product Declaration - - - + diff --git a/l10n_es_intrastat_report/wizards/l10n_es_intrastat_code_import.py b/l10n_es_intrastat_report/wizards/l10n_es_intrastat_code_import.py index 11a4ddf12ed..6f55e823797 100644 --- a/l10n_es_intrastat_report/wizards/l10n_es_intrastat_code_import.py +++ b/l10n_es_intrastat_report/wizards/l10n_es_intrastat_code_import.py @@ -2,12 +2,11 @@ # Copyright 2021 Tecnativa - João Marques # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3). -import os import xlrd from odoo import _, exceptions, models, tools -from odoo.modules.module import get_resource_path +from odoo.tools.misc import file_path UOM_MAPPING = { "p/st": "intrastat_unit_pce", @@ -32,9 +31,7 @@ def _get_intrastat_unit(self, name): def _import_hs_codes(self): code_obj = self.env["hs.code"].with_context(active_test=False) - path = os.path.join( - get_resource_path("l10n_es_intrastat_report"), "data", "Estruc_NC2023.xlsx" - ) + path = file_path("l10n_es_intrastat_report/data/Estruc_NC2023.xlsx") workbook = xlrd.open_workbook(path) sheet = workbook.sheet_by_index(0) vals_list = []