From a38a87fa3c462a3ea9080828fcdef61d82b1879b Mon Sep 17 00:00:00 2001
From: daniquilez <dani.quilez@gmail.com>
Date: Thu, 27 Feb 2025 10:36:17 +0100
Subject: [PATCH] =?UTF-8?q?[IMP]=20=E2=9C=A8=20get=5Fsuccessful=5Fpopup=5F?=
 =?UTF-8?q?message=20utils=20method=20and=20introduce=20execution=5Fdate?=
 =?UTF-8?q?=20on=20contract=20creation?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 energy_communities/models/landing_page.py     | 17 +++-------
 energy_communities/utils.py                   | 13 ++++++++
 .../wizards/change_coordinator_wizard.py      | 17 ++++------
 .../wizards/create_users_wizard.py            | 15 +++------
 .../wizards/multicompany_easy_creation.py     | 16 +++-------
 .../models/voluntary_share_interest_return.py | 14 ++-------
 energy_communities_service_invoicing/utils.py | 31 +++++++++++++------
 .../service_invoicing_action_create.py        | 16 +++++-----
 .../service_invoicing_action_create.xml       |  1 +
 9 files changed, 68 insertions(+), 72 deletions(-)

diff --git a/energy_communities/models/landing_page.py b/energy_communities/models/landing_page.py
index 74a01ebba..784428d8c 100644
--- a/energy_communities/models/landing_page.py
+++ b/energy_communities/models/landing_page.py
@@ -12,6 +12,7 @@ from ..pywordpress_client.resources.authenticate import Authenticate
 from ..pywordpress_client.resources.landing_page import (
     LandingPage as LandingPageResource,
 )
+from ..utils import get_successful_popup_message
 from .res_company import _CE_MEMBER_STATUS_VALUES, _CE_TYPE, _LEGAL_FORM_VALUES
 
 
@@ -270,18 +271,10 @@ class LandingPage(models.Model):
                 else:
                     self.sudo().remove_coordinator_filter_to_existing_communities()
             self.write({"publicdata_lastupdate_datetime": datetime.now()})
