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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user