Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JournalEntryCollection.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Collections\Accounting;
  3. use App\Models\Accounting\JournalEntry;
  4. use App\Utilities\Currency\CurrencyAccessor;
  5. use App\ValueObjects\Money;
  6. use Illuminate\Database\Eloquent\Collection;
  7. class JournalEntryCollection extends Collection
  8. {
  9. public function sumDebits(): Money
  10. {
  11. $total = $this->reduce(static function ($carry, JournalEntry $item) {
  12. if ($item->type->isDebit()) {
  13. $amountAsInteger = $item->amount;
  14. return bcadd($carry, $amountAsInteger, 0);
  15. }
  16. return $carry;
  17. }, 0);
  18. return new Money($total, CurrencyAccessor::getDefaultCurrency());
  19. }
  20. public function sumCredits(): Money
  21. {
  22. $total = $this->reduce(static function ($carry, JournalEntry $item) {
  23. if ($item->type->isCredit()) {
  24. $amountAsInteger = $item->amount;
  25. return bcadd($carry, $amountAsInteger, 0);
  26. }
  27. return $carry;
  28. }, 0);
  29. return new Money($total, CurrencyAccessor::getDefaultCurrency());
  30. }
  31. public function areBalanced(): bool
  32. {
  33. return $this->sumDebits()->getAmount() === $this->sumCredits()->getAmount();
  34. }
  35. }