First stab at Odoo filter logic

This commit is contained in:
Tobias Kunze 2025-05-26 09:14:07 +02:00
parent 1ed383ea10
commit 97fc045375

67
src/servala/core/odoo.py Normal file
View file

@ -0,0 +1,67 @@
ADDRESS_FIELDS = [
"id",
"name",
"street",
"street2",
"city",
"zip",
"state_id",
"country_id",
"email",
"phone",
"vat",
"company_type",
]
def odoo_request(*args, **kwargs):
raise NotImplementedError
def get_invoice_addresses(user):
"""Used during organization creation: retrieves all invoice
addresses the user owns or is connected to from the Odoo API."""
or_conditions = [
("email", "ilike", user.email),
("child_ids.email", "ilike", user.email),
]
# Attempt to find the Odoo user ID and add condition for records created by this user
try:
odoo_users = odoo_request(
model="res.users",
method="search_read",
domain=[("login", "=", user.email)],
fields=["id"],
limit=1, # Expecting at most one user
)
if odoo_users and (uid := odoo_users[0].get("id")):
or_conditions.append(("create_uid", "=", uid))
except Exception:
pass
if len(or_conditions) == 1:
user_conditions = or_conditions[0]
else:
# Start with the last condition and progressively prepend OR clauses with previous conditions.
user_conditions = or_conditions[-1]
for i in range(len(or_conditions) - 2, -1, -1):
user_conditions = ["|", or_conditions[i], user_conditions]
# The domain requires the partner to be an invoice address, that is:
# Of the company_type=person, and type=invoice.
# If we were searching for an existing organization, we would also have to
# filter for parent_id=odoo_company_id
static_conditions = ("&", ("company_type", "=", "person"), ("type", "=", "invoice"))
domain = ("&", static_conditions, user_conditions)
try:
invoice_addresses = odoo_request(
model="res.partner",
method="search_read",
domain=domain,
fields=ADDRESS_FIELDS,
)
return invoice_addresses or []
except Exception:
return []