Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

InvoiceTest.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. use App\Enums\Accounting\InvoiceStatus;
  3. use App\Models\Accounting\Invoice;
  4. use App\Utilities\Currency\CurrencyAccessor;
  5. beforeEach(function () {
  6. $this->defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  7. $this->withOfferings();
  8. });
  9. it('creates a basic invoice with line items and calculates totals correctly', function () {
  10. $invoice = Invoice::factory()
  11. ->withLineItems(2)
  12. ->create();
  13. $invoice->refresh();
  14. expect($invoice)
  15. ->hasLineItems()->toBeTrue()
  16. ->lineItems->count()->toBe(2)
  17. ->subtotal->toBeGreaterThan(0)
  18. ->total->toBeGreaterThan(0)
  19. ->amount_due->toBe($invoice->total);
  20. });
  21. describe('invoice approval', function () {
  22. beforeEach(function () {
  23. $this->invoice = Invoice::factory()
  24. ->withLineItems()
  25. ->approved()
  26. ->create();
  27. });
  28. test('approved invoices are marked as Unsent when not Overdue', function () {
  29. $this->invoice->update(['due_date' => now()->addDays(30)]);
  30. $this->invoice->refresh();
  31. expect($this->invoice)
  32. ->hasLineItems()->toBeTrue()
  33. ->status->toBe(InvoiceStatus::Unsent)
  34. ->wasApproved()->toBeTrue()
  35. ->approvalTransaction->not->toBeNull();
  36. });
  37. });
  38. it('creates sent invoices with line items and approval automatically', function () {
  39. $invoice = Invoice::factory()
  40. ->withLineItems()
  41. ->sent()
  42. ->create();
  43. $invoice->refresh();
  44. expect($invoice)
  45. ->hasLineItems()->toBeTrue()
  46. ->lineItems->count()->toBeGreaterThan(0)
  47. ->wasApproved()->toBeTrue()
  48. ->hasBeenSent()->toBeTrue()
  49. ->status->toBe(InvoiceStatus::Sent);
  50. });
  51. it('creates paid invoices with line items, approval, and payments automatically', function () {
  52. $invoice = Invoice::factory()
  53. ->withLineItems()
  54. ->paid()
  55. ->create();
  56. $invoice->refresh();
  57. expect($invoice)
  58. ->hasLineItems()->toBeTrue()
  59. ->lineItems->count()->toBeGreaterThan(0)
  60. ->wasApproved()->toBeTrue()
  61. ->hasBeenSent()->toBeTrue()
  62. ->hasPayments()->toBeTrue()
  63. ->isPaid()->toBeTrue()
  64. ->status->toBe(InvoiceStatus::Paid);
  65. });
  66. it('creates partial invoices with line items and partial payments automatically', function () {
  67. $invoice = Invoice::factory()
  68. ->withLineItems()
  69. ->partial()
  70. ->create();
  71. $invoice->refresh();
  72. expect($invoice)
  73. ->hasLineItems()->toBeTrue()
  74. ->lineItems->count()->toBeGreaterThan(0)
  75. ->wasApproved()->toBeTrue()
  76. ->hasBeenSent()->toBeTrue()
  77. ->hasPayments()->toBeTrue()
  78. ->status->toBeIn([InvoiceStatus::Partial, InvoiceStatus::Overdue])
  79. ->amount_paid->toBeGreaterThan(0)
  80. ->amount_paid->toBeLessThan($invoice->total);
  81. });
  82. it('creates overpaid invoices with line items and overpayments automatically', function () {
  83. $invoice = Invoice::factory()
  84. ->withLineItems()
  85. ->overpaid()
  86. ->create();
  87. $invoice->refresh();
  88. expect($invoice)
  89. ->hasLineItems()->toBeTrue()
  90. ->lineItems->count()->toBeGreaterThan(0)
  91. ->wasApproved()->toBeTrue()
  92. ->hasBeenSent()->toBeTrue()
  93. ->hasPayments()->toBeTrue()
  94. ->status->toBe(InvoiceStatus::Overpaid)
  95. ->amount_paid->toBeGreaterThan($invoice->total);
  96. });
  97. it('creates overdue invoices with line items and approval automatically', function () {
  98. $invoice = Invoice::factory()
  99. ->withLineItems()
  100. ->overdue()
  101. ->create();
  102. $invoice->refresh();
  103. expect($invoice)
  104. ->hasLineItems()->toBeTrue()
  105. ->lineItems->count()->toBeGreaterThan(0)
  106. ->wasApproved()->toBeTrue()
  107. ->status->toBe(InvoiceStatus::Overdue)
  108. ->due_date->toBeLessThan(now());
  109. });
  110. it('handles factory configure method without duplicate line items', function () {
  111. $invoice = Invoice::factory()
  112. ->withLineItems(2)
  113. ->create();
  114. $invoice->refresh();
  115. expect($invoice)
  116. ->hasLineItems()->toBeTrue()
  117. ->lineItems->count()->toBe(2)
  118. ->invoice_number->toStartWith('INV-')
  119. ->order_number->toStartWith('ORD-');
  120. });