adrema/app/Member/Member.php

263 lines
8.5 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\Activity;
2021-04-11 18:17:40 +02:00
use App\Bill\BillKind;
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;
2021-06-13 11:33:50 +02:00
use App\Group;
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;
use App\Region;
use App\Subactivity;
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-11-18 01:54:27 +01:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
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;
2021-07-06 02:25:16 +02:00
use Zoomyboy\LaravelNami\Api;
2020-04-12 00:26:44 +02:00
2022-01-02 12:32:57 +01:00
/**
* @property string $subscription_name
* @property int $pending_payment
* @property bool $is_confirmed
* @property string $age_group_icon
* @property \Carbon\Carbon $try_created_at
*/
2020-04-12 00:26:44 +02:00
class Member extends Model
{
use Notifiable;
2021-06-13 11:33:50 +02:00
use HasFactory;
2020-04-12 00:26:44 +02:00
2021-07-04 12:09:30 +02:00
public $fillable = ['firstname', 'lastname', 'nickname', 'other_country', 'birthday', 'joined_at', 'send_newspaper', 'address', 'further_address', 'zip', 'location', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents', 'nami_id', 'group_id', 'letter_address', 'country_id', 'way_id', 'nationality_id', 'subscription_id', 'region_id', 'gender_id', 'confession_id', 'letter_address', 'bill_kind_id', 'version', 'first_subactivity_id', 'first_activity_id', 'confirmed_at', 'children_phone'];
2020-04-12 00:26:44 +02:00
2021-08-23 01:59:53 +02:00
public $dates = ['try_created_at', 'joined_at', 'birthday'];
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',
2021-06-28 22:09:41 +02:00
'is_confirmed' => 'boolean',
2020-04-12 00:26:44 +02:00
];
public function scopeSearch(Builder $q, ?string $text): Builder {
if (is_null($text)) { return $q; }
2020-06-02 23:45:25 +02:00
return $q->where('firstname', 'LIKE', '%'.$text.'%')
->orWhere('lastname', 'LIKE', '%'.$text.'%')
->orWhere('address', 'LIKE', '%'.$text.'%')
->orWhere('zip', 'LIKE', '%'.$text.'%')
2021-04-10 01:39:39 +02:00
->orWhere('location', 'LIKE', '%'.$text.'%');
2020-04-12 00:26:44 +02:00
}
// ---------------------------------- Actions ----------------------------------
public function syncVersion($api): void
{
$version = $api->group($this->group->nami_id)->member($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(),
]));
}
2020-04-12 00:26:44 +02:00
//----------------------------------- Getters -----------------------------------
2021-07-06 02:25:16 +02:00
public function getFullnameAttribute(): string {
2020-04-12 00:26:44 +02:00
return $this->firstname.' '.$this->lastname;
}
2021-07-06 02:25:16 +02:00
public function getHasNamiAttribute(): bool {
2021-06-21 23:50:09 +02:00
return $this->nami_id !== null;
}
2021-08-20 19:25:17 +02:00
public function getNamiMemberships(Api $api): array {
2021-06-28 22:09:41 +02:00
return $api->group($this->group->nami_id)->member($this->nami_id)->memberships()->toArray();
}
2021-07-06 02:25:16 +02:00
public function getNamiFeeId(): ?int {
2021-07-04 12:09:30 +02:00
if (!$this->subscription) {
return null;
}
return $this->subscription->fee->nami_id;
}
2020-04-12 00:26:44 +02:00
//---------------------------------- Relations ----------------------------------
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
}
2021-07-15 21:20:57 +02:00
public function gender(): BelongsTo
{
2020-04-12 00:26:44 +02:00
return $this->belongsTo(\App\Gender::class);
}
2021-07-15 21:20:57 +02:00
public function region(): BelongsTo
{
2021-07-06 02:25:16 +02:00
return $this->belongsTo(Region::class);
2020-04-12 00:26:44 +02:00
}
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
}
2021-07-15 21:20:57 +02:00
public function payments(): HasMany
{
2021-07-04 16:56:07 +02:00
return $this->hasMany(Payment::class)->orderBy('nr');
2020-04-12 00:26:44 +02:00
}
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
}
2021-07-15 21:20:57 +02:00
public function memberships(): HasMany
{
2021-06-24 23:48:08 +02:00
return $this->hasMany(Membership::class);
2020-04-12 00:26:44 +02:00
}
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
2021-07-15 21:20:57 +02:00
public function billKind(): BelongsTo
{
2021-04-11 18:17:40 +02:00
return $this->belongsTo(BillKind::class);
}
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);
}
2021-07-15 21:20:57 +02:00
public function firstActivity(): BelongsTo
{
2021-06-23 01:05:17 +02:00
return $this->belongsTo(Activity::class, 'first_activity_id');
}
2021-07-15 21:20:57 +02:00
public function firstSubActivity(): BelongsTo
{
2021-06-23 01:05:17 +02:00
return $this->belongsTo(Subactivity::class, 'first_subactivity_id');
}
2021-11-20 00:48:42 +01:00
public function courses(): HasMany
2021-11-18 01:54:27 +01:00
{
2021-11-20 00:48:42 +01:00
return $this->hasMany(CourseMember::class);
2021-11-18 01:54:27 +01:00
}
2021-07-15 21:20:57 +02:00
public static function booted()
{
2021-07-06 02:25:16 +02:00
static::deleting(function(self $model): void {
2021-07-04 19:09:59 +02:00
$model->payments->each->delete();
});
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 -----------------------------------
2021-07-06 02:25:16 +02:00
public function scopeWithIsConfirmed(Builder $q): Builder {
return $q->selectSub('DATEDIFF(NOW(), IFNULL(confirmed_at, DATE_SUB(NOW(), INTERVAL 3 YEAR))) < 712', 'is_confirmed');
2021-06-24 23:48:08 +02:00
}
2021-07-06 02:25:16 +02:00
public function scopeWithSubscriptionName(Builder $q): Builder {
2021-07-04 18:29:21 +02:00
return $q->addSelect([
'subscription_name' => Subscription::select('name')->whereColumn('subscriptions.id', 'members.subscription_id')->limit(1)
]);
}
2021-07-06 02:25:16 +02:00
public function scopeWithPendingPayment(Builder $q): Builder {
2021-07-04 21:47:20 +02:00
return $q->addSelect([
'pending_payment' => Payment::selectRaw('SUM(subscriptions.amount)')
->whereColumn('payments.member_id', 'members.id')
->whereNeedsPayment()
->join('subscriptions', 'subscriptions.id', 'payments.subscription_id')
]);
}
2021-08-22 19:31:20 +02:00
public function scopeWithAgeGroup(Builder $q): Builder {
return $q->addSelect([
'age_group_icon' => Subactivity::select('slug')
->join('memberships', 'memberships.subactivity_id', 'subactivities.id')
->where('subactivities.is_age_group', true)
->whereColumn('memberships.member_id', 'members.id')
->limit(1)
]);
}
2021-07-06 02:25:16 +02:00
public function scopeWhereHasPendingPayment(Builder $q): Builder {
return $q->whereHas('payments', function(Builder $q): void {
$q->whereNeedsPayment();
2021-07-04 23:27:00 +02:00
});
}
2021-08-22 05:51:25 +02:00
public function scopeWhereAusstand(Builder $q): Builder {
return $q->whereHas('payments', function($q) {
return $q->whereHas('status', fn ($q) => $q->where('is_remember', true));
});
}
2021-07-06 02:25:16 +02:00
public function scopePayable(Builder $q): Builder {
return $q->where('bill_kind_id', '!=', null)->where('subscription_id', '!=', null);
2021-07-04 22:32:40 +02:00
}
2021-07-06 02:25:16 +02:00
public function scopeWhereNoPayment(Builder $q, int $year): Builder {
return $q->whereDoesntHave('payments', function(Builder $q) use ($year) {
$q->where('nr', '=', $year);
2021-07-04 22:32:40 +02:00
});
}
2021-07-06 02:25:16 +02:00
public function scopeForDashboard(Builder $q): Builder {
2021-07-04 23:27:00 +02:00
return $q->selectRaw('SUM(id)');
}
2021-08-22 05:51:25 +02:00
public function scopeFilter(Builder $q, array $filter): Builder
{
if (data_get($filter, 'ausstand', false) === true) {
$q->whereAusstand();
}
if (data_get($filter, 'bill_kind', false)) {
$q->where('bill_kind_id', $filter['bill_kind']);
}
if (data_get($filter, 'subactivity_id', false) || data_get($filter, 'activity_id', false)) {
$q->whereHas('memberships', function ($q) use ($filter) {
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']);
}
});
}
2021-08-22 05:51:25 +02:00
return $q;
}
2021-08-23 01:59:53 +02:00
public function scopeEndingTries(Builder $q): Builder
{
return $q->whereHas('memberships', fn ($q) => $q
->where('created_at', '<=', now()->subWeeks(7))
->whereHas('activity', fn ($q) => $q->where('is_try', true)))
->addSelect([
'try_created_at' => Membership::select('created_at')
->whereColumn('memberships.member_id', 'members.id')
->join('activities', 'activities.id', 'memberships.activity_id')
->where('activities.is_try', true)
]);
}
2020-04-12 00:26:44 +02:00
}