From 8cbc377253979a26656094e0194b1c7873fdd5c4 Mon Sep 17 00:00:00 2001
From: root <root@3adentaire>
Date: Mon, 1 Jul 2024 14:51:36 +0200
Subject: [PATCH] [DBG] add debug instructions and [FIX] issues

---
 wizard/ebics_statement_rop_import_wizard.py | 30 +++++++++++++++++----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/wizard/ebics_statement_rop_import_wizard.py b/wizard/ebics_statement_rop_import_wizard.py
index 3c03960..92e0f1c 100644
--- a/wizard/ebics_statement_rop_import_wizard.py
+++ b/wizard/ebics_statement_rop_import_wizard.py
@@ -1,10 +1,13 @@
 # Copyright 2021- Le Filament (https://le-filament.com)
 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
 import base64
+import logging
 from datetime import datetime
 
 from odoo import fields, models
 
+_logger = logging.getLogger(__name__)
+
 
 class EbicsStatementRopImport(models.TransientModel):
     _name = "ebics.statement.import.wizard"
@@ -32,6 +35,7 @@ class EbicsStatementRopImport(models.TransientModel):
             user_id = False
 
         # Download EBICS MVT
+        _logger.debug("Creating wizard for retrieving enriched moves")
         ebics_mvt_obj = self.env["ebics.xfer"].create(
             {
                 "ebics_config_id": ebics_conf_id.id,
@@ -51,8 +55,11 @@ class EbicsStatementRopImport(models.TransientModel):
         ebics_mvt_file = self.env["ebics.file"].browse(ebics_mvt_file_id)
         if ebics_mvt_file:
             # Process EBICS MVT (= create bank statement)
-            ebics_mvt_file.process()
+            _logger.debug("Creating statements for retrieved enriched moves")
+            ebics_mvt_file.with_context(active_model='ebics.file').process()
+
             bank_statement_ids = ebics_mvt_file.bank_statement_ids
+            _logger.debug(f"Created statements : {bank_statement_ids.mapped('date')}")
             self.log += f"""
                 <strong><u>Import des mouvements enrichis<u></strong><br/>
                 {ebics_mvt_file.note_process}
@@ -64,6 +71,7 @@ class EbicsStatementRopImport(models.TransientModel):
                 <hr/>
             """
         else:
+            _logger.debug("No enriched moves file, no creating statements")
             self.log += """
                 <strong><u>Import des mouvements enrichis<u></strong><br/>
                 Aucune donnée n'a pu être téléchargée,
@@ -72,6 +80,7 @@ class EbicsStatementRopImport(models.TransientModel):
             """
 
         # Download EBICS ROP
+        _logger.debug("Creating wizard for retrieving ROP")
         ebics_rop_obj = self.env["ebics.xfer"].create(
             {
                 "ebics_config_id": ebics_conf_id.id,
@@ -91,13 +100,15 @@ class EbicsStatementRopImport(models.TransientModel):
         ebics_rop_file = self.env["ebics.file"].browse(ebics_rop_file_id)
 
         if ebics_rop_file and bank_statement_ids:
+            _logger.debug(f"Processing ROP file {ebics_rop_file.name}")
             # Process EBICS ROP file
             ebics_rop_file.ensure_one()
             # Suppression de lignes CORPORATE
             lines_to_delete = bank_statement_ids.line_ids.filtered(
-                lambda line: "CORPORATE CARD" in line.name
+                lambda line: "CORPORATE CARD" in line.payment_ref
             )
             line_nb = len(lines_to_delete)
+            _logger.debug(f"Deleting {line_nb} statement lines : {lines_to_delete.ids}")
             lines_to_delete.unlink()
             self.log += f"""
                 <strong><u>Suppression de lignes CORPORATE<u></strong><br/>
@@ -106,11 +117,19 @@ class EbicsStatementRopImport(models.TransientModel):
             """
 
             # Ajout des lignes ROP
+            _logger.debug("Adding statement lines for ROP files")
             rop_lines = self._get_rop_lines(ebics_rop_file.data)
+            statement_ids = bank_statement_ids.filtered("date")
+            statement_to_delete = bank_statement_ids - statement_ids
+            statement_to_delete.unlink()
             for ropl in rop_lines:
-                statement_id = bank_statement_ids.filtered(
-                    lambda st: ropl["date"] == st.date
+                _logger.debug(f"Adding {ropl['payment_ref']} - {ropl['date']}")
+                _logger.debug(f"List of statement_ids : {statement_ids.mapped('date')}")
+
+                statement_id = statement_ids.filtered(
+                    lambda st: fields.Date.from_string(ropl["date"]) <= st.date
                 )[0]
+                _logger.debug(f"in {statement_id} - {statement_id.name} - {statement_id.date}")
                 ropl.update({"statement_id": statement_id.id})
                 self.env["account.bank.statement.line"].create(ropl)
             ebics_rop_file.set_to_done()
@@ -121,6 +140,7 @@ class EbicsStatementRopImport(models.TransientModel):
                         <hr/>
                     """
         else:
+            _logger.debug(f"No ROP file to process or no bank_statements created by enriched moves")
             self.log += """
                 <strong><u>Suppression de lignes CORPORATE &
                  Intégration du fichier ROP<u></strong><br/>
@@ -167,7 +187,7 @@ class EbicsStatementRopImport(models.TransientModel):
                 rop_lines.append(
                     {
                         "date": datetime.strptime(line[177:185], "%Y%m%d").date(),
-                        "name": line[29:61].rstrip()
+                        "payment_ref": line[29:61].rstrip()
                         + " - "
                         + line[138:158].rstrip()
                         + " - "
-- 
GitLab