122 lines
3.2 KiB
PHP
122 lines
3.2 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'
|
|
];
|
|
|
|
/**
|
|
* Setter magasin for a salarie.
|
|
*
|
|
* @param \App\Models\Salarie $salarie
|
|
* @return void
|
|
*/
|
|
public static function setSalarieMagasin(Salarie $salarie)
|
|
{
|
|
Salarie::setMagasin($salarie);
|
|
}
|
|
|
|
/**
|
|
* Sets the fixed collection point for a salarie.
|
|
*
|
|
* @param \App\Models\Salarie $salarie
|
|
* @return void
|
|
*/
|
|
private function setMagasin(Salarie $salarie)
|
|
{
|
|
if ($salarie->site == SalarieSite::ldccav()) {
|
|
// CAVOL.
|
|
$magasin = Magasin::find(3);
|
|
} elseif ($salarie->site == SalarieSite::dde()) {
|
|
// Dinde.
|
|
$magasin = Magasin::find(8);
|
|
} elseif ($salarie->site == SalarieSite::dpe()) {
|
|
// DPE.
|
|
$magasin = Magasin::find(9);
|
|
} elseif ($salarie->site == SalarieSite::cepacpe()) {
|
|
if (
|
|
$salarie->service == "CEPACP060"
|
|
|| $salarie->service == "CEPACP070"
|
|
|| $salarie->service == "CEPACP072"
|
|
) {
|
|
// CPE.
|
|
$magasin = Magasin::find(6);
|
|
} elseif ($salarie->service == "CEPACP065" || $salarie->service == "CEPACP066") {
|
|
// PLB.
|
|
$magasin = Magasin::find(10);
|
|
} else {
|
|
// CEPA.
|
|
$magasin = Magasin::find(10);
|
|
}
|
|
} else {
|
|
// Saint Laurent.
|
|
$magasin = Magasin::find(1);
|
|
} //end if
|
|
$salarie->magasin()->associate($magasin);
|
|
$salarie->save();
|
|
}
|
|
|
|
/**
|
|
* 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)->exists()) {
|
|
self::generateRandomString();
|
|
}
|
|
return $randomString;
|
|
}
|
|
|
|
// * 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);
|
|
}
|
|
}
|