From 7da5c4c17ef1c604e2cbb3affaface28719b455a Mon Sep 17 00:00:00 2001
From: daniquilez <dani.quilez@gmail.com>
Date: Thu, 27 Feb 2025 13:12:50 +0100
Subject: [PATCH] =?UTF-8?q?[IMP]=20=E2=9C=A8=20Avoid=20payment=20mode=20re?=
 =?UTF-8?q?quired=20on=20service=20contract=20creation?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../components/contract_utils.py              | 19 +++++------
 .../components/sale_order_utils.py            |  5 +--
 .../models/res_company.py                     |  8 ++---
 .../views/res_company_views.xml               |  3 +-
 .../wizards/service_invoicing_action.xml      |  8 ++++-
 .../service_invoicing_action_create.py        | 33 +++++++++----------
 .../service_invoicing_action_create.xml       | 22 +++++++++----
 7 files changed, 56 insertions(+), 42 deletions(-)

diff --git a/energy_communities_service_invoicing/components/contract_utils.py b/energy_communities_service_invoicing/components/contract_utils.py
index 4b45e99f2..00541918e 100644
--- a/energy_communities_service_invoicing/components/contract_utils.py
+++ b/energy_communities_service_invoicing/components/contract_utils.py
@@ -83,17 +83,16 @@ class ContractUtils(Component):
         sale_order_utils = self.component(
             usage="sale.order.utils", model_name="sale.order"
         )
-        service_invoicing_params = self._build_service_invoicing_params(
-            "modification",
-            executed_modification_action,
-            execution_date,
-            pricelist_id,
-            service_pack_id,
-            discount,
-            payment_mode_id,
-        )
         new_service_invoicing_id = sale_order_utils.create_service_invoicing_initial(
-            **service_invoicing_params
+            **self._build_service_invoicing_params(
+                "modification",
+                executed_modification_action,
+                execution_date,
+                pricelist_id,
+                service_pack_id,
+                discount,
+                payment_mode_id,
+            )
         )
         # TODO:
         # Do we really want new contract to be in_progress on a modification??
diff --git a/energy_communities_service_invoicing/components/sale_order_utils.py b/energy_communities_service_invoicing/components/sale_order_utils.py
index 5cfeeccaa..86ab78a50 100644
--- a/energy_communities_service_invoicing/components/sale_order_utils.py
+++ b/energy_communities_service_invoicing/components/sale_order_utils.py
@@ -23,7 +23,6 @@ class SaleOrderUtils(Component):
             "pricelist_id": pricelist_id.id,
             "service_invoicing_action": executed_action,
             "service_invoicing_action_description": executed_action_description,
-            "payment_mode_id": payment_mode_id.id,
             "order_line": [
                 (
                     0,
@@ -36,6 +35,8 @@ class SaleOrderUtils(Component):
                 )
             ],
         }
+        if payment_mode_id:
+            so_creation_dict["payment_mode_id"] = payment_mode_id.id
         # Apply configuration sales team to service invoicing sales order
         if company_id.service_invoicing_sale_team_id:
             so_creation_dict["team_id"] = company_id.service_invoicing_sale_team_id.id
@@ -83,11 +84,11 @@ class SaleOrderUtils(Component):
         community_company_id,
         service_pack_id,
         pricelist_id,
-        payment_mode_id,
         start_date,
         discount,
         executed_action,
         executed_action_description="none",
+        payment_mode_id=False,
     ):
         service_invoicing_id = self._create_service_invoicing(
             company_id,
diff --git a/energy_communities_service_invoicing/models/res_company.py b/energy_communities_service_invoicing/models/res_company.py
index 1c74ab3bd..0264c69bb 100644
--- a/energy_communities_service_invoicing/models/res_company.py
+++ b/energy_communities_service_invoicing/models/res_company.py
@@ -10,10 +10,10 @@ class ResCompany(models.Model):
         comodel_name="account.journal",
         string="Service invoicing journal",
     )
