Implement HR Branch isolation and Shift isolation
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user