From 76bc37e3f0fc5ab982fc940bd0900aa0dd468a99 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Thu, 3 Jul 2025 16:43:19 +0200 Subject: [PATCH] refactor odoo company type handling --- .../ROOT/pages/web-portal-billingentity.adoc | 4 ++-- src/servala/core/models/organization.py | 16 ++++++++-------- src/servala/core/models/user.py | 4 ++-- src/servala/core/odoo.py | 10 +++++----- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/modules/ROOT/pages/web-portal-billingentity.adoc b/docs/modules/ROOT/pages/web-portal-billingentity.adoc index cd7b662..0b485c8 100644 --- a/docs/modules/ROOT/pages/web-portal-billingentity.adoc +++ b/docs/modules/ROOT/pages/web-portal-billingentity.adoc @@ -17,9 +17,9 @@ Search is done this way: 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 field `is_company = False` * A record with the following field configuration: -** `company_type = person` +** `is_company = False` ** `type = invoice` ** `parent_id = company_id` diff --git a/src/servala/core/models/organization.py b/src/servala/core/models/organization.py index a9dcd4e..b8a4e2a 100644 --- a/src/servala/core/models/organization.py +++ b/src/servala/core/models/organization.py @@ -147,8 +147,8 @@ 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, + # The company ID points at a record of type res.partner with is_company=True + # The invoice ID points at a record of type res.partner with is_company=False, # type=invoice, parent_id=company_id (the invoice address). odoo_company_id = models.IntegerField(null=True) odoo_invoice_id = models.IntegerField(null=True) @@ -166,8 +166,8 @@ class BillingEntity(ServalaModelMixin, models.Model): """ 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'` + This method creates a `res.partner` record in Odoo with `is_company=True` + for the main company, and another `res.partner` record with `is_company=False` 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. @@ -187,14 +187,14 @@ class BillingEntity(ServalaModelMixin, models.Model): instance = cls.objects.create(name=name) company_payload = { "name": odoo_data.get("company_name", name), - "company_type": "company", + "is_company": True, } company_id = CLIENT.execute("res.partner", "create", [company_payload]) instance.odoo_company_id = company_id invoice_address_payload = { "name": name, - "company_type": "person", + "is_company": False, "type": "invoice", "parent_id": company_id, } @@ -249,10 +249,10 @@ class BillingEntity(ServalaModelMixin, models.Model): "invoice_address": None, } - company_fields = ["name", "company_type"] + company_fields = ["name", "is_company"] invoice_address_fields = [ "name", - "company_type", + "is_company", "type", "parent_id", "street", diff --git a/src/servala/core/models/user.py b/src/servala/core/models/user.py index 38cf80c..b8adfa4 100644 --- a/src/servala/core/models/user.py +++ b/src/servala/core/models/user.py @@ -84,7 +84,7 @@ class User(ServalaModelMixin, PermissionsMixin, AbstractBaseUser): result = odoo.CLIENT.search_read( model="res.partner", domain=[ - ("company_type", "=", "person"), + ("is_company", "=", False), ("type", "=", "contact"), ("email", "ilike", self.email), ("parent_id", "=", organization.billing_entity.odoo_company_id), @@ -107,7 +107,7 @@ class User(ServalaModelMixin, PermissionsMixin, AbstractBaseUser): partner_data = { "name": f"{self.first_name} {self.last_name}".strip() or self.email, "email": self.email, - "company_type": "person", + "is_company": False, "type": "contact", "parent_id": organization.billing_entity.odoo_company_id, } diff --git a/src/servala/core/odoo.py b/src/servala/core/odoo.py index 3d98e18..ba91dc7 100644 --- a/src/servala/core/odoo.py +++ b/src/servala/core/odoo.py @@ -15,7 +15,7 @@ ADDRESS_FIELDS = [ "email", "phone", "vat", - "company_type", + "is_company", "type", "parent_id", ] @@ -118,8 +118,8 @@ def get_odoo_countries(): def get_odoo_access_conditions(user): - # We’re building our conditions in order: - # - in exceptions, users may be using a billing account’s email + # 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 @@ -153,7 +153,7 @@ def get_odoo_access_conditions(user): odoo_contacts = CLIENT.search_read( model="res.partner", domain=[ - ("company_type", "=", "person"), + ("is_company", "=", False), ("type", "=", "contact"), ("email", "ilike", email), ], @@ -186,7 +186,7 @@ def get_invoice_addresses(user): or_conditions = get_odoo_access_conditions(user) domain = [ - ("company_type", "=", "person"), + ("is_company", "=", False), ("type", "=", "invoice"), ] + or_conditions