adrema/app/Member/MemberResource.php

212 lines
9.0 KiB
PHP
Raw Normal View History

2020-06-02 23:45:25 +02:00
<?php
namespace App\Member;
2023-05-02 23:49:16 +02:00
use App\Activity;
use App\Country;
use App\Course\Models\Course;
2022-11-22 00:37:34 +01:00
use App\Course\Resources\CourseMemberResource;
2023-05-02 23:49:16 +02:00
use App\Gender;
use App\Invoice\BillKind;
use App\Lib\HasMeta;
2023-04-25 00:28:44 +02:00
use App\Member\Data\NestedGroup;
use App\Member\Resources\NationalityResource;
use App\Member\Resources\RegionResource;
2021-08-22 22:23:17 +02:00
use App\Membership\MembershipResource;
2023-05-02 23:49:16 +02:00
use App\Nationality;
2021-07-04 16:56:07 +02:00
use App\Payment\PaymentResource;
2023-05-02 23:49:16 +02:00
use App\Payment\Status;
use App\Payment\Subscription;
2022-11-22 00:37:34 +01:00
use App\Payment\SubscriptionResource;
2023-05-02 23:49:16 +02:00
use App\Region;
use App\Subactivity;
2021-07-17 16:57:37 +02:00
use Illuminate\Http\Resources\Json\JsonResource;
2020-06-02 23:45:25 +02:00
2022-01-02 12:32:57 +01:00
/**
* @mixin \App\Member\Member
*/
2020-06-02 23:45:25 +02:00
class MemberResource extends JsonResource
{
use HasMeta;
2020-06-02 23:45:25 +02:00
/**
* Transform the resource into an array.
*
2022-03-11 20:19:17 +01:00
* @param \Illuminate\Http\Request $request
*
2023-02-17 18:57:11 +01:00
* @return array<string, mixed>
2020-06-02 23:45:25 +02:00
*/
public function toArray($request)
{
2021-04-11 02:55:26 +02:00
return [
'firstname' => $this->firstname,
'lastname' => $this->lastname,
'address' => $this->address,
'zip' => $this->zip,
'location' => $this->location,
'send_newspaper' => $this->send_newspaper,
2023-03-06 00:26:17 +01:00
'birthday' => $this->birthday?->format('Y-m-d'),
'birthday_human' => $this->birthday?->format('d.m.Y'),
2021-04-11 17:18:28 +02:00
'joined_at' => $this->joined_at->format('Y-m-d'),
'joined_at_human' => $this->joined_at->format('d.m.Y'),
2021-04-11 11:19:55 +02:00
'id' => $this->id,
2021-07-04 12:09:30 +02:00
'subscription_id' => $this->subscription_id,
2022-11-22 00:37:34 +01:00
'subscription' => new SubscriptionResource($this->whenLoaded('subscription')),
2021-04-11 11:19:55 +02:00
'gender_id' => $this->gender_id,
'gender_name' => $this->gender?->name ?: 'keine Angabe',
2023-08-25 00:23:38 +02:00
'fullname' => ($this->gender ? $this->gender->salutation . ' ' : '') . $this->fullname,
2021-04-11 11:19:55 +02:00
'further_address' => $this->further_address,
'work_phone' => $this->work_phone,
'mobile_phone' => $this->mobile_phone,
'main_phone' => $this->main_phone,
'email' => $this->email,
'email_parents' => $this->email_parents,
'fax' => $this->fax,
2021-04-11 11:44:51 +02:00
'country_id' => $this->country_id,
'region_id' => $this->region_id,
'nationality_id' => $this->nationality_id,
'other_country' => $this->other_country,
'confession_id' => $this->confession_id,
2021-04-11 17:37:26 +02:00
'letter_address' => $this->letter_address,
2022-12-06 23:20:05 +01:00
'bill_kind' => optional($this->bill_kind)->value,
2022-12-06 23:11:57 +01:00
'bill_kind_name' => optional($this->bill_kind)->value,
2022-03-11 20:19:17 +01:00
'has_nami' => null !== $this->nami_id,
2021-06-28 22:55:55 +02:00
'children_phone' => $this->children_phone,
2021-07-04 16:56:07 +02:00
'payments' => PaymentResource::collection($this->whenLoaded('payments')),
2023-08-25 00:23:38 +02:00
'pending_payment' => $this->pending_payment ? number_format($this->pending_payment / 100, 2, ',', '.') . ' €' : null,
2022-11-16 22:28:42 +01:00
'age_group_icon' => $this->ageGroupMemberships->first()?->subactivity->slug,
2022-11-22 00:37:34 +01:00
'courses' => CourseMemberResource::collection($this->whenLoaded('courses')),
2023-09-12 16:54:13 +02:00
'memberships' => MembershipResource::collection($this->whenLoaded('memberships')),
'nationality' => new NationalityResource($this->whenLoaded('nationality')),
'region' => new RegionResource($this->whenLoaded('region')),
'full_address' => $this->fullAddress,
2022-11-22 00:37:34 +01:00
'efz' => $this->efz?->format('Y-m-d'),
'efz_human' => $this->efz?->format('d.m.Y') ?: null,
'ps_at_human' => $this->ps_at?->format('d.m.Y') ?: null,
2023-11-24 13:28:54 +01:00
'recertified_at_human' => $this->recertified_at?->format('d.m.Y') ?: null,
2022-11-22 00:37:34 +01:00
'more_ps_at_human' => $this->more_ps_at?->format('d.m.Y') ?: null,
'without_education_at_human' => $this->without_education_at?->format('d.m.Y') ?: null,
'without_efz_at_human' => $this->without_efz_at?->format('d.m.Y') ?: null,
2022-03-20 16:33:56 +01:00
'efz_link' => $this->getEfzLink(),
2022-11-22 00:37:34 +01:00
'ps_at' => $this->ps_at?->format('Y-m-d'),
'more_ps_at' => $this->more_ps_at?->format('Y-m-d'),
2022-04-28 23:20:41 +02:00
'has_svk' => $this->has_svk,
2022-11-22 00:37:34 +01:00
'nami_id' => $this->nami_id,
2022-04-28 23:20:41 +02:00
'has_vk' => $this->has_vk,
2022-11-22 00:37:34 +01:00
'without_education_at' => $this->without_education_at?->format('Y-m-d'),
'without_efz_at' => $this->without_efz_at?->format('Y-m-d'),
2023-11-24 13:28:54 +01:00
'recertified_at' => $this->recertified_at?->format('Y-m-d'),
2022-04-28 23:20:41 +02:00
'multiply_pv' => $this->multiply_pv,
'multiply_more_pv' => $this->multiply_more_pv,
2022-08-23 23:49:19 +02:00
'age' => $this->getModel()->getAge(),
2022-11-16 22:28:42 +01:00
'is_leader' => $this->leaderMemberships->count() > 0,
'group_id' => $this->group_id,
2023-02-27 22:40:47 +01:00
'salutation' => $this->salutation,
2023-03-02 23:14:25 +01:00
'mitgliedsnr' => $this->mitgliedsnr,
2023-02-27 23:15:57 +01:00
'comment' => $this->comment,
2023-05-16 17:19:56 +02:00
'lat' => $this->lat,
'lon' => $this->lon,
2023-11-24 13:43:52 +01:00
'group_name' => $this->group->name,
2022-11-18 15:55:44 +01:00
'links' => [
2023-09-12 16:54:13 +02:00
'membership_index' => route('member.membership.index', ['member' => $this->getModel()]),
2023-12-17 21:13:52 +01:00
'invoiceposition_index' => route('member.invoice-position.index', ['member' => $this->getModel()]),
2023-10-18 01:11:36 +02:00
'course_index' => route('member.course.index', ['member' => $this->getModel()]),
2022-11-18 15:55:44 +01:00
'show' => route('member.show', ['member' => $this->getModel()]),
2023-05-02 23:57:20 +02:00
'edit' => route('member.edit', ['member' => $this->getModel()]),
2022-11-18 15:55:44 +01:00
],
2021-04-11 02:55:26 +02:00
];
2020-06-02 23:45:25 +02:00
}
/**
* @return array<string, mixed>
2023-03-06 00:26:17 +01:00
*/
public static function meta(): array
{
if (request()->header('X-Meta') === 'false') {
return [];
}
2023-10-31 10:38:32 +01:00
if (request()->header('X-Inertia-Partial-Data', '') !== '' && !str_contains(request()->header('X-Inertia-Partial-Data', ''), 'meta')) {
return [];
}
2023-05-03 00:25:04 +02:00
$activities = Activity::with('subactivities')->get();
$createActivities = Activity::remote()->with(['subactivities' => fn ($q) => $q->remote()])->get();
2023-05-02 23:49:16 +02:00
return [
2023-05-02 23:49:16 +02:00
'filterActivities' => Activity::where('is_filterable', true)->pluck('name', 'id'),
'filterSubactivities' => Subactivity::where('is_filterable', true)->pluck('name', 'id'),
'formActivities' => $activities->pluck('name', 'id'),
'formSubactivities' => $activities->map(function (Activity $activity) {
2023-10-31 10:38:32 +01:00
return ['subactivities' => $activity->subactivities->pluck('name', 'id'), 'id' => $activity->id];
2023-05-03 00:25:04 +02:00
})->pluck('subactivities', 'id'),
'formCreateActivities' => $createActivities->pluck('name', 'id'),
'formCreateSubactivities' => $createActivities->map(function (Activity $activity) {
2023-10-31 10:38:32 +01:00
return ['subactivities' => $activity->subactivities->pluck('name', 'id'), 'id' => $activity->id];
2023-05-02 23:49:16 +02:00
})->pluck('subactivities', 'id'),
2023-04-25 00:28:44 +02:00
'groups' => NestedGroup::cacheForSelect(),
2023-03-14 23:27:15 +01:00
'filter' => FilterScope::fromRequest(request()->input('filter', '')),
2023-05-02 23:49:16 +02:00
'courses' => Course::pluck('name', 'id'),
'regions' => Region::forSelect(),
'subscriptions' => Subscription::pluck('name', 'id'),
'countries' => Country::pluck('name', 'id'),
'genders' => Gender::pluck('name', 'id'),
'billKinds' => BillKind::forSelect(),
'nationalities' => Nationality::pluck('name', 'id'),
2023-06-14 23:20:37 +02:00
'members' => Member::ordered()->get()->map(fn ($member) => ['id' => $member->id, 'name' => $member->fullname]),
2023-05-02 23:13:00 +02:00
'links' => [
'index' => route('member.index'),
'create' => route('member.create'),
'sendpayment' => route('sendpayment.create'),
],
];
}
2023-09-07 16:33:40 +02:00
/**
* @return array<string, mixed>
*/
public static function defaultModel(): array
{
return [
'gender_id' => null,
'salutation' => '',
'nationality_id' => null,
'firstname' => '',
'lastname' => '',
'address' => '',
'further_address' => '',
'zip' => '',
'location' => '',
'birthday' => '',
'region_id' => null,
'country_id' => Country::default(),
'other_country' => '',
'main_phone' => '',
'mobile_phone' => '',
'work_phone' => '',
'children_phone' => '',
'email' => '',
'email_parents' => '',
'fax' => '',
'letter_address' => '',
'bill_kind' => null,
'subscription_id' => null,
'has_nami' => false,
'send_newspaper' => false,
'joined_at' => now()->format('Y-m-d'),
'comment' => '',
'first_activity_id' => null,
'first_subactivity_id' => null,
'efz' => null,
'ps_at' => null,
'more_ps_at' => null,
'without_education_at' => null,
'without_efz_at' => null,
'has_vk' => false,
'has_svk' => false,
'multiply_pv' => false,
'multiply_more_pv' => false,
];
}
2020-06-02 23:45:25 +02:00
}