-            return {
-                "type": "ir.actions.client",
-                "tag": "display_notification",
-                "params": {
-                    "type": "success",
-                    "title": _("Public data update successful"),
-                    "message": _(
-                        "Wordpress landing and map place has been successfully updated."
-                    ),
-                    "sticky": False,
-                },
-            }
+            return get_successful_popup_message(
+                _("Public data update successful"),
+                _("Wordpress landing and map place has been successfully updated."),
+            )
 
     def _update_wordpress(self):
         instance_company = self.env["res.company"].search(
diff --git a/energy_communities/utils.py b/energy_communities/utils.py
index fd3dcca18..f430ddd23 100644
--- a/energy_communities/utils.py
+++ b/energy_communities/utils.py
@@ -42,3 +42,16 @@ def get_translation(source, lang, mods):
     translation = code_translations.get_web_translations(mods, lang)
     translation.update(code_translations.get_python_translations(mods, lang))
     return translation.get(source, source)
+
+
+def get_successful_popup_message(title, message):
+    return {
+        "type": "ir.actions.client",
+        "tag": "display_notification",
+        "params": {
+            "type": "success",
+            "title": title,
+            "message": message,
+            "sticky": False,
+        },
+    }
diff --git a/energy_communities/wizards/change_coordinator_wizard.py b/energy_communities/wizards/change_coordinator_wizard.py
index ffbfff40c..c6cd88dd8 100644
--- a/energy_communities/wizards/change_coordinator_wizard.py
+++ b/energy_communities/wizards/change_coordinator_wizard.py
@@ -2,6 +2,8 @@ from odoo import api, fields, models
 from odoo.exceptions import ValidationError
 from odoo.tools.translate import _
 
+from ..utils import get_successful_popup_message
+
 
 class ChangeCoordinatorWizard(models.TransientModel):
     _name = "change.coordinator.wizard"
@@ -19,14 +21,7 @@ class ChangeCoordinatorWizard(models.TransientModel):
                 company.with_delay().change_coordinator(
                     self.incoming_coordinator, self.change_reason
                 )
-            return {
-                "type": "ir.actions.client",
-                "tag": "display_notification",
-                "params": {
-                    "type": "success",
-                    "title": _("Coordinator change successful"),
-                    "message": _("This community has been moved to a new coordinator"),
-                    "sticky": False,
-                    "next": {"type": "ir.actions.act_window_close"},
-                },
-            }
+            return get_successful_popup_message(
+                _("Coordinator change successful"),
+                _("This community has been moved to a new coordinator"),
+            )
diff --git a/energy_communities/wizards/create_users_wizard.py b/energy_communities/wizards/create_users_wizard.py
index ce2824d6e..6d743f4df 100644
--- a/energy_communities/wizards/create_users_wizard.py
+++ b/energy_communities/wizards/create_users_wizard.py
@@ -1,7 +1,7 @@
 from odoo import fields, models
 from odoo.tools.translate import _
 
-from ..utils import user_creator
+from ..utils import get_successful_popup_message, user_creator
 
 
 class CreateUsersWizard(models.TransientModel):
@@ -38,13 +38,6 @@ class CreateUsersWizard(models.TransientModel):
                 component.create_users_from_communities_cooperator_partners(
                     impacted_records, role_id, self.action, self.force_invite
                 )
-        return {
-            "type": "ir.actions.client",
-            "tag": "display_notification",
-            "params": {
-                "type": "success",
-                "message": _("Process has been started."),
-                "sticky": False,
-                "next": {"type": "ir.actions.act_window_close"},
-            },
-        }
+        return get_successful_popup_message(
+            _("User creation"), _("Process has been started.")
+        )
diff --git a/energy_communities/wizards/multicompany_easy_creation.py b/energy_communities/wizards/multicompany_easy_creation.py
index 58262113d..cf9e019e8 100644
--- a/energy_communities/wizards/multicompany_easy_creation.py
+++ b/energy_communities/wizards/multicompany_easy_creation.py
@@ -9,6 +9,7 @@ from ..models.res_company import (
     _HIERARCHY_LEVEL_BASE_VALUES,
     _LEGAL_FORM_VALUES,
 )
+from ..utils import get_successful_popup_message
 
 _logger = logging.getLogger(__name__)
 
@@ -175,17 +176,10 @@ class AccountMulticompanyEasyCreationWiz(models.TransientModel):
             self.with_delay().thread_action_accept()
         else:
             self.thread_action_accept()
-        return {
-            "type": "ir.actions.client",
-            "tag": "display_notification",
-            "params": {
-                "type": "success",
-                "title": _("Company creation successful"),
-                "message": _("Community creation process has been started."),
-                "sticky": False,
-                "next": {"type": "ir.actions.act_window_close"},
-            },
-        }
+        return get_successful_popup_message(
+            _("Company creation successful"),
+            _("Community creation process has been started."),
+        )
 
     def create_company(self):
         allow_new_members = False
diff --git a/energy_communities_cooperator/models/voluntary_share_interest_return.py b/energy_communities_cooperator/models/voluntary_share_interest_return.py
index d73b436a0..1da0cfa99 100644
--- a/energy_communities_cooperator/models/voluntary_share_interest_return.py
+++ b/energy_communities_cooperator/models/voluntary_share_interest_return.py
@@ -2,6 +2,8 @@ from odoo import api, fields, models
 from odoo.exceptions import ValidationError
 from odoo.tools.translate import _
 
+from odoo.addons.energy_communities.utils import get_successful_popup_message
+
 
 class VoluntaryShareInterestReturn(models.Model):
     _name = "voluntary.share.interest.return"
@@ -90,14 +92,4 @@ class VoluntaryShareInterestReturn(models.Model):
             subject=subject,
             body=body,
         )
