TransactionService.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Services;
  3. use App\Services\CurrencyFormatService;
  4. use Illuminate\Database\Eloquent\Collection as EloquentCollection;
  5. use Illuminate\Pagination\LengthAwarePaginator;
  6. use Illuminate\Support\Collection as SupportCollection;
  7. class TransactionService
  8. {
  9. public function getPaginator(array $items)
  10. {
  11. $total = count($items);
  12. $page = request('page') ?? 1;
  13. $perPage = 10;
  14. $offset = ($page - 1) * $perPage;
  15. $items = array_slice($items, $offset, $perPage);
  16. return new LengthAwarePaginator($items, $total, $perPage, $page, [
  17. 'path' => request()->url(),
  18. 'query' => request()->query(),
  19. ]);
  20. }
  21. public function totalPrice(EloquentCollection $collections)
  22. {
  23. return $collections->transform(fn($transactions) => $transactions->totalPrice());
  24. }
  25. public function totalPriceGroup(EloquentCollection $collections)
  26. {
  27. return $this->totalPrice($collections)->sum();
  28. }
  29. public function totalPriceGroupAsString(EloquentCollection $collections)
  30. {
  31. return (new CurrencyFormatService)->setRupiahFormat($this->totalPriceGroup($collections), true);
  32. }
  33. public function totalPerMonth(EloquentCollection $collections)
  34. {
  35. return $collections->transform(fn($collection) => $collection->count());
  36. }
  37. public function statisticData(SupportCollection $collections, int $take = -1)
  38. {
  39. $collections = $collections->take($take);
  40. $collections->transform(fn($collections) => $this->totalPerMonth($collections));
  41. return $collections;
  42. }
  43. }