193 lines
7.3 KiB
PHP
193 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\Admin\app\Http\Controllers;
|
|
|
|
use App\Exports\LeaveManagementExport;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Jobs\InitializeLeaveDays;
|
|
use App\Models\LeaveDays;
|
|
use App\Models\Notes;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class LeaveManagementController extends Controller
|
|
{
|
|
public function get(Request $request)
|
|
{
|
|
$yearNow = $request->query('year', now()->year);
|
|
$year = $request->year ?? $yearNow;
|
|
|
|
$leaveDays = self::getDataByYear($year);
|
|
if ($leaveDays->count() == 0) {
|
|
InitializeLeaveDays::dispatch($year);
|
|
$leaveDays = self::getDataByYear($year);
|
|
}
|
|
return AbstractController::ResultSuccess($leaveDays);
|
|
}
|
|
|
|
public function getDataByYear($year)
|
|
{
|
|
$totalLeaveDaysByMonth = Notes::join('categories', function ($join) {
|
|
$join->on('notes.n_time_type', '=', 'categories.c_code')
|
|
->where('categories.c_type', 'TIME_TYPE');
|
|
})
|
|
->leftJoin("categories as reason", function ($join) {
|
|
$join->on('n_reason', '=', 'reason.c_code');
|
|
$join->on('reason.c_type', DB::raw("CONCAT('REASON_NOTES')"));
|
|
})
|
|
->select(
|
|
DB::raw('notes.id as id'),
|
|
DB::raw('notes.n_user_id as n_user_id'),
|
|
DB::raw('notes.n_time_type as time_type'),
|
|
DB::raw('notes.n_year as year'),
|
|
DB::raw('notes.n_month as month'),
|
|
DB::raw('categories.c_value as leave_days'),
|
|
DB::raw('notes.n_day as day'),
|
|
DB::raw('notes.n_reason as reason_code'),
|
|
'reason.c_name as reason_name',
|
|
'categories.c_name as time_type_name',
|
|
// DB::raw('SUM(categories.c_value) as leave_days')
|
|
)
|
|
// ->where('notes.n_user_id', "1")
|
|
->where('notes.n_year', $year)
|
|
->whereIn('notes.n_reason', ['ONLEAVE', 'LEAVE_WITHOUT_PAY', 'TEMPORARY_ONLEAVE'])
|
|
// ->groupBy("notes.n_user_id")
|
|
->orderBy('notes.n_month')
|
|
->orderBy('notes.n_day')
|
|
->get()
|
|
->map(function ($item) {
|
|
return [
|
|
"id" => $item->id,
|
|
"day" => $item->day,
|
|
"n_user_id" => $item->n_user_id,
|
|
"reason_code" => $item->reason_code,
|
|
"reason_name" => $item->reason_name,
|
|
"time_type_name" => $item->time_type_name,
|
|
"month" => $item->month,
|
|
"leave_days" => $item->leave_days
|
|
];
|
|
})
|
|
->toArray();
|
|
// dd($totalLeaveDaysByMonth);
|
|
$leaveDays = LeaveDays::join('users', 'leave_days.ld_user_id', '=', 'users.id')
|
|
->select(
|
|
'leave_days.*',
|
|
'users.id as user_id',
|
|
'users.name as user_name',
|
|
'users.email',
|
|
'users.avatar',
|
|
'users.created_at as user_created_at',
|
|
'users.permission',
|
|
'users.updated_at as user_updated_at',
|
|
'users.remember_token',
|
|
'users.email_verified_at'
|
|
)
|
|
->where('leave_days.ld_year', $year)
|
|
->get()
|
|
->map(function ($item) use ($totalLeaveDaysByMonth) {
|
|
$monthlyLeaveDays = [];
|
|
foreach ($totalLeaveDaysByMonth as $key => $totalDays) {
|
|
if ($item->ld_user_id == $totalDays["n_user_id"]) {
|
|
$monthlyLeaveDays[] = $totalDays;
|
|
}
|
|
}
|
|
return [
|
|
'user' => [
|
|
'id' => $item->user_id,
|
|
'name' => $item->user_name,
|
|
'email' => $item->email,
|
|
'avatar' => $item->avatar,
|
|
'created_at' => $item->user_created_at,
|
|
'permission' => $item->permission,
|
|
'updated_at' => $item->user_updated_at,
|
|
'remember_token' => $item->remember_token,
|
|
'email_verified_at' => $item->email_verified_at,
|
|
],
|
|
'leaveDay' => [
|
|
'id' => $item->id,
|
|
'ld_user_id' => $item->ld_user_id,
|
|
'ld_day_total' => $item->ld_day_total,
|
|
'ld_year' => $item->ld_year,
|
|
'ld_additional_day' => $item->ld_additional_day,
|
|
'ld_special_leave_day' => $item->ld_special_leave_day,
|
|
'ld_note' => $item->ld_note,
|
|
'created_at' => $item->created_at,
|
|
'updated_at' => $item->updated_at,
|
|
],
|
|
'monthlyLeaveDays' => $monthlyLeaveDays,
|
|
];
|
|
});
|
|
return $leaveDays;
|
|
}
|
|
|
|
public function saveNoteLeave(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'totalLeave' => 'required|numeric|min:0|max:20',
|
|
// 'dayAdditional' => 'required|numeric|min:0|max:20',
|
|
// 'note' => 'required|string|max:255',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return AbstractController::ResultError($validator->errors());
|
|
}
|
|
|
|
$validatedData = $request->all();
|
|
$leaveDays = LeaveDays::find($validatedData['id']);
|
|
|
|
$leaveDays->ld_day_total = $validatedData['totalLeave'];
|
|
$leaveDays->ld_additional_day = $validatedData['dayAdditional'];
|
|
$leaveDays->ld_special_leave_day = $validatedData['specialLeave'];
|
|
$leaveDays->ld_note = $validatedData['note'];
|
|
|
|
$leaveDays->save();
|
|
|
|
return response()->json(['status' => true, 'message' => 'Updated successfully']);
|
|
}
|
|
|
|
public function updateNoteStatus(Request $request)
|
|
{
|
|
$rules = [
|
|
'id' => 'required',
|
|
'n_reason' => 'required|in:ONLEAVE,LEAVE_WITHOUT_PAY'
|
|
];
|
|
|
|
// Validate the request
|
|
$request->validate($rules);
|
|
$id = $request->input('id');
|
|
$reason = $request->input('n_reason');
|
|
|
|
$note = Notes::find($id);
|
|
if (!$note) {
|
|
return response()->json(['message' => 'Note not found', 'status' => false]);
|
|
}
|
|
|
|
$note->n_reason = $reason;
|
|
$note->save();
|
|
return response()->json(data: ['message' => 'Update success', 'status' => true]);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
$year = $request->query('year', now()->year);
|
|
$leaveDays = $this->getDataByYear($year);
|
|
|
|
if ($leaveDays->isEmpty()) {
|
|
return response()->json(['status' => false, 'message' => 'No data found']);
|
|
}
|
|
|
|
// Lọc chỉ lấy user có permission bao gồm staff
|
|
$staffData = $leaveDays->filter(function ($user) {
|
|
return isset($user['user']['permission']) && strpos($user['user']['permission'], 'staff') !== false;
|
|
});
|
|
|
|
$currentDate = date('d_His');
|
|
return Excel::download(
|
|
new LeaveManagementExport($staffData),
|
|
"LeaveManagement_{$year}_{$currentDate}.xlsx"
|
|
);
|
|
}
|
|
}
|