Skip to content

Commit

Permalink
[ADD] product_multi_barcode_constraint_per_company: new module
Browse files Browse the repository at this point in the history
  • Loading branch information
dessanhemrayev committed Dec 18, 2023
1 parent b050d23 commit e2713a1
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 0 deletions.
Empty file.
1 change: 1 addition & 0 deletions product_multi_barcode_constraint_per_company/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions product_multi_barcode_constraint_per_company/__manifest__.py
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,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import product_barcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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:
barcode = self.search(
[("id", "!=", record.id), ("name", "=", record.name)], limit=1
)
if barcode and (
not record.product_id.company_id
or (
barcode.sudo().product_id.company_id == record.product_id.company_id
)
):
existing_codes |= record
if existing_codes:
super(ProductBarcode, existing_codes)._check_duplicates()
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>
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_product_barcode_constraint
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}),
]
6 changes: 6 additions & 0 deletions setup/product_multi_barcode_constraint_per_company/setup.py
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,
)

0 comments on commit e2713a1

Please sign in to comment.