|
@@ -3,6 +3,7 @@
|
|
|
namespace App\Services;
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Models\Cart;
|
|
use App\Models\Cart;
|
|
|
|
|
+use App\Models\Client;
|
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Arr;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
@@ -29,30 +30,6 @@ class CartService
|
|
|
return Cart::create($data);
|
|
return Cart::create($data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function createWithSchedules(array $data): Cart
|
|
|
|
|
- {
|
|
|
|
|
- return DB::transaction(function () use ($data) {
|
|
|
|
|
- $items = $this->scheduleItems($data);
|
|
|
|
|
-
|
|
|
|
|
- $schedules = app(ScheduleService::class)->createSingleOrMultiple(
|
|
|
|
|
- baseData: $this->baseScheduleData($data),
|
|
|
|
|
- schedules: $items,
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- $cart = Cart::create([
|
|
|
|
|
- 'client_id' => $data['client_id'],
|
|
|
|
|
- ]);
|
|
|
|
|
-
|
|
|
|
|
- foreach ($schedules as $schedule) {
|
|
|
|
|
- $cart->items()->create([
|
|
|
|
|
- 'schedule_id' => $schedule->id,
|
|
|
|
|
- ]);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return $cart->fresh(['items.schedule.client.user', 'items.schedule.provider.user', 'items.schedule.address']);
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
public function update(int $id, array $data): ?Cart
|
|
public function update(int $id, array $data): ?Cart
|
|
|
{
|
|
{
|
|
|
$model = $this->findById($id);
|
|
$model = $this->findById($id);
|
|
@@ -79,6 +56,54 @@ class CartService
|
|
|
|
|
|
|
|
//
|
|
//
|
|
|
|
|
|
|
|
|
|
+ public function getByUserId(int $userId): Collection
|
|
|
|
|
+ {
|
|
|
|
|
+ return Cart::query()
|
|
|
|
|
+ ->with(['items.schedule.client.user', 'items.schedule.provider.user', 'items.schedule.address'])
|
|
|
|
|
+ ->whereHas('client', fn ($query) => $query->where('user_id', $userId))
|
|
|
|
|
+ ->orderBy('created_at', 'desc')
|
|
|
|
|
+ ->get();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function createForUser(array $data, int $userId): Cart
|
|
|
|
|
+ {
|
|
|
|
|
+ $client = Client::query()
|
|
|
|
|
+ ->where('user_id', $userId)
|
|
|
|
|
+ ->firstOrFail();
|
|
|
|
|
+
|
|
|
|
|
+ $data['client_id'] = $client->id;
|
|
|
|
|
+
|
|
|
|
|
+ return $this->hasSchedulePayload($data)
|
|
|
|
|
+ ? $this->createWithSchedules($data)
|
|
|
|
|
+ : $this->create($data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function createWithSchedules(array $data): Cart
|
|
|
|
|
+ {
|
|
|
|
|
+ return DB::transaction(function () use ($data) {
|
|
|
|
|
+ $items = $this->scheduleItems($data);
|
|
|
|
|
+
|
|
|
|
|
+ $schedules = app(ScheduleService::class)->createSingleOrMultiple(
|
|
|
|
|
+ baseData: $this->baseScheduleData($data),
|
|
|
|
|
+ schedules: $items,
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ $cart = Cart::create([
|
|
|
|
|
+ 'client_id' => $data['client_id'],
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($schedules as $schedule) {
|
|
|
|
|
+ $cart->items()->create([
|
|
|
|
|
+ 'schedule_id' => $schedule->id,
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $cart->fresh(['items.schedule.client.user', 'items.schedule.provider.user', 'items.schedule.address']);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //
|
|
|
|
|
+
|
|
|
private function baseScheduleData(array $data): array
|
|
private function baseScheduleData(array $data): array
|
|
|
{
|
|
{
|
|
|
return Arr::except($data, [
|
|
return Arr::except($data, [
|
|
@@ -93,6 +118,13 @@ class CartService
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private function hasSchedulePayload(array $data): bool
|
|
|
|
|
+ {
|
|
|
|
|
+ return ! empty($data['schedules'])
|
|
|
|
|
+ || ! empty($data['dates'])
|
|
|
|
|
+ || ! empty($data['date']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private function scheduleItems(array $data): array
|
|
private function scheduleItems(array $data): array
|
|
|
{
|
|
{
|
|
|
if (! empty($data['schedules'])) {
|
|
if (! empty($data['schedules'])) {
|