diff --git a/addons/account/models/account.py b/addons/account/models/account.py index e344c6a88ba..2ac3b9b0098 100644 --- a/addons/account/models/account.py +++ b/addons/account/models/account.py @@ -91,8 +91,7 @@ class AccountAccount(models.Model): rec = self.search([('code', '=', new_code), ('company_id', '=', company.id)], limit=1) if not rec: return new_code - else: - raise UserError(_('Cannot generate an unused account code.')) + raise UserError(_('Cannot generate an unused account code.')) def _compute_opening_debit_credit(self): for record in self: diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index dfc8db0015f..53976e49c23 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -124,6 +124,34 @@ class AccountChartTemplate(models.Model): property_stock_account_output_categ_id = fields.Many2one('account.account.template', string="Output Account for Stock Valuation", oldname="property_stock_account_output_categ") property_stock_valuation_account_id = fields.Many2one('account.account.template', string="Account Template for Stock Valuation") + @api.model + def _prepare_transfer_account(self): + ''' Prepare values to create the transfer account that is an intermediary account used when moving money + from a liquidity account to another. + + :param name: The transfer account name. + :param company: The company owning this account. + :return: A dictionary of values to create a new account.account. + ''' + digits = self.code_digits + prefix = self.transfer_account_code_prefix or '' + new_code = '' + for num in range(1, 100): + new_code = str(prefix.ljust(digits - 1, '0')) + str(num) + rec = self.env['account.account.template'].search([('code', '=', new_code), ('chart_template_id', '=', self.id)], limit=1) + if not rec: + break + else: + raise UserError(_('Cannot generate an unused account code.')) + current_assets_type = self.env.ref('account.data_account_type_current_assets', raise_if_not_found=False) + return { + 'name': _('Liquidity Transfer'), + 'code': new_code, + 'user_type_id': current_assets_type and current_assets_type.id or False, + 'reconcile': True, + 'chart_template_id': self.id, + } + @api.one def try_loading_for_current_company(self): self.ensure_one() @@ -909,10 +937,6 @@ class WizardMultiChartsAccounts(models.TransientModel): # Install all the templates objects and generate the real objects acc_template_ref, taxes_ref = self.chart_template_id._install_template(company, code_digits=self.code_digits) - # Create Transfer account - transfer_account_vals = self._prepare_transfer_account(_('Liquidity Transfers'), company) - company.transfer_account_id = self.env['account.account'].create(transfer_account_vals) - # Create Bank journals self._create_bank_journals_from_o2m(company, acc_template_ref) @@ -924,30 +948,6 @@ class WizardMultiChartsAccounts(models.TransientModel): company.account_purchase_tax_id = self.env['account.tax'].search([('type_tax_use', 'in', ('purchase', 'all')), ('company_id', '=', company.id)], limit=1).id return {} - @api.model - def _prepare_transfer_account(self, name, company): - ''' Prepare values to create the transfer account that is ani ntermediary account used when moving money - from a liquidity account to another. - - :param name: The transfer account name. - :param company: The company owning this account. - :return: A dictionary of values to create a new account.account. - ''' - digits = 6 - acc = self.env['account.account'].search([('company_id', '=', company.id)], limit=1) - if acc: - digits = len(acc.code) - - account_code_prefix = company.transfer_account_code_prefix or '' - current_assets_type = self.env.ref('account.data_account_type_current_assets', raise_if_not_found=False) - return { - 'name': name, - 'code': self.env['account.account']._search_new_account_code(company, digits, account_code_prefix), - 'user_type_id': current_assets_type and current_assets_type.id or False, - 'reconcile': True, - 'company_id': company.id, - } - @api.multi def _create_bank_journals_from_o2m(self, company, acc_template_ref): ''' @@ -1005,3 +1005,22 @@ class AccountReconcileModelTemplate(models.Model): ], string="Second Amount type",required=True, default='percentage') second_amount = fields.Float(string='Second Amount', digits=0, required=True, default=100.0, help="Fixed amount will count as a debit if it is negative, as a credit if it is positive.") second_tax_id = fields.Many2one('account.tax.template', string='Second Tax', ondelete='restrict', domain=[('type_tax_use', '=', 'purchase')]) + + +class IrModelData(models.Model): + _inherit = 'ir.model.data' + + @api.model + def _update(self, model, module, values, xml_id=False, store=True, noupdate=False, mode='init', res_id=False): + record_id = super(IrModelData, self)._update(model, module, values, xml_id=xml_id, store=store, noupdate=noupdate, mode=mode, res_id=res_id) + # When creating a chart template create, for the liquidity transfer account + # - an account.account.template: this allow to define account.reconcile.model.template objects refering that liquidity transfer + # account although it's not existing in any xml file + # - an entry in ir_model_data: this allow to still use the method create_record_with_xmlid() and don't make any difference between + # regular accounts created and that liquidity transfer account + if model == 'account.chart.template' and xml_id and module: + new_xml_id = xml_id + '_liquidity_transfer' + if not self.search([('model', '=', 'account.account.template'), ('module', '=', module), ('name', '=', new_xml_id)]): + vals = self.env[model].browse(record_id)._prepare_transfer_account() + self._update('account.account.template', module, vals, xml_id=new_xml_id, store=True, noupdate=noupdate, mode=mode, res_id=False) + return record_id diff --git a/addons/l10n_be/data/account_reconcile_model_template.xml b/addons/l10n_be/data/account_reconcile_model_template.xml index 192f6aeac99..a79759f9f41 100644 --- a/addons/l10n_be/data/account_reconcile_model_template.xml +++ b/addons/l10n_be/data/account_reconcile_model_template.xml @@ -27,8 +27,8 @@ + Virements internes - percentage 100 Virements internes