TransactionService.php 1.1KB

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