diff --git a/addons/stock/models/product.py b/addons/stock/models/product.py index f25a143137b..16989bbc693 100644 --- a/addons/stock/models/product.py +++ b/addons/stock/models/product.py @@ -843,7 +843,7 @@ class ProductTemplate(models.Model): # Forbid changing a product's company when quant(s) exist in another company. quant = self.env['stock.quant'].sudo().search([ ('product_id', 'in', products_changing_company.product_variant_ids.ids), - ('company_id', '!=', vals['company_id']), + ('company_id', 'not in', [vals['company_id'], False]), ('quantity', '!=', 0), ], order=None, limit=1) if quant: diff --git a/addons/stock/tests/test_product.py b/addons/stock/tests/test_product.py index 08e713c6155..88ae0cd3ed6 100644 --- a/addons/stock/tests/test_product.py +++ b/addons/stock/tests/test_product.py @@ -130,6 +130,47 @@ class TestVirtualAvailable(TestStockCommon): self.env['stock.quant']._unlink_zero_quants() product.company_id = company2.id # Should work this time. + def test_change_product_company_exclude_vendor_and_customer_location(self): + """ Checks we can change product company where only exist single company + and exist quant in vendor/customer location""" + company1 = self.env.ref('base.main_company') + customer_location = self.env.ref('stock.stock_location_customers') + supplier_location = self.env.ref('stock.stock_location_suppliers') + product = self.env['product.product'].create({ + 'name': 'Product Single Company', + 'type': 'product', + }) + # Creates a quant for company 1. + self.env['stock.quant'].create({ + 'product_id': product.id, + 'product_uom_id': self.uom_unit.id, + 'location_id': self.location_1.id, + 'quantity': 5, + }) + # Creates a quant for vendor location. + self.env['stock.quant'].create({ + 'product_id': product.id, + 'product_uom_id': self.uom_unit.id, + 'location_id': supplier_location.id, + 'quantity': -15, + }) + # Creates a quant for customer location. + self.env['stock.quant'].create({ + 'product_id': product.id, + 'product_uom_id': self.uom_unit.id, + 'location_id': customer_location.id, + 'quantity': 10, + }) + # Assigns a company: should be ok because only exist one company (exclude vendor and customer location) + product.company_id = company1.id + + # Reset product company to empty + product.company_id = False + company2 = self.env['res.company'].create({'name': 'Second Company'}) + # Assigns to another company: should be not okay because exist quants in defferent company (exclude vendor and customer location) + with self.assertRaises(UserError): + product.company_id = company2.id + def test_search_qty_available(self): product = self.env['product.product'].create({ 'name': 'Brand new product',