From b428cab34fac7a7afae038183d25b7011b23beb0 Mon Sep 17 00:00:00 2001
From: daniquilez <dani.quilez@gmail.com>
Date: Tue, 25 Feb 2025 10:25:34 +0100
Subject: [PATCH] =?UTF-8?q?[IMP]=20=E2=9C=A8=20Introducing=20closing=20rea?=
 =?UTF-8?q?son=20on=20contract?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../models/contract.py                        | 42 ++++++++++++++++++-
 .../views/contract_views.xml                  |  8 ++++
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/energy_communities_service_invoicing/models/contract.py b/energy_communities_service_invoicing/models/contract.py
index acadcae8f..392880e0a 100644
--- a/energy_communities_service_invoicing/models/contract.py
+++ b/energy_communities_service_invoicing/models/contract.py
@@ -1,14 +1,18 @@
 from datetime import datetime
 
-from odoo import api, fields, models
-from odoo.tools.translate import _
+from odoo import _, api, fields, models
 
 from ..utils import (
     _CONTRACT_STATUS_VALUES,
+    _SALE_ORDER_SERVICE_INVOICING_ACTION_VALUES,
     get_existing_open_contract,
     raise_existing_same_open_contract_error,
 )
 
+_CLOSING_ACTION_VALUES = _SALE_ORDER_SERVICE_INVOICING_ACTION_VALUES + [
+    ("close", _("Close"))
+]
+
 
 class ContractContract(models.Model):
     _inherit = "contract.contract"
@@ -42,6 +46,18 @@ class ContractContract(models.Model):
     )
     is_pack = fields.Boolean(related="contract_template_id.is_pack")
     is_free_pack = fields.Boolean(related="contract_template_id.is_free_pack")
+    closing_action = fields.Selection(
+        selection=_CLOSING_ACTION_VALUES,
+        compute="_compute_closing_action",
+        string="Closing reason",
+        default="none",
+        store=True,
+    )
+    closing_action_description = fields.Char(
+        string="Closing reason description",
+        compute="_compute_closing_action_description",
+        store=True,
+    )
     related_contract_product_ids = fields.One2many(
         "product.product",
         string="Related services",
@@ -64,6 +80,28 @@ class ContractContract(models.Model):
     # On energy communities all contracts have company_id
     company_id = fields.Many2one(required=True)
 
+    @api.depends("status", "successor_contract_id")
+    def _compute_closing_action(self):
+        for record in self:
+            record.closing_action = "none"
+            if record.status in ["closed", "closed_planned"]:
+                if record.successor_contract_id:
+                    record.closing_action = (
+                        record.successor_contract_id.sale_order_id.service_invoicing_action
+                    )
+                else:
+                    record.closing_action = "close"
+
+    @api.depends("status", "successor_contract_id")
+    def _compute_closing_action_description(self):
+        for record in self:
+            record.closing_action_description = ""
+            if record.status in ["closed", "closed_planned"]:
+                if record.successor_contract_id:
+                    record.closing_action_description = (
+                        record.successor_contract_id.sale_order_id.service_invoicing_action_description
+                    )
+
     @api.constrains("partner_id", "community_company_id")
     def _constrain_unique_contract(self):
         for record in self:
diff --git a/energy_communities_service_invoicing/views/contract_views.xml b/energy_communities_service_invoicing/views/contract_views.xml
index 9f7dd5a4b..924884b58 100644
--- a/energy_communities_service_invoicing/views/contract_views.xml
+++ b/energy_communities_service_invoicing/views/contract_views.xml
@@ -99,6 +99,12 @@
             <field name="recurring_next_date" />
             <field name="date_end" attrs="{'invisible': [('status','!=','closed')]}" />
           </group>
+          <group>
+            <field name="predecessor_contract_id" />
+            <field name="successor_contract_id" attrs="{'invisible':['|',('status','not in',['closed','closed_planned']),('successor_contract_id','!=',False)]}" />
+            <field name="closing_action" attrs="{'invisible':[('status','not in',['closed','closed_planned'])]}" />
+            <field name="closing_action_description" attrs="{'invisible':[('status','not in',['closed','closed_planned'])]}" />
+          </group>
         </sheet>
       </form>
     </field>
@@ -152,6 +158,8 @@
         <field name="discount" />
         <field name="predecessor_contract_id" />
         <field name="successor_contract_id" />
+        <field name="closing_action" attrs="{'invisible':[('status','not in',['closed','closed_planned'])]}" />
+        <field name="closing_action_description" attrs="{'invisible':[('status','not in',['closed','closed_planned'])]}" />
         <field name="sale_order_id" />
       </xpath>
     </field>
-- 
GitLab