getCostUnitsByCriteria([ 'allow_new' => true, 'type' => $type, 'archived' => false ], true, true); } public function getCurrentEvents() : array { return $this->getCostUnitsByCriteria([ 'allow_new' => true, 'type' => CostUnitType::COST_UNIT_TYPE_EVENT, 'archived' => false ]); } public function getRunningJobs() : array { return $this->getCostUnitsByCriteria([ 'allow_new' => true, 'type' => CostUnitType::COST_UNIT_TYPE_RUNNING_JOB, 'archived' => false ]); } public function getArchivedCostUnits() : array { return $this->getCostUnitsByCriteria([ 'archived' => true, 'allow_new' => false, ]); } public function getClosedCostUnits() : array{ return $this->getCostUnitsByCriteria([ 'archived' => false, 'allow_new' => false, ]); } public function getById(int $id, bool $disableAccessCheck = false) : ?CostUnit { $costUnits = self::getCostUnitsByCriteria(['id' => $id], false, $disableAccessCheck); if (count($costUnits) === 0) { return null; } return $costUnits[0]; } public function getCostUnitsByCriteria(array $criteria, bool $forDisplay = true, $disableAccessCheck = false) : array { $tenant = app('tenant'); $canSeeAll = false; $user = Auth()->user(); if ($tenant->slug !== 'lv') { if ( $user->user_role_main === UserRole::USER_ROLE_ADMIN || in_array( $user->user_role_local_group, [UserRole::USER_ROLE_GROUP_LEADER, UserRole::USER_ROLE_ADMIN]) ) { $canSeeAll = true; } } else { if ( in_array( $user->user_role_main, [UserRole::USER_ROLE_GROUP_LEADER, UserRole::USER_ROLE_ADMIN]) ) { $canSeeAll = true; } } $visibleCostUnits = []; /** @var CostUnit $costUnit */ foreach (Costunit::where($criteria)->get() as $costUnit) { if ($costUnit->tresurers()->where('user_id', $user->id)->exists() || $canSeeAll || $disableAccessCheck) { if ($forDisplay) { $visibleCostUnits[] = new CostUnitResource($costUnit)->toArray(request()); } else { $visibleCostUnits[] = $costUnit; } } } return $visibleCostUnits; } }