From a6666de7f492f9f94abc23a080134dd473d0dddc Mon Sep 17 00:00:00 2001 From: "Touati Djamel (otd)" <otd@odoo.com> Date: Fri, 5 May 2023 15:57:21 +0000 Subject: [PATCH] [FIX] stock: change product's company MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps to reproduce the bug: - Connect with the company A - Create a consumable product “P1†- Create a receipt transfer with this product - confirm the transfer - Come back to the product form - limiter le produit que a la “Company B†Problem: no user error triggered Go to inventory > operation > transfer: a Traceback is triggered Before this commit, there is no verification while changing a product's company for consumable. That can lead to an issue where some operations cannot be done because of access errors. To avoid that, this commit prevents to change the product's company if some move lines for this product exist in another company. opw-3300559 closes odoo/odoo#120702 Signed-off-by: Djamel Touati (otd) <otd@odoo.com> --- addons/stock/i18n/stock.pot | 8 ++++++++ addons/stock/models/product.py | 7 +++++++ addons/stock/tests/test_product.py | 32 ++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/addons/stock/i18n/stock.pot b/addons/stock/i18n/stock.pot index 2fcdf9d49e68..163ec9345991 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 8dbe5028d577..62a52af8b2d9 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 9ee3044f4d5c..fa3a13fdbb9d 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""" -- GitLab