adrema/app/Invoice/Resources/InvoiceResource.php

84 lines
2.5 KiB
PHP
Raw 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;
use App\Lib\HasMeta;
2023-12-16 23:52:41 +01:00
use App\Member\Member;
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-16 20:35:28 +01:00
],
'vias' => BillKind::forSelect(),
'statuses' => InvoiceStatus::forSelect(),
2023-12-16 23:52:41 +01:00
'members' => Member::forSelect(),
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
];
}
}