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.

HasJournalEntryActions.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Traits;
  3. use App\Enums\Accounting\JournalEntryType;
  4. trait HasJournalEntryActions
  5. {
  6. public string $debitAmount = '0.00';
  7. public string $creditAmount = '0.00';
  8. public function setDebitAmount(string $amount): void
  9. {
  10. $this->debitAmount = $amount;
  11. }
  12. public function getDebitAmount(): string
  13. {
  14. return $this->debitAmount;
  15. }
  16. public function getFormattedDebitAmount(): string
  17. {
  18. return money($this->getDebitAmount(), 'USD', true)->format();
  19. }
  20. public function setCreditAmount(string $amount): void
  21. {
  22. $this->creditAmount = $amount;
  23. }
  24. public function getCreditAmount(): string
  25. {
  26. return $this->creditAmount;
  27. }
  28. public function getFormattedCreditAmount(): string
  29. {
  30. return money($this->getCreditAmount(), 'USD', true)->format();
  31. }
  32. public function getBalanceDifference(): string
  33. {
  34. return bcsub($this->getDebitAmount(), $this->getCreditAmount(), 2);
  35. }
  36. public function getFormattedBalanceDifference(): string
  37. {
  38. $difference = $this->getBalanceDifference();
  39. $absoluteDifference = abs((float) $difference);
  40. return money($absoluteDifference, 'USD', true)->format();
  41. }
  42. public function isJournalEntryBalanced(): bool
  43. {
  44. return bccomp($this->getDebitAmount(), $this->getCreditAmount(), 2) === 0;
  45. }
  46. public function resetJournalEntryAmounts(): void
  47. {
  48. $this->reset(['debitAmount', 'creditAmount']);
  49. }
  50. public function adjustJournalEntryAmountsForTypeChange(JournalEntryType $newType, JournalEntryType $oldType, ?string $amount): void
  51. {
  52. if ($newType !== $oldType) {
  53. $normalizedAmount = $amount === null ? '0.00' : rtrim($amount, '.');
  54. $normalizedAmount = $this->sanitizeAndFormatAmount($normalizedAmount);
  55. if (bccomp($normalizedAmount, '0.00', 2) === 0) {
  56. return;
  57. }
  58. if ($oldType->isDebit() && $newType->isCredit()) {
  59. $this->setDebitAmount(bcsub($this->getDebitAmount(), $normalizedAmount, 2));
  60. $this->setCreditAmount(bcadd($this->getCreditAmount(), $normalizedAmount, 2));
  61. } elseif ($oldType->isCredit() && $newType->isDebit()) {
  62. $this->setDebitAmount(bcadd($this->getDebitAmount(), $normalizedAmount, 2));
  63. $this->setCreditAmount(bcsub($this->getCreditAmount(), $normalizedAmount, 2));
  64. }
  65. }
  66. }
  67. public function updateJournalEntryAmount(JournalEntryType $journalEntryType, ?string $newAmount, ?string $oldAmount): void
  68. {
  69. if ($newAmount === $oldAmount) {
  70. return;
  71. }
  72. $normalizedNewAmount = $newAmount === null ? '0.00' : rtrim($newAmount, '.');
  73. $normalizedOldAmount = $oldAmount === null ? '0.00' : rtrim($oldAmount, '.');
  74. $formattedNewAmount = $this->sanitizeAndFormatAmount($normalizedNewAmount);
  75. $formattedOldAmount = $this->sanitizeAndFormatAmount($normalizedOldAmount);
  76. $difference = bcsub($formattedNewAmount, $formattedOldAmount, 2);
  77. if ($journalEntryType->isDebit()) {
  78. $this->setDebitAmount(bcadd($this->getDebitAmount(), $difference, 2));
  79. } else {
  80. $this->setCreditAmount(bcadd($this->getCreditAmount(), $difference, 2));
  81. }
  82. }
  83. protected function sanitizeAndFormatAmount(string $amount): string
  84. {
  85. return (string) money($amount, 'USD')->getAmount();
  86. }
  87. }