Skip to content

Commit

Permalink
TA#58039 [FIX] product_category_safe_change: Code Quality Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
majouda committed Jun 3, 2024
1 parent 1e73fdc commit 5e9f6de
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
2 changes: 1 addition & 1 deletion product_category_safe_change/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "Product Category Safe Change",
"version": "14.0.1.0.0",
"version": "14.0.1.0.1",
"author": "Numigi",
"maintainer": "Numigi",
"website": "https://bit.ly/numigi-com",
Expand Down
14 changes: 8 additions & 6 deletions product_category_safe_change/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,33 @@ class Product(models.Model):
_inherit = "product.product"

def _check_category_stock_move(self):
existing_move_lines = self.env['stock.move.line'].search([
existing_move_lines = self.env['stock.move.line'].sudo().search([
('product_id', 'in', self.ids)
])
if existing_move_lines:
raise UserError(
_("You cannot modify the category of a Product with Stock Moves."))

def write(self, vals):
if 'categ_id' in vals:
self._check_category_stock_move()
for rec in self:
if rec.type != "service" and "categ_id" in vals:
rec._check_category_stock_move()
return super(Product, self).write(vals)


class ProductTemplate(models.Model):
_inherit = 'product.template'

def _check_category_stock_move(self):
existing_move_lines = self.env['stock.move.line'].search([
existing_move_lines = self.env['stock.move.line'].sudo().search([
('product_id.product_tmpl_id', 'in', self.ids)
])
if existing_move_lines:
raise UserError(
_("You cannot modify the category of a Product with Stock Moves."))

def write(self, vals):
if 'categ_id' in vals:
self._check_category_stock_move()
for rec in self:
if rec.type != "service" and "categ_id" in vals:
rec._check_category_stock_move()
return super(ProductTemplate, self).write(vals)
22 changes: 6 additions & 16 deletions product_category_safe_change/models/product_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,13 @@
class ProductCategory(models.Model):
_inherit = "product.category"

def _get_product_name(self, product_id):
return self.env["product.product"].browse(product_id).name

def _multi_company_constraints(self, existing_move_lines):
current_company = self.env.user.company_id.id
if existing_move_lines:
if current_company not in existing_move_lines.sudo().mapped(
"move_id.company_id.id"
):
return False
return True

def _check_category_stock_move(self):
domain = [("product_id.categ_id", "in", self.ids)]
existing_move_lines = self.env["stock.move.line"].search(domain)
not_allowed = self._multi_company_constraints(existing_move_lines)
if len(existing_move_lines) and not_allowed:
domain = [
("product_id.categ_id", "in", self.ids),
("company_id", "=", self.env.company.id)
]
existing_move_lines = self.env["stock.move.line"].sudo().search(domain)
if len(existing_move_lines):
product_lists = existing_move_lines.mapped("product_id.name")
product_lists = (
# select three first products found on move lines
Expand Down
9 changes: 4 additions & 5 deletions product_category_safe_change/tests/test_product_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ def test_update_product_category_with_stock_move(self):
move = self.process_stock_move(self.company_a, self.stock_location_a)
categ_all = self.env.ref('product.product_category_all')

domain = [("product_id.categ_id", "in", categ_all.ids)]
domain = [
("product_id.categ_id", "in", categ_all.ids),
("company_id", "=", self.env.company.id)
]
existing_move_lines = self.env["stock.move.line"].search(domain)
not_allowed = categ_all._multi_company_constraints(
existing_move_lines)

self.assertGreater(len(existing_move_lines), 0)
self.assertTrue(not_allowed)
self.assertEqual(move.company_id, self.env.user.company_id)

# for property_stock_account_input_categ_id
Expand Down

0 comments on commit 5e9f6de

Please sign in to comment.