2019-03-15 21:25:19 +01:00
|
|
|
# Copyright 2010 Jordi Esteve, Zikzakmedia S.L. (http://www.zikzakmedia.com)
|
|
|
|
# Copyright 2010 Pexego Sistemas Informáticos S.L.(http://www.pexego.es)
|
2016-05-25 14:05:38 +02:00
|
|
|
# Borja López Soilán
|
2019-03-15 21:25:19 +01:00
|
|
|
# Copyright 2013 Joaquin Gutierrez (http://www.gutierrezweb.es)
|
|
|
|
# Copyright 2015 Antonio Espinosa <antonioea@tecnativa.com>
|
|
|
|
# Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
|
|
|
|
# Copyright 2016 Jacques-Etienne Baudoux <je@bcim.be>
|
2018-09-18 14:41:19 +02:00
|
|
|
# Copyright 2018 Tecnativa - Pedro M. Baeza
|
2020-09-15 12:32:42 +02:00
|
|
|
# Copyright 2020 Noviat - Luc De Meyer
|
2016-05-25 14:05:38 +02:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
|
|
|
2020-01-08 21:52:03 +01:00
|
|
|
import logging
|
2016-05-25 14:05:38 +02:00
|
|
|
from contextlib import closing
|
2018-06-13 16:53:05 +02:00
|
|
|
from io import StringIO
|
2020-01-08 21:52:03 +01:00
|
|
|
|
2023-01-11 11:19:31 +01:00
|
|
|
from markupsafe import Markup
|
|
|
|
|
2020-01-08 21:52:03 +01:00
|
|
|
from odoo import _, api, exceptions, fields, models, tools
|
|
|
|
from odoo.tools import config
|
2013-10-01 18:28:19 +02:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
_logger = logging.getLogger(__name__)
|
2018-09-21 03:06:39 +02:00
|
|
|
EXCEPTION_TEXT = "Traceback (most recent call last)"
|
2013-10-01 18:28:19 +02:00
|
|
|
|
|
|
|
|
2015-03-28 14:56:47 +01:00
|
|
|
class WizardUpdateChartsAccounts(models.TransientModel):
|
2020-01-08 21:52:03 +01:00
|
|
|
_name = "wizard.update.charts.accounts"
|
|
|
|
_description = "Wizard Update Charts Accounts"
|
2013-10-01 18:28:19 +02:00
|
|
|
|
2015-03-28 14:56:47 +01:00
|
|
|
state = fields.Selection(
|
2020-01-08 21:52:03 +01:00
|
|
|
selection=[
|
|
|
|
("init", "Configuration"),
|
|
|
|
("ready", "Select records to update"),
|
|
|
|
("done", "Wizard completed"),
|
|
|
|
],
|
|
|
|
string="Status",
|
|
|
|
readonly=True,
|
|
|
|
default="init",
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
company_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="res.company",
|
|
|
|
string="Company",
|
|
|
|
required=True,
|
|
|
|
default=lambda self: self.env.user.company_id.id,
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
chart_template_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="account.chart.template",
|
|
|
|
string="Chart Template",
|
|
|
|
ondelete="cascade",
|
|
|
|
required=True,
|
|
|
|
)
|
2016-05-25 14:05:38 +02:00
|
|
|
chart_template_ids = fields.Many2many(
|
|
|
|
"account.chart.template",
|
|
|
|
string="Chart Templates",
|
|
|
|
compute="_compute_chart_template_ids",
|
2020-01-08 21:52:03 +01:00
|
|
|
help="Includes all chart templates.",
|
|
|
|
)
|
|
|
|
code_digits = fields.Integer(related="chart_template_id.code_digits")
|
2015-03-28 14:56:47 +01:00
|
|
|
lang = fields.Selection(
|
2020-01-08 21:52:03 +01:00
|
|
|
lambda self: self._get_lang_selection_options(),
|
|
|
|
"Language",
|
2016-05-25 14:05:38 +02:00
|
|
|
required=True,
|
|
|
|
help="For records searched by name (taxes, fiscal "
|
2020-01-08 21:52:03 +01:00
|
|
|
"positions), the template name will be matched against the "
|
|
|
|
"record name on this language.",
|
|
|
|
default=lambda self: self.env.context.get("lang", self.env.user.lang),
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
update_tax = fields.Boolean(
|
2020-01-08 21:52:03 +01:00
|
|
|
string="Update taxes",
|
|
|
|
default=True,
|
|
|
|
help="Existing taxes are updated. Taxes are searched by name.",
|
|
|
|
)
|
2020-09-15 12:32:42 +02:00
|
|
|
update_tax_repartition_line_account = fields.Boolean(
|
|
|
|
string="Update Tax Accounts",
|
|
|
|
default=True,
|
|
|
|
help="Update account_id field on existing Tax repartition lines",
|
|
|
|
)
|
|
|
|
update_tax_repartition_line_tags = fields.Boolean(
|
|
|
|
string="Update Tax Tags",
|
|
|
|
default=True,
|
|
|
|
help="Update tag_ids field on existing Tax repartition lines",
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
update_account = fields.Boolean(
|
2020-01-08 21:52:03 +01:00
|
|
|
string="Update accounts",
|
|
|
|
default=True,
|
|
|
|
help="Existing accounts are updated. Accounts are searched by code.",
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
update_fiscal_position = fields.Boolean(
|
2020-01-08 21:52:03 +01:00
|
|
|
string="Update fiscal positions",
|
|
|
|
default=True,
|
2015-03-28 14:56:47 +01:00
|
|
|
help="Existing fiscal positions are updated. Fiscal positions are "
|
2020-01-08 21:52:03 +01:00
|
|
|
"searched by name.",
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
continue_on_errors = fields.Boolean(
|
2020-01-08 21:52:03 +01:00
|
|
|
default=False,
|
2015-03-28 14:56:47 +01:00
|
|
|
help="If set, the wizard will continue to the next step even if "
|
2020-01-08 21:52:03 +01:00
|
|
|
"there are minor errors.",
|
2019-01-29 18:07:29 +01:00
|
|
|
)
|
2020-01-08 21:52:03 +01:00
|
|
|
recreate_xml_ids = fields.Boolean(string="Recreate missing XML-IDs")
|
2015-03-28 14:56:47 +01:00
|
|
|
tax_ids = fields.One2many(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="wizard.update.charts.accounts.tax",
|
|
|
|
inverse_name="update_chart_wizard_id",
|
|
|
|
string="Taxes",
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
account_ids = fields.One2many(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="wizard.update.charts.accounts.account",
|
|
|
|
inverse_name="update_chart_wizard_id",
|
|
|
|
string="Accounts",
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
fiscal_position_ids = fields.One2many(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="wizard.update.charts.accounts.fiscal.position",
|
|
|
|
inverse_name="update_chart_wizard_id",
|
|
|
|
string="Fiscal positions",
|
|
|
|
)
|
2022-02-23 11:20:30 +01:00
|
|
|
new_taxes = fields.Integer(compute="_compute_new_taxes_count")
|
|
|
|
new_accounts = fields.Integer(compute="_compute_new_accounts_count")
|
2018-09-18 14:41:19 +02:00
|
|
|
rejected_new_account_number = fields.Integer()
|
2015-03-28 14:56:47 +01:00
|
|
|
new_fps = fields.Integer(
|
2020-01-08 21:52:03 +01:00
|
|
|
string="New fiscal positions", compute="_compute_new_fps_count"
|
|
|
|
)
|
2022-02-23 11:20:30 +01:00
|
|
|
updated_taxes = fields.Integer(compute="_compute_updated_taxes_count")
|
2018-09-18 14:41:19 +02:00
|
|
|
rejected_updated_account_number = fields.Integer()
|
2022-02-23 11:20:30 +01:00
|
|
|
updated_accounts = fields.Integer(compute="_compute_updated_accounts_count")
|
2015-03-28 14:56:47 +01:00
|
|
|
updated_fps = fields.Integer(
|
2020-01-08 21:52:03 +01:00
|
|
|
string="Updated fiscal positions", compute="_compute_updated_fps_count"
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
deleted_taxes = fields.Integer(
|
2020-01-08 21:52:03 +01:00
|
|
|
string="Deactivated taxes", compute="_compute_deleted_taxes_count"
|
|
|
|
)
|
|
|
|
log = fields.Text(string="Messages and Errors", readonly=True)
|
2018-09-22 03:28:30 +02:00
|
|
|
tax_field_ids = fields.Many2many(
|
|
|
|
comodel_name="ir.model.fields",
|
|
|
|
relation="wizard_update_charts_tax_fields_rel",
|
|
|
|
string="Tax fields",
|
|
|
|
domain=lambda self: self._domain_tax_field_ids(),
|
|
|
|
default=lambda self: self._default_tax_field_ids(),
|
|
|
|
)
|
|
|
|
account_field_ids = fields.Many2many(
|
|
|
|
comodel_name="ir.model.fields",
|
|
|
|
relation="wizard_update_charts_account_fields_rel",
|
|
|
|
string="Account fields",
|
|
|
|
domain=lambda self: self._domain_account_field_ids(),
|
|
|
|
default=lambda self: self._default_account_field_ids(),
|
|
|
|
)
|
|
|
|
fp_field_ids = fields.Many2many(
|
|
|
|
comodel_name="ir.model.fields",
|
|
|
|
relation="wizard_update_charts_fp_fields_rel",
|
|
|
|
string="Fiscal position fields",
|
|
|
|
domain=lambda self: self._domain_fp_field_ids(),
|
|
|
|
default=lambda self: self._default_fp_field_ids(),
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
tax_matching_ids = fields.One2many(
|
|
|
|
comodel_name="wizard.tax.matching",
|
|
|
|
inverse_name="update_chart_wizard_id",
|
|
|
|
string="Taxes matching",
|
|
|
|
default=lambda self: self._default_tax_matching_ids(),
|
|
|
|
)
|
|
|
|
account_matching_ids = fields.One2many(
|
|
|
|
comodel_name="wizard.account.matching",
|
|
|
|
inverse_name="update_chart_wizard_id",
|
2019-03-15 21:25:19 +01:00
|
|
|
string="Accounts matching",
|
2019-01-29 18:07:29 +01:00
|
|
|
default=lambda self: self._default_account_matching_ids(),
|
|
|
|
)
|
|
|
|
fp_matching_ids = fields.One2many(
|
|
|
|
comodel_name="wizard.fp.matching",
|
|
|
|
inverse_name="update_chart_wizard_id",
|
2019-03-15 21:25:19 +01:00
|
|
|
string="Fiscal positions matching",
|
2019-01-29 18:07:29 +01:00
|
|
|
default=lambda self: self._default_fp_matching_ids(),
|
|
|
|
)
|
2018-09-22 03:28:30 +02:00
|
|
|
|
|
|
|
def _domain_per_name(self, name):
|
|
|
|
return [
|
2020-01-08 21:52:03 +01:00
|
|
|
("model", "=", name),
|
|
|
|
("name", "not in", tuple(self.fields_to_ignore(name))),
|
2018-09-22 03:28:30 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
def _domain_tax_field_ids(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
return self._domain_per_name("account.tax.template")
|
2018-09-22 03:28:30 +02:00
|
|
|
|
|
|
|
def _domain_account_field_ids(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
return self._domain_per_name("account.account.template")
|
2018-09-22 03:28:30 +02:00
|
|
|
|
|
|
|
def _domain_fp_field_ids(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
return self._domain_per_name("account.fiscal.position.template")
|
2018-09-22 03:28:30 +02:00
|
|
|
|
|
|
|
def _default_tax_field_ids(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
return [
|
|
|
|
(4, x.id)
|
|
|
|
for x in self.env["ir.model.fields"].search(self._domain_tax_field_ids())
|
|
|
|
]
|
2018-09-22 03:28:30 +02:00
|
|
|
|
|
|
|
def _default_account_field_ids(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
return [
|
|
|
|
(4, x.id)
|
|
|
|
for x in self.env["ir.model.fields"].search(
|
|
|
|
self._domain_account_field_ids()
|
|
|
|
)
|
|
|
|
]
|
2018-09-22 03:28:30 +02:00
|
|
|
|
|
|
|
def _default_fp_field_ids(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
return [
|
|
|
|
(4, x.id)
|
|
|
|
for x in self.env["ir.model.fields"].search(self._domain_fp_field_ids())
|
|
|
|
]
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2019-01-29 18:07:29 +01:00
|
|
|
def _get_matching_ids(self, model_name, ordered_opts):
|
|
|
|
vals = []
|
|
|
|
for seq, opt in enumerate(ordered_opts, 1):
|
2020-01-08 21:52:03 +01:00
|
|
|
vals.append((0, False, {"sequence": seq, "matching_value": opt}))
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
all_options = self.env[model_name]._get_matching_selection()
|
|
|
|
all_options = map(lambda x: x[0], all_options)
|
|
|
|
all_options = list(set(all_options) - set(ordered_opts))
|
|
|
|
|
|
|
|
for seq, opt in enumerate(all_options, len(ordered_opts) + 1):
|
2020-01-08 21:52:03 +01:00
|
|
|
vals.append((0, False, {"sequence": seq, "matching_value": opt}))
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
return vals
|
|
|
|
|
|
|
|
def _default_fp_matching_ids(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
ordered_opts = ["xml_id", "name"]
|
|
|
|
return self._get_matching_ids("wizard.fp.matching", ordered_opts)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
def _default_tax_matching_ids(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
ordered_opts = ["xml_id", "description", "name"]
|
|
|
|
return self._get_matching_ids("wizard.tax.matching", ordered_opts)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
def _default_account_matching_ids(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
ordered_opts = ["xml_id", "code", "name"]
|
|
|
|
return self._get_matching_ids("wizard.account.matching", ordered_opts)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
@api.model
|
|
|
|
def _get_lang_selection_options(self):
|
|
|
|
"""Gets the available languages for the selection."""
|
2020-01-08 21:52:03 +01:00
|
|
|
langs = self.env["res.lang"].search([])
|
2016-05-25 14:05:38 +02:00
|
|
|
return [(lang.code, lang.name) for lang in langs]
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
@api.depends("chart_template_id")
|
|
|
|
def _compute_chart_template_ids(self):
|
2019-03-15 21:25:19 +01:00
|
|
|
all_parents = self.chart_template_id._get_chart_parent_ids()
|
|
|
|
self.chart_template_ids = all_parents
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2020-01-08 21:52:03 +01:00
|
|
|
@api.depends("tax_ids")
|
2016-05-25 14:05:38 +02:00
|
|
|
def _compute_new_taxes_count(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
self.new_taxes = len(self.tax_ids.filtered(lambda x: x.type == "new"))
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2020-01-08 21:52:03 +01:00
|
|
|
@api.depends("account_ids")
|
2016-05-25 14:05:38 +02:00
|
|
|
def _compute_new_accounts_count(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
self.new_accounts = (
|
|
|
|
len(self.account_ids.filtered(lambda x: x.type == "new"))
|
|
|
|
- self.rejected_new_account_number
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2020-01-08 21:52:03 +01:00
|
|
|
@api.depends("fiscal_position_ids")
|
2016-05-25 14:05:38 +02:00
|
|
|
def _compute_new_fps_count(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
self.new_fps = len(self.fiscal_position_ids.filtered(lambda x: x.type == "new"))
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2020-01-08 21:52:03 +01:00
|
|
|
@api.depends("tax_ids")
|
2016-05-25 14:05:38 +02:00
|
|
|
def _compute_updated_taxes_count(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
self.updated_taxes = len(self.tax_ids.filtered(lambda x: x.type == "updated"))
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2020-01-08 21:52:03 +01:00
|
|
|
@api.depends("account_ids")
|
2016-05-25 14:05:38 +02:00
|
|
|
def _compute_updated_accounts_count(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
self.updated_accounts = (
|
|
|
|
len(self.account_ids.filtered(lambda x: x.type == "updated"))
|
|
|
|
- self.rejected_updated_account_number
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2020-01-08 21:52:03 +01:00
|
|
|
@api.depends("fiscal_position_ids")
|
2016-05-25 14:05:38 +02:00
|
|
|
def _compute_updated_fps_count(self):
|
2015-03-28 14:56:47 +01:00
|
|
|
self.updated_fps = len(
|
2020-01-08 21:52:03 +01:00
|
|
|
self.fiscal_position_ids.filtered(lambda x: x.type == "updated")
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2020-01-08 21:52:03 +01:00
|
|
|
@api.depends("tax_ids")
|
2016-05-25 14:05:38 +02:00
|
|
|
def _compute_deleted_taxes_count(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
self.deleted_taxes = len(self.tax_ids.filtered(lambda x: x.type == "deleted"))
|
2013-10-01 18:28:19 +02:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
@api.onchange("company_id")
|
|
|
|
def _onchage_company_update_chart_template(self):
|
|
|
|
self.chart_template_id = self.company_id.chart_template_id
|
2013-10-01 18:28:19 +02:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
def _reopen(self):
|
|
|
|
return {
|
2020-01-08 21:52:03 +01:00
|
|
|
"type": "ir.actions.act_window",
|
|
|
|
"view_mode": "form",
|
|
|
|
"res_id": self.id,
|
|
|
|
"res_model": self._name,
|
|
|
|
"target": "new",
|
2016-05-25 14:05:38 +02:00
|
|
|
# save original model in context,
|
|
|
|
# because selecting the list of available
|
|
|
|
# templates requires a model in context
|
2020-01-08 21:52:03 +01:00
|
|
|
"context": {"default_model": self._name},
|
2016-05-25 14:05:38 +02:00
|
|
|
}
|
|
|
|
|
2015-03-28 14:56:47 +01:00
|
|
|
def action_init(self):
|
|
|
|
"""Initial action that sets the initial state."""
|
2020-01-08 21:52:03 +01:00
|
|
|
self.write(
|
|
|
|
{
|
|
|
|
"state": "init",
|
|
|
|
"tax_ids": [(2, r.id, False) for r in self.tax_ids],
|
|
|
|
"account_ids": [(2, r.id, False) for r in self.account_ids],
|
|
|
|
"fiscal_position_ids": [
|
|
|
|
(2, r.id, False) for r in self.fiscal_position_ids
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
2016-05-25 14:05:38 +02:00
|
|
|
return self._reopen()
|
2015-03-28 14:56:47 +01:00
|
|
|
|
|
|
|
def action_find_records(self):
|
|
|
|
"""Searchs for records to update/create and shows them."""
|
2020-01-30 22:26:56 +01:00
|
|
|
self.clear_caches()
|
2015-03-28 14:56:47 +01:00
|
|
|
self = self.with_context(lang=self.lang)
|
|
|
|
# Search for, and load, the records to create/update.
|
|
|
|
if self.update_tax:
|
2016-05-25 14:05:38 +02:00
|
|
|
self._find_taxes()
|
2015-03-28 14:56:47 +01:00
|
|
|
if self.update_account:
|
2016-05-25 14:05:38 +02:00
|
|
|
self._find_accounts()
|
2015-03-28 14:56:47 +01:00
|
|
|
if self.update_fiscal_position:
|
2016-05-25 14:05:38 +02:00
|
|
|
self._find_fiscal_positions()
|
2015-03-28 14:56:47 +01:00
|
|
|
# Write the results, and go to the next step.
|
2020-01-08 21:52:03 +01:00
|
|
|
self.state = "ready"
|
2016-05-25 14:05:38 +02:00
|
|
|
return self._reopen()
|
2013-10-01 18:28:19 +02:00
|
|
|
|
2020-03-17 20:00:09 +01:00
|
|
|
def _check_consistency(self):
|
|
|
|
"""Method for assuring consistency in operations before performing
|
|
|
|
them. For now, implemented:
|
|
|
|
|
|
|
|
- If a parent tax is tried to be created, children taxes must be
|
|
|
|
also included to be created.
|
|
|
|
|
|
|
|
TODO:
|
|
|
|
|
|
|
|
- Check that needed accounts in taxes/FPs are created at the same time.
|
|
|
|
- Check that needed taxes in FPs are created at the same time.
|
|
|
|
"""
|
|
|
|
taxes2create = self.tax_ids.filtered(lambda x: x.type == "new")
|
|
|
|
parents2create = taxes2create.filtered(lambda x: x.tax_id.children_tax_ids)
|
|
|
|
for parent in parents2create:
|
|
|
|
if bool(
|
|
|
|
parent.tax_id.children_tax_ids - taxes2create.mapped("tax_id")
|
|
|
|
): # some children taxes are not included to be added
|
|
|
|
raise exceptions.UserError(
|
|
|
|
_(
|
|
|
|
"You have at least one parent tax template (%s) whose "
|
|
|
|
"children taxes are not going to be created. Aborting "
|
|
|
|
"as this will provoke an infinite loop. Please check "
|
|
|
|
"if children have been matched, but not the parent one."
|
|
|
|
)
|
|
|
|
% parent.tax_id.name
|
|
|
|
)
|
|
|
|
|
2015-03-28 14:56:47 +01:00
|
|
|
def action_update_records(self):
|
2016-05-25 14:05:38 +02:00
|
|
|
"""Action that creates/updates/deletes the selected elements."""
|
2020-03-17 20:00:09 +01:00
|
|
|
self._check_consistency()
|
2016-05-25 14:05:38 +02:00
|
|
|
self = self.with_context(lang=self.lang)
|
2018-09-18 14:41:19 +02:00
|
|
|
self.rejected_new_account_number = 0
|
|
|
|
self.rejected_updated_account_number = 0
|
2016-05-25 14:05:38 +02:00
|
|
|
with closing(StringIO()) as log_output:
|
|
|
|
handler = logging.StreamHandler(log_output)
|
|
|
|
_logger.addHandler(handler)
|
|
|
|
# Create or update the records.
|
|
|
|
if self.update_tax:
|
2020-09-15 12:32:42 +02:00
|
|
|
todo_dict = self._update_taxes()
|
2018-09-18 14:41:19 +02:00
|
|
|
perform_rest = True
|
2016-05-25 14:05:38 +02:00
|
|
|
if self.update_account:
|
|
|
|
self._update_accounts()
|
2020-01-08 21:52:03 +01:00
|
|
|
if (
|
|
|
|
EXCEPTION_TEXT in log_output.getvalue()
|
|
|
|
and not self.continue_on_errors
|
|
|
|
): # Abort early
|
2018-09-18 14:41:19 +02:00
|
|
|
perform_rest = False
|
|
|
|
# Clear this cache for avoiding incorrect account hits (as it was
|
|
|
|
# queried before account creation)
|
|
|
|
self.find_account_by_templates.clear_cache(self)
|
|
|
|
if self.update_tax and perform_rest:
|
2020-09-15 12:32:42 +02:00
|
|
|
self._update_taxes_pending_for_accounts(todo_dict)
|
2018-09-18 14:41:19 +02:00
|
|
|
if self.update_fiscal_position and perform_rest:
|
2016-05-25 14:05:38 +02:00
|
|
|
self._update_fiscal_positions()
|
|
|
|
# Store new chart in the company
|
|
|
|
self.company_id.chart_template_id = self.chart_template_id
|
|
|
|
_logger.removeHandler(handler)
|
|
|
|
self.log = log_output.getvalue()
|
|
|
|
# Check if errors where detected and wether we should stop.
|
2018-09-18 14:41:19 +02:00
|
|
|
if EXCEPTION_TEXT in self.log and not self.continue_on_errors:
|
2022-02-11 18:44:26 +01:00
|
|
|
raise exceptions.UserError(
|
|
|
|
_("One or more errors detected!\n\n%s") % self.log
|
|
|
|
)
|
2016-05-25 14:05:38 +02:00
|
|
|
# Store the data and go to the next step.
|
2020-01-08 21:52:03 +01:00
|
|
|
self.state = "done"
|
2016-05-25 14:05:38 +02:00
|
|
|
return self._reopen()
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2019-01-29 18:07:29 +01:00
|
|
|
def _get_real_xml_name(self, template):
|
|
|
|
[external_id] = template.get_external_id().values()
|
2020-01-08 21:52:03 +01:00
|
|
|
(name, module) = external_id.split(".")
|
2019-01-29 18:07:29 +01:00
|
|
|
return "%s.%d_%s" % (name, self.company_id.id, module)
|
|
|
|
|
2020-01-17 17:33:28 +01:00
|
|
|
@tools.ormcache("templates")
|
|
|
|
def find_taxes_by_templates(self, templates):
|
|
|
|
tax_ids = []
|
|
|
|
for tax in templates:
|
2022-04-12 18:20:12 +02:00
|
|
|
tax_id = self.find_tax_by_templates(tax)
|
|
|
|
if tax_id:
|
|
|
|
tax_ids.append(tax_id)
|
2020-01-17 17:33:28 +01:00
|
|
|
return self.env["account.tax"].browse(tax_ids)
|
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
@tools.ormcache("templates")
|
|
|
|
def find_tax_by_templates(self, templates):
|
|
|
|
"""Find a tax that matches the template."""
|
2015-03-28 14:56:47 +01:00
|
|
|
# search inactive taxes too, to avoid re-creating
|
|
|
|
# taxes that have been deactivated before
|
2020-01-08 21:52:03 +01:00
|
|
|
tax_model = self.env["account.tax"].with_context(active_test=False)
|
2016-05-25 14:05:38 +02:00
|
|
|
for template in templates:
|
2020-01-08 21:52:03 +01:00
|
|
|
for matching in self.tax_matching_ids.sorted("sequence"):
|
|
|
|
if matching.matching_value == "xml_id":
|
|
|
|
real = self.env.ref(
|
|
|
|
self._get_real_xml_name(template), raise_if_not_found=False
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
if not real:
|
|
|
|
continue
|
2020-01-08 21:52:03 +01:00
|
|
|
criteria = ("id", "=", real.id)
|
2019-01-29 18:07:29 +01:00
|
|
|
else:
|
|
|
|
field_name = matching.matching_value
|
|
|
|
if not template[field_name]:
|
|
|
|
continue
|
2020-01-08 21:52:03 +01:00
|
|
|
criteria = (field_name, "=", template[field_name])
|
|
|
|
|
|
|
|
result = tax_model.search(
|
|
|
|
[
|
|
|
|
criteria,
|
|
|
|
("company_id", "=", self.company_id.id),
|
|
|
|
("type_tax_use", "=", template.type_tax_use),
|
|
|
|
],
|
|
|
|
limit=1,
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
if result:
|
|
|
|
return result.id
|
|
|
|
|
|
|
|
return False
|
2016-05-25 14:05:38 +02:00
|
|
|
|
2020-01-17 17:33:28 +01:00
|
|
|
@tools.ormcache("templates", "current_repartition")
|
|
|
|
def find_repartition_by_templates(
|
2020-09-15 12:32:42 +02:00
|
|
|
self, templates, current_repartition, tax, inverse_name
|
2020-01-17 17:33:28 +01:00
|
|
|
):
|
2020-09-15 12:32:42 +02:00
|
|
|
upd_acc = self.update_tax_repartition_line_account
|
|
|
|
upd_tags = self.update_tax_repartition_line_tags
|
2020-01-17 17:33:28 +01:00
|
|
|
result = []
|
2020-09-15 12:32:42 +02:00
|
|
|
existing_ids = []
|
|
|
|
for i, tpl in enumerate(templates):
|
2020-01-17 17:33:28 +01:00
|
|
|
factor_percent = tpl.factor_percent
|
|
|
|
repartition_type = tpl.repartition_type
|
|
|
|
account_id = self.find_account_by_templates(tpl.account_id)
|
2020-09-15 12:32:42 +02:00
|
|
|
tags = self.env["account.account.tag"]
|
2023-01-18 11:56:15 +01:00
|
|
|
plus_expressions = tpl.plus_report_expression_ids.filtered(
|
|
|
|
lambda x: x.engine == "tax_tags"
|
2020-01-17 17:33:28 +01:00
|
|
|
)
|
2023-01-18 11:56:15 +01:00
|
|
|
for expression in plus_expressions:
|
|
|
|
country = expression.report_line_id.report_id.country_id
|
|
|
|
existing_tags = self.env["account.account.tag"]._get_tax_tags(
|
|
|
|
expression.formula, country.id
|
|
|
|
)
|
|
|
|
tags |= existing_tags.filtered(lambda x: not x.tax_negate)
|
|
|
|
minus_expressions = tpl.minus_report_expression_ids.filtered(
|
|
|
|
lambda x: x.engine == "tax_tags"
|
2020-09-15 12:32:42 +02:00
|
|
|
)
|
2023-01-18 11:56:15 +01:00
|
|
|
for expression in minus_expressions:
|
|
|
|
country = expression.report_line_id.report_id.country_id
|
|
|
|
existing_tags = self.env["account.account.tag"]._get_tax_tags(
|
|
|
|
expression.formula, country.id
|
|
|
|
)
|
|
|
|
tags |= existing_tags.filtered(lambda x: x.tax_negate)
|
2020-09-15 12:32:42 +02:00
|
|
|
tags += tpl.tag_ids
|
|
|
|
existing = self.env["account.tax.repartition.line"]
|
|
|
|
existing_candidates = current_repartition.filtered(
|
|
|
|
lambda r: r.factor_percent == factor_percent
|
|
|
|
and r.repartition_type == repartition_type
|
2022-03-24 17:59:37 +01:00
|
|
|
and r.id not in existing_ids
|
2020-09-15 12:32:42 +02:00
|
|
|
)
|
|
|
|
if len(existing_candidates) == 1:
|
|
|
|
existing = existing_candidates
|
|
|
|
elif len(existing_candidates) > 1:
|
|
|
|
# We may have this situation in case of e.g. 50%/50% on tax.
|
|
|
|
# In this case we assume that the repartition line order
|
|
|
|
# is the same between templates and actual tax objects
|
|
|
|
existing_candidate = current_repartition[i]
|
|
|
|
if existing_candidate in existing_candidates:
|
|
|
|
existing = existing_candidate
|
|
|
|
|
|
|
|
if existing:
|
|
|
|
existing_ids.append(existing.id)
|
|
|
|
upd_vals = {}
|
|
|
|
if upd_acc and existing.account_id.id != account_id:
|
|
|
|
upd_vals["account_id"] = account_id
|
|
|
|
if upd_tags:
|
|
|
|
if existing.tag_ids != tags:
|
|
|
|
upd_vals["tag_ids"] = [(6, 0, tags.ids)]
|
|
|
|
if upd_vals:
|
|
|
|
# update record
|
|
|
|
result.append((1, existing.id, upd_vals))
|
2020-01-17 17:33:28 +01:00
|
|
|
if not existing:
|
|
|
|
# create a new mapping
|
|
|
|
result.append(
|
|
|
|
(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
{
|
2020-09-15 12:32:42 +02:00
|
|
|
inverse_name: tax.id,
|
2020-01-17 17:33:28 +01:00
|
|
|
"factor_percent": factor_percent,
|
|
|
|
"repartition_type": repartition_type,
|
|
|
|
"account_id": account_id,
|
2020-09-15 12:32:42 +02:00
|
|
|
"tag_ids": [(6, 0, tags.ids)],
|
2020-01-17 17:33:28 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2020-09-15 12:32:42 +02:00
|
|
|
|
2022-04-12 18:20:12 +02:00
|
|
|
if tax.amount_type != "group":
|
|
|
|
# Mark to be removed the lines not found
|
|
|
|
remove_ids = [x for x in current_repartition.ids if x not in existing_ids]
|
|
|
|
result += [(2, x) for x in remove_ids]
|
2020-01-17 17:33:28 +01:00
|
|
|
return result
|
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
@api.model
|
|
|
|
@tools.ormcache("code")
|
|
|
|
def padded_code(self, code):
|
|
|
|
"""Return a right-zero-padded code with the chosen digits."""
|
2020-01-08 21:52:03 +01:00
|
|
|
return code.ljust(self.code_digits, "0")
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2020-01-17 17:33:28 +01:00
|
|
|
@tools.ormcache("templates")
|
|
|
|
def find_accounts_by_templates(self, templates):
|
|
|
|
account_ids = []
|
|
|
|
for account in templates:
|
2022-08-02 18:23:05 +02:00
|
|
|
account_ids.append(self.find_account_by_templates(account))
|
2020-01-17 17:33:28 +01:00
|
|
|
return self.env["account.account"].browse(account_ids)
|
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
@tools.ormcache("templates")
|
|
|
|
def find_account_by_templates(self, templates):
|
|
|
|
"""Find an account that matches the template."""
|
2020-01-08 21:52:03 +01:00
|
|
|
account_model = self.env["account.account"]
|
|
|
|
for matching in self.account_matching_ids.sorted("sequence"):
|
|
|
|
if matching.matching_value == "xml_id":
|
|
|
|
real = self.env["account.account"]
|
2019-01-29 18:07:29 +01:00
|
|
|
for template in templates:
|
|
|
|
try:
|
|
|
|
real |= self.env.ref(self._get_real_xml_name(template))
|
2020-01-17 17:33:28 +01:00
|
|
|
except BaseException:
|
2022-02-23 11:20:30 +01:00
|
|
|
_logger.info("Is not real xml Name")
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
if not real:
|
|
|
|
continue
|
2020-01-08 21:52:03 +01:00
|
|
|
criteria = ("id", "in", real.ids)
|
|
|
|
elif matching.matching_value == "code":
|
2019-01-29 18:07:29 +01:00
|
|
|
codes = templates.mapped("code")
|
|
|
|
if not codes:
|
|
|
|
continue
|
2020-01-08 21:52:03 +01:00
|
|
|
criteria = ("code", "in", list(map(self.padded_code, codes)))
|
2019-01-29 18:07:29 +01:00
|
|
|
else:
|
|
|
|
field_name = matching.matching_value
|
|
|
|
field_values = templates.mapped(field_name)
|
|
|
|
if not field_values:
|
|
|
|
continue
|
2020-01-08 21:52:03 +01:00
|
|
|
criteria = (field_name, "in", field_values)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
result = account_model.search(
|
2020-01-08 21:52:03 +01:00
|
|
|
[criteria, ("company_id", "=", self.company_id.id)]
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
if result:
|
|
|
|
return result.id
|
|
|
|
|
|
|
|
return False
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
@tools.ormcache("templates")
|
|
|
|
def find_fp_by_templates(self, templates):
|
|
|
|
"""Find a real fiscal position from a template."""
|
2020-01-08 21:52:03 +01:00
|
|
|
fp_model = self.env["account.fiscal.position"]
|
|
|
|
for matching in self.fp_matching_ids.sorted("sequence"):
|
|
|
|
if matching.matching_value == "xml_id":
|
|
|
|
real = self.env["account.fiscal.position"]
|
2019-01-29 18:07:29 +01:00
|
|
|
for template in templates:
|
|
|
|
try:
|
|
|
|
real |= self.env.ref(self._get_real_xml_name(template))
|
2020-01-17 17:33:28 +01:00
|
|
|
except BaseException:
|
2022-02-23 11:20:30 +01:00
|
|
|
_logger.info("Is not real xml Name")
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
if not real:
|
|
|
|
continue
|
2020-01-08 21:52:03 +01:00
|
|
|
criteria = ("id", "in", real.ids)
|
2019-01-29 18:07:29 +01:00
|
|
|
else:
|
|
|
|
field_name = matching.matching_value
|
|
|
|
field_values = templates.mapped(field_name)
|
|
|
|
if not field_values:
|
|
|
|
continue
|
2020-01-08 21:52:03 +01:00
|
|
|
criteria = (field_name, "in", field_values)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
2020-01-08 21:52:03 +01:00
|
|
|
result = fp_model.search(
|
|
|
|
[criteria, ("company_id", "=", self.company_id.id)], limit=1
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
if result:
|
|
|
|
return result.id
|
|
|
|
|
|
|
|
return False
|
2016-05-25 14:05:38 +02:00
|
|
|
|
2017-08-14 19:36:35 +02:00
|
|
|
@tools.ormcache("templates", "current_fp_accounts")
|
|
|
|
def find_fp_account_by_templates(self, templates, current_fp_accounts):
|
2016-05-25 14:05:38 +02:00
|
|
|
result = []
|
|
|
|
for tpl in templates:
|
2018-09-18 14:59:54 +02:00
|
|
|
pos_id = self.find_fp_by_templates(tpl.position_id)
|
|
|
|
src_id = self.find_account_by_templates(tpl.account_src_id)
|
|
|
|
dest_id = self.find_account_by_templates(tpl.account_dest_id)
|
2020-01-08 21:52:03 +01:00
|
|
|
existing = self.env["account.fiscal.position.account"].search(
|
|
|
|
[
|
|
|
|
("position_id", "=", pos_id),
|
|
|
|
("account_src_id", "=", src_id),
|
|
|
|
("account_dest_id", "=", dest_id),
|
|
|
|
]
|
|
|
|
)
|
2017-08-14 19:36:35 +02:00
|
|
|
if not existing:
|
2016-05-25 14:05:38 +02:00
|
|
|
# create a new mapping
|
2020-01-08 21:52:03 +01:00
|
|
|
result.append(
|
|
|
|
(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
{
|
|
|
|
"position_id": pos_id,
|
|
|
|
"account_src_id": src_id,
|
|
|
|
"account_dest_id": dest_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2017-08-14 19:36:35 +02:00
|
|
|
else:
|
|
|
|
current_fp_accounts -= existing
|
|
|
|
# Mark to be removed the lines not found
|
|
|
|
if current_fp_accounts:
|
|
|
|
result += [(2, x.id) for x in current_fp_accounts]
|
2016-05-25 14:05:38 +02:00
|
|
|
return result
|
|
|
|
|
2017-08-14 19:36:35 +02:00
|
|
|
@tools.ormcache("templates", "current_fp_taxes")
|
|
|
|
def find_fp_tax_by_templates(self, templates, current_fp_taxes):
|
2016-05-25 14:05:38 +02:00
|
|
|
result = []
|
|
|
|
for tpl in templates:
|
2018-09-18 14:59:54 +02:00
|
|
|
pos_id = self.find_fp_by_templates(tpl.position_id)
|
|
|
|
src_id = self.find_tax_by_templates(tpl.tax_src_id)
|
|
|
|
dest_id = self.find_tax_by_templates(tpl.tax_dest_id)
|
2020-01-08 21:52:03 +01:00
|
|
|
existing = self.env["account.fiscal.position.tax"].search(
|
|
|
|
[
|
|
|
|
("position_id", "=", pos_id),
|
|
|
|
("tax_src_id", "=", src_id),
|
|
|
|
("tax_dest_id", "=", dest_id),
|
|
|
|
]
|
|
|
|
)
|
2017-08-14 19:36:35 +02:00
|
|
|
if not existing:
|
2016-05-25 14:05:38 +02:00
|
|
|
# create a new mapping
|
2020-01-08 21:52:03 +01:00
|
|
|
result.append(
|
|
|
|
(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
{
|
|
|
|
"position_id": pos_id,
|
|
|
|
"tax_src_id": src_id,
|
|
|
|
"tax_dest_id": dest_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2017-08-14 19:36:35 +02:00
|
|
|
else:
|
|
|
|
current_fp_taxes -= existing
|
|
|
|
# Mark to be removed the lines not found
|
|
|
|
if current_fp_taxes:
|
|
|
|
result += [(2, x.id) for x in current_fp_taxes]
|
2016-05-25 14:05:38 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
@api.model
|
2018-09-18 14:41:19 +02:00
|
|
|
@tools.ormcache("name")
|
2018-09-22 03:28:30 +02:00
|
|
|
def fields_to_ignore(self, name):
|
2016-05-25 14:05:38 +02:00
|
|
|
"""Get fields that will not be used when checking differences.
|
|
|
|
|
2018-09-18 14:41:19 +02:00
|
|
|
:param str template: A template record.
|
|
|
|
:param str name: The name of the template model.
|
|
|
|
:return set: Fields to ignore in diff.
|
2014-03-21 02:01:05 +01:00
|
|
|
"""
|
2018-09-18 14:41:19 +02:00
|
|
|
specials_mapping = {
|
2020-01-08 21:52:03 +01:00
|
|
|
"account.tax.template": {"chart_template_id", "children_tax_ids"},
|
2022-02-23 11:20:30 +01:00
|
|
|
"account.account.template": set(self.env["mail.thread"]._fields)
|
|
|
|
| {"chart_template_id", "root_id", "nocreate"},
|
2020-01-08 21:52:03 +01:00
|
|
|
"account.fiscal.position.template": {"chart_template_id"},
|
2017-08-14 19:36:35 +02:00
|
|
|
}
|
2020-01-08 21:52:03 +01:00
|
|
|
specials = {
|
|
|
|
"display_name",
|
|
|
|
"__last_update",
|
|
|
|
"company_id",
|
|
|
|
} | specials_mapping.get(name, set())
|
2016-05-25 14:05:38 +02:00
|
|
|
return set(models.MAGIC_COLUMNS) | specials
|
|
|
|
|
|
|
|
@api.model
|
2023-01-11 11:19:31 +01:00
|
|
|
def diff_fields(self, template, real): # noqa: C901
|
2016-05-25 14:05:38 +02:00
|
|
|
"""Get fields that are different in template and real records.
|
|
|
|
|
2017-03-14 11:54:08 +01:00
|
|
|
:param odoo.models.Model template:
|
2016-05-25 14:05:38 +02:00
|
|
|
Template record.
|
2017-03-14 11:54:08 +01:00
|
|
|
:param odoo.models.Model real:
|
2016-05-25 14:05:38 +02:00
|
|
|
Real record.
|
|
|
|
|
|
|
|
:return dict:
|
|
|
|
Fields that are different in both records, and the expected value.
|
2014-03-21 02:01:05 +01:00
|
|
|
"""
|
2016-05-25 14:05:38 +02:00
|
|
|
result = dict()
|
2018-09-22 03:28:30 +02:00
|
|
|
ignore = self.fields_to_ignore(template._name)
|
2020-01-17 17:33:28 +01:00
|
|
|
template_field_mapping = {
|
|
|
|
"account.tax.template": self.tax_field_ids,
|
|
|
|
"account.account.template": self.account_field_ids,
|
|
|
|
"account.fiscal.position.template": self.fp_field_ids,
|
|
|
|
}
|
|
|
|
to_include = template_field_mapping[template._name].mapped("name")
|
2018-09-21 03:06:39 +02:00
|
|
|
for key, field in template._fields.items():
|
2020-10-31 17:19:19 +01:00
|
|
|
if key in ignore or key not in to_include or not hasattr(real, key):
|
2016-05-25 14:05:38 +02:00
|
|
|
continue
|
2020-01-17 17:33:28 +01:00
|
|
|
expected = None
|
2016-05-25 14:05:38 +02:00
|
|
|
# Translate template records to reals for comparison
|
2018-09-18 14:41:19 +02:00
|
|
|
relation = field.get_description(self.env).get("relation", "")
|
|
|
|
if relation:
|
2020-01-17 17:33:28 +01:00
|
|
|
if relation == "account.tax.template":
|
|
|
|
expected = self.find_taxes_by_templates(template[key])
|
|
|
|
elif relation == "account.account.template":
|
|
|
|
expected = self.find_accounts_by_templates(template[key])
|
|
|
|
elif relation == "account.fiscal.position.tax.template":
|
|
|
|
expected = self.find_fp_tax_by_templates(template[key], real[key])
|
|
|
|
elif relation == "account.fiscal.position.account.template":
|
|
|
|
expected = self.find_fp_account_by_templates(
|
|
|
|
template[key], real[key]
|
|
|
|
)
|
|
|
|
elif relation == "account.tax.repartition.line.template":
|
|
|
|
expected = self.find_repartition_by_templates(
|
2020-09-15 12:32:42 +02:00
|
|
|
template[key], real[key], real, field.inverse_name
|
2020-01-08 21:52:03 +01:00
|
|
|
)
|
2016-05-25 14:05:38 +02:00
|
|
|
# Register detected differences
|
2020-01-17 17:33:28 +01:00
|
|
|
if expected is not None:
|
2020-09-15 12:32:42 +02:00
|
|
|
if expected != [] and (
|
|
|
|
key
|
|
|
|
in ["invoice_repartition_line_ids", "refund_repartition_line_ids"]
|
2023-01-18 11:56:15 +01:00
|
|
|
or (isinstance(expected, models.Model) and expected != real[key])
|
|
|
|
or (not isinstance(expected, models.Model))
|
2020-09-15 12:32:42 +02:00
|
|
|
):
|
2020-01-17 17:33:28 +01:00
|
|
|
result[key] = expected
|
|
|
|
else:
|
2023-01-11 11:19:31 +01:00
|
|
|
template_value, real_value = template[key], real[key]
|
2020-01-17 17:33:28 +01:00
|
|
|
if template._name == "account.account.template" and key == "code":
|
|
|
|
template_value = self.padded_code(template["code"])
|
2023-01-11 11:19:31 +01:00
|
|
|
# Normalize values when one field is Char and the other is Html
|
|
|
|
if (
|
|
|
|
isinstance(template_value, str) and isinstance(real_value, Markup)
|
|
|
|
) or (
|
|
|
|
isinstance(template_value, Markup) and isinstance(real_value, str)
|
|
|
|
):
|
|
|
|
template_value = Markup(template_value).striptags()
|
|
|
|
real_value = Markup(real_value).striptags()
|
|
|
|
if template_value != real_value:
|
2020-01-17 17:33:28 +01:00
|
|
|
result[key] = template_value
|
|
|
|
# Avoid to cache recordset references
|
|
|
|
if key in result:
|
2018-09-21 23:24:53 +02:00
|
|
|
if isinstance(real._fields[key], fields.Many2many):
|
|
|
|
result[key] = [(6, 0, result[key].ids)]
|
|
|
|
elif isinstance(real._fields[key], fields.Many2one):
|
2018-09-18 14:41:19 +02:00
|
|
|
result[key] = result[key].id
|
2016-05-25 14:05:38 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def diff_notes(self, template, real):
|
|
|
|
"""Get notes for humans on why is this record going to be updated.
|
|
|
|
|
|
|
|
:param openerp.models.Model template:
|
|
|
|
Template record.
|
|
|
|
|
|
|
|
:param openerp.models.Model real:
|
|
|
|
Real record.
|
|
|
|
|
|
|
|
:return str:
|
|
|
|
Notes result.
|
2014-03-21 02:01:05 +01:00
|
|
|
"""
|
2016-05-25 14:05:38 +02:00
|
|
|
result = list()
|
|
|
|
different_fields = sorted(
|
|
|
|
template._fields[f].get_description(self.env)["string"]
|
2020-01-08 21:52:03 +01:00
|
|
|
for f in self.diff_fields(template, real).keys()
|
|
|
|
)
|
2016-05-25 14:05:38 +02:00
|
|
|
if different_fields:
|
|
|
|
result.append(
|
2020-01-08 21:52:03 +01:00
|
|
|
_("Differences in these fields: %s.") % ", ".join(different_fields)
|
|
|
|
)
|
2020-09-07 09:52:31 +02:00
|
|
|
# Special for taxes
|
|
|
|
if template._name == "account.tax.template":
|
|
|
|
if not real.active:
|
|
|
|
result.append(_("Tax is disabled."))
|
2016-05-25 14:05:38 +02:00
|
|
|
return "\n".join(result)
|
|
|
|
|
2020-03-24 18:08:56 +01:00
|
|
|
@tools.ormcache("self", "template", "real_obj")
|
|
|
|
def missing_xml_id(self, template, real_obj):
|
|
|
|
ir_model_data = self.env["ir.model.data"]
|
|
|
|
template_xmlid = ir_model_data.search(
|
|
|
|
[("model", "=", template._name), ("res_id", "=", template.id)]
|
|
|
|
)
|
|
|
|
new_xml_id = "%d_%s" % (self.company_id.id, template_xmlid.name)
|
|
|
|
return not ir_model_data.search(
|
2020-01-08 21:52:03 +01:00
|
|
|
[
|
|
|
|
("res_id", "=", real_obj.id),
|
|
|
|
("model", "=", real_obj._name),
|
2020-03-24 18:08:56 +01:00
|
|
|
("module", "=", template_xmlid.module),
|
|
|
|
("name", "=", new_xml_id),
|
2020-01-08 21:52:03 +01:00
|
|
|
]
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
2022-10-24 13:28:50 +02:00
|
|
|
def _domain_taxes_to_deactivate(self, found_taxes_ids):
|
|
|
|
return [
|
|
|
|
("company_id", "=", self.company_id.id),
|
|
|
|
("id", "not in", found_taxes_ids),
|
|
|
|
("active", "=", True),
|
|
|
|
]
|
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
def _find_taxes(self):
|
|
|
|
"""Search for, and load, tax templates to create/update/delete."""
|
2018-09-18 14:59:54 +02:00
|
|
|
found_taxes_ids = []
|
2015-03-28 14:56:47 +01:00
|
|
|
self.tax_ids.unlink()
|
2016-05-25 14:05:38 +02:00
|
|
|
# Search for changes between template and real tax
|
2020-01-08 21:52:03 +01:00
|
|
|
for template in self.chart_template_ids.with_context(active_test=False).mapped(
|
|
|
|
"tax_template_ids"
|
|
|
|
):
|
2016-05-25 14:05:38 +02:00
|
|
|
# Check if the template matches a real tax
|
2018-09-18 14:59:54 +02:00
|
|
|
tax_id = self.find_tax_by_templates(template)
|
|
|
|
if not tax_id:
|
2016-05-25 14:05:38 +02:00
|
|
|
# Tax to be created
|
2020-01-08 21:52:03 +01:00
|
|
|
self.tax_ids.create(
|
|
|
|
{
|
|
|
|
"tax_id": template.id,
|
|
|
|
"update_chart_wizard_id": self.id,
|
|
|
|
"type": "new",
|
|
|
|
"notes": _("Name or description not found."),
|
|
|
|
}
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
else:
|
2018-09-18 14:59:54 +02:00
|
|
|
found_taxes_ids.append(tax_id)
|
2016-05-25 14:05:38 +02:00
|
|
|
# Check the tax for changes
|
2020-01-08 21:52:03 +01:00
|
|
|
tax = self.env["account.tax"].browse(tax_id)
|
2016-05-25 14:05:38 +02:00
|
|
|
notes = self.diff_notes(template, tax)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
2020-03-24 18:08:56 +01:00
|
|
|
if self.recreate_xml_ids and self.missing_xml_id(template, tax):
|
2019-01-29 18:07:29 +01:00
|
|
|
notes += (notes and "\n" or "") + _("Missing XML-ID.")
|
|
|
|
|
2015-03-28 14:56:47 +01:00
|
|
|
if notes:
|
2016-05-25 14:05:38 +02:00
|
|
|
# Tax to be updated
|
2020-01-08 21:52:03 +01:00
|
|
|
self.tax_ids.create(
|
|
|
|
{
|
|
|
|
"tax_id": template.id,
|
|
|
|
"update_chart_wizard_id": self.id,
|
|
|
|
"type": "updated",
|
|
|
|
"update_tax_id": tax_id,
|
|
|
|
"notes": notes,
|
|
|
|
}
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
# search for taxes not in the template and propose them for
|
|
|
|
# deactivation
|
2020-01-08 21:52:03 +01:00
|
|
|
taxes_to_deactivate = self.env["account.tax"].search(
|
2022-10-24 13:28:50 +02:00
|
|
|
self._domain_taxes_to_deactivate(found_taxes_ids)
|
2020-01-08 21:52:03 +01:00
|
|
|
)
|
2018-09-18 14:41:19 +02:00
|
|
|
for tax in taxes_to_deactivate:
|
2020-01-08 21:52:03 +01:00
|
|
|
self.tax_ids.create(
|
|
|
|
{
|
|
|
|
"update_chart_wizard_id": self.id,
|
|
|
|
"type": "deleted",
|
|
|
|
"update_tax_id": tax.id,
|
|
|
|
"notes": _("To deactivate: not in the template"),
|
|
|
|
}
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
def _find_accounts(self):
|
|
|
|
"""Load account templates to create/update."""
|
2016-02-21 20:05:33 +01:00
|
|
|
self.account_ids.unlink()
|
2016-05-25 14:05:38 +02:00
|
|
|
for template in self.chart_template_ids.mapped("account_ids"):
|
|
|
|
# Search for a real account that matches the template
|
2018-09-18 14:59:54 +02:00
|
|
|
account_id = self.find_account_by_templates(template)
|
|
|
|
if not account_id:
|
2016-05-25 14:05:38 +02:00
|
|
|
# Account to be created
|
2020-01-08 21:52:03 +01:00
|
|
|
self.account_ids.create(
|
|
|
|
{
|
|
|
|
"account_id": template.id,
|
|
|
|
"update_chart_wizard_id": self.id,
|
|
|
|
"type": "new",
|
|
|
|
"notes": _("No account found with this code."),
|
|
|
|
}
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
else:
|
2016-05-25 14:05:38 +02:00
|
|
|
# Check the account for changes
|
2020-01-08 21:52:03 +01:00
|
|
|
account = self.env["account.account"].browse(account_id)
|
2016-05-25 14:05:38 +02:00
|
|
|
notes = self.diff_notes(template, account)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
2020-03-24 18:08:56 +01:00
|
|
|
if self.recreate_xml_ids and self.missing_xml_id(template, account):
|
2019-01-29 18:07:29 +01:00
|
|
|
notes += (notes and "\n" or "") + _("Missing XML-ID.")
|
|
|
|
|
2015-03-28 14:56:47 +01:00
|
|
|
if notes:
|
2016-05-25 14:05:38 +02:00
|
|
|
# Account to be updated
|
2020-01-08 21:52:03 +01:00
|
|
|
self.account_ids.create(
|
|
|
|
{
|
|
|
|
"account_id": template.id,
|
|
|
|
"update_chart_wizard_id": self.id,
|
|
|
|
"type": "updated",
|
|
|
|
"update_account_id": account_id,
|
|
|
|
"notes": notes,
|
|
|
|
}
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
def _find_fiscal_positions(self):
|
|
|
|
"""Load fiscal position templates to create/update."""
|
2020-01-08 21:52:03 +01:00
|
|
|
wiz_fp = self.env["wizard.update.charts.accounts.fiscal.position"]
|
2015-03-28 14:56:47 +01:00
|
|
|
self.fiscal_position_ids.unlink()
|
2017-03-14 11:54:08 +01:00
|
|
|
|
2013-10-01 18:28:19 +02:00
|
|
|
# Search for new / updated fiscal positions
|
2020-01-08 21:52:03 +01:00
|
|
|
templates = self.env["account.fiscal.position.template"].search(
|
|
|
|
[("chart_template_id", "in", self.chart_template_ids.ids)]
|
|
|
|
)
|
2016-05-25 14:05:38 +02:00
|
|
|
for template in templates:
|
|
|
|
# Search for a real fiscal position that matches the template
|
2018-09-18 14:59:54 +02:00
|
|
|
fp_id = self.find_fp_by_templates(template)
|
|
|
|
if not fp_id:
|
2016-05-25 14:05:38 +02:00
|
|
|
# Fiscal position to be created
|
2020-01-08 21:52:03 +01:00
|
|
|
wiz_fp.create(
|
|
|
|
{
|
|
|
|
"fiscal_position_id": template.id,
|
|
|
|
"update_chart_wizard_id": self.id,
|
|
|
|
"type": "new",
|
|
|
|
"notes": _("No fiscal position found with this name."),
|
|
|
|
}
|
|
|
|
)
|
2016-05-25 14:05:38 +02:00
|
|
|
else:
|
|
|
|
# Check the fiscal position for changes
|
2020-01-08 21:52:03 +01:00
|
|
|
fp = self.env["account.fiscal.position"].browse(fp_id)
|
2016-05-25 14:05:38 +02:00
|
|
|
notes = self.diff_notes(template, fp)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
2020-03-24 18:08:56 +01:00
|
|
|
if self.recreate_xml_ids and self.missing_xml_id(template, fp):
|
2019-01-29 18:07:29 +01:00
|
|
|
notes += (notes and "\n" or "") + _("Missing XML-ID.")
|
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
if notes:
|
|
|
|
# Fiscal position template to be updated
|
2020-01-08 21:52:03 +01:00
|
|
|
wiz_fp.create(
|
|
|
|
{
|
|
|
|
"fiscal_position_id": template.id,
|
|
|
|
"update_chart_wizard_id": self.id,
|
|
|
|
"type": "updated",
|
|
|
|
"update_fiscal_position_id": fp_id,
|
|
|
|
"notes": notes,
|
|
|
|
}
|
|
|
|
)
|
2013-10-01 18:28:19 +02:00
|
|
|
|
2019-01-29 18:07:29 +01:00
|
|
|
def recreate_xml_id(self, template, real_obj):
|
2020-01-08 21:52:03 +01:00
|
|
|
ir_model_data = self.env["ir.model.data"]
|
|
|
|
template_xmlid = ir_model_data.search(
|
|
|
|
[("model", "=", template._name), ("res_id", "=", template.id)]
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
new_xml_id = "%d_%s" % (self.company_id.id, template_xmlid.name)
|
2020-03-24 18:08:56 +01:00
|
|
|
ir_model_data.search(
|
|
|
|
[("model", "=", real_obj._name), ("res_id", "=", real_obj.id)]
|
|
|
|
).unlink()
|
2020-01-08 21:52:03 +01:00
|
|
|
template_xmlid.copy(
|
|
|
|
{
|
|
|
|
"model": real_obj._name,
|
|
|
|
"res_id": real_obj.id,
|
|
|
|
"name": new_xml_id,
|
|
|
|
"noupdate": True,
|
|
|
|
}
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
def _update_taxes(self):
|
|
|
|
"""Process taxes to create/update/deactivate."""
|
2020-03-17 19:56:02 +01:00
|
|
|
# First create taxes in batch
|
|
|
|
taxes_to_create = self.tax_ids.filtered(lambda x: x.type == "new")
|
2020-09-15 12:32:42 +02:00
|
|
|
todo_dict = taxes_to_create.mapped("tax_id")._generate_tax(self.company_id)
|
2022-02-23 11:20:30 +01:00
|
|
|
template_to_tax_dict = {}
|
|
|
|
for key in todo_dict["tax_template_to_tax"].keys():
|
|
|
|
template_to_tax_dict[key.id] = todo_dict["tax_template_to_tax"][key].id
|
2020-03-17 19:56:02 +01:00
|
|
|
for wiz_tax in taxes_to_create:
|
2020-09-15 12:32:42 +02:00
|
|
|
new_tax = self.env["account.tax"].browse(
|
2022-02-23 11:20:30 +01:00
|
|
|
template_to_tax_dict[wiz_tax.tax_id.id]
|
2020-09-15 12:32:42 +02:00
|
|
|
)
|
|
|
|
_logger.info(
|
|
|
|
_("Created tax %s."), "'{}' (ID:{})".format(new_tax.name, new_tax.id)
|
|
|
|
)
|
2020-03-17 19:56:02 +01:00
|
|
|
for wiz_tax in self.tax_ids.filtered(lambda x: x.type != "new"):
|
2016-05-25 14:05:38 +02:00
|
|
|
template, tax = wiz_tax.tax_id, wiz_tax.update_tax_id
|
|
|
|
# Deactivate tax
|
2020-01-08 21:52:03 +01:00
|
|
|
if wiz_tax.type == "deleted":
|
2016-05-25 14:05:38 +02:00
|
|
|
tax.active = False
|
2018-09-18 14:41:19 +02:00
|
|
|
_logger.info(_("Deactivated tax %s."), "'%s'" % tax.name)
|
2014-12-24 00:23:39 +01:00
|
|
|
continue
|
2016-05-25 14:05:38 +02:00
|
|
|
else:
|
2020-09-15 12:32:42 +02:00
|
|
|
updated = False
|
2018-09-21 03:06:39 +02:00
|
|
|
for key, value in self.diff_fields(template, tax).items():
|
2018-09-18 14:41:19 +02:00
|
|
|
# We defer update because account might not be created yet
|
2020-09-15 12:32:42 +02:00
|
|
|
if key in [
|
|
|
|
"cash_basis_transition_account_id",
|
2020-01-17 17:33:28 +01:00
|
|
|
"invoice_repartition_line_ids",
|
|
|
|
"refund_repartition_line_ids",
|
2020-09-15 12:32:42 +02:00
|
|
|
]:
|
2018-09-18 14:41:19 +02:00
|
|
|
continue
|
2016-05-25 14:05:38 +02:00
|
|
|
tax[key] = value
|
2020-09-15 12:32:42 +02:00
|
|
|
updated = True
|
|
|
|
if updated:
|
2019-01-29 18:07:29 +01:00
|
|
|
_logger.info(_("Updated tax %s."), "'%s'" % template.name)
|
2020-03-24 18:08:56 +01:00
|
|
|
if self.recreate_xml_ids and self.missing_xml_id(template, tax):
|
2019-01-29 18:07:29 +01:00
|
|
|
self.recreate_xml_id(template, tax)
|
2020-01-08 21:52:03 +01:00
|
|
|
_logger.info(
|
|
|
|
_("Updated tax %s. (Recreated XML-IDs)"), "'%s'" % template.name
|
|
|
|
)
|
2020-09-15 12:32:42 +02:00
|
|
|
return todo_dict
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
def _update_accounts(self):
|
2015-03-28 14:56:47 +01:00
|
|
|
"""Process accounts to create/update."""
|
|
|
|
for wiz_account in self.account_ids:
|
2020-01-08 21:52:03 +01:00
|
|
|
account, template = (wiz_account.update_account_id, wiz_account.account_id)
|
|
|
|
if wiz_account.type == "new":
|
2013-10-01 18:28:19 +02:00
|
|
|
# Create the account
|
2018-09-18 14:41:19 +02:00
|
|
|
tax_template_ref = {
|
2020-01-08 21:52:03 +01:00
|
|
|
tax.id: self.find_tax_by_templates(tax) for tax in template.tax_ids
|
2018-09-18 14:41:19 +02:00
|
|
|
}
|
|
|
|
vals = self.chart_template_id._get_account_vals(
|
2020-01-08 21:52:03 +01:00
|
|
|
self.company_id,
|
|
|
|
template,
|
2018-09-18 14:41:19 +02:00
|
|
|
self.padded_code(template.code),
|
|
|
|
tax_template_ref,
|
|
|
|
)
|
2013-10-01 18:28:19 +02:00
|
|
|
try:
|
2016-05-25 14:05:38 +02:00
|
|
|
with self.env.cr.savepoint():
|
2018-09-18 14:41:19 +02:00
|
|
|
self.chart_template_id.create_record_with_xmlid(
|
2020-01-08 21:52:03 +01:00
|
|
|
self.company_id, template, "account.account", vals
|
2018-09-18 14:41:19 +02:00
|
|
|
)
|
|
|
|
_logger.info(
|
2016-05-25 14:05:38 +02:00
|
|
|
_("Created account %s."),
|
2020-01-08 21:52:03 +01:00
|
|
|
"'{} - {}'".format(vals["code"], vals["name"]),
|
2018-09-18 14:41:19 +02:00
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
self.rejected_new_account_number += 1
|
2020-01-08 21:52:03 +01:00
|
|
|
if config["test_enable"]:
|
2018-09-18 14:41:19 +02:00
|
|
|
_logger.info(EXCEPTION_TEXT)
|
|
|
|
else: # pragma: no cover
|
|
|
|
_logger.exception(
|
|
|
|
"ERROR: " + _("Exception creating account %s."),
|
2020-01-08 21:52:03 +01:00
|
|
|
"'{} - {}'".format(template.code, template.name),
|
2018-09-18 14:41:19 +02:00
|
|
|
)
|
|
|
|
if not self.continue_on_errors:
|
|
|
|
break
|
2015-03-28 14:56:47 +01:00
|
|
|
else:
|
2013-10-01 18:28:19 +02:00
|
|
|
# Update the account
|
|
|
|
try:
|
2016-05-25 14:05:38 +02:00
|
|
|
with self.env.cr.savepoint():
|
2020-01-08 21:52:03 +01:00
|
|
|
for key, value in iter(
|
|
|
|
self.diff_fields(template, account).items()
|
|
|
|
):
|
2016-05-25 14:05:38 +02:00
|
|
|
account[key] = value
|
2018-09-18 14:41:19 +02:00
|
|
|
_logger.info(
|
|
|
|
_("Updated account %s."),
|
2020-01-08 21:52:03 +01:00
|
|
|
"'{} - {}'".format(account.code, account.name),
|
2018-09-18 14:41:19 +02:00
|
|
|
)
|
2020-03-24 18:08:56 +01:00
|
|
|
if self.recreate_xml_ids and self.missing_xml_id(
|
|
|
|
template, account
|
|
|
|
):
|
2019-01-29 18:07:29 +01:00
|
|
|
self.recreate_xml_id(template, account)
|
|
|
|
_logger.info(
|
|
|
|
_("Updated account %s. (Recreated XML-ID)"),
|
2020-01-08 21:52:03 +01:00
|
|
|
"'{} - {}'".format(account.code, account.name),
|
2019-01-29 18:07:29 +01:00
|
|
|
)
|
|
|
|
|
2018-09-18 14:41:19 +02:00
|
|
|
except Exception:
|
|
|
|
self.rejected_updated_account_number += 1
|
2020-01-08 21:52:03 +01:00
|
|
|
if config["test_enable"]:
|
2018-09-18 14:41:19 +02:00
|
|
|
_logger.info(EXCEPTION_TEXT)
|
|
|
|
else: # pragma: no cover
|
|
|
|
_logger.exception(
|
|
|
|
"ERROR: " + _("Exception writing account %s."),
|
2020-01-08 21:52:03 +01:00
|
|
|
"'{} - {}'".format(account.code, account.name),
|
2018-09-18 14:41:19 +02:00
|
|
|
)
|
|
|
|
if not self.continue_on_errors:
|
|
|
|
break
|
2013-10-01 18:28:19 +02:00
|
|
|
|
2020-09-15 12:32:42 +02:00
|
|
|
def _update_taxes_pending_for_accounts(self, todo_dict):
|
2015-03-28 14:56:47 +01:00
|
|
|
"""Updates the taxes (created or updated on previous steps) to set
|
2014-03-21 02:01:05 +01:00
|
|
|
the references to the accounts (the taxes where created/updated first,
|
2015-03-28 14:56:47 +01:00
|
|
|
when the referenced accounts are still not available).
|
2014-03-21 02:01:05 +01:00
|
|
|
"""
|
2020-09-15 12:32:42 +02:00
|
|
|
done = self.env["account.tax"]
|
2023-08-03 11:16:15 +02:00
|
|
|
for tax, v in todo_dict["account_dict"]["account.tax"].items():
|
2020-09-15 12:32:42 +02:00
|
|
|
vals = {}
|
|
|
|
for fld in [
|
|
|
|
"cash_basis_transition_account_id",
|
|
|
|
]:
|
|
|
|
if v[fld]:
|
|
|
|
acc_id = self.find_account_by_templates(
|
2022-02-23 11:20:30 +01:00
|
|
|
self.env["account.account.template"].browse(v[fld].id)
|
2020-09-15 12:32:42 +02:00
|
|
|
)
|
|
|
|
if acc_id:
|
|
|
|
vals[fld] = acc_id
|
|
|
|
else:
|
|
|
|
raise exceptions.UserError(
|
|
|
|
_("No real account found for template account with ID %s")
|
2022-02-23 11:20:30 +01:00
|
|
|
% v[fld].id
|
2020-09-15 12:32:42 +02:00
|
|
|
)
|
|
|
|
if vals:
|
|
|
|
tax.write(vals)
|
|
|
|
done |= tax
|
|
|
|
|
2022-06-01 11:15:20 +02:00
|
|
|
for rep_line, v in todo_dict["account_dict"][
|
|
|
|
"account.tax.repartition.line"
|
|
|
|
].items():
|
2020-09-15 12:32:42 +02:00
|
|
|
if v["account_id"]:
|
|
|
|
acc_id = self.find_account_by_templates(
|
2022-02-23 11:20:30 +01:00
|
|
|
self.env["account.account.template"].browse(v["account_id"].id)
|
2020-09-15 12:32:42 +02:00
|
|
|
)
|
|
|
|
if acc_id:
|
|
|
|
rep_line.write({"account_id": acc_id})
|
|
|
|
done |= rep_line.invoice_tax_id or rep_line.refund_tax_id
|
|
|
|
else:
|
|
|
|
raise exceptions.UserError(
|
|
|
|
_("No real account found for template account with ID %s")
|
2022-02-23 11:20:30 +01:00
|
|
|
% v["account_id"].id
|
2020-09-15 12:32:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
for wiz_tax in self.tax_ids.filtered(lambda r: r.type == "updated"):
|
2018-09-18 14:41:19 +02:00
|
|
|
template = wiz_tax.tax_id
|
|
|
|
tax = wiz_tax.update_tax_id
|
2020-01-17 17:33:28 +01:00
|
|
|
vals = {}
|
2018-09-21 03:06:39 +02:00
|
|
|
for key, value in self.diff_fields(template, tax).items():
|
2020-01-17 17:33:28 +01:00
|
|
|
if key in {
|
2020-09-15 12:32:42 +02:00
|
|
|
"cash_basis_transition_account_id",
|
2020-01-17 17:33:28 +01:00
|
|
|
"invoice_repartition_line_ids",
|
|
|
|
"refund_repartition_line_ids",
|
|
|
|
}:
|
|
|
|
vals[key] = value
|
|
|
|
if vals:
|
|
|
|
tax.write(vals)
|
2020-09-15 12:32:42 +02:00
|
|
|
done |= tax
|
|
|
|
|
|
|
|
if done:
|
|
|
|
_logger.info(
|
|
|
|
_("Post-updated account fields for taxes with IDs %s."), "%s" % done.ids
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
def _prepare_fp_vals(self, fp_template):
|
2015-03-28 14:56:47 +01:00
|
|
|
# Tax mappings
|
|
|
|
tax_mapping = []
|
|
|
|
for fp_tax in fp_template.tax_ids:
|
|
|
|
# Create the fp tax mapping
|
2020-01-08 21:52:03 +01:00
|
|
|
tax_mapping.append(
|
|
|
|
{
|
|
|
|
"tax_src_id": self.find_tax_by_templates(fp_tax.tax_src_id),
|
|
|
|
"tax_dest_id": self.find_tax_by_templates(fp_tax.tax_dest_id),
|
|
|
|
}
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
# Account mappings
|
|
|
|
account_mapping = []
|
|
|
|
for fp_account in fp_template.account_ids:
|
|
|
|
# Create the fp account mapping
|
2020-01-08 21:52:03 +01:00
|
|
|
account_mapping.append(
|
|
|
|
{
|
|
|
|
"account_src_id": (
|
|
|
|
self.find_account_by_templates(fp_account.account_src_id)
|
|
|
|
),
|
|
|
|
"account_dest_id": (
|
|
|
|
self.find_account_by_templates(fp_account.account_dest_id)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
return {
|
2020-01-08 21:52:03 +01:00
|
|
|
"company_id": self.company_id.id,
|
|
|
|
"name": fp_template.name,
|
|
|
|
"tax_ids": [(0, 0, x) for x in tax_mapping],
|
|
|
|
"account_ids": [(0, 0, x) for x in account_mapping],
|
2015-03-28 14:56:47 +01:00
|
|
|
}
|
2013-10-01 18:28:19 +02:00
|
|
|
|
2016-05-25 14:05:38 +02:00
|
|
|
def _update_fiscal_positions(self):
|
2015-03-28 14:56:47 +01:00
|
|
|
"""Process fiscal position templates to create/update."""
|
|
|
|
for wiz_fp in self.fiscal_position_ids:
|
2020-01-08 21:52:03 +01:00
|
|
|
fp, template = (wiz_fp.update_fiscal_position_id, wiz_fp.fiscal_position_id)
|
|
|
|
if wiz_fp.type == "new":
|
2013-10-01 18:28:19 +02:00
|
|
|
# Create a new fiscal position
|
2018-09-18 14:41:19 +02:00
|
|
|
self.chart_template_id.create_record_with_xmlid(
|
2020-01-08 21:52:03 +01:00
|
|
|
self.company_id,
|
|
|
|
template,
|
|
|
|
"account.fiscal.position",
|
2018-09-18 14:41:19 +02:00
|
|
|
self._prepare_fp_vals(template),
|
|
|
|
)
|
2020-01-08 21:52:03 +01:00
|
|
|
_logger.info(_("Created fiscal position %s."), "'%s'" % template.name)
|
2015-03-28 14:56:47 +01:00
|
|
|
else:
|
2018-06-13 16:53:05 +02:00
|
|
|
for key, value in self.diff_fields(template, fp).items():
|
2017-08-14 19:36:35 +02:00
|
|
|
fp[key] = value
|
2020-01-08 21:52:03 +01:00
|
|
|
_logger.info(
|
|
|
|
_("Updated fiscal position %s."), "'%s'" % template.name
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
2020-03-24 18:08:56 +01:00
|
|
|
if self.recreate_xml_ids and self.missing_xml_id(template, fp):
|
2019-01-29 18:07:29 +01:00
|
|
|
self.recreate_xml_id(template, fp)
|
|
|
|
_logger.info(
|
|
|
|
_("Updated fiscal position %s. (Recreated XML-ID)"),
|
|
|
|
"'%s'" % template.name,
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
class WizardUpdateChartsAccountsTax(models.TransientModel):
|
2020-01-08 21:52:03 +01:00
|
|
|
_name = "wizard.update.charts.accounts.tax"
|
|
|
|
_description = "Tax that needs to be updated (new or updated in the " "template)."
|
2015-03-28 14:56:47 +01:00
|
|
|
|
|
|
|
tax_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="account.tax.template", string="Tax template", ondelete="set null"
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
update_chart_wizard_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="wizard.update.charts.accounts",
|
|
|
|
string="Update chart wizard",
|
|
|
|
required=True,
|
|
|
|
ondelete="cascade",
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
type = fields.Selection(
|
2020-01-08 21:52:03 +01:00
|
|
|
selection=[
|
|
|
|
("new", "New template"),
|
|
|
|
("updated", "Updated template"),
|
|
|
|
("deleted", "Tax to deactivate"),
|
|
|
|
],
|
2021-03-08 17:25:04 +01:00
|
|
|
readonly=False,
|
2020-01-08 21:52:03 +01:00
|
|
|
)
|
|
|
|
type_tax_use = fields.Selection(related="tax_id.type_tax_use", readonly=True)
|
2015-03-28 14:56:47 +01:00
|
|
|
update_tax_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="account.tax",
|
|
|
|
string="Tax to update",
|
|
|
|
required=False,
|
|
|
|
ondelete="set null",
|
|
|
|
)
|
2022-02-23 11:20:30 +01:00
|
|
|
notes = fields.Text(readonly=True)
|
|
|
|
recreate_xml_ids = fields.Boolean(related="update_chart_wizard_id.recreate_xml_ids")
|
2015-03-28 14:56:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
class WizardUpdateChartsAccountsAccount(models.TransientModel):
|
2020-01-08 21:52:03 +01:00
|
|
|
_name = "wizard.update.charts.accounts.account"
|
|
|
|
_description = (
|
|
|
|
"Account that needs to be updated (new or updated in the " "template)."
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
|
|
|
|
account_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="account.account.template",
|
|
|
|
string="Account template",
|
|
|
|
required=True,
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
update_chart_wizard_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="wizard.update.charts.accounts",
|
|
|
|
string="Update chart wizard",
|
|
|
|
required=True,
|
|
|
|
ondelete="cascade",
|
2015-03-28 14:56:47 +01:00
|
|
|
)
|
|
|
|
type = fields.Selection(
|
2020-01-08 21:52:03 +01:00
|
|
|
selection=[("new", "New template"), ("updated", "Updated template")],
|
2021-03-08 17:25:04 +01:00
|
|
|
readonly=False,
|
2020-01-08 21:52:03 +01:00
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
update_account_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="account.account",
|
|
|
|
string="Account to update",
|
|
|
|
required=False,
|
|
|
|
ondelete="set null",
|
|
|
|
)
|
2022-02-23 11:20:30 +01:00
|
|
|
notes = fields.Text(readonly=True)
|
|
|
|
recreate_xml_ids = fields.Boolean(related="update_chart_wizard_id.recreate_xml_ids")
|
2015-03-28 14:56:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
class WizardUpdateChartsAccountsFiscalPosition(models.TransientModel):
|
2020-01-08 21:52:03 +01:00
|
|
|
_name = "wizard.update.charts.accounts.fiscal.position"
|
|
|
|
_description = (
|
|
|
|
"Fiscal position that needs to be updated (new or updated " "in the template)."
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
|
|
|
|
fiscal_position_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="account.fiscal.position.template",
|
|
|
|
string="Fiscal position template",
|
|
|
|
required=True,
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
update_chart_wizard_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="wizard.update.charts.accounts",
|
|
|
|
string="Update chart wizard",
|
|
|
|
required=True,
|
|
|
|
ondelete="cascade",
|
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
type = fields.Selection(
|
2020-01-08 21:52:03 +01:00
|
|
|
selection=[("new", "New template"), ("updated", "Updated template")],
|
2021-03-08 17:25:04 +01:00
|
|
|
readonly=False,
|
2017-08-14 19:36:35 +02:00
|
|
|
)
|
2015-03-28 14:56:47 +01:00
|
|
|
update_fiscal_position_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="account.fiscal.position",
|
|
|
|
required=False,
|
|
|
|
string="Fiscal position to update",
|
|
|
|
ondelete="set null",
|
|
|
|
)
|
2022-02-23 11:20:30 +01:00
|
|
|
notes = fields.Text(readonly=True)
|
2021-03-08 17:25:04 +01:00
|
|
|
recreate_xml_ids = fields.Boolean(
|
|
|
|
string="Recreate missing XML-IDs",
|
|
|
|
related="update_chart_wizard_id.recreate_xml_ids",
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
class WizardMatching(models.TransientModel):
|
2020-01-08 21:52:03 +01:00
|
|
|
_name = "wizard.matching"
|
|
|
|
_description = "Wizard Matching"
|
|
|
|
_order = "sequence"
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
update_chart_wizard_id = fields.Many2one(
|
2020-01-08 21:52:03 +01:00
|
|
|
comodel_name="wizard.update.charts.accounts",
|
|
|
|
string="Update chart wizard",
|
2019-01-29 18:07:29 +01:00
|
|
|
required=True,
|
2020-01-08 21:52:03 +01:00
|
|
|
ondelete="cascade",
|
2019-01-29 18:07:29 +01:00
|
|
|
)
|
2020-01-08 21:52:03 +01:00
|
|
|
sequence = fields.Integer(required=True, default=1)
|
|
|
|
matching_value = fields.Selection(selection="_get_matching_selection")
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
def _get_matching_selection(self):
|
2020-01-08 21:52:03 +01:00
|
|
|
return [("xml_id", "XML-ID")]
|
2019-01-29 18:07:29 +01:00
|
|
|
|
|
|
|
def _selection_from_files(self, model_name, field_opts):
|
|
|
|
result = []
|
|
|
|
for opt in field_opts:
|
|
|
|
model = self.env[model_name]
|
|
|
|
desc = model._fields[opt].get_description(self.env)["string"]
|
2020-01-08 21:52:03 +01:00
|
|
|
result.append((opt, "{} ({})".format(desc, opt)))
|
2019-01-29 18:07:29 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class WizardTaxMatching(models.TransientModel):
|
|
|
|
_name = "wizard.tax.matching"
|
2020-01-17 17:33:28 +01:00
|
|
|
_description = "Wizard Tax Matching"
|
2019-01-29 18:07:29 +01:00
|
|
|
_inherit = "wizard.matching"
|
|
|
|
|
|
|
|
def _get_matching_selection(self):
|
|
|
|
vals = super(WizardTaxMatching, self)._get_matching_selection()
|
2020-01-08 21:52:03 +01:00
|
|
|
vals += self._selection_from_files(
|
|
|
|
"account.tax.template", ["description", "name"]
|
|
|
|
)
|
2019-01-29 18:07:29 +01:00
|
|
|
return vals
|
|
|
|
|
|
|
|
|
|
|
|
class WizardAccountMatching(models.TransientModel):
|
|
|
|
_name = "wizard.account.matching"
|
2020-01-17 17:33:28 +01:00
|
|
|
_description = "Wizard Account Matching"
|
2019-01-29 18:07:29 +01:00
|
|
|
_inherit = "wizard.matching"
|
|
|
|
|
|
|
|
def _get_matching_selection(self):
|
|
|
|
vals = super(WizardAccountMatching, self)._get_matching_selection()
|
2020-01-08 21:52:03 +01:00
|
|
|
vals += self._selection_from_files("account.account.template", ["code", "name"])
|
2019-01-29 18:07:29 +01:00
|
|
|
return vals
|
|
|
|
|
|
|
|
|
|
|
|
class WizardFpMatching(models.TransientModel):
|
2020-01-08 21:52:03 +01:00
|
|
|
_name = "wizard.fp.matching"
|
2020-01-17 17:33:28 +01:00
|
|
|
_description = "Wizard Fiscal Position Matching"
|
2019-01-29 18:07:29 +01:00
|
|
|
_inherit = "wizard.matching"
|
|
|
|
|
|
|
|
def _get_matching_selection(self):
|
|
|
|
vals = super(WizardFpMatching, self)._get_matching_selection()
|
2020-01-08 21:52:03 +01:00
|
|
|
vals += self._selection_from_files("account.fiscal.position.template", ["name"])
|
2019-01-29 18:07:29 +01:00
|
|
|
return vals
|