From 16a1fc1fe37a96f5c16bc35b0de9eb5d6bfd615b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81lvaro=20Garc=C3=ADa?= <alvarogruiz8@gmail.com>
Date: Thu, 6 Mar 2025 13:17:08 +0100
Subject: [PATCH] =?UTF-8?q?[REF]=20=E2=99=BB=EF=B8=8F=20=20energy=5Fcommun?=
 =?UTF-8?q?ities=5Fservice=5Finvoicing?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Author: Alvaro Garcia <alvaro.garcia@somit.coop>
Author: Dani Quilez <dani.quilez@somenergia.coop>
---
 .../components/contract_utils.py              | 18 ++++----
 .../components/sale_order_utils.py            | 12 ++---
 .../data/product_data.xml                     |  4 +-
 .../models/account_move.py                    | 22 +++++----
 .../models/contract.py                        | 30 ++++++------
 .../models/contract_template.py               | 46 ++++++++++++-------
 .../models/product_template.py                | 12 ++---
 .../models/res_partner.py                     | 34 +++++++-------
 .../report/report_invoice.xml                 | 17 +------
 energy_communities_service_invoicing/utils.py | 14 +++---
 .../views/account_move_views.xml              |  4 +-
 .../views/contract_views.xml                  | 10 ++--
 .../views/res_partner_views.xml               |  4 +-
 .../views/service_invoicing_views.xml         | 10 ++--
 .../wizards/service_invoicing_action.py       | 13 +++---
 .../wizards/service_invoicing_action.xml      |  5 +-
 .../service_invoicing_action_create.py        | 29 ++++++------
 .../service_invoicing_action_create.xml       |  6 +--
 energy_selfconsumption/__manifest__.py        |  2 +
 energy_selfconsumption/data/product_data.xml  |  9 ++++
 energy_selfconsumption/models/__init__.py     |  1 +
 .../models/contract_template.py               | 15 ++++++
 22 files changed, 175 insertions(+), 142 deletions(-)
 create mode 100644 energy_selfconsumption/data/product_data.xml
 create mode 100644 energy_selfconsumption/models/contract_template.py

diff --git a/energy_communities_service_invoicing/components/contract_utils.py b/energy_communities_service_invoicing/components/contract_utils.py
index 00541918e..3269860b8 100644
--- a/energy_communities_service_invoicing/components/contract_utils.py
+++ b/energy_communities_service_invoicing/components/contract_utils.py
@@ -71,7 +71,7 @@ class ContractUtils(Component):
         execution_date,
         executed_modification_action,
         pricelist_id=None,
-        service_pack_id=None,
+        pack_id=None,
         discount=None,
         payment_mode_id=None,
     ):
@@ -89,7 +89,7 @@ class ContractUtils(Component):
                 executed_modification_action,
                 execution_date,
                 pricelist_id,
-                service_pack_id,
+                pack_id,
                 discount,
                 payment_mode_id,
             )
@@ -105,7 +105,7 @@ class ContractUtils(Component):
         self,
         execution_date,
         pricelist_id=None,
-        service_pack_id=None,
+        pack_id=None,
         discount=None,
         payment_mode_id=None,
     ):
@@ -115,10 +115,10 @@ class ContractUtils(Component):
         ).create_service_invoicing_initial(
             **self._build_service_invoicing_params(
                 "reopen",
-                "modify_service_pack,modify_pricelist,modify_discount,modify_payment_mode",
+                "modify_pack,modify_pricelist,modify_discount,modify_payment_mode",
                 execution_date,
                 pricelist_id,
-                service_pack_id,
+                pack_id,
                 discount,
                 payment_mode_id,
             )
@@ -132,7 +132,7 @@ class ContractUtils(Component):
         executed_action_description,
         execution_date,
         pricelist_id=None,
-        service_pack_id=None,
+        pack_id=None,
         discount=None,
         payment_mode_id=None,
     ):
