TransactionService.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 extends CurrencyFormatService
  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($collect) => $collect->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 $this->setRupiahFormat($this->totalPriceGroup($collections), true);
  32. }
  33. public function totalDiscountGivenGroupAsString(EloquentCollection $collections)
  34. {
  35. return $this->setRupiahFormat($collections->sum(fn($collect) => $collect->totalDiscountGiven()), true);
  36. }
  37. public function totalPerMonth(EloquentCollection $collections)
  38. {
  39. return $collections->transform(fn($collection) => $collection->count());
  40. }
  41. public function statisticData(SupportCollection $collections, int $take = -1)
  42. {
  43. $collections = $collections->take($take);
  44. $collections->transform(fn($collections) => $this->totalPerMonth($collections));
  45. return $collections;
  46. }
  47. public function topTransaction(EloquentCollection $collections, int $take = 5)
  48. {
  49. return $collections
  50. ->transform(fn($collect) => [[
  51. 'customerNumber' => $collect->first()->customer->customer_number,
  52. 'name' => $collect->first()->customer->name,
  53. 'totalPrice' => $this->totalPriceGroup($collect),
  54. ]])
  55. ->sortByDesc('totalPrice')
  56. ->take($take)
  57. ->flatten(1);
  58. }
  59. }