Skip to content

Commit f822b3d

Browse files
committed
[FIX] account_edi_ubl_cii: fix edi options on journal
In 14.0, _is_enabled_by_default_on_journal does not exist, thus every edi.format is enabled by default. This commit allows to only have facturx enabled by default. In addition, we only want the edi.format options to appear on the sale journals (out_invoice, out_refund). Finally, the warnings are no longer displayed on top of the invoice. closes odoo#94302 Signed-off-by: Laurent Smet <[email protected]>
1 parent e76c542 commit f822b3d

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

addons/account_edi/models/account_edi_format.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ def _is_compatible_with_journal(self, journal):
105105
self.ensure_one()
106106
return journal.type == 'sale'
107107

108+
def _is_enabled_by_default_on_journal(self, journal):
109+
""" Indicate if the EDI format should be selected by default on the journal passed as parameter.
110+
If True, this EDI format will be selected by default on the journal.
111+
112+
:param journal: The journal.
113+
:returns: True if this format should be enabled by default on the journal, False otherwise.
114+
"""
115+
return True
116+
108117
def _is_embedding_to_invoice_pdf_needed(self):
109118
""" Indicate if the EDI must be embedded inside the PDF report.
110119

addons/account_edi/models/account_journal.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from odoo import api, models, fields, _
55
from odoo.exceptions import UserError
66

7+
from collections import defaultdict
8+
79

810
class AccountJournal(models.Model):
911
_inherit = 'account.journal'
@@ -47,11 +49,34 @@ def _compute_compatible_edi_ids(self):
4749

4850
for journal in self:
4951
compatible_edis = edi_formats.filtered(lambda e: e._is_compatible_with_journal(journal))
50-
journal.compatible_edi_ids += compatible_edis
52+
journal.compatible_edi_ids = compatible_edis
5153

5254
@api.depends('type', 'company_id', 'company_id.country_id')
5355
def _compute_edi_format_ids(self):
5456
edi_formats = self.env['account.edi.format'].search([])
57+
journal_ids = self.ids
58+
59+
if journal_ids:
60+
self._cr.execute('''
61+
SELECT
62+
move.journal_id,
63+
ARRAY_AGG(doc.edi_format_id) AS edi_format_ids
64+
FROM account_edi_document doc
65+
JOIN account_move move ON move.id = doc.move_id
66+
WHERE doc.state IN ('to_cancel', 'to_send')
67+
AND move.journal_id IN %s
68+
GROUP BY move.journal_id
69+
''', [tuple(journal_ids)])
70+
protected_edi_formats_per_journal = {r[0]: set(r[1]) for r in self._cr.fetchall()}
71+
else:
72+
protected_edi_formats_per_journal = defaultdict(set)
5573

5674
for journal in self:
57-
journal.edi_format_ids += edi_formats.filtered(lambda e: e._is_compatible_with_journal(journal))
75+
enabled_edi_formats = edi_formats.filtered(lambda e: e._is_compatible_with_journal(journal) and
76+
e._is_enabled_by_default_on_journal(journal))
77+
78+
# The existing edi formats that are already in use so we can't remove it.
79+
protected_edi_format_ids = protected_edi_formats_per_journal.get(journal.id, set())
80+
protected_edi_formats = journal.edi_format_ids.filtered(lambda e: e.id in protected_edi_format_ids)
81+
82+
journal.edi_format_ids = enabled_edi_formats + protected_edi_formats

addons/account_edi_ubl_cii/models/account_edi_format.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def _is_compatible_with_journal(self, journal):
8787
self.ensure_one()
8888
if self.code not in FORMAT_CODES:
8989
return super()._is_compatible_with_journal(journal)
90-
return self._is_ubl_cii_available(journal.company_id)
90+
return self._is_ubl_cii_available(journal.company_id) and journal.type == 'sale'
9191

9292
def _is_enabled_by_default_on_journal(self, journal):
9393
# EXTENDS account_edi
@@ -107,6 +107,7 @@ def _post_invoice_edi(self, invoices, test_mode=False):
107107
res = {}
108108
for invoice in invoices:
109109
builder = self._get_xml_builder(invoice.company_id)
110+
# For now, the errors are not displayed anywhere, don't want to annoy the user
110111
xml_content, errors = builder._export_invoice(invoice)
111112

112113
# DEBUG: send directly to the test platform (the one used by ecosio)
@@ -127,14 +128,6 @@ def _post_invoice_edi(self, invoices, test_mode=False):
127128
'success': True,
128129
'attachment': attachment,
129130
}
130-
if errors:
131-
res[invoice].update({
132-
'success': False,
133-
'error': _("Errors occured while creating the EDI document (format: %s). The receiver "
134-
"might refuse it.", builder._description)
135-
+ '<p> <li>' + "</li> <li>".join(errors) + '</li> </p>',
136-
'blocking_level': 'info',
137-
})
138131

139132
return res
140133

0 commit comments

Comments
 (0)