From 37d2b0ba71e91624b9836ebb31cd90e7d0987af2 Mon Sep 17 00:00:00 2001
From: Emanuel Buzey <buzeyemanuel@gmail.com>
Date: Mon, 9 Oct 2023 14:04:12 +0200
Subject: [PATCH] [FIX] energy_selfconsumption: save invoicing_mode and
 product_id

---
 .../models/selfconsumption.py                 |  4 +++-
 .../views/selfconsumption_views.xml           | 14 +++++++++++++
 .../wizards/define_invoicing_mode_wizard.py   | 20 +++++++++----------
 3 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/energy_selfconsumption/models/selfconsumption.py b/energy_selfconsumption/models/selfconsumption.py
index 0aa999f94..8ac7e6f4c 100644
--- a/energy_selfconsumption/models/selfconsumption.py
+++ b/energy_selfconsumption/models/selfconsumption.py
@@ -85,7 +85,9 @@ class Selfconsumption(models.Model):
     )
     inscription_count = fields.Integer(compute=_compute_inscription_count)
     contracts_count = fields.Integer(compute=_compute_contract_count)
-    invoicing_mode = fields.Char()
+    invoicing_mode = fields.Selection(INVOICING_VALUES, string="Invoicing Mode")
+    product_id = fields.Many2one("product.product", string="Product")
+    contract_template_id = fields.Many2one("contract.template")
 
     def get_distribution_tables(self):
         self.ensure_one()
diff --git a/energy_selfconsumption/views/selfconsumption_views.xml b/energy_selfconsumption/views/selfconsumption_views.xml
index 2d41095be..fbe73d7e5 100644
--- a/energy_selfconsumption/views/selfconsumption_views.xml
+++ b/energy_selfconsumption/views/selfconsumption_views.xml
@@ -223,6 +223,20 @@
                                 </div>
                             </group>
                         </group>
+
+                        <notebook colspan="4">
+                            <page
+                string="Invoicing Mode"
+                name="invoicing_mode"
+                autofocus="autofocus"
+              >
+                                <group>
+                                    <field name="invoicing_mode" />
+                                    <field name="product_id" />
+                                </group>
+                            </page>
+                        </notebook>
+
                     </sheet>
                     <div class="oe_chatter">
                         <field name="message_follower_ids" widget="mail_followers" />
diff --git a/energy_selfconsumption/wizards/define_invoicing_mode_wizard.py b/energy_selfconsumption/wizards/define_invoicing_mode_wizard.py
index d5c29b067..dd6e93acc 100644
--- a/energy_selfconsumption/wizards/define_invoicing_mode_wizard.py
+++ b/energy_selfconsumption/wizards/define_invoicing_mode_wizard.py
@@ -40,20 +40,11 @@ class ContractGenerationWizard(models.TransientModel):
         "energy_selfconsumption.selfconsumption", readonly=True
     )
 
-    def _get_invoicing_mode_value(self):
-        """
-        This method return the invoicing_mode label.
-        """
-        for mode_value, mode_label in self.INVOICING_VALUES:
-            if mode_value == self.invoicing_mode:
-                return _(mode_label)
-        return ""
-
     def save_data_to_selfconsumption(self):
         # Create product
-        self.env["product.product"].create(
+        product_id = self.env["product.product"].create(
             {
-                "name": f"{self._get_invoicing_mode_value()} - {self.selfconsumption_id.name}",
+                "name": f"{self.invoicing_mode[1]} - {self.selfconsumption_id.name}",
                 "lst_price": self.price,
                 "company_id": self.env.company.id,
                 "project_id": self.selfconsumption_id.project_id.id,
@@ -110,6 +101,13 @@ result = line.supply_point_assignation_id.distribution_table_id.selfconsumption_
                 }
             )
 
+        self.selfconsumption_id.write(
+            {
+                "invoicing_mode": self.invoicing_mode,
+                "product_id": product_id,
+            }
+        )
+
         return {
             "type": "ir.actions.act_window_close",
         }
-- 
GitLab