[FIX] account: ensure fiscal position belongs to the company of the invoice

When bank statements are imported using Enterprise's account_online_synchronization,
or when invoices are created from incoming email using mail aliases on
`account.journal`, journal entries can end up with a fiscal position from the default
company of the user that the `account.move` creation runs as, or the import fails
with an error "'Draft Entry' belongs to company X and 'Fiscal Position' belongs to
another company".

While account.move's `_compute_fiscal_company_id` calls `account.fiscal.position`'s
`_get_fiscal_position` with actual company of the move, it passes one or two
`res.partner` records to this method that were still browsed with the default company
of the executing user which could result in a fiscal position from that company,
instead of the company of the journal entry.

closes odoo/odoo#141059

X-original-commit: 71bd4f1378f56b333830cbc5aa11c1c55dcefcc3
Signed-off-by: William André (wan) <wan@odoo.com>
Co-authored-by: William Andre <wan@odoo.com>
This commit is contained in:
Stefan Rijnhart
2023-11-05 00:55:49 +00:00
co-authored by William Andre
parent df46124079
commit 57b713cb7e
2 changed files with 26 additions and 1 deletions
+4 -1
View File
@@ -237,7 +237,10 @@ class AccountFiscalPosition(models.Model):
delivery = partner
# partner manually set fiscal position always win
manual_fiscal_position = delivery.property_account_position_id or partner.property_account_position_id
manual_fiscal_position = (
delivery.with_company(company).property_account_position_id
or partner.with_company(company).property_account_position_id
)
if manual_fiscal_position:
return manual_fiscal_position
@@ -134,6 +134,28 @@ class TestAccountMove(AccountTestInvoicingCommon):
self.test_move.line_ids[0].account_id = custom_account
def test_fiscal_position_multicompany(self):
"""A move is assigned a fiscal position that matches its own company."""
company1 = self.company_data["company"]
company2 = self.company_data_2["company"]
partner = self.env['res.partner'].create({'name': 'Belouga'})
fpos1 = self.env["account.fiscal.position"].create(
{
"name": company1.name,
"company_id": company1.id,
}
)
fpos2 = self.env["account.fiscal.position"].create(
{
"name": company2.name,
"company_id": company2.id,
}
)
partner.with_company(company1).property_account_position_id = fpos1
partner.with_company(company2).property_account_position_id = fpos2
self.test_move.sudo().with_company(company2).partner_id = partner
self.assertEqual(self.test_move.fiscal_position_id, fpos1)
def test_misc_fiscalyear_lock_date_1(self):
self.test_move.action_post()