Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
2
Indexable
Never
 public function handle()
    {
        try {
            // $dayAfterTomorrow = Carbon::now()->addDays(2)->startOfDay();
            $dayAfterTomorrow = Carbon::now()->addDays(2)->format('d/m/Y');
            Log::info('///////Day After Tomorrow///////', [$dayAfterTomorrow]);

            // $suborders = SubOrders::whereDate('delivery_date', $dayAfterTomorrow)->with('chefs','Orders:id,delivery_date')->get();
            $suborders = SubOrders::with('chefs', 'Orders')->whereHas('orders', function ($query) use ($dayAfterTomorrow) {
                $query->where('delivery_date', $dayAfterTomorrow);
            })->get();

            foreach ($suborders as $suborder) {
                Log::info('///////suborders_delivery_date///////', [$suborder->orders->delivery_date]);
                $this->notifyDrivers($suborder);
            }
            Log::info('CheckSuborderDelivery command executed successfully.');
        } catch (\Exception $e) {
            Log::error('Error in CheckSuborderDelivery command: ' . $e->getMessage());
        }
    }


    private function notifyDrivers($suborder)
    {
        // Get the chef's location
        $chefLat = $suborder->chefs->latitude;
        $chefLong = $suborder->chefs->longitude;

        // Find nearby drivers based on chef's location (this assumes you have a scope for nearby in Driver model scope radius = 10 means fetch driver comes under 10 km)
        $drivers = Driver::nearby($chefLat, $chefLong)->get();

        foreach ($drivers as $driver) {

            // Send notifications
            Mail::to($driver->email)->send(new SuborderCronJobDriverMail($suborder));
            Notification::send($driver, new SuborderCronJobDriverNotify($suborder, $driver));

            Log::info('Notification sent to Driver: ' . $driver->email);
        }
    }
Leave a Comment