45 lines
1.6 KiB
PHP
Executable File
45 lines
1.6 KiB
PHP
Executable File
<?php
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\Contabilita\IncassiHubReadService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class IncassiHubController extends Controller
|
|
{
|
|
public function index(Request $request, IncassiHubReadService $service): JsonResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'stabile_id' => ['nullable', 'integer'],
|
|
'codice_stabile' => ['nullable', 'string', 'max:20'],
|
|
'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
|
|
]);
|
|
|
|
$stabileId = isset($validated['stabile_id']) ? (int) $validated['stabile_id'] : null;
|
|
if (! $stabileId && ! empty($validated['codice_stabile']) && Schema::hasTable('stabili')) {
|
|
$codice = trim((string) $validated['codice_stabile']);
|
|
$trimmed = ltrim($codice, '0');
|
|
|
|
$stabileId = DB::table('stabili')
|
|
->where(function ($query) use ($codice, $trimmed): void {
|
|
$query->where('codice_stabile', $codice);
|
|
if ($trimmed !== '' && $trimmed !== $codice) {
|
|
$query->orWhere('codice_stabile', $trimmed);
|
|
}
|
|
})
|
|
->value('id');
|
|
}
|
|
|
|
if (! $stabileId) {
|
|
return response()->json([
|
|
'message' => 'Specificare stabile_id o codice_stabile valido.',
|
|
], 422);
|
|
}
|
|
|
|
return response()->json($service->forStabile((int) $stabileId, (int) ($validated['limit'] ?? 20)));
|
|
}
|
|
}
|