Files
@ 145fd057aca0
Branch filter:
Location: symposion_app/registrasion/tests/test_invoice.py - annotation
145fd057aca0
19.2 KiB
text/x-python
Breaks out flag-handling code into flag.py and FlagController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 875f736d67c5 875f736d67c5 875f736d67c5 ac10ea4ee895 6b10a0a7e427 563355485435 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 9a4574ef2c3b 9a4574ef2c3b 9a4574ef2c3b 9a4574ef2c3b 9a4574ef2c3b 9a4574ef2c3b 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 d9e433659d01 eab1deff7776 d9e433659d01 d9e433659d01 d9e433659d01 563355485435 d9e433659d01 875f736d67c5 875f736d67c5 875f736d67c5 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 8080d7851bda d9e433659d01 563355485435 d9e433659d01 8080d7851bda 8080d7851bda 875f736d67c5 875f736d67c5 38cdb8aa6330 38cdb8aa6330 8080d7851bda d9e433659d01 875f736d67c5 875f736d67c5 875f736d67c5 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 ca8f67c2f396 9a4574ef2c3b ca8f67c2f396 ca8f67c2f396 ca8f67c2f396 ca8f67c2f396 ca8f67c2f396 ca8f67c2f396 ca8f67c2f396 ca8f67c2f396 ca8f67c2f396 d9e433659d01 d9e433659d01 d9e433659d01 eab1deff7776 d9e433659d01 d9e433659d01 d9e433659d01 eab1deff7776 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 563355485435 d9e433659d01 d9e433659d01 9a4574ef2c3b d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 38cdb8aa6330 d9e433659d01 d9e433659d01 63dfd353c16e 63dfd353c16e 63dfd353c16e 63dfd353c16e d9e433659d01 d9e433659d01 eab1deff7776 9a4574ef2c3b d9e433659d01 d9e433659d01 875f736d67c5 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 875f736d67c5 d9e433659d01 d9e433659d01 d9e433659d01 875f736d67c5 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 8ad265a65a08 d9e433659d01 eab1deff7776 cc4249083219 d9e433659d01 d9e433659d01 d9e433659d01 563355485435 d9e433659d01 d9e433659d01 875f736d67c5 875f736d67c5 875f736d67c5 d9e433659d01 d9e433659d01 224878a10ce3 224878a10ce3 224878a10ce3 8080d7851bda 8ad265a65a08 875f736d67c5 8ad265a65a08 8ad265a65a08 8ad265a65a08 8ad265a65a08 875f736d67c5 8ad265a65a08 8ad265a65a08 8ad265a65a08 875f736d67c5 8ad265a65a08 8ad265a65a08 8ad265a65a08 8ad265a65a08 8ad265a65a08 8ad265a65a08 8ad265a65a08 8ad265a65a08 8ad265a65a08 8ad265a65a08 8ad265a65a08 563355485435 8ad265a65a08 38cdb8aa6330 8ad265a65a08 8080d7851bda eab1deff7776 8080d7851bda 8080d7851bda 8080d7851bda 563355485435 8080d7851bda 38cdb8aa6330 8080d7851bda 8080d7851bda 8080d7851bda 563355485435 8080d7851bda 8080d7851bda 8080d7851bda 563355485435 38cdb8aa6330 8080d7851bda 8080d7851bda 563355485435 38cdb8aa6330 3e4e52b165b3 3e4e52b165b3 9a4574ef2c3b 3e4e52b165b3 38cdb8aa6330 3e4e52b165b3 3e4e52b165b3 9a4574ef2c3b 3e4e52b165b3 3e4e52b165b3 3e4e52b165b3 9a4574ef2c3b 3e4e52b165b3 3e4e52b165b3 3e4e52b165b3 3e4e52b165b3 2fbe78909086 3e4e52b165b3 3e4e52b165b3 9a4574ef2c3b 3e4e52b165b3 6b10a0a7e427 3e4e52b165b3 3e4e52b165b3 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 8ad265a65a08 8ad265a65a08 8ad265a65a08 8ad265a65a08 ae8f39381f4a ac10ea4ee895 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 2fbe78909086 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 875f736d67c5 875f736d67c5 875f736d67c5 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 875f736d67c5 875f736d67c5 875f736d67c5 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 875f736d67c5 875f736d67c5 875f736d67c5 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 875f736d67c5 875f736d67c5 875f736d67c5 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 12e04c248fb1 6b10a0a7e427 6b10a0a7e427 875f736d67c5 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 875f736d67c5 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 12e04c248fb1 6b10a0a7e427 875f736d67c5 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 875f736d67c5 6b10a0a7e427 875f736d67c5 875f736d67c5 875f736d67c5 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 12e04c248fb1 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 875f736d67c5 6b10a0a7e427 12e04c248fb1 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 875f736d67c5 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 9a4574ef2c3b 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 ac10ea4ee895 875f736d67c5 ac10ea4ee895 12e04c248fb1 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 875f736d67c5 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 6b10a0a7e427 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 4cdbdb71ceee 63dfd353c16e 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 12e04c248fb1 b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf b709da97f1bf | import datetime
import pytz
from decimal import Decimal
from django.core.exceptions import ValidationError
from registrasion.models import commerce
from registrasion.models import conditions
from registrasion.models import inventory
from controller_helpers import TestingCartController
from controller_helpers import TestingCreditNoteController
from controller_helpers import TestingInvoiceController
from test_cart import RegistrationCartTestCase
UTC = pytz.timezone('UTC')
class InvoiceTestCase(RegistrationCartTestCase):
def _invoice_containing_prod_1(self, qty=1):
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_1, qty)
return TestingInvoiceController.for_cart(self.reget(cart.cart))
def _credit_note_for_invoice(self, invoice):
note = commerce.CreditNote.objects.get(invoice=invoice)
return TestingCreditNoteController(note)
def test_create_invoice(self):
current_cart = TestingCartController.for_user(self.USER_1)
# Should be able to create an invoice after the product is added
current_cart.add_to_cart(self.PROD_1, 1)
invoice_1 = TestingInvoiceController.for_cart(current_cart.cart)
# That invoice should have a single line item
line_items = commerce.LineItem.objects.filter(
invoice=invoice_1.invoice,
)
self.assertEqual(1, len(line_items))
# That invoice should have a value equal to cost of PROD_1
self.assertEqual(self.PROD_1.price, invoice_1.invoice.value)
# Adding item to cart should produce a new invoice
current_cart.add_to_cart(self.PROD_2, 1)
invoice_2 = TestingInvoiceController.for_cart(current_cart.cart)
self.assertNotEqual(invoice_1.invoice, invoice_2.invoice)
# The old invoice should automatically be voided
invoice_1_new = commerce.Invoice.objects.get(pk=invoice_1.invoice.id)
invoice_2_new = commerce.Invoice.objects.get(pk=invoice_2.invoice.id)
self.assertTrue(invoice_1_new.is_void)
self.assertFalse(invoice_2_new.is_void)
# Invoice should have two line items
line_items = commerce.LineItem.objects.filter(
invoice=invoice_2.invoice,
)
self.assertEqual(2, len(line_items))
# Invoice should have a value equal to cost of PROD_1 and PROD_2
self.assertEqual(
self.PROD_1.price + self.PROD_2.price,
invoice_2.invoice.value)
def test_invoice_controller_for_id_works(self):
invoice = self._invoice_containing_prod_1(1)
id_ = invoice.invoice.id
invoice1 = TestingInvoiceController.for_id(id_)
invoice2 = TestingInvoiceController.for_id(str(id_))
self.assertEqual(invoice.invoice, invoice1.invoice)
self.assertEqual(invoice.invoice, invoice2.invoice)
def test_create_invoice_fails_if_cart_invalid(self):
self.make_ceiling("Limit ceiling", limit=1)
self.set_time(datetime.datetime(2015, 01, 01, tzinfo=UTC))
current_cart = TestingCartController.for_user(self.USER_1)
current_cart.add_to_cart(self.PROD_1, 1)
self.add_timedelta(self.RESERVATION * 2)
cart_2 = TestingCartController.for_user(self.USER_2)
cart_2.add_to_cart(self.PROD_1, 1)
# Now try to invoice the first user
with self.assertRaises(ValidationError):
TestingInvoiceController.for_cart(current_cart.cart)
def test_paying_invoice_makes_new_cart(self):
invoice = self._invoice_containing_prod_1(1)
invoice.pay("A payment!", invoice.invoice.value)
# This payment is for the correct amount invoice should be paid.
self.assertTrue(invoice.invoice.is_paid)
# Cart should not be active
self.assertNotEqual(
commerce.Cart.STATUS_ACTIVE,
invoice.invoice.cart.status,
)
# Asking for a cart should generate a new one
new_cart = TestingCartController.for_user(self.USER_1)
self.assertNotEqual(invoice.invoice.cart, new_cart.cart)
def test_invoice_includes_discounts(self):
voucher = inventory.Voucher.objects.create(
recipient="Voucher recipient",
code="VOUCHER",
limit=1
)
discount = conditions.VoucherDiscount.objects.create(
description="VOUCHER RECIPIENT",
voucher=voucher,
)
conditions.DiscountForProduct.objects.create(
discount=discount,
product=self.PROD_1,
percentage=Decimal(50),
quantity=1
)
current_cart = TestingCartController.for_user(self.USER_1)
current_cart.apply_voucher(voucher.code)
# Should be able to create an invoice after the product is added
current_cart.add_to_cart(self.PROD_1, 1)
invoice_1 = TestingInvoiceController.for_cart(current_cart.cart)
# That invoice should have two line items
line_items = commerce.LineItem.objects.filter(
invoice=invoice_1.invoice,
)
self.assertEqual(2, len(line_items))
# That invoice should have a value equal to 50% of the cost of PROD_1
self.assertEqual(
self.PROD_1.price * Decimal("0.5"),
invoice_1.invoice.value)
def test_zero_value_invoice_is_automatically_paid(self):
voucher = inventory.Voucher.objects.create(
recipient="Voucher recipient",
code="VOUCHER",
limit=1
)
discount = conditions.VoucherDiscount.objects.create(
description="VOUCHER RECIPIENT",
voucher=voucher,
)
conditions.DiscountForProduct.objects.create(
discount=discount,
product=self.PROD_1,
percentage=Decimal(100),
quantity=1
)
current_cart = TestingCartController.for_user(self.USER_1)
current_cart.apply_voucher(voucher.code)
# Should be able to create an invoice after the product is added
current_cart.add_to_cart(self.PROD_1, 1)
invoice_1 = TestingInvoiceController.for_cart(current_cart.cart)
self.assertTrue(invoice_1.invoice.is_paid)
def test_invoice_voids_self_if_cart_is_invalid(self):
current_cart = TestingCartController.for_user(self.USER_1)
# Should be able to create an invoice after the product is added
current_cart.add_to_cart(self.PROD_1, 1)
invoice_1 = TestingInvoiceController.for_cart(current_cart.cart)
self.assertFalse(invoice_1.invoice.is_void)
# Adding item to cart should produce a new invoice
current_cart.add_to_cart(self.PROD_2, 1)
invoice_2 = TestingInvoiceController.for_cart(current_cart.cart)
self.assertNotEqual(invoice_1.invoice, invoice_2.invoice)
# Viewing invoice_1's invoice should show it as void
invoice_1_new = TestingInvoiceController(invoice_1.invoice)
self.assertTrue(invoice_1_new.invoice.is_void)
# Viewing invoice_2's invoice should *not* show it as void
invoice_2_new = TestingInvoiceController(invoice_2.invoice)
self.assertFalse(invoice_2_new.invoice.is_void)
def test_voiding_invoice_creates_new_invoice(self):
invoice_1 = self._invoice_containing_prod_1(1)
self.assertFalse(invoice_1.invoice.is_void)
invoice_1.void()
invoice_2 = TestingInvoiceController.for_cart(invoice_1.invoice.cart)
self.assertNotEqual(invoice_1.invoice, invoice_2.invoice)
def test_cannot_pay_void_invoice(self):
invoice_1 = self._invoice_containing_prod_1(1)
invoice_1.void()
with self.assertRaises(ValidationError):
invoice_1.validate_allowed_to_pay()
def test_cannot_void_paid_invoice(self):
invoice = self._invoice_containing_prod_1(1)
invoice.pay("Reference", invoice.invoice.value)
with self.assertRaises(ValidationError):
invoice.void()
def test_cannot_void_partially_paid_invoice(self):
invoice = self._invoice_containing_prod_1(1)
invoice.pay("Reference", invoice.invoice.value - 1)
self.assertTrue(invoice.invoice.is_unpaid)
with self.assertRaises(ValidationError):
invoice.void()
def test_cannot_generate_blank_invoice(self):
current_cart = TestingCartController.for_user(self.USER_1)
with self.assertRaises(ValidationError):
TestingInvoiceController.for_cart(current_cart.cart)
def test_cannot_pay_implicitly_void_invoice(self):
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_1, 1)
invoice = TestingInvoiceController.for_cart(self.reget(cart.cart))
# Implicitly void the invoice
cart.add_to_cart(self.PROD_1, 1)
with self.assertRaises(ValidationError):
invoice.validate_allowed_to_pay()
def test_overpaid_invoice_results_in_credit_note(self):
invoice = self._invoice_containing_prod_1(1)
# Invoice is overpaid by 1 unit
to_pay = invoice.invoice.value + 1
invoice.pay("Reference", to_pay)
# The total paid should be equal to the value of the invoice only
self.assertEqual(invoice.invoice.value, invoice.total_payments())
self.assertTrue(invoice.invoice.is_paid)
# There should be a credit note generated out of the invoice.
credit_notes = commerce.CreditNote.objects.filter(
invoice=invoice.invoice,
)
self.assertEqual(1, credit_notes.count())
self.assertEqual(to_pay - invoice.invoice.value, credit_notes[0].value)
def test_full_paid_invoice_does_not_generate_credit_note(self):
invoice = self._invoice_containing_prod_1(1)
# Invoice is paid evenly
invoice.pay("Reference", invoice.invoice.value)
# The total paid should be equal to the value of the invoice only
self.assertEqual(invoice.invoice.value, invoice.total_payments())
self.assertTrue(invoice.invoice.is_paid)
# There should be no credit notes
credit_notes = commerce.CreditNote.objects.filter(
invoice=invoice.invoice,
)
self.assertEqual(0, credit_notes.count())
def test_refund_partially_paid_invoice_generates_correct_credit_note(self):
invoice = self._invoice_containing_prod_1(1)
# Invoice is underpaid by 1 unit
to_pay = invoice.invoice.value - 1
invoice.pay("Reference", to_pay)
invoice.refund()
# The total paid should be zero
self.assertEqual(0, invoice.total_payments())
self.assertTrue(invoice.invoice.is_void)
# There should be a credit note generated out of the invoice.
credit_notes = commerce.CreditNote.objects.filter(
invoice=invoice.invoice,
)
self.assertEqual(1, credit_notes.count())
self.assertEqual(to_pay, credit_notes[0].value)
def test_refund_fully_paid_invoice_generates_correct_credit_note(self):
invoice = self._invoice_containing_prod_1(1)
to_pay = invoice.invoice.value
invoice.pay("Reference", to_pay)
self.assertTrue(invoice.invoice.is_paid)
invoice.refund()
# The total paid should be zero
self.assertEqual(0, invoice.total_payments())
self.assertTrue(invoice.invoice.is_refunded)
# There should be a credit note generated out of the invoice.
credit_notes = commerce.CreditNote.objects.filter(
invoice=invoice.invoice,
)
self.assertEqual(1, credit_notes.count())
self.assertEqual(to_pay, credit_notes[0].value)
def test_apply_credit_note_pays_invoice(self):
invoice = self._invoice_containing_prod_1(1)
to_pay = invoice.invoice.value
invoice.pay("Reference", to_pay)
self.assertTrue(invoice.invoice.is_paid)
invoice.refund()
# There should be one credit note generated out of the invoice.
cn = self._credit_note_for_invoice(invoice.invoice)
# That credit note should be in the unclaimed pile
self.assertEquals(1, commerce.CreditNote.unclaimed().count())
# Create a new (identical) cart with invoice
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_1, 1)
invoice2 = TestingInvoiceController.for_cart(self.reget(cart.cart))
cn.apply_to_invoice(invoice2.invoice)
self.assertTrue(invoice2.invoice.is_paid)
# That invoice should not show up as unclaimed any more
self.assertEquals(0, commerce.CreditNote.unclaimed().count())
def test_apply_credit_note_generates_new_credit_note_if_overpaying(self):
invoice = self._invoice_containing_prod_1(2)
to_pay = invoice.invoice.value
invoice.pay("Reference", to_pay)
self.assertTrue(invoice.invoice.is_paid)
invoice.refund()
# There should be one credit note generated out of the invoice.
cn = self._credit_note_for_invoice(invoice.invoice)
self.assertEquals(1, commerce.CreditNote.unclaimed().count())
# Create a new cart (of half value of inv 1) and get invoice
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_1, 1)
invoice2 = TestingInvoiceController.for_cart(self.reget(cart.cart))
cn.apply_to_invoice(invoice2.invoice)
self.assertTrue(invoice2.invoice.is_paid)
# We generated a new credit note, and spent the old one,
# unclaimed should still be 1.
self.assertEquals(1, commerce.CreditNote.unclaimed().count())
credit_note2 = commerce.CreditNote.objects.get(
invoice=invoice2.invoice,
)
# The new credit note should be the residual of the cost of cart 1
# minus the cost of cart 2.
self.assertEquals(
invoice.invoice.value - invoice2.invoice.value,
credit_note2.value,
)
def test_cannot_apply_credit_note_on_invalid_invoices(self):
invoice = self._invoice_containing_prod_1(1)
to_pay = invoice.invoice.value
invoice.pay("Reference", to_pay)
self.assertTrue(invoice.invoice.is_paid)
invoice.refund()
# There should be one credit note generated out of the invoice.
cn = self._credit_note_for_invoice(invoice.invoice)
# Create a new cart with invoice, pay it
invoice_2 = self._invoice_containing_prod_1(1)
invoice_2.pay("LOL", invoice_2.invoice.value)
# Cannot pay paid invoice
with self.assertRaises(ValidationError):
cn.apply_to_invoice(invoice_2.invoice)
invoice_2.refund()
# Cannot pay refunded invoice
with self.assertRaises(ValidationError):
cn.apply_to_invoice(invoice_2.invoice)
# Create a new cart with invoice
invoice_2 = self._invoice_containing_prod_1(1)
invoice_2.void()
# Cannot pay void invoice
with self.assertRaises(ValidationError):
cn.apply_to_invoice(invoice_2.invoice)
def test_cannot_apply_a_refunded_credit_note(self):
invoice = self._invoice_containing_prod_1(1)
to_pay = invoice.invoice.value
invoice.pay("Reference", to_pay)
self.assertTrue(invoice.invoice.is_paid)
invoice.refund()
self.assertEquals(1, commerce.CreditNote.unclaimed().count())
cn = self._credit_note_for_invoice(invoice.invoice)
cn.refund()
# Refunding a credit note should mark it as claimed
self.assertEquals(0, commerce.CreditNote.unclaimed().count())
# Create a new cart with invoice
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_1, 1)
invoice_2 = TestingInvoiceController.for_cart(self.reget(cart.cart))
# Cannot pay with this credit note.
with self.assertRaises(ValidationError):
cn.apply_to_invoice(invoice_2.invoice)
def test_cannot_refund_an_applied_credit_note(self):
invoice = self._invoice_containing_prod_1(1)
to_pay = invoice.invoice.value
invoice.pay("Reference", to_pay)
self.assertTrue(invoice.invoice.is_paid)
invoice.refund()
self.assertEquals(1, commerce.CreditNote.unclaimed().count())
cn = self._credit_note_for_invoice(invoice.invoice)
# Create a new cart with invoice
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_1, 1)
invoice_2 = TestingInvoiceController.for_cart(self.reget(cart.cart))
cn.apply_to_invoice(invoice_2.invoice)
self.assertEquals(0, commerce.CreditNote.unclaimed().count())
# Cannot refund this credit note as it is already applied.
with self.assertRaises(ValidationError):
cn.refund()
def test_money_into_void_invoice_generates_credit_note(self):
invoice = self._invoice_containing_prod_1(1)
invoice.void()
val = invoice.invoice.value
invoice.pay("Paying into the void.", val, pre_validate=False)
cn = self._credit_note_for_invoice(invoice.invoice)
self.assertEqual(val, cn.credit_note.value)
def test_money_into_refunded_invoice_generates_credit_note(self):
invoice = self._invoice_containing_prod_1(1)
val = invoice.invoice.value
invoice.pay("Paying the first time.", val)
invoice.refund()
cnval = val - 1
invoice.pay("Paying into the void.", cnval, pre_validate=False)
notes = commerce.CreditNote.objects.filter(invoice=invoice.invoice)
notes = sorted(notes, key=lambda note: note.value)
self.assertEqual(cnval, notes[0].value)
self.assertEqual(val, notes[1].value)
def test_money_into_paid_invoice_generates_credit_note(self):
invoice = self._invoice_containing_prod_1(1)
val = invoice.invoice.value
invoice.pay("Paying the first time.", val)
invoice.pay("Paying into the void.", val, pre_validate=False)
cn = self._credit_note_for_invoice(invoice.invoice)
self.assertEqual(val, cn.credit_note.value)
def test_required_category_constraints_prevent_invoicing(self):
self.CAT_1.required = True
self.CAT_1.save()
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_3, 1)
# CAT_1 is required, we don't have CAT_1 yet
with self.assertRaises(ValidationError):
invoice = TestingInvoiceController.for_cart(cart.cart)
# Now that we have CAT_1, we can check out the cart
cart.add_to_cart(self.PROD_1, 1)
invoice = TestingInvoiceController.for_cart(cart.cart)
# Paying for the invoice should work fine
invoice.pay("Boop", invoice.invoice.value)
# We have an item in the first cart, so should be able to invoice
# for the second cart, even without CAT_1 in it.
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_3, 1)
invoice2 = TestingInvoiceController.for_cart(cart.cart)
# Void invoice2, and release the first cart
# now we don't have any CAT_1
invoice2.void()
invoice.refund()
# Now that we don't have CAT_1, we can't checkout this cart
with self.assertRaises(ValidationError):
invoice = TestingInvoiceController.for_cart(cart.cart)
|