Cost units can be edited

This commit is contained in:
2026-02-08 20:06:38 +01:00
parent 6fc65e195c
commit bccfc11687
53 changed files with 2021 additions and 29 deletions

View File

@@ -0,0 +1,87 @@
<?php
namespace App\Repositories;
use App\Enumerations\CostUnitType;
use App\Enumerations\UserRole;
use App\Models\CostUnit;
use App\Resources\CostUnitResource;
class CostUnitRepository {
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) : ?CostUnit {
$costUnits = self::getCostUnitsByCriteria(['id' => $id], false);
if (count($costUnits) === 0) {
return null;
}
return $costUnits[0];
}
public function getCostUnitsByCriteria(array $criteria, bool $forDisplay = true) : 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) {
if ($forDisplay) {
$visibleCostUnits[] = new CostUnitResource($costUnit)->toArray(request());
} else {
$visibleCostUnits[] = $costUnit;
}
}
}
return $visibleCostUnits;
}
}