MutationService.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Database\Eloquent\Collection as EloquentCollection;
  4. use Illuminate\Support\Collection as SupportCollection;
  5. class MutationService
  6. {
  7. public static 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 static 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 static function totalIncomeAsString(EloquentCollection $collections)
  24. {
  25. if ($collections->count()) {
  26. return HelperService::setRupiahFormat(
  27. self::totalIncome($collections),
  28. true
  29. );
  30. } else {
  31. return HelperService::setRupiahFormat(0, true);
  32. }
  33. }
  34. public static function totalExpenseAsString(EloquentCollection $collections)
  35. {
  36. if ($collections->count()) {
  37. return HelperService::setRupiahFormat(
  38. self::totalExpense($collections),
  39. true
  40. );
  41. } else {
  42. return HelperService::setRupiahFormat(0, true);
  43. }
  44. }
  45. public static function totalAmountAsString(EloquentCollection $collections)
  46. {
  47. if ($collections->count()) {
  48. $amount = self::totalIncome($collections) - self::totalExpense($collections);
  49. } else {
  50. $amount = 0;
  51. }
  52. return HelperService::setRupiahFormat($amount, true);
  53. }
  54. public static function totalPerMonth(EloquentCollection $collections)
  55. {
  56. return $collections->transform(fn($collection) =>
  57. $collection->sum(fn($collect) =>
  58. $collect->getRawOriginal('amount')
  59. )
  60. );
  61. }
  62. public static function statistic(SupportCollection $collections)
  63. {
  64. $collections = $collections;
  65. $collections->transform(fn($collections) =>
  66. self::totalPerMonth($collections));
  67. return $collections;
  68. }
  69. }