adrema/app/Member/Membership.php

132 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2021-06-24 23:48:08 +02:00
<?php
namespace App\Member;
2021-08-22 22:23:17 +02:00
use App\Activity;
use App\Group;
2023-02-17 11:43:08 +01:00
use App\Nami\HasNamiField;
2021-08-22 22:23:17 +02:00
use App\Subactivity;
2024-09-22 17:32:29 +02:00
use Database\Factories\Member\MembershipFactory;
2022-11-16 23:39:44 +01:00
use Illuminate\Database\Eloquent\Builder;
2021-06-24 23:48:08 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2021-08-22 22:23:17 +02:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2021-06-24 23:48:08 +02:00
2022-11-16 23:39:44 +01:00
/**
* @property int $count
*/
2021-06-24 23:48:08 +02:00
class Membership extends Model
{
2024-09-22 17:32:29 +02:00
/** @use HasFactory<MembershipFactory> */
2021-06-24 23:48:08 +02:00
use HasFactory;
2023-02-17 11:43:08 +01:00
use HasNamiField;
2021-06-24 23:48:08 +02:00
2023-02-17 18:57:11 +01:00
/** @var array<int, string> */
2023-02-21 13:02:31 +01:00
public $guarded = [];
2021-08-22 22:23:17 +02:00
2023-02-17 18:57:11 +01:00
/** @var array<string, string> */
2022-12-14 15:49:12 +01:00
public $casts = [
'from' => 'date',
'to' => 'date',
2022-12-14 15:49:12 +01:00
'promised_at' => 'date',
];
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Activity, self>
*/
2021-08-22 22:23:17 +02:00
public function activity(): BelongsTo
{
return $this->belongsTo(Activity::class);
}
/**
* @return BelongsTo<Group, self>
*/
public function group(): BelongsTo
{
return $this->belongsTo(Group::class);
}
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Subactivity, self>
*/
2021-08-22 22:23:17 +02:00
public function subactivity(): BelongsTo
{
return $this->belongsTo(Subactivity::class);
}
2022-11-16 23:39:44 +01:00
2023-08-25 00:23:38 +02:00
/**
* @return BelongsTo<Member, self>
*/
public function member(): BelongsTo
{
return $this->belongsTo(Member::class);
}
2023-09-05 16:29:22 +02:00
public function isActive(): bool
{
return $this->from->isBefore(now()) && (null === $this->to || $this->to->isAfter(now()));
}
2022-11-16 23:39:44 +01:00
/**
* @param Builder<Membership> $query
*
2023-02-23 01:20:51 +01:00
* @return Builder<Membership>
*/
public function scopeActive(Builder $query): Builder
{
2023-09-05 16:29:22 +02:00
return $query->where('from', '<=', now())
->where(fn ($query) => $query->whereNull('to')->orWhere('to', '>=', now()));
2023-02-23 01:20:51 +01:00
}
/**
* @param Builder<Membership> $query
*
2022-11-16 23:39:44 +01:00
* @return Builder<Membership>
*/
public function scopeIsAgeGroup(Builder $query): Builder
{
return $query->whereHas('subactivity', fn ($builder) => $builder->where('is_age_group', true));
}
/**
* @param Builder<Membership> $query
*
* @return Builder<Membership>
*/
public function scopeIsMember(Builder $query): Builder
{
return $query->whereHas('activity', fn ($builder) => $builder->where('is_member', true));
}
/**
* @param Builder<Membership> $query
*
* @return Builder<Membership>
*/
public function scopeIsLeader(Builder $query): Builder
{
return $query->whereHas('activity', fn ($builder) => $builder->where('has_efz', true));
}
/**
* @param Builder<Membership> $query
*
* @return Builder<Membership>
*/
2023-09-05 16:29:22 +02:00
public function scopeIsTrying(Builder $query): Builder
2022-11-16 23:39:44 +01:00
{
return $query->active()->whereHas('activity', fn ($builder) => $builder->where('is_try', true));
2022-11-16 23:39:44 +01:00
}
2024-01-28 11:42:32 +01:00
public static function booted(): void
{
static::saved(function ($membership) {
$membership->member->touch();
});
static::deleted(function ($membership) {
$membership->member->touch();
});
}
2021-06-24 23:48:08 +02:00
}