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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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->is_payment) {
  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->is_payment) {
  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->is_payment) {
  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. $depositTotal = (int) $invoice->deposits()
  87. ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
  88. ->sum('amount');
  89. $withdrawalTotal = (int) $invoice->withdrawals()
  90. ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
  91. ->sum('amount');
  92. $totalPaid = $depositTotal - $withdrawalTotal;
  93. $invoiceTotal = (int) $invoice->getRawOriginal('total');
  94. $newStatus = match (true) {
  95. $totalPaid > $invoiceTotal => InvoiceStatus::Overpaid,
  96. $totalPaid === $invoiceTotal => InvoiceStatus::Paid,
  97. default => InvoiceStatus::Partial,
  98. };
  99. $paidAt = $invoice->paid_at;
  100. if (in_array($newStatus, [InvoiceStatus::Paid, InvoiceStatus::Overpaid]) && ! $paidAt) {
  101. $paidAt = $invoice->deposits()
  102. ->latest('posted_at')
  103. ->value('posted_at');
  104. }
  105. $invoice->update([
  106. 'amount_paid' => CurrencyConverter::convertCentsToFloat($totalPaid),
  107. 'status' => $newStatus,
  108. 'paid_at' => $paidAt,
  109. ]);
  110. }
  111. protected function updateBillTotals(Bill $bill, ?Transaction $excludedTransaction = null): void
  112. {
  113. $withdrawalTotal = (int) $bill->withdrawals()
  114. ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
  115. ->sum('amount');
  116. $totalPaid = $withdrawalTotal;
  117. $billTotal = (int) $bill->getRawOriginal('total');
  118. $newStatus = match (true) {
  119. $totalPaid >= $billTotal => BillStatus::Paid,
  120. default => BillStatus::Partial,
  121. };
  122. $paidAt = $bill->paid_at;
  123. if ($newStatus === BillStatus::Paid && ! $paidAt) {
  124. $paidAt = $bill->withdrawals()
  125. ->latest('posted_at')
  126. ->value('posted_at');
  127. }
  128. $bill->update([
  129. 'amount_paid' => CurrencyConverter::convertCentsToFloat($totalPaid),
  130. 'status' => $newStatus,
  131. 'paid_at' => $paidAt,
  132. ]);
  133. }
  134. }