Viewing file: OrderCountDownController.php (3.7 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers;
use App\Models\Order; use App\Models\Restaurant; use Illuminate\Http\Request; use DateTime; use DateInterval; class OrderCountDownController extends Controller { public function index() { $data['restaurants'] = []; $user = auth()->user(); if ($user->type == 'restaurant_owner') { $data['restaurants'] = Restaurant::where('user_id', $user->id)->select('id', 'name')->get(); } return view('order_count_down.index',$data); }
public function order_time_count_down(Request $request) { $user = auth()->user();
if ($user->type == 'restaurant_owner') { $restaurants = Restaurant::where('user_id', $user->id)->select('id', 'name')->get();
if ($restaurants->isNotEmpty()) { $restaurantIds = $restaurants->pluck('id');
if ($request->has('restaurant_id') && in_array($request->restaurant_id, $restaurantIds->toArray())) { $restaurantIds = [$request->restaurant_id]; }
$orders = Order::whereIn('restaurant_id', $restaurantIds) ->where('type','pay_on_table') ->orderBy('created_at', 'desc') ->get() ->map(function ($order) { return [ 'table_name' => 'Table #' . $order->table->name, 'order_number' => $order->order_number, 'order_time' => $order->created_at->format('g:i:s A'), 'remaining_time' => $this->calculateRemainingTime($order->delivered_within, $order->approved_at), 'status' => ucfirst($this->getOrderStatus($order->status)) ]; });
return response()->json([ 'data' => $orders, 'status' => 'success', ]); } }
return response()->json([ 'data' => [], 'status' => 'error', 'message' => 'No orders found or unauthorized access' ]); }
private function getOrderStatus($status) { return $status === 'ready_for_delivery' ? 'On The Way' : $status; }
private function calculateRemainingTime($deliveredWithin, $approvedTime) { $parts = explode(' ', $deliveredWithin);
if (count($parts) === 2) { $value = (int)$parts[0]; $unit = strtolower($parts[1]);
$intervalSpec = match ($unit) { 'minute', 'minutes' => "PT{$value}M", 'hour', 'hours' => "PT{$value}H", 'day', 'days' => "P{$value}D", default => null };
if ($intervalSpec) { $interval = new DateInterval($intervalSpec); $approvedDateTime = new DateTime($approvedTime); $deliveryTime = clone $approvedDateTime; $deliveryTime->add($interval); $now = new DateTime();
if ($now > $deliveryTime) { return 'Time Over'; }
$remainingTime = $deliveryTime->diff($now);
return $this->formatRemainingTime($remainingTime); } }
return 'Time Over'; }
private function formatRemainingTime($remainingTime) { $parts = []; if ($remainingTime->d > 0) { $parts[] = $remainingTime->d . ' day' . ($remainingTime->d > 1 ? 's' : ''); } if ($remainingTime->h > 0) { $parts[] = $remainingTime->h . ' hour' . ($remainingTime->h > 1 ? 's' : ''); } if ($remainingTime->i > 0) { $parts[] = $remainingTime->i . ' minute' . ($remainingTime->i > 1 ? 's' : ''); }
return implode(', ', $parts) ?: '0 minutes'; }
}
|