-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] product_multi_barcode_constraint_per_company: new module
- Loading branch information
1 parent
21f10bb
commit 92adfe2
Showing
13 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models |
19 changes: 19 additions & 0 deletions
19
product_multi_barcode_constraint_per_company/__manifest__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright 2023 Cetmix OÜ | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
{ | ||
"name": "Product Multi Barcode Constraint per Company", | ||
"version": "14.0.1.0.0", | ||
"category": "Product", | ||
"summary": """This is a bridge module between "product_multi_barcode" and | ||
"product_barcode_constraint_per_company" """, | ||
"author": "Ooops, Cetmix, Odoo Community Association (OCA)", | ||
"website": "https://github.com/OCA/stock-logistics-barcode", | ||
"license": "AGPL-3", | ||
"depends": [ | ||
"product_multi_barcode", | ||
"product_barcode_constraint_per_company", | ||
], | ||
"installable": True, | ||
"auto_install": True, | ||
} |
1 change: 1 addition & 0 deletions
1
product_multi_barcode_constraint_per_company/models/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import product_barcode |
23 changes: 23 additions & 0 deletions
23
product_multi_barcode_constraint_per_company/models/product_barcode.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2023 Cetmix OÜ | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
from odoo import api, models | ||
|
||
|
||
class ProductBarcode(models.Model): | ||
_inherit = "product.barcode" | ||
|
||
@api.constrains("name") | ||
def _check_duplicates(self): | ||
existing_codes = self.env["product.barcode"] | ||
for record in self.sudo(): | ||
barcode = self.search( | ||
[("id", "!=", record.id), ("name", "=", record.name)], limit=1 | ||
) | ||
if barcode and ( | ||
not record.product_id.company_id | ||
or (barcode.product_id.company_id == record.product_id.company_id) | ||
): | ||
existing_codes |= record | ||
if existing_codes: | ||
super(ProductBarcode, existing_codes)._check_duplicates() |
4 changes: 4 additions & 0 deletions
4
product_multi_barcode_constraint_per_company/readme/CONTRIBUTORS.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
* Ooops <https://ooops404.com> | ||
* Francesco Foresti <francesco.foresti@ooops404.com> | ||
* Cetmix <https://cetmix.com> | ||
* Dessan Hemrayev <dessanhemrayev@gmail.com> |
1 change: 1 addition & 0 deletions
1
product_multi_barcode_constraint_per_company/readme/DESCRIPTION.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a bridge module between "product_barcode_constraint_per_company" and "product_multi_barcode" |
Empty file.
Binary file added
BIN
+9.23 KB
product_multi_barcode_constraint_per_company/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import test_product_barcode_constraint |
146 changes: 146 additions & 0 deletions
146
product_multi_barcode_constraint_per_company/tests/test_product_barcode_constraint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# Copyright 2023 Cetmix OÜ | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
from psycopg2 import IntegrityError | ||
|
||
from odoo import _ | ||
from odoo.exceptions import UserError | ||
from odoo.tools.misc import mute_logger | ||
|
||
from odoo.addons.product_barcode_constraint_per_company.tests.common import ( | ||
CommonProductBarcodeConstraintPerCompany, | ||
) | ||
|
||
|
||
class TestProductBarcodeConstraint(CommonProductBarcodeConstraintPerCompany): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) | ||
cls.valid_barcode_1 = "1234567890128123" | ||
cls.valid_barcode2_1 = "0123456789012123" | ||
cls.product_test_1 = cls.product_product_obj.create( | ||
{"name": "Test product 1", "company_id": False} | ||
) | ||
cls.product_test_2 = cls.product_product_obj.create( | ||
{"name": "Test product 2", "company_id": False} | ||
) | ||
|
||
def test_create_same_company(self): | ||
"""Verifying the Existence of a Product with the Same Barcode within a Single Company""" | ||
# Create the first product in company_1 | ||
product_1 = self._create_product("Product 1", self.company_1.id) | ||
# Check if the company_id of the product matches self.company_1 | ||
self.assertEqual( | ||
product_1.company_id, | ||
self.company_1, | ||
msg="Product company ID must be equal to ID #{company}".format( | ||
company=self.company_1.id | ||
), | ||
) | ||
|
||
# Check if the company_id of the product_tmpl_id matches self.company_1 | ||
self.assertEqual( | ||
product_1.product_tmpl_id.company_id, | ||
self.company_1, | ||
msg="Product company ID must be equal to ID # {company}".format( | ||
company=self.company_1.id | ||
), | ||
) | ||
|
||
# Try to create a second product with the same barcode in company_1 | ||
# and expect an IntegrityError | ||
with mute_logger("odoo.sql_db"), self.assertRaises( | ||
IntegrityError, | ||
msg="A barcode can only be assigned to one product per company !", | ||
): | ||
self._create_product("Product 2", self.company_1.id) | ||
|
||
def test_create_different_company(self): | ||
"""Verify creating a product with the same barcode for different companies""" | ||
# Create the first product in company_1 | ||
product_1 = self._create_product("Product 1", self.company_1.id) | ||
|
||
# Create the second product in company_2 | ||
product_2 = self._create_product("Product 2", self.company_2.id) | ||
|
||
# Check that the products belong to different companies | ||
self.assertNotEqual( | ||
product_1.company_id, | ||
product_2.company_id, | ||
msg="Products should belong to different companies", | ||
) | ||
|
||
# Check that the products have the same barcode | ||
self.assertEqual( | ||
product_1.barcode, | ||
product_2.barcode, | ||
msg="Products should have the same barcode", | ||
) | ||
|
||
def test_barcode_with_same_company(self): | ||
"""Checking setting same barcode for same companies""" | ||
self.product_test_1.barcode = self.valid_barcode_1 | ||
self.assertEqual(len(self.product_test_1.barcode_ids), 1) | ||
self.assertEqual( | ||
self.product_test_1.barcode_ids.name, self.product_test_1.barcode | ||
) | ||
self.product_test_1.company_id = self.company_1.id | ||
|
||
self.product_test_2.company_id = self.company_1.id | ||
with self.assertRaises( | ||
UserError, | ||
msg=_( | ||
'The Barcode "%(barcode_name)s" already exists for ' | ||
'product "%(product_name)s" in the company %(company_name)s' | ||
) | ||
% dict( | ||
barcode_name=self.valid_barcode_1, | ||
product_name=self.product_test_1.name, | ||
company_name=self.product_test_1.company_id.name, | ||
), | ||
): | ||
self.product_test_2.barcode = self.valid_barcode_1 | ||
|
||
def test_barcodes(self): | ||
self.product_test_1.barcode = self.valid_barcode_1 | ||
self.product_test_1.company_id = self.company_1.id | ||
message = ( | ||
_( | ||
'The Barcode "%(barcode_name)s" already exists for ' | ||
'product "%(product_name)s" in the company %(company_name)s' | ||
) | ||
% dict( | ||
barcode_name=self.valid_barcode2_1, | ||
product_name=self.product_test_2.name, | ||
company_name=self.product_test_2.company_id.name, | ||
), | ||
) | ||
with self.assertRaises(UserError, msg=message): | ||
self.product_test_2.barcode_ids = [ | ||
(0, 0, {"name": self.valid_barcode2_1}), | ||
(0, 0, {"name": self.valid_barcode2_1}), | ||
] | ||
|
||
self.product_test_2.company_id = self.company_1.id | ||
with self.assertRaises(UserError, msg=message): | ||
self.product_test_2.barcode_ids = [ | ||
(0, 0, {"name": self.valid_barcode2_1}), | ||
(0, 0, {"name": self.valid_barcode2_1}), | ||
] | ||
self.product_test_2.company_id = self.company_2.id | ||
with self.assertRaises(UserError, msg=message): | ||
self.product_test_2.barcode_ids = [ | ||
(0, 0, {"name": self.valid_barcode2_1}), | ||
(0, 0, {"name": self.valid_barcode2_1}), | ||
] | ||
with self.assertRaises(UserError, msg=message): | ||
self.product_test_2.barcode_ids = [ | ||
(0, 0, {"name": self.valid_barcode2_1}), | ||
(0, 0, {"name": self.valid_barcode_1}), | ||
] | ||
|
||
with self.assertRaises(UserError, msg=message): | ||
self.product_test_2.barcode_ids = [ | ||
(0, 0, {"name": self.valid_barcode_1}), | ||
(0, 0, {"name": self.valid_barcode2_1}), | ||
] |
1 change: 1 addition & 0 deletions
1
...i_barcode_constraint_per_company/odoo/addons/product_multi_barcode_constraint_per_company
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../product_multi_barcode_constraint_per_company |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import setuptools | ||
|
||
setuptools.setup( | ||
setup_requires=['setuptools-odoo'], | ||
odoo_addon=True, | ||
) |