From a344b2611dc8a43b0faa7fa7b6060b2e5dc549dd Mon Sep 17 00:00:00 2001
From: benjamin <benjamin@le-filament.com>
Date: Wed, 22 Mar 2023 15:29:34 +0100
Subject: [PATCH] [add] print bordereau extranet function

---
 report/scop_contribution_report.py | 32 ++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/report/scop_contribution_report.py b/report/scop_contribution_report.py
index 4986c5e..c999b79 100644
--- a/report/scop_contribution_report.py
+++ b/report/scop_contribution_report.py
@@ -1,7 +1,9 @@
 # © 2022 Le Filament (<http://www.le-filament.com>)
 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
 
-from odoo import _, fields, models
+import base64
+
+from odoo import SUPERUSER_ID, _, fields, models
 from odoo.exceptions import UserError
 
 
@@ -39,7 +41,7 @@ class ScopContributionReport(models.Model):
                 contribution.has_bordereau = False
 
     # ------------------------------------------------------
-    # Button
+    # Button / Action
     # ------------------------------------------------------
     def print_report_with_payment(self):
         bordereau = (
@@ -63,6 +65,32 @@ class ScopContributionReport(models.Model):
         else:
             raise UserError(_("Pas de bordereau rattaché à cette cotisation."))
 
+    def print_contribution(self):
+        self.ensure_one()
+        bordereau = (
+            self.env["scop.bordereau"]
+            .sudo()
+            .search(
+                [
+                    ("partner_id", "=", self.partner_id.id),
+                    ("year", "=", self.year),
+                    ("state", "not in", ["draft", "cancel"]),
+                ]
+            )
+        )
+        if len(bordereau) == 1:
+            report_sudo = self.env.ref(
+                "cgscop_cotisation_cg.cgscop_bordereau_report"
+            ).sudo()
+            bordereau_pdf = report_sudo.with_user(SUPERUSER_ID)._render_qweb_pdf(
+                bordereau.id
+            )[0]
+            return base64.b64encode(bordereau_pdf)
+        else:
+            raise UserError(
+                _("Impossible de télécharger le bordereau associé à cette cotisation.")
+            )
+
     # ------------------------------------------------------
     # Business method
     # ------------------------------------------------------
-- 
GitLab