From edafff897e60443987a21b28362a749afe1f63f6 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Mon, 26 May 2025 07:09:06 +0200 Subject: [PATCH 01/22] Add odoo fields --- .../0004_billingentity_odoo_fields.py | 23 +++++++++++++++++++ src/servala/core/models/organization.py | 7 ++++++ 2 files changed, 30 insertions(+) create mode 100644 src/servala/core/migrations/0004_billingentity_odoo_fields.py diff --git a/src/servala/core/migrations/0004_billingentity_odoo_fields.py b/src/servala/core/migrations/0004_billingentity_odoo_fields.py new file mode 100644 index 0000000..bcfee2d --- /dev/null +++ b/src/servala/core/migrations/0004_billingentity_odoo_fields.py @@ -0,0 +1,23 @@ +# Generated by Django 5.2 on 2025-05-26 05:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0003_alter_organization_namespace"), + ] + + operations = [ + migrations.AddField( + model_name="billingentity", + name="odoo_company_id", + field=models.IntegerField(null=True), + ), + migrations.AddField( + model_name="billingentity", + name="odoo_invoice_id", + field=models.IntegerField(null=True), + ), + ] diff --git a/src/servala/core/models/organization.py b/src/servala/core/models/organization.py index 7ebda11..201a8b9 100644 --- a/src/servala/core/models/organization.py +++ b/src/servala/core/models/organization.py @@ -110,6 +110,13 @@ class BillingEntity(ServalaModelMixin, models.Model): max_length=100, blank=True, verbose_name=_("ERP reference") ) + # Odoo IDs are nullable for creation, should never be null in practice + # The company ID points at a record of type res.partner with company_type=company + # The invoice ID points at a record of type res.partner with company_type=person, + # type=invoic, parent_id=company_id (the invoice address). + odoo_company_id = models.IntegerField(null=True) + odoo_invoice_id = models.IntegerField(null=True) + class Meta: verbose_name = _("Billing entity") verbose_name_plural = _("Billing entities") From 639e183184d7f48e125ce712cd988c880ac021b1 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Mon, 26 May 2025 09:14:07 +0200 Subject: [PATCH 02/22] 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 [] From 3abd298dea4958eb9e6ba3aab64e14f4477e7e37 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Mon, 26 May 2025 09:14:25 +0200 Subject: [PATCH 03/22] Add Odoo settings --- .env.example | 7 ++++++- src/servala/settings.py | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 58eb7b3..aa287ce 100644 --- a/.env.example +++ b/.env.example @@ -60,4 +60,9 @@ SERVALA_KEYCLOAK_SERVER_URL='' # SERVALA_S3_SIGNATURE_VERSION='s3v4' # Configuration for Sentry error reporting -SERVALA_SENTRY_DSN='' \ No newline at end of file +SERVALA_SENTRY_DSN='' + +SERVALA_ODOO_DB='' +SERVALA_ODOO_URL='' +SERVALA_ODOO_USERNAME='' +SERVALA_ODOO_PASSWORD='' diff --git a/src/servala/settings.py b/src/servala/settings.py index 42f1021..a8b3fbc 100644 --- a/src/servala/settings.py +++ b/src/servala/settings.py @@ -90,7 +90,6 @@ SOCIALACCOUNT_PROVIDERS = { } } - SERVALA_STORAGE_BUCKET_NAME = os.environ.get("SERVALA_STORAGE_BUCKET_NAME") SERVALA_S3_ENDPOINT_URL = os.environ.get("SERVALA_S3_ENDPOINT_URL") SERVALA_ACCESS_KEY_ID = os.environ.get("SERVALA_ACCESS_KEY_ID") @@ -126,6 +125,13 @@ if all( }, } +ODOO = { + "URL": os.environ.get("SERVALA_ODOO_URL"), + "DB": os.environ.get("SERVALA_ODOO_DB"), + "USERNAME": os.environ.get("SERVALA_ODOO_USERNAME"), + "PASSWORD": os.environ.get("SERVALA_ODOO_PASSWORD"), +} + ####################################### # Non-configurable settings below # ####################################### From 93c4fc9b262f870bca19575785845639bc3a42de Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Mon, 26 May 2025 09:15:53 +0200 Subject: [PATCH 04/22] Add system checks --- README.md | 1 + src/servala/core/apps.py | 3 ++ src/servala/core/checks.py | 86 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 src/servala/core/checks.py diff --git a/README.md b/README.md index df8d74c..7241854 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,7 @@ uv run --env-file=.env src/manage.py COMMAND Useful commands: - ``migrate``: Make sure database migrations are applied. +- ``check --deploy``: Runs checks, e.g. for missing or mismatched configuration, including custom servala configuration. - ``showmigrations``: Show current database migrations status. Good for debugging. - ``runserver``: Run development server - ``clearsessions``: Clear away expired user sessions. Recommended to run regularly, e.g. weekly or monthly (doesn’t diff --git a/src/servala/core/apps.py b/src/servala/core/apps.py index ad73de8..f7cbde2 100644 --- a/src/servala/core/apps.py +++ b/src/servala/core/apps.py @@ -4,3 +4,6 @@ from django.apps import AppConfig class CoreConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "servala.core" + + def ready(self): + import servala.core.checks # noqa diff --git a/src/servala/core/checks.py b/src/servala/core/checks.py new file mode 100644 index 0000000..d1bb708 --- /dev/null +++ b/src/servala/core/checks.py @@ -0,0 +1,86 @@ +from django.conf import settings +from django.core.checks import ERROR, WARNING, CheckMessage, register + + +@register() +def check_servala_settings(app_configs, **kwargs): + """Checks all settings that should be present in all environments.""" + if app_configs: + # Don’t run if we’re meant to only test individual apps + return [] + + errors = [] + required_fields = ("URL", "DB", "USERNAME", "PASSWORD") + missing_fields = [ + field for field in required_fields if not settings.ODOO.get(field) + ] + if missing_fields: + fields = ", ".join(missing_fields) + errors.append( + CheckMessage( + level=WARNING if settings.DEBUG else ERROR, + msg=f"Missing Odoo config: {fields}", + hint="Make sure you set the required SERVALA_ODOO_* settings.", + id="servala.E001", + ) + ) + oidc_config = settings.SOCIALACCOUNT_PROVIDERS["openid_connect"]["APPS"][0] + missing_fields = [ + field for field in ("client_id", "secret") if not oidc_config.get(field) + ] + if not oidc_config["settings"]["server_url"]: + missing_fields.append("server_url") + if missing_fields: + fields = ", ".join( + [f"SERVALA_KEYCLOAK_{field.upper()}" for field in missing_fields] + ) + errors.append( + CheckMessage( + level=WARNING if settings.DEBUG else ERROR, + msg=f"Missing Keycloak config: {fields}", + id="servala.E002", + ) + ) + + if settings.SERVALA_ENVIRONMENT not in ("development", "staging", "production"): + errors.append( + CheckMessage( + level=ERROR, + msg=f"Invalid environment {settings.SERVALA_ENVIRONMENT}", + hint="Must be one of development, staging, production.", + id="servala.E003", + ) + ) + + return errors + + +@register(deploy=True) +def check_servala_production_settings(app_configs, **kwargs): + if app_configs: + # Don’t run if we’re meant to only test individual apps + return [] + + errors = [] + if settings.SERVALA_ENVIRONMENT == "development": + errors.append( + CheckMessage( + level=ERROR, + msg="Environment is set to 'development'.", + id="servala.E004", + ) + ) + if "insecure" in settings.SECRET_KEY: + errors.append( + CheckMessage( + level=ERROR, msg="Secret key contains 'insecure'.", id="servala.E005" + ) + ) + if settings.EMAIL_USE_SSL and settings.EMAIL_USE_TLS: + errors.append( + CheckMessage( + level=WARNING, + msg="Use either SSL or TLS in email config, not both!", + id="servala.W001", + ) + ) From fcca4ea514116273c767b3dc75584b02ff780e6a Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Mon, 26 May 2025 09:51:29 +0200 Subject: [PATCH 05/22] Implement user search in odoo --- src/servala/core/odoo.py | 81 ++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 12 deletions(-) diff --git a/src/servala/core/odoo.py b/src/servala/core/odoo.py index 9157d4c..59635bd 100644 --- a/src/servala/core/odoo.py +++ b/src/servala/core/odoo.py @@ -1,3 +1,7 @@ +import xmlrpc.client + +from django.conf import settings + ADDRESS_FIELDS = [ "id", "name", @@ -14,8 +18,66 @@ ADDRESS_FIELDS = [ ] -def odoo_request(*args, **kwargs): - raise NotImplementedError +def odoo_request(model, method, **kwargs): + url = settings.ODOO["URL"] + db = settings.ODOO["DB"] + username = settings.ODOO["USERNAME"] + password = settings.ODOO["PASSWORD"] + + try: + common = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/common") + uid = common.authenticate(db, username, password, {}) + + if not uid: + raise Exception("Authentication failed with Odoo.") + + models = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/object") + + # Prepare arguments for execute_kw + # Odoo's execute_kw expects: db, uid, password, model, method, args_list, kwargs_dict + # For 'search_read', args_list typically contains [domain, fields] + # and kwargs_dict contains {'limit': ..., 'offset': ..., 'order': ...} + + args_list = [] + kwargs_dict = {} + + if method == "search_read": + # Extract domain and fields for positional arguments if present + domain = kwargs.pop("domain", []) + fields = kwargs.pop("fields", []) + args_list = [domain, fields] + # Remaining kwargs are passed as the options dictionary + kwargs_dict = kwargs + else: + # For other methods, we might need a more generic way or specific handling. + # For now, assume kwargs can be passed directly if method is not 'search_read', + # or that they are passed as a list of arguments. + # This part might need refinement based on other Odoo methods used. + # A common pattern is to pass a list of IDs as the first arg for methods like 'read', 'write'. + # If 'args' is explicitly passed in kwargs, use it. + if "args" in kwargs: + args_list = kwargs.pop("args") + # Remaining kwargs are passed as the options dictionary + kwargs_dict = kwargs + + breakpoint() + result = models.execute_kw( + db, uid, password, model, method, args_list, kwargs_dict + ) + return result + + except xmlrpc.client.Fault as e: + # Handle XML-RPC specific errors (e.g., Odoo operational errors) + raise Exception(f"Odoo XML-RPC Fault: {e.faultString}") from e + except ConnectionRefusedError as e: + raise Exception( + f"Could not connect to Odoo at {url}. Connection refused." + ) from e + except Exception as e: + # General exception handling + raise Exception( + f"An error occurred while communicating with Odoo: {str(e)}" + ) from e def get_invoice_addresses(user): @@ -40,20 +102,15 @@ def get_invoice_addresses(user): 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] - + user_conditions = ["|"] * (len(or_conditions) - 1) + or_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) + domain = [ + ("company_type", "=", "person"), + ("type", "=", "invoice"), + ] + user_conditions try: invoice_addresses = odoo_request( From f774b37696321e601bdb6f20c99447cf9829ec85 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Mon, 26 May 2025 13:06:31 +0200 Subject: [PATCH 06/22] Use class-based odoo client for connection reuse --- src/servala/core/odoo.py | 112 +++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 52 deletions(-) diff --git a/src/servala/core/odoo.py b/src/servala/core/odoo.py index 59635bd..d5a0ad6 100644 --- a/src/servala/core/odoo.py +++ b/src/servala/core/odoo.py @@ -15,69 +15,77 @@ ADDRESS_FIELDS = [ "phone", "vat", "company_type", + "type", ] -def odoo_request(model, method, **kwargs): - url = settings.ODOO["URL"] - db = settings.ODOO["DB"] - username = settings.ODOO["USERNAME"] - password = settings.ODOO["PASSWORD"] +class OdooClient: + def __init__(self): + self.url = settings.ODOO["URL"] + self.db = settings.ODOO["DB"] + self.username = settings.ODOO["USERNAME"] + self.password = settings.ODOO["PASSWORD"] - try: - common = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/common") - uid = common.authenticate(db, username, password, {}) + self.common_proxy = None + self.models_proxy = None + self.uid = None - if not uid: - raise Exception("Authentication failed with Odoo.") + def _connect(self): + """This method is called on the first client request, not on instantiation, + so that we can instantiate the client on startup and reuse it across the entire + application.""" + try: + self.common_proxy = xmlrpc.client.ServerProxy(f"{self.url}/xmlrpc/2/common") + self.uid = self.common_proxy.authenticate( + self.db, self.username, self.password, {} + ) - models = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/object") + if not self.uid: + raise Exception("Authentication failed with Odoo: No UID returned.") - # Prepare arguments for execute_kw - # Odoo's execute_kw expects: db, uid, password, model, method, args_list, kwargs_dict - # For 'search_read', args_list typically contains [domain, fields] - # and kwargs_dict contains {'limit': ..., 'offset': ..., 'order': ...} + self.models_proxy = xmlrpc.client.ServerProxy(f"{self.url}/xmlrpc/2/object") - args_list = [] - kwargs_dict = {} + except xmlrpc.client.Fault as e: + raise Exception( + f"Odoo XML-RPC Fault during connection: {e.faultString}" + ) from e + except ConnectionRefusedError as e: + raise Exception( + f"Could not connect to Odoo at {self.url}. Connection refused." + ) from e + except Exception as e: + raise Exception( + f"An error occurred while connecting to Odoo: {str(e)}" + ) from e - if method == "search_read": - # Extract domain and fields for positional arguments if present - domain = kwargs.pop("domain", []) - fields = kwargs.pop("fields", []) - args_list = [domain, fields] - # Remaining kwargs are passed as the options dictionary - kwargs_dict = kwargs - else: - # For other methods, we might need a more generic way or specific handling. - # For now, assume kwargs can be passed directly if method is not 'search_read', - # or that they are passed as a list of arguments. - # This part might need refinement based on other Odoo methods used. - # A common pattern is to pass a list of IDs as the first arg for methods like 'read', 'write'. - # If 'args' is explicitly passed in kwargs, use it. - if "args" in kwargs: - args_list = kwargs.pop("args") - # Remaining kwargs are passed as the options dictionary - kwargs_dict = kwargs + def execute(self, model, method, args_list, **kwargs): + if not self.uid or not self.models_proxy: + self._connect() - breakpoint() - result = models.execute_kw( - db, uid, password, model, method, args_list, kwargs_dict - ) - return result + try: + result = self.models_proxy.execute_kw( + self.db, self.uid, self.password, model, method, args_list, kwargs + ) + return result - except xmlrpc.client.Fault as e: - # Handle XML-RPC specific errors (e.g., Odoo operational errors) - raise Exception(f"Odoo XML-RPC Fault: {e.faultString}") from e - except ConnectionRefusedError as e: - raise Exception( - f"Could not connect to Odoo at {url}. Connection refused." - ) from e - except Exception as e: - # General exception handling - raise Exception( - f"An error occurred while communicating with Odoo: {str(e)}" - ) from e + except xmlrpc.client.Fault as e: + print(f"Fault! {e}") + raise Exception(f"Odoo XML-RPC Fault: {e.faultString}") from e + except ConnectionRefusedError as e: + raise Exception( + f"Connection to Odoo at {self.url} lost or refused during operation." + ) from e + except Exception as e: + print(e) + raise Exception( + f"An error occurred while communicating with Odoo: {str(e)}" + ) from e + + def search_read(self, model, domain, fields, **kwargs): + return self.execute(model, "search_read", args_list=[domain, fields], **kwargs) + + +CLIENT = OdooClient() def get_invoice_addresses(user): From 29b630ea2b33d65cb3ad861b9537a4625c903719 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Mon, 26 May 2025 13:07:07 +0200 Subject: [PATCH 07/22] Fully implement odoo user search --- src/servala/core/odoo.py | 55 +++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/src/servala/core/odoo.py b/src/servala/core/odoo.py index d5a0ad6..81a24bc 100644 --- a/src/servala/core/odoo.py +++ b/src/servala/core/odoo.py @@ -91,26 +91,40 @@ CLIENT = OdooClient() 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), - ] + # We’re building our conditions in order: + # - in exceptions, users may be using a billing account’s email + # - 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 + or_conditions = [("email", "ilike", 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 + email = user if isinstance(user, str) else user.email + odoo_users = CLIENT.search_read( + model="res.users", + domain=[("login", "=", email)], + fields=["id"], + limit=1, + ) + if odoo_users and (uid := odoo_users[0].get("id")): + or_conditions.append(("create_uid", "=", uid)) + + odoo_contacts = CLIENT.search_read( + model="res.partner", + domain=[ + ("company_type", "=", "person"), + ("type", "=", "contact"), + ("email", "ilike", email), + ], + fields=["id", "parent_id"], + ) + if odoo_contacts: + for contact in odoo_contacts: + or_conditions.append(("parent_id", "=", contact["parent_id"][0])) + + if len(or_conditions) > 1: + or_conditions = ["|"] * (len(or_conditions) - 1) + or_conditions - user_conditions = ["|"] * (len(or_conditions) - 1) + or_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 @@ -118,12 +132,11 @@ def get_invoice_addresses(user): domain = [ ("company_type", "=", "person"), ("type", "=", "invoice"), - ] + user_conditions + ] + or_conditions try: - invoice_addresses = odoo_request( + invoice_addresses = CLIENT.search_read( model="res.partner", - method="search_read", domain=domain, fields=ADDRESS_FIELDS, ) From d72c853e72318cc4fe8b014309e3a57262ce64e1 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Mon, 26 May 2025 13:07:32 +0200 Subject: [PATCH 08/22] Implement user:odoo contact mapping ref #60 --- src/servala/core/models/user.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/servala/core/models/user.py b/src/servala/core/models/user.py index 0084593..5d513f4 100644 --- a/src/servala/core/models/user.py +++ b/src/servala/core/models/user.py @@ -6,7 +6,8 @@ from django.contrib.auth.models import ( from django.db import models from django.utils.translation import gettext_lazy as _ -from .mixins import ServalaModelMixin +from servala.core import odoo +from servala.core.models.mixins import ServalaModelMixin class UserManager(BaseUserManager): @@ -73,3 +74,22 @@ class User(ServalaModelMixin, PermissionsMixin, AbstractBaseUser): def normalize_username(self, username): return super().normalize_username(username).strip().lower() + + def get_odoo_contact(self, organization): + if ( + not organization.billing_entity + or not organization.billing_entity.odoo_company_id + ): + return + result = odoo.CLIENT.search_read( + model="res.partner", + domain=[ + ("company_type", "=", "person"), + ("type", "=", "contact"), + ("email", "ilike", self.email), + ("parent_id", "=", organization.billing_entity.odoo_company_id), + ], + fields=odoo.ADDRESS_FIELDS, + ) + if result: + return result[0] From 86cf2956a21bc9330e3d44ed8d091c15f03b53aa Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Tue, 27 May 2025 14:17:20 +0200 Subject: [PATCH 09/22] Implement template/form/view for org create --- src/servala/core/models/organization.py | 13 +++ src/servala/core/odoo.py | 1 + src/servala/frontend/forms/organization.py | 102 ++++++++++++++++++ .../templates/frontend/forms/errors.html | 18 ++++ .../templates/frontend/forms/form.html | 19 +--- .../frontend/organizations/create.html | 64 ++++++++++- src/servala/frontend/views/organization.py | 41 ++++++- 7 files changed, 236 insertions(+), 22 deletions(-) create mode 100644 src/servala/frontend/templates/frontend/forms/errors.html diff --git a/src/servala/core/models/organization.py b/src/servala/core/models/organization.py index 201a8b9..c113f78 100644 --- a/src/servala/core/models/organization.py +++ b/src/servala/core/models/organization.py @@ -124,6 +124,19 @@ class BillingEntity(ServalaModelMixin, models.Model): def __str__(self): return self.name + @classmethod + def create_from_data(cls, odoo_data): + instance = BillingEntity.objects.create(name=odoo_data.get("name")) + # TODO implement odoo creation from data + return instance + + @classmethod + def create_from_id(cls, odoo_id): + # TODO implement odoo creation from ID + # instance = BillingEntity.objects.create(name=odoo_data.get("name")) + # return instance + pass + class OrganizationOrigin(ServalaModelMixin, models.Model): """ diff --git a/src/servala/core/odoo.py b/src/servala/core/odoo.py index 81a24bc..85f0b35 100644 --- a/src/servala/core/odoo.py +++ b/src/servala/core/odoo.py @@ -97,6 +97,7 @@ def get_invoice_addresses(user): # addresses / organizations created by the user # - if the user is associated with an odoo contact, return all billing # addresses with the same parent_id + email = user.email or_conditions = [("email", "ilike", email)] email = user if isinstance(user, str) else user.email diff --git a/src/servala/frontend/forms/organization.py b/src/servala/frontend/forms/organization.py index 41cc26c..115e574 100644 --- a/src/servala/frontend/forms/organization.py +++ b/src/servala/frontend/forms/organization.py @@ -1,6 +1,9 @@ +from django import forms from django.forms import ModelForm +from django.utils.translation import gettext_lazy as _ from servala.core.models import Organization +from servala.core.odoo import get_invoice_addresses from servala.frontend.forms.mixins import HtmxMixin @@ -8,3 +11,102 @@ class OrganizationForm(HtmxMixin, ModelForm): class Meta: model = Organization fields = ("name",) + + +class OrganizationCreateForm(OrganizationForm): + billing_processing_choice = forms.ChoiceField( + choices=[ + ("existing", _("Use an existing billing address")), + ("new", _("Create a new billing address")), + ], + widget=forms.RadioSelect, + label=_("Billing Address"), + initial="new", # Will change to 'existing' if options are found + ) + existing_odoo_address_id = forms.ChoiceField( + label=_("Existing Billing Address"), + required=False, + ) + + # Fields for creating a new billing address in Odoo, prefixed with 'ba_' + ba_name = forms.CharField( + label=_("Contact Person / Company Name"), required=False, max_length=100 + ) + ba_street = forms.CharField(label=_("Street"), required=False, max_length=100) + ba_street2 = forms.CharField( + label=_("Street 2 (Optional)"), required=False, max_length=100 + ) + ba_city = forms.CharField(label=_("City"), required=False, max_length=100) + ba_zip = forms.CharField(label=_("ZIP Code"), required=False, max_length=20) + # For state & country, Odoo uses structured data. For now, text input. + # These will need mapping logic when actual Odoo creation is implemented. + ba_state_name = forms.CharField( + label=_("State / Province"), required=False, max_length=100 + ) + ba_country_name = forms.CharField( + label=_("Country"), required=False, max_length=100 + ) + ba_email = forms.EmailField(label=_("Billing Email"), required=False) + ba_phone = forms.CharField(label=_("Billing Phone"), required=False, max_length=30) + ba_vat = forms.CharField(label=_("VAT ID"), required=False, max_length=50) + + class Meta(OrganizationForm.Meta): + pass + + def __init__(self, *args, user=None, **kwargs): + super().__init__(*args, **kwargs) + self.user = user + self.odoo_addresses = get_invoice_addresses(self.user) + + if self.odoo_addresses: + address_choices = [("", _("---------"))] + for addr in self.odoo_addresses: + display_parts = [ + addr.get("name"), + addr.get("street"), + addr.get("city"), + addr.get("zip"), + ] + display_name = ", ".join(filter(None, display_parts)) + address_choices.append((str(addr["id"]), display_name)) + + self.fields["existing_odoo_address_id"].choices = address_choices + if not self.is_bound and "billing_processing_choice" not in self.initial: + self.fields["billing_processing_choice"].initial = "existing" + else: + # No existing Odoo addresses. Force 'new' choice. + self.fields["billing_processing_choice"].choices = [ + ("new", _("Create a new billing address")), + ] + self.fields["billing_processing_choice"].initial = "new" + self.fields["billing_processing_choice"].widget = forms.HiddenInput() + self.fields["existing_odoo_address_id"].widget = forms.HiddenInput() + + def clean(self): + cleaned_data = super().clean() + choice = cleaned_data.get("billing_processing_choice") + if choice == "new": + required_fields = [ + "ba_name", + "ba_street", + "ba_city", + "ba_zip", + "ba_state_name", + "ba_country_name", + "ba_email", + ] + for field_name in required_fields: + if not cleaned_data.get(field_name): + self.add_error( + field_name, + _( + "This field is required when creating a new billing address." + ), + ) + else: + existing_id_str = cleaned_data.get("existing_odoo_address_id") + if not existing_id_str: + self.add_error( + "existing_odoo_address_id", _("Please select an existing address.") + ) + return cleaned_data diff --git a/src/servala/frontend/templates/frontend/forms/errors.html b/src/servala/frontend/templates/frontend/forms/errors.html new file mode 100644 index 0000000..964687d --- /dev/null +++ b/src/servala/frontend/templates/frontend/forms/errors.html @@ -0,0 +1,18 @@ +{% load i18n %} +{% if form.non_field_errors or form.errors %} + +{% endif %} diff --git a/src/servala/frontend/templates/frontend/forms/form.html b/src/servala/frontend/templates/frontend/forms/form.html index 8ab90d1..361b161 100644 --- a/src/servala/frontend/templates/frontend/forms/form.html +++ b/src/servala/frontend/templates/frontend/forms/form.html @@ -1,21 +1,4 @@ -{% load i18n %} -{% if form.non_field_errors or form.errors %} - -{% endif %} +{% include "frontend/forms/errors.html" %}
{% for field, errors in fields %}{{ field.as_field_group }}{% endfor %} diff --git a/src/servala/frontend/templates/frontend/organizations/create.html b/src/servala/frontend/templates/frontend/organizations/create.html index 2d7cfa4..fc0fe58 100644 --- a/src/servala/frontend/templates/frontend/organizations/create.html +++ b/src/servala/frontend/templates/frontend/organizations/create.html @@ -6,5 +6,67 @@ {% endblock page_title %} {% endblock html_title %} {% block card_content %} - {% include "includes/form.html" %} +
+ {% include "frontend/forms/errors.html" %} + {% csrf_token %} +
+
+ {{ form.name.as_field_group }} +
+

