Commit 6f2307c5 authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix employee delete: remove broken DI param, add cascade logic

The delete method had an unused EmployeeService parameter that could
cause Livewire method resolution issues. Removed it, and added proper
cascading: deactivate trainer record, unlink+soft-delete user account,
then soft-delete the employee — all in one transaction.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent db04d624
......@@ -5,8 +5,8 @@
use App\Domain\HR\Enums\EmployeeStatus;
use App\Domain\HR\Enums\EmploymentType;
use App\Domain\HR\Models\Employee;
use App\Domain\HR\Services\EmployeeService;
use App\Domain\Identity\Services\PermissionService;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Attributes\Url;
......@@ -51,12 +51,33 @@ public function updatedBranchId(): void
$this->resetPage();
}
public function delete(string $uuid, EmployeeService $service): void
public function delete(string $uuid): void
{
$this->authorize('employees.delete');
$employee = Employee::where('uuid', $uuid)->firstOrFail();
$employee->delete();
DB::transaction(function () use ($employee) {
// 1. Deactivate trainer record if exists
if ($employee->trainer) {
$employee->trainer->update(['status' => 'inactive']);
}
// 2. Soft-delete linked user account if exists
if ($employee->user_id) {
$user = \App\Models\User::find($employee->user_id);
if ($user) {
if ($user->person_id) {
\App\Domain\Identity\Models\Person::where('id', $user->person_id)
->update(['user_id' => null]);
}
$user->delete();
}
}
// 3. Soft-delete the employee
$employee->delete();
});
session()->flash('success', __('تم حذف الموظف بنجاح'));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment