feat: L'Ami Fiduciaire V1.0.0 — full codebase with Story 0.1 complete
Initial commit of the L'Ami Fiduciaire SaaS platform built on Laravel 12, Vue 3, Inertia.js 2, and Tailwind CSS 4. Story 0.1 (rename folders to declarations in database) is implemented and code-reviewed: migration, rollback, and 6 Pest tests all passing. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
293
app/Http/Controllers/ClientController.php
Normal file
293
app/Http/Controllers/ClientController.php
Normal file
@@ -0,0 +1,293 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\ClientStatus;
|
||||
use App\Enums\LegalForm;
|
||||
use App\Http\Requests\StoreClientRequest;
|
||||
use App\Http\Requests\UpdateClientRequest;
|
||||
use App\Models\Client;
|
||||
use App\Models\Workspace;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class ClientController extends Controller
|
||||
{
|
||||
protected function legalFormLabels(): array
|
||||
{
|
||||
$labels = [
|
||||
'sarl' => 'SARL',
|
||||
'sa' => 'SA',
|
||||
'snc' => 'SNC',
|
||||
'scs' => 'SCS',
|
||||
'eurl' => 'EURL',
|
||||
'sel' => 'SEL',
|
||||
'auto_entrepreneur' => 'Auto-entrepreneur',
|
||||
'entreprise_individuelle' => 'Entreprise individuelle',
|
||||
'other' => 'Autre',
|
||||
];
|
||||
|
||||
return array_intersect_key($labels, array_flip(LegalForm::getValues()));
|
||||
}
|
||||
|
||||
protected function clientStatusLabels(): array
|
||||
{
|
||||
return [
|
||||
ClientStatus::Active => 'Actif',
|
||||
ClientStatus::Inactive => 'Inactif',
|
||||
ClientStatus::Suspended => 'Suspendu',
|
||||
];
|
||||
}
|
||||
|
||||
protected function currentWorkspace(Request $request): Workspace
|
||||
{
|
||||
$workspaceId = $request->session()->get('current_workspace_id');
|
||||
|
||||
return Workspace::query()->findOrFail($workspaceId);
|
||||
}
|
||||
|
||||
protected function serializeContacts(Client $client): array
|
||||
{
|
||||
return $client->contacts->map(fn ($c) => [
|
||||
'id' => $c->id,
|
||||
'full_name' => $c->full_name,
|
||||
'job_title' => $c->job_title,
|
||||
'email' => $c->email,
|
||||
'phone' => $c->phone,
|
||||
'is_principal' => $c->is_principal,
|
||||
])->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the clients.
|
||||
*/
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$workspace = $this->currentWorkspace($request);
|
||||
|
||||
$perPage = min(max((int) $request->input('per_page', 10), 10), 100);
|
||||
|
||||
$clients = $workspace->clients()
|
||||
->latest()
|
||||
->paginate($perPage)
|
||||
->through(fn (Client $client) => [
|
||||
'id' => $client->id,
|
||||
'company_name' => $client->company_name,
|
||||
'legal_form' => $client->legal_form->value,
|
||||
'ice' => $client->ice,
|
||||
'status' => $client->status?->value,
|
||||
'showUrl' => route('clients.show', $client),
|
||||
'editUrl' => route('clients.edit', $client),
|
||||
'destroyUrl' => route('clients.destroy', $client),
|
||||
]);
|
||||
|
||||
return Inertia::render('clients/Index', [
|
||||
'clients' => $clients,
|
||||
'createUrl' => route('clients.create'),
|
||||
'workspaceName' => $workspace->name,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new client.
|
||||
*/
|
||||
public function create(Request $request): Response
|
||||
{
|
||||
$workspace = $this->currentWorkspace($request);
|
||||
|
||||
return Inertia::render('clients/Create', [
|
||||
'indexUrl' => route('clients.index'),
|
||||
'storeUrl' => route('clients.store'),
|
||||
'legalForms' => $this->legalFormLabels(),
|
||||
'clientStatusLabels' => $this->clientStatusLabels(),
|
||||
'workspaceUsers' => $workspace->users()->orderBy('users.name')->select('users.id', 'users.name', 'users.email')->get()->map(fn ($u) => [
|
||||
'id' => $u->id,
|
||||
'name' => $u->name,
|
||||
'email' => $u->email,
|
||||
])->values()->all(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created client in storage.
|
||||
*/
|
||||
public function store(StoreClientRequest $request): RedirectResponse
|
||||
{
|
||||
$workspace = $this->currentWorkspace($request);
|
||||
$data = $request->validated();
|
||||
$contacts = $data['contacts'];
|
||||
unset($data['contacts']);
|
||||
$data['workspace_id'] = $workspace->id;
|
||||
|
||||
$client = Client::query()->create($data);
|
||||
|
||||
foreach ($contacts as $contact) {
|
||||
$client->contacts()->create($contact);
|
||||
}
|
||||
|
||||
return to_route('clients.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified client.
|
||||
*/
|
||||
public function show(Request $request, Client $client): Response
|
||||
{
|
||||
$workspace = $this->currentWorkspace($request);
|
||||
$this->authorizeClient($workspace, $client);
|
||||
|
||||
$client->load(['internalResponsible', 'contacts']);
|
||||
|
||||
$folders = $client->folders()
|
||||
->with(['assignee'])
|
||||
->latest()
|
||||
->limit(50)
|
||||
->get()
|
||||
->map(fn ($f) => [
|
||||
'id' => $f->id,
|
||||
'title' => $f->title,
|
||||
'type' => $f->type->value,
|
||||
'status' => $f->status->value,
|
||||
'due_date' => $f->due_date?->format('Y-m-d'),
|
||||
'created_at' => $f->created_at->format('Y-m-d'),
|
||||
'showUrl' => route('folders.show', $f),
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$allFolders = $client->folders()->get();
|
||||
$stats = [
|
||||
'total' => $allFolders->count(),
|
||||
'by_status' => $allFolders->groupBy(fn ($f) => $f->status->value)
|
||||
->map->count()
|
||||
->all(),
|
||||
'by_type' => $allFolders->groupBy(fn ($f) => $f->type->value)
|
||||
->map->count()
|
||||
->all(),
|
||||
];
|
||||
|
||||
return Inertia::render('clients/Show', [
|
||||
'client' => [
|
||||
'id' => $client->id,
|
||||
'company_name' => $client->company_name,
|
||||
'legal_form' => $client->legal_form->value,
|
||||
'ice' => $client->ice,
|
||||
'fiscal_id' => $client->fiscal_id,
|
||||
'rc' => $client->rc,
|
||||
'cnss' => $client->cnss,
|
||||
'patente' => $client->patente,
|
||||
'contacts' => $this->serializeContacts($client),
|
||||
'internal_responsible_id' => $client->internal_responsible_id,
|
||||
'internal_responsible_name' => $client->internalResponsible?->name,
|
||||
'status' => $client->status?->value,
|
||||
'internal_notes' => $client->internal_notes,
|
||||
],
|
||||
'folders' => $folders,
|
||||
'stats' => $stats,
|
||||
'indexUrl' => route('clients.index'),
|
||||
'editUrl' => route('clients.edit', $client),
|
||||
'createFolderUrl' => route('folders.create', ['client_id' => $client->id]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified client.
|
||||
*/
|
||||
public function edit(Request $request, Client $client): Response
|
||||
{
|
||||
$workspace = $this->currentWorkspace($request);
|
||||
$this->authorizeClient($workspace, $client);
|
||||
|
||||
$client->load('contacts');
|
||||
|
||||
return Inertia::render('clients/Edit', [
|
||||
'client' => [
|
||||
'id' => $client->id,
|
||||
'company_name' => $client->company_name,
|
||||
'legal_form' => $client->legal_form->value,
|
||||
'ice' => $client->ice,
|
||||
'fiscal_id' => $client->fiscal_id,
|
||||
'rc' => $client->rc,
|
||||
'cnss' => $client->cnss,
|
||||
'patente' => $client->patente,
|
||||
'contacts' => $this->serializeContacts($client),
|
||||
'internal_responsible_id' => $client->internal_responsible_id,
|
||||
'status' => $client->status?->value,
|
||||
'internal_notes' => $client->internal_notes,
|
||||
],
|
||||
'indexUrl' => route('clients.index'),
|
||||
'updateUrl' => route('clients.update', $client),
|
||||
'legalForms' => $this->legalFormLabels(),
|
||||
'clientStatusLabels' => $this->clientStatusLabels(),
|
||||
'workspaceUsers' => $workspace->users()->orderBy('users.name')->select('users.id', 'users.name', 'users.email')->get()->map(fn ($u) => [
|
||||
'id' => $u->id,
|
||||
'name' => $u->name,
|
||||
'email' => $u->email,
|
||||
])->values()->all(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified client in storage.
|
||||
*/
|
||||
public function update(UpdateClientRequest $request, Client $client): RedirectResponse
|
||||
{
|
||||
$workspace = $this->currentWorkspace($request);
|
||||
$this->authorizeClient($workspace, $client);
|
||||
|
||||
$data = $request->validated();
|
||||
$contacts = $data['contacts'];
|
||||
unset($data['contacts']);
|
||||
|
||||
DB::transaction(function () use ($client, $data, $contacts) {
|
||||
$client->update($data);
|
||||
|
||||
$submittedIds = collect($contacts)
|
||||
->pluck('id')
|
||||
->filter()
|
||||
->all();
|
||||
|
||||
$client->contacts()
|
||||
->whereNotIn('id', $submittedIds)
|
||||
->get()
|
||||
->each
|
||||
->delete();
|
||||
|
||||
foreach ($contacts as $contactData) {
|
||||
if (! empty($contactData['id'])) {
|
||||
$client->contacts()
|
||||
->where('id', $contactData['id'])
|
||||
->first()
|
||||
?->update($contactData);
|
||||
} else {
|
||||
$client->contacts()->create($contactData);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return to_route('clients.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified client from storage.
|
||||
*/
|
||||
public function destroy(Request $request, Client $client): RedirectResponse
|
||||
{
|
||||
$workspace = $this->currentWorkspace($request);
|
||||
$this->authorizeClient($workspace, $client);
|
||||
|
||||
$client->delete();
|
||||
|
||||
return to_route('clients.index');
|
||||
}
|
||||
|
||||
protected function authorizeClient(Workspace $workspace, Client $client): void
|
||||
{
|
||||
if ($client->workspace_id !== $workspace->id) {
|
||||
abort(404);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user