From 4be586cecf97f4b6455775a3f0fdc67e39bec347 Mon Sep 17 00:00:00 2001
From: Daniil Digtyar Vasilieva <daniildigtyar@gmail.com>
Date: Tue, 21 Nov 2023 14:19:50 +0100
Subject: [PATCH] [FIX] energy_selfconsumption: next_period not computing
 correctly when applying a different start_date

---
 energy_selfconsumption/models/__init__.py     |  1 +
 .../models/contract_line.py                   | 28 +++++++++++++++++++
 .../wizards/contract_generation_wizard.py     |  7 ++++-
 3 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 energy_selfconsumption/models/contract_line.py

diff --git a/energy_selfconsumption/models/__init__.py b/energy_selfconsumption/models/__init__.py
index 196fcb328..ab0cc16ce 100644
--- a/energy_selfconsumption/models/__init__.py
+++ b/energy_selfconsumption/models/__init__.py
@@ -5,4 +5,5 @@ from . import distribution_table
 from . import supply_point_assignation
 from . import project
 from . import contract
+from . import contract_line
 from . import product
diff --git a/energy_selfconsumption/models/contract_line.py b/energy_selfconsumption/models/contract_line.py
new file mode 100644
index 000000000..dde33cdcb
--- /dev/null
+++ b/energy_selfconsumption/models/contract_line.py
@@ -0,0 +1,28 @@
+from odoo import _, api, fields, models
+from odoo.exceptions import ValidationError
+
+
+class ContractLine(models.Model):
+    _inherit = "contract.line"
+
+    # This validation is raised when writing date_start on the contract and recurring_next_date is yet not computed
+    # Fixed by just checking when the recurrence is at line level (line_recurrence)
+    # TODO create a PR to OCA fixing this
+    @api.constrains("recurring_next_date", "date_start")
+    def _check_recurring_next_date_start_date(self):
+        for line in self:
+            if line.display_type == "line_section" or not line.recurring_next_date:
+                continue
+            if (
+                line.contract_id.line_recurrence
+                and line.date_start
+                and line.recurring_next_date
+            ):
+                if line.date_start > line.recurring_next_date:
+                    raise ValidationError(
+                        _(
+                            "You can't have a date of next invoice anterior "
+                            "to the start of the contract line '%s'"
+                        )
+                        % line.name
+                    )
diff --git a/energy_selfconsumption/wizards/contract_generation_wizard.py b/energy_selfconsumption/wizards/contract_generation_wizard.py
index 99c3adca0..a5f5035be 100644
--- a/energy_selfconsumption/wizards/contract_generation_wizard.py
+++ b/energy_selfconsumption/wizards/contract_generation_wizard.py
@@ -44,6 +44,12 @@ class ContractGenerationWizard(models.TransientModel):
 
         # Create contracts
         for supply_point_assignation in distribution_id.supply_point_assignation_ids:
+            # We write the date_start on the template, so it is not overwrite from the template
+            self.selfconsumption_id.product_id.contract_template_id.write(
+                {
+                    "date_start": self.start_date,
+                }
+            )
             contract = self.env["contract.contract"].create(
                 {
                     "name": _("Contract - %s - %s")
@@ -54,7 +60,6 @@ class ContractGenerationWizard(models.TransientModel):
                     "partner_id": supply_point_assignation.supply_point_id.partner_id.id,
                     "supply_point_assignation_id": supply_point_assignation.id,
                     "company_id": self.env.company.id,
-                    "date_start": self.start_date,
                     "contract_template_id": self.selfconsumption_id.product_id.contract_template_id.id,
                 }
             )
-- 
GitLab