Shifts filter: Only show rooms with shifts

This commit is contained in:
Igor Scheller 2023-04-30 13:41:59 +02:00 committed by Michael Weimann
parent 983fb988b4
commit 8bb8837e38
1 changed files with 22 additions and 3 deletions

View File

@ -4,6 +4,8 @@ use Engelsystem\Database\Db;
use Engelsystem\Helpers\Carbon; use Engelsystem\Helpers\Carbon;
use Engelsystem\Models\AngelType; use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\Shifts\NeededAngelType;
use Engelsystem\Models\Shifts\Shift;
use Engelsystem\Models\UserAngelType; use Engelsystem\Models\UserAngelType;
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
@ -113,9 +115,26 @@ function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $da
/** /**
* @return Room[]|Collection * @return Room[]|Collection
*/ */
function load_rooms() function load_rooms(bool $onlyWithActiveShifts = false)
{ {
$rooms = Room::orderBy('name')->get(); $rooms = Room::orderBy('name');
if ($onlyWithActiveShifts) {
$roomIdsFromAngelType = NeededAngelType::query()
->whereNotNull('room_id')
->select('room_id');
$roomIdsFromShift = Shift::query()
->leftJoin('needed_angel_types', 'shifts.id', 'needed_angel_types.shift_id')
->whereNotNull('needed_angel_types.shift_id')
->select('shifts.room_id');
$rooms->whereIn('id', $roomIdsFromAngelType)
->orWhereIn('id', $roomIdsFromShift);
}
$rooms = $rooms->get();
if ($rooms->isEmpty()) { if ($rooms->isEmpty()) {
error(__('The administration has not configured any rooms yet.')); error(__('The administration has not configured any rooms yet.'));
throw_redirect(page_link_to('/')); throw_redirect(page_link_to('/'));
@ -210,7 +229,7 @@ function view_user_shifts()
$session = session(); $session = session();
$days = load_days(); $days = load_days();
$rooms = load_rooms(); $rooms = load_rooms(true);
$types = load_types(); $types = load_types();
$ownAngelTypes = []; $ownAngelTypes = [];