data['title'] = 'New Orders';
$this->data['settings'] = DB::table('settings')->find(1);
$orders = DB::table('takeaway_orders')->where('status','!=',5)->where('platform','!=','pos')->orderBy('id','desc')->get();
if(!empty($orders)){
foreach($orders as $row){
if($row->checkout != 'guest'){
$customer = DB::table('customers')->where('id',$row->customer_id)->select('name')->first();
$row->customer_name = $customer ? $customer->name : 'Guest';
}else{
$row->customer_name = $row->g_name;
}
}
}
$this->data['orders'] = $orders;
return view('admin.orders.new_orders', $this->data);
}
/*
| Orders
|--------------------------------------------------------------------------
*/
public function get_orders(Request $request)
{
$this->data['title'] = 'Orders';
$this->data['settings'] = DB::table('settings')->find(1);
$query = DB::table('takeaway_orders')->where('platform', '!=', 'pos');
// Filters
if ($request->has('date') && $request->date != '') {
$query->whereDate('created_at', $request->date);
}
if ($request->has('order_type') && $request->order_type != '') {
$query->where('order_type', $request->order_type);
}
if ($request->has('order_day') && $request->order_day != '') {
$query->where('order_day', $request->order_day);
}
if ($request->has('status') && $request->status != '') {
$query->where('status', $request->status);
}
$orders = $query->orderBy('id', 'desc')->paginate(30);
foreach ($orders as $row) {
if ($row->checkout != 'guest') {
$customer = DB::table('customers')->where('id', $row->customer_id)->select('name')->first();
$row->customer_name = $customer ? $customer->name : 'N/A';
} else {
$row->customer_name = $row->g_name;
}
// Get all items for this order
$orderItems = DB::table('takeaway_order_details')->where('order_id', $row->id)->get();
// Check if any items are next-day
$hasNextDayItems = $orderItems->contains('nextday_order', 'yes');
// Check if ALL items are next-day
$allItemsNextDay = $orderItems->count() > 0 && $orderItems->every('nextday_order', 'yes');
$row->order_due_tomorrow = $hasNextDayItems;
$row->all_items_nextday = $allItemsNextDay;
}
$this->data['orders'] = $orders;
$this->data['statusCounts'] = [
'placed' => DB::table('takeaway_orders')->where('status', 0)->count(),
'accepted' => DB::table('takeaway_orders')->where('status', 1)->count(),
'declined' => DB::table('takeaway_orders')->where('status', 2)->count(),
'pending' => DB::table('takeaway_orders')->where('status', 3)->count(),
'delivered' => DB::table('takeaway_orders')->where('status', 4)->count(),
'paid' => DB::table('takeaway_orders')->where('status', 5)->count(),
];
// print_r($orders);exit;
return view('admin.orders.orders', $this->data);
}
/*
| get_orders_ajax
|--------------------------------------------------------------------------
*/
public function get_orders_ajax()
{
$orders = DB::table('takeaway_orders')->where('status','!=',5)->where('status','!=',2)->where('platform','!=','pos')->orderBy('id','desc')->get();
if(!empty($orders)){
foreach($orders as $row){
if($row->checkout != 'guest'){
$customer = DB::table('customers')->where('id',$row->customer_id)->select('name')->first();
$row->customer_name = $customer->name;
}else{
$row->customer_name = $row->g_name;
}
// Get all items for this order
$orderItems = DB::table('takeaway_order_details')->where('order_id', $row->id)->get();
// Check if any items are next-day
$hasNextDayItems = $orderItems->contains('nextday_order', 'yes');
// Check if ALL items are next-day
$allItemsNextDay = $orderItems->count() > 0 && $orderItems->every('nextday_order', 'yes');
$row->order_due_tomorrow = $hasNextDayItems;
$row->all_items_nextday = $allItemsNextDay;
}
}
if (!empty($orders)) {
ob_start();
foreach($orders as $row ){
?>
order_day == 'later'){?>
| Scheduled Order : order_day_date)) ?> |
order_day == 'later'){ echo 'class="scheduled-order"'; } ?>>
| id ?> |
checkout != 'guest'){?>
customer_name) ?>
customer_name) ?>
checkout) ?>
order_due_tomorrow){ ?>
(Next Day Order)
|
created_at)) ?>
created_at)) ?>
|
total) ?> |
order_type == 'delivery'){?>
order_type).' '.env('CURRENCY').number_format($row->delivery_charges) ?>
order_type == 'pickup'){?>
order_type).' '.$row->pickup_time ?>
|
|
all_items_nextday): ?>
order_due_tomorrow): ?>
|
No New Order Received.';
}
}
/*
| get_order_detail_ajax
|--------------------------------------------------------------------------
*/
public function get_order_detail_ajax(Request $request)
{
$settings = Settings::findorFail(1);
$order = DB::table('takeaway_orders')
->where('id',$request->id)
->select(
'*',
'id as order_id',
'created_at as order_date',
'status as order_status'
)->first();
if(!empty($order)){
if($order->checkout != 'guest'){
$customer = DB::table('customers')->where('id',$order->customer_id)->first();
$order->customer_name = $customer->name;
$order->customer_phone = $customer->mobile_number;
$order->customer_address = $customer->address;
}else{
$order->customer_name = $order->g_name;
$order->customer_phone = $order->g_phone;
$order->customer_address = $order->g_address;
}
}
$order->restaurant_name = ucwords($settings->name);
$order->restaurant_address = $settings->address .', '. $settings->city;
$order->phone = $settings->phone_no;
$order->order_date = date('d-M-Y h:i:s A',strtotime($order->order_date));
$order->order_detail = \App\TakeawayOrderDetails::where('order_id', $order->order_id)->get();
$feedback = \App\CustomerFeedback::where('order_id', $order->order_id)->first();
if(!empty($feedback)){
$order->feedback = $feedback;
$order->feedback_flag = 1;
}else{
$order->feedback = '';
$order->feedback_flag = 0;
}
return response()->json($order);
}
/*
| order_status_update
|--------------------------------------------------------------------------
*/
public function order_status_update(Request $request)
{
if($request->id){
$order = \App\TakeawayOrders::findorFail($request->id);
if($request->status == 1){
$order->user_id = Auth::User()->id;
$order->user = Auth::User()->name;
//kitchen
$kitchen = new \App\Kitchen;
$kitchen->order_id = $request->id;
$kitchen->save();
}
$order->status = $request->status;
$order->save();
}
}
/*
| print_order
|--------------------------------------------------------------------------
*/
public function print_order(Request $request)
{
$order = DB::table('takeaway_orders')
->where('id',$request->id)
->select('*','created_at as order_date','id as order_id' ,'status as order_status','customer_id','platform as order_platform')
->first();
if(!empty($order)){
if($order->checkout != 'guest'){
$customer = DB::table('customers')->where('id',$order->customer_id)->first();
$order->customer_name = $customer->name;
$order->customer_phone = $customer->mobile_number;
$order->customer_address = $customer->address;
}else{
$order->customer_name = $order->g_name;
$order->customer_phone = $order->g_phone;
$order->customer_address = $order->g_address;
}
$order->is_nextday = $request->has('nextday') ? ($request->nextday == 1) : null;
}
$this->data['order'] = $order;
$this->data['order_detail'] = DB::table('takeaway_order_details')->where('order_id', $request->id)
->when($request->has('nextday'), function($query) use ($request) {
if ($request->nextday == 1) {
$query->where('nextday_order', 'yes');
}
if ($request->nextday == 0) {
$query->where('nextday_order', 'no');
}
})->get();
$this->data['settings'] = Settings::find(1);
return view('admin.orders.print', $this->data);
}
/*
| pos_print_order
|--------------------------------------------------------------------------
*/
public function pos_print_order(Request $request)
{
$settings = Settings::findorFail(1);
if($request->reprint_key == '' && $settings->reprint_key == ''){
return back();
}
if($settings->reprint_key != $request->reprint_key){
$request->session()->flash('error', 'Enter correct key to re-print the receipt.');
return back();
}
$order = DB::table('takeaway_orders')
->where('id',$request->id)
->select('*','created_at as order_date','id as order_id' ,'status as order_status','customer_id','platform as order_platform')
->first();
if(!empty($order)){
if($order->checkout != 'guest'){
$customer = DB::table('customers')->where('id',$order->customer_id)->first();
$order->customer_name = $customer->name;
$order->customer_phone = $customer->mobile_number;
$order->customer_address = $customer->address;
}else{
$order->customer_name = $order->g_name;
$order->customer_phone = $order->g_phone;
$order->customer_address = $order->g_address;
}
}
$this->data['order'] = $order;
$this->data['order_detail'] = DB::table('takeaway_order_details')->where('order_id',$request->id)->get();
$this->data['settings'] = $settings;
return view('admin.orders.print', $this->data);
}
public function dueTomorrowOrders(Request $request)
{
$this->data['title'] = 'Due Tomorrow Orders';
// Get orders with next-day items
$orders = DB::table('takeaway_orders as to')
->join('takeaway_order_details as tod', 'to.id', '=', 'tod.order_id')
->where('tod.nextday_order', 'yes')
->where('to.status','!=', 5)
->whereDate('to.created_at', Carbon::today())
->select('to.*')
->distinct()
->get();
// Format for FullCalendar
$events = [];
foreach ($orders as $order) {
$events[] = [
'title' => 'Order #' . $order->id,
'start' => Carbon::tomorrow()->format('Y-m-d'),
'allDay' => true,
'url' => route('printOrder', ['id' => $order->id, 'nextday' => 1]),
'backgroundColor' => '#ffc107',
'borderColor' => '#ffc107',
'textColor' => '#000'
];
}
$this->data['events'] = json_encode($events);
$this->data['settings'] = DB::table('settings')->find(1);
return view('admin.orders.due_tomorrow', $this->data);
}
}