You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransactionObserver.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Observers;
  3. use App\Enums\Accounting\BillStatus;
  4. use App\Enums\Accounting\InvoiceStatus;
  5. use App\Models\Accounting\Bill;
  6. use App\Models\Accounting\Invoice;
  7. use App\Models\Accounting\Transaction;
  8. use App\Services\TransactionService;
  9. use App\Utilities\Currency\CurrencyConverter;
  10. use Illuminate\Database\Eloquent\Builder;
  11. use Illuminate\Support\Facades\DB;
  12. class TransactionObserver
  13. {
  14. public function __construct(
  15. protected TransactionService $transactionService,
  16. ) {}
  17. /**
  18. * Handle the Transaction "saving" event.
  19. */
  20. public function saving(Transaction $transaction): void
  21. {
  22. if ($transaction->type->isTransfer() && $transaction->description === null) {
  23. $transaction->description = 'Account Transfer';
  24. }
  25. }
  26. /**
  27. * Handle the Transaction "created" event.
  28. */
  29. public function created(Transaction $transaction): void
  30. {
  31. $this->transactionService->createJournalEntries($transaction);
  32. if (! $transaction->transactionable) {
  33. return;
  34. }
  35. $document = $transaction->transactionable;
  36. if ($document instanceof Invoice) {
  37. $this->updateInvoiceTotals($document);
  38. } elseif ($document instanceof Bill) {
  39. $this->updateBillTotals($document);
  40. }
  41. }
  42. /**
  43. * Handle the Transaction "updated" event.
  44. */
  45. public function updated(Transaction $transaction): void
  46. {
  47. $transaction->refresh(); // DO NOT REMOVE
  48. $this->transactionService->updateJournalEntries($transaction);
  49. if (! $transaction->transactionable) {
  50. return;
  51. }
  52. $document = $transaction->transactionable;
  53. if ($document instanceof Invoice) {
  54. $this->updateInvoiceTotals($document);
  55. } elseif ($document instanceof Bill) {
  56. $this->updateBillTotals($document);
  57. }
  58. }
  59. /**
  60. * Handle the Transaction "deleting" event.
  61. */
  62. public function deleting(Transaction $transaction): void
  63. {
  64. DB::transaction(function () use ($transaction) {
  65. $this->transactionService->deleteJournalEntries($transaction);
  66. if (! $transaction->transactionable) {
  67. return;
  68. }
  69. $document = $transaction->transactionable;
  70. if (($document instanceof Invoice || $document instanceof Bill) && ! $document->exists) {
  71. return;
  72. }
  73. if ($document instanceof Invoice) {
  74. $this->updateInvoiceTotals($document, $transaction);
  75. } elseif ($document instanceof Bill) {
  76. $this->updateBillTotals($document, $transaction);
  77. }
  78. });
  79. }
  80. public function deleted(Transaction $transaction): void
  81. {
  82. //
  83. }
  84. protected function updateInvoiceTotals(Invoice $invoice, ?Transaction $excludedTransaction = null): void
  85. {
  86. if (! $invoice->hasPayments()) {
  87. return;
  88. }
  89. $depositTotal = (int) $invoice->deposits()
  90. ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
  91. ->sum('amount');
  92. $withdrawalTotal = (int) $invoice->withdrawals()
  93. ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
  94. ->sum('amount');
  95. $totalPaid = $depositTotal - $withdrawalTotal;
  96. $invoiceTotal = (int) $invoice->getRawOriginal('total');
  97. $newStatus = match (true) {
  98. $totalPaid > $invoiceTotal => InvoiceStatus::Overpaid,
  99. $totalPaid === $invoiceTotal => InvoiceStatus::Paid,
  100. default => InvoiceStatus::Partial,
  101. };
  102. $paidAt = $invoice->paid_at;
  103. if (in_array($newStatus, [InvoiceStatus::Paid, InvoiceStatus::Overpaid]) && ! $paidAt) {
  104. $paidAt = $invoice->deposits()
  105. ->latest('posted_at')
  106. ->value('posted_at');
  107. }
  108. $invoice->update([
  109. 'amount_paid' => CurrencyConverter::convertCentsToFloat($totalPaid),
  110. 'status' => $newStatus,
  111. 'paid_at' => $paidAt,
  112. ]);
  113. }
  114. protected function updateBillTotals(Bill $bill, ?Transaction $excludedTransaction = null): void
  115. {
  116. if (! $bill->hasPayments()) {
  117. return;
  118. }
  119. $withdrawalTotal = (int) $bill->withdrawals()
  120. ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
  121. ->sum('amount');
  122. $totalPaid = $withdrawalTotal;
  123. $billTotal = (int) $bill->getRawOriginal('total');
  124. $newStatus = match (true) {
  125. $totalPaid >= $billTotal => BillStatus::Paid,
  126. default => BillStatus::Partial,
  127. };
  128. $paidAt = $bill->paid_at;
  129. if ($newStatus === BillStatus::Paid && ! $paidAt) {
  130. $paidAt = $bill->withdrawals()
  131. ->latest('posted_at')
  132. ->value('posted_at');
  133. }
  134. $bill->update([
  135. 'amount_paid' => CurrencyConverter::convertCentsToFloat($totalPaid),
  136. 'status' => $newStatus,
  137. 'paid_at' => $paidAt,
  138. ]);
  139. }
  140. }