{% translate "Billing Information" %}

+ {{ form.billing_processing_choice.as_field_group }} +
{{ form.existing_odoo_address_id.as_field_group }}
+
+ {{ form.ba_name.as_field_group }} + {{ form.ba_street.as_field_group }} + {{ form.ba_street2.as_field_group }} + {{ form.ba_city.as_field_group }} + {{ form.ba_zip.as_field_group }} + {{ form.ba_state_name.as_field_group }} + {{ form.ba_country_name.as_field_group }} + {{ form.ba_email.as_field_group }} + {{ form.ba_phone.as_field_group }} + {{ form.ba_vat.as_field_group }} +
+
+ +
+
+
+
+ {% endblock card_content %} diff --git a/src/servala/frontend/views/organization.py b/src/servala/frontend/views/organization.py index 7bfdee5..fe0bbb2 100644 --- a/src/servala/frontend/views/organization.py +++ b/src/servala/frontend/views/organization.py @@ -1,18 +1,53 @@ from django.shortcuts import redirect +from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, DetailView from rules.contrib.views import AutoPermissionRequiredMixin -from servala.core.models import Organization -from servala.frontend.forms import OrganizationForm +from servala.core.models import BillingEntity, Organization +from servala.frontend.forms.organization import OrganizationCreateForm, OrganizationForm from servala.frontend.views.mixins import HtmxUpdateView, OrganizationViewMixin class OrganizationCreateView(AutoPermissionRequiredMixin, CreateView): - form_class = OrganizationForm + form_class = OrganizationCreateForm model = Organization template_name = "frontend/organizations/create.html" + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs["user"] = self.request.user + return kwargs + def form_valid(self, form): + billing_choice = form.cleaned_data.get("billing_processing_choice") + billing_entity = None + + if billing_choice == "new": + billing_entity = BillingEntity.create_from_data( + { + key[3:]: value + for key, value in form.cleaned_data.items() + if key.startswith("ba_") + } + ) + elif odoo_id := form.cleaned_data.get("existing_odoo_address_id"): + billing_entity = BillingEntity.objects.filter( + odoo_invoice_id=odoo_id + ).first() + + if not billing_entity: + billing_entity = BillingEntity.create_from_id(odoo_id) + + if not billing_entity: + form.add_error( + None, + _( + "Could not determine or create the billing entity. Please check your input." + ), + ) + return self.form_invalid(form) + + form.instance.billing_entity = billing_entity instance = form.instance.create_organization( form.instance, owner=self.request.user ) From 0bb9f8785de2c09707bdc4361ce8d7f35ee61de7 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Tue, 3 Jun 2025 10:40:10 +0200 Subject: [PATCH 10/22] Retrieve country list from odoo --- src/servala/core/odoo.py | 27 ++++++++++++++ src/servala/frontend/forms/organization.py | 43 ++++++++++++---------- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/servala/core/odoo.py b/src/servala/core/odoo.py index 85f0b35..22d45ec 100644 --- a/src/servala/core/odoo.py +++ b/src/servala/core/odoo.py @@ -87,6 +87,33 @@ class OdooClient: CLIENT = OdooClient() +# Odoo countries do not change, so they are fetched once per process +COUNTRIES = [] + + +def get_odoo_countries(): + global COUNTRIES + if COUNTRIES: + return COUNTRIES + + try: + odoo_countries_data = CLIENT.search_read( + model="res.country", domain=[], fields=["id", "name"] + ) + # Format as Django choices: [(value, label), ...] + COUNTRIES = [ + (country["id"], country["name"]) for country in odoo_countries_data + ] + # Sort by country name for better UX in dropdowns + COUNTRIES.sort(key=lambda x: x[1]) + except Exception as e: + # Log the error or handle it as appropriate for your application + # For now, return an empty list or a default if Odoo is unavailable + print(f"Error fetching Odoo countries: {e}") + return [("", "Error fetching countries")] # Or just [] + + return COUNTRIES + def get_invoice_addresses(user): """Used during organization creation: retrieves all invoice diff --git a/src/servala/frontend/forms/organization.py b/src/servala/frontend/forms/organization.py index 115e574..d37ce91 100644 --- a/src/servala/frontend/forms/organization.py +++ b/src/servala/frontend/forms/organization.py @@ -3,7 +3,7 @@ from django.forms import ModelForm from django.utils.translation import gettext_lazy as _ from servala.core.models import Organization -from servala.core.odoo import get_invoice_addresses +from servala.core.odoo import get_invoice_addresses, get_odoo_countries from servala.frontend.forms.mixins import HtmxMixin @@ -28,33 +28,36 @@ class OrganizationCreateForm(OrganizationForm): required=False, ) - # Fields for creating a new billing address in Odoo, prefixed with 'ba_' - ba_name = forms.CharField( - label=_("Contact Person / Company Name"), required=False, max_length=100 + # Fields for creating a new billing address in Odoo, prefixed with 'invoice_' + invoice_street = forms.CharField(label=_("Line 1"), required=False, max_length=100) + invoice_street2 = forms.CharField(label=_("Line 2"), required=False, max_length=100) + invoice_city = forms.CharField(label=_("City"), required=False, max_length=100) + invoice_zip = forms.CharField(label=_("Postal Code"), required=False, max_length=20) + invoice_country = forms.ChoiceField( + label=_("Country"), + required=False, + choices=get_odoo_countries(), ) - ba_street = forms.CharField(label=_("Street"), required=False, max_length=100) - ba_street2 = forms.CharField( - label=_("Street 2 (Optional)"), required=False, max_length=100 + invoice_email = forms.EmailField(label=_("Billing Email"), required=False) + invoice_phone = forms.CharField( + label=_("Billing Phone"), required=False, max_length=30 ) - ba_city = forms.CharField(label=_("City"), required=False, max_length=100) - ba_zip = forms.CharField(label=_("ZIP Code"), required=False, max_length=20) - # For state & country, Odoo uses structured data. For now, text input. - # These will need mapping logic when actual Odoo creation is implemented. - ba_state_name = forms.CharField( - label=_("State / Province"), required=False, max_length=100 - ) - ba_country_name = forms.CharField( - label=_("Country"), required=False, max_length=100 - ) - ba_email = forms.EmailField(label=_("Billing Email"), required=False) - ba_phone = forms.CharField(label=_("Billing Phone"), required=False, max_length=30) - ba_vat = forms.CharField(label=_("VAT ID"), required=False, max_length=50) + invoice_vat = forms.CharField(label=_("VAT ID"), required=False, max_length=50) class Meta(OrganizationForm.Meta): pass def __init__(self, *args, user=None, **kwargs): super().__init__(*args, **kwargs) + + if not self.initial.get("invoice_country"): + default_country_name = "Switzerland" + country_choices = self.fields["invoice_country"].choices + for country_id, country_name_label in country_choices: + if country_name_label == default_country_name: + self.initial["invoice_country"] = country_id + break + self.user = user self.odoo_addresses = get_invoice_addresses(self.user) From e2b13a322398f83f557f815a98961bce6152a2cf Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Tue, 3 Jun 2025 10:42:12 +0200 Subject: [PATCH 11/22] Rename form fields --- src/servala/frontend/forms/organization.py | 21 +++++++------------ .../frontend/organizations/create.html | 20 +++++++++--------- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/servala/frontend/forms/organization.py b/src/servala/frontend/forms/organization.py index d37ce91..a0b3800 100644 --- a/src/servala/frontend/forms/organization.py +++ b/src/servala/frontend/forms/organization.py @@ -90,26 +90,19 @@ class OrganizationCreateForm(OrganizationForm): choice = cleaned_data.get("billing_processing_choice") if choice == "new": required_fields = [ - "ba_name", - "ba_street", - "ba_city", - "ba_zip", - "ba_state_name", - "ba_country_name", - "ba_email", + "invoice_street", + "invoice_city", + "invoice_zip", + "invoice_country", + "invoice_email", ] for field_name in required_fields: if not cleaned_data.get(field_name): - self.add_error( - field_name, - _( - "This field is required when creating a new billing address." - ), - ) + self.add_error(field_name, _("This field is required.")) else: existing_id_str = cleaned_data.get("existing_odoo_address_id") if not existing_id_str: self.add_error( - "existing_odoo_address_id", _("Please select an existing address.") + "existing_odoo_address_id", _("Please select an invoice address.") ) return cleaned_data diff --git a/src/servala/frontend/templates/frontend/organizations/create.html b/src/servala/frontend/templates/frontend/organizations/create.html index fc0fe58..29ac934 100644 --- a/src/servala/frontend/templates/frontend/organizations/create.html +++ b/src/servala/frontend/templates/frontend/organizations/create.html @@ -17,16 +17,16 @@ {{ form.billing_processing_choice.as_field_group }}
{{ form.existing_odoo_address_id.as_field_group }}
- {{ form.ba_name.as_field_group }} - {{ form.ba_street.as_field_group }} - {{ form.ba_street2.as_field_group }} - {{ form.ba_city.as_field_group }} - {{ form.ba_zip.as_field_group }} - {{ form.ba_state_name.as_field_group }} - {{ form.ba_country_name.as_field_group }} - {{ form.ba_email.as_field_group }} - {{ form.ba_phone.as_field_group }} - {{ form.ba_vat.as_field_group }} + {{ form.invoice_name.as_field_group }} + {{ form.invoice_street.as_field_group }} + {{ form.invoice_street2.as_field_group }} + {{ form.invoice_city.as_field_group }} + {{ form.invoice_zip.as_field_group }} + {{ form.invoice_state_name.as_field_group }} + {{ form.invoice_country.as_field_group }} + {{ form.invoice_email.as_field_group }} + {{ form.invoice_phone.as_field_group }} + {{ form.invoice_vat.as_field_group }}
From f973d33e510e242fc60d48b2d522425e6d3ff6da Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Tue, 3 Jun 2025 11:04:53 +0200 Subject: [PATCH 12/22] Improve look of invoice address form --- .../frontend/organizations/create.html | 96 ++++++++++++++----- 1 file changed, 70 insertions(+), 26 deletions(-) diff --git a/src/servala/frontend/templates/frontend/organizations/create.html b/src/servala/frontend/templates/frontend/organizations/create.html index 29ac934..4fcdc7e 100644 --- a/src/servala/frontend/templates/frontend/organizations/create.html +++ b/src/servala/frontend/templates/frontend/organizations/create.html @@ -5,31 +5,75 @@ {% translate "Create a new organization" %} {% endblock page_title %} {% endblock html_title %} -{% block card_content %} -
- {% include "frontend/forms/errors.html" %} - {% csrf_token %} -
-
- {{ form.name.as_field_group }} -
-

