HasMutation.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Models\Helpers;
  3. use Illuminate\Database\Eloquent\Collection;
  4. trait HasMutation
  5. {
  6. use CurrencyFormat;
  7. protected static function totalIncome(Collection $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. protected static function totalExpense(Collection $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. protected static function totalIncomeAsString(Collection $collections)
  28. {
  29. return parent::setRupiahFormat(self::totalIncome($collections), 0, true);
  30. }
  31. protected static function totalExpenseAsString(Collection $collections)
  32. {
  33. return '- ' . parent::setRupiahFormat(self::totalExpense($collections), 0, true);
  34. }
  35. protected static function totalAmount(Collection $collections)
  36. {
  37. $amount = self::totalIncome($collections) - self::totalExpense($collections);
  38. if ($amount < 0) {
  39. return '- ' . parent::setRupiahFormat(abs($amount), 0, true);
  40. } else {
  41. return parent::setRupiahFormat($amount, 0, true);
  42. }
  43. }
  44. }