Skip to content

Commit

Permalink
TA#72146 [16.0][MIG] product_category_safe_change (#206)
Browse files Browse the repository at this point in the history
Co-authored-by: Lanto R. <[email protected]>
Co-authored-by: Majda EL MARIOULI <[email protected]>
  • Loading branch information
3 people authored Feb 4, 2025
1 parent e3bc283 commit 92ea8ce
Show file tree
Hide file tree
Showing 18 changed files with 434 additions and 5 deletions.
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ jobs:
name: Setup Log Folder For Reports
command: sudo mkdir -p .log && sudo chmod 777 .log

# - run:
# name: Run Test
# command: docker-compose run --rm odoo run_pytest.sh
- run:
name: Run Test
command: docker-compose run --rm odoo run_pytest.sh

# - store_test_results:
# path: .log
- store_test_results:
path: .log

# job that find the next tag for the current branch/repo and push the tag to github.
# it will trigger the publish of a new docker image.
Expand Down
1 change: 1 addition & 0 deletions .docker_files/main/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"category": "Other",
"summary": "Install all addons required for testing.",
"depends": [
"product_category_safe_change",
"stock_account_visibility",
"stock_move_origin_link",
"stock_picking_show_address",
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RUN gitoo install-all --conf_file /gitoo.yml --destination "${THIRD_PARTY_ADDONS

USER odoo

COPY product_category_safe_change /mnt/extra-addons/product_category_safe_change
COPY stock_account_visibility /mnt/extra-addons/stock_account_visibility
COPY stock_move_origin_link /mnt/extra-addons/stock_move_origin_link
COPY stock_picking_show_address /mnt/extra-addons/stock_picking_show_address
Expand Down
83 changes: 83 additions & 0 deletions product_category_safe_change/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Product Category Safe Change
============================
This module allows to restrict the modification of a product category if the product linked to it has already some stock move
associated to it.

Usage
-----
* Restricting the modification of a category on product

As a user who can edit a product, I go to a product which had already some stock move.
I edit the product and change the category of the product.
I click on the `Save` button.
I get the following blocking error message:

.. image:: https://raw.githubusercontent.com/Numigi/odoo-stock-addons/16.0/product_category_safe_change/static/description/product_variant_category.png

.. image:: https://raw.githubusercontent.com/Numigi/odoo-stock-addons/16.0/product_category_safe_change/static/description/product_variant_new_category.png

.. image:: https://raw.githubusercontent.com/Numigi/odoo-stock-addons/16.0/product_category_safe_change/static/description/product_variant_category_validate_error.png


* Restricting Inventory Properties Setup Changes

As a user in the `Inventory/Manager` group, I go to an product category (on `Inventory>Configuration`).
I have access to modify a category.
I try to modify one of the following fields (only available on view when `Inventory Valuation` value is ``Automated``(real_time)):
-Stock Input Account
-Stock Output Account
-Stock Valuation Account
-Stock Journal
And then click on the `Save` button.
If I have products with this category that have stock move, I get the following blocking error message:
.. image:: https://raw.githubusercontent.com/Numigi/odoo-stock-addons/16.0/product_category_safe_change/static/description/product_category_account_updated.png
Multi company contrainsts
-------------------------
Examples : in the context of a multi-company database, with shared articles:
The data is as follows:
Companies: Company A, Company B
Category: Category 1
Product: Product 1 (case where I only have this product in the database)
On the database, I have the following stock moves:
* Case 1:
+-----------------+--------------------+-----------+-----------------------+
| Stock move name | Stock move Company | Product | Constraint applied to |
+=================+====================+===========+=======================+
| BL001 | Company A | Product 1 | Company A only |
+-----------------+--------------------+-----------+-----------------------+
Expected result: I can modify the category settings under Company B. I cannot modify the product category since it is common to all companies.
* Case 2:
+-----------------+--------------------+-----------+-------------------------+
| Stock move name | Stock move Company | Product | Constraint applied to |
+=================+====================+===========+=========================+
| BL001 | Company A | Product 1 | Company A and company B |
| BL002 | Company B | Product 1 | Company A and company B |
+-----------------+--------------------+-----------+-------------------------+
Expected result: I cannot change the category settings under any of the companies (A and B). I cannot modify the category of the product since it is common to all companies.
* Case 3:
+-----------------+--------------------+-----------+-----------------------+
| Stock move name | Stock move Company | Product | Constraint applied to |
+=================+====================+===========+=======================+
| BL001 | Company B | Product 1 | Company B only |
+-----------------+--------------------+-----------+-----------------------+
Expected result: I can modify the category settings under Company A. I cannot modify the item category since it is common to all companies.
NOTICE :
This behavior is applied in the same way when I have several articles in the database:
1. If the user tries to modify the parameters of a category under a company for which there is no stock.move associated via the products, the constraint is not applied.
2. If the user tries to modify the category of an article, while there is a stock movement associated with the article in the database (all companies combined), then the constraint applies.
Contributors
------------
* Numigi (tm) and all its contributors (https://bit.ly/numigiens)
4 changes: 4 additions & 0 deletions product_category_safe_change/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2023 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
17 changes: 17 additions & 0 deletions product_category_safe_change/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2023 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Product Category Safe Change",
"version": "16.0.1.0.0",
"author": "Numigi",
"maintainer": "Numigi",
"website": "https://bit.ly/numigi-com",
"license": "AGPL-3",
"category": "Stock",
"depends": ["stock_account"],
"summary": "Add some restriction to the product category of articles",
"data": [],
"installable": True,
"auto_install": True,
}
60 changes: 60 additions & 0 deletions product_category_safe_change/i18n/fr.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * product_category_safe_change
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-01-31 12:20+0000\n"
"PO-Revision-Date: 2025-01-31 12:20+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: product_category_safe_change
#. odoo-python
#: code:addons/product_category_safe_change/models/product_category.py:0
#, python-format
msgid ""
"\n"
" You cannot modify Stock Properties Parameters when\n"
" related Products have existing Stock Moves.\n"
"\n"
" Stock moves exist for the following products:\n"
" - "
msgstr ""
"\n"
" Vous ne pouvez pas modifier le paramétrage des propriétés de stock \n"
" si un des articles de la catégorie a des mouvements de stock associés.\n"
"\n"
" Des mouvements de stock sont existants pour les articles suivants:\n"
" - "

#. module: product_category_safe_change
#: model:ir.model,name:product_category_safe_change.model_product_template
msgid "Product"
msgstr "Produit"

#. module: product_category_safe_change
#: model:ir.model,name:product_category_safe_change.model_product_category
msgid "Product Category"
msgstr "Catégorie de produit"

#. module: product_category_safe_change
#: model:ir.model,name:product_category_safe_change.model_product_product
msgid "Product Variant"
msgstr "Variante de produit"

#. module: product_category_safe_change
#. odoo-python
#: code:addons/product_category_safe_change/models/product.py:0
#: code:addons/product_category_safe_change/models/product_template.py:0
#, python-format
msgid "You cannot modify the category of a Product with Stock Moves."
msgstr ""
"Vous ne pouvez pas modifier la catégorie d’un article ayant des mouvements "
"de stocks associés."
6 changes: 6 additions & 0 deletions product_category_safe_change/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright 2023 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import product
from . import product_category
from . import product_template
25 changes: 25 additions & 0 deletions product_category_safe_change/models/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2023 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, models
from odoo.exceptions import UserError


class Product(models.Model):
_inherit = "product.product"

@api.constrains("categ_id")
def _check_category_stock_move(self):
for product in self:
if product.type != "service":
existing_move_lines = (
self.env["stock.move.line"]
.sudo()
.search([("product_id", "=", product.id)])
)
if existing_move_lines:
raise UserError(
_(
"You cannot modify the category of a Product with Stock Moves."
)
)
41 changes: 41 additions & 0 deletions product_category_safe_change/models/product_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2023 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, models
from odoo.exceptions import UserError


class ProductCategory(models.Model):
_inherit = "product.category"

@api.constrains(
"property_stock_account_input_categ_id",
"property_stock_account_output_categ_id",
"property_stock_valuation_account_id",
"property_stock_journal",
)
def _check_category_stock_move(self):
for category in self:
domain = [
("product_id.categ_id", "=", category.id),
("company_id", "=", self.env.company.id),
]
existing_move_lines = self.env["stock.move.line"].sudo().search(domain)

if existing_move_lines:
# Select only three first products found on move lines
# if there is more than 3
product_lists = existing_move_lines.mapped("product_id.name")
product_lists = (
product_lists[:3] if len(product_lists) > 3 else product_lists
)
products = "\n- ".join(product_lists)

message = _(
"""
You cannot modify Stock Properties Parameters when
related Products have existing Stock Moves.\n
Stock moves exist for the following products:
- """
)
raise UserError(message + products)
25 changes: 25 additions & 0 deletions product_category_safe_change/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2023 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, models
from odoo.exceptions import UserError


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

@api.constrains("categ_id")
def _check_category_stock_move(self):
for product in self:
if product.type != "service":
existing_move_lines = (
self.env["stock.move.line"]
.sudo()
.search([("product_id.product_tmpl_id", "=", product.id)])
)
if existing_move_lines:
raise UserError(
_(
"You cannot modify the category of a Product with Stock Moves."
)
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions product_category_safe_change/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2023 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import test_product_category
Loading

0 comments on commit 92ea8ce

Please sign in to comment.