|
| 1 | +# Copyright 2025 Engenere.one |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | +from datetime import datetime, timedelta |
| 4 | + |
| 5 | +from odoo.tests import Form, tagged |
| 6 | +from odoo.tests.common import TransactionCase |
| 7 | + |
| 8 | + |
| 9 | +@tagged("post_install", "-at_install") |
| 10 | +class TestOverdueWarn(TransactionCase): |
| 11 | + def setUp(self): |
| 12 | + super().setUp() |
| 13 | + self.company = self.env.ref("base.main_company") |
| 14 | + self.partner = self.env["res.partner"].create( |
| 15 | + { |
| 16 | + "name": "Test Partner", |
| 17 | + "country_id": self.env.ref("base.br").id, |
| 18 | + "company_id": self.company.id, |
| 19 | + } |
| 20 | + ) |
| 21 | + self.today = datetime.now().date() |
| 22 | + self.revenue_acc = self.env["account.account"].search( |
| 23 | + [ |
| 24 | + ("company_id", "=", self.company.id), |
| 25 | + ( |
| 26 | + "user_type_id", |
| 27 | + "=", |
| 28 | + self.env.ref("account.data_account_type_revenue").id, |
| 29 | + ), |
| 30 | + ], |
| 31 | + limit=1, |
| 32 | + ) |
| 33 | + |
| 34 | + self.payment_term = self.env["account.payment.term"].create( |
| 35 | + { |
| 36 | + "name": "Immediate payment", |
| 37 | + "line_ids": [ |
| 38 | + ( |
| 39 | + 0, |
| 40 | + 0, |
| 41 | + { |
| 42 | + "value": "balance", |
| 43 | + "days": 0, |
| 44 | + }, |
| 45 | + ), |
| 46 | + ], |
| 47 | + } |
| 48 | + ) |
| 49 | + |
| 50 | + self.payment_term_installments_a = self.env["account.payment.term"].create( |
| 51 | + { |
| 52 | + "name": "Pay in 3 installments", |
| 53 | + "line_ids": [ |
| 54 | + ( |
| 55 | + 0, |
| 56 | + 0, |
| 57 | + { |
| 58 | + "value": "percent", |
| 59 | + "value_amount": 33.33, |
| 60 | + "days": 0, |
| 61 | + }, |
| 62 | + ), |
| 63 | + ( |
| 64 | + 0, |
| 65 | + 0, |
| 66 | + { |
| 67 | + "value": "percent", |
| 68 | + "value_amount": 33.33, |
| 69 | + "days": 0, |
| 70 | + }, |
| 71 | + ), |
| 72 | + ( |
| 73 | + 0, |
| 74 | + 0, |
| 75 | + { |
| 76 | + "value": "balance", |
| 77 | + "days": 0, |
| 78 | + }, |
| 79 | + ), |
| 80 | + ], |
| 81 | + } |
| 82 | + ) |
| 83 | + |
| 84 | + self.payment_term_installments_b = self.env["account.payment.term"].create( |
| 85 | + { |
| 86 | + "name": "Pay in 3 installments", |
| 87 | + "line_ids": [ |
| 88 | + ( |
| 89 | + 0, |
| 90 | + 0, |
| 91 | + { |
| 92 | + "value": "percent", |
| 93 | + "value_amount": 33.33, |
| 94 | + "days": 0, |
| 95 | + }, |
| 96 | + ), |
| 97 | + ( |
| 98 | + 0, |
| 99 | + 0, |
| 100 | + { |
| 101 | + "value": "percent", |
| 102 | + "value_amount": 33.33, |
| 103 | + "days": 0, |
| 104 | + }, |
| 105 | + ), |
| 106 | + ( |
| 107 | + 0, |
| 108 | + 0, |
| 109 | + { |
| 110 | + "value": "balance", |
| 111 | + "days": 14, |
| 112 | + }, |
| 113 | + ), |
| 114 | + ], |
| 115 | + } |
| 116 | + ) |
| 117 | + |
| 118 | + def test_out_invoice_draft(self): |
| 119 | + out_invoice_draft = self.env["account.move"].create( |
| 120 | + { |
| 121 | + "partner_id": self.partner.id, |
| 122 | + "move_type": "out_invoice", |
| 123 | + "company_id": self.company.id, |
| 124 | + "currency_id": self.company.currency_id.id, |
| 125 | + "invoice_date": self.today - timedelta(days=9), |
| 126 | + "invoice_payment_term_id": self.payment_term.id, |
| 127 | + "invoice_line_ids": [ |
| 128 | + ( |
| 129 | + 0, |
| 130 | + 0, |
| 131 | + { |
| 132 | + "name": "Product Test", |
| 133 | + "price_unit": 500, |
| 134 | + "quantity": 1, |
| 135 | + "account_id": self.revenue_acc.id, |
| 136 | + }, |
| 137 | + ) |
| 138 | + ], |
| 139 | + } |
| 140 | + ) |
| 141 | + self.assertEqual( |
| 142 | + out_invoice_draft.invoice_date_due, |
| 143 | + out_invoice_draft.line_ids[1].date_maturity, |
| 144 | + ) |
| 145 | + self.assertEqual(out_invoice_draft.state, "draft") |
| 146 | + self.assertEqual(self.partner.overdue_invoice_count, 0) |
| 147 | + self.assertEqual(self.partner.overdue_invoice_amount, 0.0) |
| 148 | + |
| 149 | + def test_confirmed_supplier_invoice(self): |
| 150 | + out_invoice_supplier = self.env["account.move"].create( |
| 151 | + { |
| 152 | + "partner_id": self.partner.id, |
| 153 | + "move_type": "in_invoice", |
| 154 | + "company_id": self.company.id, |
| 155 | + "currency_id": self.company.currency_id.id, |
| 156 | + "invoice_date": self.today - timedelta(days=9), |
| 157 | + "invoice_payment_term_id": self.payment_term.id, |
| 158 | + "invoice_line_ids": [ |
| 159 | + ( |
| 160 | + 0, |
| 161 | + 0, |
| 162 | + { |
| 163 | + "name": "Product Test", |
| 164 | + "price_unit": 500, |
| 165 | + "quantity": 1, |
| 166 | + "account_id": self.revenue_acc.id, |
| 167 | + }, |
| 168 | + ) |
| 169 | + ], |
| 170 | + } |
| 171 | + ) |
| 172 | + out_invoice_supplier.action_post() |
| 173 | + self.assertEqual( |
| 174 | + out_invoice_supplier.invoice_date_due, |
| 175 | + out_invoice_supplier.line_ids[1].date_maturity, |
| 176 | + ) |
| 177 | + self.assertEqual(self.partner.overdue_invoice_count, 0) |
| 178 | + self.assertEqual(self.partner.overdue_invoice_amount, 0.0) |
| 179 | + |
| 180 | + def test_mixed_case_with_two_invoices(self): |
| 181 | + |
| 182 | + out_invoice_a = self.env["account.move"].create( |
| 183 | + { |
| 184 | + "partner_id": self.partner.id, |
| 185 | + "move_type": "out_invoice", |
| 186 | + "company_id": self.company.id, |
| 187 | + "currency_id": self.company.currency_id.id, |
| 188 | + "invoice_date": self.today - timedelta(days=10), |
| 189 | + "invoice_payment_term_id": self.payment_term_installments_a.id, |
| 190 | + "invoice_line_ids": [ |
| 191 | + ( |
| 192 | + 0, |
| 193 | + 0, |
| 194 | + { |
| 195 | + "name": "Product Test", |
| 196 | + "price_unit": 900, |
| 197 | + "quantity": 1, |
| 198 | + "account_id": self.revenue_acc.id, |
| 199 | + }, |
| 200 | + ), |
| 201 | + ], |
| 202 | + } |
| 203 | + ) |
| 204 | + out_invoice_a.action_post() |
| 205 | + |
| 206 | + out_invoice_b = self.env["account.move"].create( |
| 207 | + { |
| 208 | + "partner_id": self.partner.id, |
| 209 | + "move_type": "out_invoice", |
| 210 | + "company_id": self.company.id, |
| 211 | + "currency_id": self.company.currency_id.id, |
| 212 | + "invoice_date": self.today - timedelta(days=10), |
| 213 | + "invoice_payment_term_id": self.payment_term_installments_b.id, |
| 214 | + "invoice_line_ids": [ |
| 215 | + ( |
| 216 | + 0, |
| 217 | + 0, |
| 218 | + { |
| 219 | + "name": "Product Test", |
| 220 | + "price_unit": 900, |
| 221 | + "quantity": 1, |
| 222 | + "account_id": self.revenue_acc.id, |
| 223 | + }, |
| 224 | + ), |
| 225 | + ], |
| 226 | + } |
| 227 | + ) |
| 228 | + out_invoice_b.action_post() |
| 229 | + |
| 230 | + action_data_a = out_invoice_a.action_register_payment() |
| 231 | + wizard_a = Form( |
| 232 | + self.env["account.payment.register"].with_context( |
| 233 | + **action_data_a["context"] |
| 234 | + ) |
| 235 | + ).save() |
| 236 | + wizard_a.amount = 450.0 |
| 237 | + wizard_a.action_create_payments() |
| 238 | + |
| 239 | + self.assertEqual( |
| 240 | + out_invoice_b.invoice_date_due, |
| 241 | + out_invoice_b.line_ids[3].date_maturity, |
| 242 | + ) |
| 243 | + self.assertEqual(self.partner.overdue_invoice_count, 2) |
| 244 | + self.assertEqual(self.partner.overdue_invoice_amount, 1049.94) |
| 245 | + |
| 246 | + def test_confirmed_invoice_with_past_date(self): |
| 247 | + out_invoice_past_paid = self.env["account.move"].create( |
| 248 | + { |
| 249 | + "partner_id": self.partner.id, |
| 250 | + "move_type": "out_invoice", |
| 251 | + "company_id": self.company.id, |
| 252 | + "currency_id": self.company.currency_id.id, |
| 253 | + "invoice_date": self.today - timedelta(days=5), |
| 254 | + "invoice_payment_term_id": self.payment_term.id, |
| 255 | + "invoice_line_ids": [ |
| 256 | + ( |
| 257 | + 0, |
| 258 | + 0, |
| 259 | + { |
| 260 | + "name": "Product Test", |
| 261 | + "price_unit": 500.0, |
| 262 | + "quantity": 1, |
| 263 | + "account_id": self.revenue_acc.id, |
| 264 | + }, |
| 265 | + ) |
| 266 | + ], |
| 267 | + } |
| 268 | + ) |
| 269 | + out_invoice_past_paid.action_post() |
| 270 | + action_data = out_invoice_past_paid.action_register_payment() |
| 271 | + wizard = Form( |
| 272 | + self.env["account.payment.register"].with_context(**action_data["context"]) |
| 273 | + ).save() |
| 274 | + wizard.action_create_payments() |
| 275 | + self.assertEqual( |
| 276 | + out_invoice_past_paid.invoice_date_due, |
| 277 | + out_invoice_past_paid.line_ids[1].date_maturity, |
| 278 | + ) |
| 279 | + self.assertEqual(self.partner.overdue_invoice_count, 0) |
| 280 | + self.assertEqual(self.partner.overdue_invoice_amount, 0) |
| 281 | + |
| 282 | + def test_confirmed_invoice_with_future_date_unpaid(self): |
| 283 | + out_invoice_future = self.env["account.move"].create( |
| 284 | + { |
| 285 | + "partner_id": self.partner.id, |
| 286 | + "move_type": "out_invoice", |
| 287 | + "company_id": self.company.id, |
| 288 | + "currency_id": self.company.currency_id.id, |
| 289 | + "invoice_date": self.today + timedelta(days=5), |
| 290 | + "invoice_payment_term_id": self.payment_term.id, |
| 291 | + "invoice_line_ids": [ |
| 292 | + ( |
| 293 | + 0, |
| 294 | + 0, |
| 295 | + { |
| 296 | + "name": "Product Test", |
| 297 | + "price_unit": 500, |
| 298 | + "quantity": 1, |
| 299 | + "account_id": self.revenue_acc.id, |
| 300 | + }, |
| 301 | + ) |
| 302 | + ], |
| 303 | + } |
| 304 | + ) |
| 305 | + out_invoice_future.action_post() |
| 306 | + self.assertEqual( |
| 307 | + out_invoice_future.invoice_date_due, |
| 308 | + out_invoice_future.line_ids[1].date_maturity, |
| 309 | + ) |
| 310 | + self.assertEqual(self.partner.overdue_invoice_count, 0) |
| 311 | + self.assertEqual(self.partner.overdue_invoice_amount, 0) |
0 commit comments