Debian Codegraph @

addons/l10n_hr_edi/tests/test_mer_api.py

raw
1 """
2 These are LIVE tests of the MojEracun API (demo server).
3 Early inbox documents used for direct testing: 3084664, 3084663, 3084656, 3083666, 3082259
4 # First two have broken taxes, last two have a proper HR tax
5 # All five have scuffed company setup
6 """
7
8 from odoo import Command
9 from odoo.exceptions import UserError
10 from odoo.tests import tagged
11
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 from ..tools import (
15 _mer_api_query_document_process_status_outbox,
16 _mer_api_receive_document,
17 )
18
19
20 @tagged('external', 'external_l10n', 'post_install', '-post_install_l10n', '-at_install', '-standard')
21 class TestL10nHrEdiMerApi(TestL10nHrEdiCommon, TestAccountMoveSendCommon):
22
23 @classmethod
24 def setUpClass(cls):
25 super().setUpClass()
26
27 cls.env.company.write({
28 'l10n_hr_mer_connection_mode': 'test',
29 'l10n_hr_mer_username': '12513',
30 'l10n_hr_mer_password': 'AeXLoJf9Ld',
31 'l10n_hr_mer_company_ident': 'BE0477472701',
32 'l10n_hr_mer_software_ident': 'Test-002',
33 })
34 cls.env.company._l10n_hr_activate_mojeracun()
35
36 def _test_mer_api_send(self):
37 """
38 Test sending generated invoice through MER, bypassing most of the `account.move.send` flow.
39 WARNING: permanently changes the state of the demo server!
40 """
41 self.setup_partner_as_hr(self.env.company.partner_id)
42 self.setup_partner_as_mirror(self.partner_a)
43 tax = self.env['account.chart.template'].ref('VAT_S_IN_ROC_25')
44
45 invoice = self.env['account.move'].create({
46 'move_type': 'out_invoice',
47 'partner_id': self.partner_a.id,
48 'invoice_date': '2025-01-01',
49 'invoice_line_ids': [
50 Command.create({
51 'product_id': self.product_a.id,
52 'price_unit': 100.0,
53 'tax_ids': [Command.set(tax.ids)],
54 }),
55 ],
56 })
57 invoice.action_post()
58 invoice.l10n_hr_edi_addendum_id = self.env['l10n_hr_edi.addendum'].create({
59 'move_id': invoice.id,
60 'invoice_sending_time': '2025-01-02',
61 'fiscalization_number': self.env['account.move']._get_l10n_hr_fiscalization_number(invoice.name),
62 })
63
64 moves_data = {move.sudo(): {**self.env['account.move.send']._get_default_sending_settings(move)} for move in [invoice]}
65 self.env['account.move.send']._generate_invoice_documents(moves_data, allow_fallback_pdf=False)
66
67 self.assertRecordValues(invoice, [{
68 'l10n_hr_mer_document_status': '20',
69 }])
70 self.assertNotEqual(invoice.l10n_hr_mer_document_eid, False)
71
72 def _test_mer_flow_send(self):
73 """
74 Test sending generated invoice through MER using the normal `account.move.send` flow.
75 WARNING: permanently changes the state of the demo server!
76 """
77 self.setup_partner_as_hr(self.env.company.partner_id)
78 self.setup_partner_as_mirror(self.partner_a)
79 tax = self.env['account.chart.template'].ref('VAT_S_IN_ROC_25')
80
81 invoice = self.env['account.move'].create({
82 'move_type': 'out_invoice',
83 'partner_id': self.partner_a.id,
84 'invoice_date': '2025-01-01',
85 'invoice_line_ids': [
86 Command.create({
87 'product_id': self.product_a.id,
88 'price_unit': 100.0,
89 'tax_ids': [Command.set(tax.ids)],
90 }),
91 ],
92 })
93 invoice.action_post()
94 invoice.l10n_hr_edi_addendum_id = self.env['l10n_hr_edi.addendum'].create({
95 'move_id': invoice.id,
96 'invoice_sending_time': '2025-01-02',
97 'fiscalization_number': self.env['account.move']._get_l10n_hr_fiscalization_number(invoice.name),
98 })
99
100 send_and_print = self.create_send_and_print(invoice)
101 send_and_print._generate_and_send_invoices(invoice)
102
103 self.assertRecordValues(invoice, [{
104 'l10n_hr_mer_document_status': '20',
105 }])
106 self.assertNotEqual(invoice.l10n_hr_mer_document_eid, False)
107
108 def test_mer_api_query_document_process_status_outbox(self):
109 """
110 Test querying MER for statuse of an outbox document.
111 """
112 electronic_id = '3120260'
113 response = _mer_api_query_document_process_status_outbox(
114 self.env.company,
115 electronic_id=electronic_id,
116 status_id=None,
117 invoice_year=None,
118 invoice_number=None,
119 date_from=None,
120 date_to=None,
121 by_update_date=None
122 )
123 self.assertEqual(response[0], {
124 'ElectronicId': 3120260,
125 'DocumentNr': '3/1/1',
126 'DocumentTypeId': 1,
127 'DocumentTypeName': 'Račun',
128 'StatusId': 40,
129 'StatusName': 'Preuzet',
130 'OutboundFiscalizationStatus': None,
131 'ReceiverBusinessNumber': 'BE0477472701',
132 'ReceiverBusinessUnit': '',
133 'ReceiverBusinessName': 'Odoo S.A.',
134 'Created': '2025-12-11T13:06:20.3779916',
135 'Updated': '2025-12-11T13:08:42.2070931',
136 'IssueDate': '2025-12-11T00:00:00',
137 'Sent': '2025-12-11T13:06:20.7546287',
138 'Delivered': '2025-12-11T13:08:42.207087',
139 'DocumentProcessStatusId': 4,
140 'DocumentProcessStatusName': 'Potvrda zaprimanja',
141 'AdditionalDokumentStatusId': None,
142 'RejectReason': None
143 })
144
145 def test_mer_api_receive(self):
146 """
147 Test recieving a document with known MER ElectronicId from the MER system and importing it.
148 Invoices sent to the demo server are returned as vendor bills.
149 """
150 electronic_id = '3120260'
151 response = _mer_api_receive_document(
152 self.env.company,
153 electronic_id=electronic_id
154 )
155 self.assertEqual(response[:100], b'<?xml version="1.0" encoding="Utf-8" standalone="no"?><Invoice xmlns="urn:oasis:names:specification:')
156 attachment = self.env["ir.attachment"].create({
157 "name": f'mojeracun_{electronic_id}_attachment.xml',
158 "raw": response,
159 "type": "binary",
160 "mimetype": "application/xml",
161 })
162 move = self.env.company._l10n_hr_mer_import_invoice(
163 attachment,
164 {
165 'mer_document_eid': electronic_id,
166 'mer_document_status': '40',
167 'business_document_status': '0',
168 'fiscalization_status': '0',
169 'fiscalization_channel_type': '0',
170 }
171 )
172 self.assertEqual(
173 [
174 move.invoice_date.strftime("%Y-%m-%d"),
175 move.partner_id.name,
176 move.amount_total,
177 move.invoice_line_ids.product_id.name,
178 move.l10n_hr_process_type,
179 move.l10n_hr_fiscalization_number,
180 move.line_ids[0].tax_ids,
181 move.line_ids[0].l10n_hr_kpd_category_id.name,
182 ],
183 [
184 '2025-12-11',
185 'HR Company',
186 4000.0,
187 'Large Cabinet',
188 'P1',
189 '3/1/1',
190 self.env['account.chart.template'].ref('VAT_P_IN_ROC_25'), # The tax is flipped to purchase
191 '31.00.12',
192 ]
193 )
194
195 # Currently would break because of random test responses from fiscalization API, relies on a test exemption in the code
196 def test_mer_flow_get_new_documents(self):
197 """
198 Test the flow of getting new documents from the MER server.
199 For testing purposes, this is set up to fetch all the documents present rather than only
200 the 'new' ones, allowing the test to be rerun successfully with no server changes,
201 and to not notify MER of successful document import.
202 Only a select number of 'trusted' documents are imported to avoid dragging the test out.
203 """
204 self.env.company._l10n_hr_mer_get_new_documents(
205 undelivered_only=False,
206 slc=(10, 20), # Documents 3107515, 3110637, 3110651, 3111902, 3111911, 3114870, 3120196, 3120260, 3120276, 3120289
207 )[self.env.company.id]
208 moves = self.env['account.move'].search([('l10n_hr_mer_document_eid', '!=', False)])
209 for move in moves:
210 self.assertNotEqual(move.invoice_date, False)
211 self.assertNotEqual(move.partner_id, False)
212 self.assertNotEqual(move.amount_total, 0.0)
213 self.assertNotEqual(move.invoice_line_ids.product_id, False)
214 self.assertNotEqual(move.line_ids, False)
215 self.assertNotEqual(move.l10n_hr_process_type, False)
216 self.assertNotEqual(move.l10n_hr_fiscalization_number, False)
217 self.assertEqual(any(line.l10n_hr_kpd_category_id for line in move.line_ids), True)
218
219 # Currently results in random responses form the test server, cannot be set up to be tested properly
220 # NEEDS TO BE REWRITTEN FOR THE UPDATED PAYMENT FLOW
221 def test_mer_flow_report_payment(self):
222 """
223 Test reporting a payment for a document within the MER system.
224 First create a partial payment for the document, then add the remaining payment.
225 WARNING: permanently changes the state of the demo server!
226 """
227 electronic_id = '3084663'
228 response = _mer_api_receive_document(
229 self.env.company,
230 electronic_id=electronic_id
231 )
232 attachment = self.env["ir.attachment"].create({
233 "name": f'mojeracun_{electronic_id}_attachment.xml',
234 "raw": response,
235 "type": "binary",
236 "mimetype": "application/xml",
237 })
238 move = self.env.company._l10n_hr_mer_import_invoice(
239 attachment,
240 {
241 'mer_document_eid': electronic_id,
242 'mer_document_status': '40',
243 'business_document_status': '0',
244 'fiscalization_status': '0',
245 'fiscalization_channel_type': '0',
246 }
247 )
248 move.action_post()
249
250 self.env['account.payment.register'].with_context(active_model='account.move', active_ids=move.ids).create({
251 'amount': 25,
252 'payment_date': move.date,
253 })._create_payments()
254 # We can't actually check anything other than the action failing in a predictable way
255 # because test server responce for this API endpoint is random
256 try:
257 move.l10n_hr_edi_mer_action_report_paid()
258 except UserError as e:
259 if 'Error handling request:' in e.args:
260 pass
261 self.env['account.payment.register'].with_context(active_model='account.move', active_ids=move.ids).create({
262 'amount': 100,
263 'payment_date': move.date,
264 })._create_payments()
265 try:
266 move.l10n_hr_edi_mer_action_report_paid()
267 except UserError as e:
268 if 'Error handling request:' in e.args:
269 pass