Skip to content
Browse files

[ADD] account: when installing a localization, create xml_id for obje…

…ct that are created from templates
  • Loading branch information...
csnauwaert committed Dec 3, 2015
1 parent 2373cad commit 01e7ed3494971000ae0955bce36288b3b7a4b0b2
Showing with 20 additions and 10 deletions.
  1. +20 −10 addons/account/models/
@@ -310,6 +310,16 @@ def _load_template(self, company, code_digits=None, transfer_account_id=None, ac

return account_ref, taxes_ref

def create_record_with_xmlid(self, company, template, model, vals):
# Create a record for the given model with the given vals and
# also create an entry in ir_model_data to have an xmlid for the newly created record
# xmlid is the concatenation of company_id and template_xml_id
ir_model_data = self.env['']
template_xmlid =[('model', '=', str(template._model)), ('res_id', '=',])
new_xml_id = str('_'
return ir_model_data._update(model, template_xmlid.module, vals, xml_id=new_xml_id, store=True, noupdate=True, mode='init', res_id=False)

def generate_account(self, tax_template_ref, acc_template_ref, code_digits, company):
""" This method for generating accounts from templates.
@@ -344,8 +354,8 @@ def generate_account(self, tax_template_ref, acc_template_ref, code_digits, comp
'tag_ids': [(6, 0, [ for t in account_template.tag_ids])],
new_account = self.env['account.account'].create(vals)
acc_template_ref[] =
new_account = self.create_record_with_xmlid(company, account_template, 'account.account', vals)
acc_template_ref[] = new_account
return acc_template_ref

@@ -361,18 +371,18 @@ def generate_fiscal_position(self, tax_template_ref, acc_template_ref, company):
positions = self.env['account.fiscal.position.template'].search([('chart_template_id', '=',])
for position in positions:
new_fp = self.env['account.fiscal.position'].create({'company_id':, 'name':, 'note': position.note})
new_fp = self.create_record_with_xmlid(company, position, 'account.fiscal.position', {'company_id':, 'name':, 'note': position.note})
for tax in position.tax_ids:
self.create_record_with_xmlid(company, tax, '', {
'tax_src_id': tax_template_ref[],
'tax_dest_id': tax.tax_dest_id and tax_template_ref[] or False,
'position_id': new_fp
for acc in position.account_ids:
self.create_record_with_xmlid(company, acc, 'account.fiscal.position.account', {
'account_src_id': acc_template_ref[],
'account_dest_id': acc_template_ref[],
'position_id': new_fp
return True

@@ -463,10 +473,10 @@ def _generate_tax(self, company):
vals_tax = tax._get_tax_vals(company)
vals_tax['children_tax_ids'] = children_ids and [(6, 0, children_ids)] or []
new_tax = self.env[''].create(vals_tax)
tax_template_to_tax[] =
new_tax = self.env['account.chart.template'].create_record_with_xmlid(company, tax, '', vals_tax)
tax_template_to_tax[] = new_tax
# Since the accounts have not been created yet, we have to wait before filling these fields
todo_dict[] = {
todo_dict[new_tax] = {

1 comment on commit 01e7ed3


This comment has been minimized.

Copy link

commented on 01e7ed3 Apr 22, 2016

@csnauwaert Have we got any utility function at our disposal to make conditional data imports? Say, we have an account on the base company, and we want to assign this account on some other model, let's say a partner, we want to pre-load, then we assume base company (id=1) to be present. The following will fail in a data load, if the user deleted the base company for whatsoever reason:

<field name="account_id" eval="ref('account.1_account_template_1000')/>

Something of the following kind would be useful:

<field name="account_id" eval="try('account.1_account_template_1000')/>

Or better even:

<record option="try" ... >
<field name="account_id" eval="ref('account.1_account_template_1000')/>
Please sign in to comment.
You can’t perform that action at this time.