Files
mareike/app/Resources/CostUnitResource.php

62 lines
2.5 KiB
PHP

<?php
namespace App\Resources;
use App\Enumerations\InvoiceStatus;
use App\Enumerations\InvoiceType;
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];
$amounts = [];
$overAllAmount = new Amount(0, 'Euro');
foreach (InvoiceType::all() as $invoiceType) {
$overAllAmount->addAmount($costUnitRepository->sumupByInvoiceType($this->costUnit, $invoiceType));
$amounts[$invoiceType->slug]['string'] = $costUnitRepository->sumupByInvoiceType($this->costUnit, $invoiceType)->toString();
$amounts[$invoiceType->slug]['name'] = $invoiceType->name;
}
$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(),
'amounts' => $amounts,
'overAllAmount' => ['text' => $overAllAmount->toString(), 'value' => $overAllAmount],
]);
return $data;
}
}