*/ public $guarded = []; /** * @var array */ 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']; /** * @var array */ public $dates = ['try_created_at', 'joined_at', 'birthday', 'efz', 'ps_at', 'more_ps_at', 'without_education_at', 'without_efz_at']; /** * @var array */ public $casts = [ 'pending_payment' => 'integer', 'send_newspaper' => 'boolean', 'gender_id' => 'integer', 'way_id' => 'integer', 'country_id' => 'integer', 'region_id' => 'integer', 'confession_id' => 'integer', 'nami_id' => 'integer', 'has_svk' => 'boolean', 'has_vk' => 'boolean', 'multiply_pv' => 'boolean', 'multiply_more_pv' => 'boolean', 'is_leader' => 'boolean', 'bill_kind' => BillKind::class, 'mitgliedsnr' => 'integer', ]; /** * @return array */ public function phoneNumbers(): array { return ['main_phone', 'mobile_phone', 'work_phone', 'children_phone', 'fax']; } /** * @return array}> */ public function sluggable(): array { return [ 'slug' => ['source' => ['firstname', 'lastname']], ]; } // ---------------------------------- Actions ---------------------------------- public function syncVersion(): void { $version = app(NamiSettings::class)->login()->member($this->group->nami_id, $this->nami_id)->version; $this->update(['version' => $version]); } public function createPayment(array $attributes): void { $this->payments()->create(array_merge($attributes, [ 'last_remembered_at' => now(), ])); } // ----------------------------------- Getters ----------------------------------- public function getFullnameAttribute(): string { return $this->firstname.' '.$this->lastname; } public function getPreferredPhoneAttribute(): ?string { if ($this->mobile_phone) { return $this->mobile_phone; } if ($this->main_phone) { return $this->main_phone; } return null; } public function getPreferredEmailAttribute(): ?string { if ($this->email) { return $this->email; } if ($this->email_parents) { return $this->email_parents; } return null; } public function getEtagAttribute(): string { return $this->updated_at->timestamp.'_'.$this->version; } public function getFullAddressAttribute(): string { return $this->address.', '.$this->zip.' '.$this->location; } public function getEfzLink(): ?string { return $this->isLeader() ? route('efz', ['member' => $this]) : null; } public function getNamiFeeId(): ?int { if (!$this->subscription) { return null; } return $this->subscription->fee->nami_id; } public function isLeader(): bool { return $this->memberships()->isLeader()->exists(); } public function getAge(): ?int { return $this->birthday?->diffInYears(now()); } // ---------------------------------- Relations ---------------------------------- /** * @return BelongsTo */ public function country(): BelongsTo { return $this->belongsTo(Country::class); } /** * @return BelongsTo */ public function gender(): BelongsTo { return $this->belongsTo(Gender::class); } /** * @return BelongsTo */ public function region(): BelongsTo { return $this->belongsTo(Region::class)->withDefault([ 'name' => '-- kein --', 'nami_id' => null, ]); } /** * @return BelongsTo */ public function confession(): BelongsTo { return $this->belongsTo(Confession::class); } /** * @return BelongsTo */ public function nationality(): BelongsTo { return $this->belongsTo(Nationality::class); } /** * @return BelongsTo */ public function subscription(): BelongsTo { return $this->belongsTo(Subscription::class); } /** * @return BelongsTo */ public function group(): BelongsTo { return $this->belongsTo(Group::class); } /** * @return HasMany */ public function courses(): HasMany { return $this->hasMany(CourseMember::class); } /** * @return HasMany */ public function memberships(): HasMany { return $this->hasMany(Membership::class); } /** * @return HasMany */ public function payments(): HasMany { return $this->hasMany(Payment::class)->orderBy('nr'); } /** * @return HasMany */ public function leaderMemberships(): HasMany { return $this->ageGroupMemberships()->isLeader(); } /** * @return HasMany */ public function ageGroupMemberships(): HasMany { return $this->memberships()->isAgeGroup(); } public static function booted() { static::deleting(function (self $model): void { $model->payments->each->delete(); $model->memberships->each->delete(); }); static::saving(fn ($model) => $model->updateSearch()); } // ---------------------------------- Scopes ----------------------------------- /** * @param Builder $query * * @return Builder */ public function scopeOrdered(Builder $query): Builder { return $query->orderByRaw('lastname, firstname'); } /** * @param Builder $query * * @return Builder */ public function scopeWithPendingPayment(Builder $query): Builder { return $query->addSelect([ 'pending_payment' => Payment::selectRaw('SUM(subscription_children.amount)') ->whereColumn('payments.member_id', 'members.id') ->whereNeedsPayment() ->join('subscriptions', 'subscriptions.id', 'payments.subscription_id') ->join('subscription_children', 'subscriptions.id', 'subscription_children.parent_id'), ]); } /** * @param Builder $query * * @return Builder */ public function scopeWhereHasPendingPayment(Builder $query): Builder { return $query->whereHas('payments', function (Builder $q): void { $q->whereNeedsPayment(); }); } /** * @param Builder $query * * @return Builder */ public function scopeWhereAusstand(Builder $query): Builder { return $query->whereHas('payments', function ($q) { return $q->whereHas('status', fn ($q) => $q->where('is_remember', true)); }); } /** * @param Builder $query * * @return Builder */ public function scopePayable(Builder $query): Builder { return $query->where('bill_kind', '!=', null)->where('subscription_id', '!=', null); } /** * @param Builder $query * * @return Builder */ public function scopeWhereNoPayment(Builder $query, int $year): Builder { return $query->whereDoesntHave('payments', function (Builder $q) use ($year) { $q->where('nr', '=', $year); }); } /** * @param Builder $query * * @return Builder */ public function scopeForDashboard(Builder $query): Builder { return $query->selectRaw('SUM(id)'); } /** * @todo refactor this to an actual filter model * * @param Builder $query * @param array $filter * * @return Builder */ public function scopeFilter(Builder $query, array $filter): Builder { if (true === data_get($filter, 'ausstand', false)) { $query->whereAusstand(); } if (data_get($filter, 'bill_kind', false)) { $query->where('bill_kind', BillKind::fromValue($filter['bill_kind'])); } if (data_get($filter, 'subactivity_id', false) || data_get($filter, 'activity_id', false)) { $query->whereHas('memberships', function ($q) use ($filter) { $q->active(); if (data_get($filter, 'subactivity_id', false)) { $q->where('subactivity_id', $filter['subactivity_id']); } if (data_get($filter, 'activity_id', false)) { $q->where('activity_id', $filter['activity_id']); } }); } return $query; } /** * @param Builder $query * * @return Builder */ public function scopeWhereCurrentGroup(Builder $query): Builder { $group = app(NamiSettings::class)->localGroup(); if (!$group) { return $query; } return $query->where('group_id', $group->id); } 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 { $card = new VCard([ 'VERSION' => '3.0', 'FN' => $this->fullname, 'N' => [$this->lastname, $this->firstname, '', '', ''], 'BDAY' => $this->birthday->format('Ymd'), 'CATEGORIES' => 'Scoutrobot', 'UID' => $this->slug, ]); if ($this->main_phone) { $card->add('TEL', $this->main_phone, ['type' => 'voice']); } if ($this->mobile_phone) { $card->add('TEL', $this->mobile_phone, ['type' => 'work']); } if ($this->children_phone) { $card->add('TEL', $this->children_phone, ['type' => 'cell']); } if ($this->email) { $card->add('EMAIL', $this->email, ['type' => 'internet']); } if ($this->email_parents) { $card->add('EMAIL', $this->email_parents, ['type' => 'aol']); } $card->add('ADR', [ '', '', $this->address ?: '', $this->location ?: '', $this->region?->name ?: '', $this->zip ?: '', $this->country?->name ?: '', ]); return $card; } 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), ]); } /** * @return array */ #[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(' '); } }