TransactionService.php 2.6KB

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