From c20e3b95a82fd35c1dcefd159525f59e2c255e9d Mon Sep 17 00:00:00 2001
From: Josse Colpaert <jco@odoo.com>
Date: Mon, 3 Jan 2022 12:44:34 +0000
Subject: [PATCH] [FIX] l10n_it_edi_sdicoop: avoid tracebacks when no file,
 batching
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When we ack a file for having correctly received the response e.g.,
the file is deleted on the server and will not have a file key in the reponse,
at least before the server itself will have a new response to share.

Before, it would traceback, while now, we do nothing.  (as we are still waiting)

Also, when we get the notice that the term of 2 weeks is passed, it means
it is successfull.

Also, for the batching we had a traceback, because it was possible to have
a batching mixing step 1 and step 2, which fails doing invoices.l10n_it_edi_transaction

So we added whether there is a tranaction or not in the batch key.

opw-2703669

closes odoo/odoo#82298

Signed-off-by: William André (wan) <wan@odoo.com>
---
 .../models/account_edi_format.py              | 30 +++++++++++++------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/addons/l10n_it_edi_sdicoop/models/account_edi_format.py b/addons/l10n_it_edi_sdicoop/models/account_edi_format.py
index 880eb484a8dd..7f6412862aa4 100644
--- a/addons/l10n_it_edi_sdicoop/models/account_edi_format.py
+++ b/addons/l10n_it_edi_sdicoop/models/account_edi_format.py
@@ -110,6 +110,13 @@ class AccountEdiFormat(models.Model):
 
         return super()._support_batching(move=move, state=state, company=company)
 
+    def _get_batch_key(self, move, state):
+        # OVERRIDE
+        if self.code != 'fattura_pa':
+            return super()._get_batch_key(move, state)
+
+        return move.move_type, bool(move.l10n_it_edi_transaction)
+
     def _l10n_it_post_invoices_step_1(self, invoices):
         ''' Send the invoices to the proxy.
         '''
@@ -209,6 +216,10 @@ class AccountEdiFormat(models.Model):
                 to_return[invoice] = {'error': _('You are not allowed to check the status of this invoice.'), 'blocking_level': 'error'}
                 continue
 
+            if not response.get('file'): # It means there is no status update, so we can skip it
+                document = invoice.edi_document_ids.filtered(lambda d: d.edi_format_id.code == 'fattura_pa')
+                to_return[invoice] = {'error': document.error, 'blocking_level': document.blocking_level}
+                continue
             xml = proxy_user._decrypt_data(response['file'], response['key'])
             response_tree = etree.fromstring(xml)
             if state == 'ricevutaConsegna':
@@ -227,25 +238,26 @@ class AccountEdiFormat(models.Model):
             elif state == 'notificaEsito':
                 outcome = response_tree.find('Esito').text
                 if outcome == 'EC01':
-                    to_return[invoice] = {'attachment': invoice.l10n_it_edi_attachment_id}
+                    to_return[invoice] = {'attachment': invoice.l10n_it_edi_attachment_id, 'success': True}
                 else:  # ECO2
                     to_return[invoice] = {'error': _('The invoice was refused by the addressee.'), 'blocking_level': 'error'}
             elif state == 'NotificaDecorrenzaTermini':
-                to_return[invoice] = {'error': _('Expiration of the maximum term for communication of acceptance/refusal'), 'blocking_level': 'error'}
+                to_return[invoice] = {'attachment': invoice.l10n_it_edi_attachment_id, 'success': True}
             proxy_acks.append(id_transaction)
 
-        try:
-            proxy_user._make_request(server_url + '/api/l10n_it_edi/1/ack',
-                                     params={'transaction_ids': proxy_acks})
-        except AccountEdiProxyError as e:
-            # Will be ignored and acked again next time.
-            _logger.error('Error while acking file to SdiCoop: %s', e)
+        if proxy_acks:
+            try:
+                proxy_user._make_request(server_url + '/api/l10n_it_edi/1/ack',
+                                        params={'transaction_ids': proxy_acks})
+            except AccountEdiProxyError as e:
+                # Will be ignored and acked again next time.
+                _logger.error('Error while acking file to SdiCoop: %s', e)
 
         return to_return
 
     def _post_fattura_pa(self, invoices):
         # OVERRIDE
-        if not invoices.l10n_it_edi_transaction:
+        if not invoices[0].l10n_it_edi_transaction:
             return self._l10n_it_post_invoices_step_1(invoices)
         else:
             return self._l10n_it_post_invoices_step_2(invoices)
-- 
GitLab