diff --git a/addons/stock/i18n/stock.pot b/addons/stock/i18n/stock.pot index 40882a96f2e29d91ff55836d8a66eb2d8ce5dbaf..9f65752086030c2c3be5f5ebe56b93689cdf6edc 100644 --- a/addons/stock/i18n/stock.pot +++ b/addons/stock/i18n/stock.pot @@ -7624,6 +7624,14 @@ msgid "" " 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 a77e7562ce648f7a329e21fc8c21d5e0497e657f..92dd5925205bffed4d2af337cbaf5e4abaec4a16 100644 --- a/addons/stock/models/product.py +++ b/addons/stock/models/product.py @@ -860,6 +860,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 9614a76347d9939f9d05c234f30c2f58d35fe98f..588e9b7ea4525a86bb18d756577cfb7435753e5a 100644 --- a/addons/stock/tests/test_product.py +++ b/addons/stock/tests/test_product.py @@ -134,6 +134,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"""