Cost units can be edited
This commit is contained in:
87
app/Repositories/CostUnitRepository.php
Normal file
87
app/Repositories/CostUnitRepository.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user