From 7ddd84eb7f792d9cd1e0d2c9b485212513302ec9 Mon Sep 17 00:00:00 2001
From: Benjamin <benjamin@le-filament.com>
Date: Mon, 7 Jun 2021 15:24:58 +0200
Subject: [PATCH] [debug] add debug Ebics Download

---
 __manifest__.py      |   2 +-
 models/ebics_xfer.py | 133 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+), 1 deletion(-)
 create mode 100644 models/ebics_xfer.py

diff --git a/__manifest__.py b/__manifest__.py
index 186bd34..7218809 100644
--- a/__manifest__.py
+++ b/__manifest__.py
@@ -7,7 +7,7 @@
     "application": False,
     "installable": True,
     "depends": [
-        "account", "sale"
+        "account", "sale", "account_ebics"
     ],
     "data": [
         "views/account_bank_statement.xml",
diff --git a/models/ebics_xfer.py b/models/ebics_xfer.py
new file mode 100644
index 0000000..a030be9
--- /dev/null
+++ b/models/ebics_xfer.py
@@ -0,0 +1,133 @@
+# Copyright 2020 Le Filament
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+
+import base64
+import logging
+import os
+from sys import exc_info
+from traceback import format_exception
+
+from odoo import api, fields, models, _
+from odoo.exceptions import UserError
+
+_logger = logging.getLogger(__name__)
+
+try:
+    import fintech
+    from fintech.ebics import EbicsKeyRing, EbicsBank, EbicsUser, EbicsClient,\
+        EbicsFunctionalError, EbicsTechnicalError, EbicsVerificationError
+    fintech.cryptolib = 'cryptography'
+except ImportError:
+    EbicsBank = object
+    _logger.warning('Failed to import fintech')
+
+
+class EbicsXfer(models.TransientModel):
+    _inherit = 'ebics.xfer'
+
+    @api.multi
+    def ebics_download(self):
+        self.ensure_one()
+        self.ebics_config_id._check_ebics_files()
+        ctx = self._context.copy()
+        _logger.info(ctx)
+        self.note = ''
+        client = self._setup_client()
+        _logger.info(client)
+        if client:
+            download_formats = (
+                    self.format_id
+                    or self.ebics_config_id.ebics_file_format_ids.filtered(lambda r: r.type == 'down')
+            )
+            ebics_files = self.env['ebics.file']
+            date_from = self.date_from and self.date_from.isoformat() or None
+            date_to = self.date_to and self.date_to.isoformat() or None
+            for df in download_formats:
+                try:
+                    success = False
+                    if df.order_type == 'FDL':
+                        _logger.info(df)
+                        data = client.FDL(df.name, date_from, date_to)
+                        _logger.info(data)
+                    else:
+                        params = None
+                        if date_from and date_to:
+                            params = {'DateRange': {
+                                'Start': date_from,
+                                'End': date_to,
+                            }}
+                        data = client.download(df.order_type, params=params)
+                    ebics_files += self._handle_download_data(data, df)
+                    success = True
+                except EbicsFunctionalError:
+                    _logger.info("EbicsFunctionalError")
+                    e = exc_info()
+                    _logger.info(e)
+                    self.note += '\n'
+                    self.note += _(
+                        "EBICS Functional Error during download of File Format %s (%s):"
+                    ) % (df.name, df.order_type)
+                    self.note += '\n'
+                    self.note += '%s (code: %s)' % (e[1].message, e[1].code)
+                except EbicsTechnicalError:
+                    _logger.info("EbicsTechnicalError")
+                    e = exc_info()
+                    _logger.info(e)
+                    self.note += '\n'
+                    self.note += _(
+                        "EBICS Technical Error during download of File Format %s (%s):"
+                    ) % (df.name, df.order_type)
+                    self.note += '\n'
+                    self.note += '%s (code: %s)' % (e[1].message, e[1].code)
+                except EbicsVerificationError:
+                    self.note += '\n'
+                    self.note += _(
+                        "EBICS Verification Error during download of "
+                        "File Format %s (%s):"
+                    ) % (df.name, df.order_type)
+                    self.note += '\n'
+                    self.note += _("The EBICS response could not be verified.")
+                except UserError as e:
+                    self.note += '\n'
+                    self.note += _(
+                        "Warning during download of File Format %s (%s):"
+                    ) % (df.name, df.order_type)
+                    self.note += '\n'
+                    self.note += e.name
+                except Exception:
+                    self.note += '\n'
+                    self.note += _(
+                        "Unknown Error during download of File Format %s (%s):"
+                    ) % (df.name, df.order_type)
+                    tb = ''.join(format_exception(*exc_info()))
+                    self.note += '\n%s' % tb
+                else:
+                    # mark received data so that it is not included in further
+                    # downloads
+                    trans_id = client.last_trans_id
+                    client.confirm_download(trans_id=trans_id, success=success)
+
+            ctx['ebics_file_ids'] = ebics_files._ids
+
+            if ebics_files:
+                self.note += '\n'
+                for f in ebics_files:
+                    self.note += _(
+                        "EBICS File '%s' is available for further processing."
+                    ) % f.name
+                    self.note += '\n'
+
+        module = __name__.split('addons.')[1].split('.')[0]
+        result_view = self.env.ref(
+            '%s.ebics_xfer_view_form_result' % module)
+        return {
+            'name': _('EBICS file transfer result'),
+            'res_id': self.id,
+            'view_type': 'form',
+            'view_mode': 'form',
+            'res_model': 'ebics.xfer',
+            'view_id': result_view.id,
+            'target': 'new',
+            'context': ctx,
+            'type': 'ir.actions.act_window',
+        }
\ No newline at end of file
-- 
GitLab