@@ -140,9 +140,9 @@ class ContractUtils(Component):
         return {
             "company_id": self.work.record.partner_id.related_company_id,
             "community_company_id": self.work.record.community_company_id,
-            "service_pack_id": service_pack_id
-            if "modify_service_pack" in executed_action_description_list
-            else self.work.record.service_pack_id,
+            "pack_id": pack_id
+            if "modify_pack" in executed_action_description_list
+            else self.work.record.pack_id,
             "pricelist_id": pricelist_id
             if "modify_pricelist" in executed_action_description_list
             else self.work.record.pricelist_id,
diff --git a/energy_communities_service_invoicing/components/sale_order_utils.py b/energy_communities_service_invoicing/components/sale_order_utils.py
index 86ab78a50..d5bdbe3ee 100644
--- a/energy_communities_service_invoicing/components/sale_order_utils.py
+++ b/energy_communities_service_invoicing/components/sale_order_utils.py
@@ -9,7 +9,7 @@ class SaleOrderUtils(Component):
         self,
         company_id,
         community_company_id,
-        service_pack_id,
+        pack_id,
         pricelist_id,
         payment_mode_id,
         start_date,
@@ -28,7 +28,7 @@ class SaleOrderUtils(Component):
                     0,
                     0,
                     {
-                        "product_id": service_pack_id.id,
+                        "product_id": pack_id.id,
                         "date_start": start_date,
                         "date_end": start_date,
                     },
@@ -50,7 +50,7 @@ class SaleOrderUtils(Component):
         self,
         company_id,
         community_company_id,
-        service_pack_id,
+        pack_id,
         pricelist_id,
         payment_mode_id,
         start_date,
@@ -61,7 +61,7 @@ class SaleOrderUtils(Component):
         so = self.create_service_invoicing_sale_order(
             company_id,
             community_company_id,
-            service_pack_id,
+            pack_id,
             pricelist_id,
             payment_mode_id,
             start_date,
@@ -82,7 +82,7 @@ class SaleOrderUtils(Component):
         self,
         company_id,
         community_company_id,
-        service_pack_id,
+        pack_id,
         pricelist_id,
         start_date,
         discount,
@@ -93,7 +93,7 @@ class SaleOrderUtils(Component):
         service_invoicing_id = self._create_service_invoicing(
             company_id,
             community_company_id,
-            service_pack_id,
+            pack_id,
             pricelist_id,
             payment_mode_id,
             start_date,
diff --git a/energy_communities_service_invoicing/data/product_data.xml b/energy_communities_service_invoicing/data/product_data.xml
index 89e2a323c..bb69508a5 100644
--- a/energy_communities_service_invoicing/data/product_data.xml
+++ b/energy_communities_service_invoicing/data/product_data.xml
@@ -1,8 +1,8 @@
 <odoo>
-  <record id="product_category_pack" model="product.category">
+  <record id="product_category_platform_pack" model="product.category">
     <field name="name">Platform Service Pack</field>
   </record>
-  <record id="product_category_service" model="product.category">
+  <record id="product_category_platform_service" model="product.category">
     <field name="name">Platform Service</field>
   </record>
 </odoo>
diff --git a/energy_communities_service_invoicing/models/account_move.py b/energy_communities_service_invoicing/models/account_move.py
index 378c9bd62..381e6c69b 100644
--- a/energy_communities_service_invoicing/models/account_move.py
+++ b/energy_communities_service_invoicing/models/account_move.py
@@ -6,20 +6,24 @@ class AccountMove(models.Model):
 
     ref_invoice_id = fields.Many2one(
         comodel_name="account.move",
-        compute="_compute_ref_invoice_id_related_contract_id_is_pack_is_contract",
+        compute="_compute_ref_invoice_id_related_contract_id_pack_type_is_contract",
+        compute_sudo=True,
         store=False,
     )
     related_contract_id = fields.Many2one(
         comodel_name="contract.contract",
-        compute="_compute_ref_invoice_id_related_contract_id_is_pack_is_contract",
+        compute="_compute_ref_invoice_id_related_contract_id_pack_type_is_contract",
+        compute_sudo=True,
         store=False,
     )
-    is_pack = fields.Boolean(
-        compute="_compute_ref_invoice_id_related_contract_id_is_pack_is_contract",
+    pack_type = fields.Boolean(
+        compute="_compute_ref_invoice_id_related_contract_id_pack_type_is_contract",
+        compute_sudo=True,
         store=True,
     )
     is_contract = fields.Boolean(
-        compute="_compute_ref_invoice_id_related_contract_id_is_pack_is_contract",
+        compute="_compute_ref_invoice_id_related_contract_id_pack_type_is_contract",
+        compute_sudo=True,
         store=True,
     )
     related_community_company_id = fields.Many2one(
@@ -30,11 +34,11 @@ class AccountMove(models.Model):
     )
 
     @api.depends("invoice_line_ids", "ref")
-    def _compute_ref_invoice_id_related_contract_id_is_pack_is_contract(self):
+    def _compute_ref_invoice_id_related_contract_id_pack_type_is_contract(self):
         for record in self:
             record.ref_invoice_id = False
             record.related_contract_id = False
-            record.is_pack = False
+            record.pack_type = 'none'
             record.is_contract = False
             rel_inv = False
             if record.ref:
@@ -45,7 +49,7 @@ class AccountMove(models.Model):
                 )
                 if rel_inv:
                     record.ref_invoice_id = rel_inv.id
-                    record.is_pack = rel_inv.is_pack
+                    record.pack_type = rel_inv.pack_type
                     record.is_contract = rel_inv.is_contract
                     if rel_inv.related_contract_id:
                         record.related_contract_id = rel_inv.related_contract_id.id
@@ -54,6 +58,6 @@ class AccountMove(models.Model):
                     first_move_line = record.invoice_line_ids[0]
                     if first_move_line.contract_line_id:
                         rel_contract = first_move_line.contract_line_id.contract_id
-                        record.is_pack = rel_contract.is_pack
+                        record.pack_type = rel_contract.pack_type
                         record.related_contract_id = rel_contract.id
                         record.is_contract = True
diff --git a/energy_communities_service_invoicing/models/contract.py b/energy_communities_service_invoicing/models/contract.py
index 512e65349..0d949cfe0 100644
--- a/energy_communities_service_invoicing/models/contract.py
+++ b/energy_communities_service_invoicing/models/contract.py
@@ -7,7 +7,7 @@ from ..utils import (
     _CONTRACT_STATUS_VALUES,
     _SALE_ORDER_SERVICE_INVOICING_ACTION_VALUES,
     get_existing_open_pack_contract,
-    raise_existing_same_open_pack_contract_error,
+    raise_existing_same_open_platform_pack_contract_error,
 )
 
 _CLOSING_ACTION_VALUES = _SALE_ORDER_SERVICE_INVOICING_ACTION_VALUES + [
@@ -45,7 +45,7 @@ class ContractContract(models.Model):
     last_date_invoiced = fields.Date(
         string="Last Date Invoiced", compute="_compute_last_date_invoiced", store=False
     )
-    is_pack = fields.Boolean(related="contract_template_id.is_pack")
+    pack_type = fields.Selection(related="contract_template_id.pack_type")
     is_free_pack = fields.Boolean(related="contract_template_id.is_free_pack")
     closing_action = fields.Selection(
         selection=_CLOSING_ACTION_VALUES,
@@ -65,10 +65,10 @@ class ContractContract(models.Model):
         compute="_compute_related_contract_product_ids",
         store=False,
     )
-    service_pack_id = fields.Many2one(
+    pack_id = fields.Many2one(
         "product.product",
         string="Service Pack",
-        compute="_compute_service_pack_id",
+        compute="_compute_pack_id",
         store=False,
     )
     sale_order_id = fields.Many2one(
@@ -107,9 +107,9 @@ class ContractContract(models.Model):
     def _constrain_unique_contract(self):
         for record in self:
             if record.community_company_id:
-                existing_contract = record._get_existing_same_open_pack_contract()
+                existing_contract = record._get_existing_same_open_platform_pack_contract()
                 if existing_contract:
-                    raise_existing_same_open_pack_contract_error(existing_contract)
+                    raise_existing_same_open_platform_pack_contract_error(existing_contract)
 
     def _compute_received_invoices_count(self):
         for record in self:
@@ -142,9 +142,9 @@ class ContractContract(models.Model):
                 ].last_date_invoiced
 
     @api.depends("contract_template_id")
-    def _compute_service_pack_id(self):
+    def _compute_pack_id(self):
         for record in self:
-            record.service_pack_id = False
+            record.pack_id = False
             if record.contract_template_id:
                 rel_product = self.env["product.product"].search(
                     [
@@ -157,7 +157,7 @@ class ContractContract(models.Model):
                     limit=1,
                 )
                 if rel_product:
-                    record.service_pack_id = rel_product.id
+                    record.pack_id = rel_product.id
 
     def _recurring_create_invoice(self, date_ref=False):
         moves = super()._recurring_create_invoice(date_ref)
@@ -177,11 +177,11 @@ class ContractContract(models.Model):
 
     def action_reopen_contract(self):
         return self._action_contract(
-            "reopen", self.service_pack_id, self.pricelist_id, self.payment_mode_id
+            "reopen", self.pack_id, self.pricelist_id, self.payment_mode_id
         )
 
     def _action_contract(
-        self, action, service_pack_id=False, pricelist_id=False, payment_mode_id=False
+        self, action, pack_id=False, pricelist_id=False, payment_mode_id=False
     ):
         self.ensure_one()
         create_dict = {
@@ -189,8 +189,8 @@ class ContractContract(models.Model):
             "executed_action": action,
             "discount": self.discount,
         }
-        if service_pack_id:
-            create_dict["service_pack_id"] = service_pack_id.id
+        if pack_id:
+            create_dict["pack_id"] = pack_id.id
         if pricelist_id:
             create_dict["pricelist_id"] = pricelist_id.id
         if payment_mode_id:
@@ -259,9 +259,9 @@ class ContractContract(models.Model):
                     received_invoices.append(invoice.id)
         return received_invoices
 
-    def _get_existing_same_open_pack_contract(self):
+    def _get_existing_same_open_platform_pack_contract(self):
         return get_existing_open_pack_contract(
-            self.env, self.partner_id, self.community_company_id, self
+            self.env, self.partner_id, "platform_pack", contract_id=self, custom_query=[("community_company_id", "=", self.community_company_id.id)]
         )
 
     def get_active_monitoring_members(self):
diff --git a/energy_communities_service_invoicing/models/contract_template.py b/energy_communities_service_invoicing/models/contract_template.py
index c4008db80..440855b79 100644
--- a/energy_communities_service_invoicing/models/contract_template.py
+++ b/energy_communities_service_invoicing/models/contract_template.py
@@ -1,31 +1,45 @@
-from odoo import api, fields, models
+from odoo import api, fields, models, _
 
+PACK_VALUES = [
+    ("platform_pack", _("Platform Pack")),
+    ("none", _("None")),
+]
 
 class ContractTemplate(models.Model):
     _name = "contract.template"
     _inherit = "contract.template"
 
-    is_pack = fields.Boolean(compute="compute_is_pack", store=True)
     is_free_pack = fields.Boolean(string="Is a free pack")
-
-    def compute_is_pack(self):
-        try:
-            categ_id = self.env.ref(
-                "energy_communities_service_invoicing.product_category_pack"
-            ).id
-        except:
-            categ_id = False
-        for record in self:
-            if categ_id:
-                record.is_pack = bool(
+    pack_type = fields.Selection(PACK_VALUES, compute="_compute_pack_type", string="Pack Type", store=True)
+    
+    def _get_pack_product_from_category(self, category_id, value):
+        return value if bool(
                     self.env["product.template"].search(
                         [
-                            ("property_contract_template_id", "=", record.id),
+                            ("property_contract_template_id", "=", self.id),
                             (
                                 "categ_id",
                                 "=",
-                                categ_id,
+                                category_id,
                             ),
                         ]
                     )
-                )
+                ) else 'none'
+    
+    def _set_custom_pack_type(self, ref_category, value):
+        try:
+            categ_id = self.env.ref(
+                ref_category
+            ).id
+        except:
+            categ_id = False
+        if categ_id:
+            self.pack_type = self._get_pack_product_from_category(categ_id, value)
+
+    def custom_compute_pack_type(self):
+        self._set_custom_pack_type("energy_communities_service_invoicing.product_category_platform_pack", 'platform_pack')
+
+    def _compute_pack_type(self):
+        for record in self:
+            record.custom_compute_pack_type()
+
diff --git a/energy_communities_service_invoicing/models/product_template.py b/energy_communities_service_invoicing/models/product_template.py
index d9b8d3eef..6b56c94ee 100644
--- a/energy_communities_service_invoicing/models/product_template.py
+++ b/energy_communities_service_invoicing/models/product_template.py
@@ -13,6 +13,7 @@ class ProductTemplate(models.Model):
         compute="_compute_related_contract_product_ids",
         store=False,
     )
+    pack_type = fields.Selection(related="property_contract_template_id.pack_type")
 
     @api.depends("property_contract_template_id")
     def _compute_related_contract_product_ids(self):
@@ -25,14 +26,13 @@ class ProductTemplate(models.Model):
                 record.related_contract_product_ids = rel_products
 
     @api.constrains("property_contract_template_id")
-    def compute_contract_template_is_pack(self):
-        for record in self:
-            ctemplates = self.env["contract.template"].search([])
-            for ctemplate in ctemplates:
-                ctemplate.compute_is_pack()
+    def _constraint_contract_template_pack_type(self):
+        ctemplates = self.env["contract.template"].search([])
+        for ctemplate in ctemplates:
+            ctemplate._compute_pack_type()
 
     @api.constrains("description_sale")
-    def compute_contract_template_line_name(self):
+    def _constraint_contract_template_line_name(self):
         for record in self:
             ctemplatelines = self.env["contract.template.line"].search(
                 [("product_id", "=", record.product_variant_id.id)]
diff --git a/energy_communities_service_invoicing/models/res_partner.py b/energy_communities_service_invoicing/models/res_partner.py
index 02db8bb7b..f71e7fe79 100644
--- a/energy_communities_service_invoicing/models/res_partner.py
+++ b/energy_communities_service_invoicing/models/res_partner.py
@@ -9,39 +9,39 @@ class ResPartner(models.Model):
     _name = "res.partner"
     _inherit = ["res.partner"]
 
-    service_pack_id = fields.Many2one(
+    platform_pack_id = fields.Many2one(
         "product.product",
-        string="Service Pack",
-        compute="_compute_service_pack_id",
+        string="Platform Service Pack",
+        compute="_compute_platform_pack_id",
         store=False,
     )
-    pack_contract_status = fields.Selection(
+    platform_pack_contract_status = fields.Selection(
         selection=_PACK_CONTRACT_STATUS_VALUES,
-        string="Service Pack Status",
-        compute="_compute_service_pack_status",
+        string="Platform Service Pack Status",
+        compute="_compute_platform_pack_status",
         store=False,
     )
 
-    def _compute_service_pack_status(self):
+    def _compute_platform_pack_status(self):
         for record in self:
-            record.pack_contract_status = "none"
-            rel_contract = record._get_related_service_contract()
+            record.platform_pack_contract_status = "none"
+            rel_contract = record._get_related_platform_pack_contract()
             if rel_contract:
-                record.pack_contract_status = rel_contract.status
+                record.platform_pack_contract_status = rel_contract.status
 
-    def _compute_service_pack_id(self):
+    def _compute_platform_pack_id(self):
         for record in self:
-            record.service_pack_id = False
-            rel_contract = record._get_related_service_contract()
+            record.platform_pack_id = False
+            rel_contract = record._get_related_platform_pack_contract()
             if rel_contract:
-                if rel_contract.service_pack_id:
-                    record.service_pack_id = rel_contract.service_pack_id.id
+                if rel_contract.pack_id:
+                    record.platform_pack_id = rel_contract.pack_id.id
 
-    def _get_related_service_contract(self):
+    def _get_related_platform_pack_contract(self):
         return self.env["contract.contract"].search(
             [
                 ("community_company_id", "=", self.related_company_id.id),
-                ("is_pack", "=", True),
+                ("pack_type", "=", "platform_pack"),
             ],
             limit=1,
         )
diff --git a/energy_communities_service_invoicing/report/report_invoice.xml b/energy_communities_service_invoicing/report/report_invoice.xml
index aed13d4d3..e1ce48ee5 100644
--- a/energy_communities_service_invoicing/report/report_invoice.xml
+++ b/energy_communities_service_invoicing/report/report_invoice.xml
@@ -1,7 +1,7 @@
 <odoo>
   <template id="report_invoice_document" inherit_id="account.report_invoice_document">
     <xpath expr="//div[@id='informations']" position="inside">
-      <div class="col-auto col-3 mw-100 mb-2" t-if="o.is_pack">
+      <div class="col-auto col-3 mw-100 mb-2" t-if="o.pack_type == 'platform_pack'">
         <strong>Community:</strong>
         <t t-if="o.related_community_company_id.comercial_name">
           <p class="m-0" t-field="o.related_community_company_id.comercial_name"/>
@@ -13,19 +13,4 @@
     </xpath>
   </template>
 </odoo>
-<!--<xpath expr="//table" position="before">-->
-<!--  <div class="row">-->
-<!--    <div class="col-6" name="related_community_company_id">-->
-<!--      <label for="related_community_company_id">Related community company</label>-->
-<!--      <address-->
-<!--        class="mb-0"-->
-<!--        t-field="o.related_community_company_id.partner_id"-->
-<!--        t-options="{'widget': 'contact', 'fields': ['address', 'name'], 'no_marker': True}"-->
-<!--      />-->
-<!--      <div t-if="o.related_community_company_id.partner_id.vat">-->
-<!--        Tax ID: <span t-field="o.related_community_company_id.partner_id.vat"/>-->
-<!--      </div>-->
-<!--    </div>-->
-<!--  </div>-->
-<!--</xpath>-->
 
diff --git a/energy_communities_service_invoicing/utils.py b/energy_communities_service_invoicing/utils.py
index 18562a076..2f63628b5 100644
--- a/energy_communities_service_invoicing/utils.py
+++ b/energy_communities_service_invoicing/utils.py
@@ -68,7 +68,7 @@ def service_invoicing_form_view_for_platform_admins(
 
 
 # TODO: Think a bit more about more about if this 3 methods must go to contract utils component
-def raise_existing_same_open_pack_contract_error(existing_contract):
+def raise_existing_same_open_platform_pack_contract_error(existing_contract):
     raise ValidationError(
         _(
             "It already exists an open contract ({}) with same company and community."
@@ -77,26 +77,26 @@ def raise_existing_same_open_pack_contract_error(existing_contract):
 
 
 def get_existing_open_pack_contract(
-    env, partner_id, community_company_id, contract_id=False
+    env, partner_id, pack_type, contract_id=False, custom_query=[]
 ):
+    #("community_company_id", "=", community_company_id.id),
     query = [
-        ("partner_id", "=", partner_id.id),
-        ("community_company_id", "=", community_company_id.id),
-        ("is_pack", "=", True),
+        ("partner_id", "=", partner_id.id), 
+        ("pack_type", "=", pack_type),
         ("status", "in", ["paused", "in_progress"]),
     ]
     if contract_id:
         query.append(("id", "!=", contract_id.id))
+    query = custom_query + query
     return env["contract.contract"].search(query, limit=1)
 
-
 def get_existing_last_closed_pack_contract(
     env, partner_id, community_company_id, contract_id=False
 ):
     query = [
         ("partner_id", "=", partner_id.id),
         ("community_company_id", "=", community_company_id.id),
-        ("is_pack", "=", True),
+        ("pack_type", "=", "platform_pack"),
         ("status", "in", ["closed_planned", "closed"]),
         ("successor_contract_id", "=", False),
     ]
diff --git a/energy_communities_service_invoicing/views/account_move_views.xml b/energy_communities_service_invoicing/views/account_move_views.xml
index 272df95a6..8778184dd 100644
--- a/energy_communities_service_invoicing/views/account_move_views.xml
+++ b/energy_communities_service_invoicing/views/account_move_views.xml
@@ -5,11 +5,11 @@
       <field name="inherit_id" ref="account.view_move_form" />
       <field name="arch" type="xml">
         <xpath expr="//field[@name='partner_id']" position="after">
-          <field name="is_pack" invisible="True" />
+          <field name="pack_type" invisible="True" />
           <field name="is_contract" invisible="True" />
           <field name="invoice_origin" attrs="{'invisible': [('is_contract','=',False)]}" />
           <field name="related_contract_id" attrs="{'invisible': [('is_contract','=',False)]}" />
-          <field name="related_community_company_id" attrs="{'invisible': [('is_pack','=',False)]}" />
+          <field name="related_community_company_id" attrs="{'invisible': [('pack_type' ,'=', 'platform_pack')]}" />
         </xpath>
       </field>
   </record>
diff --git a/energy_communities_service_invoicing/views/contract_views.xml b/energy_communities_service_invoicing/views/contract_views.xml
index b19994cca..e0f63f4e5 100644
--- a/energy_communities_service_invoicing/views/contract_views.xml
+++ b/energy_communities_service_invoicing/views/contract_views.xml
@@ -47,9 +47,9 @@
           decoration-success="status == 'in_progress'"
         />
         <field name="partner_id" />
-        <field name="community_company_id" />
-        <field name="service_pack_id" />
-        <field name="is_pack" />
+        <field name="community_company_id" attrs="{'invisible': [('pack_type', '!=', 'platform_pack')]}" />
+        <field name="pack_id" />
+        <field name="pack_type" />
       </tree>
     </field>
   </record>
@@ -92,7 +92,7 @@
         <field name="community_company_id" />
       </xpath>
       <xpath expr="//field[@name='pricelist_id']" position="after">
-        <field name="service_pack_id" />
+        <field name="pack_id" />
       </xpath>
       <xpath expr="//field[@name='user_id']" position="after">
         <field name="date_start" />
@@ -135,7 +135,7 @@
           <!--</div>-->
           <group>
             <field name="name" />
-            <field name="service_pack_id" options="{'no_open': True}" />
+            <field name="pack_id" options="{'no_open': True}" />
             <field name="related_contract_product_ids" widget="one2many" >
               <tree editable="bottom">
                 <field name="name" readonly="1"/>
diff --git a/energy_communities_service_invoicing/views/res_partner_views.xml b/energy_communities_service_invoicing/views/res_partner_views.xml
index 87850b0f4..305418539 100644
--- a/energy_communities_service_invoicing/views/res_partner_views.xml
+++ b/energy_communities_service_invoicing/views/res_partner_views.xml
@@ -6,8 +6,8 @@
     <field name="arch" type="xml">
       <xpath expr="//field[@name='vat']" position="after">
         <field name="company_hierarchy_level" invisible="1" />
-        <field name="service_pack_id" attrs="{'invisible': [('company_hierarchy_level', '!=', 'community')]}" options="{'no_open': True}" />
-        <field name="pack_contract_status" attrs="{'invisible': [('company_hierarchy_level', '!=', 'community')]}" />
+        <field name="platform_pack_id" attrs="{'invisible': [('company_hierarchy_level', '!=', 'community')]}" options="{'no_open': True}" />
+        <field name="platform_pack_contract_status" attrs="{'invisible': [('company_hierarchy_level', '!=', 'community')]}" />
       </xpath>
     </field>
   </record>
diff --git a/energy_communities_service_invoicing/views/service_invoicing_views.xml b/energy_communities_service_invoicing/views/service_invoicing_views.xml
index 683cdfda0..52ce9ab42 100644
--- a/energy_communities_service_invoicing/views/service_invoicing_views.xml
+++ b/energy_communities_service_invoicing/views/service_invoicing_views.xml
@@ -28,7 +28,7 @@
     <field name="name">Packs</field>
     <field name="res_model">product.template</field>
     <field name="view_mode">tree,form</field>
-    <field name="domain" eval="[('categ_id','=',ref('product_category_pack'))]" />
+    <field name="domain" eval="[('categ_id','=',ref('product_category_platform_pack'))]" />
   </record>
 
   <record
@@ -38,7 +38,7 @@
     <field name="name">Services</field>
     <field name="res_model">product.template</field>
     <field name="view_mode">tree,form</field>
-    <field name="domain" eval="[('categ_id','=',ref('product_category_service'))]" />
+    <field name="domain" eval="[('categ_id','=',ref('product_category_platform_service'))]" />
   </record>
 
   <record
@@ -48,7 +48,7 @@
     <field name="name">Packs contract templates</field>
     <field name="res_model">contract.template</field>
     <field name="view_mode">tree,form</field>
-    <field name="domain">[('is_pack','=',True)]</field>
+    <field name="domain">[(pack_type, '=', 'platform_pack')]</field>
   </record>
 
   <record
@@ -58,7 +58,7 @@
     <field name="name">Service invoices (issued)</field>
     <field name="res_model">account.move</field>
     <field name="view_mode">tree,form</field>
-    <field name="domain" eval="[('move_type', 'in', ['out_invoice', 'out_refund']),('is_pack','=',True)]"/>
+    <field name="domain" eval="[('move_type', 'in', ['out_invoice', 'out_refund']), ('pack_type', '=', 'platform_pack')]"/>
   </record>
 
   <record
@@ -68,7 +68,7 @@
     <field name="name">Service invoices (received)</field>
     <field name="res_model">account.move</field>
     <field name="view_mode">tree,form</field>
-    <field name="domain" eval="[('move_type', 'in', ['in_invoice', 'in_refund']),('is_pack','=',True)]"/>
+    <field name="domain" eval="[('move_type', 'in', ['in_invoice', 'in_refund']), ('pack_type', '=', 'platform_pack')]"/>
   </record>
 
   <record
diff --git a/energy_communities_service_invoicing/wizards/service_invoicing_action.py b/energy_communities_service_invoicing/wizards/service_invoicing_action.py
index 59a6b2d9f..a2778ad12 100644
--- a/energy_communities_service_invoicing/wizards/service_invoicing_action.py
+++ b/energy_communities_service_invoicing/wizards/service_invoicing_action.py
@@ -22,9 +22,10 @@ class ServiceInvoicingActionWizard(models.TransientModel):
         selection=_SERVICE_INVOICING_EXECUTED_ACTION_VALUES
     )
     pricelist_id = fields.Many2one("product.pricelist", string="Select pricelist")
-    service_pack_id = fields.Many2one("product.product", string="Service pack")
+    pack_id = fields.Many2one("product.product", string="Pack")
     discount = fields.Float(string="Discount (%)", digits="Discount")
     payment_mode_id = fields.Many2one("account.payment.mode", string="Payment mode")
+    pack_type = fields.Selection(related="service_invoicing_id.pack_type")
 
     def execute_activate(self):
         with contract_utils(self.env, self.service_invoicing_id) as component:
@@ -42,7 +43,7 @@ class ServiceInvoicingActionWizard(models.TransientModel):
                 self.execution_date,
                 executed_modification_action,
                 self.pricelist_id,
-                self.service_pack_id,
+                self.pack_id,
                 self.discount,
                 self.payment_mode_id,
             )
@@ -55,7 +56,7 @@ class ServiceInvoicingActionWizard(models.TransientModel):
             service_invoicing_id = component.reopen(
                 self.execution_date,
                 self.pricelist_id,
-                self.service_pack_id,
+                self.pack_id,
                 self.discount,
                 self.payment_mode_id,
             )
@@ -66,7 +67,7 @@ class ServiceInvoicingActionWizard(models.TransientModel):
     def _validate_execute_modify(self):
         if (
             not self.pricelist_id
-            and not self.service_pack_id
+            and not self.pack_id
             and not self.payment_mode_id
             and self.discount == self.service_invoicing_id.discount
         ):
@@ -76,10 +77,10 @@ class ServiceInvoicingActionWizard(models.TransientModel):
         executed_modification_action = ""
         if self.pricelist_id:
             executed_modification_action += "modify_pricelist"
-        if self.service_pack_id:
+        if self.pack_id:
             if bool(executed_modification_action):
                 executed_modification_action += ","
-            executed_modification_action += "modify_service_pack"
+            executed_modification_action += "modify_pack"
         if self.payment_mode_id:
             if bool(executed_modification_action):
                 executed_modification_action += ","
diff --git a/energy_communities_service_invoicing/wizards/service_invoicing_action.xml b/energy_communities_service_invoicing/wizards/service_invoicing_action.xml
index f66cf95e0..f5585a315 100644
--- a/energy_communities_service_invoicing/wizards/service_invoicing_action.xml
+++ b/energy_communities_service_invoicing/wizards/service_invoicing_action.xml
@@ -10,6 +10,7 @@
         <sheet>
           <group>
             <field name="executed_action" invisible="1" />
+            <field name="pack_type" invisible="1" />
             <field
               name="service_invoicing_id"
               required="1"
@@ -19,8 +20,8 @@
             />
             <field name="execution_date" required="1"/>
             <field
-              name="service_pack_id"
-              domain="[('is_contract','=',True)]"
+              name="pack_id"
+              domain="[('pack_type','=', pack_type)]"
               attrs="{
                 'invisible': [('executed_action','not in',['modification','reopen'])],
                 'required': [('executed_action','=','reopen')]
diff --git a/energy_communities_service_invoicing/wizards/service_invoicing_action_create.py b/energy_communities_service_invoicing/wizards/service_invoicing_action_create.py
index e36abc4bb..1258fdd77 100644
--- a/energy_communities_service_invoicing/wizards/service_invoicing_action_create.py
+++ b/energy_communities_service_invoicing/wizards/service_invoicing_action_create.py
@@ -10,7 +10,7 @@ from odoo.addons.energy_communities.utils import (
 from ..utils import (
     get_existing_last_closed_pack_contract,
     get_existing_open_pack_contract,
-    raise_existing_same_open_pack_contract_error,
+    raise_existing_same_open_platform_pack_contract_error,
     service_invoicing_form_view_for_platform_admins,
     service_invoicing_tree_view,
 )
@@ -34,9 +34,9 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
     community_company_mids = fields.Many2many(
         comodel_name="res.company",
     )
-    service_pack_id = fields.Many2one(
+    platform_pack_id = fields.Many2one(
         "product.product",
-        string="Service pack",
+        string="Platform service pack",
     )
     payment_mode_id = fields.Many2one(
         "account.payment.mode",
@@ -48,22 +48,22 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
 
     allowed_community_company_ids = fields.Many2many(
         comodel_name="res.company",
-        _compute="_compute_allowed_community_company_ids",
+        compute="_compute_allowed_community_company_ids",
         store=False,
     )
     allowed_payment_mode_ids = fields.Many2many(
         comodel_name="account.payment.mode",
-        _compute="_compute_allowed_payment_mode_ids",
+        compute="_compute_allowed_payment_mode_ids",
         store=False,
     )
-    pack_product_categ_id = fields.Many2one(
-        "product.category", compute="_compute_pack_product_categ_id", store=False
+    platform_pack_product_categ_id = fields.Many2one(
+        "product.category", compute="_compute_platform_pack_product_categ_id", store=False
     )
 
-    def _compute_pack_product_categ_id(self):
+    def _compute_platform_pack_product_categ_id(self):
         for record in self:
-            record.pack_product_categ_id = self.env.ref(
-                "energy_communities_service_invoicing.product_category_pack"
+            record.platform_pack_product_categ_id = self.env.ref(
+                "energy_communities_service_invoicing.product_category_platform_pack"
             ).id
 
     @api.depends("company_id", "community_company_mids")
@@ -119,7 +119,7 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
                 service_invoicing_id = component.reopen(
                     self.execution_date,
                     self.pricelist_id,
-                    self.service_pack_id,
+                    self.platform_pack_id,
                     self.discount,
                     payment_mode_id,
                 )
@@ -129,7 +129,7 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
                 service_invoicing_id = component.create_service_invoicing_initial(
                     company_id,
                     community_company_id,
-                    self.service_pack_id,
+                    self.platform_pack_id,
                     self.pricelist_id,
                     self.execution_date,
                     self.discount,
@@ -182,10 +182,11 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
         # Check if already open one and raise error
         for record in impacted_records:
             existing_contract = get_existing_open_pack_contract(
-                self.env, record.parent_id.partner_id, record
+                self.env, record.parent_id.partner_id, "platform_pack", contract_id=False, custom_query=[("community_company_id", "=", record.id)]
             )
+
             if existing_contract:
-                raise_existing_same_open_pack_contract_error(existing_contract)
+                raise_existing_same_open_platform_pack_contract_error(existing_contract)
 
     def _validate_service_invoicing_action_create_multicommunity(self, company_id_list):
         impacted_records = self.env["res.company"].browse(company_id_list)
diff --git a/energy_communities_service_invoicing/wizards/service_invoicing_action_create.xml b/energy_communities_service_invoicing/wizards/service_invoicing_action_create.xml
index 309c02e08..c52f35648 100644
--- a/energy_communities_service_invoicing/wizards/service_invoicing_action_create.xml
+++ b/energy_communities_service_invoicing/wizards/service_invoicing_action_create.xml
@@ -12,7 +12,7 @@
             <field name="creation_type" invisible="1" />
             <field name="allowed_community_company_ids" invisible="1" />
             <field name="allowed_payment_mode_ids" invisible="1" />
-            <field name="pack_product_categ_id" invisible="1" />
+            <field name="platform_pack_product_categ_id" invisible="1" />
             <field name="execution_date" required="1"/>
             <field
               name="company_id"
@@ -33,8 +33,8 @@
                 <field name="parent_id"/>
              </tree>
             </field>
-            <field name="service_pack_id"
-              domain="[('categ_id','=',pack_product_categ_id)]"
+            <field name="platform_pack_id"
+              domain="[('categ_id','=', platform_pack_product_categ_id)]"
               required="1"
             />
             <field name="pricelist_id" required="1" domain="[('company_id','=',False)]"/>
diff --git a/energy_selfconsumption/__manifest__.py b/energy_selfconsumption/__manifest__.py
index 94378c23b..965109dc6 100644
--- a/energy_selfconsumption/__manifest__.py
+++ b/energy_selfconsumption/__manifest__.py
@@ -26,6 +26,7 @@
         "web_m2x_options",
         "l10n_es",
         "report_csv",
+        "energy_communities_service_invoicing",
     ],
     "external_dependencies": {
         "python": ["pandas>=2.0.3", "numpy>=1.24.4", "openupgradelib>=3.6.1"]
@@ -41,6 +42,7 @@
         "data/mail_template.xml",
         "data/ir_attachment_data.xml",
         "data/ir_cron.xml",
+        "data/product_data.xml",
         "views/contract_views.xml",
         "views/selfconsumption_views.xml",
         "views/supply_point_views.xml",
diff --git a/energy_selfconsumption/data/product_data.xml b/energy_selfconsumption/data/product_data.xml
new file mode 100644
index 000000000..3350c52e3
--- /dev/null
+++ b/energy_selfconsumption/data/product_data.xml
@@ -0,0 +1,9 @@
+<odoo>
+  <record id="product_category_selfconsumption_pack" model="product.category">
+    <field name="name">Selfconsumption Pack</field>
+  </record>
+  <record id="product_category_selfconsumption_service" model="product.category">
+    <field name="name">Selfconsumption Service</field>
+  </record>
+</odoo>
+
diff --git a/energy_selfconsumption/models/__init__.py b/energy_selfconsumption/models/__init__.py
index 3a53b3984..17238e412 100644
--- a/energy_selfconsumption/models/__init__.py
+++ b/energy_selfconsumption/models/__init__.py
@@ -14,3 +14,4 @@ from . import project
 from . import selfconsumption
 from . import supply_point
 from . import supply_point_assignation
+from . import contract_template
diff --git a/energy_selfconsumption/models/contract_template.py b/energy_selfconsumption/models/contract_template.py
new file mode 100644
index 000000000..bf4933d03
--- /dev/null
+++ b/energy_selfconsumption/models/contract_template.py
@@ -0,0 +1,15 @@
+from odoo import api, fields, models, _
+
+PACK_VALUES = [
+    ("selfconsumption_pack", _("Selfconsumption Pack")),
+]
+
+class ContractTemplate(models.Model):
+    _inherit = "contract.template"
+
+    pack_type = fields.Selection(selection_add=PACK_VALUES)
+
+    def custom_compute_pack_type(self):
+        super().custom_compute_pack_type()
+        if self.pack_type == 'none':
+            self.set_custom_pack_type("selfconsumption.product_category_selfconsumption_pack", 'selfconsumption_pack')
-- 
GitLab