Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[18.0][IMP] product_customerinfo: Allow searching product templates by customer info #1912

Merged
merged 2 commits into from
Mar 21, 2025

Conversation

carlos-lopez-tecnativa
Copy link
Contributor

@carlos-lopez-tecnativa carlos-lopez-tecnativa commented Mar 10, 2025

Now, in the sale order, the product template is displayed by default. This update adds the ability to search for product templates using customer info.

Reference: odoo/odoo#155449

TT54233
@Tecnativa @pedrobaeza @victoralmau could you please review this.

@OCA-git-bot
Copy link
Contributor

Hi @luisg123v,
some modules you are maintaining are being modified, check this out!


@api.model
def name_search(self, name, args=None, operator="ilike", limit=100):
res = super().name_search(name, args=args, operator=operator, limit=limit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should control the extra search by a context key activated in the sales order, and give priority to the result of product.customerinfo records. How is this handled in purchase by core?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In purchase.order, the partner_id is passed by context, similar to how it is done in sale.order.

In this module, when searching for product.product, the name_search does not expect any specific context(just partner_id). For product.template, it follows the same logic as product.product. What exactly needs to be changed? Please provide more details.

product.product

def name_search(self, name, args=None, operator="ilike", limit=100):
res = super().name_search(name, args=args, operator=operator, limit=limit)
res_ids_len = len(res)
if (
not name
and limit
or not self._context.get("partner_id")
or res_ids_len >= limit
):
return res
limit -= res_ids_len
supplier_domain = [
("partner_id", "=", self._context.get("partner_id")),
"|",
("product_code", operator, name),
("product_name", operator, name),
]
match_domain = [("product_tmpl_id.customer_ids", "any", supplier_domain)]
products = self.search_fetch(
expression.AND([args or [], match_domain]), ["display_name"], limit=limit
)
return res + [(product.id, product.display_name) for product in products.sudo()]

product.template
https://github.com/Tecnativa/product-attribute/blob/0b2668128d84949bff76d553e0f4d93fb7e8dfb4/product_customerinfo/models/product_template.py#L95-L96

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see the context key. It seems enough.

Copy link
Member

@pedrobaeza pedrobaeza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can part of this code reused in product.product for not duplicating it?

…omer info

Now, in the sale order, the product template is displayed by default. This update adds the ability to search for product templates using customer info.

Reference: odoo/odoo#155449
@carlos-lopez-tecnativa carlos-lopez-tecnativa force-pushed the 18.0-product_customerinfo-name-search branch from 0b26681 to 544a7bf Compare March 10, 2025 17:48
@carlos-lopez-tecnativa
Copy link
Contributor Author

Can part of this code reused in product.product for not duplicating it?

Note that both name_search implementations are different in terms of the fields used: product.product relies on product_tmpl_id.customer_ids, while product.template uses customer_ids. I don’t believe it is necessary to create a new AbstractModel here. Please let me know if this solution is sufficient or if the abstract model is needed

# prefetch the fields used by the `display_name`
domain = [
("product_tmpl_id", "in", product_template_ids),
("product_id", "=", False),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should not be product_id=false or if you define a specific record for a variant it does not work correctly, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When creating a record from product.product or product.template, the product_id field is always empty by default. It is only set when the user explicitly selects a variant in this field. In such cases, the customer info should be linked specifically to that variant. Therefore, from my perspective, it is correct that this record is not considered here.

- Changed invisible to column_invisible in list views.
- Show or hide product_id and product_template_id depending on whether the view is for product.template or product.product.
- Created a specific view for product.product to pass default values via context to product_tmpl_id and prevent errors when passing an ID from product.product to product.template.
- Removed group="base.group_multi_company" from product_tmpl_id as it was incorrectly applied.
@pedrobaeza
Copy link
Member

/ocabot merge minor

@OCA-git-bot
Copy link
Contributor

What a great day to merge this nice PR. Let's do it!
Prepared branch 18.0-ocabot-merge-pr-1912-by-pedrobaeza-bump-minor, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit ef76386 into OCA:18.0 Mar 21, 2025
5 of 7 checks passed
@OCA-git-bot
Copy link
Contributor

Congratulations, your PR was merged at 3951d49. Thanks a lot for contributing to OCA. ❤️

@pedrobaeza pedrobaeza deleted the 18.0-product_customerinfo-name-search branch March 21, 2025 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants