adrema/app/Invoice/Actions/InvoiceStoreAction.php

64 lines
1.8 KiB
PHP
Raw Normal View History

2023-12-13 00:35:39 +01:00
<?php
namespace App\Invoice\Actions;
2023-12-16 11:18:00 +01:00
use App\Invoice\BillKind;
2023-12-13 00:35:39 +01:00
use App\Invoice\Enums\InvoiceStatus;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
use App\Invoice\Models\Invoice;
2023-12-16 23:53:18 +01:00
use App\Lib\Events\Succeeded;
2023-12-13 00:35:39 +01:00
use Illuminate\Validation\Rule;
class InvoiceStoreAction
{
use AsAction;
/**
* @return array<string, string|array<int, string|Rule>>
*/
public function rules(): array
{
return [
'status' => ['required', 'string', 'max:255', Rule::in(InvoiceStatus::values())],
2023-12-16 11:18:00 +01:00
'via' => ['required', 'string', 'max:255', Rule::in(BillKind::values())],
2023-12-13 00:35:39 +01:00
'to' => 'array',
'to.address' => 'required|string|max:255',
'to.location' => 'required|string|max:255',
'to.zip' => 'required|string|max:255',
'to.name' => 'required|string|max:255',
'greeting' => 'required|string|max:255',
'positions' => 'array',
'positions.*.description' => 'required|string|max:300',
'positions.*.price' => 'required|integer|min:0',
'positions.*.member_id' => 'required|exists:members,id',
];
}
/**
* @return array<string, string>
*/
public function getValidationAttributes(): array
{
return [
'to.address' => 'Adresse',
'to.name' => 'Name',
'to.zip' => 'PLZ',
'to.location' => 'Ort',
'status' => 'Status',
2023-12-16 11:18:00 +01:00
'via' => 'Rechnungsweg',
2023-12-13 00:35:39 +01:00
];
}
public function handle(ActionRequest $request): void
{
$invoice = Invoice::create($request->safe()->except('positions'));
foreach ($request->validated('positions') as $position) {
$invoice->positions()->create($position);
}
2023-12-16 23:53:18 +01:00
Succeeded::message('Rechnung erstellt.')->dispatch();
2023-12-13 00:35:39 +01:00
}
}