| 1 |
from odoo import _, api, models |
| 2 |
|
| 3 |
|
| 4 |
class AccountMoveSend(models.AbstractModel): |
| 5 |
_inherit = 'account.move.send' |
| 6 |
|
| 7 |
@api.model |
| 8 |
def _l10n_jo_is_edi_applicable(self, move): |
| 9 |
return move.l10n_jo_edi_is_needed and move.l10n_jo_edi_state not in ['sent', 'demo'] |
| 10 |
|
| 11 |
def _get_all_extra_edis(self) -> dict: |
| 12 |
|
| 13 |
res = super()._get_all_extra_edis() |
| 14 |
res.update({'jo_edi': {'label': _("JoFotara (Jordan EDI)"), 'is_applicable': self._l10n_jo_is_edi_applicable}}) |
| 15 |
return res |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
def _get_alerts(self, moves, moves_data): |
| 22 |
|
| 23 |
alerts = super()._get_alerts(moves, moves_data) |
| 24 |
if self.env.company.l10n_jo_edi_demo_mode: |
| 25 |
alerts['l10n_jo_edi_demo_mode'] = { |
| 26 |
'level': 'info', |
| 27 |
'message': _("Demo mode is enabled."), |
| 28 |
} |
| 29 |
if non_eligible_jo_moves := moves.filtered(lambda m: 'jo_edi' in moves_data[m]['extra_edis'] and not self._l10n_jo_is_edi_applicable(m)): |
| 30 |
alerts['l10n_jo_edi_non_eligible_moves'] = { |
| 31 |
'message': _( |
| 32 |
"JoFotara e-invoicing was enabled but the following invoices cannot be e-invoiced:\n%(moves)s\n", |
| 33 |
moves="\n".join(f"- {move.display_name}" for move in non_eligible_jo_moves), |
| 34 |
), |
| 35 |
'action_text': _("View Invoice(s)"), |
| 36 |
'action': non_eligible_jo_moves._get_records_action(name=_("Check Invoice(s)")), |
| 37 |
} |
| 38 |
return alerts |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
def _get_invoice_extra_attachments(self, move): |
| 45 |
|
| 46 |
return super()._get_invoice_extra_attachments(move) + move.l10n_jo_edi_xml_attachment_id |
| 47 |
|
| 48 |
def _get_placeholder_mail_attachments_data(self, move, invoice_edi_format=None, extra_edis=None, pdf_report=None): |
| 49 |
|
| 50 |
res = super()._get_placeholder_mail_attachments_data(move, invoice_edi_format=invoice_edi_format, extra_edis=extra_edis, pdf_report=pdf_report) |
| 51 |
|
| 52 |
if not move.l10n_jo_edi_xml_attachment_id and 'jo_edi' in extra_edis: |
| 53 |
attachment_name = move._l10n_jo_edi_get_xml_attachment_name() |
| 54 |
res.append( |
| 55 |
{ |
| 56 |
"id": f"placeholder_{attachment_name}", |
| 57 |
"name": attachment_name, |
| 58 |
"mimetype": "application/xml", |
| 59 |
"placeholder": True, |
| 60 |
} |
| 61 |
) |
| 62 |
return res |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
def _call_web_service_before_invoice_pdf_render(self, invoices_data): |
| 69 |
|
| 70 |
super()._call_web_service_before_invoice_pdf_render(invoices_data) |
| 71 |
|
| 72 |
for invoice, invoice_data in invoices_data.items(): |
| 73 |
if 'jo_edi' in invoice_data['extra_edis']: |
| 74 |
if error_message := invoice.with_company(invoice.company_id)._l10n_jo_edi_send(): |
| 75 |
invoice_data["error"] = { |
| 76 |
"error_title": _("Errors when submitting the JoFotara e-invoice:"), |
| 77 |
"errors": [error_message], |
| 78 |
} |
| 79 |
|
| 80 |
if self._can_commit(): |
| 81 |
self.env.cr.commit() |