From 07677d5f97f00b301fa2e30a3aeb9fe2da9ccffb Mon Sep 17 00:00:00 2001
From: Roy Le <roy.le@viindoo.com>
Date: Wed, 13 Oct 2021 01:35:34 +0000
Subject: [PATCH] [FW][FIX] sale: conflicting context content

Most uses (correct ones) of the `partner` context key content expect a `res.partner` recordset as `partner` context value.
This value is extracted during the pricelist price computation (but not used anyway...).

But in one case, fixed by the current commit, a `res.partner` id is placed in the context, as `partner` value.  In some cases, this may trigger "Comparing Apple and Oranges" errors, since `with_context` calls returns a new or existing environment, verifying whether an environment with the same values (user, context, ...) exists.
During this comparison, the new context, with an id (int) as `partner` value, is compared with existing contexts, potentially including some with a recordset on the same key.  Such a comparison fails on the lowest `__eq__` level, raising "Comparing apples with oranges" error.

This commit fixes this case, by making sure the value put in the `partner` context value is always a recordset, and not an id.

closes odoo/odoo#78936

Note: In the future, this context key should be dropped because it's still a bad practice to put recordsets in the context.
Forward-port-of: odoo/odoo#78276
Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
---
 addons/sale/models/account_move.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/addons/sale/models/account_move.py b/addons/sale/models/account_move.py
index 3e2f588ab1c0..54d691554445 100644
--- a/addons/sale/models/account_move.py
+++ b/addons/sale/models/account_move.py
@@ -208,7 +208,7 @@ class AccountMoveLine(models.Model):
 
         if self.product_id.expense_policy == 'sales_price':
             product = self.product_id.with_context(
-                partner=order.partner_id.id,
+                partner=order.partner_id,
                 date_order=order.date_order,
                 pricelist=order.pricelist_id.id,
                 uom=self.product_uom_id.id,
-- 
GitLab