[IMP] account: allow to switch accounting packages (CoA...)
We can now select a new localization module in place of the existing one, if no accounting has been done yet. This allows to set a default CoA for Germany and China, by choosing arbitrary one of the existing localization package at the installation since they can directly switch if needed. Was PR #16605.
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
<sheet>
|
||||
<field name="company_id" invisible="1"/>
|
||||
<field name="has_chart_of_accounts" invisible="1"/>
|
||||
<field name="has_accounting_entries" invisible="1"/>
|
||||
<h2>Fiscal Localization</h2>
|
||||
<div class="row mt16 o_settings_container">
|
||||
<div class="col-xs-12 col-md-6 o_setting_box">
|
||||
@@ -32,13 +33,13 @@
|
||||
Taxes, fiscal positions, chart of accounts & legal statements for your country
|
||||
</div>
|
||||
<div class="content-group">
|
||||
<span attrs="{'invisible': [('has_chart_of_accounts','!=',False)]}">
|
||||
<span attrs="{'invisible': [('has_accounting_entries', '=', True)]}">
|
||||
<div class="row mt16">
|
||||
<label string="Package" class="col-md-3 o_light_label"/>
|
||||
<field name="chart_template_id" widget="selection"/>
|
||||
</div>
|
||||
<div>
|
||||
<button name="%(account.open_account_charts_modules)d" icon="fa-arrow-right" type="action" string="Install More Packages" class="btn-link" attrs="{'invisible': [('has_chart_of_accounts','!=',False)]}"/>
|
||||
<button name="%(account.open_account_charts_modules)d" icon="fa-arrow-right" type="action" string="Install More Packages" class="btn-link"/>
|
||||
</div>
|
||||
</span>
|
||||
<div class="row mt16" groups="base.group_no_one">
|
||||
|
||||
@@ -27,4 +27,5 @@
|
||||
'data/l10n_cn_small_business_chart_data.xml',
|
||||
'data/account_chart_template_data.yml',
|
||||
],
|
||||
'auto_install': True
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user