Skip to content
Permalink
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/chart_template.py
@@ -310,6 +310,16 @@ def _load_template(self, company, code_digits=None, transfer_account_id=None, ac

return account_ref, taxes_ref

@api.multi
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['ir.model.data']
template_xmlid = ir_model_data.search([('model', '=', str(template._model)), ('res_id', '=', template.id)])
new_xml_id = str(company.id)+'_'+template_xmlid.name
return ir_model_data._update(model, template_xmlid.module, vals, xml_id=new_xml_id, store=True, noupdate=True, mode='init', res_id=False)

@api.multi
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
'company_id': company.id,
'tag_ids': [(6, 0, [t.id for t in account_template.tag_ids])],
}
new_account = self.env['account.account'].create(vals)
acc_template_ref[account_template.id] = new_account.id
new_account = self.create_record_with_xmlid(company, account_template, 'account.account', vals)
acc_template_ref[account_template.id] = new_account
return acc_template_ref

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

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

1 comment on commit 01e7ed3

@blaggacao

This comment has been minimized.

Copy link
Contributor

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.