TransactionService.php 2.8KB

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