106 lines
3.8 KiB
PHP
106 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\ClientStatus;
|
|
use App\Models\Client;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class ClientController extends Controller
|
|
{
|
|
public function index(): View
|
|
{
|
|
$clients = Client::withCount(['invoices', 'events', 'contentBriefs'])
|
|
->latest()
|
|
->paginate(12);
|
|
|
|
return view('clients.index', compact('clients'));
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
$statuses = ClientStatus::cases();
|
|
return view('clients.create', compact('statuses'));
|
|
}
|
|
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'email' => 'nullable|email|unique:clients,email',
|
|
'phone' => 'nullable|string|max:50',
|
|
'company' => 'nullable|string|max:255',
|
|
'ig_handle' => 'nullable|string|max:100',
|
|
'tiktok_handle' => 'nullable|string|max:100',
|
|
'youtube_handle' => 'nullable|string|max:100',
|
|
'address_line1' => 'nullable|string|max:255',
|
|
'address_line2' => 'nullable|string|max:255',
|
|
'city' => 'nullable|string|max:100',
|
|
'state' => 'nullable|string|max:100',
|
|
'postal_code' => 'nullable|string|max:20',
|
|
'country' => 'nullable|string|max:100',
|
|
'status' => 'required|in:active,inactive,prospect',
|
|
'notes' => 'nullable|string',
|
|
]);
|
|
|
|
Client::create($validated);
|
|
|
|
return redirect()->route('clients.index')
|
|
->with('success', '🐱 Client added successfully!');
|
|
}
|
|
|
|
public function show(Client $client): View
|
|
{
|
|
$client->load(['events' => fn ($q) => $q->latest()->limit(5)]);
|
|
$client->load(['invoices' => fn ($q) => $q->latest()->limit(5)]);
|
|
$client->load(['contentBriefs' => fn ($q) => $q->latest()->limit(5)]);
|
|
$client->load(['mediaFiles' => fn ($q) => $q->latest()->limit(10)]);
|
|
|
|
return view('clients.show', compact('client'));
|
|
}
|
|
|
|
public function edit(Client $client): View
|
|
{
|
|
$statuses = ClientStatus::cases();
|
|
return view('clients.edit', compact('client', 'statuses'));
|
|
}
|
|
|
|
public function update(Request $request, Client $client): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'email' => 'nullable|email|unique:clients,email,' . $client->id,
|
|
'phone' => 'nullable|string|max:50',
|
|
'company' => 'nullable|string|max:255',
|
|
'ig_handle' => 'nullable|string|max:100',
|
|
'tiktok_handle' => 'nullable|string|max:100',
|
|
'youtube_handle' => 'nullable|string|max:100',
|
|
'address_line1' => 'nullable|string|max:255',
|
|
'address_line2' => 'nullable|string|max:255',
|
|
'city' => 'nullable|string|max:100',
|
|
'state' => 'nullable|string|max:100',
|
|
'postal_code' => 'nullable|string|max:20',
|
|
'country' => 'nullable|string|max:100',
|
|
'status' => 'required|in:active,inactive,prospect',
|
|
'notes' => 'nullable|string',
|
|
]);
|
|
|
|
$client->update($validated);
|
|
|
|
return redirect()->route('clients.show', $client)
|
|
->with('success', '🐱 Client updated successfully!');
|
|
}
|
|
|
|
public function destroy(Client $client): RedirectResponse
|
|
{
|
|
$client->delete();
|
|
|
|
return redirect()->route('clients.index')
|
|
->with('success', '🐱 Client removed.');
|
|
}
|
|
}
|