114 lines
4.3 KiB
PHP
114 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class GesconImportController extends Controller
|
|
{
|
|
private array $qaViews = [
|
|
'vw_gescon_import_unita_proprieta' => 'Unità e Diritti Reali',
|
|
'vw_gescon_import_millesimi' => 'Tabelle Millesimali',
|
|
'vw_gescon_import_operazioni' => 'Operazioni Contabili',
|
|
'vw_gescon_import_incassi_rate' => 'Incassi vs Rate',
|
|
'vw_gescon_import_giroconti' => 'Giroconti',
|
|
'vw_gescon_import_detrazioni' => 'Detrazioni Fiscali',
|
|
'vw_gescon_import_addebiti_personali' => 'Addebiti Personalizzati',
|
|
];
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$viewsStatus = [];
|
|
foreach ($this->qaViews as $view => $label) {
|
|
$exists = $this->viewExists($view);
|
|
$count = $exists ? DB::table($view)->count() : 0;
|
|
$viewsStatus[] = compact('view', 'label', 'exists', 'count');
|
|
}
|
|
|
|
$output = session('gescon_import_output');
|
|
return view('gescon_import.index', [
|
|
'viewsStatus' => $viewsStatus,
|
|
'output' => $output,
|
|
]);
|
|
}
|
|
|
|
public function run(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'stabile' => ['nullable', 'string', 'max:20'],
|
|
'anno' => ['nullable', 'digits:4'],
|
|
'limit' => ['nullable', 'integer', 'min:1'],
|
|
'solo' => ['nullable', 'in:unita,soggetti,diritti,millesimi,voci,operazioni,rate,incassi,giroconti,straord,addebiti,detrazioni,tutto'],
|
|
'dry_run' => ['nullable', 'boolean'],
|
|
]);
|
|
|
|
$params = [];
|
|
if (!empty($data['stabile'])) $params['--stabile'] = $data['stabile'];
|
|
if (!empty($data['anno'])) $params['--anno'] = $data['anno'];
|
|
if (!empty($data['limit'])) $params['--limit'] = (string)$data['limit'];
|
|
if (!empty($data['solo'])) $params['--solo'] = $data['solo'];
|
|
if (!empty($data['dry_run'])) $params['--dry-run'] = true;
|
|
|
|
try {
|
|
$code = Artisan::call('gescon:import-full', $params);
|
|
// Aggiorna viste QA se non già fatte dal comando
|
|
try {
|
|
Artisan::call('migrate', ['--path' => 'database/migrations/2025_08_14_120000_create_gescon_domain_tables_and_views.php']);
|
|
} catch (\Throwable $e) {
|
|
}
|
|
$output = Artisan::output();
|
|
session()->flash('gescon_import_output', $output);
|
|
return redirect()->route('gescon.import.index')->with('status', $code === 0 ? 'Import completato' : 'Import con errori');
|
|
} catch (\Throwable $e) {
|
|
Log::error('Errore import Gescon: ' . $e->getMessage());
|
|
return redirect()->route('gescon.import.index')->with('error', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function view(Request $request, string $view)
|
|
{
|
|
if (!array_key_exists($view, $this->qaViews)) {
|
|
abort(404);
|
|
}
|
|
if (!$this->viewExists($view)) {
|
|
return redirect()->route('gescon.import.index')->with('error', 'Vista non disponibile: ' . $view);
|
|
}
|
|
|
|
$perPage = (int)($request->get('per_page', 50));
|
|
$rows = DB::table($view)->paginate($perPage)->appends($request->query());
|
|
|
|
return view('gescon_import.view', [
|
|
'viewName' => $view,
|
|
'label' => $this->qaViews[$view],
|
|
'rows' => $rows,
|
|
'columns' => $this->columnsOf($view),
|
|
]);
|
|
}
|
|
|
|
private function viewExists(string $name): bool
|
|
{
|
|
try {
|
|
$exists = DB::select("SELECT table_name FROM information_schema.VIEWS WHERE table_schema = DATABASE() AND table_name = ?", [$name]);
|
|
return !empty($exists);
|
|
} catch (\Throwable $e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private function columnsOf(string $name): array
|
|
{
|
|
try {
|
|
$cols = DB::select("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE table_schema = DATABASE() AND table_name = ? ORDER BY ORDINAL_POSITION", [$name]);
|
|
return array_map(fn($r) => $r->COLUMN_NAME, $cols);
|
|
} catch (\Throwable $e) {
|
|
// fallback: leggi prima riga
|
|
$first = DB::table($name)->first();
|
|
return $first ? array_keys((array)$first) : [];
|
|
}
|
|
}
|
|
}
|