Implement HR Branch isolation and Shift isolation
This commit is contained in:
@@ -47,7 +47,7 @@ class AttendanceRecordController extends Controller
|
||||
if (Auth::user()->can('manage-any-attendance-records')) {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId());
|
||||
} elseif (Auth::user()->can('manage-attendance-records')) {
|
||||
$branchId = Auth::user()->employee->branch_id ?? null;
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$q->where(function ($subQ) use ($branchId) {
|
||||
$subQ->where('attendance_records.branch_id', $branchId)
|
||||
@@ -200,7 +200,7 @@ class AttendanceRecordController extends Controller
|
||||
$employeeQuery = Employee::whereIn('created_by', getCompanyAndUsersId());
|
||||
|
||||
if (Auth::user()->can('manage-attendance-records') && !Auth::user()->can('manage-any-attendance-records')) {
|
||||
$branchId = Auth::user()->employee->branch_id ?? null;
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$employeeQuery->where('branch_id', $branchId);
|
||||
} else {
|
||||
@@ -1141,7 +1141,7 @@ class AttendanceRecordController extends Controller
|
||||
if (Auth::user()->can('manage-any-attendance-records')) {
|
||||
$query->whereIn('created_by', getCompanyAndUsersId());
|
||||
} else {
|
||||
$myBranchId = Auth::user()->employee->branch_id ?? null;
|
||||
$myBranchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($myBranchId) {
|
||||
$query->whereHas('employee', function ($q) use ($myBranchId) {
|
||||
$q->where('branch_id', $myBranchId);
|
||||
|
||||
@@ -197,6 +197,12 @@ class EmployeeController extends Controller
|
||||
|
||||
$shifts = \App\Models\Shift::whereIn('created_by', getCompanyAndUsersId())
|
||||
->where('status', 'active')
|
||||
->where(function($q) {
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId && !Auth::user()->can('manage-any-shifts')) {
|
||||
$q->where('branch_id', $branchId)->orWhereNull('branch_id');
|
||||
}
|
||||
})
|
||||
->get(['id', 'name', 'start_time', 'end_time']);
|
||||
|
||||
$attendancePolicies = \App\Models\AttendancePolicy::whereIn('created_by', getCompanyAndUsersId())
|
||||
@@ -520,6 +526,12 @@ class EmployeeController extends Controller
|
||||
|
||||
$shifts = \App\Models\Shift::whereIn('created_by', getCompanyAndUsersId())
|
||||
->where('status', 'active')
|
||||
->where(function($q) {
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId && !Auth::user()->can('manage-any-shifts')) {
|
||||
$q->where('branch_id', $branchId)->orWhereNull('branch_id');
|
||||
}
|
||||
})
|
||||
->get(['id', 'name', 'start_time', 'end_time']);
|
||||
|
||||
$attendancePolicies = \App\Models\AttendancePolicy::whereIn('created_by', getCompanyAndUsersId())
|
||||
|
||||
@@ -104,7 +104,7 @@ class EmployeeSalaryController extends Controller
|
||||
if (Auth::user()->can('manage-any-employee-salaries')) {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId());
|
||||
} elseif (Auth::user()->can('manage-employee-salaries')) {
|
||||
$branchId = Auth::user()->employee->branch_id ?? null;
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$q->whereHas('employee.employee', function ($eq) use ($branchId) {
|
||||
$eq->where('branch_id', $branchId);
|
||||
|
||||
@@ -57,7 +57,7 @@ class LeaveApplicationController extends Controller
|
||||
if (Auth::user()->can('manage-any-leave-applications')) {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId());
|
||||
} elseif (Auth::user()->can('manage-leave-applications')) {
|
||||
$branchId = Auth::user()->employee->branch_id ?? null;
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$q->whereHas('employee.employee', function ($eq) use ($branchId) {
|
||||
$eq->where('branch_id', $branchId);
|
||||
|
||||
@@ -22,7 +22,7 @@ class LeaveBalanceController extends Controller
|
||||
if (Auth::user()->can('manage-any-leave-balances')) {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId());
|
||||
} elseif (Auth::user()->can('manage-leave-balances')) {
|
||||
$branchId = Auth::user()->employee->branch_id ?? null;
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$q->whereHas('employee.employee', function ($eq) use ($branchId) {
|
||||
$eq->where('branch_id', $branchId);
|
||||
@@ -125,7 +125,7 @@ class LeaveBalanceController extends Controller
|
||||
$employeeQuery = Employee::whereIn('created_by', getCompanyAndUsersId());
|
||||
|
||||
if (Auth::user()->can('manage-leave-balances') && !Auth::user()->can('manage-any-leave-balances')) {
|
||||
$branchId = Auth::user()->employee->branch_id ?? null;
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$employeeQuery->where('branch_id', $branchId);
|
||||
} else {
|
||||
|
||||
@@ -49,7 +49,7 @@ class PayslipController extends Controller
|
||||
if (Auth::user()->can('manage-any-payslips')) {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId());
|
||||
} elseif (Auth::user()->can('manage-payslips')) {
|
||||
$branchId = Auth::user()->employee->branch_id ?? null;
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$q->whereHas('employee.employee', function ($eq) use ($branchId) {
|
||||
$eq->where('branch_id', $branchId);
|
||||
|
||||
@@ -16,7 +16,15 @@ class ShiftController extends Controller
|
||||
if (Auth::user()->can('manage-any-shifts')) {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId());
|
||||
} else {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId()); // Show all company shift templates
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$q->where(function($q2) use ($branchId) {
|
||||
$q2->where('branch_id', $branchId)
|
||||
->orWhereNull('branch_id'); // company wide shifts
|
||||
})->whereIn('created_by', getCompanyAndUsersId());
|
||||
} else {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -104,6 +112,12 @@ class ShiftController extends Controller
|
||||
$validated['created_by'] = creatorId();
|
||||
$validated['status'] = $validated['status'] ?? 'active';
|
||||
$validated['is_night_shift'] = $validated['is_night_shift'] ?? false;
|
||||
|
||||
if (!Auth::user()->can('manage-any-shifts')) {
|
||||
$validated['branch_id'] = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
} else {
|
||||
$validated['branch_id'] = null; // company wide
|
||||
}
|
||||
|
||||
// Check if shift with same name already exists
|
||||
$exists = Shift::where('name', $validated['name'])
|
||||
@@ -126,7 +140,12 @@ class ShiftController extends Controller
|
||||
if (Auth::user()->can('manage-any-shifts')) {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId());
|
||||
} else {
|
||||
$q->where('created_by', Auth::id());
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$q->where('branch_id', $branchId)->where('created_by', Auth::id());
|
||||
} else {
|
||||
$q->where('created_by', Auth::id());
|
||||
}
|
||||
}
|
||||
})
|
||||
->first();
|
||||
@@ -174,7 +193,12 @@ class ShiftController extends Controller
|
||||
if (Auth::user()->can('manage-any-shifts')) {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId());
|
||||
} else {
|
||||
$q->where('created_by', Auth::id());
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$q->where('branch_id', $branchId)->where('created_by', Auth::id());
|
||||
} else {
|
||||
$q->where('created_by', Auth::id());
|
||||
}
|
||||
}
|
||||
})
|
||||
->first();
|
||||
@@ -238,7 +262,7 @@ class ShiftController extends Controller
|
||||
});
|
||||
|
||||
if (!Auth::user()->can('manage-any-shifts')) {
|
||||
$branchId = Auth::user()->employee->branch_id ?? null;
|
||||
$branchId = Auth::user()->branch_id ?? Auth::user()->employee->branch_id ?? null;
|
||||
if ($branchId) {
|
||||
$query->whereHas('employee', function($q) use ($branchId) {
|
||||
$q->where('branch_id', $branchId);
|
||||
|
||||
@@ -121,10 +121,12 @@ class UserController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
$branches = \App\Models\Branch::whereIn('created_by', getCompanyAndUsersId())->get(['id', 'name']);
|
||||
|
||||
return Inertia::render('users/index', [
|
||||
'users' => $users,
|
||||
'roles' => $roles,
|
||||
'branches' => $branches,
|
||||
'planLimits' => $planLimits,
|
||||
'filters' => [
|
||||
'search' => $request->search ?? '',
|
||||
@@ -183,6 +185,7 @@ class UserController extends BaseController
|
||||
'password' => Hash::make($request->password),
|
||||
'created_by' => creatorId(),
|
||||
'lang' => $userLang,
|
||||
'branch_id' => $request->branch_id,
|
||||
]);
|
||||
|
||||
if ($user && $request->roles) {
|
||||
@@ -222,6 +225,9 @@ class UserController extends BaseController
|
||||
if ($user) {
|
||||
$user->name = $request->name;
|
||||
$user->email = $request->email;
|
||||
if ($request->has('branch_id')) {
|
||||
$user->branch_id = $request->branch_id;
|
||||
}
|
||||
|
||||
// find and syncing role
|
||||
if ($request->roles) {
|
||||
|
||||
Reference in New Issue
Block a user