Contributors mailing list archives

contributors@odoo-community.org

Browse archives

Avatar

Re: [Odoo CE 17.0] Help needed with client search on budgets

by
Jose Luis Baños Consulting, S.L.U., Alejandro Párraga Alcázar
- 26/11/2024 09:44:20
Hello everyone,
 
after making a custom module with Francescos' code and fixing the search function, I came across a problem related to how the partner is displayed at the sales order form:
 
When searching for a customer, they are displayed with the following format: "[x_cliente_interno] + customer name", which is the exact format I needed. But the problem is that when I select any customer, it doesn't show it's address and neither it's VAT below the customer anymore.
 
Here I send my version of the code from Francesco:
 
from odoo import models, fields, api

class ResPartner(models.Model):
 
_inherit = “res.partner”

x_cliente_interno = fields.Integer() # use a standard field for consistency

@api.depends(“name”, “x_cliente_interno”)
def _compute_display_name(self):
“""Show reference code before partner name""”
super()._compute_display_name()
for partner in self:
name = partner.name or “unknown”
cliente_interno = f"[{partner.x_cliente_interno}] " if partner.x_cliente_interno else ""
# !! Disclaimer !!
# If you copy this code and change it DO NOT ASSIGN the value of partner.name,
# that would compromise your datas. Only re-assign partner.display_name
partner.display_name = cliente_interno + name # It's replacing the whole field

@api.model
def _name_search(self, name, domain=None, operator='ilike', limit=None, order=None):
“""Add custom field `cliente_interno` to the search domain""”
domain = domain or []
if name:
domain += ['|', '|', ('name', operator, name),                     
('email', operator, name),                     
('x_cliente_interno', '=', name)]
return self._search(domain, limit=limit, order=order)
 
I tried fixing it by myself, and even if I wasn't able to, I discovered the field "partner.display_name" is the one causing this problem.
 
Thanks in advance for the help.
El 20/11/2024 9:21 CET Redes Sociales JLBBERP <redes_sociales@jlbberp.com> escribió:
 
 
Hello Francesco,

I have seen your message right now, and I would like to thank you so much for not only your answer, but also for you writing the code I should do. I do not know how could I pay you back this favor that you have done for me.
 
I rely on the community because I am in my beginnings and because of everything that my company JLBB ERP has given me through good collaboration, I am now checking it out. Do not doubt that everything I can contribute, you will find me here.
 
Thank you Francesco for the help.
 
Salutations to the community.
El 20/11/2024 0:02 CET Francesco Ballerini <notifications@odoo-community.org> escribió:
 
 
I apologize, it is now clear to me also that you need both the search from a custom field and the way the name is displayed, but the search is indeed prioritized. 
 
As Daniel and Xavier pointed out, in Odoo 17.0 you can already submit the "ref" field (field label on GUI is "Reference") and get the right record. E.g. if you have a contact named John Doe with Reference set on "ab-001" and you search for "ab-001" in the contacts search or dropdown you will fetch John Doe. 
 
You could migrate your "x_cliente_interno" data into the "ref" field, and this would automatically unlock the search feature. However if you need to keep this data in a specific custom field you will need to override _name_search() method. 
In every case you will also need to override _compute_display_name if you want to also show the code before the field. I provided a couple of snippet below (just as an example, especially the _name_search override needs to be refined).
 
Let me point out another thing: from the field name I assume you are using a field defined from GUI, unfortunately it's not recommended to use those kind of fields in python methods as it will easily lead to inconsistencies and bugs (e.g. if you delete field from db and not remove it from python methods..). This means that if you want to use a custom field in _name_search() override you should migrate data to a standard field aniway.

This is an example of what you might do on a custom module. As you can see the _name_search() implementation has to be refined while the_compute_display_name method will work as is. 
If you decide to migrate data into the "ref" field you can only implement _compute_display_name by replacing "cliente_interno" with "ref" and you do not need to implement _name_search() in this case.
 
from odoo import models, fields, api
from odoo.osv import expression

class ResPartner(models.Model):
_inherit = "res.partner"

cliente_interno = fields.Char() # use a standard field for consistency

@api.depends("name", "cliente_interno")
def _compute_display_name(self):
"""Show reference code before partner name"""
super()._compute_display_name()
for partner in self:
name = partner.name or "unknown"
cliente_interno = f"[{partner.cliente_interno}] " if partner.cliente_interno else ""
# !! Disclaimer !!
#I f you copy this code and change it DO NOT ASSIGN the value of partner.name,
# that would compromise your datas. Only re-assign partner.display_name
partner.display_name = cliente_interno + name

@api.model
def _name_search(self, name, domain=None, operator='ilike', limit=None, order=None):
"""Add custom field `cliente_interno` to the search domain"""
domain = expression.OR([domain or [], [("cliente_interno", operator, name)]])
# this doesn't work properly, it seems like the domain we pass gets overridden,
# might take this as example and adjust by debugging/find more examples on source code
return super()._name_search(name, domain=domain, operator=operator, limit=limit, order=order)
 
 
Regards
 
--Francesco
 
Il giorno mar 19 nov 2024 alle ore 14:13 Daniel Reis <notifications@odoo-community.org> ha scritto:
From your description, it feels like this is already available in standard Odoo:
The "ref" field is to be used for Client codes

The code is no longer presented in front of the name.
I think an OCA module is needed to bring that feature back.

/Daniel

On 19/11/2024 09:22, Redes Sociales JLBBERP wrote:
Hello everyone,
 
I am writing this message because I need assistance with modifying how searching a client while doing a budget works.
 
What I am trying to do is, instead of searching just by the name, I want it to work like the following: I made a custom field called "x_cliente_interno", which is a number to identify the client, and I would like to make the search work like how product search works: If you write either the product's ID or the product's name, it will show you both fields merged from the data you wrote. Something like this:
 
 
And when you select it, it shows the data like this:
 
 
To sum up, what I am trying to do is that when you search a client, the search shows you the field "x_cliente_interno" behind the client name, like this: "[x_cliente_interno] client name" and it should also stay like in the second image when selected.
 
Thanks in advance for the help.

--
DANIEL REIS
MANAGING PARTNER

>> Schedule time on my calendar.
M: +351 919 991 307
E: dreis@OpenSourceIntegrators.com
A: Avenida da República 3000, Estoril Office Center, 2649-517 Cascais

[Logo OpenSourceIntegrators.com]

_______________________________________________
Mailing-List: https://odoo-community.org/groups/contributors-15
Post to: mailto:contributors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe

_______________________________________________
Mailing-List: https://odoo-community.org/groups/contributors-15
Post to: mailto:contributors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe

 
 

Reference