Fix HR branch isolation for leave balances and attendance records, and separate My Attendance

This commit is contained in:
2026-07-06 12:50:37 +08:00
parent 91adb44439
commit 755650c3b3
416 changed files with 14578 additions and 14536 deletions

View File

@@ -25,9 +25,11 @@ class AttendanceRecordController extends Controller
$user = Auth::user();
// Employee self-service: redirect to calendar but pass their own records only
if ($user->type === 'employee') {
if ($user->type === 'employee' || $request->has('my_attendance')) {
// For employees, show only their own attendance in calendar view
return redirect()->route('hr.attendance-records.calendar', $request->all());
if (!$request->routeIs('hr.attendance-records.calendar')) {
return redirect()->route('hr.attendance-records.calendar', array_merge($request->all(), ['my_attendance' => 1]));
}
}
// Default to calendar view unless explicitly requesting list view
@@ -196,7 +198,16 @@ class AttendanceRecordController extends Controller
// Get employees for filter dropdown (compatible with getFilteredEmployees logic)
$employeeQuery = Employee::whereIn('created_by', getCompanyAndUsersId());
if (Auth::user()->can('manage-own-attendance-records') && ! Auth::user()->can('manage-any-attendance-records')) {
if (Auth::user()->can('manage-attendance-records') && !Auth::user()->can('manage-any-attendance-records')) {
$branchId = Auth::user()->employee->branch_id ?? null;
if ($branchId) {
$employeeQuery->where('branch_id', $branchId);
} else {
$employeeQuery->where(function ($q) {
$q->where('created_by', Auth::id())->orWhere('user_id', Auth::id());
});
}
} elseif (Auth::user()->can('manage-own-attendance-records') && ! Auth::user()->can('manage-any-attendance-records')) {
$employeeQuery->where(function ($q) {
$q->where('created_by', Auth::id())->orWhere('user_id', Auth::id());
});
@@ -947,7 +958,7 @@ class AttendanceRecordController extends Controller
$user = Auth::user();
// Employee self-service: show only their own calendar
if ($user->type === 'employee') {
if ($user->type === 'employee' || $request->has('my_attendance')) {
$month = $request->input('month', date('n'));
$year = $request->input('year', date('Y'));
@@ -1125,6 +1136,19 @@ class AttendanceRecordController extends Controller
->where('status', 'completed');
});
if (Auth::user()->can('manage-any-attendance-records')) {
$query->whereIn('created_by', getCompanyAndUsersId());
} else {
$myBranchId = Auth::user()->employee->branch_id ?? null;
if ($myBranchId) {
$query->whereHas('employee', function ($q) use ($myBranchId) {
$q->where('branch_id', $myBranchId);
});
} else {
$query->where('id', Auth::id());
}
}
if ($department_id) {
$query->whereHas('employee', function($q) use ($department_id) {
$q->where('department_id', $department_id);