File manager - Edit - /var/www/payraty/hris-standalone/app/Http/Controllers/AppraisalController.php
Back
<?php namespace App\Http\Controllers; use Carbon\Carbon; use App\Models\Branch; use App\Models\Employee; use App\Models\Appraisal; use App\Models\Indicator; use App\Models\Department; use App\Models\Designation; use App\Models\Competencies; use Illuminate\Http\Request; use App\Models\EventEmployee; use App\Models\Performance_Type; use App\Models\Event as LocalEvent; use Illuminate\Support\Facades\Log; class AppraisalController extends Controller { public function index(Request $request) { if (\Auth::user()->can('Manage Appraisal')) { $user = \Auth::user(); $competencyCount = Competencies::where('created_by', '=', $user->creatorId())->count(); $query = Appraisal::where('created_by', '=', $user->creatorId()); $isManager = false; // default if ($user->type == 'employee') { $employee = Employee::where('user_id', $user->id)->first(); if ($employee) { // Manager case: get all departments managed by this employee $managedDepartments = Department::where('manager_id', $employee->id)->pluck('id'); if ($managedDepartments->count() > 0) { $isManager = true; // mark as manager // get all employees in those departments $employeeIds = Employee::whereIn('department_id', $managedDepartments)->pluck('id'); // include self + team $query->whereIn('employee', $employeeIds->push($employee->id)); } else { // not a manager → show only personal $query->where('employee', $employee->id); } } } // Apply date filter if (!empty($request->start_date)) { $query->whereDate('created_at', '>=', $request->start_date); } if (!empty($request->end_date)) { $query->whereDate('created_at', '<=', $request->end_date); } $appraisals = $query->with(['employees.department', 'employees.designation', 'branches', 'creatorUser'])->get(); return view('appraisal.index', compact('appraisals', 'competencyCount', 'user', 'isManager')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function create() { if (\Auth::user()->can('Manage Appraisal')) { $user = \Auth::user(); $brances = Branch::where('created_by', '=', $user->creatorId())->get(); $performance_types = Performance_Type::where('created_by', '=', $user->creatorId())->get(); if ($user->type == 'employee') { // Manager check $employee = Employee::where('user_id', $user->id)->first(); $managedDepartments = Department::where('manager_id', $employee->id)->pluck('id'); if ($managedDepartments->count() > 0) { // Employees in managed departments only $employeeList = Employee::whereIn('department_id', $managedDepartments)->pluck('name', 'id'); } else { // Not a manager → can only self-appraise $employeeList = Employee::where('id', $employee->id)->pluck('name', 'id'); } } else { // Admin/HR → all employees $employeeList = Employee::where('created_by', $user->creatorId())->pluck('name', 'id'); } $employeeList->prepend('Select Employee', ''); return view('appraisal.create', compact('employeeList', 'brances', 'performance_types')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function store(Request $request) { if (\Auth::user()->can('Manage Appraisal')) { // Check if multi-employee mode is enabled $isMultiMode = $request->has('multi_employee_mode') && $request->multi_employee_mode; // Dynamic validation rules $validationRules = [ 'brances' => 'required', 'rating' => 'required', ]; if ($isMultiMode) { $validationRules['employees'] = 'required|array|min:1'; $validationRules['employees.*'] = 'exists:employees,id'; // Adjust table name as needed } else { $validationRules['employee'] = 'required'; } $validator = \Validator::make($request->all(), $validationRules); if ($validator->fails()) { $messages = $validator->getMessageBag(); return redirect()->back()->with('error', $messages->first()); } try { \DB::beginTransaction(); if ($isMultiMode) { // Handle multiple employees $employeeIds = $request->employees; $createdCount = 0; foreach ($employeeIds as $employeeId) { $appraisal = new Appraisal(); $appraisal->branch = $request->brances; $appraisal->employee = $employeeId; $appraisal->appraisal_date = $request->appraisal_date; $appraisal->rating = json_encode($request->rating, true); $appraisal->remark = $request->remark; $appraisal->deadline = $request->deadline; $appraisal->created_by = \Auth::user()->creatorId(); $appraisal->created_user_id = \Auth::id(); $appraisal->save(); $createdCount++; // Find the employee details for the event $employee = Employee::find($employeeId); if ($employee) { // Create a new event for the appraisal $event = new LocalEvent(); $event->branch_id = $appraisal->branch; $event->department_id = json_encode([$employee->department_id]); $event->employee_id = json_encode([$employee->id]); $event->title = 'Appraisal Elapsed for ' . $employee->name; $event->start_date = $request->deadline; $event->end_date = $request->deadline; $event->color = '#007bff'; // You can choose a different color. $event->description = 'Appraisal period: ' . Carbon::parse($appraisal->appraisal_date)->toDateString() . ' to ' . $appraisal->deadline . '. Appraisal due for ' . $employee->name . '.'; $event->created_by = \Auth::user()->creatorId(); $event->save(); // Link the event to the employee $eventEmployee = new EventEmployee(); $eventEmployee->event_id = $event->id; $eventEmployee->employee_id = $employee->id; $eventEmployee->created_by = \Auth::user()->creatorId(); $eventEmployee->save(); } } \DB::commit(); if ($createdCount > 0) { $message = $createdCount === 1 ? __('Appraisal successfully created for 1 employee.') : __('Appraisals successfully created for :count employees.', ['count' => $createdCount]); $skippedCount = count($employeeIds) - $createdCount; if ($skippedCount > 0) { $message .= ' ' . __(':count employees were skipped (appraisal already exists).', ['count' => $skippedCount]); } return redirect()->route('appraisal.index')->with('success', $message); } else { return redirect()->route('appraisal.index')->with('warning', __('No new appraisals were created. All selected employees already have appraisals for this period.')); } } else { // Handle single employee (original logic) $appraisal = new Appraisal(); $appraisal->branch = $request->brances; $appraisal->employee = $request->employee; $appraisal->appraisal_date = $request->appraisal_date; $appraisal->rating = json_encode($request->rating, true); $appraisal->remark = $request->remark; $appraisal->deadline = $request->deadline; $appraisal->created_by = \Auth::user()->creatorId(); $appraisal->created_user_id = \Auth::id(); $appraisal->save(); $employee = Employee::find($request->employee); if ($employee) { // Create a new event for the appraisal. $event = new LocalEvent(); $event->branch_id = $appraisal->branch; $event->department_id = json_encode([$employee->department_id]); $event->employee_id = json_encode([$employee->id]); $event->title = 'Appraisal Elapsed for ' . $employee->name; $event->start_date = $request->deadline; $event->end_date = $request->deadline; $event->color = '#007bff'; // You can choose a different color. $event->description = 'Appraisal period: ' . Carbon::parse($appraisal->appraisal_date)->toDateString() . ' to ' . $appraisal->deadline . '. Appraisal due for ' . $employee->name . '.'; $event->created_by = \Auth::user()->creatorId(); $event->save(); // Link the event to the employee. $eventEmployee = new EventEmployee(); $eventEmployee->event_id = $event->id; $eventEmployee->employee_id = $employee->id; $eventEmployee->created_by = \Auth::user()->creatorId(); $eventEmployee->save(); } \DB::commit(); return redirect()->route('appraisal.index')->with('success', __('Appraisal successfully created.')); } } catch (\Exception $e) { \DB::rollback(); Log::error('Error creating appraisal(s): ' . $e->getMessage()); return redirect()->back()->with('error', __('An error occurred while creating the appraisal(s). Please try again.')); } } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function show(Appraisal $appraisal) { // Decode the appraisal ratings $appraisalRatings = $appraisal->rating ? json_decode($appraisal->rating, true) : []; // Get all performance types created by the user $all_performance_types = Performance_Type::where('created_by', '=', \Auth::user()->creatorId())->get(); // Get employee info $employee = Employee::find($appraisal->employee); // Get the latest indicator for the employee's branch/department/designation $indicator = Indicator::where('branch', $employee->branch_id) ->where('department', $employee->department_id) ->where('designation', $employee->designation_id) ->latest('created_at') ->first(); // Decode indicator ratings or fallback to empty $indicatorRatings = $indicator && !empty($indicator->rating) ? json_decode($indicator->rating, true) : []; // Filter performance types to only include those with ratings in appraisal or indicator $performance_types = $all_performance_types->filter(function ($performance_type) use ($appraisalRatings, $indicatorRatings) { return $performance_type->types->some(function ($type) use ($appraisalRatings, $indicatorRatings) { return isset($appraisalRatings[$type->id]) || isset($indicatorRatings[$type->id]); }); }); // Pass SEPARATE arrays to the view - this is what your blade template expects return view('appraisal.show', [ 'appraisal' => $appraisal, 'performance_types' => $performance_types, 'ratings' => $indicatorRatings, // This is for the "Indicator" column (target ratings) 'rating' => $appraisalRatings, // This is for the "Appraisal" column (actual ratings) ]); } public function edit(Appraisal $appraisal) { if (\Auth::user()->can('Edit Appraisal')) { $all_performance_types = Performance_Type::where('created_by', '=', \Auth::user()->creatorId())->get(); $employee = Employee::where('created_by', \Auth::user()->creatorId())->get()->pluck('name', 'id'); $employee->prepend('Select Employee', ''); $brances = Branch::where('created_by', '=', \Auth::user()->creatorId())->get(); $rating = json_decode($appraisal->rating, true); // Filter performance types to only include those with ratings in this appraisal $performance_types = $all_performance_types->filter(function ($performance_type) use ($rating) { // Check if any of the types in this performance_type have ratings return $performance_type->types->some(function ($type) use ($rating) { return isset($rating[$type->id]) && !empty($rating[$type->id]); }); }); $is_employee = \Auth::user()->type == 'employee'; return view('appraisal.edit', get_defined_vars()); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function update(Request $request, Appraisal $appraisal) { if (\Auth::user()->can('Edit Appraisal')) { $validator = \Validator::make( $request->all(), [ // 'brances' => 'required', // 'employees' => 'required', 'rating' => 'required', ] ); if ($appraisal->deadline && Carbon::now()->gt(Carbon::parse($appraisal->deadline))) { return redirect()->back()->with('error', __('The deadline to update this appraisal has passed.')); } if ($validator->fails()) { $messages = $validator->getMessageBag(); return redirect()->back()->with('error', $messages->first()); } // $appraisal->branch = $request->brances; // $appraisal->employee = $request->employees; // $appraisal->appraisal_date = $request->appraisal_date; $appraisal->rating = json_encode($request->rating, true); $appraisal->remark = $request->remark; $appraisal->save(); return redirect()->route('appraisal.index')->with('success', __('Appraisal successfully updated.')); } } public function destroy(Appraisal $appraisal) { if (\Auth::user()->can('Delete Appraisal')) { if ($appraisal->created_by == \Auth::user()->creatorId()) { $appraisal->delete(); return redirect()->route('appraisal.index')->with('success', __('Appraisal successfully deleted.')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function empByStar(Request $request) { $employee = Employee::find($request->employee); if (!$employee) { return response()->json([ 'success' => false, 'message' => 'Employee not found' ]); } // Get the latest indicator for this employee $indicator = Indicator::where('branch', $employee->branch_id) ->where('department', $employee->department_id) ->where('designation', $employee->designation_id) ->latest('created_at') ->first(); // Decode ratings or set to empty array $ratings = $indicator && !empty($indicator->rating) ? json_decode($indicator->rating, true) : []; // Get all performance types $performance_types = Performance_Type::where('created_by', '=', \Auth::user()->creatorId())->get(); // Filter to only show performance types that have ratings in the indicator if (!empty($ratings)) { $performance_types = $performance_types->filter(function ($performance_type) use ($ratings) { return $performance_type->types->some(function ($type) use ($ratings) { return isset($ratings[$type->id]); }); }); } $viewRender = view('appraisal.star', compact('ratings', 'performance_types'))->render(); return response()->json([ 'success' => true, 'html' => $viewRender, 'has_indicator' => !empty($ratings) ]); } public function empByStarModified(Request $request) { $employee = Employee::find($request->employee); if (!$employee) { return response()->json([ 'success' => false, 'message' => 'Employee not found' ]); } // Get the latest indicator $indicator = Indicator::where('branch', $employee->branch_id) ->where('department', $employee->department_id) ->where('designation', $employee->designation_id) ->latest('created_at') ->first(); $ratings = $indicator && !empty($indicator->rating) ? json_decode($indicator->rating, true) : []; // Get all performance types $performance_types = Performance_Type::with('types') ->where('created_by', \Auth::user()->creatorId()) ->get(); // Filter to only show performance types with indicator ratings if (!empty($ratings)) { $performance_types = $performance_types->filter(function ($performance_type) use ($ratings) { return $performance_type->types->some(function ($type) use ($ratings) { return isset($ratings[$type->id]); }); })->values(); // Reset array keys } return response()->json([ 'success' => true, 'ratings' => $ratings, 'performance_types' => $performance_types, 'has_indicator' => !empty($ratings) ]); } public function empByStar1(Request $request) { $employee = Employee::find($request->employee); $appraisal = Appraisal::find($request->appraisal); if (!$employee || !$appraisal) { return response()->json([ 'success' => false, 'message' => 'Employee or Appraisal not found' ]); } // Get the latest indicator for this employee $indicator = Indicator::where('branch', $employee->branch_id) ->where('department', $employee->department_id) ->where('designation', $employee->designation_id) ->latest('created_at') ->first(); // Get indicator ratings (for target/expected ratings) $ratings = $indicator && !empty($indicator->rating) ? json_decode($indicator->rating, true) : []; // Get appraisal ratings (for actual ratings) $rating = json_decode($appraisal->rating, true); if (empty($rating)) { return response()->json([ 'success' => false, 'message' => 'No appraisal ratings found' ]); } // Get the rating keys from the appraisal $ratingKeys = array_keys($rating); // Filter performance types to only include those with ratings in this appraisal $performance_types = Performance_Type::where('created_by', \Auth::user()->creatorId()) ->whereHas('types', function ($query) use ($ratingKeys) { $query->whereIn('id', $ratingKeys); }) ->with('types') ->get(); // Filter the types within each performance type to only show rated ones $performance_types = $performance_types->map(function ($performance_type) use ($ratingKeys) { $performance_type->types = $performance_type->types->filter(function ($type) use ($ratingKeys) { return in_array($type->id, $ratingKeys); }); return $performance_type; }); $viewRender = view('appraisal.staredit', compact('ratings', 'rating', 'performance_types'))->render(); return response()->json([ 'success' => true, 'html' => $viewRender ]); } public function getemployee(Request $request) { $user = \Auth::user(); $query = Employee::where('branch_id', $request->branch_id); if ($user->type === 'employee') { $employee = Employee::where('user_id', $user->id)->first(); if ($employee) { $managedDepartments = Department::where('branch_id', $request->branch_id) ->where('manager_id', $employee->id) ->pluck('id'); if ($managedDepartments->count() > 0) { $query->whereIn('department_id', $managedDepartments) ->orWhere('id', $employee->id); // include self } else { $query->where('id', $employee->id); // only self } } } $employees = $query->get(); return response()->json(['employee' => $employees]); } public function getDepartmentByBranch(Request $request) { $query = Department::where('branch_id', $request->branch_id); $user = \Auth::user(); if ($user->type === 'employee') { $employee = Employee::where('user_id', $user->id)->first(); if ($employee) { $query->where('manager_id', $employee->id); } } $data = $query->get(); return response()->json([ 'exists' => $data->count() > 0, 'departments' => $data, ]); } public function getEmployeesByDepartment(Request $request) { $user = \Auth::user(); $query = Employee::where('department_id', $request->department_id); if ($user->type === 'employee') { $employee = Employee::where('user_id', $user->id)->first(); if ($employee) { $isManager = Department::where('id', $request->department_id) ->where('manager_id', $employee->id) ->exists(); if (!$isManager) { // Not manager → only return self $query->where('id', $employee->id); } } } $employees = $query->get(); return response()->json($employees); } public function checkBranchIndicator(Request $request) { $branchExists = Indicator::where('branch', $request->branch_id) ->where('created_by', \Auth::user()->creatorId()) ->exists(); return response()->json(['exists' => $branchExists]); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.30 | Generation time: 0 |
proxy
|
phpinfo
|
Settings