From 797e2e71a24f7e9e3abadccfb600bc953b1281e9 Mon Sep 17 00:00:00 2001
From: Juliana <juliana@le-filament.com>
Date: Mon, 18 Jul 2022 18:06:28 +0200
Subject: [PATCH] [ADD]Add generation expense justif zip

---
 wizard/adefpat_project_justif_zip_wizard.py  | 49 ++++++++++++++++++++
 wizard/adefpat_project_justif_zip_wizard.xml |  6 +++
 2 files changed, 55 insertions(+)

diff --git a/wizard/adefpat_project_justif_zip_wizard.py b/wizard/adefpat_project_justif_zip_wizard.py
index de55af7..af2be7b 100644
--- a/wizard/adefpat_project_justif_zip_wizard.py
+++ b/wizard/adefpat_project_justif_zip_wizard.py
@@ -221,3 +221,52 @@ class AdefpatProjectJustifZip(models.TransientModel):
             )
         except Exception as e:
             raise UserError(json.loads(e.details).get('message'))
+
+    @api.multi
+    def build_expense_zip(self):
+        user_ids = self.env["res.users"].search([("active", "=", True)])
+
+        compteur = 0
+        json_file = []
+        for user in user_ids:
+            if user.has_group('base.group_user'):
+                compteur += 1
+                json_file.append({
+                    "id": "1" + str(compteur),
+                    "name": user.name,
+                    "description": user.name,
+                    "search": {
+                        "language": "fts-alfresco",
+                        "query": "PATH:'/app:company_home/st:sites/cm:odoo/cm:documentLibrary/cm:Justificatifs//* AND =adefpat:justificatifUtilisateurs:'"+ user.login +"' AND (+adefpat:justificatifDate:['" + str(
+                            self.period_start) + "' TO '" + str(
+                            self.period_end) + "'])",
+                    }
+                })
+
+        virtual_json = {
+            "name": "Justificatifs Frais",
+            "nodes": [{
+                "id": "1",
+                "name": "Justificatifs Frais",
+                "description": "Justificatifs Frais",
+                "nodes": json_file,
+            }]
+        }
+
+        js = json.dumps(virtual_json, indent=4)
+        prop = {
+            'cmis:secondaryObjectTypeIds': ['P:adefpat:justificatif'],
+        }
+        # 'adefpat:financementConvention': str(self.type_convention_id.name),
+        backend = self.env['cmis.backend'].search([], limit=1)
+        json_file_cmis = self.env.user.company_id.proof_justif_cmis
+        repo = backend.get_cmis_repository().getObject(json_file_cmis)
+        try:
+            file = repo.createDocument(
+                name="justifs_frais_" + str(date.today()) + ".json",
+                properties=prop,
+                contentFile=js,
+                contentType="application/json"
+            )
+        except Exception as e:
+            raise UserError(json.loads(e.details).get('message'))
\ No newline at end of file
diff --git a/wizard/adefpat_project_justif_zip_wizard.xml b/wizard/adefpat_project_justif_zip_wizard.xml
index d886017..d93b807 100644
--- a/wizard/adefpat_project_justif_zip_wizard.xml
+++ b/wizard/adefpat_project_justif_zip_wizard.xml
@@ -21,6 +21,12 @@
                         string="Justifs par dossier"
                         type="object"
                     />
+                    <button
+                        class="btn btn-sm btn-primary"
+                        name="build_expense_zip"
+                        string="Justifs Frais"
+                        type="object"
+                    />
                     <button
                         class="btn btn-sm btn-primary"
                         name="build_facture_fournisseur"
-- 
GitLab