|
1 | 1 | # Copyright 2024 bosd |
2 | 2 | # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
3 | 3 |
|
4 | | -from dateutil.relativedelta import relativedelta |
5 | | -from odoo.tests import common, tagged |
6 | 4 | from odoo import fields |
| 5 | +from odoo.tests import common, tagged |
| 6 | + |
7 | 7 |
|
8 | 8 | @tagged("post_install", "-at_install") |
9 | 9 | class TestContractInvoiceOffset(common.TransactionCase): |
10 | | - |
11 | 10 | @classmethod |
12 | 11 | def setUpClass(cls): |
13 | 12 | super().setUpClass() |
14 | 13 | cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) |
15 | | - cls.product = cls.env["product.product"].create({"name": "Test Service", "type": "service"}) |
16 | | - cls.contract = cls.env["contract.contract"].create({ |
17 | | - "name": "Test Offset Contract", |
18 | | - "partner_id": cls.partner.id, |
19 | | - "recurring_interval": 1, |
20 | | - "recurring_rule_type": "monthly", |
21 | | - "date_start": "2025-01-01", |
22 | | - "contract_line_ids": [ |
23 | | - (0, 0, { |
24 | | - "product_id": cls.product.id, |
25 | | - "name": "Service", |
26 | | - "quantity": 1, |
27 | | - "price_unit": 100, |
28 | | - "recurring_interval": 1, |
29 | | - "recurring_rule_type": "monthly", |
30 | | - "date_start": "2025-01-01", |
31 | | - }) |
32 | | - ] |
33 | | - }) |
| 14 | + cls.product = cls.env["product.product"].create( |
| 15 | + {"name": "Test Service", "type": "service"} |
| 16 | + ) |
| 17 | + cls.contract = cls.env["contract.contract"].create( |
| 18 | + { |
| 19 | + "name": "Test Offset Contract", |
| 20 | + "partner_id": cls.partner.id, |
| 21 | + "recurring_interval": 1, |
| 22 | + "recurring_rule_type": "monthly", |
| 23 | + "date_start": "2025-01-01", |
| 24 | + "contract_line_ids": [ |
| 25 | + ( |
| 26 | + 0, |
| 27 | + 0, |
| 28 | + { |
| 29 | + "product_id": cls.product.id, |
| 30 | + "name": "Service", |
| 31 | + "quantity": 1, |
| 32 | + "price_unit": 100, |
| 33 | + "recurring_interval": 1, |
| 34 | + "recurring_rule_type": "monthly", |
| 35 | + "date_start": "2025-01-01", |
| 36 | + }, |
| 37 | + ) |
| 38 | + ], |
| 39 | + } |
| 40 | + ) |
34 | 41 |
|
35 | 42 | def test_prepaid_advance_one_month(self): |
36 | 43 | """Test Pre-paid + 1 month advance (offset -1 month).""" |
37 | | - self.contract.write({ |
38 | | - "recurring_invoicing_type": "pre-paid", |
39 | | - "invoicing_offset_type": "months", |
40 | | - "invoicing_offset_value": -1, |
41 | | - }) |
| 44 | + self.contract.write( |
| 45 | + { |
| 46 | + "recurring_invoicing_type": "pre-paid", |
| 47 | + "invoicing_offset_type": "months", |
| 48 | + "invoicing_offset_value": -1, |
| 49 | + } |
| 50 | + ) |
42 | 51 | # Standard Next Invoice for 2025-01-01 start is 2025-01-01. |
43 | 52 | # With -1 month offset, it should be 2024-12-01. |
44 | 53 | self.contract._compute_recurring_next_date() |
45 | 54 | self.assertEqual( |
46 | 55 | self.contract.recurring_next_date, |
47 | 56 | fields.Date.to_date("2024-12-01"), |
48 | | - "Invoice date should be one month prior to start date" |
| 57 | + "Invoice date should be one month prior to start date", |
49 | 58 | ) |
50 | 59 |
|
51 | 60 | def test_postpaid_delayed_one_month(self): |
52 | 61 | """Test Post-paid + 1 month delay.""" |
53 | | - self.contract.write({ |
54 | | - "recurring_invoicing_type": "post-paid", |
55 | | - "invoicing_offset_type": "months", |
56 | | - "invoicing_offset_value": 1, |
57 | | - }) |
58 | | - |
| 62 | + self.contract.write( |
| 63 | + { |
| 64 | + "recurring_invoicing_type": "post-paid", |
| 65 | + "invoicing_offset_type": "months", |
| 66 | + "invoicing_offset_value": 1, |
| 67 | + } |
| 68 | + ) |
| 69 | + |
59 | 70 | self.contract._compute_recurring_next_date() |
60 | 71 | # End date of first period (2025-01-01 monthly) is 2025-01-31. |
61 | 72 | # Next Invoice Date = 2025-01-31 + 1 month = 2025-02-28. |
62 | 73 | self.assertEqual( |
63 | 74 | self.contract.recurring_next_date, |
64 | 75 | fields.Date.to_date("2025-02-28"), |
65 | | - "Invoice date should be one month after period end" |
| 76 | + "Invoice date should be one month after period end", |
66 | 77 | ) |
67 | 78 |
|
68 | 79 | def test_days_fallback(self): |
69 | 80 | """Test that 'days' still works (backward compatibility logic).""" |
70 | | - self.contract.write({ |
71 | | - "recurring_invoicing_type": "post-paid", |
72 | | - "invoicing_offset_type": "days", |
73 | | - "invoicing_offset_value": 5, # Additional 5 days |
74 | | - }) |
| 81 | + self.contract.write( |
| 82 | + { |
| 83 | + "recurring_invoicing_type": "post-paid", |
| 84 | + "invoicing_offset_type": "days", |
| 85 | + "invoicing_offset_value": 5, # Additional 5 days |
| 86 | + } |
| 87 | + ) |
75 | 88 | # Base offset (from contract logic if unmodified) is 1 day for post-paid. |
76 | | - # Our logic: base_date (Jan 31) + days(recurring_offset(1) + offset_value(5)) = +6 days. |
| 89 | + # Our logic: base_date (Jan 31) + days(recurring_offset(1) + offset_value(5)) |
| 90 | + # = +6 days. |
77 | 91 | # Jan 31 + 6 days = Feb 6. |
78 | 92 | self.contract._compute_recurring_next_date() |
79 | 93 | self.assertEqual( |
80 | 94 | self.contract.recurring_next_date, |
81 | 95 | fields.Date.to_date("2025-02-06"), |
82 | | - "Invoice date should be 6 days after period end" |
| 96 | + "Invoice date should be 6 days after period end", |
83 | 97 | ) |
84 | 98 |
|
85 | 99 | def test_apply_offset_on_invoice_creation(self): |
86 | 100 | """Verify that invoice creation respects the offsets.""" |
87 | | - self.contract.write({ |
88 | | - "recurring_invoicing_type": "pre-paid", |
89 | | - "invoicing_offset_type": "months", |
90 | | - "invoicing_offset_value": -1, |
91 | | - }) |
| 101 | + self.contract.write( |
| 102 | + { |
| 103 | + "recurring_invoicing_type": "pre-paid", |
| 104 | + "invoicing_offset_type": "months", |
| 105 | + "invoicing_offset_value": -1, |
| 106 | + } |
| 107 | + ) |
92 | 108 | # Next date is 2024-12-01. If we run invoice creation for that date, |
93 | 109 | # we expect an invoice for the period 2025-01-01 to 2025-01-31. |
94 | | - |
| 110 | + |
95 | 111 | # Force next date just to be sure |
96 | 112 | self.contract.recurring_next_date = "2024-12-01" |
97 | | - |
| 113 | + |
98 | 114 | invoice = self.contract._recurring_create_invoice() |
99 | 115 | self.assertTrue(invoice, "Invoice should be created") |
100 | | - |
| 116 | + |
101 | 117 | # Check Invoice Date |
102 | 118 | self.assertEqual( |
103 | 119 | invoice.invoice_date, |
104 | 120 | fields.Date.to_date("2024-12-01"), |
105 | | - "Invoice date should be Dec 1st" |
| 121 | + "Invoice date should be Dec 1st", |
106 | 122 | ) |
107 | | - |
| 123 | + |
108 | 124 | # Check Line Description or Metadata if available to verify period? |
109 | 125 | # Standard contract creates description "Service" (or template). |
110 | 126 | # We can check contract line's last_date_invoiced. |
111 | 127 | line = self.contract.contract_line_ids[0] |
112 | 128 | self.assertEqual( |
113 | 129 | line.last_date_invoiced, |
114 | 130 | fields.Date.to_date("2025-01-31"), |
115 | | - "Last date invoiced should be end of the service period (Jan 31)" |
| 131 | + "Last date invoiced should be end of the service period (Jan 31)", |
116 | 132 | ) |
0 commit comments