40 lines
1.4 KiB
PHP
40 lines
1.4 KiB
PHP
<?php
|
|
namespace App\Services\Consumi;
|
|
|
|
use App\Models\FatturaElettronica;
|
|
use App\Models\StabileServizioTariffa;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class ConsumiRiscaldamentoTariffeIngestionService
|
|
{
|
|
/** @param array<string,mixed> $parsed */
|
|
public function ingest(FatturaElettronica $fattura, array $parsed, ?int $stabileServizioId = null): array
|
|
{
|
|
if (! Schema::hasTable('stabile_servizio_tariffe')) {
|
|
return ['status' => 'skipped', 'message' => 'Tabella stabile_servizio_tariffe non presente'];
|
|
}
|
|
|
|
$stabileId = (int) ($fattura->stabile_id ?? 0);
|
|
if ($stabileId <= 0) {
|
|
return ['status' => 'error', 'message' => 'Stabile non valido'];
|
|
}
|
|
|
|
$row = StabileServizioTariffa::query()->updateOrCreate(
|
|
['fattura_elettronica_id' => (int) $fattura->id],
|
|
[
|
|
'stabile_id' => $stabileId,
|
|
'stabile_servizio_id' => $stabileServizioId,
|
|
'fornitore_id' => (int) ($fattura->fornitore_id ?: 0) ?: null,
|
|
'data_fattura' => $fattura->data_fattura,
|
|
'decorrenza_tariffe' => $parsed['pagamento']['scadenza'] ?? null,
|
|
'profilo_tariffario' => 'standard',
|
|
'payload' => $parsed,
|
|
]
|
|
);
|
|
|
|
return [
|
|
'status' => 'ok',
|
|
'id' => (int) $row->id,
|
|
];
|
|
}
|
|
} |