Debian Codegraph @

addons/l10n_jo_edi/tests/test_jo_edi_precision.py

raw
1 from odoo import Command
2 from odoo.tests import tagged
3 from odoo.tools import float_compare, float_round
4 from odoo.addons.l10n_jo_edi.tests.jo_edi_common import JoEdiCommon
5 from odoo.addons.l10n_jo_edi.models.account_edi_xml_ubl_21_jo import JO_MAX_DP
6
7
8 @tagged('post_install_l10n', 'post_install', '-at_install')
9 class TestJoEdiPrecision(JoEdiCommon):
10 def _equality_check(self, vals_dict, up_to_jo_max_dp=True):
11 def equal_strict(val1, val2):
12 return val1 == val2
13
14 def equal_jo_max_dp(val1, val2):
15 return float_compare(val1, val2, JO_MAX_DP) == 0
16
17 equals = equal_jo_max_dp if up_to_jo_max_dp else equal_strict
18 first_tuple = None
19 error_message = ""
20 for label, value in vals_dict.items():
21 if not first_tuple:
22 first_tuple = (label, value)
23 else:
24 if not equals(value, first_tuple[1]):
25 error_message += f"{label} ({value}) != {first_tuple[0]} ({first_tuple[1]})\n"
26 return error_message
27
28 def _extract_vals_from_subtotals(self, subtotals, defaults):
29 for subtotal in subtotals:
30 tax_percent = float(subtotal.findtext('{*}TaxCategory/{*}Percent', default=-1))
31 if tax_percent == -1: # special (fixed amount) tax
32 defaults.update({
33 'taxable_amount_special': float(subtotal.findtext('{*}TaxableAmount')),
34 'tax_amount_special': float(subtotal.findtext('{*}TaxAmount')),
35 })
36 defaults['total_tax_amount'] += defaults['tax_amount_special']
37 else:
38 defaults.update({
39 'taxable_amount_general': float(subtotal.findtext('{*}TaxableAmount')),
40 'tax_amount_general_subtotal': float(subtotal.findtext('{*}TaxAmount')),
41 'tax_percent': tax_percent / 100,
42 })
43 defaults['total_tax_amount'] += defaults['tax_amount_general_subtotal']
44
45 return defaults
46
47 def _round_max_dp(self, value):
48 return float_round(value, JO_MAX_DP)
49
50 def _sum_max_dp(self, iterable):
51 return sum(self._round_max_dp(element) for element in iterable)
52
53 def _validate_jo_edi_numbers(self, xml_string, invoice):
54 """
55 TLDR: This method checks that units sum up to total values.
56 ===================================================================================================
57 Problem statement:
58 When an EDI is submitted to JoFotara portal, multiple validations are executed to ensure invoice data integrity.
59 The most important ones of these validations are the following:-
60 --------------------------- ▼ invoice line level ▼ ---------------------------
61 1. line_extension_amount = (price_unit * quantity) - discount
62 2. taxable_amount = line_extension_amount
63 3. rounding_amount = line_extension_amount + general_tax_amount + special_tax_amount
64 --------------------------- ▼ invoice level ▼ ---------------------------------
65 4. tax_exclusive_amount = sum(price_unit * quantity)
66 5. tax_inclusive_amount = sum(price_unit * quantity - discount + general_tax_amount + special_tax_amount)
67 6. payable_amount = tax_inclusive_amount
68 -------------------------------------------------------------------------------
69 The JoFotara portal, however, has no tolerance with rounding errors up to 9 decimal places.
70 Hence, the reported values are expected to be up to 9 decimal places,
71 and the aggregated units should match reported totals up to 9 decimal places.
72 Moreover, reported totals have to equal (or at least be as close as possible) to totals stored in Odoo.
73 And since the JOD has precision of 3 decimal places, everything is stored in Odoo approximated to 3 decimal places.
74 -------------------------------------------------------------------------------
75 This method runs validations in a fashion similar to those running on the JoFotara portal.
76 It returns all the errors encountered as a string.
77 """
78 root = self.get_xml_tree_from_string(xml_string)
79 error_message = ""
80
81 total_discount = float(root.findtext('./{*}AllowanceCharge/{*}Amount') or 0.0)
82 total_tax = float(root.findtext('./{*}TaxTotal/{*}TaxAmount', default=0))
83
84 tax_exclusive_amount = float(root.findtext('./{*}LegalMonetaryTotal/{*}TaxExclusiveAmount'))
85 tax_inclusive_amount = float(root.findtext('./{*}LegalMonetaryTotal/{*}TaxInclusiveAmount'))
86 self.assertEqual(float_compare(tax_inclusive_amount, invoice.amount_total, 2), 0, f'{tax_inclusive_amount} != {invoice.amount_total}')
87 monetary_values_discount = float(root.findtext('./{*}LegalMonetaryTotal/{*}AllowanceTotalAmount') or 0.0)
88 payable_amount = float(root.findtext('./{*}LegalMonetaryTotal/{*}PayableAmount'))
89
90 error_message += self._equality_check({ # They have to be exactly the same, no decimal difference is tolerated
91 'Monetary Values discount': monetary_values_discount,
92 'Total Discount': total_discount,
93 }, up_to_jo_max_dp=False)
94 error_message += self._equality_check({ # They have to be exactly the same, no decimal difference is tolerated
95 'Payable Amount': payable_amount,
96 'Tax Inclusive Amount': tax_inclusive_amount,
97 }, up_to_jo_max_dp=False)
98
99 lines = []
100 for xml_line in root.findall('./{*}InvoiceLine'):
101 line_extension_amount = float(xml_line.findtext('{*}LineExtensionAmount'))
102 line = {
103 'id': xml_line.findtext('{*}ID'),
104 'quantity': float(xml_line.findtext('{*}InvoicedQuantity')),
105 'line_extension_amount': line_extension_amount,
106 'tax_amount_general': float(xml_line.findtext('{*}TaxTotal/{*}TaxAmount', default=0)),
107 'rounding_amount': float(xml_line.findtext('{*}TaxTotal/{*}RoundingAmount', default=line_extension_amount)), # defaults to line_extension_amount in the absence of taxes
108 **self._extract_vals_from_subtotals(
109 subtotals=xml_line.findall('{*}TaxTotal/{*}TaxSubtotal'),
110 defaults={
111 'taxable_amount_general': line_extension_amount,
112 'tax_amount_general_subtotal': 0,
113 'tax_percent': 0,
114 'taxable_amount_special': line_extension_amount,
115 'tax_amount_special': 0,
116 'total_tax_amount': 0,
117 }),
118 'price_unit': float(xml_line.findtext('{*}Price/{*}PriceAmount')),
119 'discount': float(xml_line.findtext('{*}Price/{*}AllowanceCharge/{*}Amount') or 0.0),
120 }
121 lines.append(line)
122 line_errors = self._equality_check({
123 # taxable_amount = line_extension_amount = price_unit * quantity - discount
124 'General Taxable Amount': line['taxable_amount_general'],
125 'Special Taxable Amount': line['taxable_amount_special'],
126 'Line Extension Amount': line['line_extension_amount'],
127 'Price Unit * Quantity - Discount': line['price_unit'] * line['quantity'] - line['discount'],
128 }) + self._equality_check({
129 # rounding_amount = line_extension_amount + total_tax_amount
130 'Rounding Amount': line['rounding_amount'],
131 'Line Extension Amount + Total Tax': line['line_extension_amount'] + line['total_tax_amount'],
132 }) + self._equality_check({
133 'General Tax Amount': line['tax_amount_general'],
134 'General Tax Amount in subtotal': line['tax_amount_general_subtotal'],
135 'Taxable Amount * Tax Percent': (line['taxable_amount_general'] + line['tax_amount_special']) * line['tax_percent'],
136 })
137 if line_errors:
138 error_message += f"Errors on the line {line['id']}\n"
139 error_message += line_errors
140 error_message += "-------------------------------------------------------------------------\n"
141
142 aggregated_tax_exclusive_amount = self._sum_max_dp(line['price_unit'] * line['quantity'] for line in lines)
143 aggregated_tax_inclusive_amount = self._sum_max_dp(line['price_unit'] * line['quantity'] - line['discount'] + line['total_tax_amount'] for line in lines)
144 aggregated_tax_amount = sum(line['tax_amount_general'] for line in lines)
145 aggregated_discount_amount = sum(line['discount'] for line in lines)
146
147 error_message += self._equality_check({
148 'Tax Exclusive Amount': tax_exclusive_amount,
149 'Aggregated Tax Exclusive Amount': aggregated_tax_exclusive_amount,
150 }) + self._equality_check({
151 'Tax Inclusive Amount': tax_inclusive_amount,
152 'Aggregated Tax Inclusive Amount': aggregated_tax_inclusive_amount,
153 'Tax Exclusive Amount - Total Discount + Total Tax': tax_exclusive_amount - total_discount + sum(line['total_tax_amount'] for line in lines),
154 }) + self._equality_check({
155 'Tax Amount': total_tax,
156 'Aggregated Tax Amount': aggregated_tax_amount,
157 }) + self._equality_check({
158 'Discount Amount': total_discount,
159 'Aggregated Discount Amount': aggregated_discount_amount,
160 })
161
162 return error_message
163
164 def _validate_invoice_vals_jo_edi_numbers(self, invoice_vals):
165 with self.subTest(sub_test_name=invoice_vals['name']):
166 invoice = self._l10n_jo_create_invoice(invoice_vals)
167 generated_file = self.env['account.edi.xml.ubl_21.jo']._export_invoice(invoice)[0]
168 errors = self._validate_jo_edi_numbers(generated_file, invoice)
169 self.assertFalse(errors, errors)
170
171 def test_jo_sales_invoice_precision(self):
172 eur = self.env.ref('base.EUR')
173 self.setup_currency_rate(eur, 1.41)
174 self.company.l10n_jo_edi_taxpayer_type = 'sales'
175 self.company.l10n_jo_edi_sequence_income_source = '16683693'
176
177 self._validate_invoice_vals_jo_edi_numbers({
178 'name': 'TestEIN022',
179 'currency_id': eur.id,
180 'date': '2023-11-12',
181 'invoice_line_ids': [
182 Command.create({
183 'product_id': self.product_a.id,
184 'quantity': 3.48,
185 'price_unit': 1.56,
186 'discount': 2.5,
187 'tax_ids': [Command.set(self.jo_general_tax_16_included.ids)],
188 }),
189 Command.create({
190 'product_id': self.product_b.id,
191 'quantity': 6.02,
192 'price_unit': 2.79,
193 'discount': 2.5,
194 'tax_ids': [Command.set(self.jo_general_tax_16_included.ids)],
195 }),
196 ],
197 })
198
199 self._validate_invoice_vals_jo_edi_numbers({
200 'name': 'TestEIN023',
201 'date': '2023-11-12',
202 'invoice_line_ids': [
203 Command.create({
204 'product_id': self.product_a.id,
205 'quantity': 5,
206 'price_unit': 206.25,
207 'discount': 12.73,
208 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
209 }),
210 Command.create({
211 'product_id': self.product_a.id,
212 'quantity': 5,
213 'price_unit': 195,
214 'discount': 15.39,
215 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
216 }),
217 Command.create({
218 'product_id': self.product_a.id,
219 'quantity': 5,
220 'price_unit': 206.25,
221 'discount': 14.55,
222 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
223 }),
224 ],
225 })
226
227 self._validate_invoice_vals_jo_edi_numbers({
228 'name': 'TestEIN024',
229 'date': '2023-11-12',
230 'invoice_line_ids': [
231 Command.create({
232 'product_id': self.product_a.id,
233 'quantity': 10,
234 'price_unit': 206.25,
235 'discount': 12.72,
236 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
237 }),
238 Command.create({
239 'product_id': self.product_a.id,
240 'quantity': 7,
241 'price_unit': 187.5,
242 'discount': 16,
243 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
244 }),
245 Command.create({
246 'product_id': self.product_a.id,
247 'quantity': 10,
248 'price_unit': 66.25,
249 'discount': 8.3,
250 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
251 }),
252 Command.create({
253 'product_id': self.product_a.id,
254 'quantity': 6,
255 'price_unit': 33,
256 'discount': 0,
257 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
258 }),
259 Command.create({
260 'product_id': self.product_a.id,
261 'quantity': 1,
262 'price_unit': 206.25,
263 'discount': 14.45,
264 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
265 }),
266 ],
267 })
268
269 self._validate_invoice_vals_jo_edi_numbers({
270 'name': 'TestEIN025',
271 'date': '2023-11-12',
272 'invoice_line_ids': [
273 Command.create({
274 'product_id': self.product_a.id,
275 'quantity': 1,
276 'price_unit': 3.75,
277 'discount': 25,
278 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
279 }),
280 Command.create({
281 'product_id': self.product_a.id,
282 'quantity': 0.2,
283 'price_unit': 13.75,
284 'discount': 25,
285 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
286 }),
287 Command.create({
288 'product_id': self.product_a.id,
289 'quantity': 0.5,
290 'price_unit': 5.85,
291 'discount': 25,
292 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
293 }),
294 ],
295 })
296
297 self._validate_invoice_vals_jo_edi_numbers({
298 'name': 'TestEIN026',
299 'date': '2023-11-12',
300 'invoice_line_ids': [
301 Command.create({
302 'product_id': self.product_a.id,
303 'quantity': 30,
304 'price_unit': 22.2,
305 'discount': 0,
306 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
307 }),
308 Command.create({
309 'product_id': self.product_a.id,
310 'quantity': 3,
311 'price_unit': 22.2,
312 'discount': 100,
313 'tax_ids': [Command.set(self.jo_general_tax_16.ids)],
314 }),
315 ],
316 })
317
318 def test_jo_special_invoice_precision(self):
319 self.company.l10n_jo_edi_taxpayer_type = 'special'
320 self.company.l10n_jo_edi_sequence_income_source = '16683693'
321 self._validate_invoice_vals_jo_edi_numbers({
322 'name': 'TestEIN014',
323 'invoice_date': '2023-11-10',
324 'invoice_line_ids': [
325 Command.create({
326 'product_id': self.product_b.id,
327 'price_unit': 100,
328 'quantity': 1,
329 'tax_ids': [Command.set((self.jo_general_tax_10 | self.jo_special_tax_10).ids)],
330 }),
331 Command.create({
332 'product_id': self.product_a.id,
333 'price_unit': 100,
334 'quantity': 1,
335 'tax_ids': [Command.set((self.jo_general_tax_10 | self.jo_special_tax_5).ids)],
336 }),
337 Command.create({
338 'product_id': self.product_a.id,
339 'price_unit': 100,
340 'quantity': 1,
341 'tax_ids': [Command.set((self.jo_general_tax_16 | self.jo_special_tax_5).ids)],
342 }),
343 Command.create({
344 'product_id': self.product_b.id,
345 'price_unit': 100,
346 'quantity': 1,
347 'tax_ids': [Command.set((self.jo_general_tax_16 | self.jo_special_tax_10).ids)],
348 }),
349 ],
350 })
351
352 def test_jo_credit_notes_price_unit(self):
353 def get_price_units(xml_string):
354 root = self.get_xml_tree_from_string(xml_string)
355 for xml_line in root.findall('./{*}InvoiceLine'):
356 yield float(xml_line.findtext('{*}Price/{*}PriceAmount'))
357 self.company.l10n_jo_edi_taxpayer_type = 'sales'
358 self.company.l10n_jo_edi_sequence_income_source = '16683693'
359 invoice = self._l10n_jo_create_invoice({
360 'name': 'TestEIN014',
361 'invoice_date': '2023-11-10',
362 'invoice_line_ids': [
363 Command.create({
364 'product_id': self.product_b.id,
365 'price_unit': 11.11,
366 'quantity': 9833,
367 'discount': 3.12,
368 'tax_ids': [Command.set((self.jo_general_tax_16_included).ids)],
369 }),
370 Command.create({
371 'product_id': self.product_a.id,
372 'price_unit': 10000.01,
373 'quantity': 93333,
374 'discount': 99.71,
375 'tax_ids': [Command.set((self.jo_general_tax_16_included).ids)],
376 }),
377 Command.create({
378 'product_id': self.product_a.id,
379 'price_unit': 0.01,
380 'quantity': 0.11,
381 'discount': 2,
382 'tax_ids': [Command.set((self.jo_general_tax_16_included).ids)],
383 }),
384 ],
385 })
386 refund = self._l10n_jo_create_refund(invoice, 'return reason', {
387 'invoice_line_ids': [
388 Command.create({
389 'product_id': self.product_b.id,
390 'price_unit': 11.11,
391 'quantity': 3.11,
392 'discount': 3.12,
393 'tax_ids': [Command.set((self.jo_general_tax_16_included).ids)],
394 }),
395 Command.create({
396 'product_id': self.product_a.id,
397 'price_unit': 10000.01,
398 'quantity': 2.02,
399 'discount': 99.71,
400 'tax_ids': [Command.set((self.jo_general_tax_16_included).ids)],
401 }),
402 Command.create({
403 'product_id': self.product_a.id,
404 'price_unit': 0.01,
405 'quantity': 0.1,
406 'discount': 2,
407 'tax_ids': [Command.set((self.jo_general_tax_16_included).ids)],
408 }),
409 ],
410 })
411 invoice_file = self.env['account.edi.xml.ubl_21.jo']._export_invoice(invoice)[0]
412 refund_file = self.env['account.edi.xml.ubl_21.jo']._export_invoice(refund)[0]
413 for invoice_price_unit, refund_price_unit in zip(get_price_units(invoice_file), get_price_units(refund_file)):
414 self.assertEqual(invoice_price_unit, refund_price_unit)
415
416 def test_jo_total_tax_and_lines_taxes_rounding_error(self):
417 """
418 The aim of this test is to ensure that the taxes amounts on lines are calculated using rounded base amounts
419 this would get broken if _add_tax_details_in_base_line uses round_globally
420 """
421 self.company.l10n_jo_edi_taxpayer_type = 'sales'
422 self.company.l10n_jo_edi_sequence_income_source = '16683693'
423
424 self._validate_invoice_vals_jo_edi_numbers({
425 'name': 'TestEIN022',
426 'date': '2023-11-12',
427 'invoice_line_ids': [
428 Command.create({
429 'product_id': self.product_a.id,
430 'quantity': 1,
431 'price_unit': 109,
432 'tax_ids': [Command.set(self.jo_general_tax_16_included.ids)],
433 }) for _ in range(20)
434 ],
435 })