MutationService.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Database\Eloquent\Collection;
  4. class MutationService extends CurrencyFormatService
  5. {
  6. public function totalIncome(Collection $collections)
  7. {
  8. foreach ($collections->chunk(100) as $chunk) {
  9. return $chunk->sum(function ($collect) {
  10. if ($collect->getRawOriginal('type') == 1) {
  11. return $collect->getRawOriginal('amount');
  12. }
  13. });
  14. }
  15. }
  16. public function totalExpense(Collection $collections)
  17. {
  18. foreach ($collections->chunk(100) as $chunk) {
  19. return $chunk->sum(function ($collect) {
  20. if ($collect->getRawOriginal('type') == 2) {
  21. return $collect->getRawOriginal('amount');
  22. }
  23. });
  24. }
  25. }
  26. public function totalIncomeAsString(Collection $collections)
  27. {
  28. return $this->setRupiahFormat($this->totalIncome($collections), true);
  29. }
  30. public function totalExpenseAsString(Collection $collections)
  31. {
  32. return $this->setRupiahFormat($this->totalExpense($collections), true);
  33. }
  34. public function totalAmount(Collection $collections)
  35. {
  36. $amount = $this->totalIncome($collections) - $this->totalExpense($collections);
  37. return $this->setRupiahFormat($amount, true);
  38. }
  39. }