adrema/app/Member/Member.php

516 lines
14 KiB
PHP
Raw Normal View History

2020-04-12 00:26:44 +02:00
<?php
2020-06-02 23:45:25 +02:00
namespace App\Member;
2020-04-12 00:26:44 +02:00
2021-07-15 21:20:57 +02:00
use App\Confession;
use App\Country;
2021-11-20 00:48:42 +01:00
use App\Course\Models\CourseMember;
2023-02-17 18:57:11 +01:00
use App\Gender;
2021-06-13 11:33:50 +02:00
use App\Group;
2023-04-18 22:08:45 +02:00
use App\Invoice\BillKind;
use App\Invoice\Models\InvoicePosition;
2023-02-05 23:35:08 +01:00
use App\Nami\HasNamiField;
2021-07-15 21:20:57 +02:00
use App\Nationality;
use App\Payment\Subscription;
2022-10-28 12:01:04 +02:00
use App\Pdf\Sender;
2021-07-15 21:20:57 +02:00
use App\Region;
2022-03-06 02:57:39 +01:00
use App\Setting\NamiSettings;
2022-10-07 22:41:43 +02:00
use Carbon\Carbon;
2022-10-06 20:57:38 +02:00
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Builder;
2021-07-15 21:20:57 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2021-07-06 02:25:16 +02:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2021-07-15 21:20:57 +02:00
use Illuminate\Notifications\Notifiable;
2023-02-26 20:51:59 +01:00
use Laravel\Scout\Attributes\SearchUsingFullText;
2023-03-03 00:30:33 +01:00
use Laravel\Scout\Searchable;
2022-10-06 20:57:38 +02:00
use Sabre\VObject\Component\VCard;
2022-10-07 22:41:43 +02:00
use Sabre\VObject\Reader;
2022-10-28 12:01:04 +02:00
use Spatie\LaravelData\Lazy;
2023-05-16 17:19:56 +02:00
use Zoomyboy\Osm\Address;
use Zoomyboy\Osm\Coordinate;
use Zoomyboy\Osm\Geolocatable;
use Zoomyboy\Osm\HasGeolocation;
2023-03-03 00:30:33 +01:00
use Zoomyboy\Phone\HasPhoneNumbers;
2020-04-12 00:26:44 +02:00
2022-01-02 12:32:57 +01:00
/**
2022-11-17 02:15:29 +01:00
* @property string $subscription_name
* @property int $pending_payment
2022-01-02 12:32:57 +01:00
*/
2023-05-16 17:19:56 +02:00
class Member extends Model implements Geolocatable
2020-04-12 00:26:44 +02:00
{
use Notifiable;
2023-02-05 23:35:08 +01:00
use HasNamiField;
2021-06-13 11:33:50 +02:00
use HasFactory;
2022-10-06 20:57:38 +02:00
use Sluggable;
2023-02-26 20:51:59 +01:00
use Searchable;
2023-03-03 00:30:33 +01:00
use HasPhoneNumbers;
2023-05-16 17:19:56 +02:00
use HasGeolocation;
2020-04-12 00:26:44 +02:00
2023-02-08 00:16:35 +01:00
/**
* @var array<string, string>
*/
2022-05-31 21:50:35 +02:00
public $guarded = [];
2020-04-12 00:26:44 +02:00
2022-10-07 14:47:35 +02:00
/**
* @var array<int, string>
*/
2024-04-25 23:48:51 +02:00
public static array $namiFields = ['firstname', 'lastname', 'joined_at', 'birthday', 'send_newspaper', 'address', 'zip', 'location', 'nickname', 'other_country', 'further_address', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents', 'gender_id', 'confession_id', 'region_id', 'country_id', 'fee_id', 'nationality_id', 'slug', 'subscription_id'];
2022-10-07 14:47:35 +02:00
/**
* @var array<int, string>
*/
2023-11-24 13:28:54 +01:00
public $dates = ['try_created_at', 'recertified_at', 'joined_at', 'birthday', 'efz', 'ps_at', 'more_ps_at', 'without_education_at', 'without_efz_at'];
2020-04-12 00:26:44 +02:00
2022-10-07 14:47:35 +02:00
/**
* @var array<string, string>
*/
2020-04-12 00:26:44 +02:00
public $casts = [
2021-07-04 21:47:20 +02:00
'pending_payment' => 'integer',
2021-04-10 02:11:13 +02:00
'send_newspaper' => 'boolean',
2020-04-12 00:26:44 +02:00
'gender_id' => 'integer',
'way_id' => 'integer',
'country_id' => 'integer',
'region_id' => 'integer',
'confession_id' => 'integer',
'nami_id' => 'integer',
2022-04-28 23:20:41 +02:00
'has_svk' => 'boolean',
'has_vk' => 'boolean',
'multiply_pv' => 'boolean',
'multiply_more_pv' => 'boolean',
2022-09-06 01:25:04 +02:00
'is_leader' => 'boolean',
2022-12-06 23:11:57 +01:00
'bill_kind' => BillKind::class,
2023-03-02 23:14:25 +01:00
'mitgliedsnr' => 'integer',
2020-04-12 00:26:44 +02:00
];
2023-03-03 00:30:33 +01:00
/**
* @return array<int, string>
*/
public function phoneNumbers(): array
{
return ['main_phone', 'mobile_phone', 'work_phone', 'children_phone', 'fax'];
}
2022-10-07 14:47:35 +02:00
/**
2024-01-14 13:51:48 +01:00
* @return SluggableConfig
2022-10-07 14:47:35 +02:00
*/
2022-10-06 20:57:38 +02:00
public function sluggable(): array
{
return [
'slug' => ['source' => ['firstname', 'lastname']],
];
}
// ---------------------------------- Actions ----------------------------------
2022-03-06 02:57:39 +01:00
public function syncVersion(): void
{
2023-02-05 23:35:08 +01:00
$version = app(NamiSettings::class)->login()->member($this->group->nami_id, $this->nami_id)->version;
$this->update(['version' => $version]);
}
2020-06-02 23:45:25 +02:00
2022-05-17 01:37:07 +02:00
// ----------------------------------- Getters -----------------------------------
2022-03-11 20:19:17 +01:00
public function getFullnameAttribute(): string
{
2023-08-15 15:11:12 +02:00
return $this->firstname . ' ' . $this->lastname;
2020-04-12 00:26:44 +02:00
}
2022-10-06 20:57:38 +02:00
public function getPreferredPhoneAttribute(): ?string
{
if ($this->mobile_phone) {
return $this->mobile_phone;
}
if ($this->main_phone) {
return $this->main_phone;
}
return null;
}
2022-10-07 14:47:35 +02:00
public function getPreferredEmailAttribute(): ?string
{
if ($this->email) {
return $this->email;
}
if ($this->email_parents) {
return $this->email_parents;
}
return null;
}
2022-10-06 20:57:38 +02:00
public function getEtagAttribute(): string
{
2023-08-15 15:11:12 +02:00
return $this->updated_at->timestamp . '_' . $this->version;
2022-10-06 20:57:38 +02:00
}
2022-08-23 23:49:19 +02:00
public function getFullAddressAttribute(): string
{
2024-01-28 11:42:32 +01:00
return $this->address && $this->zip && $this->location
? $this->address . ', ' . $this->zip . ' ' . $this->location
: '';
2022-08-23 23:49:19 +02:00
}
2022-03-20 16:33:56 +01:00
public function getEfzLink(): ?string
{
2024-03-07 21:24:12 +01:00
return $this->address && $this->zip && $this->location && $this->birthday
2022-03-20 16:33:56 +01:00
? route('efz', ['member' => $this])
: null;
}
2022-03-11 20:19:17 +01:00
public function getNamiFeeId(): ?int
{
2021-07-04 12:09:30 +02:00
if (!$this->subscription) {
return null;
}
return $this->subscription->fee->nami_id;
}
2022-08-23 23:49:19 +02:00
public function isLeader(): bool
{
2023-10-31 10:38:32 +01:00
return $this->leaderMemberships->count() > 0;
2022-08-23 23:49:19 +02:00
}
2023-03-06 00:26:17 +01:00
public function getAge(): ?int
2022-08-23 23:49:19 +02:00
{
2023-03-06 00:26:17 +01:00
return $this->birthday?->diffInYears(now());
2022-08-23 23:49:19 +02:00
}
2024-01-28 11:42:32 +01:00
protected function getAusstand(): int
{
return (int) $this->invoicePositions()->whereHas('invoice', fn ($query) => $query->whereNeedsPayment())->sum('price');
}
2022-05-17 01:37:07 +02:00
// ---------------------------------- Relations ----------------------------------
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Country, self>
*/
2021-07-15 21:20:57 +02:00
public function country(): BelongsTo
{
return $this->belongsTo(Country::class);
2020-04-12 00:26:44 +02:00
}
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Gender, self>
*/
2021-07-15 21:20:57 +02:00
public function gender(): BelongsTo
{
2023-02-17 18:57:11 +01:00
return $this->belongsTo(Gender::class);
2020-04-12 00:26:44 +02:00
}
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Region, self>
*/
2021-07-15 21:20:57 +02:00
public function region(): BelongsTo
{
return $this->belongsTo(Region::class)->withDefault([
'name' => '-- kein --',
'nami_id' => null,
]);
2020-04-12 00:26:44 +02:00
}
/**
* @return HasMany<InvoicePosition>
*/
public function invoicePositions(): HasMany
{
return $this->hasMany(InvoicePosition::class);
}
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Confession, self>
*/
2021-07-15 21:20:57 +02:00
public function confession(): BelongsTo
{
2021-07-06 02:25:16 +02:00
return $this->belongsTo(Confession::class);
2020-04-12 00:26:44 +02:00
}
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Nationality, self>
*/
2021-07-15 21:20:57 +02:00
public function nationality(): BelongsTo
{
2021-06-13 11:33:50 +02:00
return $this->belongsTo(Nationality::class);
2020-04-12 00:26:44 +02:00
}
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Subscription, self>
*/
2021-07-15 21:20:57 +02:00
public function subscription(): BelongsTo
{
2021-07-04 12:09:30 +02:00
return $this->belongsTo(Subscription::class);
2020-04-12 00:26:44 +02:00
}
2021-04-11 18:17:40 +02:00
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Group, self>
*/
2021-07-15 21:20:57 +02:00
public function group(): BelongsTo
{
2021-06-13 11:33:50 +02:00
return $this->belongsTo(Group::class);
}
2023-02-17 18:57:11 +01:00
/**
* @return HasMany<CourseMember>
*/
2022-11-16 23:27:01 +01:00
public function courses(): HasMany
2021-07-15 21:20:57 +02:00
{
2022-11-16 23:27:01 +01:00
return $this->hasMany(CourseMember::class);
2021-06-23 01:05:17 +02:00
}
2023-02-17 18:57:11 +01:00
/**
* @return HasMany<Membership>
*/
public function memberships(): HasMany
{
return $this->hasMany(Membership::class);
}
2022-11-16 23:27:01 +01:00
/**
* @return HasMany<Membership>
*/
public function leaderMemberships(): HasMany
2021-07-15 21:20:57 +02:00
{
2023-10-31 10:38:32 +01:00
return $this->ageGroupMemberships()->isLeader()->active();
2021-06-23 01:05:17 +02:00
}
2022-11-16 23:27:01 +01:00
/**
* @return HasMany<Membership>
*/
public function ageGroupMemberships(): HasMany
2021-11-18 01:54:27 +01:00
{
return $this->memberships()->isAgeGroup()->active();
2021-11-18 01:54:27 +01:00
}
2021-07-15 21:20:57 +02:00
public static function booted()
{
2022-03-11 20:19:17 +01:00
static::deleting(function (self $model): void {
2022-11-16 23:27:01 +01:00
$model->memberships->each->delete();
2023-04-25 00:32:44 +02:00
$model->courses->each->delete();
$model->invoicePositions->each(function ($position) {
$position->delete();
});
2021-07-04 19:09:59 +02:00
});
2021-04-11 18:17:40 +02:00
}
2021-06-24 23:48:08 +02:00
2021-06-28 22:09:41 +02:00
// ---------------------------------- Scopes -----------------------------------
2023-02-17 18:57:11 +01:00
/**
* @param Builder<self> $query
2023-03-03 00:30:33 +01:00
*
2023-02-17 18:57:11 +01:00
* @return Builder<self>
*/
public function scopeOrdered(Builder $query): Builder
{
2023-02-17 18:57:11 +01:00
return $query->orderByRaw('lastname, firstname');
}
2023-02-17 18:57:11 +01:00
/**
* @param Builder<self> $query
2023-03-03 00:30:33 +01:00
*
2023-02-17 18:57:11 +01:00
* @return Builder<self>
*/
public function scopeWithPendingPayment(Builder $query): Builder
2022-03-11 20:19:17 +01:00
{
2023-02-17 18:57:11 +01:00
return $query->addSelect([
2023-12-19 02:00:42 +01:00
'pending_payment' => InvoicePosition::selectRaw('SUM(price)')
->whereColumn('invoice_positions.member_id', 'members.id')
2024-01-25 23:40:29 +01:00
->whereHas('invoice', fn ($query) => $query->whereNeedsPayment()),
2021-07-04 21:47:20 +02:00
]);
}
2023-02-17 18:57:11 +01:00
/**
* @param Builder<self> $query
2023-03-03 00:30:33 +01:00
*
2023-02-17 18:57:11 +01:00
* @return Builder<self>
*/
public function scopeWhereHasPendingPayment(Builder $query): Builder
2022-03-11 20:19:17 +01:00
{
2023-12-17 01:49:12 +01:00
return $query->whereHas('invoicePositions', fn ($q) => $q->whereHas('invoice', fn ($q) => $q->whereNeedsPayment()));
2021-07-04 23:27:00 +02:00
}
2023-02-17 18:57:11 +01:00
/**
* @param Builder<self> $query
2023-03-03 00:30:33 +01:00
*
2023-02-17 18:57:11 +01:00
* @return Builder<self>
*/
public function scopePayable(Builder $query): Builder
2022-03-11 20:19:17 +01:00
{
2023-02-17 18:57:11 +01:00
return $query->where('bill_kind', '!=', null)->where('subscription_id', '!=', null);
2021-07-04 22:32:40 +02:00
}
2023-02-17 18:57:11 +01:00
/**
* @param Builder<self> $query
2023-03-03 00:30:33 +01:00
*
2023-02-17 18:57:11 +01:00
* @return Builder<self>
*/
public function scopeForDashboard(Builder $query): Builder
2022-03-11 20:19:17 +01:00
{
2023-02-17 18:57:11 +01:00
return $query->selectRaw('SUM(id)');
2021-07-04 23:27:00 +02:00
}
/**
* @param Builder<self> $query
2023-03-03 00:30:33 +01:00
*
* @return Builder<self>
*/
public function scopeWhereCurrentGroup(Builder $query): Builder
{
$group = app(NamiSettings::class)->localGroup();
if (!$group) {
return $query;
}
return $query->where('group_id', $group->id);
}
2022-10-07 22:41:43 +02:00
public static function fromVcard(string $url, string $data): static
{
$settings = app(NamiSettings::class);
$card = Reader::read($data);
[$lastname, $firstname] = $card->N->getParts();
2023-08-15 15:11:12 +02:00
[$deprecated1, $deprecated2, $address, $location, $region, $zip, $country] = $card->ADR->getParts();
2022-10-07 22:41:43 +02:00
return new static([
'joined_at' => now(),
'send_newspaper' => false,
'firstname' => $firstname,
'lastname' => $lastname,
'birthday' => Carbon::createFromFormat('Ymd', $card->BDAY->getValue()),
'slug' => pathinfo($url, PATHINFO_FILENAME),
'address' => $address,
'zip' => $zip,
'location' => $location,
'group_id' => $settings->default_group_id,
'nationality_id' => Nationality::firstWhere('name', 'deutsch')->id,
'subscription_id' => Subscription::firstWhere('name', 'Voll')->id,
]);
}
public function toVcard(): Vcard
2022-10-06 20:57:38 +02:00
{
$card = new VCard([
2022-10-07 14:47:35 +02:00
'VERSION' => '3.0',
2022-10-06 20:57:38 +02:00
'FN' => $this->fullname,
'N' => [$this->lastname, $this->firstname, '', '', ''],
2022-10-06 21:25:35 +02:00
'CATEGORIES' => 'Scoutrobot',
2022-10-07 22:41:43 +02:00
'UID' => $this->slug,
2022-10-06 20:57:38 +02:00
]);
2023-07-24 16:55:07 +02:00
if ($this->birthday) {
$card->add('BDAY', $this->birthday->format('Ymd'));
}
2022-10-07 23:04:38 +02:00
if ($this->main_phone) {
$card->add('TEL', $this->main_phone, ['type' => 'voice']);
2022-10-06 20:57:38 +02:00
}
if ($this->mobile_phone) {
2022-10-07 23:04:38 +02:00
$card->add('TEL', $this->mobile_phone, ['type' => 'work']);
2022-10-06 20:57:38 +02:00
}
2022-10-06 21:25:35 +02:00
if ($this->children_phone) {
2022-10-07 23:04:38 +02:00
$card->add('TEL', $this->children_phone, ['type' => 'cell']);
2022-10-06 21:25:35 +02:00
}
2022-10-06 20:57:38 +02:00
if ($this->email) {
2022-10-07 23:04:38 +02:00
$card->add('EMAIL', $this->email, ['type' => 'internet']);
2022-10-06 20:57:38 +02:00
}
2022-10-07 14:47:35 +02:00
2022-10-06 20:57:38 +02:00
if ($this->email_parents) {
2022-10-07 23:04:38 +02:00
$card->add('EMAIL', $this->email_parents, ['type' => 'aol']);
2022-10-06 20:57:38 +02:00
}
2022-10-06 21:25:35 +02:00
$card->add('ADR', [
'',
'',
2022-10-06 20:57:38 +02:00
$this->address ?: '',
$this->location ?: '',
$this->region?->name ?: '',
$this->zip ?: '',
$this->country?->name ?: '',
2022-10-06 21:25:35 +02:00
]);
2022-10-06 20:57:38 +02:00
return $card;
}
2022-10-28 12:01:04 +02:00
public function toSender(): Sender
{
return Sender::from([
'name' => $this->fullname,
'address' => $this->address,
2023-08-15 15:11:12 +02:00
'zipLocation' => $this->zip . ' ' . $this->location,
'mglnr' => Lazy::create(fn () => 'Mglnr.: ' . $this->nami_id),
2022-10-28 12:01:04 +02:00
]);
}
2023-02-26 20:51:59 +01:00
2023-12-16 23:52:41 +01:00
/**
* @return array<int, array{id: int, name: string}>
*/
public static function forSelect(): array
{
return static::select(['id', 'firstname', 'lastname'])->get()->map(fn ($member) => ['id' => $member->id, 'name' => $member->fullname])->toArray();
}
2023-05-16 17:19:56 +02:00
// -------------------------------- Geolocation --------------------------------
// *****************************************************************************
public function fillCoordinate(Coordinate $coordinate): void
{
$this->updateQuietly(['lat' => $coordinate->lat, 'lon' => $coordinate->lon]);
}
public function getAddressForGeolocation(): ?Address
{
return new Address($this->address, $this->zip, $this->location);
}
public function destroyCoordinate(): void
{
$this->updateQuietly([
'lat' => null,
'lon' => null,
]);
}
2023-08-15 15:11:12 +02:00
public function needsGeolocationUpdate(): bool
{
return $this->getOriginal('address') !== $this->address
|| $this->getOriginal('zip') !== $this->zip
|| $this->getOriginal('location') !== $this->location;
}
2024-01-25 23:40:29 +01:00
// --------------------------------- Searching ---------------------------------
// *****************************************************************************
/**
* Get the indexable data array for the model.
*
* @return array<string, mixed>
*/
public function toSearchableArray()
{
return [
'address' => $this->fullAddress,
'fullname' => $this->fullname,
2024-01-28 11:42:32 +01:00
'firstname' => $this->firstname,
'lastname' => $this->lastname,
'birthday' => $this->birthday?->format('Y-m-d'),
'ausstand' => $this->getAusstand(),
2024-01-28 18:38:20 +01:00
'bill_kind' => $this->bill_kind?->value,
2024-01-28 11:42:32 +01:00
'group_id' => $this->group->id,
2024-01-29 01:37:28 +01:00
'group_name' => $this->group->inner_name ?: $this->group->name,
'links' => [
'show' => route('member.show', ['member' => $this], false),
'edit' => route('member.edit', ['member' => $this], false),
2024-01-29 01:37:28 +01:00
],
'age_group_icon' => $this->ageGroupMemberships->first()?->subactivity->slug,
'is_leader' => $this->leaderMemberships()->count() > 0,
2024-01-28 11:42:32 +01:00
'memberships' => $this->memberships()->active()->get()
2024-01-29 00:13:03 +01:00
->map(fn ($membership) => [...$membership->only('activity_id', 'subactivity_id'), 'both' => $membership->activity_id . '|' . $membership->subactivity_id, 'with_group' => $membership->group_id . '|' . $membership->activity_id . '|' . $membership->subactivity_id]),
2024-01-25 23:40:29 +01:00
];
}
2020-04-12 00:26:44 +02:00
}