Add HR resource labels and helper methods for various resources; update locale handling in user model and department factory

This commit is contained in:
Mekan1206
2026-07-31 23:40:35 +05:00
parent 581cb8241c
commit 20d032c4cc
83 changed files with 4386 additions and 39 deletions

View File

@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
class Department extends Model
{
@@ -30,6 +31,38 @@ class Department extends Model
];
}
protected static function booted(): void
{
static::saving(function (Department $department): void {
if (blank($department->code) && filled($department->name)) {
$department->code = static::generateUniqueCodeFromName(
$department->name,
$department->id,
);
}
});
}
public static function generateUniqueCodeFromName(string $name, ?int $ignoreId = null): string
{
$base = Str::upper(Str::slug($name));
$query = static::query()
->when($ignoreId, fn ($query) => $query->where('id', '!=', $ignoreId));
if (! $query->clone()->where('code', $base)->exists()) {
return $base;
}
$maxSuffix = $query->clone()
->where('code', 'like', "{$base}-%")
->pluck('code')
->map(fn (string $code): int => (int) Str::after($code, "{$base}-"))
->max();
return "{$base}-".(($maxSuffix ?? 0) + 1);
}
public function employees(): HasMany
{
return $this->hasMany(Employee::class);