adrema/app/Member/Member.php

154 lines
4.4 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
use App\Events\MemberCreated;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
2021-04-11 18:17:40 +02:00
use App\Bill\BillKind;
2021-06-13 11:33:50 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Nationality;
use App\Group;
2021-06-23 01:05:17 +02:00
use App\Activity;
use App\Subactivity;
2021-06-24 23:48:08 +02:00
use Zoomyboy\LaravelNami\NamiUser;
2021-07-04 12:09:30 +02:00
use App\Payment\Subscription;
2021-07-04 16:56:07 +02:00
use App\Payment\Payment;
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
public $dates = ['joined_at', 'birthday'];
public $casts = [
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
];
2020-06-02 23:45:25 +02:00
public function scopeSearch($q, $text) {
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
}
2020-06-02 23:45:25 +02:00
2020-04-12 00:26:44 +02:00
//----------------------------------- Getters -----------------------------------
public function getFullnameAttribute() {
return $this->firstname.' '.$this->lastname;
}
2021-06-21 23:50:09 +02:00
public function getHasNamiAttribute() {
return $this->nami_id !== null;
}
2021-06-28 22:09:41 +02:00
public function getNamiMemberships($api) {
return $api->group($this->group->nami_id)->member($this->nami_id)->memberships()->toArray();
}
2021-07-04 12:09:30 +02:00
public function getNamiFeeId() {
if (!$this->subscription) {
return null;
}
return $this->subscription->fee->nami_id;
}
2020-04-12 00:26:44 +02:00
//---------------------------------- Relations ----------------------------------
public function country()
{
return $this->belongsTo(\App\Country::class);
}
public function gender()
{
return $this->belongsTo(\App\Gender::class);
}
public function region()
{
return $this->belongsTo(\App\Region::class);
}
public function confession()
{
return $this->belongsTo(\App\Confession::class);
}
public function payments()
{
2021-07-04 16:56:07 +02:00
return $this->hasMany(Payment::class)->orderBy('nr');
2020-04-12 00:26:44 +02:00
}
public function way()
{
2020-06-02 23:45:25 +02:00
return $this->belongsTo(App\Way::class);
2020-04-12 00:26:44 +02:00
}
public function nationality()
{
2021-06-13 11:33:50 +02:00
return $this->belongsTo(Nationality::class);
2020-04-12 00:26:44 +02:00
}
public function memberships()
{
2021-06-24 23:48:08 +02:00
return $this->hasMany(Membership::class);
2020-04-12 00:26:44 +02:00
}
2021-07-04 12:09:30 +02:00
public function subscription()
2020-04-12 00:26:44 +02:00
{
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
public function billKind() {
return $this->belongsTo(BillKind::class);
}
2021-06-13 11:33:50 +02:00
public function group() {
return $this->belongsTo(Group::class);
}
2021-06-23 01:05:17 +02:00
public function firstActivity() {
return $this->belongsTo(Activity::class, 'first_activity_id');
}
public function firstSubActivity() {
return $this->belongsTo(Subactivity::class, 'first_subactivity_id');
}
2021-04-11 18:17:40 +02:00
public static function booted() {
static::updating(function($model) {
if ($model->nami_id === null) {
$model->bill_kind_id = null;
}
});
2021-07-04 19:09:59 +02:00
static::deleting(function($model) {
$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 -----------------------------------
public function scopeWithIsConfirmed($q) {
$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-04 18:29:21 +02:00
public function scopeWithSubscriptionName($q) {
return $q->addSelect([
'subscription_name' => Subscription::select('name')->whereColumn('subscriptions.id', 'members.subscription_id')->limit(1)
]);
}
2020-04-12 00:26:44 +02:00
}