Skip to content
Snippets Groups Projects
Commit a6666de7 authored by Touati Djamel (otd)'s avatar Touati Djamel (otd)
Browse files

[FIX] stock: change product's company


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: default avatarDjamel Touati (otd) <otd@odoo.com>
parent 8848bb57
Branches
Tags
No related merge requests found
......@@ -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 ""
......
......@@ -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),
......
......@@ -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"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment