ldc-boutique-cnc-sable/app/Models/Employee.php
Jérémy Munsch 3b3997d547
feat(employees): automated sync with Arcadia API, BO settings, Shopper value objects, and pagination migration
- Implement automated synchronization of employees via Arcadia API (EmployeeSyncService, SynchronizeEmployeesWithArcadiaApi, FetchAndUpsertArcadiaEmployees)
- Add Back-Office application settings for employee sites (Setting singleton, GetSettingsQuery, UpdateSettings, SettingsController, EmployeeSitesInputComponent.vue)
- Strongly type Employee attributes using Domain Value Objects (ShopperEstablishmentCode, ShopperSiteCode, ShopperServiceCode) and Eloquent Casts
- Migrate pagination from Domain to Shared\Pagination namespace across queries and inputs
- Remove legacy and dead CSV employee import/export code
- Restore Domain\Ports\Services\CsvGenerator interface for Regate order generation
- Add comprehensive Pest unit, integration, and feature test coverage
2026-10-27 09:58:44 +01:00

112 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Casts\CodeCast;
use App\Casts\MatriculeCast;
use App\Casts\ShopperEstablishmentCodeCast;
use App\Casts\ShopperServiceCodeCast;
use App\Casts\ShopperSiteCodeCast;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
/**
* @property int $id
* @property string|null $customer_id
* @property \Domain\Models\ValueObjects\Matricule $matricule
* @property string $nom
* @property string $prenom
* @property \Illuminate\Support\Carbon|null $start_date
* @property \Illuminate\Support\Carbon|null $end_date
* @property \Domain\Models\ValueObjects\ShopperEstablishmentCode $etablissement
* @property \Domain\Models\ValueObjects\ShopperSiteCode $site
* @property \Domain\Models\ValueObjects\ShopperServiceCode $service
* @property \Domain\Models\ValueObjects\Code $code
* @property \Illuminate\Support\Carbon|null $last_sync
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Models\Customer|null $customer
* @property-read mixed $full_name
* @property-read bool $is_active
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
*
* @method static Builder<static>|Employee active()
*
* @mixin \Eloquent
*/
#[Fillable([
'matricule',
'nom',
'prenom',
'code',
'start_date',
'end_date',
'etablissement',
'site',
'service',
])]
class Employee extends Model
{
use Notifiable;
/** @return \Illuminate\Database\Eloquent\Casts\Attribute<string,never> */
protected function fullName(): Attribute
{
return Attribute::make(get: fn () => Str::title("{$this->nom} {$this->prenom}"));
}
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Customer,$this> */
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
#[\Override]
protected function casts(): array
{
return [
'code' => CodeCast::class,
'matricule' => MatriculeCast::class,
'last_sync' => 'datetime',
'start_date' => 'datetime',
'end_date' => 'datetime',
'etablissement' => ShopperEstablishmentCodeCast::class,
'site' => ShopperSiteCodeCast::class,
'service' => ShopperServiceCodeCast::class,
];
}
/** @return \Illuminate\Database\Eloquent\Casts\Attribute<bool,never> */
protected function isActive(): Attribute
{
return Attribute::make(
get: fn (): bool => $this->end_date?->greaterThanOrEqualTo(Date::now()) ?? true
);
}
/**
* @param \Illuminate\Database\Eloquent\Builder<static> $query
* @return \Illuminate\Database\Eloquent\Builder<static>
*/
protected function scopeActive(Builder $query): Builder
{
return $query->where(
fn ($q) => $q
->where('end_date', '>=', Date::now())
->orWhereNull('end_date')
);
}
public function routeNotificationForMail(): string
{
return $this->customer?->email->value ?? '';
}
}