151 lines
4.5 KiB
PHP
151 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Enumerations\CostUnitType;
|
|
use App\Enumerations\InvoiceStatus;
|
|
use App\Enumerations\UserRole;
|
|
use App\Models\CostUnit;
|
|
use App\Resources\CostUnitResource;
|
|
use App\ValueObjects\Amount;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
class CostUnitRepository {
|
|
public function getCostUnitsForNewInvoice(string $type) : array {
|
|
return $this->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;
|
|
}
|
|
|
|
public function listForSummary(int $maxCountCostUnits) : array {
|
|
$costUnits = $this->getCostUnitsByCriteria([
|
|
'archived' => false,
|
|
'tenant' => app('tenant')->slug,
|
|
],false);
|
|
|
|
foreach ($costUnits as &$cu) {
|
|
$cu->new_invoices_count = $cu->invoices()->where('status', 'new')->count();
|
|
$cu->approved_invoices_count = $cu->invoices()->where('status', 'approved')->count();
|
|
}
|
|
|
|
$costUnits = collect($costUnits)
|
|
->sortByDesc('approved_invoices_count')
|
|
->sortByDesc('new_invoices_count')
|
|
->take($maxCountCostUnits)
|
|
->values()
|
|
->all();
|
|
|
|
$returnData = [];
|
|
|
|
foreach ($costUnits as $costUnit) {
|
|
if($costUnit->approved_invoices_count === 0 && $costUnit->new_invoices_count === 0) {
|
|
continue;
|
|
}
|
|
|
|
if (strlen($costUnit->name) > 15) {
|
|
$costUnit->name = substr($costUnit->name, 8, 13) . '...';
|
|
}
|
|
$costUnit->totalAmount = $this->sumupAmounts($costUnit)->toString();
|
|
$returnData[] = $costUnit;
|
|
}
|
|
|
|
return $returnData;
|
|
}
|
|
|
|
public function sumupAmounts(CostUnit $costUnit) : Amount {
|
|
$amount = new Amount(0, '');
|
|
|
|
foreach ($costUnit->invoices()->get() as $invoice) {
|
|
if ($invoice->status === InvoiceStatus::INVOICE_STATUS_DENIED) {
|
|
continue;
|
|
}
|
|
|
|
$amount->addAmount(Amount::fromString($invoice->amount));
|
|
}
|
|
return $amount;
|
|
}
|
|
|
|
}
|