| 1 |
from odoo import _, api, models |
| 2 |
|
| 3 |
|
| 4 |
class AccountMoveSend(models.AbstractModel): |
| 5 |
_inherit = 'account.move.send' |
| 6 |
|
| 7 |
@api.model |
| 8 |
def _is_ro_edi_applicable(self, move): |
| 9 |
return all([ |
| 10 |
move._need_ubl_cii_xml('ciusro') or move.ubl_cii_xml_id, |
| 11 |
move.country_code == 'RO', |
| 12 |
not move.l10n_ro_edi_state, |
| 13 |
]) |
| 14 |
|
| 15 |
def _get_all_extra_edis(self) -> dict: |
| 16 |
|
| 17 |
res = super()._get_all_extra_edis() |
| 18 |
res.update({'ro_edi': {'label': _("Send E-Factura to SPV"), 'is_applicable': self._is_ro_edi_applicable}}) |
| 19 |
return res |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
def _get_alerts(self, moves, moves_data): |
| 26 |
|
| 27 |
alerts = super()._get_alerts(moves, moves_data) |
| 28 |
if waiting_moves := moves.filtered(lambda m: m.l10n_ro_edi_state in ['invoice_not_indexed', 'invoice_sent']): |
| 29 |
alerts['l10n_ro_edi_warning_waiting_moves'] = { |
| 30 |
'message': _( |
| 31 |
"The following invoice(s) are waiting for answer from the Romanian SPV: %s." |
| 32 |
"We won't send them again.", |
| 33 |
', '.join(waiting_moves.mapped('name')) |
| 34 |
), |
| 35 |
'action_text': _("View Invoice(s)"), |
| 36 |
'action': waiting_moves._get_records_action(name=_("Check Invoice(s)")), |
| 37 |
} |
| 38 |
return alerts |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
def _hook_invoice_document_before_pdf_report_render(self, invoice, invoice_data): |
| 45 |
if 'ro_edi' in invoice_data['extra_edis']: |
| 46 |
invoice_data['invoice_edi_format'] = 'ciusro' |
| 47 |
super()._hook_invoice_document_before_pdf_report_render(invoice, invoice_data) |
| 48 |
|
| 49 |
def _call_web_service_after_invoice_pdf_render(self, invoices_data): |
| 50 |
|
| 51 |
super()._call_web_service_after_invoice_pdf_render(invoices_data) |
| 52 |
|
| 53 |
for invoice, invoice_data in invoices_data.items(): |
| 54 |
if 'ro_edi' in invoice_data['extra_edis']: |
| 55 |
|
| 56 |
if invoice.l10n_ro_edi_document_ids: |
| 57 |
|
| 58 |
invoice_data['error'] = { |
| 59 |
'error_title': _("The CIUS-RO E-Factura has already been sent"), |
| 60 |
} |
| 61 |
continue |
| 62 |
|
| 63 |
if invoice_data.get('ubl_cii_xml_attachment_values'): |
| 64 |
xml_data = invoice_data['ubl_cii_xml_attachment_values']['raw'] |
| 65 |
elif invoice.ubl_cii_xml_id: |
| 66 |
xml_data = invoice.ubl_cii_xml_id.raw |
| 67 |
else: |
| 68 |
invoice_data['error'] = { |
| 69 |
'error_title': _("The CIUS-RO E-Factura could not be found"), |
| 70 |
} |
| 71 |
continue |
| 72 |
|
| 73 |
if errors := invoice._l10n_ro_edi_send_invoice(xml_data): |
| 74 |
invoice_data['error'] = { |
| 75 |
'error_title': _("Error when sending CIUS-RO E-Factura to the SPV"), |
| 76 |
'errors': errors, |
| 77 |
} |