diff --git a/addons/account/models/account_config_settings.py b/addons/account/models/account_config_settings.py index 1e64aa013a4..920106a111a 100644 --- a/addons/account/models/account_config_settings.py +++ b/addons/account/models/account_config_settings.py @@ -12,6 +12,7 @@ class AccountConfigSettings(models.TransientModel): company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env.user.company_id) + has_accounting_entries = fields.Boolean(compute='_compute_has_chart_of_accounts') currency_id = fields.Many2one('res.currency', related="company_id.currency_id", required=True, string='Currency', help="Main currency of the company.") currency_exchange_journal_id = fields.Many2one( @@ -83,9 +84,7 @@ class AccountConfigSettings(models.TransientModel): if self.default_purchase_tax_id: ir_values_obj.sudo().set_default('product.template', "supplier_taxes_id", [self.default_purchase_tax_id.id], for_all_users=True, company_id=self.company_id.id) """ install a chart of accounts for the given company (if required) """ - if self.chart_template_id and not self.has_chart_of_accounts and self.company_id.expects_chart_of_accounts: - if self.company_id.chart_template_id and self.chart_template_id != self.company_id.chart_template_id: - raise UserError(_('You can not change a company chart of account once it has been installed')) + if self.chart_template_id and self.chart_template_id != self.company_id.chart_template_id: wizard = self.env['wizard.multi.charts.accounts'].create({ 'company_id': self.company_id.id, 'chart_template_id': self.chart_template_id.id, @@ -105,6 +104,8 @@ class AccountConfigSettings(models.TransientModel): @api.depends('company_id') def _compute_has_chart_of_accounts(self): self.has_chart_of_accounts = bool(self.company_id.chart_template_id) + self.chart_template_id = self.company_id.chart_template_id or False + self.has_accounting_entries = self.env['wizard.multi.charts.accounts'].existing_accounting(self.company_id) @api.onchange('group_analytic_accounting') def onchange_analytic_accounting(self): diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index 3164eb79b51..2fe33e02e3a 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -3,6 +3,7 @@ from odoo.exceptions import AccessError from odoo import api, fields, models, _ from odoo import SUPERUSER_ID +from odoo.exceptions import UserError import logging @@ -827,6 +828,14 @@ class WizardMultiChartsAccounts(models.TransientModel): ref_taxs.write({'amount': value, 'name': _('Tax %.2f%%') % value, 'description': '%.2f%%' % value}) return True + @api.multi + def existing_accounting(self, company_id): + model_to_check = ['account.move.line', 'account.invoice', 'account.move', 'account.payment', 'account.bank.statement'] + for model in model_to_check: + if len(self.env[model].search([('company_id', '=', company_id.id)])) > 0: + return True + return False + @api.multi def execute(self): ''' @@ -834,13 +843,33 @@ class WizardMultiChartsAccounts(models.TransientModel): all the provided information to create the accounts, the banks, the journals, the taxes, the accounting properties... accordingly for the chosen company. ''' - if len(self.env['account.account'].search([('company_id', '=', self.company_id.id)])) > 0: - # We are in a case where we already have some accounts existing, meaning that user has probably - # created its own accounts and does not need a coa, so skip installation of coa. - _logger.info('Could not install chart of account since some accounts already exists for the company (%s)', (self.company_id.id,)) - return {} if not self.env.user._is_admin(): raise AccessError(_("Only administrators can change the settings")) + + existing_accounts = self.env['account.account'].search([('company_id', '=', self.company_id.id)]) + if existing_accounts: + # we tolerate switching from accounting package (localization module) as long as there isn't yet any accounting + # entries created for the company. + if self.existing_accounting(self.company_id): + raise UserError(_('Could not install new chart of account as there are already accounting entries existing')) + + # delete accounting properties + prop_values = ['account.account,%s' % (account_id,) for account_id in existing_accounts.ids] + existing_journals = self.env['account.journal'].search([('company_id', '=', self.company_id.id)]) + if existing_journals: + prop_values.extend(['account.journal,%s' % (journal_id,) for journal_id in existing_journals.ids]) + accounting_props = self.env['ir.property'].search([('value_reference', 'in', prop_values)]) + if accounting_props: + accounting_props.unlink() + + # delete account, journal, tax, fiscal position and reconciliation model + models_to_delete = ['account.reconcile.model', 'account.fiscal.position', 'account.tax', 'account.journal'] + for model in models_to_delete: + res = self.env[model].search([('company_id', '=', self.company_id.id)]) + if len(res): + res.unlink() + existing_accounts.unlink() + ir_values_obj = self.env['ir.values'] company = self.company_id self.company_id.write({'currency_id': self.currency_id.id, diff --git a/addons/account/views/account_config_settings_views.xml b/addons/account/views/account_config_settings_views.xml index c0d98d69fab..d7aad5c3dfe 100644 --- a/addons/account/views/account_config_settings_views.xml +++ b/addons/account/views/account_config_settings_views.xml @@ -21,6 +21,7 @@ +

Fiscal Localization

@@ -32,13 +33,13 @@ Taxes, fiscal positions, chart of accounts & legal statements for your country
- +
-
diff --git a/addons/l10n_cn_small_business/__manifest__.py b/addons/l10n_cn_small_business/__manifest__.py index c5e504f2da7..ad7e3922b5e 100644 --- a/addons/l10n_cn_small_business/__manifest__.py +++ b/addons/l10n_cn_small_business/__manifest__.py @@ -27,4 +27,5 @@ 'data/l10n_cn_small_business_chart_data.xml', 'data/account_chart_template_data.yml', ], + 'auto_install': True } diff --git a/addons/l10n_de_skr03/__manifest__.py b/addons/l10n_de_skr03/__manifest__.py index 9438102ab65..5e7cfe6b5c1 100644 --- a/addons/l10n_de_skr03/__manifest__.py +++ b/addons/l10n_de_skr03/__manifest__.py @@ -69,4 +69,5 @@ German accounting chart and localization. 'data/account_tax_fiscal_position_data.xml', 'data/account_chart_template_data.yml', ], + 'auto_install': True }