netgescon-day0/tests/Feature/FornitoreTecnorepairAndIntegrationsTest.php

240 lines
9.4 KiB
PHP

<?php
use App\Filament\Pages\Fornitore\PraticheTecnorepair;
use App\Filament\Pages\Fornitore\ProdottiCatalogo;
use App\Filament\Pages\Fornitore\SerialiCatalogo;
use App\Filament\Pages\Gescon\FornitoreScheda;
use App\Models\AssistenzaTecnorepairScheda;
use App\Models\Fornitore;
use App\Models\FornitoreDipendente;
use App\Models\Product;
use App\Models\ProductSerial;
use App\Models\User;
use App\Services\Catalog\ContabilitaMysqlSerialImportService;
use App\Services\Tecnorepair\TecnoRepairArchiveService;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Models\Role;
use Symfony\Component\HttpKernel\Exception\HttpException;
beforeEach(function () {
Role::firstOrCreate(['name' => 'super-admin', 'guard_name' => 'web']);
Role::firstOrCreate(['name' => 'fornitore', 'guard_name' => 'web']);
$fornitore236 = Fornitore::query()->find(236);
if (! $fornitore236) {
$fornitore236 = new Fornitore();
$fornitore236->id = 236;
$fornitore236->ragione_sociale = 'NETHOME sas di BARONE M. & C.';
$fornitore236->partita_iva = '10055221005';
$fornitore236->codice_fiscale = '10055221005';
$fornitore236->codice_univoco = 'NETHOME';
$fornitore236->amministratore_id = 13;
$fornitore236->operational_config = [
'accounting_db' => [
'host' => '192.168.0.36',
'port' => 3307,
'database' => 'arc_nehr',
'username' => 'NETGESCON',
],
'tecnorepair' => [
'mdb_path' => '/home/michele/netgescon-day0-backup/Miki-Bug-workspace/screenshot/Assistenza gestionale/Archivi/TecnoRepairDB.mdb',
],
];
$fornitore236->save();
}
$fornitore392 = Fornitore::query()->find(392);
if (! $fornitore392) {
$fornitore392 = new Fornitore();
$fornitore392->id = 392;
$fornitore392->ragione_sociale = 'NCOM SRL';
$fornitore392->partita_iva = '14001151001';
$fornitore392->codice_univoco = 'NCOMSRL';
$fornitore392->amministratore_id = 13;
$fornitore392->save();
}
$michele = User::query()->where('email', 'michele@nethome.it')->first();
if (! $michele) {
$michele = User::query()->create([
'name' => 'Michele Barone',
'email' => 'michele@nethome.it',
'password' => bcrypt('password'),
]);
}
$michele->syncRoles(['super-admin']);
$nethomeUser = User::query()->where('email', 'nethomestore@gmail.com')->first();
if (! $nethomeUser) {
$nethomeUser = User::query()->create([
'name' => 'Nethome Store',
'email' => 'nethomestore@gmail.com',
'password' => bcrypt('password'),
]);
}
$nethomeUser->syncRoles(['fornitore']);
$dipendente = FornitoreDipendente::query()->where('email', 'nethomestore@gmail.com')->first();
if (! $dipendente) {
FornitoreDipendente::query()->create([
'email' => 'nethomestore@gmail.com',
'fornitore_id' => 236,
'user_id' => $nethomeUser->id,
'nome' => 'Nethome Store',
'attivo' => true,
]);
}
AssistenzaTecnorepairScheda::query()->firstOrCreate(
['legacy_numero_scheda' => '1831'],
[
'amministratore_id' => 13,
'fornitore_id' => 236,
'customer_name' => 'BARONE MICHELE',
'product_model' => 'NOTEBOOK ASUS ROG',
'serial_number' => 'ASUS12345',
'status_code' => '21',
'status_label' => 'INGRESSO ACCETTAZIONE',
'status_bucket' => 'in_progress',
'defect_reported' => 'NON SI ACCENDE',
'repair_description' => 'In attesa diagnosi',
'communications' => 'Cliente avvisato',
'metadata' => ['raw' => ['isRiconsegnato' => 0]],
]
);
});
it('verifies user and role alignment for super-admin and fornitore', function () {
$michele = User::where('email', 'michele@nethome.it')->first();
expect($michele)->not->toBeNull();
$roles = $michele->roles->pluck('name')->all();
expect($roles)->toBe(['super-admin']);
$nethomeUser = User::where('email', 'nethomestore@gmail.com')->first();
expect($nethomeUser)->not->toBeNull();
$nethomeRoles = $nethomeUser->roles->pluck('name')->all();
expect($nethomeRoles)->toBe(['fornitore']);
$dipendente = FornitoreDipendente::where('email', 'nethomestore@gmail.com')->first();
expect($dipendente)->not->toBeNull()
->and((int) $dipendente->fornitore_id)->toBe(236)
->and((int) $dipendente->user_id)->toBe((int) $nethomeUser->id);
});
it('strictly restricts integration settings to Nethome on FornitoreScheda', function () {
$michele = User::where('email', 'michele@nethome.it')->first();
Auth::login($michele);
// Fornitore 236 (Nethome)
$pageNethome = new FornitoreScheda();
$pageNethome->mount(236);
expect($pageNethome->isNethomeFornitore())->toBeTrue();
// Can save settings on Nethome
$pageNethome->mysqlHost = '192.168.0.36';
$pageNethome->mysqlPort = '3307';
$pageNethome->mysqlDatabase = 'arc_nehr';
$pageNethome->saveNethomeIntegrations();
$fornitore236 = Fornitore::find(236);
expect(data_get($fornitore236->operational_config, 'accounting_db.host'))->toBe('192.168.0.36')
->and(data_get($fornitore236->operational_config, 'tecnorepair.mdb_path'))->not->toBeEmpty();
// Fornitore 392 (NCOM SRL - Non Nethome)
$pageOther = new FornitoreScheda();
$pageOther->mount(392);
expect($pageOther->isNethomeFornitore())->toBeFalse();
// Attempting to save on non-Nethome throws 403
expect(fn() => $pageOther->saveNethomeIntegrations())->toThrow(HttpException::class);
expect(fn() => $pageOther->testMysqlConnection())->toThrow(HttpException::class);
expect(fn() => $pageOther->sincronizzaContabilitaNethome())->toThrow(HttpException::class);
expect(fn() => $pageOther->sincronizzaTecnorepairNethome())->toThrow(HttpException::class);
});
it('executes automated repair workflows on AssistenzaTecnorepairScheda', function () {
$service = app(TecnoRepairArchiveService::class);
$scheda = AssistenzaTecnorepairScheda::firstOrCreate(
['legacy_numero_scheda' => 'TEST-999'],
[
'amministratore_id' => 13,
'fornitore_id' => 236,
'customer_name' => 'TEST CUSTOMER',
'product_model' => 'HP PROBOOK',
'serial_number' => 'HPTEST12345',
'status_code' => '21',
'status_label' => 'INGRESSO ACCETTAZIONE',
'defect_reported' => 'NON SI ACCENDE',
]
);
// 1. Workflow Riparato
$service->chiudiComeRiparato($scheda, 'Sostituito condensatore e completato collaudo', 'MICHELE BARONE');
$scheda->refresh();
expect($scheda->status_code)->toBe('4')
->and($scheda->status_label)->toBe('RIPARATO')
->and($scheda->status_bucket)->toBe('completed')
->and(data_get($scheda->metadata, 'workflow_status'))->toBe('repaired');
// 2. Workflow Reso Fornitore RMA
$service->rendiAFornitoreRma($scheda, 'RMA-TEST-2026', 392, 'Guasto scheda madre');
$scheda->refresh();
expect($scheda->status_code)->toBe('33')
->and($scheda->status_label)->toBe('MANDATO IN GARANZIA AL PRODUTTORE')
->and($scheda->status_bucket)->toBe('in_progress')
->and($scheda->rma_code)->toBe('RMA-TEST-2026');
// 3. Workflow Rottamazione / Ricambi
$service->chiudiComeRottamato($scheda, 'Dispositivo non conveniente da riparare', true);
$scheda->refresh();
expect($scheda->status_code)->toBe('23')
->and($scheda->status_bucket)->toBe('scrapped')
->and(data_get($scheda->metadata, 'workflow_status'))->toBe('spare_parts');
});
it('mounts PraticheTecnorepair page and renders desktop grid and detail modal', function () {
$michele = User::where('email', 'michele@nethome.it')->first();
Auth::login($michele);
$page = new PraticheTecnorepair();
$page->mount();
expect($page->fornitoreId)->toBe(236)
->and($page->fornitoreLabel)->toContain('NETHOME')
->and(count($page->rows))->toBeGreaterThan(0)
->and($page->totals['totale'])->toBeGreaterThan(0);
// Open first scheda in modal
$firstRowId = $page->rows[0]['id'];
$page->openScheda($firstRowId);
expect($page->selectedSchedaId)->toBe($firstRowId)
->and($page->activeScheda)->not->toBeNull()
->and($page->activeScheda['cliente_nome'])->not->toBeEmpty();
// Verify tabs switching
$page->setSchedaTab('riparazione');
expect($page->schedaTab)->toBe('riparazione');
$page->closeScheda();
expect($page->selectedSchedaId)->toBeNull()
->and($page->activeScheda)->toBeNull();
});
it('verifies sync buttons and automatic updates on SerialiCatalogo and ProdottiCatalogo', function () {
$michele = User::where('email', 'michele@nethome.it')->first();
Auth::login($michele);
// SerialiCatalogo
$serialiPage = new SerialiCatalogo();
$serialiPage->mount();
expect($serialiPage->fornitoreId)->toBe(236);
expect($serialiPage->getPraticheUrl())->toContain('fornitore/pratiche');
// ProdottiCatalogo
$prodottiPage = new ProdottiCatalogo();
$prodottiPage->mount();
expect($prodottiPage->fornitoreId)->toBe(236);
expect($prodottiPage->getPraticheUrl())->toContain('fornitore/pratiche');
});