adrema/app/Payment/SubscriptionController.php

105 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2021-07-04 01:44:41 +02:00
<?php
namespace App\Payment;
2022-01-02 12:32:57 +01:00
use App\Fee;
2021-07-04 01:44:41 +02:00
use App\Http\Controllers\Controller;
2022-02-12 01:16:37 +01:00
use Illuminate\Http\RedirectResponse;
2021-07-04 01:44:41 +02:00
use Illuminate\Http\Request;
2022-02-12 01:16:37 +01:00
use Inertia\Response;
2021-07-04 01:44:41 +02:00
class SubscriptionController extends Controller
{
2022-02-12 01:16:37 +01:00
public function index(Request $request): Response
{
2021-07-04 01:44:41 +02:00
session()->put('menu', 'subscription');
session()->put('title', 'Beiträge');
2022-02-12 14:28:03 +01:00
return \Inertia::render('subscription/SubscriptionIndex', [
2021-07-04 01:44:41 +02:00
'data' => SubscriptionResource::collection(Subscription::get()),
]);
}
2022-02-12 01:16:37 +01:00
public function create(): Response
{
2021-07-04 01:44:41 +02:00
session()->put('menu', 'subscription');
session()->put('title', 'Beitrag erstellen');
2022-02-12 14:28:03 +01:00
return \Inertia::render('subscription/SubscriptionForm', [
2022-01-02 12:32:57 +01:00
'fees' => Fee::pluck('name', 'id'),
2021-07-04 01:44:41 +02:00
'mode' => 'create',
2022-12-13 23:11:32 +01:00
'data' => [
'children' => [],
],
2023-05-02 23:13:00 +02:00
'meta' => SubscriptionResource::meta(),
2021-07-04 01:44:41 +02:00
]);
}
2022-02-12 01:16:37 +01:00
public function store(Request $request): RedirectResponse
{
2022-12-13 23:11:32 +01:00
$subscriptionParams = $request->validate([
2021-07-04 01:44:41 +02:00
'name' => 'required|max:255',
2022-12-14 00:23:03 +01:00
'split' => 'present|boolean',
2021-07-04 01:44:41 +02:00
'fee_id' => 'required|exists:fees,id',
2022-12-14 23:20:05 +01:00
'for_promise' => 'present|boolean',
2022-11-17 21:05:35 +01:00
], [], [
'fee_id' => 'Nami-Beitrag',
2022-12-14 23:20:05 +01:00
'for_promise' => 'Für Versprechen benutzen',
2022-12-13 23:11:32 +01:00
]);
$children = $request->validate([
'children' => 'present|array',
'children.*.amount' => 'required|numeric',
'children.*.name' => 'required|max:255',
]);
$subscription = Subscription::create($subscriptionParams);
$subscription->children()->createMany($children['children']);
2021-07-04 01:44:41 +02:00
return redirect()->route('subscription.index');
}
2022-02-12 01:16:37 +01:00
public function edit(Subscription $subscription, Request $request): Response
{
2021-07-04 01:44:41 +02:00
session()->put('menu', 'subscription');
session()->put('title', "Beitrag {$subscription->name} bearbeiten");
2022-02-12 14:28:03 +01:00
return \Inertia::render('subscription/SubscriptionForm', [
2022-01-02 12:32:57 +01:00
'fees' => Fee::pluck('name', 'id'),
2021-07-04 01:44:41 +02:00
'mode' => 'edit',
'data' => new SubscriptionResource($subscription),
2023-05-02 23:13:00 +02:00
'meta' => SubscriptionResource::meta(),
2021-07-04 01:44:41 +02:00
]);
}
2022-02-12 01:16:37 +01:00
public function update(Subscription $subscription, Request $request): RedirectResponse
{
2022-12-13 23:11:32 +01:00
$subscriptionParams = $request->validate([
2021-07-04 01:44:41 +02:00
'name' => 'required|max:255',
2022-12-14 00:23:03 +01:00
'split' => 'present|boolean',
2021-07-04 01:44:41 +02:00
'fee_id' => 'required|exists:fees,id',
2022-12-14 23:20:05 +01:00
'for_promise' => 'present|boolean',
2022-11-17 21:05:35 +01:00
], [], [
'fee_id' => 'Nami-Beitrag',
2022-12-14 23:20:05 +01:00
'for_promise' => 'Für Versprechen benutzen',
2022-12-13 23:11:32 +01:00
]);
$subscription->update($subscriptionParams);
$children = $request->validate([
'children' => 'present|array',
'children.*.amount' => 'required|numeric',
'children.*.name' => 'required|max:255',
]);
$subscription->children()->delete();
$subscription->children()->createMany($children['children']);
return redirect()->route('subscription.index');
}
public function destroy(Subscription $subscription): RedirectResponse
{
$subscription->delete();
2021-07-04 01:44:41 +02:00
return redirect()->route('subscription.index');
}
}