migrant-web/app/Http/Controllers/Employer/WorkerController.php
2026-05-21 10:39:23 +05:30

155 lines
5.4 KiB
PHP

<?php
namespace App\Http\Controllers\Employer;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Models\User;
use App\Models\Worker;
use App\Models\WorkerCategory;
use App\Models\Shortlist;
use App\Models\JobOffer;
class WorkerController extends Controller
{
private function resolveCurrentUser()
{
$sess = session('user');
$sessId = is_array($sess) ? ($sess['id'] ?? null) : ($sess->id ?? null);
if (!$sessId) {
$user = User::where('role', 'employer')->first();
if ($user) {
session(['user' => (object)[
'id' => $user->id,
'name' => $user->name,
'email' => $user->email,
'role' => 'employer',
'subscription_status' => $user->subscription_status ?? 'active',
]]);
return $user;
}
} else {
return User::find($sessId);
}
return null;
}
public function index(Request $request)
{
$user = $this->resolveCurrentUser();
$employerId = $user ? $user->id : 2;
// Fetch all non-deleted workers to show their status changes (active vs Hired)
$dbWorkers = Worker::with(['category', 'skills'])->get();
$workers = $dbWorkers->map(function ($w) {
return [
'id' => $w->id,
'name' => $w->name,
'nationality' => $w->nationality,
'category' => $w->category ? $w->category->name : 'General Helper',
'skills' => $w->skills->pluck('name')->toArray(),
'availability' => $w->availability,
'experience' => $w->experience,
'salary' => (int)$w->salary,
'religion' => $w->religion,
'languages' => ['English', 'Arabic'],
'age' => $w->age,
'verified' => (bool)$w->verified,
'status' => $w->status, // Map worker status dynamically (e.g. active, Hired)
'bio' => $w->bio,
];
})->toArray();
// Get saved shortlist for current employer
$shortlistedIds = Shortlist::where('employer_id', $employerId)->pluck('worker_id')->toArray();
// Dynamically build filter metadata from DB values
$dbCategories = WorkerCategory::pluck('name')->toArray();
$dbNationalities = Worker::distinct()->pluck('nationality')->toArray();
$filtersMetadata = [
'categories' => array_merge(['All Categories'], $dbCategories),
'nationalities' => array_merge(['All Nationalities'], $dbNationalities),
'availabilities' => ['All Availabilities', 'Immediate', '1 Week', '2 Weeks', '1 Month'],
'experienceLevels' => ['All Experience', '1-2 Years', '3-5 Years', '5+ Years'],
'religions' => ['All Religions', 'Christian', 'Muslim', 'Hindu', 'Buddhist'],
];
return Inertia::render('Employer/Workers/Index', [
'initialWorkers' => $workers,
'initialShortlistedIds' => $shortlistedIds,
'filtersMetadata' => $filtersMetadata,
]);
}
public function show($id)
{
$w = Worker::with(['category', 'skills', 'documents'])->findOrFail($id);
$worker = [
'id' => $w->id,
'name' => $w->name,
'nationality' => $w->nationality,
'category' => $w->category ? $w->category->name : 'General Helper',
'skills' => $w->skills->pluck('name')->toArray(),
'availability' => $w->availability,
'experience' => $w->experience,
'experience_years' => 5,
'salary' => (int)$w->salary,
'religion' => $w->religion,
'languages' => ['English', 'Arabic'],
'age' => $w->age,
'verified' => (bool)$w->verified,
'status' => $w->status,
'bio' => $w->bio,
'passport_status' => 'OCR Verified',
'visa_status' => 'Transferable',
];
return Inertia::render('Employer/Workers/Show', [
'worker' => $worker,
]);
}
/**
* Submit a secure hiring offer from the Employer Web Portal.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\RedirectResponse
*/
public function sendOffer(Request $request, $id)
{
$request->validate([
'work_date' => 'required|date',
'location' => 'required|string|max:255',
'salary' => 'required|numeric|min:0',
'notes' => 'nullable|string|max:1000',
]);
$user = $this->resolveCurrentUser();
$employerId = $user ? $user->id : 2;
// Verify worker exists
$worker = Worker::findOrFail($id);
// Create job offer record
JobOffer::create([
'employer_id' => $employerId,
'worker_id' => $worker->id,
'work_date' => $request->work_date,
'location' => $request->location,
'salary' => $request->salary,
'notes' => $request->notes,
'status' => 'pending',
]);
return redirect()->route('employer.hiring.success', ['id' => $id])
->with('success', 'Hiring offer has been successfully dispatched to the worker!');
}
}