MutationService.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Database\Eloquent\Collection as EloquentCollection;
  4. use Illuminate\Support\Collection as SupportCollection;
  5. class MutationService extends CurrencyFormatService
  6. {
  7. public function totalIncome(EloquentCollection $collections)
  8. {
  9. foreach ($collections->chunk(100) as $chunk) {
  10. return $chunk->sum(function ($collect) {
  11. if ($collect->getRawOriginal('type') == 1) {
  12. return $collect->getRawOriginal('amount');
  13. }
  14. });
  15. }
  16. }
  17. public function totalExpense(EloquentCollection $collections)
  18. {
  19. foreach ($collections->chunk(100) as $chunk) {
  20. return $chunk->sum(function ($collect) {
  21. if ($collect->getRawOriginal('type') == 2) {
  22. return $collect->getRawOriginal('amount');
  23. }
  24. });
  25. }
  26. }
  27. public function totalIncomeAsString(EloquentCollection $collections)
  28. {
  29. return $this->setRupiahFormat($this->totalIncome($collections), true);
  30. }
  31. public function totalExpenseAsString(EloquentCollection $collections)
  32. {
  33. return $this->setRupiahFormat($this->totalExpense($collections), true);
  34. }
  35. public function totalAmount(EloquentCollection $collections)
  36. {
  37. $amount = $this->totalIncome($collections) - $this->totalExpense($collections);
  38. return $this->setRupiahFormat($amount, true);
  39. }
  40. public function totalPerMonth(EloquentCollection $collections)
  41. {
  42. return $collections->transform(fn($collection) => $collection->sum(fn($collect) => $collect->getRawOriginal('amount')));
  43. }
  44. public function statisticData(SupportCollection $collections, int $take = -1)
  45. {
  46. $collections = $collections->take($take);
  47. $collections->transform(fn($collections) => $this->totalPerMonth($collections));
  48. return $collections;
  49. }
  50. }