diff --git a/addons/stock/i18n/stock.pot b/addons/stock/i18n/stock.pot index 25452a8e85330562f90a1cd65f478fa8247e6337..91b28ee2a21b7ff7bfd80979ec7245c22780af04 100644 --- a/addons/stock/i18n/stock.pot +++ b/addons/stock/i18n/stock.pot @@ -8168,6 +8168,14 @@ msgid "" "it belonging to another company." msgstr "" +#. module: stock +#: code:addons/stock/models/product.py:0 +#, python-format +msgid "" +"This product's company cannot be changed as long as there are stock moves of " +"it belonging to another company." +msgstr "" + #. module: stock #: model:ir.model.fields,help:stock.field_stock_change_product_qty__new_quantity msgid "" diff --git a/addons/stock/models/product.py b/addons/stock/models/product.py index 4a6a10a619536f67c15c1053dc99dbc2bbd8d6bf..1f5394979d2dcae86301f21a9c06315c5a72f6e6 100644 --- a/addons/stock/models/product.py +++ b/addons/stock/models/product.py @@ -851,6 +851,13 @@ class ProductTemplate(models.Model): if 'company_id' in vals and vals['company_id']: products_changing_company = self.filtered(lambda product: product.company_id.id != vals['company_id']) if products_changing_company: + move = self.env['stock.move'].sudo().search([ + ('product_id', 'in', products_changing_company.product_variant_ids.ids), + ('company_id', 'not in', [vals['company_id'], False]), + ], order=None, limit=1) + if move: + raise UserError(_("This product's company cannot be changed as long as there are stock moves of it belonging to another company.")) + # 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), diff --git a/addons/stock/tests/test_product.py b/addons/stock/tests/test_product.py index 88ae0cd3ed6c9ef4bf0d2b1b48b1cf7a81ab565b..8a69883fd58faf15fab9d3f402bd57ba028c0956 100644 --- a/addons/stock/tests/test_product.py +++ b/addons/stock/tests/test_product.py @@ -130,6 +130,38 @@ class TestVirtualAvailable(TestStockCommon): self.env['stock.quant']._unlink_zero_quants() product.company_id = company2.id # Should work this time. + def test_change_product_company_02(self): + """ Checks we can't change the product's company if this product has + stock move line in another company. """ + company1 = self.env.ref('base.main_company') + company2 = self.env['res.company'].create({'name': 'Second Company'}) + product = self.env['product.product'].create({ + 'name': 'Product [TEST - Change Company]', + 'type': 'consu', + }) + picking = self.env['stock.picking'].create({ + 'location_id': self.env.ref('stock.stock_location_customers').id, + 'location_dest_id': self.env.ref('stock.stock_location_stock').id, + 'picking_type_id': self.ref('stock.picking_type_in'), + }) + self.env['stock.move'].create({ + 'name': 'test', + 'location_id': self.env.ref('stock.stock_location_customers').id, + 'location_dest_id': self.env.ref('stock.stock_location_stock').id, + 'product_id': product.id, + 'product_uom': product.uom_id.id, + 'product_uom_qty': 1, + 'picking_id': picking.id, + }) + picking.action_confirm() + wizard_data = picking.button_validate() + wizard = Form(self.env[wizard_data['res_model']].with_context(wizard_data['context'])).save() + wizard.process() + + product.company_id = company1.id + with self.assertRaises(UserError): + product.company_id = company2.id + 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"""