diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 81fe7ef0045..1e98c038495 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -523,7 +523,8 @@ class PurchaseOrder(models.Model): for line in self.order_line: # Do not add a contact as a supplier partner = self.partner_id if not self.partner_id.parent_id else self.partner_id.parent_id - if line.product_id and partner not in line.product_id.seller_ids.partner_id and len(line.product_id.seller_ids) <= 10: + already_seller = (partner | self.partner_id) & line.product_id.seller_ids.mapped('partner_id') + if line.product_id and not already_seller and len(line.product_id.seller_ids) <= 10: # Convert the price in the right currency. currency = partner.property_purchase_currency_id or self.env.company.currency_id price = self.currency_id._convert(line.price_unit, currency, line.company_id, line.date_order or fields.Date.today(), round=False) diff --git a/addons/purchase/tests/test_purchase.py b/addons/purchase/tests/test_purchase.py index bf9293d9a16..decb799cbee 100644 --- a/addons/purchase/tests/test_purchase.py +++ b/addons/purchase/tests/test_purchase.py @@ -333,3 +333,42 @@ class TestPurchase(AccountTestInvoicingCommon): po_line.product_id = product purchase_order_coco = po_form.save() self.assertEqual(purchase_order_coco.order_line.price_unit, currency_rate.rate * product.standard_price, "Value shouldn't be rounded 🍫") + + def test_purchase_not_creating_useless_product_vendor(self): + """ This test ensures that the product vendor is not created when the + product is not set on the purchase order line. + """ + + #create a contact of type contact + contact = self.env['res.partner'].create({ + 'name': 'Contact', + 'type': 'contact', + }) + + #create a contact of type Delivery Address lnked to the contact + delivery_address = self.env['res.partner'].create({ + 'name': 'Delivery Address', + 'type': 'delivery', + 'parent_id': contact.id, + }) + + #create a product that use the delivery address as vendor + product = self.env['product.product'].create({ + 'name': 'Product A', + 'seller_ids': [(0, 0, { + 'partner_id': delivery_address.id, + 'min_qty': 1.0, + 'price': 1.0, + })] + }) + + #create a purchase order with the delivery address as partner + po_form = Form(self.env['purchase.order']) + po_form.partner_id = delivery_address + with po_form.order_line.new() as po_line: + po_line.product_id = product + po_line.product_qty = 1.0 + po = po_form.save() + po.button_confirm() + + self.assertEqual(po.order_line.product_id.seller_ids.mapped('partner_id'), delivery_address)