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.

JournalEntryRepository.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Repositories\Accounting;
  3. use App\Models\Accounting\Account;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Support\Carbon;
  6. class JournalEntryRepository
  7. {
  8. public function sumAmounts(Account $account, string $type, ?string $startDate = null, ?string $endDate = null): int
  9. {
  10. $query = $account->journalEntries()->where('type', $type);
  11. if ($startDate && $endDate) {
  12. $endOfDay = Carbon::parse($endDate)->endOfDay();
  13. $query->whereHas('transaction', static function (Builder $query) use ($startDate, $endOfDay) {
  14. $query->whereBetween('posted_at', [$startDate, $endOfDay]);
  15. });
  16. } elseif ($startDate) {
  17. $query->whereHas('transaction', static function (Builder $query) use ($startDate) {
  18. $query->where('posted_at', '<=', $startDate);
  19. });
  20. }
  21. return $query->sum('amount');
  22. }
  23. public function sumDebitAmounts(Account $account, string $startDate, ?string $endDate = null): int
  24. {
  25. return $this->sumAmounts($account, 'debit', $startDate, $endDate);
  26. }
  27. public function sumCreditAmounts(Account $account, string $startDate, ?string $endDate = null): int
  28. {
  29. return $this->sumAmounts($account, 'credit', $startDate, $endDate);
  30. }
  31. }