| 1 |
from contextlib import contextmanager |
| 2 |
import json |
| 3 |
from lxml import etree |
| 4 |
import requests |
| 5 |
|
| 6 |
from odoo import Command, fields |
| 7 |
from odoo.exceptions import UserError |
| 8 |
from odoo.tests import tagged |
| 9 |
from odoo.tools import file_open |
| 10 |
|
| 11 |
from odoo.addons.account.tests.common import PatchRequestsMixin |
| 12 |
from odoo.addons.account.tests.test_account_move_send import TestAccountMoveSendCommon |
| 13 |
from odoo.addons.l10n_hr_edi.tests.test_hr_edi_common import TestL10nHrEdiCommon |
| 14 |
|
| 15 |
|
| 16 |
@tagged('post_install_l10n', 'post_install', '-at_install') |
| 17 |
class TestHrEdiFlowsMocked(TestL10nHrEdiCommon, TestAccountMoveSendCommon, PatchRequestsMixin): |
| 18 |
|
| 19 |
@classmethod |
| 20 |
def setUpClass(cls): |
| 21 |
super().setUpClass() |
| 22 |
company = cls.company_data['company'] |
| 23 |
company.write({ |
| 24 |
'l10n_hr_mer_username': '...', |
| 25 |
'l10n_hr_mer_password': '...', |
| 26 |
'l10n_hr_mer_company_ident': '12345678', |
| 27 |
'l10n_hr_mer_software_ident': 'Saodoo-001', |
| 28 |
'l10n_hr_mer_connection_state': 'active', |
| 29 |
}) |
| 30 |
|
| 31 |
def assertRequestsEqual(self, actual_request, expected_request): |
| 32 |
""" Override so that when we check the arguments to `requests.Session.request`, |
| 33 |
we use `assertXmlTreeEqual` on the XML documents passed as argument, to benefit |
| 34 |
from ___ignore___ and better error messages. |
| 35 |
""" |
| 36 |
|
| 37 |
if ( |
| 38 |
(actual_file := actual_request.get('json', {}).get('File')) |
| 39 |
and (expected_file := expected_request.get('json', {}).get('File')) |
| 40 |
): |
| 41 |
@contextmanager |
| 42 |
def restore_files(): |
| 43 |
try: |
| 44 |
actual_request['json']['File'] = expected_request['json']['File'] = 'Placeholder for comparison' |
| 45 |
yield |
| 46 |
finally: |
| 47 |
actual_request['json']['File'] = actual_file |
| 48 |
expected_request['json']['File'] = expected_file |
| 49 |
|
| 50 |
with restore_files(): |
| 51 |
super().assertRequestsEqual(actual_request, expected_request) |
| 52 |
self.assertXmlTreeEqual( |
| 53 |
etree.fromstring(actual_file.encode()), |
| 54 |
etree.fromstring(expected_file.encode()) |
| 55 |
) |
| 56 |
else: |
| 57 |
super().assertRequestsEqual(actual_request, expected_request) |
| 58 |
|
| 59 |
# ------------------------------------------------------------------------- |
| 60 |
# Helpers |
| 61 |
# ------------------------------------------------------------------------- |
| 62 |
|
| 63 |
def _get_base_url(self): |
| 64 |
return 'https://demo.moj-eracun.hr' |
| 65 |
|
| 66 |
def _build_request(self, endpoint, request_args=None): |
| 67 |
return { |
| 68 |
'method': 'post', |
| 69 |
'url': self._get_base_url() + endpoint, |
| 70 |
'headers': {'charset': 'utf-8', 'content-type': 'application/json'}, |
| 71 |
'data': None, |
| 72 |
'json': { |
| 73 |
'Username': self.env.company.l10n_hr_mer_username, |
| 74 |
'Password': self.env.company.l10n_hr_mer_password, |
| 75 |
'CompanyId': self.env.company.l10n_hr_mer_company_ident, |
| 76 |
'SoftwareId': self.env.company.l10n_hr_mer_software_ident, |
| 77 |
**(request_args or {}), |
| 78 |
}, |
| 79 |
'timeout': 30, |
| 80 |
} |
| 81 |
|
| 82 |
def _build_response(self, status_code, response_json=None, response_content=None): |
| 83 |
response = requests.Response() |
| 84 |
response.status_code = status_code |
| 85 |
if response_json is not None: |
| 86 |
response._content = json.dumps(response_json).encode() |
| 87 |
elif response_content is not None: |
| 88 |
response._content = response_content |
| 89 |
return response |
| 90 |
|
| 91 |
# ------------------------------------------------------------------------- |
| 92 |
# Sending flow |
| 93 |
# ------------------------------------------------------------------------- |
| 94 |
|
| 95 |
def test_10_send_invoice(self): |
| 96 |
self.setup_partner_as_hr(self.env.company.partner_id) |
| 97 |
self.setup_partner_as_hr_alt(self.partner_a) |
| 98 |
tax = self.env['account.chart.template'].ref('VAT_S_IN_ROC_25') |
| 99 |
|
| 100 |
# 1. Create invoice |
| 101 |
|
| 102 |
invoice = self.env['account.move'].create({ |
| 103 |
'invoice_date': '2025-01-01', |
| 104 |
'date': '2025-01-01', |
| 105 |
'move_type': 'out_invoice', |
| 106 |
'partner_id': self.partner_a.id, |
| 107 |
'invoice_line_ids': [ |
| 108 |
Command.create({ |
| 109 |
'product_id': self.product_a.id, |
| 110 |
'price_unit': 100.0, |
| 111 |
'tax_ids': [Command.set(tax.ids)], |
| 112 |
}), |
| 113 |
], |
| 114 |
}) |
| 115 |
invoice.action_post() |
| 116 |
|
| 117 |
self.assertRecordValues(invoice, [{ |
| 118 |
'l10n_hr_process_type': 'P1', |
| 119 |
'l10n_hr_fiscal_user_id': self.env.user.partner_id.id, |
| 120 |
'l10n_hr_operator_name': 'Because I am accountman!', |
| 121 |
'l10n_hr_operator_oib': '01234567896', |
| 122 |
}]) |
| 123 |
|
| 124 |
# 2. Send invoice to MER |
| 125 |
|
| 126 |
send_and_print = self.create_send_and_print(invoice) |
| 127 |
|
| 128 |
with file_open('l10n_hr_edi/tests/flows/out_invoice.xml', 'r') as f: |
| 129 |
expected_invoice_xml = f.read() |
| 130 |
|
| 131 |
with self.assertRequests([ |
| 132 |
( |
| 133 |
# Request 1: Send invoice |
| 134 |
self._build_request( |
| 135 |
endpoint='/apis/v2/send', |
| 136 |
request_args={ |
| 137 |
'File': expected_invoice_xml, |
| 138 |
}, |
| 139 |
), |
| 140 |
self._build_response( |
| 141 |
status_code=200, |
| 142 |
response_json={ |
| 143 |
'ElectronicId': '3083666', |
| 144 |
'DocumentNr': invoice.name, |
| 145 |
'DocumentTypeId': 1, |
| 146 |
'DocumentTypeName': 'Račun', |
| 147 |
'StatusId': 20, |
| 148 |
'StatusName': 'Obrađen', |
| 149 |
'RecipientBusinessNumber': 'BE0477472701', |
| 150 |
'RecipientBusinessUnit': '', |
| 151 |
'RecipientBusinessName': 'Odoo S.A.', |
| 152 |
'Created': '2025-10-14T14:27:06.2388492+02:00', |
| 153 |
'Sent': '2025-10-14T14:27:06.3355877+02:00', |
| 154 |
'Modified': '2025-10-14T14:27:06.3355877+02:00', |
| 155 |
'Delivered': None, |
| 156 |
} |
| 157 |
), |
| 158 |
), |
| 159 |
]): |
| 160 |
send_and_print._generate_and_send_invoices(invoice) |
| 161 |
|
| 162 |
self.assertRecordValues(invoice, [{ |
| 163 |
'l10n_hr_mer_document_status': '20', |
| 164 |
'l10n_hr_mer_document_eid': '3083666', |
| 165 |
}]) |
| 166 |
|
| 167 |
def test_send_invoice_error(self): |
| 168 |
self.setup_partner_as_hr(self.env.company.partner_id) |
| 169 |
self.setup_partner_as_hr_alt(self.partner_a) |
| 170 |
tax = self.env['account.chart.template'].ref('VAT_S_IN_ROC_25') |
| 171 |
|
| 172 |
# 1. Create invoice |
| 173 |
|
| 174 |
invoice = self.env['account.move'].create({ |
| 175 |
'invoice_date': '2025-01-01', |
| 176 |
'date': '2025-01-01', |
| 177 |
'move_type': 'out_invoice', |
| 178 |
'partner_id': self.partner_a.id, |
| 179 |
'invoice_line_ids': [ |
| 180 |
Command.create({ |
| 181 |
'product_id': self.product_a.id, |
| 182 |
'price_unit': 100.0, |
| 183 |
'tax_ids': [Command.set(tax.ids)], |
| 184 |
}), |
| 185 |
], |
| 186 |
}) |
| 187 |
invoice.action_post() |
| 188 |
|
| 189 |
self.assertRecordValues(invoice, [{ |
| 190 |
'l10n_hr_process_type': 'P1', |
| 191 |
'l10n_hr_fiscal_user_id': self.env.user.partner_id.id, |
| 192 |
'l10n_hr_operator_name': 'Because I am accountman!', |
| 193 |
'l10n_hr_operator_oib': '01234567896', |
| 194 |
}]) |
| 195 |
|
| 196 |
# 2. Send invoice to MER |
| 197 |
|
| 198 |
send_and_print = self.create_send_and_print(invoice) |
| 199 |
|
| 200 |
with file_open('l10n_hr_edi/tests/flows/out_invoice.xml', 'r') as f: |
| 201 |
expected_invoice_xml = f.read() |
| 202 |
|
| 203 |
with self.assertRaisesRegex(UserError, 'Korisničko ime i lozinka nisu ispravni.. Trace ID: 4f701362-96cc-49c6-a297-854e740ad719.'): |
| 204 |
with self.assertRequests([ |
| 205 |
( |
| 206 |
# Request 1: Send invoice |
| 207 |
self._build_request( |
| 208 |
endpoint='/apis/v2/send', |
| 209 |
request_args={ |
| 210 |
'File': expected_invoice_xml, |
| 211 |
}, |
| 212 |
), |
| 213 |
self._build_response( |
| 214 |
status_code=200, |
| 215 |
response_json={ |
| 216 |
'Username': |
| 217 |
{ |
| 218 |
'Value': 'Incorrect', |
| 219 |
'Messages': ['Korisničko ime i lozinka nisu ispravni.. Trace ID: 4f701362-96cc-49c6-a297-854e740ad719.'] |
| 220 |
} |
| 221 |
} |
| 222 |
), |
| 223 |
), |
| 224 |
]): |
| 225 |
send_and_print._generate_and_send_invoices(invoice) |
| 226 |
|
| 227 |
# ------------------------------------------------------------------------- |
| 228 |
# Receiving flow |
| 229 |
# ------------------------------------------------------------------------- |
| 230 |
|
| 231 |
def test_20_receive_invoice(self): |
| 232 |
self.setup_partner_as_hr(self.env.company.partner_id) |
| 233 |
|
| 234 |
# 1. Fetch inbound invoices |
| 235 |
|
| 236 |
with file_open('l10n_hr_edi/tests/flows/in_invoice.xml', 'rb') as f: |
| 237 |
mocked_in_invoice_bytes = f.read() |
| 238 |
|
| 239 |
with self.assertRequests([ |
| 240 |
( |
| 241 |
# Request 1: queryInbox |
| 242 |
self._build_request(endpoint='/apis/v2/queryInbox'), |
| 243 |
self._build_response( |
| 244 |
status_code=200, |
| 245 |
response_json=[{ |
| 246 |
'ElectronicId': 3148006, |
| 247 |
'DocumentNr': '2/1/1', |
| 248 |
'DocumentTypeId': 1, |
| 249 |
'DocumentTypeName': 'Račun', |
| 250 |
'StatusId': 40, |
| 251 |
'StatusName': 'Preuzet', |
| 252 |
'SenderBusinessNumber': 'BE0477472701', |
| 253 |
'SenderBusinessUnit': None, |
| 254 |
'SenderBusinessName': 'Odoo S.A.', |
| 255 |
'Updated': '2025-12-29T09:37:04.5605292', |
| 256 |
'Sent': '2025-12-24T10:31:17.5989975', |
| 257 |
'Delivered': '2025-12-29T09:37:04.5605231', |
| 258 |
'Imported': False |
| 259 |
}] |
| 260 |
) |
| 261 |
), |
| 262 |
( |
| 263 |
# Request 2: statusInbox |
| 264 |
self._build_request( |
| 265 |
endpoint='/api/fiscalization/statusInbox', |
| 266 |
request_args={ |
| 267 |
'ElectronicId': '3148006', |
| 268 |
} |
| 269 |
), |
| 270 |
self._build_response( |
| 271 |
status_code=200, |
| 272 |
response_json=[{ |
| 273 |
'electronicId': 762506406, |
| 274 |
'senderName': 'Mock d.o.o.', |
| 275 |
'senderIdentifierValue': '12345678901', |
| 276 |
'channelType': 1, |
| 277 |
'channelTypeDescription': 'eIzvještavanje', |
| 278 |
'businessStatusReason': 'Mock business status reason', |
| 279 |
'messages': [{ |
| 280 |
'status': 0, |
| 281 |
'statusDescription': 'Uspjeh', |
| 282 |
'fiscalizationRequestId': '73743778-d386-466b-938f-020831fa9092', |
| 283 |
'dateOfFiscalization': '2025-12-29T16:15:29.3745898+01:00', |
| 284 |
'message': 'Mock fiscalization successful', |
| 285 |
'encodedXml': 'TW9jayBYTUwgY29udGVudCBmb3IgZmlzY2FsaXphdGlvbiBzdGF0dXMgcmVzcG9uc2U=', |
| 286 |
'errorCode': 'S001', |
| 287 |
'errorCodeDescription': 'Sistemska greška prilikom obrade zahtjeva', |
| 288 |
'messageType': 0, |
| 289 |
'messageTypeDescription': 'Kao POŠILJATELJ dohvati status fiskalizacijske poruke' |
| 290 |
}] |
| 291 |
}] |
| 292 |
) |
| 293 |
), |
| 294 |
( |
| 295 |
# Request 3: queryDocumentProcessStatusInbox |
| 296 |
self._build_request( |
| 297 |
endpoint='/apis/v2/queryDocumentProcessStatusInbox', |
| 298 |
request_args={ |
| 299 |
'ElectronicId': '3148006', |
| 300 |
} |
| 301 |
), |
| 302 |
self._build_response( |
| 303 |
status_code=200, |
| 304 |
response_json=[{ |
| 305 |
'ElectronicId': 3148006, |
| 306 |
'DocumentNr': '2/1/1', |
| 307 |
'DocumentTypeId': 1, |
| 308 |
'DocumentTypeName': 'Račun', |
| 309 |
'StatusId': 40, |
| 310 |
'StatusName': 'Preuzet', |
| 311 |
'SenderBusinessNumber': 'BE0477472701', |
| 312 |
'SenderBusinessUnit': None, |
| 313 |
'SenderBusinessName': 'Odoo S.A.', |
| 314 |
'Sent': '2025-12-24T10:31:17.5989975', |
| 315 |
'Delivered': '2025-12-29T09:37:04.5605231', |
| 316 |
'IssueDate': '2025-12-24T00:00:00', |
| 317 |
'DocumentProcessStatusId': 4, |
| 318 |
'DocumentProcessStatusName': 'Potvrda zaprimanja', |
| 319 |
'AdditionalDokumentStatusId': None, |
| 320 |
'RejectReason': None, |
| 321 |
'InboundFiscalizationStatus': None |
| 322 |
}] |
| 323 |
) |
| 324 |
), |
| 325 |
( |
| 326 |
# Request 4: receive |
| 327 |
self._build_request( |
| 328 |
endpoint='/apis/v2/receive', |
| 329 |
request_args={ |
| 330 |
'ElectronicId': '3148006', |
| 331 |
} |
| 332 |
), |
| 333 |
self._build_response( |
| 334 |
status_code=200, |
| 335 |
response_content=mocked_in_invoice_bytes |
| 336 |
) |
| 337 |
), |
| 338 |
]): |
| 339 |
self.company_data['default_journal_purchase'].l10n_hr_mer_get_new_documents_all() |
| 340 |
|
| 341 |
fetched_invoice = self.env['account.move'].search([('l10n_hr_mer_document_eid', '=', '3148006')]) |
| 342 |
|
| 343 |
self.assertRecordValues(fetched_invoice, [{ |
| 344 |
'ref': '2/1/1', |
| 345 |
'payment_reference': 'HR00 211', |
| 346 |
'invoice_date': fields.Date.from_string('2025-12-24'), |
| 347 |
'invoice_date_due': fields.Date.from_string('2025-12-24'), |
| 348 |
'amount_untaxed': 47.0, |
| 349 |
'amount_tax': 6.11, |
| 350 |
'amount_total': 53.11, |
| 351 |
'l10n_hr_mer_document_eid': '3148006', |
| 352 |
'l10n_hr_mer_document_status': '40', |
| 353 |
'l10n_hr_process_type': 'P1', |
| 354 |
'l10n_hr_business_document_status': '4', |
| 355 |
'l10n_hr_fiscalization_number': '2/1/1', |
| 356 |
'l10n_hr_fiscalization_status': '0', |
| 357 |
'l10n_hr_fiscalization_error': 'S001 - Sistemska greška prilikom obrade zahtjeva', |
| 358 |
'l10n_hr_fiscalization_request': '73743778-d386-466b-938f-020831fa9092', |
| 359 |
'l10n_hr_fiscalization_channel_type': '1', |
| 360 |
}]) |
| 361 |
|
| 362 |
self.assertRecordValues(fetched_invoice.partner_id, [{ |
| 363 |
'name': 'HR Company', |
| 364 |
'vat': 'HR18724543544', |
| 365 |
'street': 'Vukasi', |
| 366 |
'city': 'Ježdovec', |
| 367 |
'zip': '10250', |
| 368 |
'country_id': self.env.ref('base.hr').id, |
| 369 |
}]) |
| 370 |
|
| 371 |
self.assertRecordValues(fetched_invoice.invoice_line_ids, [{ |
| 372 |
'name': '[E-COM10] Pedal Bin', |
| 373 |
'quantity': 1.0, |
| 374 |
'price_unit': 47.0, |
| 375 |
}]) |
| 376 |
|
| 377 |
self.assertEqual(fetched_invoice.invoice_line_ids.tax_ids.amount, 13.0) |