133 lines
4.3 KiB
PHP
133 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\ConsultantRequest;
|
|
use App\Models\Consultant;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class ConsultantController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorize('viewAny', Consultant::class);
|
|
|
|
$query = Consultant::query()->withCount('dataCentres');
|
|
|
|
if ($search = $request->string('cari')->toString()) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('nama_perunding', 'like', "%$search%")
|
|
->orWhere('no_pendaftaran_syarikat', 'like', "%$search%")
|
|
->orWhere('emel', 'like', "%$search%");
|
|
});
|
|
}
|
|
|
|
if ($request->filled('status')) {
|
|
$query->where('aktif', $request->status === 'aktif');
|
|
}
|
|
|
|
$consultants = $query->orderBy('nama_perunding')->paginate(15)->withQueryString();
|
|
|
|
return view('consultants.index', compact('consultants'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->authorize('create', Consultant::class);
|
|
|
|
return view('consultants.create', ['consultant' => new Consultant]);
|
|
}
|
|
|
|
public function store(ConsultantRequest $request)
|
|
{
|
|
$consultant = DB::transaction(function () use ($request) {
|
|
$userId = null;
|
|
|
|
if ($request->boolean('buat_akaun')) {
|
|
$user = User::create([
|
|
'name' => $request->user_name,
|
|
'email' => $request->user_email,
|
|
'password' => Hash::make($request->user_password),
|
|
'is_active' => true,
|
|
'email_verified_at' => now(),
|
|
]);
|
|
$user->assignRole('Perunding');
|
|
$userId = $user->id;
|
|
}
|
|
|
|
return Consultant::create([
|
|
'user_id' => $userId,
|
|
'nama_perunding' => $request->nama_perunding,
|
|
'no_pendaftaran_syarikat' => $request->no_pendaftaran_syarikat,
|
|
'alamat' => $request->alamat,
|
|
'emel' => $request->emel,
|
|
'telefon' => $request->telefon,
|
|
'aktif' => $request->boolean('aktif'),
|
|
]);
|
|
});
|
|
|
|
return redirect()->route('consultants.show', $consultant)
|
|
->with('success', 'Perunding berjaya didaftarkan.');
|
|
}
|
|
|
|
public function show(Consultant $consultant)
|
|
{
|
|
$this->authorize('view', $consultant);
|
|
|
|
$consultant->load(['user', 'dataCentres', 'assignments.dataCentre', 'assignments.assignedBy']);
|
|
|
|
return view('consultants.show', compact('consultant'));
|
|
}
|
|
|
|
public function edit(Consultant $consultant)
|
|
{
|
|
$this->authorize('update', $consultant);
|
|
|
|
return view('consultants.edit', compact('consultant'));
|
|
}
|
|
|
|
public function update(ConsultantRequest $request, Consultant $consultant)
|
|
{
|
|
$this->authorize('update', $consultant);
|
|
|
|
DB::transaction(function () use ($request, $consultant) {
|
|
$consultant->update([
|
|
'nama_perunding' => $request->nama_perunding,
|
|
'no_pendaftaran_syarikat' => $request->no_pendaftaran_syarikat,
|
|
'alamat' => $request->alamat,
|
|
'emel' => $request->emel,
|
|
'telefon' => $request->telefon,
|
|
'aktif' => $request->boolean('aktif'),
|
|
]);
|
|
|
|
if ($request->boolean('buat_akaun') && ! $consultant->user_id) {
|
|
$user = User::create([
|
|
'name' => $request->user_name,
|
|
'email' => $request->user_email,
|
|
'password' => Hash::make($request->user_password),
|
|
'is_active' => true,
|
|
'email_verified_at' => now(),
|
|
]);
|
|
$user->assignRole('Perunding');
|
|
$consultant->update(['user_id' => $user->id]);
|
|
}
|
|
});
|
|
|
|
return redirect()->route('consultants.show', $consultant)
|
|
->with('success', 'Maklumat perunding dikemas kini.');
|
|
}
|
|
|
|
public function destroy(Consultant $consultant)
|
|
{
|
|
$this->authorize('delete', $consultant);
|
|
|
|
$consultant->delete();
|
|
|
|
return redirect()->route('consultants.index')
|
|
->with('success', 'Perunding telah dipadam.');
|
|
}
|
|
}
|