From d40956c7909bffa338fd0138565044571e852da3 Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Thu, 6 Dec 2018 12:14:05 +0000 Subject: [PATCH] [FIX] account: install a chart of account on another company 1. Create odoo instance with "--without-demo=ALL" option. 2. Install Accounting module. 3. Add Chart of Accounts manually (e.g. code 111111 Bank and Cash and 222222 Other Income) 4. Create Journal. 5. Create Journal Entries. 6. Activate multi-company. 7. Set new company to current user and set the new company as current company. 8. Install accounting localization: Accounting >> Configuration >> Settings >> Install More Packages >> (install any localization module) Before this commit, there was an error because when installing any module, the environment in which it happens is in uid = 1 and doesn't have a context anymore Thus, when arriving in the charts of accounts code, the env's user is still 1 Which doesn't mean anything in V12, since it is the system user After this commit, the user is retrieved from the request OPW 1911677 closes odoo/odoo#29310 --- addons/account/models/chart_template.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index 4c5c9645fef..317f98e99bf 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -5,6 +5,7 @@ from odoo import api, fields, models, _ from odoo import SUPERUSER_ID from odoo.exceptions import UserError from odoo.tools import pycompat +from odoo.http import request import logging @@ -164,7 +165,13 @@ class AccountChartTemplate(models.Model): of accounts had been created for it yet. """ self.ensure_one() - company = self.env.user.company_id + if request: + company_id = request.env.user.company_id.id + company = self.env['res.company'].browse(company_id) + else: + # fallback to company of current user, most likely __system__ + # (won't work well for multi-company) + company = self.env.user.company_id # If we don't have any chart of account on this company, install this chart of account if not company.chart_template_id: self.load_for_current_company(15.0, 15.0) @@ -178,7 +185,13 @@ class AccountChartTemplate(models.Model): rights. """ self.ensure_one() - company = self.env.user.company_id + if request: + company_id = request.env.user.company_id.id + company = self.env['res.company'].browse(company_id) + else: + # fallback to company of current user, most likely __system__ + # (won't work well for multi-company) + company = self.env.user.company_id # Ensure everything is translated to the company's language, not the user's one. self = self.with_context(lang=company.partner_id.lang) if not self.env.user._is_admin():