-    service_invoicing_payment_mode_id = fields.Many2one(
-        comodel_name="account.payment.mode",
-        string="Service invoicing payment mode",
-    )
+    # service_invoicing_payment_mode_id = fields.Many2one(
+    #     comodel_name="account.payment.mode",
+    #     string="Service invoicing payment mode",
+    # )
     service_invoicing_sale_team_id = fields.Many2one(
         comodel_name="crm.team",
         string="Service invoicing sales team",
diff --git a/energy_communities_service_invoicing/views/res_company_views.xml b/energy_communities_service_invoicing/views/res_company_views.xml
index 3b5673954..7c49d6fa3 100644
--- a/energy_communities_service_invoicing/views/res_company_views.xml
+++ b/energy_communities_service_invoicing/views/res_company_views.xml
@@ -7,7 +7,7 @@
     <field name="state">code</field>
     <field name="groups_id" eval="[(4,ref('energy_communities.group_platform_manager'))]" />
     <field name="code">
-        action = model.get_service_invoicing_action_create_wizard_form_view()
+        action = model.get_multiple_service_invoicing_action_create_wizard_form_view()
     </field>
   </record>
   <record id="view_service_invocing_company_form" model="ir.ui.view">
@@ -20,7 +20,6 @@
           <group>
             <field name="id" invisible="1"/>
             <field name="service_invoicing_journal_id" domain="[('company_id', '=', id)]" />
-            <field name="service_invoicing_payment_mode_id" domain="[('company_id', '=', id)]" />
             <field name="service_invoicing_sale_team_id" domain="[('company_id', '=', id)]" />
           </group>
         </page>
diff --git a/energy_communities_service_invoicing/wizards/service_invoicing_action.xml b/energy_communities_service_invoicing/wizards/service_invoicing_action.xml
index dc16b29be..f66cf95e0 100644
--- a/energy_communities_service_invoicing/wizards/service_invoicing_action.xml
+++ b/energy_communities_service_invoicing/wizards/service_invoicing_action.xml
@@ -10,7 +10,13 @@
         <sheet>
           <group>
             <field name="executed_action" invisible="1" />
-            <field name="service_invoicing_id" required="1" domain="[('community_company_id','!=',False)]"/>
+            <field
+              name="service_invoicing_id"
+              required="1"
+              readonly="1"
+              domain="[('community_company_id','!=',False)]"
+              options="{'no_open': True}"
+            />
             <field name="execution_date" required="1"/>
             <field
               name="service_pack_id"
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 f13cb08ca..82768993b 100644
--- a/energy_communities_service_invoicing/wizards/service_invoicing_action_create.py
+++ b/energy_communities_service_invoicing/wizards/service_invoicing_action_create.py
@@ -21,6 +21,9 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
     _description = "Create service invoicing for an energy community"
     _inherit = ["user.currentcompany.mixin"]
 
+    creation_type = fields.Selection(
+        [("single", "Single"), ("multiple", "Multiple")], default="single"
+    )
     execution_date = fields.Date(string="Execution date")
     company_id = fields.Many2one("res.company", string="Coordinator")
     community_company_id = fields.Many2one(
@@ -76,27 +79,23 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
     @api.depends("company_id", "community_company_mids")
     def _compute_allowed_payment_mode_ids(self):
         for record in self:
-            if record.community_company_mids:
-                query = [("company_id", "=", self.user_current_company.id)]
-            else:
-                query = [("company_id", "=", record.company_id.id)]
             record.allowed_payment_mode_ids = self.env["account.payment.mode"].search(
-                query
+                [("company_id", "=", self.user_current_company.id)]
             )
 
     @api.onchange("company_id")
     def _compute_service_invoicing_action_create_wizard_allowed_values(self):
         for record in self:
+            record._compute_pack_product_categ_id()
             record._compute_allowed_community_company_ids()
             record._compute_allowed_payment_mode_ids()
 
     def execute_create(self):
-        if self.community_company_mids:
+        if self.creation_type == "multiple":
             for community in self.community_company_mids:
                 self._execute_create_one(
                     community,
                     community.parent_id,
-                    self.env.company.service_invoicing_payment_mode_id,
                 )
             return service_invoicing_tree_view(self.env)
         else:
@@ -107,7 +106,9 @@ 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=False
+    ):
         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
@@ -130,15 +131,15 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
                     community_company_id,
                     self.service_pack_id,
                     self.pricelist_id,
-                    payment_mode_id,
                     self.execution_date,
                     self.discount,
                     "activate",
                     "active_platform_service_invocing",
+                    payment_mode_id,
                 )
         return service_invoicing_id
 
