[FIX] sale: get correct journal to create corresponding invoice

Be in company C1. Create the invoice I for a SO in company C2.
The invoice is created on the default account.journal for C1.
This happens when e.g. C2 is a child company of C1.
Then I is accessible from C1, but not from C2
as reading the journal violates the multi-company record rules.

Note that if company_id == False, then
self.env['account.invoice'].with_context(company_id=company_id)
    .default_get(['journal_id'])['journal_id']
returns False, and in that case, it would raise below.
To keep with the existing behaviour, we put a fallback on
self.env.user.company_id.id
which means that the values we pass are:
'company_id': False, 'journal_id': J for some J in company C
which is flaky, but won't trigger any extra errors.

opw 2068291

closes odoo/odoo#37002

X-original-commit: 427280d9760a71e6ca2961bcf4a2f9f157e6dddd
Signed-off-by: Nans Lefebvre (len) <len@odoo.com>
This commit is contained in:
Nans Lefebvre
2019-09-17 11:36:57 +00:00
committed by fw-bot
parent d82db0a6bd
commit 66e1049d85
+4 -2
View File
@@ -448,7 +448,9 @@ class SaleOrder(models.Model):
a clean extension chain).
"""
self.ensure_one()
journal_id = self.env['account.invoice'].default_get(['journal_id'])['journal_id']
company_id = self.company_id.id
journal_id = (self.env['account.invoice'].with_context(company_id=company_id or self.env.user.company_id.id)
.default_get(['journal_id'])['journal_id'])
if not journal_id:
raise UserError(_('Please define an accounting sales journal for this company.'))
invoice_vals = {
@@ -463,7 +465,7 @@ class SaleOrder(models.Model):
'comment': self.note,
'payment_term_id': self.payment_term_id.id,
'fiscal_position_id': self.fiscal_position_id.id or self.partner_invoice_id.property_account_position_id.id,
'company_id': self.company_id.id,
'company_id': company_id,
'user_id': self.user_id and self.user_id.id,
'team_id': self.team_id.id,
'transaction_ids': [(6, 0, self.transaction_ids.ids)],