fix(ordinarie): resolve Column not found: 1054 Unknown column tabella in Ordinarie consuntivo queries (task-d095b0e23b)

This commit is contained in:
michele 2026-08-29 00:38:48 +02:00
parent cd69e7a914
commit b95a43276c

View File

@ -1351,25 +1351,37 @@ public function getConsuntivoProperty(): array
$excludeCods = array_values(array_unique(array_merge($excludeCods, $creDebCodes))); $excludeCods = array_values(array_unique(array_merge($excludeCods, $creDebCodes)));
} }
} }
$isNetgescon = $this->netgesconWorkflowMode;
$codSpeCol = $isNetgescon ? 'o.conto_contabile' : 'cod_spe';
$tabellaSelect = ($isNetgescon || ! Schema::connection('gescon_import')->hasColumn('operazioni', 'tabella'))
? DB::raw('null as tabella')
: 'tabella';
$groupByFields = ($isNetgescon || ! Schema::connection('gescon_import')->hasColumn('operazioni', 'tabella'))
? [DB::raw($codSpeCol)]
: ['tabella', DB::raw($codSpeCol)];
$competCol = $isNetgescon ? 'o.compet' : 'compet';
$rows = $base $rows = $base
->where(function ($query): void { ->where(function ($query) use ($competCol): void {
$query->whereNull('compet') $query->whereNull($competCol)
->orWhere('compet', '!=', 'P'); ->orWhere($competCol, '!=', 'P');
}) })
->whereNotIn('cod_spe', $excludeCods) ->whereNotIn($codSpeCol, $excludeCods)
->select('tabella', 'cod_spe') ->select($tabellaSelect, DB::raw("{$codSpeCol} as cod_spe"))
->selectRaw("SUM( ->selectRaw("SUM(
CASE CASE
WHEN (COALESCE(importo_spese, 0) + COALESCE(importo_entrate, 0) + COALESCE(importo_debiti, 0) + COALESCE(importo_crediti, 0)) = 0 THEN WHEN (COALESCE(" . ($isNetgescon ? "o.dare" : "importo_spese") . ", 0) + COALESCE(" . ($isNetgescon ? "o.avere" : "importo_entrate") . ", 0) + COALESCE(" . ($isNetgescon ? "0" : "importo_debiti") . ", 0) + COALESCE(" . ($isNetgescon ? "0" : "importo_crediti") . ", 0)) = 0 THEN
CASE CASE
WHEN cod_spe = 'RB1' THEN -1 * COALESCE(importo_euro, importo, 0) WHEN {$codSpeCol} = 'RB1' THEN -1 * COALESCE(" . ($isNetgescon ? "o.dare, o.avere" : "importo_euro, importo") . ", 0)
WHEN natura2 IN ('E', 'ENTRATA', 'ENTRATE', 'CREDITO') THEN -1 * COALESCE(importo_euro, importo, 0) WHEN " . ($isNetgescon ? "null" : "natura2") . " IN ('E', 'ENTRATA', 'ENTRATE', 'CREDITO') THEN -1 * COALESCE(" . ($isNetgescon ? "o.dare, o.avere" : "importo_euro, importo") . ", 0)
ELSE COALESCE(importo_euro, importo, 0) ELSE COALESCE(" . ($isNetgescon ? "o.dare, o.avere" : "importo_euro, importo") . ", 0)
END END
ELSE (COALESCE(importo_spese, 0) + COALESCE(importo_debiti, 0) - COALESCE(importo_entrate, 0) - COALESCE(importo_crediti, 0)) ELSE (COALESCE(" . ($isNetgescon ? "o.dare" : "importo_spese") . ", 0) + COALESCE(" . ($isNetgescon ? "0" : "importo_debiti") . ", 0) - COALESCE(" . ($isNetgescon ? "o.avere" : "importo_entrate") . ", 0) - COALESCE(" . ($isNetgescon ? "0" : "importo_crediti") . ", 0))
END END
) as totale") ) as totale")
->groupBy('tabella', 'cod_spe') ->groupBy($groupByFields)
->get(); ->get();
$frazRows = collect(); $frazRows = collect();
@ -3955,17 +3967,20 @@ public function openMastrino(string $tabella, string $codSpe): void
$this->mastrinoTabella = $tabella; $this->mastrinoTabella = $tabella;
$this->mastrinoCodSpe = $codSpe; $this->mastrinoCodSpe = $codSpe;
$query = $this->buildOperazioniQuery() $isNetgescon = $this->netgesconWorkflowMode;
->where('cod_spe', $codSpe) $codSpeCol = $isNetgescon ? 'o.conto_contabile' : 'cod_spe';
->orderBy('dt_spe')
->orderBy('id_operaz');
if (trim($tabella) !== '') { $query = $this->buildOperazioniQuery()
->where($codSpeCol, $codSpe)
->orderBy($isNetgescon ? 'o.data_operazione' : 'dt_spe')
->orderBy($isNetgescon ? 'o.id' : 'id_operaz');
if (! $isNetgescon && trim($tabella) !== '' && Schema::connection('gescon_import')->hasColumn('operazioni', 'tabella')) {
$query->where('tabella', $tabella); $query->where('tabella', $tabella);
} }
if ($this->filterAnno) { if ($this->filterAnno) {
$query->whereYear('dt_spe', $this->filterAnno); $query->whereYear($isNetgescon ? 'o.data_operazione' : 'dt_spe', $this->filterAnno);
} }
$rows = $query->get(); $rows = $query->get();
@ -3990,15 +4005,15 @@ public function sortBy(string $field): void
{ {
$allowed = [ $allowed = [
'id_operaz', 'id_operaz',
'n_spe',
'dt_spe', 'dt_spe',
'legacy_year',
'gestione',
'cod_spe', 'cod_spe',
'tabella', 'tabella',
'cod_for',
'benef', 'benef',
'importo_euro', 'importo_euro',
'num_fat', 'num_fat',
'fe_uid',
'rif_rda',
]; ];
if (! in_array($field, $allowed, true)) { if (! in_array($field, $allowed, true)) {
@ -4015,22 +4030,24 @@ public function sortBy(string $field): void
public function getSpecialCodesSummaryProperty(): array public function getSpecialCodesSummaryProperty(): array
{ {
$base = $this->buildOperazioniQuery(); $base = $this->buildOperazioniQuery();
$isNetgescon = $this->netgesconWorkflowMode;
$codSpeCol = $isNetgescon ? 'o.conto_contabile' : 'cod_spe';
$rows = $base $rows = $base
->whereIn('cod_spe', ['Z01', 'Z02']) ->whereIn($codSpeCol, ['Z01', 'Z02'])
->select('cod_spe') ->select(DB::raw("{$codSpeCol} as cod_spe"))
->selectRaw("SUM( ->selectRaw("SUM(
CASE CASE
WHEN (COALESCE(importo_spese, 0) + COALESCE(importo_entrate, 0) + COALESCE(importo_debiti, 0) + COALESCE(importo_crediti, 0)) = 0 THEN WHEN (COALESCE(" . ($isNetgescon ? "o.dare" : "importo_spese") . ", 0) + COALESCE(" . ($isNetgescon ? "o.avere" : "importo_entrate") . ", 0) + COALESCE(" . ($isNetgescon ? "0" : "importo_debiti") . ", 0) + COALESCE(" . ($isNetgescon ? "0" : "importo_crediti") . ", 0)) = 0 THEN
CASE CASE
WHEN natura2 IN ('E', 'ENTRATA', 'ENTRATE', 'CREDITO') THEN -1 * COALESCE(importo_euro, importo, 0) WHEN " . ($isNetgescon ? "null" : "natura2") . " IN ('E', 'ENTRATA', 'ENTRATE', 'CREDITO') THEN -1 * COALESCE(" . ($isNetgescon ? "o.dare, o.avere" : "importo_euro, importo") . ", 0)
ELSE COALESCE(importo_euro, importo, 0) ELSE COALESCE(" . ($isNetgescon ? "o.dare, o.avere" : "importo_euro, importo") . ", 0)
END END
ELSE (COALESCE(importo_spese, 0) + COALESCE(importo_debiti, 0) - COALESCE(importo_entrate, 0) - COALESCE(importo_crediti, 0)) ELSE (COALESCE(" . ($isNetgescon ? "o.dare" : "importo_spese") . ", 0) + COALESCE(" . ($isNetgescon ? "0" : "importo_debiti") . ", 0) - COALESCE(" . ($isNetgescon ? "o.avere" : "importo_entrate") . ", 0) - COALESCE(" . ($isNetgescon ? "0" : "importo_crediti") . ", 0))
END END
) as totale") ) as totale")
->groupBy('cod_spe') ->groupBy(DB::raw($codSpeCol))
->get(); ->get();
$map = $rows->mapWithKeys(fn($row) => [strtoupper((string) $row->cod_spe) => (float) ($row->totale ?? 0)])->all(); $map = $rows->mapWithKeys(fn($row) => [strtoupper((string) $row->cod_spe) => (float) ($row->totale ?? 0)])->all();