'This function is called at the confirmation of the wizard to generate the COA from the templates. It will read
all the provided information to create the accounts, the banks, the journals, the taxes, the
accounting properties... accordingly for the chosen company.'

@api.multi
def execute(self):

    if (len(self.env['account.account'].search([('company_id', '=', self.company_id.id)])) > 0):
        _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'))
    ir_values_obj = self.env['ir.values']
    company = self.company_id
    self.company_id.write({'currency_id': self.currency_id.id, 'accounts_code_digits': self.code_digits, 'anglo_saxon_accounting': self.use_anglo_saxon, 'bank_account_code_prefix': self.bank_account_code_prefix, 'cash_account_code_prefix': self.cash_account_code_prefix, 'chart_template_id': self.chart_template_id.id})
    self.currency_id.write({'active': True})
    if (company.id == 1):
        for reference in ['product.list_price', 'product.standard_price', 'product.list0']:
            try:
                tmp2 = self.env.ref(reference).write({'currency_id': self.currency_id.id})
            except ValueError:
                pass
    self._create_tax_templates_from_rates(company.id)
    (acc_template_ref, taxes_ref) = self.chart_template_id._install_template(company, code_digits=self.code_digits, transfer_account_id=self.transfer_account_id)
    if (self.sale_tax_id and taxes_ref):
        ir_values_obj.sudo().set_default('product.template', 'taxes_id', [taxes_ref[self.sale_tax_id.id]], for_all_users=True, company_id=company.id)
    if (self.purchase_tax_id and taxes_ref):
        ir_values_obj.sudo().set_default('product.template', 'supplier_taxes_id', [taxes_ref[self.purchase_tax_id.id]], for_all_users=True, company_id=company.id)
    self._create_bank_journals_from_o2m(company, acc_template_ref)
    account_obj = self.env['account.account']
    unaffected_earnings_xml = self.env.ref('account.data_unaffected_earnings')
    if (unaffected_earnings_xml and (not account_obj.search([('company_id', '=', company.id), ('user_type_id', '=', unaffected_earnings_xml.id)]))):
        account_obj.create({'code': '999999', 'name': _('Undistributed    Profits/Losses'), 'user_type_id': unaffected_earnings_xml.id, 'company_id': company.id})
    return {}
