<?php
namespace App\Http\Controllers\Branch;
use App\Http\Controllers\Controller;
use App\Model\Admin;
use App\Model\Order;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
class SystemController extends Controller
{
public function dashboard()
{
$data = self::order_stats_data();
$earning = [];
for ($i = 1; $i <= 12; $i++) {
$from = date('Y-' . $i . '-01');
$to = date('Y-' . $i . '-30');
$earning[$i] = Order::where(['order_status' => 'delivered', 'branch_id' => auth('branch')->id()])->whereBetween('created_at', [$from, $to])->sum('order_amount');
}
return view('branch-views.dashboard', compact('data', 'earning'));
}
public function order_stats(Request $request)
{
session()->put('statistics_type', $request['statistics_type']);
$data = self::order_stats_data();
return response()->json([
'view' => view('admin-views.partials._dashboard-order-stats', compact('data'))->render()
], 200);
}
public function order_stats_data()
{
$today = session()->has('statistics_type') && session('statistics_type') == 'today' ? 1 : 0;
$this_month = session()->has('statistics_type') && session('statistics_type') == 'this_month' ? 1 : 0;
$pending = Order::where(['order_status' => 'pending', 'branch_id' => auth('branch')->id()])
->when($today, function ($query) {
return $query->whereDate('created_at', Carbon::today());
})
->when($this_month, function ($query) {
return $query->whereMonth('created_at', Carbon::now());
})
->count();
$confirmed = Order::where(['order_status' => 'confirmed', 'branch_id' => auth('branch')->id()])
->when($today, function ($query) {
return $query->whereDate('created_at', Carbon::today());
})
->when($this_month, function ($query) {
return $query->whereMonth('created_at', Carbon::now());
})
->count();
$processing = Order::where(['order_status' => 'processing', 'branch_id' => auth('branch')->id()])
->when($today, function ($query) {
return $query->whereDate('created_at', Carbon::today());
})
->when($this_month, function ($query) {
return $query->whereMonth('created_at', Carbon::now());
})
->count();
$out_for_delivery = Order::where(['order_status' => 'out_for_delivery', 'branch_id' => auth('branch')->id()])
->when($today, function ($query) {
return $query->whereDate('created_at', Carbon::today());
})
->when($this_month, function ($query) {
return $query->whereMonth('created_at', Carbon::now());
})
->count();
$delivered = Order::where(['order_status' => 'delivered', 'branch_id' => auth('branch')->id()])
->when($today, function ($query) {
return $query->whereDate('created_at', Carbon::today());
})
->when($this_month, function ($query) {
return $query->whereMonth('created_at', Carbon::now());
})
->count();
$all = Order::where(['branch_id' => auth('branch')->id()])
->when($today, function ($query) {
return $query->whereDate('created_at', Carbon::today());
})
->when($this_month, function ($query) {
return $query->whereMonth('created_at', Carbon::now());
})
->count();
$returned = Order::where(['order_status' => 'returned', 'branch_id' => auth('branch')->id()])
->when($today, function ($query) {
return $query->whereDate('created_at', Carbon::today());
})
->when($this_month, function ($query) {
return $query->whereMonth('created_at', Carbon::now());
})
->count();
$failed = Order::where(['order_status' => 'failed', 'branch_id' => auth('branch')->id()])
->when($today, function ($query) {
return $query->whereDate('created_at', Carbon::today());
})
->when($this_month, function ($query) {
return $query->whereMonth('created_at', Carbon::now());
})
->count();
$data = [
'pending' => $pending,
'confirmed' => $confirmed,
'processing' => $processing,
'out_for_delivery' => $out_for_delivery,
'delivered' => $delivered,
'all' => $all,
'returned' => $returned,
'failed' => $failed
];
return $data;
}
public function restaurant_data()
{
$new_order = DB::table('orders')->where(['branch_id' => auth('branch')->id(), 'checked' => 0])->count();
return response()->json([
'success' => 1,
'data' => ['new_order' => $new_order]
]);
}
public function settings()
{
return view('branch-views.settings');
}
}