maskurir/app/Http/Controllers/HomeController.php

85 lines
2.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Location;
use App\Models\Order;
use App\Models\User;
use Carbon\Carbon;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$totalGajiKurir = Order::whereNotNull('kurir_id')
->whereMonth('created_at', Carbon::now()->month)
->whereYear('created_at', Carbon::now()->year)
->count() * 5000;
$totalOrders = Order::whereMonth('created_at', Carbon::now()->month)
->count();
$ordersProcessing = Order::where('status', 'Assigned')->count();
$ordersCompleted = Order::where('status', 'Delivered')->count();
// Ambil semua kurir
$kurirs = User::where('role', 'kurir')->get();
$today = now()->format('Y-m-d');
// Ambil lokasi terbaru per user hari ini
$latestLocations = Location::select('user_id', 'latitude', 'longitude', 'created_at')
->whereDate('created_at', $today)
->latest('created_at')
->get()
->keyBy('user_id'); // Supaya bisa diakses pakai $latestLocations[$kurir->id]
// Ambil semua lokasi yang sudah diupload oleh kurir, termasuk informasi user (kurir)
$locations = Location::with('user')
->select('locations.*')
->join(Location::raw('(
SELECT user_id, MAX(id) as latest_id
FROM locations
WHERE DATE(created_at) = CURDATE()
GROUP BY user_id
) as latest'), function ($join) {
$join->on('locations.user_id', '=', 'latest.user_id')
->on('locations.id', '=', 'latest.latest_id');
})
->get();
$today = now()->format('Y-m-d');
$locationsPresence = Location::with('user')
->select('user_id', Location::raw('MIN(created_at) as check_in_time'))
->whereDate('created_at', $today)
->groupBy('user_id')
->get();
$presensiCount = $locationsPresence->count();
$presensiRecords = Location::with('user')
->select('user_id', Location::raw('MIN(created_at) as check_in_time'))
->whereDate('created_at', $today)
->groupBy('user_id')
->get();
return view('home', compact('locations', 'totalOrders', 'ordersProcessing', 'ordersCompleted',
'totalGajiKurir', 'presensiCount', 'presensiRecords', 'kurirs', 'latestLocations'));
}
}