Project
Function
Sample CLI
gway odoo fetch-customers
References
['error']
Full Code
def fetch_customers(
*,
name=None,
email=None,
phone=None,
country=None,
latest_quotes=None,
**kwargs
):
"""
Fetch customers from Odoo with optional filters.
Parameters:
name (str, optional): Filter customers by their name or part of it.
email (str, optional): Filter customers by their email address or part of it.
phone (str, optional): Filter customers by their phone number or part of it.
country (str, optional): Filter customers by their country name or part of it.
**kwargs: Additional filters to be applied, passed as key-value pairs.
Returns:
dict: The fetched customers.
"""
# TODO: If latest_quotes is a number, also fetch the many latest quotes for this customer.
model = 'res.partner'
method = 'search_read'
# Start with an empty domain filter
domain_filter = []
if name:
domain_filter.append(('name', 'ilike', name))
if email:
domain_filter.append(('email', 'ilike', email))
if phone:
domain_filter.append(('phone', 'ilike', phone))
if country:
domain_filter.append(('country_id.name', 'ilike', country))
for field, value in kwargs.items():
domain_filter.append((field, 'ilike', value))
fields_to_fetch = ['name', 'create_date']
try:
result = execute_kw(
[domain_filter], {'fields': fields_to_fetch},
model=model, method=method
)
return result
except Exception as e:
gw.error(f"Error fetching customers: {e}")
raise