netgescon-day0/tests/Feature/ImportGesconFullPipelineTest.php

123 lines
4.4 KiB
PHP

<?php
use App\Models\User;
use App\Models\Stabile;
use App\Models\Amministratore;
use App\Models\GestioneContabile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Artisan;
test('it imports and updates operations incrementally using update-only option', function () {
$user = User::factory()->create();
$user->assignRole('super-admin');
$this->actingAs($user);
$amministratore = Amministratore::create([
'user_id' => $user->id,
'nome' => 'Admin',
'cognome' => 'Test',
'denominazione' => 'Amministrazione Test',
'codice_fiscale' => '12345678901',
]);
$stabile = Stabile::create([
'amministratore_id' => $amministratore->id,
'codice_stabile' => '9999',
'denominazione' => 'Condominio Test Contabilita',
'codice_fiscale' => '12345678901',
'indirizzo' => 'Via Test 1',
'citta' => 'Roma',
'cap' => '00100',
'provincia' => 'RM',
'codice_destinatario_sdi' => '0000000',
]);
$gestione = GestioneContabile::create([
'tenant_id' => 'default',
'stabile_id' => $stabile->id,
'anno_gestione' => 2026,
'tipo_gestione' => 'ordinaria',
'denominazione' => 'Gestione Ordinaria 2026',
'data_inizio' => '2026-01-01',
'data_fine' => '2026-12-31',
'stato' => 'aperta',
'protocollo_prefix' => 'O2026',
]);
// Setup staging operations table
Schema::connection('gescon_import')->dropIfExists('operazioni');
Schema::connection('gescon_import')->create('operazioni', function ($table) {
$table->id('id_operaz');
$table->string('cod_stabile')->nullable();
$table->string('legacy_year')->nullable();
$table->string('compet')->nullable();
$table->string('gestione')->nullable();
$table->integer('n_stra')->nullable();
$table->integer('n_spe')->nullable();
$table->string('cod_for')->nullable();
$table->string('cod_spe')->nullable();
$table->string('natura2')->nullable();
$table->decimal('importo_euro', 15, 2)->nullable();
$table->decimal('importo_spese', 15, 2)->nullable();
$table->decimal('importo_entrate', 15, 2)->nullable();
$table->string('note')->nullable();
$table->string('natura')->nullable();
$table->date('dt_spe')->nullable();
});
// Seed initial staging operations data
DB::connection('gescon_import')->table('operazioni')->insert([
'id_operaz' => 888,
'cod_stabile' => '9999',
'legacy_year' => '2026',
'compet' => 'C',
'gestione' => 'O',
'n_stra' => 0,
'importo_euro' => 150.00,
'importo_spese' => 150.00,
'importo_entrate' => 0.00,
'note' => 'Original Operation',
'dt_spe' => '2026-07-01',
]);
// Run first import to create the domain entry
Artisan::call('gescon:import-full', [
'--stabile' => '9999',
'--solo' => 'operazioni',
]);
// Verify it was created
$imported = DB::table('operazioni_contabili')->where('legacy_id', 888)->first();
expect($imported)->not->toBeNull();
expect($imported->descrizione)->toBe('Original Operation');
// Simulate user editing/reconciling this transaction in NetGescon
DB::table('operazioni_contabili')->where('id', $imported->id)->update([
'stato_operazione' => 'reconciled_by_user',
]);
// Update staging data in MDB (e.g. amount or note changes at source)
DB::connection('gescon_import')->table('operazioni')->where('id_operaz', 888)->update([
'note' => 'Updated Operation Note',
]);
// Run pipeline again WITH --update-only to check if the user modifications and record are preserved
Artisan::call('gescon:import-full', [
'--stabile' => '9999',
'--solo' => 'operazioni',
'--update-only' => true,
]);
$reimported = DB::table('operazioni_contabili')->where('legacy_id', 888)->first();
expect($reimported)->not->toBeNull();
// Verify the record was NOT deleted and recreated (which would lose the status edited by user)
expect($reimported->id)->toBe($imported->id);
expect($reimported->stato_operazione)->toBe('reconciled_by_user');
// Verify the note was updated incrementally
expect($reimported->descrizione)->toBe('Updated Operation Note');
// Clean up staging
Schema::connection('gescon_import')->dropIfExists('operazioni');
});