From 9ee9745d72ffadd7ab27af59e1891d3d8229608b Mon Sep 17 00:00:00 2001
From: daniquilez <dani.quilez@gmail.com>
Date: Tue, 18 Mar 2025 15:50:15 +0100
Subject: [PATCH 1/3] =?UTF-8?q?[REL]=20=F0=9F=8F=97=EF=B8=8F=20Bump=20vers?=
 =?UTF-8?q?ions?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* energy_communities to 16.0.0.2.2
* energy_communities_cooperator to 16.0.0.1.9
* energy_communities_crm to 16.0.0.1.5
* energy_communities_service_invoicing to 16.0.0.1.2
* energy_communities_selfconsumption to 16.0.0.2.1
---
 energy_communities/__manifest__.py                   | 2 +-
 energy_communities_cooperator/__manifest__.py        | 2 +-
 energy_communities_crm/__manifest__.py               | 2 +-
 energy_communities_service_invoicing/__manifest__.py | 2 +-
 energy_selfconsumption/__manifest__.py               | 2 +-
 setup/energy_communities_crm/setup.py                | 1 +
 6 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/energy_communities/__manifest__.py b/energy_communities/__manifest__.py
index 18c1e21dc..118b55ed8 100644
--- a/energy_communities/__manifest__.py
+++ b/energy_communities/__manifest__.py
@@ -1,6 +1,6 @@
 {
     "name": "Energy Community",
-    "version": "16.0.0.2.1",
+    "version": "16.0.0.2.2",
     "depends": [
         "account",
         "account_banking_mandate",
diff --git a/energy_communities_cooperator/__manifest__.py b/energy_communities_cooperator/__manifest__.py
index b6b2a5f58..35367bca1 100644
--- a/energy_communities_cooperator/__manifest__.py
+++ b/energy_communities_cooperator/__manifest__.py
@@ -12,7 +12,7 @@
     # Check https://github.com/odoo/odoo/blob/14.0/odoo/addons/base/data/ir_module_category_data.xml
     # for the full list
     "category": "Cooperative management",
-    "version": "16.0.0.1.8",
+    "version": "16.0.0.1.9",
     "license": "AGPL-3",
     # any module necessary for this one to work correctly
     "depends": [
diff --git a/energy_communities_crm/__manifest__.py b/energy_communities_crm/__manifest__.py
index 1dee8ef87..57454cacb 100644
--- a/energy_communities_crm/__manifest__.py
+++ b/energy_communities_crm/__manifest__.py
@@ -12,7 +12,7 @@
     # Check https://github.com/odoo/odoo/blob/14.0/odoo/addons/base/data/ir_module_category_data.xml
     # for the full list
     "category": "Sales/CRM",
-    "version": "16.0.0.1.4",
+    "version": "16.0.0.1.5",
     "license": "AGPL-3",
     # any module necessary for this one to work correctly
     "depends": [
diff --git a/energy_communities_service_invoicing/__manifest__.py b/energy_communities_service_invoicing/__manifest__.py
index 6cb1f667b..0133e4e50 100644
--- a/energy_communities_service_invoicing/__manifest__.py
+++ b/energy_communities_service_invoicing/__manifest__.py
@@ -9,7 +9,7 @@
     "author": "Som comunitats",
     "website": "https://coopdevs.org",
     "category": "Contract Management",
-    "version": "16.0.0.1.1",
+    "version": "16.0.0.1.2",
     # any module necessary for this one to work correctly
     "depends": [
         "base",
diff --git a/energy_selfconsumption/__manifest__.py b/energy_selfconsumption/__manifest__.py
index c5b550637..39544c56c 100644
--- a/energy_selfconsumption/__manifest__.py
+++ b/energy_selfconsumption/__manifest__.py
@@ -9,7 +9,7 @@
     "author": "Coopdevs Treball SCCL & Som Energia SCCL",
     "website": "https://coopdevs.org",
     "category": "Customizations",
-    "version": "16.0.0.2.0",
+    "version": "16.0.0.2.1",
     "license": "AGPL-3",
     "depends": [
         "base",
diff --git a/setup/energy_communities_crm/setup.py b/setup/energy_communities_crm/setup.py
index 28c57bb64..27d506ebf 100644
--- a/setup/energy_communities_crm/setup.py
+++ b/setup/energy_communities_crm/setup.py
@@ -1,6 +1,7 @@
 import setuptools
 
 setuptools.setup(
+    name="odoo_addon_energy_communities_crm",
     setup_requires=['setuptools-odoo'],
     odoo_addon=True,
 )
-- 
GitLab


From 100f602b6db5449bfe00cdae2c7749e18c775951 Mon Sep 17 00:00:00 2001
From: daniquilez <dani.quilez@gmail.com>
Date: Tue, 18 Mar 2025 18:21:35 +0100
Subject: [PATCH 2/3] =?UTF-8?q?[REF]=20=E2=99=BB=EF=B8=8F=20Make=20sale=20?=
 =?UTF-8?q?order=20utils=20more=20flexible.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 energy_communities/utils.py                   |  4 +-
 .../components/sale_order_utils.py            | 42 ++++++++++---------
 .../models/sale_order.py                      | 30 ++++++++-----
 3 files changed, 46 insertions(+), 30 deletions(-)

diff --git a/energy_communities/utils.py b/energy_communities/utils.py
index f430ddd23..fda66fed4 100644
--- a/energy_communities/utils.py
+++ b/energy_communities/utils.py
@@ -6,6 +6,7 @@ from odoo.tools.translate import code_translations
 
 from odoo.addons.component.core import Component, WorkContext
 from odoo.addons.contract.models.contract import ContractContract
+from odoo.addons.sale.models.sale_order import SaleOrder
 
 
 def _get_component(
@@ -34,8 +35,9 @@ def contract_utils(
 @contextmanager
 def sale_order_utils(
     env: Environment,
+    sale_order_id: SaleOrder = None,
 ) -> Component:
-    yield _get_component(env, "sale.order", "sale.order.utils")
+    yield _get_component(env, "sale.order", "sale.order.utils", sale_order_id)
 
 
 def get_translation(source, lang, mods):
diff --git a/energy_communities_service_invoicing/components/sale_order_utils.py b/energy_communities_service_invoicing/components/sale_order_utils.py
index 460a084b1..5487a5cf3 100644
--- a/energy_communities_service_invoicing/components/sale_order_utils.py
+++ b/energy_communities_service_invoicing/components/sale_order_utils.py
@@ -1,11 +1,12 @@
+from odoo.exceptions import MissingError
+
 from odoo.addons.component.core import Component
-from odoo.addons.energy_communities.utils import contract_utils
 
 
 class SaleOrderUtils(Component):
     _inherit = "sale.order.utils"
 
-    def _create_service_invoicing_sale_order(
+    def create_service_invoicing_sale_order(
         self,
         company_id,
         pack_id,
@@ -53,7 +54,7 @@ class SaleOrderUtils(Component):
         # Trigger name computattion in oder to include product's description_sale
         for order_line in sale_order.order_line:
             order_line._compute_name()
-        return sale_order
+        self.work.record = sale_order
 
     def create_service_invoicing_initial(
         self,
@@ -66,7 +67,7 @@ class SaleOrderUtils(Component):
         payment_mode_id=False,
         metadata=False,
     ):
-        so = self._create_service_invoicing_sale_order(
+        self.create_service_invoicing_sale_order(
             company_id,
             pack_id,
             pricelist_id,
@@ -76,19 +77,22 @@ class SaleOrderUtils(Component):
             executed_action_description,
             metadata,
         )
-        so.action_confirm()
-        service_invoicing_id = self._get_related_contracts(so)
-        # TODO: We must call contract_utils with a better component and workcontext modification approach
-        with contract_utils(self.env, service_invoicing_id) as component:
-            component.setup_initial_data()
-            component.clean_non_service_lines()
-            if service_invoicing_id.is_free_pack:
-                component.set_contract_status_active(start_date)
-        return service_invoicing_id
+        contract_utils = self.confirm()
+        # TODO: Decide if this must be by design
+        if contract_utils.work.record.is_free_pack:
+            contract_utils.set_contract_status_active(start_date)
+        return contract_utils.work.record
 
-    def _get_related_contracts(self, sale_order):
-        return (
-            self.env["contract.line"]
-            .search([("sale_order_line_id", "in", sale_order.order_line.ids)])
-            .mapped("contract_id")
-        )
+    def confirm(self):
+        if not self.work.record:
+            raise MissingError(
+                _("Sale order must be defined in order to confirm it on component")
+            )
+        self.work.record.action_confirm()
+        with self.collection.work_on(
+            "contract.contract", record=self.work.record.service_invoicing_id
+        ) as work:
+            contract_utils = work.component("contract.utils")
+            contract_utils.setup_initial_data()
+            contract_utils.clean_non_service_lines()
+            return contract_utils
diff --git a/energy_communities_service_invoicing/models/sale_order.py b/energy_communities_service_invoicing/models/sale_order.py
index 67447ae36..8d3690522 100644
--- a/energy_communities_service_invoicing/models/sale_order.py
+++ b/energy_communities_service_invoicing/models/sale_order.py
@@ -17,6 +17,21 @@ class SaleOrder(models.Model):
         string="Service invoicing action description",
         default="none",
     )
+    service_invoicing_id = fields.Many2one(
+        "contract.contract",
+        string="Related contract",
+        compute="_compute_service_invoicing_id",
+        store=False,
+    )
+
+    def _compute_service_invoicing_id(self):
+        for record in self:
+            record.service_invoicing_id = False
+            contract = self.env["contract.contract"].search(
+                [("sale_order_id", "=", self.id)], limit=1
+            )
+            if contract:
+                record.service_invoicing_id = contract.id
 
     def action_create_contract(self):
         contracts = super().action_create_contract()
@@ -32,18 +47,13 @@ class SaleOrder(models.Model):
 
     def action_show_contracts(self):
         self.ensure_one()
-        action = self.env["ir.actions.act_window"]._for_xml_id(
-            "contract.action_customer_contract"
-        )
-
-        contracts = self.env["contract.contract"].search(
-            [("sale_order_id", "=", self.id)]
-        )
-        if len(contracts) == 1:
-            # If there is only one contract, open it directly
+        if self.service_invoicing_id:
+            action = self.env["ir.actions.act_window"]._for_xml_id(
+                "contract.action_customer_contract"
+            )
             action.update(
                 {
-                    "res_id": contracts.id,
+                    "res_id": self.service_invoicing_id.id,
                     "view_mode": "form",
                     "views": filter(lambda view: view[1] == "form", action["views"]),
                 }
-- 
GitLab


From 2b0b6d4b26e87f21b2745c1d6ba327258aa6a2d3 Mon Sep 17 00:00:00 2001
From: daniquilez <dani.quilez@gmail.com>
Date: Wed, 19 Mar 2025 10:10:29 +0100
Subject: [PATCH 3/3] =?UTF-8?q?[REF]=20=E2=99=BB=EF=B8=8F=20Service=20invo?=
 =?UTF-8?q?icing=20components=20work=20with=20partner=5Fid=20instead=20com?=
 =?UTF-8?q?pany=5Fid?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../components/contract_utils.py              |  2 +-
 .../components/sale_order_utils.py            | 21 ++++++++++---------
 .../service_invoicing_action_create.py        |  2 +-
 3 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/energy_communities_service_invoicing/components/contract_utils.py b/energy_communities_service_invoicing/components/contract_utils.py
index 181c63508..cc5e994ba 100644
--- a/energy_communities_service_invoicing/components/contract_utils.py
+++ b/energy_communities_service_invoicing/components/contract_utils.py
@@ -154,7 +154,7 @@ class ContractUtils(Component):
     ):
         executed_action_description_list = executed_action_description.split(",")
         return {
-            "company_id": self.work.record.partner_id.related_company_id,
+            "partner_id": self.work.record.partner_id,
             "pack_id": pack_id
             if "modify_pack" in executed_action_description_list
             else self.work.record.pack_id,
diff --git a/energy_communities_service_invoicing/components/sale_order_utils.py b/energy_communities_service_invoicing/components/sale_order_utils.py
index 5487a5cf3..49be821ce 100644
--- a/energy_communities_service_invoicing/components/sale_order_utils.py
+++ b/energy_communities_service_invoicing/components/sale_order_utils.py
@@ -8,7 +8,7 @@ class SaleOrderUtils(Component):
 
     def create_service_invoicing_sale_order(
         self,
-        company_id,
+        partner_id,
         pack_id,
         pricelist_id,
         payment_mode_id,
@@ -18,7 +18,7 @@ class SaleOrderUtils(Component):
         metadata,
     ):
         so_creation_dict = {
-            "partner_id": company_id.partner_id.id,
+            "partner_id": partner_id.id,
             "company_id": self.env.company.id,
             "commitment_date": start_date,
             "pricelist_id": pricelist_id.id,
@@ -58,7 +58,7 @@ class SaleOrderUtils(Component):
 
     def create_service_invoicing_initial(
         self,
-        company_id,
+        partner_id,
         pack_id,
         pricelist_id,
         start_date,
@@ -68,7 +68,7 @@ class SaleOrderUtils(Component):
         metadata=False,
     ):
         self.create_service_invoicing_sale_order(
-            company_id,
+            partner_id,
             pack_id,
             pricelist_id,
             payment_mode_id,
@@ -77,11 +77,7 @@ class SaleOrderUtils(Component):
             executed_action_description,
             metadata,
         )
-        contract_utils = self.confirm()
-        # TODO: Decide if this must be by design
-        if contract_utils.work.record.is_free_pack:
-            contract_utils.set_contract_status_active(start_date)
-        return contract_utils.work.record
+        return self.confirm()
 
     def confirm(self):
         if not self.work.record:
@@ -95,4 +91,9 @@ class SaleOrderUtils(Component):
             contract_utils = work.component("contract.utils")
             contract_utils.setup_initial_data()
             contract_utils.clean_non_service_lines()
-            return contract_utils
+            # TODO: Decide if this must be by design
+            if contract_utils.work.record.is_free_pack:
+                contract_utils.set_contract_status_active(
+                    self.work.record.commitment_date
+                )
+            return contract_utils.work.record
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 d0c803047..50c9be490 100644
--- a/energy_communities_service_invoicing/wizards/service_invoicing_action_create.py
+++ b/energy_communities_service_invoicing/wizards/service_invoicing_action_create.py
@@ -130,7 +130,7 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
             with sale_order_utils(self.env) as component:
                 # TODO: pass community_company_id as metadata
                 service_invoicing_id = component.create_service_invoicing_initial(
-                    company_id,
+                    company_id.partner_id,
                     self.platform_pack_id,
                     self.pricelist_id,
                     self.execution_date,
-- 
GitLab