166 lines
5.4 KiB
PHP
166 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Attendance;
|
|
use App\Models\User;
|
|
use App\Models\Permission;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ToprankController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$month = $request->input('month', Carbon::now()->month);
|
|
$year = $request->input('year', Carbon::now()->year);
|
|
$sort = $request->input('sort', 'desc');
|
|
|
|
$ranking = User::select('users.id', 'users.name')
|
|
->where('role', 'karyawan')
|
|
->withCount(['permissions' => function($query) use ($month, $year) {
|
|
$query->whereMonth('start_date', $month)
|
|
->whereYear('start_date', $year);
|
|
}])
|
|
->orderBy('permissions_count', $sort)
|
|
->paginate(10);
|
|
|
|
$months = [
|
|
1 => 'Januari', 2 => 'Februari', 3 => 'Maret', 4 => 'April',
|
|
5 => 'Mei', 6 => 'Juni', 7 => 'Juli', 8 => 'Agustus',
|
|
9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Desember'
|
|
];
|
|
|
|
return view('dashboard.toprank', [
|
|
'ranking' => $ranking,
|
|
'months' => $months,
|
|
'currentMonth' => $month,
|
|
'currentYear' => $year,
|
|
'sort' => $sort
|
|
]);
|
|
}
|
|
|
|
public function userPermissionsWeb($userId, Request $request)
|
|
{
|
|
try {
|
|
$month = $request->input('month', Carbon::now()->month);
|
|
$year = $request->input('year', Carbon::now()->year);
|
|
|
|
$user = User::where('id', $userId)
|
|
->where('role', 'karyawan')
|
|
->firstOrFail();
|
|
|
|
$permissions = Permission::where('user_id', $userId)
|
|
->whereMonth('start_date', $month)
|
|
->whereYear('start_date', $year)
|
|
->orderBy('start_date', 'desc')
|
|
->get()
|
|
->map(function ($item) {
|
|
return [
|
|
'id' => $item->id,
|
|
'category' => $item->category,
|
|
'reason' => $item->reason,
|
|
'start_date' => $item->start_date,
|
|
'end_date' => $item->end_date,
|
|
'status' => $item->status,
|
|
'proof_photo' => $item->proof_photo,
|
|
'created_at' => $item->created_at,
|
|
'approved_at' => $item->approved_at,
|
|
'approved_by' => optional($item->approver)->name
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'all' => $permissions,
|
|
'accepted' => $permissions->where('status', 'accepted')->values(),
|
|
'pending' => $permissions->where('status', 'pending')->values(),
|
|
'rejected' => $permissions->where('status', 'rejected')->values()
|
|
],
|
|
'stats' => [
|
|
'accepted' => $permissions->where('status', 'accepted')->count(),
|
|
'pending' => $permissions->where('status', 'pending')->count(),
|
|
'rejected' => $permissions->where('status', 'rejected')->count()
|
|
]
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal memuat data izin: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
// TOP RANK ABSEN
|
|
public function absensi(Request $request)
|
|
{
|
|
$month = $request->input('month', Carbon::now()->month);
|
|
$year = $request->input('year', Carbon::now()->year);
|
|
$sort = $request->input('sort', 'desc');
|
|
|
|
$ranking = User::select('users.id', 'users.name')
|
|
->where('role', 'karyawan')
|
|
->withCount(['attendances as total_absen' => function ($query) use ($month, $year) {
|
|
$query->whereMonth('created_at', $month)
|
|
->whereYear('created_at', $year);
|
|
}])
|
|
->orderBy('total_absen', $sort)
|
|
->paginate(10);
|
|
|
|
$months = [
|
|
1 => 'Januari', 2 => 'Februari', 3 => 'Maret', 4 => 'April',
|
|
5 => 'Mei', 6 => 'Juni', 7 => 'Juli', 8 => 'Agustus',
|
|
9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Desember'
|
|
];
|
|
|
|
if ($request->ajax()) {
|
|
// Jika request AJAX, hanya kirim partial table
|
|
return view('dashboard.toprank_absensi', [
|
|
'ranking' => $ranking,
|
|
'months' => $months,
|
|
'currentMonth' => $month,
|
|
'currentYear' => $year,
|
|
'sort' => $sort
|
|
]);
|
|
}
|
|
|
|
// Jika normal page load
|
|
return view('dashboard.toprank_absensi', [
|
|
'ranking' => $ranking,
|
|
'months' => $months,
|
|
'currentMonth' => $month,
|
|
'currentYear' => $year,
|
|
'sort' => $sort
|
|
]);
|
|
}
|
|
|
|
|
|
public function userAttendances($userId, Request $request)
|
|
{
|
|
try {
|
|
$month = $request->input('month', Carbon::now()->month);
|
|
$year = $request->input('year', Carbon::now()->year);
|
|
|
|
$attendances = Attendance::with(['location', 'user'])
|
|
->where('user_id', $userId)
|
|
->whereMonth('created_at', $month)
|
|
->whereYear('created_at', $year)
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $attendances
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal memuat data absensi: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
|
|
} |