138 lines
3.6 KiB
PHP
138 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\SalarieSite;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use App\Models\Customers;
|
|
|
|
class Salarie extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'matricule',
|
|
'nom',
|
|
'prenom',
|
|
'code',
|
|
'start_date',
|
|
'end_date',
|
|
'etablissement',
|
|
'site',
|
|
'service'
|
|
];
|
|
|
|
/**
|
|
* Sets the fixed collection point for a salarie.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function setMagasin()
|
|
{
|
|
switch ($this->site) {
|
|
case SalarieSite::ldccav():
|
|
// Cavol.
|
|
$magasin = Magasin::find(3);
|
|
break;
|
|
case SalarieSite::dde():
|
|
// Dinde.
|
|
$magasin = Magasin::find(8);
|
|
break;
|
|
case SalarieSite::dpe():
|
|
// DPE.
|
|
$magasin = Magasin::find(9);
|
|
break;
|
|
case SalarieSite::cepacpe():
|
|
if (in_array($this->service, ["CEPACP060", "CEPACP070", "CEPACP072"])) {
|
|
// CPE.
|
|
$magasin = Magasin::find(6);
|
|
} elseif (in_array($this->service, ["CEPACP065", "CEPACP066"])) {
|
|
// PLB.
|
|
$magasin = Magasin::find(11);
|
|
} else {
|
|
// CEPA.
|
|
$magasin = Magasin::find(10);
|
|
}
|
|
break;
|
|
case SalarieSite::ldcplb():
|
|
// PLB.
|
|
$magasin = Magasin::find(11);
|
|
break;
|
|
case SalarieSite::tpwiss():
|
|
// Wissous.
|
|
$magasin = Magasin::find(7);
|
|
break;
|
|
case SalarieSite::sl():
|
|
case SalarieSite::gdfroi():
|
|
case SalarieSite::slela():
|
|
default:
|
|
// Saint Laurent.
|
|
$magasin = Magasin::find(1);
|
|
} //end switch
|
|
|
|
$this->magasin()->associate($magasin);
|
|
}
|
|
|
|
/**
|
|
* Simple Custom Random string generator
|
|
*
|
|
* @param integer $length
|
|
* @param object|null $model
|
|
* @param string $characters
|
|
* @return string
|
|
*/
|
|
public static function generateRandomString(
|
|
int $length = 10,
|
|
object $model = null,
|
|
string $characters = 'abcdefghjkmnpqrstuwxyzABCDEFGHJKMNPQRSTUWXYZ123456789'
|
|
) {
|
|
$charactersLength = strlen($characters);
|
|
$randomString = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
}
|
|
if (isset($model) && $model->where('code', $randomString)->count()) {
|
|
self::generateRandomString();
|
|
}
|
|
return $randomString;
|
|
}
|
|
|
|
/**
|
|
* getFormattedMatricule method
|
|
*
|
|
* @param string $matricule
|
|
* @return string Digits only without leading zeros.
|
|
*/
|
|
public static function getFormattedMatricule(string $matricule): string
|
|
{
|
|
return str_pad(ltrim(preg_replace('/[^\d]/', '', $matricule), '0'), 7, "0", STR_PAD_LEFT);
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* BelongsTo customer relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function customer()
|
|
{
|
|
return $this->belongsTo(Customers::class);
|
|
}
|
|
|
|
/**
|
|
* belongsTo magasin relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\belongsTo
|
|
*/
|
|
public function magasin()
|
|
{
|
|
return $this->belongsTo(Magasin::class);
|
|
}
|
|
}
|