| 1 |
from odoo import _, api, models, SUPERUSER_ID |
| 2 |
|
| 3 |
|
| 4 |
class AccountMoveSend(models.AbstractModel): |
| 5 |
_inherit = "account.move.send" |
| 6 |
|
| 7 |
@api.model |
| 8 |
def _is_rs_edi_applicable(self, move): |
| 9 |
return move.l10n_rs_edi_is_eligible |
| 10 |
|
| 11 |
def _get_all_extra_edis(self) -> dict: |
| 12 |
# EXTENDS 'account' |
| 13 |
res = super()._get_all_extra_edis() |
| 14 |
res.update({'rs_edi': {'label': 'eFaktura', 'is_applicable': self._is_rs_edi_applicable, 'help': 'Send the E-Invoice to Government via eFaktura'}}) |
| 15 |
res.update({'rs_cir_checkbox': {'is_applicable': self._is_rs_edi_applicable, 'label': _("Send to CIR"), 'help': _("Send to Central Invoice Register(For B2G and the public sector)")}}) |
| 16 |
return res |
| 17 |
|
| 18 |
@api.model |
| 19 |
def _get_invoice_extra_attachments(self, invoice): |
| 20 |
# EXTENDS 'account' |
| 21 |
return super()._get_invoice_extra_attachments(invoice) + invoice.l10n_rs_edi_attachment_id |
| 22 |
|
| 23 |
@api.model |
| 24 |
def _call_web_service_after_invoice_pdf_render(self, invoices_data): |
| 25 |
# EXTENDS 'account' |
| 26 |
super()._call_web_service_after_invoice_pdf_render(invoices_data) |
| 27 |
for invoice, invoice_data in invoices_data.items(): |
| 28 |
# Not all invoices may need EDI. |
| 29 |
if 'rs_edi' not in invoice_data['extra_edis']: |
| 30 |
continue |
| 31 |
if not invoice.company_id.l10n_rs_edi_api_key: |
| 32 |
invoice_data["error"] = { |
| 33 |
"error_title": _("eFaktura API Key is missing."), |
| 34 |
"errors": [_("Please configure the eFaktura API Key in the company settings.")], |
| 35 |
} |
| 36 |
continue |
| 37 |
send_to_cir = 'rs_cir_checkbox' in invoice_data['extra_edis'] |
| 38 |
xml, error = invoice._l10n_rs_edi_send(send_to_cir) |
| 39 |
if error: |
| 40 |
invoice_data["error"] = { |
| 41 |
"error_title": _("Errors when submitting the e-invoice to eFaktura:"), |
| 42 |
"errors": [error], |
| 43 |
} |
| 44 |
continue |
| 45 |
invoice_data['l10n_rs_edi_attachment_values'] = invoice._l10n_rs_edi_get_attachment_values(xml) |
| 46 |
|
| 47 |
if self._can_commit(): |
| 48 |
self.env.cr.commit() |
| 49 |
|
| 50 |
@api.model |
| 51 |
def _link_invoice_documents(self, invoices_data): |
| 52 |
# EXTENDS 'account' |
| 53 |
super()._link_invoice_documents(invoices_data) |
| 54 |
attachments_vals = [ |
| 55 |
invoice_data.get('l10n_rs_edi_attachment_values') |
| 56 |
for invoice_data in invoices_data.values() |
| 57 |
if invoice_data.get('l10n_rs_edi_attachment_values') |
| 58 |
] |
| 59 |
if attachments_vals: |
| 60 |
attachments = self.env['ir.attachment'].with_user(SUPERUSER_ID).create(attachments_vals) |
| 61 |
res_ids = [attachment.res_id for attachment in attachments] |
| 62 |
self.env['account.move'].browse(res_ids).invalidate_recordset(fnames=['l10n_rs_edi_attachment_id', 'l10n_rs_edi_attachment_file']) |