%PDF- %PDF-
Direktori : /home/forge/api-takeaseat.eco-n-tech.co.uk/app/Http/Controllers/Booking/ |
Current File : //home/forge/api-takeaseat.eco-n-tech.co.uk/app/Http/Controllers/Booking/CreateBooking.php |
<?php namespace App\Http\Controllers\Booking; use Carbon\Carbon; use App\Models\Product; use App\Models\Payment; use App\Models\Booking; use App\Models\Client; use App\Models\Therapist; use App\Helpers\MoneyHelper; use App\Mail\Clients\NewBooking; use App\Notifications\Client\NewBooking as ClientBookingSMS; use App\Mail\Therapist\NewBooking as NewTherapistBooking; use App\Notifications\Therapist\NewBooking as TherapistBookingSMS; use Illuminate\Support\Facades\Mail; use App\Http\Requests\StoreBooking; use App\Http\Resources\BookingResource; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class CreateBooking extends Controller { /** * Create a new booking * * @param string $payment_id * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function __invoke(StoreBooking $request) { $user = auth()->user(); $therapist = Therapist::with('user') ->findOrFail($request->get('therapist_id')); $bookings = []; foreach($request->get('sessions') as $session) { $product = Product::findOrfail($session['product_id']); $payment = Payment::create([ 'customer_id' => $user->id, 'seller_id' => $therapist->user->id, 'product_id' => $product->id, 'amount' => $product->price, 'fees_collected' => 0, 'is_complete' => true ]); if($request->has('charges')) { foreach($request->get('charges') as $charge) { $payment->charges()->create([ 'payment_id' => $payment->id, 'charge_id' => $charge['id'] ]); } } if($request->has('transfer')) { $transfer = $request->get('transfer'); $payment->transfer()->create([ 'payment_id' => $payment->id, 'transfer_id' => $transfer['id'] ]); } $booking = Booking::create([ 'therapist_id' => $therapist->id, 'user_id' => $user->id, 'session_id' => $session['session_id'], 'address_id' => $session['address_id'], 'payment_id' => $payment->id, 'status' => array_search('Pending', Booking::STATUSES), 'start_date' => Carbon::parse($session['slot']), 'end_date' => Carbon::parse($session['slot'])->addHour() ]); // Push the created booking to the array $bookings[] = $booking; } // Send Notification Emails Mail::to($user)->send(new NewBooking($user, $therapist->user, $bookings)); Mail::to($therapist->user)->send(new NewTherapistBooking($therapist->user, $user, $bookings)); // Send SMS $user->notify(new ClientBookingSMS($bookings)); $therapist->user->notify(new TherapistBookingSMS($bookings)); // Create a new client if they don't exist $client = Client::updateOrCreate( ['therapist_id' => $therapist->id, 'user_id' => $user->id], ['therapist_id' => $therapist->id, 'user_id' => $user->id] ); return response()->json(collect($bookings)->map(function ($item, $key) { return new BookingResource($item); })); } }