Debian Codegraph @

addons/l10n_it_edi_doi/tests/test_amounts_and_warnings.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from freezegun import freeze_time
4
5 from odoo import Command
6 from odoo.addons.l10n_it_edi_doi.tests.common import TestItEdiDoi
7 from odoo.tests import tagged, Form
8
9
10 @tagged('post_install_l10n', 'post_install', '-at_install')
11 class TestItEdiDoiRemaining(TestItEdiDoi):
12 @classmethod
13 def setUpClass(cls):
14 super().setUpClass()
15 cls.env.user.group_ids |= cls.env.ref('sales_team.group_sale_salesman')
16
17 def create_invoice(self, declaration, invoice_line_vals):
18 return self.env['account.move'].create({
19 'move_type': 'out_invoice',
20 'company_id': self.company.id,
21 'partner_id': declaration.partner_id.id,
22 'invoice_date': declaration.start_date,
23 'l10n_it_edi_doi_id': declaration.id,
24 'invoice_line_ids': invoice_line_vals,
25 })
26
27 def get_sale_order_vals(self, declaration, order_line_vals):
28 return {
29 'company_id': self.company.id,
30 'partner_id': declaration.partner_id.id,
31 'date_order': declaration.start_date,
32 'pricelist_id': self.pricelist.id,
33 'l10n_it_edi_doi_id': declaration.id,
34 'order_line': order_line_vals,
35 }
36
37 def create_sale_order(self, declaration, order_line_vals):
38 sale_order_vals = self.get_sale_order_vals(declaration, order_line_vals)
39 return self.env['sale.order'].create(sale_order_vals)
40
41 def test_invoice_line_edit(self):
42 """
43 Ensure the warnings are computed correctly when editing line values on an invoice.
44 """
45 declaration = self.declaration_1000
46 declaration_tax = declaration.company_id.l10n_it_edi_doi_tax_id
47
48 self.assertRecordValues(declaration, [{
49 'invoiced': 0.0,
50 'not_yet_invoiced': 0.0,
51 'remaining': 1000.0,
52 }])
53
54 invoice = self.create_invoice(declaration, [
55 Command.create({
56 'name': 'declaration line',
57 'quantity': 2,
58 'price_unit': 1000.0, # == declaration.threshold
59 'tax_ids': [Command.set(declaration_tax.ids)],
60 }),
61 ])
62
63 with Form(invoice) as invoice_form:
64 with invoice_form.invoice_line_ids.edit(0) as line_form:
65 line_form.price_unit = 2000
66 line_form.save()
67 self.assertEqual(
68 invoice_form.l10n_it_edi_doi_warning,
69 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 3,000.00\xa0€, this document included.\n"
70 "Invoiced: 4,000.00\xa0€; Not Yet Invoiced: 0.00\xa0€"
71 )
72 with invoice_form.invoice_line_ids.edit(0) as line_form:
73 line_form.quantity = 1
74 line_form.save()
75 self.assertEqual(
76 invoice_form.l10n_it_edi_doi_warning,
77 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 1,000.00\xa0€, this document included.\n"
78 "Invoiced: 2,000.00\xa0€; Not Yet Invoiced: 0.00\xa0€"
79 )
80
81 def test_sale_order_line_edit(self):
82 """
83 Ensure the warnings are computed correctly when editing line values on a quotation or sale order.
84 """
85 declaration = self.declaration_1000
86 declaration_tax = declaration.company_id.l10n_it_edi_doi_tax_id
87
88 self.assertRecordValues(declaration, [{
89 'invoiced': 0.0,
90 'not_yet_invoiced': 0.0,
91 'remaining': 1000.0,
92 }])
93
94 order = self.create_sale_order(declaration, [
95 Command.create({
96 'name': 'declaration line',
97 'product_id': self.product_1.id,
98 'price_unit': 1000.0, # == declaration.threshold
99 'product_uom_qty': 2,
100 'tax_ids': [Command.set(declaration_tax.ids)],
101 }),
102 ])
103
104 with Form(order) as order_form:
105 with order_form.order_line.edit(0) as line_form:
106 line_form.price_unit = 2000
107 line_form.save()
108 self.assertEqual(
109 order_form.l10n_it_edi_doi_warning,
110 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 3,000.00\xa0€, this document included.\n"
111 "Invoiced: 0.00\xa0€; Not Yet Invoiced: 4,000.00\xa0€"
112 )
113 with order_form.order_line.edit(0) as line_form:
114 line_form.product_uom_qty = 1
115 line_form.price_unit = 2000
116 line_form.save()
117 self.assertEqual(
118 order_form.l10n_it_edi_doi_warning,
119 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 1,000.00\xa0€, this document included.\n"
120 "Invoiced: 0.00\xa0€; Not Yet Invoiced: 2,000.00\xa0€"
121 )
122 order_form.save()
123
124 order.action_confirm()
125 # unchanged warning
126 self.assertEqual(
127 order.l10n_it_edi_doi_warning,
128 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 1,000.00\xa0€, this document included.\n"
129 "Invoiced: 0.00\xa0€; Not Yet Invoiced: 2,000.00\xa0€"
130 )
131 with order_form.order_line.edit(0) as line_form:
132 line_form.price_unit = 3000
133 line_form.save()
134 self.assertEqual(
135 order_form.l10n_it_edi_doi_warning,
136 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 2,000.00\xa0€, this document included.\n"
137 "Invoiced: 0.00\xa0€; Not Yet Invoiced: 3,000.00\xa0€"
138 )
139 with order_form.order_line.edit(0) as line_form:
140 line_form.product_uom_qty = 2
141 line_form.price_unit = 3000
142 line_form.save()
143 self.assertEqual(
144 order_form.l10n_it_edi_doi_warning,
145 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 5,000.00\xa0€, this document included.\n"
146 "Invoiced: 0.00\xa0€; Not Yet Invoiced: 6,000.00\xa0€"
147 )
148
149 def test_invoice(self):
150 """
151 Ensure the amounts and warnings are computed correctly in the following flow:
152 We create a single invoice and post it.
153 """
154 declaration = self.declaration_1000
155 declaration_tax = declaration.company_id.l10n_it_edi_doi_tax_id
156
157 self.assertRecordValues(declaration, [{
158 'invoiced': 0.0,
159 'not_yet_invoiced': 0.0,
160 'remaining': 1000.0,
161 }])
162
163 invoice = self.create_invoice(declaration, [
164 Command.create({
165 'name': 'declaration line',
166 'quantity': 1,
167 'price_unit': 1000.0, # == declaration.threshold
168 'tax_ids': [Command.set(declaration_tax.ids)],
169 }),
170 Command.create({
171 # The line should be ignored since it does not use the special tax
172 'name': 'not a declaration line',
173 'quantity': 1,
174 'price_unit': 2000.0, # > declaration.threshold; not counted
175 'tax_ids': False,
176 }),
177 ])
178 # The amounts have not changed since the invoice has not been posted yet.
179 self.assertRecordValues(declaration, [{
180 'invoiced': 0.0,
181 'not_yet_invoiced': 0.0,
182 'remaining': 1000.0,
183 }])
184 # There is no warning since posting the invoice would not exceed the threshold.
185 # (only lines with the special tax are counted)
186 self.assertEqual(invoice.l10n_it_edi_doi_warning, "")
187
188 # Update the declaration part of the invoice to exceed the threshold
189 invoice.invoice_line_ids[0].price_unit = 2000 # > declaration.threshold
190 # The amounts in the warning are the same as the amounts on the declaration after posting the invoice.
191 self.assertEqual(
192 invoice.l10n_it_edi_doi_warning,
193 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 1,000.00\xa0€, this document included.\n"
194 "Invoiced: 2,000.00\xa0€; Not Yet Invoiced: 0.00\xa0€"
195 )
196
197 invoice.action_post()
198 self.assertRecordValues(declaration, [{
199 'invoiced': 2000.0,
200 'not_yet_invoiced': 0.0,
201 'remaining': -1000.0,
202 }])
203 self.assertEqual(
204 invoice.l10n_it_edi_doi_warning,
205 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 1,000.00\xa0€, this document included.\n"
206 "Invoiced: 2,000.00\xa0€; Not Yet Invoiced: 0.00\xa0€"
207 )
208
209 def test_sale_order_and_independent_invoice(self):
210 """
211 Ensure the amounts and warnings are computed correctly in the following flow:
212 * We create a quotation and confirm it to sales order.
213 * Then we create a single invoice independent of the sales order and post it.
214 I.e. the invoice should not influence the Not Yet Invoiced amount of the declaration.
215 """
216 declaration = self.declaration_1000
217 declaration_tax = declaration.company_id.l10n_it_edi_doi_tax_id
218
219 order = self.create_sale_order(declaration, [
220 Command.create({
221 'name': 'declaration line',
222 'product_id': self.product_1.id,
223 'price_unit': 1000.0, # == declaration.threshold
224 'tax_ids': [Command.set(declaration_tax.ids)],
225 }),
226 Command.create({
227 'name': 'not a declaration line',
228 'product_id': self.product_1.id,
229 'price_unit': 2000.0, # > declaration.threshold; not counted
230 'tax_ids': False,
231 }),
232 ])
233
234 # There is no warning since confirming the sale order would not exceed the threshold.
235 # (only lines with the special tax are counted)
236 self.assertEqual(order.l10n_it_edi_doi_warning, "")
237
238 # We only count sales orders not quotations
239 self.assertRecordValues(declaration, [{
240 'invoiced': 0.0,
241 'not_yet_invoiced': 0.0,
242 'remaining': 1000.0,
243 }])
244
245 # Update the declaration part of `order` to exceed the threshold
246 order.order_line[0].price_unit = 2000 # > declaration.threshold
247 # Now we show the warning
248 self.assertEqual(
249 order.l10n_it_edi_doi_warning,
250 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 1,000.00\xa0€, this document included.\n"
251 "Invoiced: 0.00\xa0€; Not Yet Invoiced: 2,000.00\xa0€"
252 )
253
254 order.action_confirm()
255 self.assertRecordValues(declaration, [{
256 'invoiced': 0.0,
257 'not_yet_invoiced': 2000.0,
258 'remaining': -1000.0,
259 }])
260
261 invoice = self.create_invoice(declaration, [
262 Command.create({
263 'name': 'declaration line',
264 'quantity': 1,
265 'price_unit': 1000.0,
266 'tax_ids': [Command.set(declaration_tax.ids)],
267 }),
268 Command.create({
269 # The line should be ignored since it does not use the special tax
270 'name': 'none declaration line',
271 'quantity': 1,
272 'price_unit': 2000.0, # > declaration.threshold; not counted
273 'tax_ids': False,
274 }),
275 ])
276 # The amounts have not changed since the invoice has not been posted yet.
277 self.assertRecordValues(declaration, [{
278 'invoiced': 0.0,
279 'not_yet_invoiced': 2000.0,
280 'remaining': -1000.0,
281 }])
282
283 # The warning has the updated values though
284 self.assertEqual(
285 invoice.l10n_it_edi_doi_warning,
286 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 2,000.00\xa0€, this document included.\n"
287 "Invoiced: 1,000.00\xa0€; Not Yet Invoiced: 2,000.00\xa0€"
288 )
289
290 invoice.action_post()
291 self.assertRecordValues(declaration, [{
292 'invoiced': 1000.0,
293 'not_yet_invoiced': 2000.0,
294 'remaining': -2000.0,
295 }])
296
297 @freeze_time('2019-12-31') # declaration.end_date
298 def test_overinvoiced_sale_order_and_credit_note(self):
299 """
300 Ensure the amounts and warnings are computed correctly in the following flow:
301 * We create a quotation and confirm it to sales order.
302 * Then we invoice the sales order in 2 downpayment invoices of 50% each.
303 I.e. the Invoiced amount should be transferred correctly from Not Yet Invoiced to Invoiced
304 * We increase the amount on one of the invoices s.t. it exceeds the sales order amount.
305 I.e. the Invoiced amount increases more than the Not Yet Invoiced amount is lowered
306 * We reverse the invoice exceeding the sales order amount by creating a credit note.
307 I.e. check the amounts are computed correctly on the warning.
308 """
309
310 declaration = self.declaration_1000
311 declaration_tax = declaration.company_id.l10n_it_edi_doi_tax_id
312
313 # Add an order that is not used in the rest of this test.
314 # This way we can always show the warning and that this amount will not be removed from Not Yet Invoiced.
315 independent_order = self.create_sale_order(declaration, [
316 Command.create({
317 'name': 'declaration line',
318 'product_id': self.product_1.id,
319 'price_unit': 2000.0, # > declaration.threshold
320 'tax_ids': [Command.set(declaration_tax.ids)],
321 }),
322 Command.create({
323 'name': 'not a declaration line',
324 'product_id': self.product_1.id,
325 'price_unit': 2000.0, # > declaration.threshold; not counted
326 'tax_ids': False,
327 }),
328 ])
329 independent_order.action_confirm()
330 self.assertRecordValues(declaration, [{
331 'invoiced': 0.0,
332 'not_yet_invoiced': 2000.0, # 2000 "base" from independent_order
333 'remaining': -1000.0,
334 }])
335
336 order = self.create_sale_order(declaration, [
337 Command.create({
338 'name': 'declaration line',
339 'product_id': self.product_1.id,
340 'price_unit': 1000.0, # == declaration.threshold
341 'tax_ids': [Command.set(declaration_tax.ids)],
342 }),
343 Command.create({
344 'name': 'not a declaration line',
345 'product_id': self.product_1.id,
346 'price_unit': 2000.0, # > declaration.threshold; not counted
347 'tax_ids': False,
348 }),
349 ])
350 order.action_confirm()
351 self.assertRecordValues(declaration, [{
352 'invoiced': 0.0,
353 'not_yet_invoiced': 3000.0, # 2000 "base" + 1000 from `order`
354 'remaining': -2000.0,
355 }])
356
357 for _i in range(2):
358 self.env['sale.advance.payment.inv'].with_context({
359 'active_model': 'sale.order',
360 'active_ids': [order.id],
361 'active_id': order.id,
362 'default_journal_id': self.company_data_2['default_journal_sale'].id,
363 }).create({
364 'advance_payment_method': 'percentage',
365 'amount': 50,
366 }).create_invoices()
367
368 invoice = order.invoice_ids[0]
369
370 # The invoice just moves amount from `not_invoiced_yet` to `invoiced`.
371 # It does not lower the remaining ammount.
372 self.assertEqual(
373 invoice.l10n_it_edi_doi_warning,
374 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 2,000.00\xa0€, this document included.\n"
375 "Invoiced: 500.00\xa0€; Not Yet Invoiced: 2,500.00\xa0€"
376 )
377
378 invoice.invoice_line_ids[0].price_unit = 2000 # 1000 more than the sales order declaration amount
379 # Changing an invoice line does not affect the not yet invoiced amount of sale order lines not linked to that line
380 self.assertEqual(
381 invoice.l10n_it_edi_doi_warning,
382 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 3,500.00\xa0€, this document included.\n"
383 "Invoiced: 2,000.00\xa0€; Not Yet Invoiced: 2,500.00\xa0€"
384 )
385 invoice.action_post()
386 self.assertRecordValues(declaration, [{
387 'invoiced': 2000.0, # 2000 from invoice
388 'not_yet_invoiced': 2000.0, # 2000 "base"
389 'remaining': -3000.0,
390 }])
391
392 invoice2 = order.invoice_ids[1]
393 invoice2.action_post()
394 self.assertEqual(
395 invoice2.l10n_it_edi_doi_warning,
396 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 3,500.00\xa0€, this document included.\n"
397 "Invoiced: 2,500.00\xa0€; Not Yet Invoiced: 2,000.00\xa0€"
398 )
399 self.assertRecordValues(declaration, [{
400 'invoiced': 2500.0, # 2000 + 500 from the 2 downpayment invoices
401 'not_yet_invoiced': 2000.0, # 2000 "base"
402 'remaining': -3500.0,
403 }])
404
405 # Reverse the invoice via a credit note
406 self.env['account.move.reversal'].with_company(self.company).create(
407 {
408 'move_ids': [Command.set((invoice.id,))],
409 'date': '2019-12-31',
410 'journal_id': invoice.journal_id.id,
411 }
412 ).reverse_moves()
413
414 # The invoice we reversed invoiced more than the sales order amount.
415 credit_note = invoice.reversal_move_ids
416 self.assertEqual(
417 credit_note.l10n_it_edi_doi_warning,
418 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 2,000.00\xa0€, this document included.\n"
419 "Invoiced: 500.00\xa0€; Not Yet Invoiced: 2,500.00\xa0€"
420 )
421
422 credit_note.action_post()
423 self.assertRecordValues(declaration, [{
424 'invoiced': 500, # 1 downpayment of 50% on 1000 sale order
425 'not_yet_invoiced': 2500, # 2000 ("base") + 500 (left on sale order)
426 'remaining': -2000,
427 }])
428
429 @freeze_time('2019-12-31') # declaration.end_date
430 def test_consolidated_billing(self):
431 """
432 Ensure the amounts and warnings are computed correctly in the following flow:
433 1. We create multiple quotations with 1 line each with qty 2 and confirm them all to sales order.
434 2. We create a single invoice for the SOs from the previous step.
435 There is one line per SO.
436 3. We change the amount on the one of the invoice lines without changing the qty.
437 4. We increase the qty on one line.
438 5. We decrease the qty on another line (not from step 4)
439 I.e. we check that:
440 * The not yet invoiced amount on the SO linked to the line from step (3) is still 0.
441 * The lines from step (4) and (5) do not affect each other
442 * Increasing the qty on line (5) to be higher than the SO amount does not lead to a negative amount on the linked SO.
443 """
444
445 declaration = self.declaration_1000
446 declaration_tax = declaration.company_id.l10n_it_edi_doi_tax_id
447
448 orders = self.env['sale.order'].create([
449 self.get_sale_order_vals(declaration, [
450 Command.create({
451 'name': 'declaration line',
452 'product_id': self.product_1.id,
453 'product_uom_qty': 2,
454 'price_unit': 2000.0, # > declaration.threshold
455 'tax_ids': [Command.set(declaration_tax.ids)],
456 }),
457 ]) for dummy in range(3)
458 ])
459 orders.action_confirm()
460 self.assertRecordValues(declaration, [{
461 'invoiced': 0.0,
462 'not_yet_invoiced': 12000.0,
463 'remaining': -11000.0,
464 }])
465
466 invoice = orders._create_invoices()
467 self.assertRecordValues(declaration, [{
468 'invoiced': 0.0,
469 'not_yet_invoiced': 12000.0,
470 'remaining': -11000.0,
471 }])
472 self.assertEqual(
473 invoice.l10n_it_edi_doi_warning,
474 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 11,000.00\xa0€, this document included.\n"
475 "Invoiced: 12,000.00\xa0€; Not Yet Invoiced: 0.00\xa0€"
476 )
477
478 invoice.invoice_line_ids[0].price_unit = 1000
479 # in the warning:
480 # * invoiced amount decreases by 2000 (since we reduced the price_unit by 1000)
481 # * not yet invoiced amount stays the same (all quantities still invoiced)
482 self.assertEqual(
483 invoice.l10n_it_edi_doi_warning,
484 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 9,000.00\xa0€, this document included.\n"
485 "Invoiced: 10,000.00\xa0€; Not Yet Invoiced: 0.00\xa0€"
486 )
487 self.assertRecordValues(declaration, [{
488 'invoiced': 0.0,
489 'not_yet_invoiced': 12000.0,
490 'remaining': -11000.0,
491 }])
492
493 invoice.invoice_line_ids[1].quantity = 1
494 # in the warning: 1 qty (2000 €) moves from invoiced to net yet invoiced
495 self.assertEqual(
496 invoice.l10n_it_edi_doi_warning,
497 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 9,000.00\xa0€, this document included.\n"
498 "Invoiced: 8,000.00\xa0€; Not Yet Invoiced: 2,000.00\xa0€"
499 )
500 self.assertRecordValues(declaration, [{
501 'invoiced': 0.0,
502 'not_yet_invoiced': 12000.0,
503 'remaining': -11000.0,
504 }])
505
506 invoice.invoice_line_ids[2].quantity = 3
507 # in the warning:
508 # * invoiced amount increases by 2000 (since we increase the quantity by 1)
509 # * not yet invoiced amount stays the same (all quantities still invoiced)
510 self.assertEqual(
511 invoice.l10n_it_edi_doi_warning,
512 "Pay attention, the threshold of your Declaration of Intent test 2019-threshold 1000 of 1,000.00\xa0€ is exceeded by 11,000.00\xa0€, this document included.\n"
513 "Invoiced: 10,000.00\xa0€; Not Yet Invoiced: 2,000.00\xa0€"
514 )
515 self.assertRecordValues(declaration, [{
516 'invoiced': 0.0,
517 'not_yet_invoiced': 12000.0,
518 'remaining': -11000.0,
519 }])
520
521 invoice.action_post()
522 # Same values as in the last warning
523 self.assertRecordValues(declaration, [{
524 'invoiced': 10000.0,
525 'not_yet_invoiced': 2000.0,
526 'remaining': -11000.0,
527 }])