From 025670c0a4a40c5fd46fed0b3b34a129e2a407ad Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Sun, 15 Jun 2025 16:02:39 +0200 Subject: [PATCH] Allow users to reuse invoice addresses --- src/servala/core/odoo.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/servala/core/odoo.py b/src/servala/core/odoo.py index 1e75c51..f8487c9 100644 --- a/src/servala/core/odoo.py +++ b/src/servala/core/odoo.py @@ -120,13 +120,30 @@ def get_invoice_addresses(user): addresses the user owns or is connected to from the Odoo API.""" # We’re building our conditions in order: # - in exceptions, users may be using a billing account’s email + # - if the user is an admin or owner of a Servala organization # - if the user is associated with an odoo user, return all billing # addresses / organizations created by the user # - if the user is associated with an odoo contact, return all billing # addresses with the same parent_id + from servala.core.models.organization import ( + OrganizationMembership, + OrganizationRole, + ) + email = user.email or_conditions = [("email", "ilike", email)] + servala_invoice_ids = list( + OrganizationMembership.objects.filter( + user=user, role__in=[OrganizationRole.ADMIN, OrganizationRole.OWNER] + ) + .values_list("organization__billing_entity__odoo_invoice_id", flat=True) + .distinct() + ) + servala_invoice_ids = [pk for pk in servala_invoice_ids if pk] + if servala_invoice_ids: + or_conditions.append(("id", "in", servala_invoice_ids)) + odoo_users = CLIENT.search_read( model="res.users", domain=[("login", "=", email), ("active", "=", True)],