adrema/app/Member/MemberController.php

139 lines
5.0 KiB
PHP
Raw Permalink Normal View History

2020-06-02 23:45:25 +02:00
<?php
namespace App\Member;
2021-06-23 01:05:17 +02:00
use App\Activity;
2021-07-17 18:46:02 +02:00
use App\Confession;
use App\Country;
use App\Gender;
use App\Http\Controllers\Controller;
2021-07-04 16:56:07 +02:00
use App\Http\Views\MemberView;
2022-11-07 16:18:11 +01:00
use App\Letter\BillKind;
2021-07-17 18:46:02 +02:00
use App\Nationality;
use App\Payment\Subscription;
use App\Region;
2021-11-19 00:53:19 +01:00
use App\Setting\GeneralSettings;
2022-05-01 21:00:15 +02:00
use App\Setting\NamiSettings;
2022-02-12 16:16:56 +01:00
use Illuminate\Http\RedirectResponse;
2021-07-17 18:46:02 +02:00
use Illuminate\Http\Request;
2021-07-15 20:56:53 +02:00
use Inertia\Response;
use Zoomyboy\LaravelNami\NamiException;
2020-06-02 23:45:25 +02:00
class MemberController extends Controller
{
2022-02-12 16:16:56 +01:00
public array $filter = [
2021-08-22 05:51:25 +02:00
'ausstand' => false,
2021-08-22 16:15:16 +02:00
'bill_kind' => null,
'activity_id' => null,
'subactivity_id' => null,
2021-08-22 05:51:25 +02:00
];
2022-03-11 20:19:17 +01:00
public function index(Request $request, GeneralSettings $settings): Response
{
2021-04-11 01:25:40 +02:00
session()->put('menu', 'member');
session()->put('title', 'Mitglieder');
2022-01-02 12:32:57 +01:00
2021-08-22 05:51:25 +02:00
$query = [
'filter' => array_merge(
$this->filter,
json_decode($request->query('filter', '{}'), true)
),
];
2021-04-11 01:25:40 +02:00
2021-08-22 05:51:25 +02:00
$payload = app(MemberView::class)->index($request, $query['filter']);
2021-07-04 22:32:40 +02:00
$payload['toolbar'] = [
['href' => route('member.create'), 'label' => 'Mitglied anlegen', 'color' => 'primary', 'icon' => 'plus'],
2021-11-19 00:53:19 +01:00
['href' => route('allpayment.create'), 'label' => 'Rechnungen erstellen', 'color' => 'primary', 'icon' => 'plus', 'show' => $settings->hasModule('bill')],
['href' => route('sendpayment.create'), 'label' => 'Rechnungen versenden', 'color' => 'info', 'icon' => 'envelope', 'show' => $settings->hasModule('bill')],
2021-07-04 22:32:40 +02:00
];
2021-08-22 05:51:25 +02:00
$payload['query'] = $query;
2022-01-02 12:32:57 +01:00
$payload['billKinds'] = BillKind::pluck('name', 'id');
2021-07-04 16:56:07 +02:00
2022-02-12 14:47:07 +01:00
return \Inertia::render('member/VIndex', $payload);
2020-06-02 23:45:25 +02:00
}
2021-04-11 11:19:55 +02:00
2022-02-12 16:16:56 +01:00
public function create(): Response
{
2021-06-22 00:22:30 +02:00
session()->put('menu', 'member');
session()->put('title', 'Mitglied erstellen');
2021-06-23 01:05:17 +02:00
$activities = Activity::with('subactivities')->get();
2022-02-12 14:50:27 +01:00
return \Inertia::render('member/VForm', [
2021-06-23 01:05:17 +02:00
'activities' => $activities->pluck('name', 'id'),
2022-03-11 20:19:17 +01:00
'subactivities' => $activities->map(function (Activity $activity) {
2022-01-02 12:32:57 +01:00
return ['subactivities' => $activity->subactivities()->pluck('name', 'id'), 'id' => $activity->id];
2021-06-23 01:05:17 +02:00
})->pluck('subactivities', 'id'),
2022-01-02 12:32:57 +01:00
'billKinds' => BillKind::pluck('name', 'id'),
'genders' => Gender::pluck('name', 'id'),
'countries' => Country::pluck('name', 'id'),
'regions' => Region::where('is_null', false)->pluck('name', 'id'),
'nationalities' => Nationality::pluck('name', 'id'),
'confessions' => Confession::where('is_null', false)->pluck('name', 'id'),
'subscriptions' => Subscription::pluck('name', 'id'),
2021-06-22 23:00:27 +02:00
'data' => [
2022-03-11 20:19:17 +01:00
'country_id' => Country::default(),
2021-06-22 23:00:27 +02:00
],
2021-06-22 00:22:30 +02:00
'mode' => 'create',
]);
}
2022-05-01 21:00:15 +02:00
public function store(MemberRequest $request, NamiSettings $settings): RedirectResponse
2022-02-12 16:16:56 +01:00
{
2022-05-01 21:00:15 +02:00
$request->persistCreate($settings);
2021-06-22 00:22:30 +02:00
return redirect()->route('member.index');
}
2022-02-12 16:16:56 +01:00
public function edit(Member $member, Request $request): Response
{
2021-04-11 11:19:55 +02:00
session()->put('menu', 'member');
2021-06-28 23:15:16 +02:00
session()->put('title', "Mitglied {$member->firstname} {$member->lastname} bearbeiten");
2021-04-11 11:19:55 +02:00
2021-07-05 13:31:20 +02:00
$activities = Activity::with('subactivities')->get();
2022-02-12 14:50:27 +01:00
return \Inertia::render('member/VForm', [
2021-07-05 13:31:20 +02:00
'activities' => $activities->pluck('name', 'id'),
2022-03-11 20:19:17 +01:00
'subactivities' => $activities->map(function ($activity) {
2021-07-05 13:31:20 +02:00
return ['subactivities' => $activity->subactivities->pluck('name', 'id'), 'id' => $activity->id];
})->pluck('subactivities', 'id'),
2022-01-02 12:32:57 +01:00
'billKinds' => BillKind::pluck('name', 'id'),
'genders' => Gender::pluck('name', 'id'),
'countries' => Country::pluck('name', 'id'),
'regions' => Region::where('is_null', false)->pluck('name', 'id'),
'nationalities' => Nationality::pluck('name', 'id'),
'confessions' => Confession::where('is_null', false)->pluck('name', 'id'),
2022-05-17 00:04:00 +02:00
'subscriptions' => Subscription::select('name', 'id')->get(),
2021-06-22 00:22:30 +02:00
'data' => new MemberResource($member),
'mode' => 'edit',
2022-04-29 00:23:28 +02:00
'conflict' => '1' === $request->query('conflict', '0'),
2021-04-11 11:19:55 +02:00
]);
}
2022-02-12 16:16:56 +01:00
public function update(Member $member, MemberRequest $request): RedirectResponse
{
try {
$request->persistUpdate($member);
} catch (NamiException $e) {
if ($e->isConflict()) {
return redirect()->route('member.edit', ['member' => $member, 'conflict' => '1']);
} else {
throw $e;
}
}
2021-04-11 11:19:55 +02:00
return redirect()->route('member.index');
}
2021-07-04 19:09:59 +02:00
2022-02-12 16:16:56 +01:00
public function destroy(Member $member): RedirectResponse
{
2022-11-16 22:08:08 +01:00
if ($member->nami_id) {
DeleteJob::dispatch($member->nami_id);
2021-07-04 19:09:59 +02:00
}
$member->delete();
return redirect()->back();
}
2020-06-02 23:45:25 +02:00
}