-    def get_service_invoicing_action_create_wizard_form_view(self):
+    def get_multiple_service_invoicing_action_create_wizard_form_view(self):
         if "active_ids" in self.env.context.keys():
             self._validate_service_invoicing_action_create(
                 self.env.context["active_ids"]
@@ -148,6 +149,7 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
             )
             wizard = self.env["service.invoicing.action.create.wizard"].create(
                 {
+                    "creation_type": "multiple",
                     "community_company_mids": self.env.context["active_ids"],
                 }
             )
@@ -168,6 +170,10 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
         return False
 
     def _validate_service_invoicing_action_create(self, company_id_list):
+        if self.env.company.hierarchy_level != "instance":
+            raise ValidationError(
+                _("This action is only allowed when you're on instance level.")
+            )
         impacted_records = self.env["res.company"].browse(company_id_list)
         # Check all selected companies are communities
         hierarchy_levels = list(set(impacted_records.mapped("hierarchy_level")))
@@ -191,10 +197,3 @@ class ServiceInvoicingActionCreateWizard(models.TransientModel):
                         record.name
                     )
                 )
-            # Check current company has configuration payment mode for multicompany creation
-            if not self.env.company.service_invoicing_payment_mode_id:
-                raise ValidationError(
-                    _(
-                        "Platform {} must have a service invoicing payment mode defined"
-                    ).format(self.env.company.name)
-                )
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 0a285562d..309c02e08 100644
--- a/energy_communities_service_invoicing/wizards/service_invoicing_action_create.xml
+++ b/energy_communities_service_invoicing/wizards/service_invoicing_action_create.xml
@@ -6,9 +6,10 @@
     <field name="name">service.invoicing.action.create.wizard.form</field>
     <field name="model">service.invoicing.action.create.wizard</field>
     <field name="arch" type="xml">
-      <form string="Service invoicing">
+      <form string="Service Contract">
         <sheet>
           <group>
+            <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" />
@@ -16,21 +17,30 @@
             <field
               name="company_id"
               domain="[('hierarchy_level','=','coordinator')]"
-              attrs="{'invisible':[('community_company_mids','!=',[])],'required':[('community_company_mids','=',[])]}"
+              attrs="{'invisible':[('creation_type','=','multiple')],'required':[('creation_type','=','single')]}"
             />
             <field
               name="community_company_id"
-              attrs="{'invisible':[('community_company_mids','!=',[])],'required':[('community_company_mids','=',[])]}"
+              attrs="{'invisible':[('creation_type','=','multiple')],'required':[('creation_type','=','single')]}"
             />
-            <field name="community_company_mids" />
+            <field 
+              name="community_company_mids"
+              attrs="{'invisible':[('creation_type','=','single')],'required':[('creation_type','=','multiple')]}"
+              widget="many2many"
+            >
+             <tree>
+                <field name="name"/>
+                <field name="parent_id"/>
+             </tree>
+            </field>
             <field name="service_pack_id"
-              required="1"
               domain="[('categ_id','=',pack_product_categ_id)]"
+              required="1"
             />
             <field name="pricelist_id" required="1" domain="[('company_id','=',False)]"/>
             <field
               name="payment_mode_id"
-              attrs="{'invisible':[('community_company_mids','!=',[])],'required':[('community_company_mids','=',[])]}"
+              attrs="{'invisible':[('creation_type','=','multiple')]}"
             />
             <field name="discount" required="1" />
           </group>
-- 
GitLab