adrema/app/Member/Member.php

500 lines
13 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;
2022-11-07 16:18:11 +01:00
use App\Letter\BillKind;
2023-02-05 23:35:08 +01:00
use App\Nami\HasNamiField;
2021-07-15 21:20:57 +02:00
use App\Nationality;
2021-07-04 16:56:07 +02:00
use App\Payment\Payment;
2021-07-15 21:20:57 +02:00
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-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
*/
2020-04-12 00:26:44 +02:00
class Member extends Model
{
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;
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>
*/
2023-02-27 22:40:47 +01: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'];
2022-10-07 14:47:35 +02:00
/**
* @var array<int, string>
*/
2022-11-22 00:37:34 +01:00
public $dates = ['try_created_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
/**
* @return array<string, array{source: array<int, string>}>
*/
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
public function createPayment(array $attributes): void
{
$this->payments()->create(array_merge($attributes, [
'last_remembered_at' => now(),
]));
}
2022-05-17 01:37:07 +02:00
// ----------------------------------- Getters -----------------------------------
2022-03-11 20:19:17 +01:00
public function getFullnameAttribute(): string
{
2020-04-12 00:26:44 +02:00
return $this->firstname.' '.$this->lastname;
}
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
{
return $this->updated_at->timestamp.'_'.$this->version;
}
2022-08-23 23:49:19 +02:00
public function getFullAddressAttribute(): string
{
return $this->address.', '.$this->zip.' '.$this->location;
}
2022-03-20 16:33:56 +01:00
public function getEfzLink(): ?string
{
2022-08-23 23:49:19 +02:00
return $this->isLeader()
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
{
2022-11-16 23:27:01 +01:00
return $this->memberships()->isLeader()->exists();
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
}
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
}
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);
}
/**
* @return HasMany<Payment>
*/
public function payments(): HasMany
{
return $this->hasMany(Payment::class)->orderBy('nr');
}
2022-11-16 23:27:01 +01:00
/**
* @return HasMany<Membership>
*/
public function leaderMemberships(): HasMany
2021-07-15 21:20:57 +02:00
{
2022-11-16 23:27:01 +01:00
return $this->ageGroupMemberships()->isLeader();
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
{
2022-11-16 23:27:01 +01:00
return $this->memberships()->isAgeGroup();
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 {
2021-07-04 19:09:59 +02:00
$model->payments->each->delete();
2022-11-16 23:27:01 +01:00
$model->memberships->each->delete();
2021-07-04 19:09:59 +02:00
});
2023-02-26 20:51:59 +01:00
static::saving(fn ($model) => $model->updateSearch());
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([
2022-12-13 23:11:32 +01:00
'pending_payment' => Payment::selectRaw('SUM(subscription_children.amount)')
2021-07-04 21:47:20 +02:00
->whereColumn('payments.member_id', 'members.id')
->whereNeedsPayment()
2022-12-13 23:11:32 +01:00
->join('subscriptions', 'subscriptions.id', 'payments.subscription_id')
->join('subscription_children', 'subscriptions.id', 'subscription_children.parent_id'),
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-02-17 18:57:11 +01:00
return $query->whereHas('payments', function (Builder $q): void {
2021-07-06 02:25:16 +02:00
$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 scopeWhereAusstand(Builder $query): Builder
2022-03-11 20:19:17 +01:00
{
2023-02-17 18:57:11 +01:00
return $query->whereHas('payments', function ($q) {
2021-08-22 05:51:25 +02:00
return $q->whereHas('status', fn ($q) => $q->where('is_remember', true));
});
}
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 scopeWhereNoPayment(Builder $query, int $year): Builder
2022-03-11 20:19:17 +01:00
{
2023-02-17 18:57:11 +01:00
return $query->whereDoesntHave('payments', function (Builder $q) use ($year) {
2021-07-06 02:25:16 +02:00
$q->where('nr', '=', $year);
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
}
2023-02-17 18:57:11 +01:00
/**
* @todo refactor this to an actual filter model
2023-03-03 00:30:33 +01:00
*
2023-03-14 23:57:52 +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>
*/
2023-03-14 23:27:15 +01:00
public function scopeWithFilter(Builder $query, FilterScope $filter): Builder
2021-08-22 05:51:25 +02:00
{
2023-03-14 23:27:15 +01:00
return $filter->apply($query);
2021-08-22 05:51:25 +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();
[$deprecated1, $deprecated2 , $address, $location, $region, $zip, $country] = $card->ADR->getParts();
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, '', '', ''],
'BDAY' => $this->birthday->format('Ymd'),
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
]);
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,
'zipLocation' => $this->zip.' '.$this->location,
'mglnr' => Lazy::create(fn () => 'Mglnr.: '.$this->nami_id),
]);
}
2023-02-26 20:51:59 +01:00
/**
* @return array<string, mixed>
*/
#[SearchUsingFullText(['search_text'])]
public function toSearchableArray(): array
{
return [
'search_text' => $this->search_text ?: '',
];
}
public function updateSearch(): void
{
$this->attributes['search_text'] = collect([
$this->fullname,
$this->fullAddress,
])->implode(' ');
}
2020-04-12 00:26:44 +02:00
}