Refactor employee management by implementing automatic employee number generation, updating national ID handling, and enhancing localization for gender and nationality fields across various resources. Adjust form components and validation logic to improve user experience and data integrity.

This commit is contained in:
Mekan1206
2026-08-02 21:06:37 +05:00
parent 28cbef8eb5
commit eed46157cd
25 changed files with 1175 additions and 51 deletions

View File

@@ -48,6 +48,38 @@ class Employee extends Model
];
}
protected static function booted(): void
{
static::saving(function (Employee $employee): void {
if (blank($employee->employee_number)) {
$employee->employee_number = static::generateUniqueEmployeeNumber();
}
});
}
public static function generateUniqueEmployeeNumber(): string
{
$prefix = (string) config('hr.employee_number.prefix', 'EMP-');
$padding = (int) config('hr.employee_number.padding', 5);
$latestNumber = static::withTrashed()
->where('employee_number', 'like', $prefix.'%')
->pluck('employee_number')
->map(function (string $number) use ($prefix): ?int {
if (! preg_match('/^'.preg_quote($prefix, '/').'(\d+)$/', $number, $matches)) {
return null;
}
return (int) $matches[1];
})
->filter()
->max();
$next = ($latestNumber ?? 0) + 1;
return $prefix.str_pad((string) $next, $padding, '0', STR_PAD_LEFT);
}
public function department(): BelongsTo
{
return $this->belongsTo(Department::class);