Files
mareike/app/Resources/CostUnitResource.php

51 lines
1.9 KiB
PHP

<?php
namespace App\Resources;
use App\Enumerations\InvoiceStatus;
use App\Models\CostUnit;
use App\Repositories\CostUnitRepository;
use App\ValueObjects\Amount;
class CostUnitResource {
private CostUnit $costUnit;
public function __construct(CostUnit $costUnit) {
$this->costUnit = $costUnit;
}
public function toArray($request) {
$costUnitRepository = new CostUnitRepository();
$totalAmount = $costUnitRepository->sumupAmounts($this->costUnit)->getAmount();
$donatedAmount = $costUnitRepository->sumupAmounts($this->costUnit, true)->getAmount();
$countInvoices = $costUnitRepository->countInvoices($this->costUnit);
$countNewInvoices = $countInvoices[InvoiceStatus::INVOICE_STATUS_NEW];
$countApprovedInvoices = $countInvoices[InvoiceStatus::INVOICE_STATUS_APPROVED];
$countDonatedInvoices = $countInvoices[InvoiceStatus::INVOICE_META_STATUS_DONATED];
$countDeniedInvoices = $countInvoices[InvoiceStatus::INVOICE_STATUS_DENIED];
$data = array_merge(
$this->costUnit->toArray(),
[
'distanceAllowanceSmall' => new Amount($this->costUnit->distance_allowance, '')->toString(),
'distanceAllowanceFull' => new Amount($this->costUnit->distance_allowance, ' Euro')->toString(),
'totalAmount' => new Amount($totalAmount, ' Euro')->toString(),
'donatedAmount' => new Amount($donatedAmount, ' Euro')->toString(),
'countNewInvoices' => $countNewInvoices,
'countApprovedInvoices' => $countApprovedInvoices,
'countDonatedInvoices' => $countDonatedInvoices,
'countDeniedInvoices' => $countDeniedInvoices,
'treasurers' => $this->costUnit->treasurers()->get()->map(fn($user) => new UserResource($user))->toArray(),
]);
return $data;
}
}