From a7ebb5151060f9435941b9da50182e4ec5b58db7 Mon Sep 17 00:00:00 2001
From: roen-odoo <roen@odoo.com>
Date: Mon, 11 Sep 2023 13:12:30 +0200
Subject: [PATCH] [FIX] point_of_sale: use correct partner_id for invoiced
 orders

Current behavior:
When invoicing an order that was made in a closed PoS session, the wrong
partner was used for the invoice. And so the total due was assigned to
the wrong partner if you used the "Customer Account" payment method.

Steps to reproduce:
- Open PoS and make an order for a customer using the Customer Account
  payment method.
- Close the PoS session.
- Open the PoS session again, and invoice the order you made in the
  previous session.

Fix:
Use the commercial_partner_id of the order's partner instead of the
partner_id directly.

opw-3478670

closes odoo/odoo#135007

Signed-off-by: Vlad Stroia (vlst) <vlst@odoo.com>
---
 addons/point_of_sale/models/pos_order.py              | 2 +-
 addons/point_of_sale/tests/test_point_of_sale_flow.py | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/addons/point_of_sale/models/pos_order.py b/addons/point_of_sale/models/pos_order.py
index 6c9e540771bc..d3e820f1f563 100644
--- a/addons/point_of_sale/models/pos_order.py
+++ b/addons/point_of_sale/models/pos_order.py
@@ -775,7 +775,7 @@ class PosOrder(models.Model):
                 aml_vals_entry_found[0]['balance'] += payment_id.amount
             else:
                 aml_vals_list_per_nature['payment_terms'].append({
-                    'partner_id': self.partner_id.id if is_split_transaction else False,
+                    'partner_id': commercial_partner.id if is_split_transaction else False,
                     'name': f"{reversed_move_receivable_account_id.code} {reversed_move_receivable_account_id.code}",
                     'account_id': reversed_move_receivable_account_id.id,
                     'currency_id': self.currency_id.id,
diff --git a/addons/point_of_sale/tests/test_point_of_sale_flow.py b/addons/point_of_sale/tests/test_point_of_sale_flow.py
index dbe9129871a8..952d03bfe747 100644
--- a/addons/point_of_sale/tests/test_point_of_sale_flow.py
+++ b/addons/point_of_sale/tests/test_point_of_sale_flow.py
@@ -1549,6 +1549,7 @@ class TestPointOfSaleFlow(TestPointOfSaleCommon):
             'type': 'product',
             'categ_id': self.env.ref('product.product_category_all').id,
         })
+        self.partner1.write({'parent_id': self.env['res.partner'].create({'name': 'Parent'}).id})
 
         #add customer account payment method to pos config
         self.pos_config.write({
@@ -1607,6 +1608,7 @@ class TestPointOfSaleFlow(TestPointOfSaleCommon):
         reverser_customer_payment_entry = reverse_payment.line_ids.filtered(lambda l: l.account_id.account_type == 'asset_receivable')
         #check that both use the same account
         self.assertEqual(len(reverser_customer_payment_entry), 2)
+        self.assertTrue(order.account_move.line_ids.partner_id == self.partner1.commercial_partner_id)
         self.assertEqual(reverser_customer_payment_entry[0].balance, -2.0)
         self.assertEqual(reverser_customer_payment_entry[1].balance, -4.0)
         self.assertEqual(original_customer_payment_entry.account_id.id, reverser_customer_payment_entry.account_id.id)
-- 
GitLab