| 1 |
""" |
| 2 |
MojEracun version of the proxy user model, set up as an AbstractModel as the data is held on res_company. |
| 3 |
The integration requires a lot of adjustments to work directly with an extrenal service provier and the original |
| 4 |
edi_proxy_user cannot be used as a basis as it is too closely tied to Odoo's own IAP server structure. |
| 5 |
""" |
| 6 |
|
| 7 |
import logging |
| 8 |
import requests |
| 9 |
from json import JSONDecodeError |
| 10 |
|
| 11 |
from odoo.exceptions import UserError |
| 12 |
|
| 13 |
_logger = logging.getLogger(__name__) |
| 14 |
TIMEOUT = 30 |
| 15 |
|
| 16 |
|
| 17 |
class MojEracunServiceError(Exception): |
| 18 |
|
| 19 |
def __init__(self, code, message=False): |
| 20 |
self.code = code |
| 21 |
self.message = message |
| 22 |
super().__init__(message or code) |
| 23 |
|
| 24 |
|
| 25 |
# ------------------------------------------------------------------------- |
| 26 |
# HELPER METHODS |
| 27 |
# ------------------------------------------------------------------------- |
| 28 |
|
| 29 |
def _get_server_url(company, edi_mode=None): |
| 30 |
edi_mode = edi_mode or company.l10n_hr_mer_connection_mode |
| 31 |
urls = { |
| 32 |
'prod': 'https://www.moj-eracun.hr', |
| 33 |
'test': 'https://demo.moj-eracun.hr', |
| 34 |
'demo': '???', |
| 35 |
} |
| 36 |
return urls[edi_mode] |
| 37 |
|
| 38 |
|
| 39 |
def _make_request(company, endpoint_type, params=False): |
| 40 |
""" |
| 41 |
Returns: |
| 42 |
For multiple document endpoints (such as query*): list of dicts |
| 43 |
For single document endpoints (such as markPaid): dict |
| 44 |
For receive specifically: bytestring |
| 45 |
""" |
| 46 |
endpoints = { |
| 47 |
'send': '/apis/v2/send', |
| 48 |
'query_inbox': '/apis/v2/queryInbox', |
| 49 |
'receive': '/apis/v2/receive', |
| 50 |
'update_status': '/apis/v2/UpdateDokumentProcessStatus', |
| 51 |
'query_status_inbox': '/apis/v2/queryDocumentProcessStatusInbox', |
| 52 |
'query_status_outbox': '/apis/v2/queryDocumentProcessStatusOutbox', |
| 53 |
'notify_import': False, # Includes eID and has to be handled separately |
| 54 |
'mark_paid': '/api/fiscalization/markPaid', |
| 55 |
'reject': '/api/fiscalization/reject', |
| 56 |
'fisc_outbox': '/api/fiscalization/statusOutbox', |
| 57 |
'fisc_inbox': '/api/fiscalization/statusInbox', |
| 58 |
} |
| 59 |
endpoint = f"/apis/v2/notifyimport/{params.pop('eid')}" if endpoint_type == 'notify_import' else endpoints.get(endpoint_type) |
| 60 |
if not endpoint: |
| 61 |
raise MojEracunServiceError('Invalid API endpoint') |
| 62 |
payload = params |
| 63 |
url = f"{_get_server_url(company)}{endpoint}" |
| 64 |
|
| 65 |
# Last barrier : in case the demo mode is not handled by the caller, we block access. |
| 66 |
if company.l10n_hr_mer_connection_mode == 'demo': |
| 67 |
raise MojEracunServiceError("block_demo_mode", "Can't access the proxy in demo mode") |
| 68 |
|
| 69 |
try: |
| 70 |
response = requests.post( |
| 71 |
url, |
| 72 |
json=payload, |
| 73 |
timeout=TIMEOUT, |
| 74 |
headers={'content-type': 'application/json', 'charset': 'utf-8'} |
| 75 |
) |
| 76 |
except (ValueError, requests.exceptions.ConnectionError, requests.exceptions.MissingSchema, requests.exceptions.Timeout, requests.exceptions.HTTPError): |
| 77 |
raise MojEracunServiceError('connection_error', |
| 78 |
company.env._('The url that this service requested returned an error. The url it tried to contact was %s', url)) |
| 79 |
|
| 80 |
# Structure-specific error handling |
| 81 |
if response.status_code != 200: |
| 82 |
try: |
| 83 |
error_message = response.json() |
| 84 |
error_message = error_message.get('errors') or error_message.get('message') |
| 85 |
except (JSONDecodeError, TypeError): |
| 86 |
error_message = False |
| 87 |
raise UserError(company.env._("Error handling request: %s", error_message) if error_message else company.env._("HTTP %s: Connection error.", response.status_code)) |
| 88 |
|
| 89 |
if endpoint != endpoints['receive']: |
| 90 |
try: |
| 91 |
response_json = response.json() |
| 92 |
except (JSONDecodeError, TypeError): |
| 93 |
raise MojEracunServiceError('Invalid response format received') |
| 94 |
if 'error' in response_json: |
| 95 |
message = company.env._('The url that this service requested returned an error. The url it tried to contact was %(url)s. %(error_message)s', url=url, error_message=response_json['error']['message']) |
| 96 |
if response_json['error']['code'] == 404: |
| 97 |
message = company.env._('The url that this service tried to contact does not exist. The url was “%s”', url) |
| 98 |
raise MojEracunServiceError('connection_error', message) |
| 99 |
return response_json |
| 100 |
else: |
| 101 |
return response.content |
| 102 |
|
| 103 |
|
| 104 |
def _call_mer_service(company, endpoint, params=None): |
| 105 |
params_base = { |
| 106 |
'Username': company.l10n_hr_mer_username, |
| 107 |
'Password': company.l10n_hr_mer_password, |
| 108 |
'CompanyId': company.l10n_hr_mer_company_ident, |
| 109 |
'CompanyBu': company.partner_id.l10n_hr_business_unit_code, |
| 110 |
'SoftwareId': company.l10n_hr_mer_software_ident, |
| 111 |
} |
| 112 |
if params: |
| 113 |
params = params_base | params |
| 114 |
else: |
| 115 |
params = params_base |
| 116 |
params_to_delete = [] |
| 117 |
for key, value in params.items(): |
| 118 |
if not value: |
| 119 |
params_to_delete.append(key) |
| 120 |
for key in params_to_delete: |
| 121 |
params.pop(key) |
| 122 |
try: |
| 123 |
response = _make_request( |
| 124 |
company, |
| 125 |
endpoint, |
| 126 |
params=params, |
| 127 |
) |
| 128 |
except MojEracunServiceError as e: |
| 129 |
raise UserError(e) |
| 130 |
|
| 131 |
return response |
| 132 |
|
| 133 |
|
| 134 |
# ------------------------------------------------------------------------- |
| 135 |
# MOJERACUN API CALL METHODS |
| 136 |
# ------------------------------------------------------------------------- |
| 137 |
|
| 138 |
def _mer_api_send(company, xml_file): |
| 139 |
""" |
| 140 |
Send electronic document to a recipient. |
| 141 |
""" |
| 142 |
params = { |
| 143 |
'File': xml_file, |
| 144 |
} |
| 145 |
response_dict = _call_mer_service(company, 'send', params=params) |
| 146 |
return response_dict |
| 147 |
|
| 148 |
|
| 149 |
def _mer_api_query_inbox(company, filter=None, electronic_id=None, status_id=None, date_from=None, date_to=None): |
| 150 |
""" |
| 151 |
Status description. Query methods are refered as basic MER document statuses. |
| 152 |
When receiving response from query methods, you will get croatian names of statuses |
| 153 |
(U obradi, Poslan, Preuzet, Povučeno preuzimanje, Neuspjelo). |
| 154 |
""" |
| 155 |
params = { |
| 156 |
'Filter': filter, |
| 157 |
'ElectronicId': electronic_id, |
| 158 |
'StatusId': status_id, |
| 159 |
'From': date_from, |
| 160 |
'To': date_to, |
| 161 |
} |
| 162 |
response_list = _call_mer_service(company, 'query_inbox', params=params) |
| 163 |
return response_list |
| 164 |
|
| 165 |
|
| 166 |
def _mer_api_receive_document(company, electronic_id): |
| 167 |
""" |
| 168 |
Receive method is used for downloading documents. Both sent and incoming, eg. Inbox and Outbox documents. |
| 169 |
""" |
| 170 |
params = { |
| 171 |
'ElectronicId': electronic_id, |
| 172 |
} |
| 173 |
response_bstr = _call_mer_service(company, 'receive', params=params) |
| 174 |
if response_bstr[:5] != b'<?xml': |
| 175 |
raise MojEracunServiceError('service_error', company.env._("Failed to retrieve document XML for ElectronicId: %s", electronic_id)) |
| 176 |
return response_bstr |
| 177 |
|
| 178 |
|
| 179 |
def _mer_api_update_document_process_status(company, electronic_id, status_id, rejection_reason=None): |
| 180 |
""" |
| 181 |
Document process status codes are used to update status of document after it has been |
| 182 |
downloaded or received in the system of the another information provider / access points. |
| 183 |
They are also referred as business document statuses. |
| 184 |
Statuses 4 and 99 cannot be modified via API. |
| 185 |
""" |
| 186 |
params = { |
| 187 |
'ElectronicId': electronic_id, |
| 188 |
'StatusId': status_id, |
| 189 |
'RejectReason': rejection_reason, |
| 190 |
} |
| 191 |
response_dict = _call_mer_service(company, 'update_status', params=params) |
| 192 |
return response_dict |
| 193 |
|
| 194 |
|
| 195 |
def _mer_api_query_document_process_status_inbox(company, electronic_id=None, status_id=None, invoice_year=None, invoice_number=None, date_from=None, date_to=None, by_update_date=None): |
| 196 |
""" |
| 197 |
Query inbox is used to discover new documents sent to your company or business unit. For this method, the API returns 20.000 results. |
| 198 |
""" |
| 199 |
params = { |
| 200 |
'ElectronicId': electronic_id, |
| 201 |
'StatusId': status_id, |
| 202 |
'InvoiceYear': invoice_year, |
| 203 |
'InvoiceNumber': invoice_number, |
| 204 |
'DateFrom': date_from, |
| 205 |
'DateTo': date_to, |
| 206 |
'ByUpdateDate': by_update_date, |
| 207 |
} |
| 208 |
response_list = _call_mer_service(company, 'query_status_inbox', params=params) |
| 209 |
return response_list |
| 210 |
|
| 211 |
|
| 212 |
def _mer_api_query_document_process_status_outbox(company, electronic_id=None, status_id=None, invoice_year=None, invoice_number=None, date_from=None, date_to=None, by_update_date=None): |
| 213 |
""" |
| 214 |
Query inbox is used to discover new documents sent to your company or business unit. For this method, the API returns 20.000 results. |
| 215 |
""" |
| 216 |
params = { |
| 217 |
'ElectronicId': electronic_id, |
| 218 |
'StatusId': status_id, |
| 219 |
'InvoiceYear': invoice_year, |
| 220 |
'InvoiceNumber': invoice_number, |
| 221 |
'DateFrom': date_from, |
| 222 |
'DateTo': date_to, |
| 223 |
'ByUpdateDate': by_update_date, |
| 224 |
} |
| 225 |
response_list = _call_mer_service(company, 'query_status_outbox', params=params) |
| 226 |
return response_list |
| 227 |
|
| 228 |
|
| 229 |
def _mer_api_notify_import(company, electronic_id): |
| 230 |
""" |
| 231 |
Notify import method is used for sending an information that an invoice is imported into an ERP. |
| 232 |
You can use it to update which document you have successfully imported and to make procedure for |
| 233 |
importing only documents that you previously did not download and import. |
| 234 |
""" |
| 235 |
response_dict = _call_mer_service(company, 'notify_import', params={'eid': electronic_id}) |
| 236 |
return response_dict |
| 237 |
|
| 238 |
|
| 239 |
def _mer_api_mark_paid(company, electronic_id, payment_date, payment_amount, payment_method): |
| 240 |
""" |
| 241 |
Methods for sending payment information for sent documents. |
| 242 |
""" |
| 243 |
params = { |
| 244 |
'ElectronicId': electronic_id, |
| 245 |
'PaymentDate': payment_date, |
| 246 |
'PaymentAmount': payment_amount, |
| 247 |
'PaymentMethod': payment_method, |
| 248 |
} |
| 249 |
response_dict = _call_mer_service(company, 'mark_paid', params=params) |
| 250 |
return response_dict |
| 251 |
|
| 252 |
|
| 253 |
def _mer_api_reject_with_id(company, electronic_id, rejection_date, rejection_type, rejection_desc): |
| 254 |
""" |
| 255 |
Methods for sending information regarding rejection for received electronic documents. |
| 256 |
""" |
| 257 |
params = { |
| 258 |
'ElectronicId': electronic_id, |
| 259 |
'RejectionDate': rejection_date, |
| 260 |
'RejectionReasonType': rejection_type, |
| 261 |
'RejectionReasonDescription': rejection_desc, |
| 262 |
} |
| 263 |
response_dict = _call_mer_service(company, 'reject', params=params) |
| 264 |
return response_dict |
| 265 |
|
| 266 |
|
| 267 |
def _mer_api_check_fiscalization_status_outbox(company, electronic_id=False, message_type=False, date_from=False, date_to=False, by_update_date=False, request_id=False, status=False): |
| 268 |
""" |
| 269 |
This endpoint retrieves the fiscalization status of a document using its ElectronicId and MessageType. |
| 270 |
""" |
| 271 |
params = { |
| 272 |
'ElectronicId': electronic_id, |
| 273 |
'MessageType': message_type, |
| 274 |
'DateFrom': date_from, |
| 275 |
'DateTo': date_to, |
| 276 |
'ByUpdateDate': by_update_date, |
| 277 |
'FiscalizationRequestID': request_id, |
| 278 |
'Status': status, |
| 279 |
} |
| 280 |
response_list = _call_mer_service(company, 'fisc_outbox', params=params) |
| 281 |
return response_list |
| 282 |
|
| 283 |
|
| 284 |
def _mer_api_check_fiscalization_status_inbox(company, electronic_id=False, message_type=False, date_from=False, date_to=False, by_update_date=False, request_id=False, status=False): |
| 285 |
""" |
| 286 |
This endpoint retrieves the fiscalization status of a document using its ElectronicId and MessageType. |
| 287 |
""" |
| 288 |
params = { |
| 289 |
'ElectronicId': electronic_id, |
| 290 |
'MessageType': message_type, |
| 291 |
'DateFrom': date_from, |
| 292 |
'DateTo': date_to, |
| 293 |
'ByUpdateDate': by_update_date, |
| 294 |
'FiscalizationRequestID': request_id, |
| 295 |
'Status': status, |
| 296 |
} |
| 297 |
response_list = _call_mer_service(company, 'fisc_inbox', params=params) |
| 298 |
return response_list |