2023-12-16 00:16:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Invoice\Actions;
|
|
|
|
|
2023-12-20 23:38:09 +01:00
|
|
|
use App\Invoice\Events\InvoicesMassStored;
|
2023-12-16 00:16:07 +01:00
|
|
|
use App\Invoice\Models\Invoice;
|
2023-12-16 11:44:32 +01:00
|
|
|
use App\Lib\JobMiddleware\JobChannels;
|
|
|
|
use App\Lib\JobMiddleware\WithJobState;
|
|
|
|
use App\Lib\Queue\TracksJob;
|
2023-12-16 00:16:07 +01:00
|
|
|
use App\Member\Member;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2023-12-20 23:38:09 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2023-12-16 00:16:07 +01:00
|
|
|
use Lorisleiva\Actions\ActionRequest;
|
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
|
|
|
|
class MassStoreAction
|
|
|
|
{
|
|
|
|
use AsAction;
|
2023-12-16 11:44:32 +01:00
|
|
|
use TracksJob;
|
2023-12-16 00:16:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'year' => 'required|numeric',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle(int $year): void
|
|
|
|
{
|
2023-12-20 23:38:09 +01:00
|
|
|
/** @var Collection<int, Invoice> */
|
|
|
|
$invoices = collect([]);
|
|
|
|
|
2023-12-16 00:16:07 +01:00
|
|
|
$memberGroup = Member::payable()->get()
|
|
|
|
->groupBy(fn ($member) => "{$member->bill_kind->value}{$member->lastname}{$member->address}{$member->zip}{$member->location}");
|
|
|
|
foreach ($memberGroup as $members) {
|
|
|
|
$invoice = Invoice::createForMember($members->first());
|
|
|
|
|
|
|
|
foreach ($members as $member) {
|
|
|
|
foreach ($member->subscription->children as $child) {
|
|
|
|
$invoice->positions()->create([
|
2024-01-26 15:06:16 +01:00
|
|
|
'description' => str($child->name)->replace('{name}', $member->firstname . ' ' . $member->lastname)->replace('{year}', (string) $year),
|
2023-12-16 00:16:07 +01:00
|
|
|
'price' => $child->amount,
|
|
|
|
'member_id' => $member->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 23:38:09 +01:00
|
|
|
|
|
|
|
$invoices->push($invoice);
|
2023-12-16 00:16:07 +01:00
|
|
|
}
|
2023-12-20 23:38:09 +01:00
|
|
|
|
|
|
|
event(new InvoicesMassStored($year, $invoices));
|
2023-12-16 00:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function asController(ActionRequest $request): JsonResponse
|
|
|
|
{
|
2023-12-16 11:44:32 +01:00
|
|
|
$this->startJob($request->year);
|
2023-12-16 00:16:07 +01:00
|
|
|
|
|
|
|
return response()->json([]);
|
|
|
|
}
|
2023-12-16 11:44:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $parameters
|
|
|
|
*/
|
|
|
|
public function jobState(WithJobState $jobState, ...$parameters): WithJobState
|
|
|
|
{
|
|
|
|
return $jobState
|
|
|
|
->after('Zahlungen erstellt')
|
|
|
|
->failed('Fehler beim Erstellen von Zahlungen')
|
|
|
|
->shouldReload(JobChannels::make()->add('invoice'));
|
|
|
|
}
|
2023-12-16 00:16:07 +01:00
|
|
|
}
|