MutationService.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. if ($collections->count()) {
  30. return $this->setRupiahFormat($this->totalIncome($collections), true);
  31. } else {
  32. return $this->setRupiahFormat(0, true);
  33. }
  34. }
  35. public function totalExpenseAsString(EloquentCollection $collections)
  36. {
  37. if ($collections->count()) {
  38. return $this->setRupiahFormat($this->totalExpense($collections), true);
  39. } else {
  40. return $this->setRupiahFormat(0, true);
  41. }
  42. }
  43. public function totalAmountAsString(EloquentCollection $collections)
  44. {
  45. if ($collections->count()) {
  46. $amount = $this->totalIncome($collections) - $this->totalExpense($collections);
  47. } else {
  48. $amount = 0;
  49. }
  50. return $this->setRupiahFormat($amount, true);
  51. }
  52. public function totalPerMonth(EloquentCollection $collections)
  53. {
  54. return $collections->transform(fn($collection) => $collection->sum(fn($collect) => $collect->getRawOriginal('amount')));
  55. }
  56. public function statisticData(SupportCollection $collections, int $take = -1)
  57. {
  58. $collections = $collections->take($take);
  59. $collections->transform(fn($collections) => $this->totalPerMonth($collections));
  60. return $collections;
  61. }
  62. }