CartService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Cart;
  4. use App\Models\Client;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Database\Eloquent\Collection;
  7. use Illuminate\Support\Facades\DB;
  8. class CartService
  9. {
  10. public function getAll(): Collection
  11. {
  12. return Cart::query()
  13. ->with(['items.schedule'])
  14. ->orderBy('created_at', 'desc')
  15. ->get();
  16. }
  17. public function findById(int $id): ?Cart
  18. {
  19. return Cart::query()
  20. ->with(['items.schedule'])
  21. ->find($id);
  22. }
  23. public function create(array $data): Cart
  24. {
  25. return Cart::create($data);
  26. }
  27. public function update(int $id, array $data): ?Cart
  28. {
  29. $model = $this->findById($id);
  30. if (! $model) {
  31. return null;
  32. }
  33. $model->update($data);
  34. return $model->fresh();
  35. }
  36. public function delete(int $id): bool
  37. {
  38. $model = $this->findById($id);
  39. if (! $model) {
  40. return false;
  41. }
  42. return $model->delete();
  43. }
  44. //
  45. public function getByUserId(int $userId): Collection
  46. {
  47. return Cart::query()
  48. ->with(['items.schedule.client.user', 'items.schedule.provider.user', 'items.schedule.address'])
  49. ->whereHas('client', fn ($query) => $query->where('user_id', $userId))
  50. ->orderBy('created_at', 'desc')
  51. ->get();
  52. }
  53. public function createForUser(array $data, int $userId): Cart
  54. {
  55. $client = Client::query()
  56. ->where('user_id', $userId)
  57. ->firstOrFail();
  58. $data['client_id'] = $client->id;
  59. return $this->hasSchedulePayload($data)
  60. ? $this->createWithSchedules($data)
  61. : $this->create($data);
  62. }
  63. public function createWithSchedules(array $data): Cart
  64. {
  65. return DB::transaction(function () use ($data) {
  66. $items = $this->scheduleItems($data);
  67. $schedules = app(ScheduleService::class)->createSingleOrMultiple(
  68. baseData: $this->baseScheduleData($data),
  69. schedules: $items,
  70. );
  71. $cart = Cart::create([
  72. 'client_id' => $data['client_id'],
  73. ]);
  74. foreach ($schedules as $schedule) {
  75. $cart->items()->create([
  76. 'schedule_id' => $schedule->id,
  77. ]);
  78. }
  79. return $cart->fresh(['items.schedule.client.user', 'items.schedule.provider.user', 'items.schedule.address']);
  80. });
  81. }
  82. //
  83. private function baseScheduleData(array $data): array
  84. {
  85. return Arr::except($data, [
  86. 'schedules',
  87. 'dates',
  88. 'date',
  89. 'period_type',
  90. 'start_time',
  91. 'end_time',
  92. 'total_amount',
  93. 'offers_meal',
  94. ]);
  95. }
  96. private function hasSchedulePayload(array $data): bool
  97. {
  98. return ! empty($data['schedules'])
  99. || ! empty($data['dates'])
  100. || ! empty($data['date']);
  101. }
  102. private function scheduleItems(array $data): array
  103. {
  104. if (! empty($data['schedules'])) {
  105. return $data['schedules'];
  106. }
  107. if (! empty($data['dates'])) {
  108. return array_map(
  109. fn (string $date) => array_merge(
  110. Arr::only($data, ['period_type', 'start_time', 'end_time', 'total_amount', 'offers_meal']),
  111. ['date' => $date],
  112. ),
  113. $data['dates'],
  114. );
  115. }
  116. return [
  117. Arr::only($data, ['date', 'period_type', 'start_time', 'end_time', 'total_amount', 'offers_meal']),
  118. ];
  119. }
  120. }