-        return {
-            "type": "ir.actions.client",
-            "tag": "display_notification",
-            "params": {
-                "type": "success",
-                "title": subject,
-                "message": body,
-                "sticky": False,
-                "next": {"type": "ir.actions.act_window_close"},
-            },
-        }
+        return get_successful_popup_message(subject, body)
diff --git a/energy_communities_service_invoicing/utils.py b/energy_communities_service_invoicing/utils.py
index e9d5e6d38..15ba8b89a 100644
--- a/energy_communities_service_invoicing/utils.py
+++ b/energy_communities_service_invoicing/utils.py
@@ -22,17 +22,30 @@ _SALE_ORDER_SERVICE_INVOICING_ACTION_VALUES = [
 
 
 def service_invoicing_tree_view(env: Environment):
+    # return {
+    #     "type": "ir.actions.act_window",
+    #     "res_model": "contract.contract",
+    #     "views": [
+    #         (
+    #             env.ref(
+    #                 "energy_communities_service_invoicing.view_service_invoicing_tree"
+    #             ).id,
+    #             "tree",
+    #         ),
+    #     ],
+    #     "target": "current",
+    # }
+
     return {
-        "type": "ir.actions.act_window",
+        # 'name': _('test'),
+        "view_type": "tree",
+        "view_mode": "tree",
+        "view_id": env.ref(
+            "energy_communities_service_invoicing.view_service_invoicing_window_platform_manager"
+        ).id,
         "res_model": "contract.contract",
-        "views": [
-            (
-                env.ref(
-                    "energy_communities_service_invoicing.view_service_invoicing_tree"
-                ).id,
-                "form",
-            ),
-        ],
+        # 'context': "{'type':'out_invoice'}",
+        "type": "ir.actions.act_window",
         "target": "current",
     }
 
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 33a67f2a1..92e08d406 100644
--- a/energy_communities_service_invoicing/wizards/service_invoicing_action_create.py
+++ b/energy_communities_service_invoicing/wizards/service_invoicing_action_create.py
@@ -1,11 +1,10 @@
-from datetime import datetime
-
 from odoo import api, fields, models
 from odoo.exceptions import ValidationError
 from odoo.tools.translate import _
 
 from odoo.addons.energy_communities.utils import (
     contract_utils,
+    get_successful_popup_message,
     sale_order_utils,
 )
 
@@ -14,7 +13,6 @@ from ..utils import (
     get_existing_open_contract,
     raise_existing_same_open_contract_error,
     service_invoicing_form_view_for_platform_admins,
-    service_invoicing_tree_view,
 )
 
 
@@ -23,6 +21,7 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
     _description = "Create service invoicing for an energy community"
     _inherit = ["user.currentcompany.mixin"]
 
+    execution_date = fields.Date(string="Execution date")
     company_id = fields.Many2one("res.company", string="Coordinator")
     community_company_id = fields.Many2one(
         "res.company",
@@ -99,7 +98,10 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
                     community.parent_id,
                     self.env.company.service_invoicing_payment_mode_id,
                 )
-            return service_invoicing_tree_view(self.env)
+            return get_successful_popup_message(
+                _("Service invoicing contracts successfully created"),
+                _("Visit Community Management section to manage them"),
+            )
         else:
             service_invoicing_id = self._execute_create_one(
                 self.community_company_id, self.company_id, self.payment_mode_id
@@ -108,7 +110,7 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
                 self.env, service_invoicing_id
             )
 
-    def execute_create_one(self, community_company_id, company_id, payment_mode_id):
+    def _execute_create_one(self, community_company_id, company_id, payment_mode_id):
         self._validate_service_invoicing_action_create([community_company_id.id])
         existing_closed_contract = get_existing_last_closed_contract(
             self.env, company_id.partner_id, community_company_id
@@ -117,7 +119,7 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
         if existing_closed_contract:
             with contract_utils(self.env, existing_closed_contract) as component:
                 service_invoicing_id = component.reopen(
-                    datetime.now(),
+                    self.execution_date,
                     self.pricelist_id,
                     self.service_pack_id,
                     self.discount,
@@ -132,7 +134,7 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
                     self.service_pack_id,
                     self.pricelist_id,
                     payment_mode_id,
-                    datetime.now(),
+                    self.execution_date,
                     self.discount,
                     "activate",
                     "active_platform_service_invocing",
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 776c34b6e..0a285562d 100644
--- a/energy_communities_service_invoicing/wizards/service_invoicing_action_create.xml
+++ b/energy_communities_service_invoicing/wizards/service_invoicing_action_create.xml
@@ -12,6 +12,7 @@
             <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="execution_date" required="1"/>
             <field
               name="company_id"
               domain="[('hierarchy_level','=','coordinator')]"
-- 
GitLab