Debian Codegraph @

addons/l10n_es_edi_verifactu/tests/test_json.py

raw symbols SCIP
1 import datetime
2 import json
3
4 from freezegun import freeze_time
5
6 from odoo import Command
7 from odoo.tests import tagged
8 from .common import TestL10nEsEdiVerifactuCommon
9
10
11 @tagged('post_install_l10n', 'post_install', '-at_install')
12 class TestL10nEsEdiVerifactuJson(TestL10nEsEdiVerifactuCommon):
13
14 @classmethod
15 def setUpClass(cls):
16 super().setUpClass()
17 cls.fakenow = datetime.datetime(2024, 12, 5)
18 cls.startClassPatcher(freeze_time(cls.fakenow))
19 # `freeze_time` does not change the `create_date`
20 cls.startClassPatcher(cls._mock_create_date(cls, '2024-12-05'))
21
22 def test_huella_generation(self):
23 """
24 Test cases taken from the examples in the AEAT documentation about fingerprint ("huella") generation:
25 'Detalle de las especificaciones técnicas para generación de la huella o hash de los registros de facturación'
26 """
27 render_values = {
28 'cancellation': False,
29 'record_type': 'RegistroAlta',
30
31 'RegistroAlta': {
32 'IDFactura': {
33 'IDEmisorFactura': " 89890001K ",
34 'NumSerieFactura': " 12345678/G33 ",
35 'FechaExpedicionFactura': " 01-01-2024 ",
36 },
37 'TipoFactura': " F1 ",
38 'CuotaTotal': " 12.35 ",
39 'ImporteTotal': " 123.45 ",
40 'Encadenamiento': {
41 'PrimerRegistro': 'S',
42 },
43 'FechaHoraHusoGenRegistro': " 2024-01-01T19:20:30+01:00 ",
44 },
45 }
46 fingerprint = self.env['l10n_es_edi_verifactu.document']._fingerprint(render_values)
47 self.assertEqual(fingerprint, "3C464DAF61ACB827C65FDA19F352A4E3BDC2C640E9E9FC4CC058073F38F12F60")
48
49 render_values = {
50 'cancellation': False,
51 'record_type': 'RegistroAlta',
52
53 'RegistroAlta': {
54 'IDFactura': {
55 'IDEmisorFactura': " 89890001K ",
56 'NumSerieFactura': " 12345679/G34 ",
57 'FechaExpedicionFactura': " 01-01-2024 ",
58 },
59 'TipoFactura': " F1 ",
60 'CuotaTotal': " 12.35 ",
61 'ImporteTotal': " 123.45 ",
62 'Encadenamiento': {
63 'RegistroAnterior': {
64 'Huella': "3C464DAF61ACB827C65FDA19F352A4E3BDC2C640E9E9FC4CC058073F38F12F60",
65 },
66 },
67 'FechaHoraHusoGenRegistro': " 2024-01-01T19:20:35+01:00 ",
68 },
69 }
70 fingerprint = self.env['l10n_es_edi_verifactu.document']._fingerprint(render_values)
71 self.assertEqual(fingerprint, "F7B94CFD8924EDFF273501B01EE5153E4CE8F259766F88CF6ACB8935802A2B97")
72
73 render_values = {
74 'cancellation': True,
75 'record_type': 'RegistroAnulacion',
76
77 'RegistroAnulacion': {
78 'IDFactura': {
79 'IDEmisorFacturaAnulada': " 89890001K ",
80 'NumSerieFacturaAnulada': " 12345679/G34 ",
81 'FechaExpedicionFacturaAnulada': " 01-01-2024 ",
82 },
83 'Encadenamiento': {
84 'RegistroAnterior': {
85 'Huella': "F7B94CFD8924EDFF273501B01EE5153E4CE8F259766F88CF6ACB8935802A2B97",
86 },
87 },
88 'FechaHoraHusoGenRegistro': " 2024-01-01T19:20:40+01:00 ",
89 },
90 }
91 fingerprint = self.env['l10n_es_edi_verifactu.document']._fingerprint(render_values)
92 self.assertEqual(fingerprint, "177547C0D57AC74748561D054A9CEC14B4C4EA23D1BEFD6F2E69E3A388F90C68")
93
94 def test_invoice_1_and_credit_note(self):
95 invoice = self.env['account.move'].create({
96 'move_type': 'out_invoice',
97 'invoice_date': '2019-01-30',
98 'date': '2019-01-30',
99 'partner_id': self.partner_b.id, # Spanish customer
100 'invoice_line_ids': [
101 Command.create({'product_id': self.product_1.id, 'price_unit': 100.0, 'tax_ids': [Command.set(self.tax21_goods.ids)]}),
102 Command.create({'product_id': self.product_1.id, 'price_unit': 200.0, 'tax_ids': [Command.set(self.tax21_services.ids)]}),
103 Command.create({'product_id': self.product_1.id, 'price_unit': 1000.0, 'tax_ids': [Command.set(self.tax10_goods.ids)]}),
104 ],
105 })
106 invoice.action_post()
107
108 with self._mock_last_document(None):
109 document = invoice._l10n_es_edi_verifactu_create_documents()[invoice]
110 self.assertFalse(document.errors)
111 with self._mock_zeep_registration_operation_certificate_issue():
112 batch_dict, _info = document._send_as_batch()
113 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_1.json'))
114
115 # Test the credit note
116 self.env['account.move.reversal'].with_company(self.company).create(
117 {
118 'move_ids': [Command.set((invoice.id,))],
119 'date': '2019-02-10',
120 'journal_id': invoice.journal_id.id,
121 'l10n_es_edi_verifactu_refund_reason': 'R1',
122 }
123 ).reverse_moves()
124 credit_note = invoice.reversal_move_ids
125 credit_note.invoice_date = '2019-02-11'
126 credit_note.action_post()
127
128 with self._mock_last_document(None):
129 document = credit_note._l10n_es_edi_verifactu_create_documents()[credit_note]
130 self.assertFalse(document.errors)
131 with self._mock_zeep_registration_operation_certificate_issue():
132 batch_dict, _info = document._send_as_batch()
133 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_1_credit_note.json'))
134
135 def test_invoice_1_and_substitution(self):
136 invoice = self.env['account.move'].create({
137 'move_type': 'out_invoice',
138 'invoice_date': '2019-01-30',
139 'date': '2019-01-30',
140 'partner_id': self.partner_b.id, # Spanish customer
141 'invoice_line_ids': [
142 Command.create({'product_id': self.product_1.id, 'price_unit': 100.0, 'tax_ids': [Command.set(self.tax21_goods.ids)]}),
143 Command.create({'product_id': self.product_1.id, 'price_unit': 200.0, 'tax_ids': [Command.set(self.tax21_services.ids)]}),
144 Command.create({'product_id': self.product_1.id, 'price_unit': 1000.0, 'tax_ids': [Command.set(self.tax10_goods.ids)]}),
145 ],
146 })
147 invoice.action_post()
148
149 with self._mock_last_document(None):
150 document = invoice._l10n_es_edi_verifactu_create_documents()[invoice]
151 self.assertFalse(document.errors)
152 with self._mock_zeep_registration_operation_certificate_issue():
153 batch_dict, _info = document._send_as_batch()
154 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_1.json'))
155
156 # Create a reversal and a substitution move
157 self.env['account.move.reversal'].with_company(self.company).create(
158 {
159 'move_ids': [Command.set((invoice.id,))],
160 'date': '2019-02-10',
161 'journal_id': invoice.journal_id.id,
162 # By default:
163 # 'l10n_es_edi_verifactu_refund_reason': 'R1',
164 }
165 ).reverse_moves(is_modify=True)
166
167 credit_note = invoice.reversal_move_ids
168 with self._mock_last_document(None):
169 document = credit_note._l10n_es_edi_verifactu_create_documents()[credit_note]
170 self.assertFalse(document.errors)
171 with self._mock_zeep_registration_operation_certificate_issue():
172 batch_dict, _info = document._send_as_batch()
173 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_1_reversal_for_substitution.json'))
174
175 substitution_move = invoice.l10n_es_edi_verifactu_substitution_move_ids
176 substitution_move.invoice_line_ids[0].price_unit = 50
177 substitution_move.invoice_date = '2019-02-11'
178 substitution_move.action_post()
179 with self._mock_last_document(None):
180 document = substitution_move._l10n_es_edi_verifactu_create_documents()[substitution_move]
181 self.assertFalse(document.errors)
182 with self._mock_zeep_registration_operation_certificate_issue():
183 batch_dict, _info = document._send_as_batch()
184 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_1_correction_substitution.json'))
185
186 def test_invoice_2(self):
187 """
188 I.e. test that the following are handled correctly
189 * Recargo de equivalencia taxes
190 * 'FechaOperacion' field (set as `delivery_date` in case it is different from the `invoice_date`)
191 """
192 invoice = self.env['account.move'].create({
193 'move_type': 'out_invoice',
194 'invoice_date': '2019-01-30',
195 'delivery_date': '2019-02-01',
196 'date': '2019-01-30',
197 'partner_id': self.partner_b.id, # Spanish customer
198 'invoice_line_ids': [
199 Command.create({'product_id': self.product_1.id, 'price_unit': 100.0, 'tax_ids': [Command.set((self.tax10_goods + self.tax1p4_services_recargo).ids)]}),
200 Command.create({'product_id': self.product_1.id, 'price_unit': 200.0, 'tax_ids': [Command.set((self.tax21_services + self.tax5p2_services_recargo).ids)]}),
201 ],
202 })
203 invoice.action_post()
204
205 with self._mock_last_document(None):
206 document = invoice._l10n_es_edi_verifactu_create_documents()[invoice]
207 self.assertFalse(document.errors)
208 with self._mock_zeep_registration_operation_certificate_issue():
209 batch_dict, _info = document._send_as_batch()
210 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_2.json'))
211
212 def test_invoice_3(self):
213 """
214 Test withholding / retention taxes (taxes with `l10n_es_type` 'retencion').
215 * We need to ignore them in the generation of the XML
216 * We need ignore them for the total in the QR code
217 """
218 invoice = self.env['account.move'].create({
219 'move_type': 'out_invoice',
220 'invoice_date': '2019-01-30',
221 'date': '2019-01-30',
222 'partner_id': self.partner_b.id, # Spanish customer
223 'invoice_line_ids': [
224 Command.create({
225 'product_id': self.product_1.id,
226 'price_unit': 1000.0,
227 'tax_ids': [Command.set((self.tax10_goods + self.tax1_withholding).ids)]
228 }),
229 ],
230 })
231 invoice.action_post()
232 self.assertEqual(invoice.amount_total, 1090.0)
233
234 with self._mock_last_document(None):
235 document = invoice._l10n_es_edi_verifactu_create_documents()[invoice]
236 self.assertFalse(document.errors)
237
238 expected_qr_code_url = '/report/barcode/?barcode_type=QR&value=https%3A%2F%2Fprewww2.aeat.es%2Fwlpl%2FTIKE-CONT%2FValidarQR%3Fnif%3DA39200019%26numserie%3DINV%252F2019%252F00001%26fecha%3D30-01-2019%26importe%3D1100.00&barLevel=M&width=180&height=180'
239 self.assertEqual(invoice.l10n_es_edi_verifactu_qr_code, expected_qr_code_url)
240
241 with self._mock_zeep_registration_operation_certificate_issue():
242 batch_dict, _info = document._send_as_batch()
243 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_3.json'))
244
245 def test_invoice_4(self):
246 """
247 Test 0% and exempt taxes (i.e. taxes with `l10n_es_type` 'no_sujeto', 'no_sujeto_loc', 'exento').
248 """
249 invoice = self.env['account.move'].create({
250 'move_type': 'out_invoice',
251 'invoice_date': '2019-01-30',
252 'date': '2019-01-30',
253 'partner_id': self.partner_b.id, # Spanish customer
254 'invoice_line_ids': [
255 Command.create({
256 'product_id': self.product_1.id,
257 'price_unit': 1000.0,
258 'tax_ids': [Command.set(self.tax0_no_sujeto_loc.ids)]
259 }),
260 Command.create({
261 'product_id': self.product_1.id,
262 'price_unit': 100.0,
263 'tax_ids': [Command.set(self.tax0_exento.ids)]
264 }),
265 Command.create({
266 'product_id': self.product_1.id,
267 'price_unit': 10.0,
268 'tax_ids': [Command.set(self.tax0_no_sujeto.ids)]
269 }),
270 Command.create({
271 'product_id': self.product_1.id,
272 'price_unit': 1.0,
273 'tax_ids': [Command.set(self.tax0_isp.ids)]
274 }),
275 ],
276 })
277 invoice.action_post()
278 self.assertEqual(invoice.amount_total, 1111.0)
279
280 with self._mock_last_document(None):
281 document = invoice._l10n_es_edi_verifactu_create_documents()[invoice]
282 self.assertFalse(document.errors)
283
284 with self._mock_zeep_registration_operation_certificate_issue():
285 batch_dict, _info = document._send_as_batch()
286 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_4.json'))
287
288 def test_invoice_export(self):
289 """
290 Test the Clave Regimen for exports
291 """
292 invoice = self.env['account.move'].create({
293 'move_type': 'out_invoice',
294 'invoice_date': '2019-01-30',
295 'delivery_date': '2019-02-01',
296 'date': '2019-01-30',
297 'partner_id': self.partner_a.id, # Belgian customer
298 'invoice_line_ids': [
299 Command.create({'product_id': self.product_1.id, 'price_unit': 100.0, 'tax_ids': [Command.set(self.tax0_exento_export.ids)]}),
300 ],
301 })
302 invoice.action_post()
303
304 with self._mock_last_document(None):
305 document = invoice._l10n_es_edi_verifactu_create_documents()[invoice]
306 self.assertFalse(document.errors)
307 with self._mock_zeep_registration_operation_certificate_issue():
308 batch_dict, _info = document._send_as_batch()
309 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_export.json'))
310
311 def test_invoice_multicurrency_1(self):
312 invoice = self.env['account.move'].create({
313 'move_type': 'out_invoice',
314 'invoice_date': '2019-01-30',
315 'date': '2019-01-30',
316 'currency_id': self.other_currency.id,
317 'partner_id': self.partner_a.id, # Belgian customer
318 'invoice_line_ids': [
319 Command.create({'product_id': self.product_1.id, 'price_unit': 100.0, 'tax_ids': [Command.set(self.tax21_goods.ids)]}),
320 Command.create({'product_id': self.product_1.id, 'price_unit': 200.0, 'tax_ids': [Command.set(self.tax21_services.ids)]}),
321 Command.create({'product_id': self.product_1.id, 'price_unit': 1000.0, 'tax_ids': [Command.set(self.tax10_goods.ids)]}),
322 ],
323 })
324 invoice.action_post()
325
326 with self._mock_last_document(None):
327 document = invoice._l10n_es_edi_verifactu_create_documents()[invoice]
328 self.assertFalse(document.errors)
329 with self._mock_zeep_registration_operation_certificate_issue():
330 batch_dict, _info = document._send_as_batch()
331 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_multi_currency_1.json'))
332
333 def test_multiple_invoices_with_predecessor(self):
334 invoices = self.env['account.move'].create([
335 {
336 'move_type': 'out_invoice',
337 'invoice_date': '2024-12-30',
338 'date': '2019-01-30',
339 'name': 'INV/2019/00026',
340 'partner_id': self.partner_b.id, # Spanish customer
341 'invoice_line_ids': [
342 Command.create({'product_id': self.product_1.id, 'price_unit': 100.0, 'tax_ids': [Command.set(self.tax21_goods.ids)]}),
343 Command.create({'product_id': self.product_1.id, 'price_unit': 200.0, 'tax_ids': [Command.set(self.tax21_services.ids)]}),
344 Command.create({'product_id': self.product_1.id, 'price_unit': 1000.0, 'tax_ids': [Command.set(self.tax10_goods.ids)]}),
345 ],
346 },
347 {
348 'move_type': 'out_invoice',
349 'invoice_date': '2019-02-01',
350 'date': '2019-01-30',
351 'partner_id': self.partner_b.id, # Spanish customer
352 'invoice_line_ids': [
353 Command.create({'product_id': self.product_1.id, 'price_unit': 100.0, 'tax_ids': [Command.set(self.tax21_goods.ids)]}),
354 Command.create({'product_id': self.product_1.id, 'price_unit': 200.0, 'tax_ids': [Command.set(self.tax21_services.ids)]}),
355 Command.create({'product_id': self.product_1.id, 'price_unit': 1000.0, 'tax_ids': [Command.set(self.tax10_goods.ids)]}),
356 ],
357 },
358 {
359 'move_type': 'out_invoice',
360 'invoice_date': '2019-02-03',
361 'date': '2019-01-30',
362 'partner_id': self.partner_b.id, # Spanish customer
363 'invoice_line_ids': [
364 Command.create({'product_id': self.product_1.id, 'price_unit': 100.0, 'tax_ids': [Command.set(self.tax21_goods.ids)]}),
365 Command.create({'product_id': self.product_1.id, 'price_unit': 200.0, 'tax_ids': [Command.set(self.tax21_services.ids)]}),
366 Command.create({'product_id': self.product_1.id, 'price_unit': 1000.0, 'tax_ids': [Command.set(self.tax10_goods.ids)]}),
367 ],
368 },
369 ])
370 invoices.action_post()
371
372 # We create a dummy document for the record identifier only
373 dummy_start_document_dict = {
374 'RegistroAnulacion': {
375 'IDFactura': {
376 'IDEmisorFacturaAnulada': '59962470K',
377 'NumSerieFacturaAnulada': 'INV/2018/00001',
378 'FechaExpedicionFacturaAnulada': '01-01-2018',
379 },
380 'Huella': 'FA5DC48A0640BEB02A05160FD30020D1EA67FC1B400800ECDD9FC785E137C864',
381 'FechaHoraHusoGenRegistro': " 2024-01-01T19:20:30+01:00 ",
382 },
383 }
384 dummy_start_document = self.env['l10n_es_edi_verifactu.document'].sudo().create([{
385 'company_id': self.company.id,
386 'document_type': 'submission',
387 }])
388 dummy_start_document.sudo().json_attachment_id = self.env['ir.attachment'].sudo().create({
389 'raw': json.dumps(dummy_start_document_dict, indent=4).encode(),
390 'name': dummy_start_document.json_attachment_filename,
391 'res_id': dummy_start_document.id,
392 'res_model': dummy_start_document._name,
393 'mimetype': 'application/json',
394 })
395 with self._mock_last_document(dummy_start_document):
396 document1_reversed = invoices[0]._l10n_es_edi_verifactu_create_documents()[invoices[0]]
397 self.assertFalse(document1_reversed.errors)
398 # Register the invoice so that we can create a cancellation for it
399 with self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_accepted_registration.json'):
400 batch_dict, _info = document1_reversed._send_as_batch()
401 self.assertEqual(invoices[0].l10n_es_edi_verifactu_state, 'accepted')
402
403 # We do not use `_mock_last_document` in the following to check that it works w/o mocking
404 document0 = invoices[1]._l10n_es_edi_verifactu_create_documents()[invoices[1]]
405 self.assertFalse(document0.errors)
406 document1 = invoices[0]._l10n_es_edi_verifactu_create_documents(cancellation=True)[invoices[0]]
407 self.assertFalse(document1.errors)
408 document2 = invoices[2]._l10n_es_edi_verifactu_create_documents()[invoices[2]]
409 self.assertFalse(document2.errors)
410 documents = document0 + document1 + document2
411
412 with self._mock_zeep_registration_operation_certificate_issue():
413 batch_dict, _info = documents._send_as_batch()
414 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_multiple_invoices_with_predecessor.json'))
415
416 def test_invoice_cancellation_1(self):
417 invoice = self.env['account.move'].create({
418 'move_type': 'out_invoice',
419 'invoice_date': '2024-12-30',
420 'date': '2019-01-30',
421 'name': 'INV/2019/00026',
422 'partner_id': self.partner_b.id, # Spanish customer
423 'invoice_line_ids': [
424 Command.create({'product_id': self.product_1.id, 'price_unit': 100.0, 'tax_ids': [Command.set(self.tax21_goods.ids)]}),
425 Command.create({'product_id': self.product_1.id, 'price_unit': 200.0, 'tax_ids': [Command.set(self.tax21_services.ids)]}),
426 Command.create({'product_id': self.product_1.id, 'price_unit': 1000.0, 'tax_ids': [Command.set(self.tax10_goods.ids)]}),
427 ],
428 })
429 invoice.action_post()
430
431 with self._mock_last_document(None):
432 invoice_document = invoice._l10n_es_edi_verifactu_create_documents()[invoice]
433 self.assertFalse(invoice_document.errors)
434 with self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_accepted_registration.json'):
435 batch_dict, _info = invoice_document._send_as_batch()
436 self.assertEqual(invoice.l10n_es_edi_verifactu_state, 'accepted')
437
438 cancellation_document = invoice._l10n_es_edi_verifactu_create_documents(cancellation=True)[invoice]
439 self.assertFalse(cancellation_document.errors)
440 with self._mock_zeep_registration_operation_certificate_issue():
441 batch_dict, _info = cancellation_document._send_as_batch()
442 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_cancellation_1.json'))
443
444 def test_invoice_simplified_partner(self):
445 simplified_partner = self.env.ref('l10n_es.partner_simplified')
446 invoice = self.env['account.move'].create({
447 'move_type': 'out_invoice',
448 'invoice_date': '2019-01-30',
449 'date': '2019-01-30',
450 'partner_id': simplified_partner.id,
451 'invoice_line_ids': [
452 Command.create({'product_id': self.product_1.id, 'price_unit': 100.0, 'tax_ids': [Command.set(self.tax21_goods.ids)]}),
453 ],
454 })
455 invoice.action_post()
456
457 with self._mock_last_document(None):
458 document = invoice._l10n_es_edi_verifactu_create_documents()[invoice]
459 self.assertFalse(document.errors)
460 with self._mock_zeep_registration_operation_certificate_issue():
461 batch_dict, _info = document._send_as_batch()
462 self.assertEqual(batch_dict, self._json_file_to_dict('l10n_es_edi_verifactu/tests/files/test_invoice_simplified_partner.json'))