diff --git a/addons/stock/i18n/stock.pot b/addons/stock/i18n/stock.pot index 2fcdf9d49e685a6d69054c261b4b1a32ca45c9d6..163ec9345991e878ff1b734a47fb574846590e96 100644 --- a/addons/stock/i18n/stock.pot +++ b/addons/stock/i18n/stock.pot @@ -6632,6 +6632,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 8dbe5028d577eb1c9b8a4ad2996fde7742e3e0f7..62a52af8b2d999eb85d9cf27fb2cd1de153687f1 100644 --- a/addons/stock/models/product.py +++ b/addons/stock/models/product.py @@ -765,6 +765,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 9ee3044f4d5cd6f636c0921db61d74ddece2ea3b..fa3a13fdbb9d46cae69da9fc443867fea8d502db 100644 --- a/addons/stock/tests/test_product.py +++ b/addons/stock/tests/test_product.py @@ -133,6 +133,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"""