From eacb2a7d514e8253c4ed146684fa8c7addd03b93 Mon Sep 17 00:00:00 2001 From: "Mattis Megevand (mmeg)" Date: Tue, 23 Jan 2024 11:18:36 +0100 Subject: [PATCH] [FIX] sale_purchase: use the associated company with SO on service PO Steps to reproduce: - Setup two companies and a product - For one of the company activate "Subcontract Service" on the product - Make an SO with the product and in Other Infos set the company with the one that has subcontract service on - Confirm the SO with the company that doesn't have it on Issue: No PO is generated. Solution: Make sure we got the right context when generating purchase. opw-3622278 closes odoo/odoo#150626 Related: odoo/enterprise#54316 Signed-off-by: William Henrotin (whe) --- .../sale_purchase/models/sale_order_line.py | 1 + .../sale_purchase/tests/test_sale_purchase.py | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/addons/sale_purchase/models/sale_order_line.py b/addons/sale_purchase/models/sale_order_line.py index 161cea9f333..f7616aac3f8 100644 --- a/addons/sale_purchase/models/sale_order_line.py +++ b/addons/sale_purchase/models/sale_order_line.py @@ -283,6 +283,7 @@ class SaleOrderLine(models.Model): """ sale_line_purchase_map = {} for line in self: + line = line.with_company(line._purchase_service_get_company()) # Do not regenerate PO line if the SO line has already created one in the past (SO cancel/reconfirmation case) if line.product_id.service_to_purchase and not line.purchase_line_count: result = line._purchase_service_create() diff --git a/addons/sale_purchase/tests/test_sale_purchase.py b/addons/sale_purchase/tests/test_sale_purchase.py index 0347a70b700..c53de9a8483 100644 --- a/addons/sale_purchase/tests/test_sale_purchase.py +++ b/addons/sale_purchase/tests/test_sale_purchase.py @@ -305,3 +305,38 @@ class TestSalePurchase(TestCommonSalePurchaseNoChart): sale_order.action_confirm() pol = sale_order._get_purchase_orders().order_line self.assertEqual(pol.name, f"{self.service_purchase_1.display_name}\n\n{product_attribute.name}: {product_attribute_value.name}: {custom_value}") + + def test_sale_order_multi_comp(self): + ''' Test that the purchase service is done on the right company (the one of the SO) ''' + company_a, company_b = self.company_data["company"], self.company_data_2["company"] + + service_purchase = self.env['product.product'].with_company(company_a).create({ + 'name': "service 1", + 'purchase_ok': True, + 'sale_ok': True, + 'list_price': 50, + 'type': 'service', + 'service_to_purchase': True, + 'seller_ids': [ + (0, 0, {'partner_id': self.partner_b.id, 'price': 100, 'company_id': company_a.id}), + ], + }) + self.assertFalse(service_purchase.with_company(company_b).service_to_purchase) + + so = self.env['sale.order'].create({ + 'partner_id': self.partner_a.id, + 'company_id': company_a.id, + 'order_line': [ + (0, 0, { + 'name': service_purchase.name, + 'product_id': service_purchase.id, + 'product_uom_qty': 1, + }) + ], + }) + + self.env.user.company_id = company_b + so.action_confirm() + + po = self.env['purchase.order'].search([('partner_id', '=', self.partner_b.id)], limit=1) + self.assertTrue(po)