MutationService.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. return $collections->sum(function ($collect) {
  10. if ($collect->getRawOriginal('type') == 1) {
  11. return $collect->getRawOriginal('amount');
  12. }
  13. });
  14. }
  15. public function totalExpense(EloquentCollection $collections)
  16. {
  17. return $collections->sum(function ($collect) {
  18. if ($collect->getRawOriginal('type') == 2) {
  19. return $collect->getRawOriginal('amount');
  20. }
  21. });
  22. }
  23. public function totalIncomeAsString(EloquentCollection $collections)
  24. {
  25. if ($collections->count()) {
  26. return $this->setRupiahFormat($this->totalIncome($collections), true);
  27. } else {
  28. return $this->setRupiahFormat(0, true);
  29. }
  30. }
  31. public function totalExpenseAsString(EloquentCollection $collections)
  32. {
  33. if ($collections->count()) {
  34. return $this->setRupiahFormat($this->totalExpense($collections), true);
  35. } else {
  36. return $this->setRupiahFormat(0, true);
  37. }
  38. }
  39. public function totalAmountAsString(EloquentCollection $collections)
  40. {
  41. if ($collections->count()) {
  42. $amount = $this->totalIncome($collections) - $this->totalExpense($collections);
  43. } else {
  44. $amount = 0;
  45. }
  46. return $this->setRupiahFormat($amount, true);
  47. }
  48. public function totalPerMonth(EloquentCollection $collections)
  49. {
  50. return $collections->transform(fn($collection) => $collection->sum(fn($collect) => $collect->getRawOriginal('amount')));
  51. }
  52. public function statisticData(SupportCollection $collections, int $take = -1)
  53. {
  54. $collections = $collections->take($take);
  55. $collections->transform(fn($collections) => $this->totalPerMonth($collections));
  56. return $collections;
  57. }
  58. }