{% translate "Billing Information" %}

- {{ form.billing_processing_choice.as_field_group }} -
{{ form.existing_odoo_address_id.as_field_group }}
-
- {{ form.invoice_name.as_field_group }} - {{ form.invoice_street.as_field_group }} - {{ form.invoice_street2.as_field_group }} - {{ form.invoice_city.as_field_group }} - {{ form.invoice_zip.as_field_group }} - {{ form.invoice_state_name.as_field_group }} - {{ form.invoice_country.as_field_group }} - {{ form.invoice_email.as_field_group }} - {{ form.invoice_phone.as_field_group }} - {{ form.invoice_vat.as_field_group }} +{% block content %} +
+ +
+
+
+
+ {% include "frontend/forms/errors.html" %} + {% csrf_token %} + {{ form.name.as_field_group }} +
+
-
- +
+ {% if form.existing_odoo_address_id and form.existing_odoo_address_id.choices %} +
+
+
+

{% translate "Billing Information" %}

+
+
+
+ {{ form.billing_processing_choice.as_field_group }} +
{{ form.existing_odoo_address_id.as_field_group }}
+
+
+
+
+ {% endif %} +
+
+
+
+

{% translate "Invoice Address" %}

+
+
+
+ {{ form.invoice_vat.as_field_group }} +
+ {{ form.invoice_street.as_field_group }} + {{ form.invoice_street2.as_field_group }} +
{{ form.invoice_zip.as_field_group }}
+
{{ form.invoice_city.as_field_group }}
+ {{ form.invoice_country.as_field_group }} +
+
+
+
+
+
+
+

