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