Skip to content

Commit

Permalink
[MIG] l10n_es_aeat: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiadavid committed Feb 16, 2024
1 parent dabdd88 commit 65c5b3b
Show file tree
Hide file tree
Showing 28 changed files with 687 additions and 586 deletions.
4 changes: 3 additions & 1 deletion l10n_es_aeat/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{
"name": "AEAT Base",
"summary": "Modulo base para declaraciones de la AEAT",
"version": "16.0.2.2.0",
"version": "17.0.1.0.0",
"author": "Pexego, "
"Acysos S.L., "
"AvanzOSC, "
Expand All @@ -27,6 +27,8 @@
"data": [
"security/aeat_security.xml",
"security/ir.model.access.csv",
"data/aeat_tax.xml",
"data/aeat_account.xml",
"data/aeat_partner.xml",
"data/ir_config_parameter.xml",
"data/aeat_tax_agency_data.xml",
Expand Down
4 changes: 4 additions & 0 deletions l10n_es_aeat/data/aeat_account.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<function model="aeat.account" name="update_accounts" />
</odoo>
4 changes: 4 additions & 0 deletions l10n_es_aeat/data/aeat_tax.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<function model="aeat.tax" name="update_taxes" />
</odoo>
4 changes: 2 additions & 2 deletions l10n_es_aeat/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ def create_column_thirdparty_invoice(cr):
cr.execute("UPDATE account_journal SET thirdparty_invoice = False")


def pre_init_hook(cr):
create_column_thirdparty_invoice(cr)
def pre_init_hook(env):
create_column_thirdparty_invoice(env.cr)
2 changes: 2 additions & 0 deletions l10n_es_aeat/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
from . import aeat_soap
from . import aeat_tax_agency
from . import account_move
from . import aeat_tax
from . import aeat_account
2 changes: 0 additions & 2 deletions l10n_es_aeat/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class AccountMove(models.Model):
thirdparty_number = fields.Char(
string="Third-party number",
index=True,
readonly=True,
states={"draft": [("readonly", False)]},
copy=False,
help="Número de la factura emitida por un tercero.",
)
Expand Down
3 changes: 1 addition & 2 deletions l10n_es_aeat/models/account_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class AccountTax(models.Model):
_inherit = "account.tax"

def _clear_tax_id_from_tax_template_cache(self):
Company = self.env["res.company"]
Company._get_tax_id_from_tax_template.clear_cache(Company)
self.env.registry.clear_cache()

@api.model_create_multi
def create(self, vals_list):
Expand Down
48 changes: 48 additions & 0 deletions l10n_es_aeat/models/aeat_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2016 Tecnativa - Antonio Espinosa
# Copyright 2019 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl
import logging
import traceback

from odoo import api, fields, models

_logger = logging.getLogger()


class AeatAccount(models.Model):
_name = "aeat.account"
_description = "AEAT Account"
_order = "name"

name = fields.Char()
xmlid = fields.Char()

@api.model
def update_accounts(self):
try:
chart_template = self.env["account.chart.template"].sudo()
accounts = {}
data = chart_template._parse_csv("es_common", "account.account", "l10n_es")
for key in data:
accounts[
key
] = f"{data[key]['code']} - {data[key].get('name@es') or data[key]['name']}"
self._update_accounts(accounts)
except Exception:
_logger.error(traceback.format_exc())
raise

def _update_accounts(self, account_data):
accounts = self.env["aeat.account"].search([])
accounts.filtered(lambda x: x.xmlid not in account_data).unlink()
accounts._load_records(
[
{
"xml_id": f"l10n_es_aeat.{key}",
"noupdate": True,
"values": {"xmlid": key, "name": name},
}
for key, name in account_data.items()
],
update=True,
)
46 changes: 46 additions & 0 deletions l10n_es_aeat/models/aeat_tax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2016 Tecnativa - Antonio Espinosa
# Copyright 2019 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl
import logging
import traceback

from odoo import api, fields, models

_logger = logging.getLogger()


class AeatTax(models.Model):
_name = "aeat.tax"
_description = "AEAT Tax"
_order = "name"

name = fields.Char()
xmlid = fields.Char()

@api.model
def update_taxes(self):
try:
chart_template = self.env["account.chart.template"].sudo()
taxes = {}
data = chart_template._parse_csv("es_common", "account.tax", "l10n_es")
for key in data:
taxes[key] = data[key].get("description@es") or data[key]["description"]
self._update_taxes(taxes)
except Exception:
_logger.error(traceback.format_exc())
raise

def _update_taxes(self, tax_data):
taxes = self.env["aeat.tax"].search([])
taxes.filtered(lambda x: x.xmlid not in tax_data).unlink()
taxes._load_records(
[
{
"xml_id": f"l10n_es_aeat.{key}",
"noupdate": True,
"values": {"xmlid": key, "name": name},
}
for key, name in tax_data.items()
],
update=True,
)
6 changes: 2 additions & 4 deletions l10n_es_aeat/models/l10n_es_aeat_map_tax_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ class L10nEsAeatMapTaxLine(models.Model):
_description = "AEAT tax mapping line"

field_number = fields.Integer(required=True)
tax_ids = fields.Many2many(
comodel_name="account.tax.template", string="Taxes templates"
)
tax_ids = fields.Many2many(comodel_name="aeat.tax", string="Taxes templates")
account_id = fields.Many2one(
comodel_name="account.account.template",
comodel_name="aeat.account",
string="Account Template",
)
name = fields.Char(required=True)
Expand Down
35 changes: 2 additions & 33 deletions l10n_es_aeat/models/l10n_es_aeat_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,12 @@ def _get_export_config(self, date):
comodel_name="res.company",
string="Company",
required=True,
readonly=True,
default=lambda self: self.env.company,
states={"draft": [("readonly", False)]},
)
company_vat = fields.Char(
string="VAT number",
size=9,
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
)
number = fields.Char(
string="Model number",
Expand All @@ -107,54 +103,40 @@ def _get_export_config(self, date):
previous_number = fields.Char(
string="Previous declaration number",
size=13,
states={"done": [("readonly", True)]},
)
contact_name = fields.Char(
string="Full Name",
size=40,
help="Must have name and surname.",
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
)
contact_phone = fields.Char(
string="Phone",
size=9,
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
)
contact_email = fields.Char(
size=50,
readonly=True,
states={"draft": [("readonly", False)]},
)
representative_vat = fields.Char(
string="L.R. VAT number",
size=9,
readonly=True,
help="Legal Representative VAT number.",
states={"draft": [("readonly", False)]},
)
year = fields.Integer(
default=_default_year,
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
)
statement_type = fields.Selection(
selection=[("N", "Normal"), ("C", "Complementary"), ("S", "Substitutive")],
default="N",
readonly=True,
required=True,
states={"draft": [("readonly", False)]},
)
support_type = fields.Selection(
selection=[("C", "DVD"), ("T", "Telematics")],
default="T",
readonly=True,
required=True,
states={"draft": [("readonly", False)]},
)
calculation_date = fields.Datetime()
state = fields.Selection(
Expand Down Expand Up @@ -194,24 +176,18 @@ def _get_export_config(self, date):
selection="get_period_type_selection",
required=True,
default=_default_period_type,
readonly=True,
states={"draft": [("readonly", False)]},
)
date_start = fields.Date(
string="Starting date",
required=True,
readonly=True,
store=True,
compute="_compute_dates",
states={"draft": [("readonly", False)]},
)
date_end = fields.Date(
string="Ending date",
required=True,
readonly=True,
store=True,
compute="_compute_dates",
states={"draft": [("readonly", False)]},
)
allow_posting = fields.Boolean(compute="_compute_allow_posting")
counterpart_account_id = fields.Many2one(
Expand All @@ -226,7 +202,6 @@ def _get_export_config(self, date):
domain="[('type', '=', 'general'), ('company_id', '=', company_id)]",
default=_default_journal,
help="Journal in which post the move.",
states={"done": [("readonly", True)]},
)
move_id = fields.Many2one(
comodel_name="account.move",
Expand Down Expand Up @@ -322,12 +297,7 @@ def _compute_dates(self):
f"{report.year}-{starting_month}-01"
)
report.date_end = fields.Date.to_date(
"%s-%s-%s"
% (
report.year,
ending_month,
monthrange(report.year, ending_month)[1],
)
f"{report.year}-{ending_month}-{monthrange(report.year, ending_month)[1]}"
)
elif report.period_type in (
"01",
Expand All @@ -347,8 +317,7 @@ def _compute_dates(self):
month = int(report.period_type)
report.date_start = fields.Date.to_date(f"{report.year}-{month}-01")
report.date_end = fields.Date.to_date(
"%s-%s-%s"
% (report.year, month, monthrange(report.year, month)[1])
f"{report.year}-{month}-{monthrange(report.year, month)[1]}"
)

@api.depends("date_start")
Expand Down
19 changes: 4 additions & 15 deletions l10n_es_aeat/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,15 @@ def _get_tax_id_from_tax_template(self, tax_template, company):
"""Low level cached search for a tax given its tax template and
company.
"""
xmlids = (
self.sudo()
.env["ir.model.data"]
.search_read(
[
("model", "=", "account.tax.template"),
("res_id", "=", tax_template.id),
],
["name", "module"],
)
)
return (
xmlids
tax_template
and self.sudo()
.env["ir.model.data"]
.search(
[
("model", "=", "account.tax"),
("module", "=", xmlids[0]["module"]),
("name", "=", "{}_{}".format(company.id, xmlids[0]["name"])),
("module", "=", "account"),
("name", "=", f"{company.id}_{tax_template.xmlid}"),
]
)
.res_id
Expand Down Expand Up @@ -98,7 +87,7 @@ def get_taxes_from_templates(self, tax_templates):
tax_ids = []
# We need to rebrowse the records to avoid a problem with the ormcache
# and virtual records that populate m2m as NewId.
for tmpl in self.env["account.tax.template"].browse(tax_templates.ids):
for tmpl in self.env["aeat.tax"].browse(tax_templates.ids):
tax_id = self._get_tax_id_from_tax_template(tmpl, self)
if tax_id:
tax_ids.append(tax_id)
Expand Down
4 changes: 4 additions & 0 deletions l10n_es_aeat/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ access_model_l10n_es_aeat_map_tax_admin,aeat.mod.map.tax.code admin,model_l10n_e
access_model_l10n_es_aeat_map_tax_aeat,aeat.mod.map.tax.code aeat,model_l10n_es_aeat_map_tax,group_account_aeat,1,0,0,0
access_model_l10n_es_aeat_map_tax_line_admin,aeat.mod.map.tax.code.line admin,model_l10n_es_aeat_map_tax_line,base.group_system,1,1,1,1
access_model_l10n_es_aeat_map_taxe_line_aeat,aeat.mod.map.tax.code.line aeat,model_l10n_es_aeat_map_tax_line,group_account_aeat,1,0,0,0
access_model_l10n_es_aeat_tax_admin,aeat.tax admin,model_aeat_tax,base.group_system,1,1,1,1
access_model_l10n_es_aeat_tax_aeat,aeat.tax aeat,model_aeat_tax,group_account_aeat,1,0,0,0
access_model_l10n_es_aeat_account_admin,aeat.account admin,model_aeat_account,base.group_system,1,1,1,1
access_model_l10n_es_aeat_account_aeat,aeat.account aeat,model_aeat_account,group_account_aeat,1,0,0,0
access_model_l10n_es_aeat_tax_line_aeat,l10n.es.aeat.tax.line aeat,model_l10n_es_aeat_tax_line,group_account_aeat,1,1,1,1
access_l10n_es_aeat_certificate_manager,l10n_es_aeat_certificate manager,model_l10n_es_aeat_certificate,account.group_account_manager,1,1,1,1
access_aeat_tax_agency_account,access_aeat_tax_agency_account,model_aeat_tax_agency,base.group_user,1,0,0,0
Expand Down
3 changes: 2 additions & 1 deletion l10n_es_aeat/tests/test_l10n_es_aeat_export_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ def test_export_config_file(self):
],
}
)
new_report = self.env["l10n.es.aeat.report"].new({"name": "Test Report"})

new_report = self.env["l10n.es.aeat.report"]
export_to_boe = self.env["l10n.es.aeat.report.export_to_boe"].create(
{"name": "test_export_to_boe.txt"}
)
Expand Down
4 changes: 2 additions & 2 deletions l10n_es_aeat/tests/test_l10n_es_aeat_map_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _test_map_line_overlap(self, from1, to1, from2, to2, raise_exception):
def test_map_line_overlaps(self):
for from1, to1, from2, to2, raise_exception in _DATES_MAPPING:
_logger.info(
"Tax map 1 with date_from %s and date_to %s against tax map 2 "
"with date_from %s and date_to %s" % (from1, to1, from2, to2)
f"Tax map 1 with date_from {from1} and date_to {to1} against tax map 2 "
f"with date_from {from2} and date_to {to2}"
)
self._test_map_line_overlap(from1, to1, from2, to2, raise_exception)
7 changes: 3 additions & 4 deletions l10n_es_aeat/tests/test_l10n_es_aeat_mod_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ def _chart_of_accounts_create(cls):
cls.company = cls.env["res.company"].create(
{"name": "Spanish test company", "currency_id": cls.env.ref("base.EUR").id}
)
cls.chart = cls.env.ref("l10n_es.account_chart_template_pymes")
cls.env.ref("base.group_multi_company").write({"users": [(4, cls.env.uid)]})
cls.env.user.write(
{"company_ids": [(4, cls.company.id)], "company_id": cls.company.id}
)
chart = cls.env.ref("l10n_es.account_chart_template_pymes")
chart.try_loading()
chart = cls.env["account.chart.template"]
chart.try_loading(template_code="es_pymes", company=cls.company)
cls.with_context(company_id=cls.company.id)
return True

Expand Down Expand Up @@ -101,7 +100,7 @@ def _get_taxes(cls, descs):
taxes = cls.env["account.tax"]
for desc in descs.split(","):
parts = desc.split(".")
module = parts[0] if len(parts) > 1 else "l10n_es"
module = parts[0] if len(parts) > 1 else "l10n_es_aeat"
xml_id = parts[1] if len(parts) > 1 else parts[0]
if xml_id.lower() != xml_id and len(parts) == 1:
# shortcut for not changing existing tests with old codes
Expand Down
Loading

0 comments on commit 65c5b3b

Please sign in to comment.