adrema/app/Invoice/Resources/InvoiceResource.php

90 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2023-12-16 01:13:49 +01:00
<?php
namespace App\Invoice\Resources;
2023-12-16 20:35:28 +01:00
use App\Invoice\BillKind;
use App\Invoice\Enums\InvoiceStatus;
2023-12-16 01:13:49 +01:00
use App\Invoice\Models\Invoice;
2024-05-14 01:29:39 +02:00
use App\Invoice\Scopes\InvoiceFilterScope;
2023-12-16 01:13:49 +01:00
use App\Lib\HasMeta;
2023-12-16 23:52:41 +01:00
use App\Member\Member;
2024-02-10 01:45:41 +01:00
use App\Payment\Subscription;
2023-12-16 01:13:49 +01:00
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @mixin Invoice
*/
class InvoiceResource extends JsonResource
{
use HasMeta;
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array<string, mixed>
*/
public function toArray($request)
{
return [
2023-12-17 00:45:03 +01:00
'id' => $this->id,
'to' => $this->to,
2023-12-16 01:13:49 +01:00
'sum_human' => number_format($this->positions->sum('price') / 100, 2, ',', '') . ' €',
2023-12-16 13:08:17 +01:00
'sent_at_human' => $this->sent_at?->format('d.m.Y') ?: '',
2023-12-16 01:13:49 +01:00
'status' => $this->status->value,
2023-12-16 11:18:00 +01:00
'via' => $this->via->value,
2023-12-17 00:45:03 +01:00
'positions' => InvoicePositionResource::collection($this->whenLoaded('positions')),
'greeting' => $this->greeting,
2023-12-17 23:00:52 +01:00
'usage' => $this->usage,
2023-12-18 01:15:16 +01:00
'mail_email' => $this->mail_email,
2023-12-17 00:45:03 +01:00
'links' => [
2023-12-17 23:27:55 +01:00
'pdf' => route('invoice.pdf', ['invoice' => $this->getModel()]),
2023-12-18 00:16:58 +01:00
'rememberpdf' => route('invoice.rememberpdf', ['invoice' => $this->getModel()]),
2023-12-17 00:45:03 +01:00
'update' => route('invoice.update', ['invoice' => $this->getModel()]),
2023-12-17 00:55:31 +01:00
'destroy' => route('invoice.destroy', ['invoice' => $this->getModel()]),
2023-12-17 00:45:03 +01:00
]
2023-12-16 01:13:49 +01:00
];
}
/**
* @return array<string, mixed>
*/
public static function meta(): array
{
return [
'links' => [
'mass-store' => route('invoice.mass-store'),
2023-12-16 22:30:56 +01:00
'store' => route('invoice.store'),
2023-12-19 02:00:42 +01:00
'masspdf' => route('invoice.masspdf'),
2024-02-11 20:21:33 +01:00
'newInvoiceAttributes' => route('invoice.new-invoice-attributes')
2023-12-16 20:35:28 +01:00
],
'vias' => BillKind::forSelect(),
'statuses' => InvoiceStatus::forSelect(),
2023-12-16 23:52:41 +01:00
'members' => Member::forSelect(),
2024-02-10 01:45:41 +01:00
'subscriptions' => Subscription::forSelect(),
2024-05-14 01:29:39 +02:00
'filter' => InvoiceFilterScope::fromRequest(request()->input('filter', '')),
2023-12-16 22:30:56 +01:00
'default' => [
'to' => [
'name' => '',
'address' => '',
'zip' => '',
'location' => '',
],
'positions' => [],
'greeting' => '',
'status' => InvoiceStatus::NEW->value,
'via' => null,
2023-12-17 23:00:52 +01:00
'usage' => '',
2023-12-18 01:15:16 +01:00
'mail_email' => '',
2023-12-16 22:30:56 +01:00
],
'default_position' => [
2023-12-17 00:45:03 +01:00
'id' => null,
2023-12-16 22:30:56 +01:00
'price' => 0,
'description' => '',
2023-12-16 23:52:41 +01:00
'member_id' => null,
2023-12-16 22:30:56 +01:00
]
2023-12-16 01:13:49 +01:00
];
}
}