{% translate "Invoice Contact" %}

+
+
+
+ {{ form.invoice_email.as_field_group }} + {{ form.invoice_phone.as_field_group }} +
+
+
+
+
+
+
+
+
+
+ +
@@ -65,8 +109,8 @@ } else { // No existing addresses found, a new address has to be entered. if (existingSection) existingSection.style.display = 'none' - newSection.style.display = '' + if (newSection) newSection.style.display = '' // Ensure newSection is not null } }); -{% endblock card_content %} +{% endblock content %} From fe6580c3d3f08ccc2b1277ee34715d213bedb1a0 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Tue, 3 Jun 2025 11:19:23 +0200 Subject: [PATCH 13/22] Implement billing address creation via odoo --- src/servala/core/models/organization.py | 63 ++++++++++++++++++++-- src/servala/frontend/forms/organization.py | 13 ++--- src/servala/frontend/views/organization.py | 9 ++-- 3 files changed, 67 insertions(+), 18 deletions(-) diff --git a/src/servala/core/models/organization.py b/src/servala/core/models/organization.py index c113f78..fe22623 100644 --- a/src/servala/core/models/organization.py +++ b/src/servala/core/models/organization.py @@ -1,7 +1,7 @@ import rules import urlman from django.conf import settings -from django.db import models +from django.db import models, transaction from django.utils.functional import cached_property from django.utils.text import slugify from django.utils.translation import gettext_lazy as _ @@ -9,6 +9,7 @@ from django_scopes import ScopedManager, scopes_disabled from servala.core import rules as perms from servala.core.models.mixins import ServalaModelMixin +from servala.core.odoo import CLIENT class Organization(ServalaModelMixin, models.Model): @@ -125,9 +126,63 @@ class BillingEntity(ServalaModelMixin, models.Model): return self.name @classmethod - def create_from_data(cls, odoo_data): - instance = BillingEntity.objects.create(name=odoo_data.get("name")) - # TODO implement odoo creation from data + @transaction.atomic + def create_from_data(cls, name, odoo_data): + """ + Creates a BillingEntity and corresponding Odoo records. + + This method creates a `res.partner` record in Odoo with `company_type='company'` + for the main company, and another `res.partner` record with `company_type='person'` + and `type='invoice'` (linked via `parent_id` to the first record) for the + invoice address. The IDs of these Odoo records are stored in the BillingEntity. + + Args: + odoo_data (dict): A dictionary containing the data for creating + the BillingEntity and Odoo records. + + Expected keys in `odoo_data`: + - `invoice_street` (str): Street for the invoice address. + - `invoice_street2` (str): Second line of street address for the invoice address. + - `invoice_city` (str): City for the invoice address. + - `invoice_zip` (str): ZIP/Postal code for the invoice address. + - `invoice_country` (int): Odoo `res.country` ID for the invoice address country. + - `invoice_email` (str): Email address for the invoice contact. + - `invoice_phone` (str): Phone number for the invoice contact. + """ + instance = cls.objects.create(name=name) + company_payload = { + "name": odoo_data.get("company_name", name), + "company_type": "company", + } + if vat := odoo_data.get("invoice_vat"): + company_payload["vat"] = vat + company_id = CLIENT.execute("res.partner", "create", [company_payload]) + instance.odoo_company_id = company_id + + invoice_address_payload = { + "name": name, + "company_type": "person", + "type": "invoice", + "parent_id": company_id, + } + invoice_optional_fields = { + "street": odoo_data.get("invoice_street"), + "street2": odoo_data.get("invoice_street2"), + "city": odoo_data.get("invoice_city"), + "zip": odoo_data.get("invoice_zip"), + "country_id": odoo_data.get("invoice_country"), + "email": odoo_data.get("invoice_email"), + } + invoice_address_payload.update( + {k: v for k, v in invoice_optional_fields.items() if v is not None} + ) + + invoice_address_id = CLIENT.execute( + "res.partner", "create", [invoice_address_payload] + ) + instance.odoo_invoice_id = invoice_address_id + + instance.save(update_fields=["odoo_company_id", "odoo_invoice_id"]) return instance @classmethod diff --git a/src/servala/frontend/forms/organization.py b/src/servala/frontend/forms/organization.py index a0b3800..b08c2f7 100644 --- a/src/servala/frontend/forms/organization.py +++ b/src/servala/frontend/forms/organization.py @@ -39,9 +39,7 @@ class OrganizationCreateForm(OrganizationForm): choices=get_odoo_countries(), ) invoice_email = forms.EmailField(label=_("Billing Email"), required=False) - invoice_phone = forms.CharField( - label=_("Billing Phone"), required=False, max_length=30 - ) + invoice_phone = forms.CharField(label=_("Phone"), required=False, max_length=30) invoice_vat = forms.CharField(label=_("VAT ID"), required=False, max_length=50) class Meta(OrganizationForm.Meta): @@ -77,18 +75,13 @@ class OrganizationCreateForm(OrganizationForm): if not self.is_bound and "billing_processing_choice" not in self.initial: self.fields["billing_processing_choice"].initial = "existing" else: - # No existing Odoo addresses. Force 'new' choice. - self.fields["billing_processing_choice"].choices = [ - ("new", _("Create a new billing address")), - ] - self.fields["billing_processing_choice"].initial = "new" - self.fields["billing_processing_choice"].widget = forms.HiddenInput() + self.fields.pop("billing_processing_choice") self.fields["existing_odoo_address_id"].widget = forms.HiddenInput() def clean(self): cleaned_data = super().clean() choice = cleaned_data.get("billing_processing_choice") - if choice == "new": + if not choice or choice == "new": required_fields = [ "invoice_street", "invoice_city", diff --git a/src/servala/frontend/views/organization.py b/src/servala/frontend/views/organization.py index fe0bbb2..834dede 100644 --- a/src/servala/frontend/views/organization.py +++ b/src/servala/frontend/views/organization.py @@ -22,13 +22,14 @@ class OrganizationCreateView(AutoPermissionRequiredMixin, CreateView): billing_choice = form.cleaned_data.get("billing_processing_choice") billing_entity = None - if billing_choice == "new": + if not billing_choice or billing_choice == "new": billing_entity = BillingEntity.create_from_data( + form.cleaned_data["name"], { - key[3:]: value + key: value for key, value in form.cleaned_data.items() - if key.startswith("ba_") - } + if key.startswith("invoice_") + }, ) elif odoo_id := form.cleaned_data.get("existing_odoo_address_id"): billing_entity = BillingEntity.objects.filter( From 19fbc4b5f349958c888f2fdadd8c593de074b91d Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Tue, 3 Jun 2025 11:33:32 +0200 Subject: [PATCH 14/22] Show invoice address on organization detail page --- src/servala/core/models/organization.py | 43 ++++++ .../frontend/organizations/update.html | 125 ++++++++++++++---- 2 files changed, 145 insertions(+), 23 deletions(-) diff --git a/src/servala/core/models/organization.py b/src/servala/core/models/organization.py index fe22623..2a90464 100644 --- a/src/servala/core/models/organization.py +++ b/src/servala/core/models/organization.py @@ -192,6 +192,49 @@ class BillingEntity(ServalaModelMixin, models.Model): # return instance pass + @cached_property + def odoo_data(self): + data = { + "company": None, + "invoice_address": None, + } + + company_fields = ["name", "company_type", "vat"] + invoice_address_fields = [ + "name", + "company_type", + "type", + "parent_id", + "street", + "street2", + "city", + "zip", + "country_id", + "email", + ] + + if self.odoo_company_id: + company_records = CLIENT.search_read( + model="res.partner", + domain=[["id", "=", self.odoo_company_id]], + fields=company_fields, + limit=1, + ) + if company_records: + data["company"] = company_records[0] + + if self.odoo_invoice_id: + invoice_address_records = CLIENT.search_read( + model="res.partner", + domain=[["id", "=", self.odoo_invoice_id]], + fields=invoice_address_fields, + limit=1, + ) + if invoice_address_records: + data["invoice_address"] = invoice_address_records[0] + + return data + class OrganizationOrigin(ServalaModelMixin, models.Model): """ diff --git a/src/servala/frontend/templates/frontend/organizations/update.html b/src/servala/frontend/templates/frontend/organizations/update.html index 0d55f22..384d5a5 100644 --- a/src/servala/frontend/templates/frontend/organizations/update.html +++ b/src/servala/frontend/templates/frontend/organizations/update.html @@ -36,26 +36,105 @@ {% endpartialdef org-name-edit %} -{% block card_content %} -
- - - - - {% partial org-name %} - - - - - - -
- {% translate "Name" %} -
- {% translate "Namespace" %} - -
{{ form.instance.namespace }}
- {% translate "System-generated namespace for Kubernetes resources." %} -
-
-{% endblock card_content %} +{% block content %} +
+
+
+
+
+ + + + + {% partial org-name %} + + + + + + +
+ {% translate "Name" %} +
+ {% translate "Namespace" %} + +
{{ form.instance.namespace }}
+ {% translate "System-generated namespace for Kubernetes resources." %} +
+
+
+
+
+ {% if form.instance.billing_entity and form.instance.billing_entity.odoo_data.invoice_address %} +
+
+

{% translate "Billing Address" %}

+
+
+
+ {% with odoo_data=form.instance.billing_entity.odoo_data %} +
+ + + {% if odoo_data.invoice_address %} + + + + + + + + + + {% if odoo_data.invoice_address.street2 %} + + + + + {% endif %} + + + + + + + + + + + + + + + + + + + + {% endif %} + +
+ {% translate "Invoice Contact Name" %} + {{ odoo_data.invoice_address.name|default:"" }}
+ {% translate "Street" %} + {{ odoo_data.invoice_address.street|default:"" }}
+ {% translate "Street 2" %} + {{ odoo_data.invoice_address.street2 }}
+ {% translate "City" %} + {{ odoo_data.invoice_address.city|default:"" }}
+ {% translate "ZIP Code" %} + {{ odoo_data.invoice_address.zip|default:"" }}
+ {% translate "Country" %} + {{ odoo_data.invoice_address.country_id.1|default:"" }}
+ {% translate "VAT ID" %} + {{ odoo_data.company.vat|default:"" }}
+ {% translate "Invoice Email" %} + {{ odoo_data.invoice_address.email|default:"" }}
+
+ {% endwith %} +
+
+
+ {% endif %} +
+{% endblock content %} From b54f8ecbc2a4f150aae92253ffcfc8528f198411 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Wed, 4 Jun 2025 11:06:32 +0200 Subject: [PATCH 15/22] Remove VAT field --- src/servala/core/models/organization.py | 4 +--- src/servala/frontend/forms/organization.py | 1 - .../frontend/templates/frontend/organizations/create.html | 2 -- .../frontend/templates/frontend/organizations/update.html | 6 ------ 4 files changed, 1 insertion(+), 12 deletions(-) diff --git a/src/servala/core/models/organization.py b/src/servala/core/models/organization.py index 2a90464..4b9b435 100644 --- a/src/servala/core/models/organization.py +++ b/src/servala/core/models/organization.py @@ -154,8 +154,6 @@ class BillingEntity(ServalaModelMixin, models.Model): "name": odoo_data.get("company_name", name), "company_type": "company", } - if vat := odoo_data.get("invoice_vat"): - company_payload["vat"] = vat company_id = CLIENT.execute("res.partner", "create", [company_payload]) instance.odoo_company_id = company_id @@ -199,7 +197,7 @@ class BillingEntity(ServalaModelMixin, models.Model): "invoice_address": None, } - company_fields = ["name", "company_type", "vat"] + company_fields = ["name", "company_type"] invoice_address_fields = [ "name", "company_type", diff --git a/src/servala/frontend/forms/organization.py b/src/servala/frontend/forms/organization.py index b08c2f7..37a2a35 100644 --- a/src/servala/frontend/forms/organization.py +++ b/src/servala/frontend/forms/organization.py @@ -40,7 +40,6 @@ class OrganizationCreateForm(OrganizationForm): ) invoice_email = forms.EmailField(label=_("Billing Email"), required=False) invoice_phone = forms.CharField(label=_("Phone"), required=False, max_length=30) - invoice_vat = forms.CharField(label=_("VAT ID"), required=False, max_length=50) class Meta(OrganizationForm.Meta): pass diff --git a/src/servala/frontend/templates/frontend/organizations/create.html b/src/servala/frontend/templates/frontend/organizations/create.html index 4fcdc7e..ae409d3 100644 --- a/src/servala/frontend/templates/frontend/organizations/create.html +++ b/src/servala/frontend/templates/frontend/organizations/create.html @@ -42,8 +42,6 @@
- {{ form.invoice_vat.as_field_group }} -
{{ form.invoice_street.as_field_group }} {{ form.invoice_street2.as_field_group }}
{{ form.invoice_zip.as_field_group }}
diff --git a/src/servala/frontend/templates/frontend/organizations/update.html b/src/servala/frontend/templates/frontend/organizations/update.html index 384d5a5..97d266d 100644 --- a/src/servala/frontend/templates/frontend/organizations/update.html +++ b/src/servala/frontend/templates/frontend/organizations/update.html @@ -116,12 +116,6 @@ {{ odoo_data.invoice_address.country_id.1|default:"" }} - - - {% translate "VAT ID" %} - - {{ odoo_data.company.vat|default:"" }} - {% translate "Invoice Email" %} From 3a16a4a301b783a5b74eb215b255806df17cf748 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Wed, 4 Jun 2025 11:33:17 +0200 Subject: [PATCH 16/22] Fix rendering of radio inputs --- src/servala/frontend/forms/renderers.py | 2 ++ .../django/forms/widgets/input_option.html | 6 ++++++ .../templates/django/forms/widgets/radio.html | 15 +++++++++++++++ src/servala/settings.py | 4 ++-- src/servala/static/css/servala.css | 6 ++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/servala/frontend/templates/django/forms/widgets/input_option.html create mode 100644 src/servala/frontend/templates/django/forms/widgets/radio.html diff --git a/src/servala/frontend/forms/renderers.py b/src/servala/frontend/forms/renderers.py index b6a9995..eb50961 100644 --- a/src/servala/frontend/forms/renderers.py +++ b/src/servala/frontend/forms/renderers.py @@ -20,6 +20,8 @@ class VerticalFormRenderer(TemplatesSetting): def get_class_names(self, field): input_type = self.get_field_input_type(field) errors = "is-invalid " if field.errors else "" + if input_type == "radio": + return f"{errors}form-check-input" if input_type == "checkbox": return f"{errors}form-check-input" return f"{errors}form-control" diff --git a/src/servala/frontend/templates/django/forms/widgets/input_option.html b/src/servala/frontend/templates/django/forms/widgets/input_option.html new file mode 100644 index 0000000..9a7654d --- /dev/null +++ b/src/servala/frontend/templates/django/forms/widgets/input_option.html @@ -0,0 +1,6 @@ +{% include "django/forms/widgets/input.html" %} +{% if widget.wrap_label %} + +{% endif %} diff --git a/src/servala/frontend/templates/django/forms/widgets/radio.html b/src/servala/frontend/templates/django/forms/widgets/radio.html new file mode 100644 index 0000000..1f363b9 --- /dev/null +++ b/src/servala/frontend/templates/django/forms/widgets/radio.html @@ -0,0 +1,15 @@ +{# Change compared to Django: only render widget.attrs.class in actual option widget, not in wrapper #} +{% with id=widget.attrs.id %} +
+ {% for group, options, index in widget.optgroups %} + {% if group %} +
+ + {% endif %} + {% for option in options %} +
{% include option.template_name with widget=option %}
+ {% endfor %} + {% if group %}
{% endif %} + {% endfor %} +
+{% endwith %} diff --git a/src/servala/settings.py b/src/servala/settings.py index a8b3fbc..bb69ebb 100644 --- a/src/servala/settings.py +++ b/src/servala/settings.py @@ -145,11 +145,11 @@ INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + # The frontend app is loaded early in order to supersede some allauth views/behaviour + "servala.frontend", "django.forms", "template_partials", "rules.apps.AutodiscoverRulesConfig", - # The frontend app is loaded early in order to supersede some allauth views/behaviour - "servala.frontend", "allauth", "allauth.account", "allauth.socialaccount", diff --git a/src/servala/static/css/servala.css b/src/servala/static/css/servala.css index 1e418fb..b7a5b8b 100644 --- a/src/servala/static/css/servala.css +++ b/src/servala/static/css/servala.css @@ -54,6 +54,12 @@ html[data-bs-theme="dark"] .btn-outline-primary, .btn-outline-primary { margin-bottom: 0; } +fieldset .form-check-input + label { + font-weight: normal; + min-height: 1.5rem; + margin: .125rem; +} + .search-form .form-body>.row { display: flex; &>.col-12 { From c7d079e9bf1aa5c785a65c8b80c6bf5489a8ec6c Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Wed, 4 Jun 2025 11:41:16 +0200 Subject: [PATCH 17/22] Fix missing invoice address choices --- .../frontend/templates/frontend/organizations/create.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/servala/frontend/templates/frontend/organizations/create.html b/src/servala/frontend/templates/frontend/organizations/create.html index ae409d3..967e7ec 100644 --- a/src/servala/frontend/templates/frontend/organizations/create.html +++ b/src/servala/frontend/templates/frontend/organizations/create.html @@ -19,7 +19,7 @@
- {% if form.existing_odoo_address_id and form.existing_odoo_address_id.choices %} + {% if form.billing_processing_choice %}
From d306cbe52bcd1b59f31fedddfaf13e90669703c8 Mon Sep 17 00:00:00 2001 From: Renovate Bot Date: Thu, 5 Jun 2025 03:01:35 +0000 Subject: [PATCH 18/22] Update dependency django to v5.2.2 --- pyproject.toml | 2 +- uv.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8c10259..d81d43f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ requires-python = ">=3.13.3" dependencies = [ "argon2-cffi>=25.1.0", "cryptography>=45.0.3", - "django==5.2.1", + "django==5.2.2", "django-allauth>=65.9.0", "django-fernet-encrypted-fields>=0.3.0", "django-scopes>=2.0.0", diff --git a/uv.lock b/uv.lock index 148ac2c..b851752 100644 --- a/uv.lock +++ b/uv.lock @@ -253,16 +253,16 @@ wheels = [ [[package]] name = "django" -version = "5.2.1" +version = "5.2.2" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "asgiref" }, { name = "sqlparse" }, { name = "tzdata", marker = "sys_platform == 'win32'" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/ac/10/0d546258772b8f31398e67c85e52c66ebc2b13a647193c3eef8ee433f1a8/django-5.2.1.tar.gz", hash = "sha256:57fe1f1b59462caed092c80b3dd324fd92161b620d59a9ba9181c34746c97284", size = 10818735, upload-time = "2025-05-07T14:06:17.543Z" } +sdist = { url = "https://files.pythonhosted.org/packages/39/17/4567ee12bb84114c544d5c4a792e7226db517ac78f552111e9dc62d1de14/django-5.2.2.tar.gz", hash = "sha256:85852e517f84435e9b13421379cd6c43ef5b48a9c8b391d29a26f7900967e952", size = 10827542, upload-time = "2025-06-04T13:52:40.879Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/90/92/7448697b5838b3a1c6e1d2d6a673e908d0398e84dc4f803a2ce11e7ffc0f/django-5.2.1-py3-none-any.whl", hash = "sha256:a9b680e84f9a0e71da83e399f1e922e1ab37b2173ced046b541c72e1589a5961", size = 8301833, upload-time = "2025-05-07T14:06:10.955Z" }, + { url = "https://files.pythonhosted.org/packages/b4/5c/5d00acab6c062b154e5a0f092938ae5a0c698dbc4362b68e23200960f32c/django-5.2.2-py3-none-any.whl", hash = "sha256:997ef2162d04ead6869551b22cde4e06da1f94cf595f4af3f3d3afeae1f3f6fe", size = 8302562, upload-time = "2025-06-04T13:52:33.14Z" }, ] [[package]] @@ -954,7 +954,7 @@ dev = [ requires-dist = [ { name = "argon2-cffi", specifier = ">=25.1.0" }, { name = "cryptography", specifier = ">=45.0.3" }, - { name = "django", specifier = "==5.2.1" }, + { name = "django", specifier = "==5.2.2" }, { name = "django-allauth", specifier = ">=65.9.0" }, { name = "django-fernet-encrypted-fields", specifier = ">=0.3.0" }, { name = "django-scopes", specifier = ">=2.0.0" }, From 7c2f1b74d51ac7b3d91e2a2426afe0eb637e5aa8 Mon Sep 17 00:00:00 2001 From: Renovate Bot Date: Thu, 5 Jun 2025 03:01:40 +0000 Subject: [PATCH 19/22] Update Python to >=3.13.4 --- pyproject.toml | 2 +- uv.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8c10259..ecc3b81 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ name = "servala" version = "0.0.0" description = "Servala portal server and frontend" readme = "README.md" -requires-python = ">=3.13.3" +requires-python = ">=3.13.4" dependencies = [ "argon2-cffi>=25.1.0", "cryptography>=45.0.3", diff --git a/uv.lock b/uv.lock index 148ac2c..703217e 100644 --- a/uv.lock +++ b/uv.lock @@ -1,6 +1,6 @@ version = 1 revision = 2 -requires-python = ">=3.13.3" +requires-python = ">=3.13.4" [[package]] name = "argon2-cffi" From 9a46229e813faa498f918d121436eb98eb744798 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Thu, 5 Jun 2025 09:24:34 +0200 Subject: [PATCH 20/22] Code style --- src/servala/settings.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/servala/settings.py b/src/servala/settings.py index bb69ebb..8322e4a 100644 --- a/src/servala/settings.py +++ b/src/servala/settings.py @@ -10,11 +10,12 @@ Servala is run using environment variables. Documentation: """ import os -import sentry_sdk from pathlib import Path + +import sentry_sdk +from django.contrib import messages from sentry_sdk.integrations.django import DjangoIntegration -from django.contrib import messages from servala.__about__ import __version__ as version SERVALA_ENVIRONMENT = os.environ.get("SERVALA_ENVIRONMENT", "development") From dae5e7153bbd30903239680abea8484e7df06a6b Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Thu, 5 Jun 2025 09:24:42 +0200 Subject: [PATCH 21/22] Implement Odoo parent ID fetch --- src/servala/core/models/organization.py | 27 ++++++++++++++++++---- src/servala/frontend/views/organization.py | 5 ++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/servala/core/models/organization.py b/src/servala/core/models/organization.py index 4b9b435..e610572 100644 --- a/src/servala/core/models/organization.py +++ b/src/servala/core/models/organization.py @@ -184,11 +184,28 @@ class BillingEntity(ServalaModelMixin, models.Model): return instance @classmethod - def create_from_id(cls, odoo_id): - # TODO implement odoo creation from ID - # instance = BillingEntity.objects.create(name=odoo_data.get("name")) - # return instance - pass + @transaction.atomic + def create_from_id(cls, name, odoo_id): + parent_data = CLIENT.search_read( + model="res.partner", + domain=[["id", "=", odoo_id]], + fields=["parent_id"], + limit=1, + ) + # Data validation: If the data is not as expected, we just return None, + # rather than raising an exception, for now. + if not parent_data: + return + if not (parent_info := parent_data[0].get("parent_id")): + return + if not isinstance(parent_info, (list, tuple)) or not len(parent_info) > 0: + # parent_info is a tuple of the parent’s ID and name + return + + instance = cls.objects.create( + name=name, odoo_invoice_id=odoo_id, odoo_company_id=parent_info[0] + ) + return instance @cached_property def odoo_data(self): diff --git a/src/servala/frontend/views/organization.py b/src/servala/frontend/views/organization.py index 834dede..29eb5f5 100644 --- a/src/servala/frontend/views/organization.py +++ b/src/servala/frontend/views/organization.py @@ -21,10 +21,11 @@ class OrganizationCreateView(AutoPermissionRequiredMixin, CreateView): def form_valid(self, form): billing_choice = form.cleaned_data.get("billing_processing_choice") billing_entity = None + name = form.cleaned_data["name"] if not billing_choice or billing_choice == "new": billing_entity = BillingEntity.create_from_data( - form.cleaned_data["name"], + name, { key: value for key, value in form.cleaned_data.items() @@ -37,7 +38,7 @@ class OrganizationCreateView(AutoPermissionRequiredMixin, CreateView): ).first() if not billing_entity: - billing_entity = BillingEntity.create_from_id(odoo_id) + billing_entity = BillingEntity.create_from_id(name, odoo_id) if not billing_entity: form.add_error( From 9008833306315db819d88cf22768186df35985a6 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Thu, 5 Jun 2025 10:58:12 +0200 Subject: [PATCH 22/22] add billing entity docs --- docs/modules/ROOT/nav.adoc | 1 + .../ROOT/pages/web-portal-billingentity.adoc | 26 +++++++++++++++++++ src/servala/core/models/organization.py | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 docs/modules/ROOT/pages/web-portal-billingentity.adoc diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc index 447852e..cff0b83 100644 --- a/docs/modules/ROOT/nav.adoc +++ b/docs/modules/ROOT/nav.adoc @@ -4,6 +4,7 @@ * xref:web-portal.adoc[] ** xref:web-portal-admin.adoc[Admin] ** xref:web-portal-controlplanes.adoc[Control-Planes] +** xref:web-portal-billingentity.adoc[Billing Entities] * xref:web-portal-planning.adoc[] ** xref:user-stories.adoc[] diff --git a/docs/modules/ROOT/pages/web-portal-billingentity.adoc b/docs/modules/ROOT/pages/web-portal-billingentity.adoc new file mode 100644 index 0000000..cd7b662 --- /dev/null +++ b/docs/modules/ROOT/pages/web-portal-billingentity.adoc @@ -0,0 +1,26 @@ += Web Portal Billing Entities + +Billing entities are used to connect an invoice address in Odoo to an organization in Servala. + +When creating a new organization, the billing information is required to be added. + +== Existing Billing Address + +With the email address of the currently logged-in user, Odoo is searched for existing `res.partner` records and presented in the dropdown. + +Search is done this way: + +* `res.partner` records created by a matching Odoo user. +* User email matches an invoice address or contact address + +== New Billing Address + +When choosing to add a new billing address, two new records are created in the Odoo `res.partner` model: + +* A record with the field `company_type = company` +* A record with the following field configuration: +** `company_type = person` +** `type = invoice` +** `parent_id = company_id` + +The resulting database IDs are stored in the Servala portal database for referencing the records in Odoo. diff --git a/src/servala/core/models/organization.py b/src/servala/core/models/organization.py index e610572..e89d6e3 100644 --- a/src/servala/core/models/organization.py +++ b/src/servala/core/models/organization.py @@ -114,7 +114,7 @@ class BillingEntity(ServalaModelMixin, models.Model): # Odoo IDs are nullable for creation, should never be null in practice # The company ID points at a record of type res.partner with company_type=company # The invoice ID points at a record of type res.partner with company_type=person, - # type=invoic, parent_id=company_id (the invoice address). + # type=invoice, parent_id=company_id (the invoice address). odoo_company_id = models.IntegerField(null=True) odoo_invoice_id = models.IntegerField(null=True)