From 69f4595c51d9bea031bc07c69333d1791647a231 Mon Sep 17 00:00:00 2001
From: Daniil Digtyar Vasilieva <daniildigtyar@gmail.com>
Date: Wed, 19 Jul 2023 13:09:47 +0200
Subject: [PATCH] [IMP] energy_selfconsumption: add selfconsumption import
 template

---
 .../data/ir_attactment_data.xml                  |  9 +++++++++
 .../wizards/selfconsumption_import_wizard.py     | 16 +++++++++++++---
 .../selfconsumption_import_wizard_views.xml      |  6 +++++-
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/energy_selfconsumption/data/ir_attactment_data.xml b/energy_selfconsumption/data/ir_attactment_data.xml
index e162bea8d..c22bf2eb0 100644
--- a/energy_selfconsumption/data/ir_attactment_data.xml
+++ b/energy_selfconsumption/data/ir_attactment_data.xml
@@ -1,6 +1,15 @@
 <?xml version="1.0" encoding="utf-8" ?>
 <odoo>
     <data>
+        <record model="ir.attachment" id="selfconsumption_table_example_attachment">
+            <field name="name">import_selfconsumption_template.csv</field>
+            <field name="type">binary</field>
+            <field name="datas">
+                RE5JIFNvY2nDsyxGZWNoYSBlZmVjdGl2YSxDVVBTLENhbGxlIDEsQ2FsbDIsQ2l1ZGFkLEPDs2RpZ28gZGUgbGEgcHJvdmluY2lhLEPDs2RpZ28gcG9zdGFsLEPDs2RpZ28gSVNPIGRlbCBwYcOtcyxETkkgVGl0dWxhcixOb21icmUgVGl0dWxhcixBcGVsbGlkb3MgVGl0dWxhcgoyNDQ5NjUwOEosMDEvMDEvMjAyMyxFUzAzOTA3MjYyMDExNDkyNTNYTCxDYXJyZXIgTWFqb3IgMixOwrogMSxNYWRyaWQsTSwyODAwMSxFUywyNDQ5NjUwOEosLApZMjU4MzQyNkIsLEVTMDM5MDcwMDg3NjQ5NDUyMkpaLEF2ZW5pZGEgTWFqb3IsTsK6IDIsQmFyY2Vsb25hLEIsODAwMSxFUyxZMjU4MzQyNkIsLAowNTc0OTkxM00sLEVTMDM5MDg1ODQ3MTM2MDExM0JaLFRyYXZlc2lhIE1ham9yLE7CuiAyLExsZWlkYSxMLDI1MDAxLEVTLDA1NzQ5OTEzTSwsCg==
+            </field>
+            <field name="company_id"></field>
+        </record>
+
         <record model="ir.attachment" id="distribution_table_example_attachment">
             <field name="name">import_distribution_table_example.csv</field>
             <field name="type">binary</field>
diff --git a/energy_selfconsumption/wizards/selfconsumption_import_wizard.py b/energy_selfconsumption/wizards/selfconsumption_import_wizard.py
index 8d7ad445a..97e9a54d1 100644
--- a/energy_selfconsumption/wizards/selfconsumption_import_wizard.py
+++ b/energy_selfconsumption/wizards/selfconsumption_import_wizard.py
@@ -37,9 +37,10 @@ class SelfconsumptionImportWizard(models.TransientModel):
 
     @api.constrains("import_file")
     def _constrains_import_file(self):
-        format = str(self.fname.split(".")[1])
-        if format != "csv":
-            raise ValidationError(_("Only csv format files are accepted."))
+        if self.fname:
+            format = str(self.fname.split(".")[1])
+            if format != 'csv':
+                raise ValidationError(_("Only csv format files are accepted."))
 
     def import_file_button(self):
         error_string_list = ""
@@ -68,6 +69,15 @@ class SelfconsumptionImportWizard(models.TransientModel):
             )
         return True
 
+    def download_template_button(self):
+        distribution_table_example_attachment = self.env.ref('energy_selfconsumption.selfconsumption_table_example_attachment')
+        download_url = '/web/content/{}/?download=true'.format(str(distribution_table_example_attachment.id))
+        return {
+            "type": "ir.actions.act_url",
+            "url": download_url,
+            "target": "new",
+        }
+
     def get_line_dict(self, line):
         return {
             "partner_vat": line[0] or False,
diff --git a/energy_selfconsumption/wizards/selfconsumption_import_wizard_views.xml b/energy_selfconsumption/wizards/selfconsumption_import_wizard_views.xml
index 3184369a5..67f8b4e78 100644
--- a/energy_selfconsumption/wizards/selfconsumption_import_wizard_views.xml
+++ b/energy_selfconsumption/wizards/selfconsumption_import_wizard_views.xml
@@ -8,6 +8,10 @@
             <field name="arch" type="xml">
                 <form>
                     <sheet>
+                        <p>You can download an example template for the import. The template must have this format and
+                            be a CSV file.</p>
+                        <button type="object" name="download_template_button">Download Template</button>
+                        <separator/>
                         <group>
                             <field name="import_file" filename="fname" widget="binary"></field>
                             <field name="fname" invisible="1"></field>
@@ -19,7 +23,7 @@
                         </group>
                     </sheet>
                     <footer>
-                        <button type="object" name="import_file_button">IMPORT</button>
+                        <button type="object" name="import_file_button">Import</button>
                     </footer>
                 </form>
             </field>
-- 
GitLab