From 25882dafabafe29e9f2c831dff348be5ddd27c75 Mon Sep 17 00:00:00 2001 From: Marc Durepos Date: Fri, 31 Jan 2025 13:33:33 -0500 Subject: [PATCH] new module customer_product_code_search --- customer_product_code_search/__init__.py | 1 + customer_product_code_search/__manifest__.py | 21 ++++ .../models/__init__.py | 1 + .../models/product.py | 29 +++++ .../tests/__init__.py | 1 + .../tests/test_product_search.py | 101 ++++++++++++++++++ 6 files changed, 154 insertions(+) create mode 100644 customer_product_code_search/__init__.py create mode 100644 customer_product_code_search/__manifest__.py create mode 100644 customer_product_code_search/models/__init__.py create mode 100644 customer_product_code_search/models/product.py create mode 100644 customer_product_code_search/tests/__init__.py create mode 100644 customer_product_code_search/tests/test_product_search.py diff --git a/customer_product_code_search/__init__.py b/customer_product_code_search/__init__.py new file mode 100644 index 0000000..0650744 --- /dev/null +++ b/customer_product_code_search/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/customer_product_code_search/__manifest__.py b/customer_product_code_search/__manifest__.py new file mode 100644 index 0000000..7b1d60d --- /dev/null +++ b/customer_product_code_search/__manifest__.py @@ -0,0 +1,21 @@ +{ + "name": "Customer Product Code Search", + "version": "18.0.1.0.0", + "category": "Sales", + "summary": "Enhance product search to include customer product codes", + "description": """ + This module extends the product search functionality to include customer product codes + in the product name search results. This makes it easier to find products using + customer-specific product codes in any context where products are searched. + """, + "author": "Bemade", + "website": "https://www.bemade.org", + "depends": [ + "product", + "customer_product_code", + ], + "data": [], + "installable": True, + "auto_install": True, + "license": "Other proprietary", +} diff --git a/customer_product_code_search/models/__init__.py b/customer_product_code_search/models/__init__.py new file mode 100644 index 0000000..9649db7 --- /dev/null +++ b/customer_product_code_search/models/__init__.py @@ -0,0 +1 @@ +from . import product diff --git a/customer_product_code_search/models/product.py b/customer_product_code_search/models/product.py new file mode 100644 index 0000000..1cde9b9 --- /dev/null +++ b/customer_product_code_search/models/product.py @@ -0,0 +1,29 @@ +from odoo import api, models +from odoo.osv import expression + + +class ProductProduct(models.Model): + _inherit = "product.product" + + @api.model + def _search_display_name(self, operator, value): + domains = super()._search_display_name(operator, value) + + # Base domain for customer codes + customer_code_domain = [ + "|", + ("product_customer_code_ids.product_code", operator, value), + ("product_customer_code_ids.product_name", operator, value), + ] + + # Add partner restriction if partner_id is in context + if partner_id := self.env.context.get("partner_id"): + customer_code_domain = expression.AND( + [ + customer_code_domain, + [("product_customer_code_ids.partner_id", "=", partner_id)], + ] + ) + + domains = expression.OR([domains, customer_code_domain]) + return domains diff --git a/customer_product_code_search/tests/__init__.py b/customer_product_code_search/tests/__init__.py new file mode 100644 index 0000000..6678ccd --- /dev/null +++ b/customer_product_code_search/tests/__init__.py @@ -0,0 +1 @@ +from . import test_product_search diff --git a/customer_product_code_search/tests/test_product_search.py b/customer_product_code_search/tests/test_product_search.py new file mode 100644 index 0000000..84a9d5b --- /dev/null +++ b/customer_product_code_search/tests/test_product_search.py @@ -0,0 +1,101 @@ +from odoo.tests import tagged +from odoo.tests.common import TransactionCase + + +@tagged("post_install", "-at_install") +class TestCustomerProductCodeSearch(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + # Create test partners + cls.partner1 = cls.env["res.partner"].create( + { + "name": "Test Partner 1", + } + ) + cls.partner2 = cls.env["res.partner"].create( + { + "name": "Test Partner 2", + } + ) + + # Create test products + cls.product1 = cls.env["product.product"].create( + { + "name": "Test Product 1", + "type": "consu", + "default_code": "TEST001", + } + ) + cls.product2 = cls.env["product.product"].create( + { + "name": "Test Product 2", + "type": "consu", + "default_code": "TEST002", + } + ) + + # Create customer product codes + cls.customer_code1 = cls.env["product.customer.code"].create( + { + "product_id": cls.product1.product_tmpl_id.id, + "partner_id": cls.partner1.id, + "product_code": "CUST001", + "product_name": "Customer 1 Product Name", + } + ) + cls.customer_code2 = cls.env["product.customer.code"].create( + { + "product_id": cls.product2.product_tmpl_id.id, + "partner_id": cls.partner2.id, + "product_code": "CUST002", + "product_name": "Customer 2 Product Name", + } + ) + + def test_search_by_customer_code_with_partner(self): + """Test searching products by customer code with partner context""" + # Search with partner1 context + products = ( + self.env["product.product"] + .with_context(partner_id=self.partner1.id) + .search([("display_name", "ilike", "CUST")]) + ) + self.assertIn( + self.product1, + products, + "Should find product1 when searching by customer code with partner1 context", + ) + self.assertNotIn( + self.product2, + products, + "Should not find product2 when searching with partner1 context", + ) + + def test_search_by_customer_code_without_partner(self): + """Test searching products by customer code without partner context""" + # Search without partner context should find both products + products = self.env["product.product"].search( + [("display_name", "ilike", "CUST")] + ) + self.assertIn( + self.product1, + products, + "Should find product1 when searching by customer code without partner context", + ) + self.assertIn( + self.product2, + products, + "Should find product2 when searching by customer code without partner context", + ) + + # Search for specific customer code + products = self.env["product.product"].search( + [("display_name", "ilike", "CUST002")] + ) + self.assertIn( + self.product2, + products, + "Should find product2 when searching by its specific customer code without partner context", + )