Files
roxane/app/Models/Member.php

170 lines
6.7 KiB
PHP
Raw Normal View History

2025-10-22 17:09:48 +02:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
2025-10-26 00:16:25 +02:00
use Illuminate\Notifications\Notifiable;
2025-10-22 17:09:48 +02:00
2025-10-24 14:09:54 +02:00
/**
* @property int $id
* @property int|null $user_id
2025-11-15 17:09:56 +01:00
* @property string|null $dolibarr_id
2025-10-24 14:09:54 +02:00
* @property string|null $keycloak_id
* @property string $status
* @property string $nature
2025-11-15 17:09:56 +01:00
* @property int|null $type_id
2025-10-24 14:09:54 +02:00
* @property int|null $group_id
* @property string|null $lastname
* @property string|null $firstname
* @property string $email
2026-01-30 15:27:23 +01:00
* @property string $retzien_email
2025-10-24 14:09:54 +02:00
* @property string|null $company
* @property string|null $date_of_birth
* @property string|null $address
* @property string|null $zipcode
* @property string|null $city
* @property string|null $country
* @property string|null $phone1
* @property string|null $phone2
* @property int $public_membership
2025-11-15 17:09:56 +01:00
* @property string|null $website_url
2025-10-24 14:09:54 +02:00
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property string|null $deleted_at
* @property-read string $full_name
* @property-read \App\Models\MemberGroup|null $group
2026-01-30 15:27:23 +01:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\IspconfigMember> $ispconfigs
* @property-read int|null $ispconfigs_count
2025-10-24 14:09:54 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Membership> $memberships
* @property-read int|null $memberships_count
2026-01-30 15:27:23 +01:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\NextCloudMember> $nextcloudAccounts
* @property-read int|null $nextcloud_accounts_count
2025-11-15 17:09:56 +01:00
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
* @property-read int|null $notifications_count
2025-10-24 14:09:54 +02:00
* @property-read \App\Models\User|null $user
*
2025-10-24 14:09:54 +02:00
* @method static \Database\Factories\MemberFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member query()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereAddress($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereCity($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereCompany($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereCountry($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereDateOfBirth($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereDeletedAt($value)
2025-11-15 17:09:56 +01:00
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereDolibarrId($value)
2025-10-24 14:09:54 +02:00
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereEmail($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereFirstname($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereGroupId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereKeycloakId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereLastname($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereNature($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member wherePhone1($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member wherePhone2($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member wherePublicMembership($value)
2025-11-15 17:09:56 +01:00
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereRetzienEmail($value)
2025-10-24 14:09:54 +02:00
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereStatus($value)
2025-11-15 17:09:56 +01:00
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereTypeId($value)
2025-10-24 14:09:54 +02:00
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereUserId($value)
2025-11-15 17:09:56 +01:00
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereWebsiteUrl($value)
2025-10-24 14:09:54 +02:00
* @method static \Illuminate\Database\Eloquent\Builder<static>|Member whereZipcode($value)
*
2025-10-24 14:09:54 +02:00
* @mixin \Eloquent
*/
2025-10-22 17:09:48 +02:00
class Member extends Model
{
2025-10-26 00:16:25 +02:00
use HasFactory, Notifiable;
2025-10-22 17:09:48 +02:00
protected $fillable = [
'user_id',
2025-11-15 17:09:56 +01:00
'dolibarr_id',
2025-10-22 17:09:48 +02:00
'keycloak_id',
'status',
'nature',
2025-11-15 17:09:56 +01:00
'type_id',
2025-10-22 17:09:48 +02:00
'group_id',
'lastname',
'firstname',
'email',
'company',
'date_of_birth',
'address',
'zipcode',
'city',
'country',
'phone1',
'phone2',
2025-10-24 14:09:54 +02:00
'public_membership',
'website_url',
2025-10-22 17:09:48 +02:00
];
public static function getAttributeLabel(string $attribute): string
{
return __("members.fields.$attribute");
}
public function getFullNameAttribute(): string
{
return "{$this->firstname} {$this->lastname}";
}
public function getRetzienEmailAttribute(): ?string
{
$emails = explode(';', $this->email);
return collect($emails)->filter(fn ($email) => str_contains($email, '@retzien.fr'))->first();
}
2025-10-22 17:09:48 +02:00
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function group(): BelongsTo
{
return $this->belongsTo(MemberGroup::class);
}
public function memberships(): HasMany
{
return $this->hasMany(Membership::class);
}
public function ispconfigs(): HasMany
{
return $this->hasMany(IspconfigMember::class, 'member_id');
}
2026-02-03 10:53:23 +01:00
public function nextcloudAccounts(): HasMany
{
2026-02-03 10:53:23 +01:00
return $this->hasMany(NextCloudMember::class, 'member_id');
}
2026-02-03 10:53:23 +01:00
public function lastMembership(): Membership
{
2026-02-03 10:53:23 +01:00
return $this->memberships()->where('status', 'active')->first();
}
2026-02-03 10:53:23 +01:00
public function hasService(string $serviceIdentifier): bool
{
2026-02-03 10:53:23 +01:00
$membership = $this->lastMembership();
2026-02-03 10:53:23 +01:00
return $membership->services()->where('identifier', $serviceIdentifier)->exists();
}
2026-02-03 10:53:23 +01:00
2026-02-08 21:59:16 +01:00
public function isExpired(): bool
{
// Member ayant leur dernière adhésion non renouvellée de puis plus d'un mois
$lastMembership = $this->lastMembership();
2026-02-08 21:59:16 +01:00
return $lastMembership->status === 'expired' || $lastMembership->created_at->addMonths(1) < now();
}
2025-10-22 17:09:48 +02:00
}