From 97fc0453753eda770eedfba42388635e24627ade Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Mon, 26 May 2025 09:14:07 +0200 Subject: [PATCH] First stab at Odoo filter logic --- src/servala/core/odoo.py | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/servala/core/odoo.py diff --git a/src/servala/core/odoo.py b/src/servala/core/odoo.py new file mode 100644 index 0000000..9157d4c --- /dev/null +++ b/src/servala/core/odoo.py @@ -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 []