Initial commit: Horux Strategy Platform
- Laravel 11 backend with API REST - React 18 + TypeScript + Vite frontend - Multi-parser architecture for accounting systems (CONTPAQi, Aspel, SAP) - 27+ financial metrics calculation - PDF report generation with Browsershot - Complete documentation (10 documents) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
99
backend/app/Http/Controllers/ClienteController.php
Normal file
99
backend/app/Http/Controllers/ClienteController.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Cliente;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ClienteController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
if ($user->isAdmin() || $user->isAnalista()) {
|
||||
$clientes = Cliente::with('giro')->get();
|
||||
} else {
|
||||
$clientes = Cliente::where('id', $user->cliente_id)->with('giro')->get();
|
||||
}
|
||||
|
||||
return response()->json($clientes);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'nombre_empresa' => 'required|string|max:255',
|
||||
'giro_id' => 'required|exists:giros,id',
|
||||
'moneda' => 'string|max:3',
|
||||
'logo' => 'nullable|image|max:2048',
|
||||
]);
|
||||
|
||||
$cliente = new Cliente([
|
||||
'nombre_empresa' => $validated['nombre_empresa'],
|
||||
'giro_id' => $validated['giro_id'],
|
||||
'moneda' => $validated['moneda'] ?? 'MXN',
|
||||
]);
|
||||
|
||||
if ($request->hasFile('logo')) {
|
||||
$path = $request->file('logo')->store('logos', 'public');
|
||||
$cliente->logo = $path;
|
||||
}
|
||||
|
||||
$cliente->save();
|
||||
|
||||
return response()->json($cliente->load('giro'), 201);
|
||||
}
|
||||
|
||||
public function show(Request $request, Cliente $cliente): JsonResponse
|
||||
{
|
||||
if (!$request->user()->canAccessCliente($cliente->id)) {
|
||||
return response()->json(['message' => 'No autorizado'], 403);
|
||||
}
|
||||
|
||||
return response()->json($cliente->load(['giro', 'balanzas', 'reportes']));
|
||||
}
|
||||
|
||||
public function update(Request $request, Cliente $cliente): JsonResponse
|
||||
{
|
||||
if (!$request->user()->canAccessCliente($cliente->id)) {
|
||||
return response()->json(['message' => 'No autorizado'], 403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'nombre_empresa' => 'string|max:255',
|
||||
'giro_id' => 'exists:giros,id',
|
||||
'moneda' => 'string|max:3',
|
||||
'logo' => 'nullable|image|max:2048',
|
||||
'configuracion' => 'nullable|array',
|
||||
]);
|
||||
|
||||
if ($request->hasFile('logo')) {
|
||||
if ($cliente->logo) {
|
||||
Storage::disk('public')->delete($cliente->logo);
|
||||
}
|
||||
$validated['logo'] = $request->file('logo')->store('logos', 'public');
|
||||
}
|
||||
|
||||
$cliente->update($validated);
|
||||
|
||||
return response()->json($cliente->load('giro'));
|
||||
}
|
||||
|
||||
public function destroy(Request $request, Cliente $cliente): JsonResponse
|
||||
{
|
||||
if (!$request->user()->isAdmin()) {
|
||||
return response()->json(['message' => 'No autorizado'], 403);
|
||||
}
|
||||
|
||||
if ($cliente->logo) {
|
||||
Storage::disk('public')->delete($cliente->logo);
|
||||
}
|
||||
|
||||
$cliente->delete();
|
||||
|
||||
return response()->json(['message' => 'Cliente eliminado']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user