2024-02-10 01:42:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Invoice\Actions;
|
|
|
|
|
|
|
|
use App\Invoice\Models\Invoice;
|
|
|
|
use App\Member\Member;
|
|
|
|
use App\Payment\Subscription;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Lorisleiva\Actions\ActionRequest;
|
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
|
|
|
|
class MemberNewInvoiceAction
|
|
|
|
{
|
|
|
|
use AsAction;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'year' => 'required|integer|gte:0',
|
|
|
|
'subscription_id' => 'required|exists:subscriptions,id',
|
2024-02-11 20:21:33 +01:00
|
|
|
'member_id' => 'required|exists:members,id',
|
2024-02-10 01:42:51 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
|
|
|
public function handle(Member $member, Subscription $subscription, int $year): array
|
|
|
|
{
|
|
|
|
$invoice = Invoice::createForMember($member, Member::where('id', $member->id)->get(), $year, $subscription);
|
|
|
|
|
|
|
|
return [
|
|
|
|
...$invoice->getAttributes(),
|
|
|
|
'to' => $invoice->to,
|
|
|
|
'positions' => $invoice->getRelationValue('positions')->toArray(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function asController(ActionRequest $request, Member $member): JsonResponse
|
|
|
|
{
|
2024-02-11 20:21:33 +01:00
|
|
|
$payload = $this->handle(Member::find($request->member_id), Subscription::find($request->input('subscription_id')), $request->input('year'));
|
2024-02-10 01:42:51 +01:00
|
|
|
return response()->json($payload);
|
|
|
|
}
|
|
|
|
}
|