ldc-portsalaries/app/Models/Employee.php
2021-06-07 18:28:25 +02:00

277 lines
7.8 KiB
PHP

<?php
namespace App\Models;
use App\Enums\CrudAction;
use App\Enums\CrudType;
use App\Traits\IsCrudModel;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection as SupportCollection;
class Employee extends Authenticatable
{
use HasFactory;
use IsCrudModel;
protected $crudLabelColumn = 'email';
protected $crudValueColumn = 'id';
protected $fillable = [
'matricule',
'firstname',
'lastname',
'email',
'site_id'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'password_date' => 'datetime',
];
protected $attributes = [
'password_attempts' => 0
];
public function __construct($attributes = [])
{
self::$editableProperties = [
'matricule' => [
'label' => trans('Matricule'),
'type' => CrudType::string(),
'readonly' => true,
'validate' => [
'string',
'min:10',
'max:50'
],
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'firstname' => [
'label' => trans('Prénom'),
'type' => CrudType::string(),
'validate' => [
'string',
'max:60'
],
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'lastname' => [
'label' => trans('Nom'),
'type' => CrudType::string(),
'nullable' => false,
'validate' => [
'string',
'max:60'
],
'rules' => [
'min' => 5,
'max' => 10,
'step' => 2
],
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'email' => [
'label' => trans('Email'),
'type' => CrudType::email(),
'nullable' => false,
'validate' => [
'max:150'
],
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'password' => [
'label' => trans('Mot de passe'),
'placeholder' => '#M1s3Cure.P4ss0rd!',
'type' => CrudType::password(),
'nullable' => false,
'validate' => [
'max:150'
],
'actions' => [
CrudAction::create(),
CrudAction::delete()
]
],
'site_id' => [
'label' => trans('Site'),
'type' => CrudType::belongsTo(),
'belongsTo' => Site::class,
'actions' => [
CrudAction::create(),
CrudAction::update()
]
],
'blocked' => [
'label' => trans('Verrouillé'),
'type' => CrudType::boolean(),
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::update(),
],
'getAttribute' => function () {
return $this->getAttribute('password_attempts') >= 3;
},
'setAttribute' => function ($value) {
$this->setAttribute('password_attempts', $value ? 3 : 0);
return $this->getAttribute('password_attempts');
},
]
];
parent::__construct($attributes);
}
protected static function booted()
{
static::creating(function ($employee) {
$employee->password = Hash::make($employee->password);
$employee->password_date = Carbon::now()->subDay();
});
static::updating(function ($employee) {
if (Hash::needsRehash($employee->password)) {
$employee->password = Hash::make($employee->password);
}
});
}
// RELATIONS
/**
* Has one Site
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function site()
{
return $this->belongsTo(Site::class);
}
/**
* Has many Planning
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function plannings()
{
return $this->hasMany(Planning::class);
}
/**
* Has one EmployeeMailToken
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function token()
{
return $this->hasOne(EmployeeMailToken::class);
}
/**
* BelongsToMany
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function reponseSondages()
{
return $this->belongsToMany(ReponseSondage::class)->withTimestamps();
}
// METHODS
/**
* Has the user answered to a specific sondage ?
* @param Sondage $sondage
* @return bool
*/
public function hasAnweredToSondage(Sondage $sondage): bool
{
$responsesIDS = $sondage->reponseSondages()->pluck(sprintf('%s.id', (new ReponseSondage())->getTable()));
return (bool)$this->reponseSondages()->wherePivotIn('reponse_sondage_id', $responsesIDS)->count();
}
/**
* Get Employee site flashes
*
* @return Collection
*/
public function getFlashes(): Collection
{
return $this->site->flashes()->orderBy('created_at', 'DESC')->get();
}
/**
* Get Employee unanswered Sondages
*
* @return Collection
*/
public function unAnsweredSondages(): Collection
{
$siteId = $this->site_id;
$sondagesIDS = $this->reponseSondages()->with('sondage')->get()->pluck('sondage.id');
return Sondage::whereHas('site', function ($q) use ($siteId) {
$q->whereId($siteId);
})->whereNotIn('id', $sondagesIDS)
->orderBy('created_at', 'DESC')
->get();
}
/**
* Prevent Infinite loop on mail send to Employee
* {@inheritdoc}
* @url https://stackoverflow.com/questions/52514863/laravel-getqueueablerelations-circular-relation
*/
public function getQueueableRelations()
{
return [];
}
/**
* Get Plannings for the current week + next week
*
* @return \Illuminate\Support\Collection
*/
public function getWeekAndNextPlannings(): SupportCollection
{
$now = Carbon::now()->format('Y/m/d');
$nowPlus7 = Carbon::now()->addDays(7)->format('Y/m/d');
return collect([
'now' => Planning::whereDate('start', '<', $now)
->whereDate('end', '>', $now)
->where('employee_id', $this->id)
->first(),
'next' => Planning::whereDate('start', '<', $nowPlus7)
->whereDate('end', '>', $nowPlus7)
->where('employee_id', $this->id)
->first()
]);
}
}