| 1 |
import uuid |
| 2 |
from json import JSONDecodeError |
| 3 |
import requests |
| 4 |
|
| 5 |
from odoo import _, api, fields, models |
| 6 |
from requests.exceptions import Timeout, ConnectionError, HTTPError |
| 7 |
|
| 8 |
DEMO_EFAKTURA_URL = 'https://demoefaktura.mfin.gov.rs/api/publicApi/sales-invoice/ubl' |
| 9 |
EFAKTURA_URL = 'https://efakturadev.mfin.gov.rs/api/publicApi/sales-invoice/ubl' |
| 10 |
|
| 11 |
|
| 12 |
class AccountMove(models.Model): |
| 13 |
_inherit = 'account.move' |
| 14 |
|
| 15 |
l10n_rs_edi_uuid = fields.Char( |
| 16 |
string="RS Invoice UUID", |
| 17 |
compute="_compute_l10n_rs_edi_uuid", |
| 18 |
copy=False, |
| 19 |
store=True, |
| 20 |
help="Unique Identifier for an invoice used as request id", |
| 21 |
) |
| 22 |
|
| 23 |
l10n_rs_edi_is_eligible = fields.Boolean( |
| 24 |
compute="_compute_l10n_rs_edi_is_eligible", |
| 25 |
store=True, |
| 26 |
help="Technical field to determine if this invoice is eligible to be e-invoiced.", |
| 27 |
) |
| 28 |
|
| 29 |
l10n_rs_edi_attachment_file = fields.Binary( |
| 30 |
string="Serbian E-Invoice XML File", |
| 31 |
copy=False, |
| 32 |
attachment=True, |
| 33 |
help="Serbia: technical field holding the e-invoice XML data.", |
| 34 |
) |
| 35 |
|
| 36 |
l10n_rs_edi_attachment_id = fields.Many2one( |
| 37 |
comodel_name='ir.attachment', |
| 38 |
string="eFaktura XML Attachment", |
| 39 |
compute=lambda self: self._compute_linked_attachment_id('l10n_rs_edi_attachment_id', 'l10n_rs_edi_attachment_file'), |
| 40 |
depends=['l10n_rs_edi_attachment_file'], |
| 41 |
) |
| 42 |
|
| 43 |
l10n_rs_edi_state = fields.Selection( |
| 44 |
string="Serbia E-Invoice state", |
| 45 |
selection=[ |
| 46 |
('sent', 'Sent'), |
| 47 |
('sending_failed', 'Error'), |
| 48 |
], |
| 49 |
tracking=True, |
| 50 |
readonly=True, |
| 51 |
copy=False, |
| 52 |
) |
| 53 |
|
| 54 |
l10n_rs_edi_error = fields.Text( |
| 55 |
string="Serbia E-Invoice error", |
| 56 |
copy=False, |
| 57 |
readonly=True, |
| 58 |
) |
| 59 |
|
| 60 |
l10n_rs_tax_date_obligations_code = fields.Selection( |
| 61 |
string="Tax Date Obligations", |
| 62 |
selection=[ |
| 63 |
('35', 'By Delivery Date'), |
| 64 |
('3', 'By Issuance Date'), |
| 65 |
('432', 'By Billing System'), |
| 66 |
], |
| 67 |
store=True, |
| 68 |
readonly=False, |
| 69 |
compute='_compute_l10n_rs_tax_date_obligations_code', |
| 70 |
) |
| 71 |
l10n_rs_edi_invoice = fields.Char(string="Invoice Id", copy=False) |
| 72 |
l10n_rs_edi_sales_invoice = fields.Char(string="Sales Invoice Id", copy=False) |
| 73 |
l10n_rs_edi_purchase_invoice = fields.Char(string="Purchase Invoice Id", copy=False) |
| 74 |
|
| 75 |
@api.depends('country_code', 'move_type') |
| 76 |
def _compute_show_delivery_date(self): |
| 77 |
# EXTENDS 'account' |
| 78 |
super()._compute_show_delivery_date() |
| 79 |
for move in self: |
| 80 |
if move.l10n_rs_edi_is_eligible: |
| 81 |
move.show_delivery_date = True |
| 82 |
|
| 83 |
@api.depends("country_code", "move_type") |
| 84 |
def _compute_l10n_rs_edi_is_eligible(self): |
| 85 |
self.fetch(["country_code"]) |
| 86 |
for move in self: |
| 87 |
move.l10n_rs_edi_is_eligible = move.country_code == 'RS' and move.is_sale_document() and move.l10n_rs_edi_state in (False, 'sending_failed') |
| 88 |
|
| 89 |
@api.depends('country_code') |
| 90 |
def _compute_l10n_rs_tax_date_obligations_code(self): |
| 91 |
for move in self: |
| 92 |
if move.country_code == 'RS': |
| 93 |
move.l10n_rs_tax_date_obligations_code = '3' |
| 94 |
|
| 95 |
@api.depends('l10n_rs_edi_state') |
| 96 |
def _compute_show_reset_to_draft_button(self): |
| 97 |
# EXTENDS 'account' |
| 98 |
super()._compute_show_reset_to_draft_button() |
| 99 |
for move in self: |
| 100 |
if move.show_reset_to_draft_button and move.l10n_rs_edi_state == 'sent': |
| 101 |
move.show_reset_to_draft_button = False |
| 102 |
|
| 103 |
@api.depends('l10n_rs_edi_is_eligible') |
| 104 |
def _compute_l10n_rs_edi_uuid(self): |
| 105 |
for move in self: |
| 106 |
if move.l10n_rs_edi_is_eligible and not move.l10n_rs_edi_uuid: |
| 107 |
move.l10n_rs_edi_uuid = uuid.uuid4() |
| 108 |
|
| 109 |
def button_draft(self): |
| 110 |
# EXTENDS 'account' |
| 111 |
self.write({ |
| 112 |
"l10n_rs_edi_error": False, |
| 113 |
"l10n_rs_edi_state": False, |
| 114 |
}) |
| 115 |
return super().button_draft() |
| 116 |
|
| 117 |
def _l10n_rs_edi_send(self, send_to_cir): |
| 118 |
self.ensure_one() |
| 119 |
self.env['res.company']._with_locked_records(self) |
| 120 |
xml, errors = self.env['account.edi.xml.ubl.rs']._export_invoice(self) |
| 121 |
if errors: |
| 122 |
return xml, errors |
| 123 |
params = { |
| 124 |
'requestId': self.l10n_rs_edi_uuid, |
| 125 |
'sendToCir': 'Yes' if send_to_cir else 'No' |
| 126 |
} |
| 127 |
url = DEMO_EFAKTURA_URL if self.company_id.l10n_rs_edi_demo_env else EFAKTURA_URL |
| 128 |
headers = { |
| 129 |
'Content-Type': 'application/xml', |
| 130 |
'ApiKey': self.company_id.l10n_rs_edi_api_key, |
| 131 |
} |
| 132 |
error_message = False |
| 133 |
try: |
| 134 |
response = requests.post(url=url, params=params, headers=headers, data=xml, timeout=30) |
| 135 |
response.raise_for_status() |
| 136 |
except (Timeout, ConnectionError, HTTPError) as exception: |
| 137 |
error_message = _("There was a problem with the connection with eFaktura: %s", exception) |
| 138 |
self.message_post(body=error_message) |
| 139 |
return xml, error_message |
| 140 |
dict_response = {} |
| 141 |
try: |
| 142 |
dict_response = response.json() |
| 143 |
except JSONDecodeError as e: |
| 144 |
error_message = _("Invalid response from eFaktura: %s", str(e)) |
| 145 |
self.l10n_rs_edi_state = 'sending_failed' if error_message else 'sent' |
| 146 |
self.l10n_rs_edi_error = error_message |
| 147 |
self.l10n_rs_edi_invoice = dict_response.get('InvoiceId') |
| 148 |
self.l10n_rs_edi_purchase_invoice = dict_response.get('PurchaseInvoiceId') |
| 149 |
self.l10n_rs_edi_sales_invoice = dict_response.get('SalesInvoiceId') |
| 150 |
return xml, error_message |
| 151 |
|
| 152 |
def _l10n_rs_edi_get_attachment_values(self, xml): |
| 153 |
self.ensure_one() |
| 154 |
return { |
| 155 |
'name': self._l10n_rs_edi_get_xml_attachment_name(), |
| 156 |
'mimetype': 'application/xml', |
| 157 |
'description': _('RS E-Invoice: %s', self.move_type), |
| 158 |
'company_id': self.company_id.id, |
| 159 |
'res_id': self.id, |
| 160 |
'res_model': self._name, |
| 161 |
'res_field': 'l10n_rs_edi_attachment_file', |
| 162 |
'raw': xml, |
| 163 |
'type': 'binary', |
| 164 |
} |
| 165 |
|
| 166 |
def _l10n_rs_edi_get_xml_attachment_name(self): |
| 167 |
return f"{self.name.replace('/', '_')}_edi.xml" |