netgescon-day0/app/Http/Controllers/Admin/QuickImportController.php

85 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Models\GesconImportMapping;
use App\Services\QuickImport\QuickStabiliImporter;
use App\Services\GesconImport\EssentialImportService;
use Illuminate\Support\Facades\Auth;
class QuickImportController extends Controller
{
public function stabile(Request $request)
{
$validated = $request->validate([
'legacy_code' => 'required|string'
]);
$legacy = $validated['legacy_code'];
$mapping = GesconImportMapping::where('user_id', $request->user()->id)
->where(function ($q) use ($legacy) {
$q->where('legacy_code', $legacy)->orWhere('legacy_code', '*');
})
->orderByDesc('legacy_code')
->first();
$importer = new QuickStabiliImporter((string)($request->user()->tenant_id ?? 'default'));
$result = $importer->import($legacy, $mapping?->meta ?? null);
// Se richiesta estesa (full=1) e import base ok, lancia pipeline essential (gestioni + banche + enrichment)
if ($result['ok'] && $request->boolean('full')) {
try {
$amm = Auth::user()->amministratore; // assumiamo esista per l'utente admin
if ($amm) {
$service = new EssentialImportService();
$steps = $service->runAll($amm, $legacy, ['path' => $request->input('path')]);
$result['full_import'] = true;
$result['steps'] = $steps;
} else {
$result['full_import'] = false;
$result['warning'] = 'Amministratore non associato, impossibile eseguire full import';
}
} catch (\Throwable $e) {
$result['full_import'] = false;
$result['error_full'] = $e->getMessage();
}
}
return \response()->json($result, $result['ok'] ? 200 : 422);
}
/**
* HTTP sync endpoint: esegue sincronizzazione hash-based per uno stabile legacy.
* Parametri accettati: legacy_code (req), with_gestioni (bool), with_banche (bool), path (opt), dry (bool)
*/
public function syncStabile(Request $request)
{
$data = $request->validate([
'legacy_code' => 'required|string',
'with_gestioni' => 'sometimes|boolean',
'with_banche' => 'sometimes|boolean',
'path' => 'sometimes|string|nullable',
'dry' => 'sometimes|boolean',
]);
$amm = Auth::user()->amministratore;
if (!$amm) {
return new JsonResponse(['ok' => false, 'error' => 'Amministratore non associato all\'utente corrente'], 422);
}
$svc = new EssentialImportService();
try {
$res = $svc->syncStabile($amm, (string)$data['legacy_code'], [
'with_gestioni' => (bool)($data['with_gestioni'] ?? false),
'with_banche' => (bool)($data['with_banche'] ?? false),
'path' => $data['path'] ?? null,
'dry' => (bool)($data['dry'] ?? false),
]);
return new JsonResponse(['ok' => true] + $res, 200);
} catch (\Throwable $e) {
return new JsonResponse(['ok' => false, 'error' => $e->getMessage()], 500);
}
}
}