new module customer_product_code_search

This commit is contained in:
Marc Durepos 2025-01-31 13:33:33 -05:00
parent 549cbf2579
commit 25882dafab
6 changed files with 154 additions and 0 deletions

View file

@ -0,0 +1 @@
from . import models

View file

@ -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",
}

View file

@ -0,0 +1 @@
from . import product

View file

@ -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

View file

@ -0,0 +1 @@
from . import test_product_search

View file

@ -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",
)