[FEAT] Agile Technology = studio di consulenza che gestisce sé stessa (consolidamento)

Opzione A (decisione utente 24/6): nessun duplicato, nessuna rinomina. Seeder
idempotente+transazionale build_agile_consulente.php:
- consulting_firm 1: anagrafica REALE (P.IVA 07776161213, sede legale Roma Piazza
  di Campitelli 2 00186 RM, PEC agile.software@pec.it) — erano dati placeholder.
- org 996003 (Nuova Agile Technology srl, P.IVA 07776161213): consulting_firm_id=1
  → prima azienda auto-gestita dallo studio.
- firm_org_assignments: studio 1 ↔ org 996003 ↔ 4 admin.
- users.consulting_firm_id=1 per Benassati(4, consulente), Silvia Garretto(103,
  legale rappr = presidenza@), Tagliavini(326), Fattori(330).
Manuale/procedure/policy (12 doc published) già su 996003, non toccati. Rollback nel file.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
DevEnv nis2-agile
2026-06-24 08:09:07 +02:00
co-authored by Claude Opus 4.8
parent e5f7a7d3a6
commit 3b9f30a890
@@ -0,0 +1,83 @@
<?php
/**
* build_agile_consulente.php — "Agile Technology" come STUDIO DI CONSULENZA
* (consulting_firm id=1) che gestisce sé stessa (org 996003) come PRIMA azienda.
*
* Opzione A (consolidamento, decisione utente 2026-06-24): NESSUN duplicato, NESSUNA rinomina.
* IDEMPOTENTE + TRANSAZIONALE. Esegue:
* 1) consulting_firm 1: anagrafica REALE (P.IVA 07776161213, sede legale Roma
* Piazza di Campitelli 2 00186 RM, PEC agile.software@pec.it).
* 2) organizations 996003: consulting_firm_id=1 → azienda auto-gestita dallo studio.
* 3) firm_org_assignments: studio 1 ↔ org 996003 ↔ i 4 admin.
* 4) users: consulting_firm_id=1 per i 4 admin (Benassati consulente, Silvia legale
* rappr. = presidenza@ (l'utente reale; "presidente@" non esiste), Tagliavini, Fattori).
* Manuale/procedure/policy: GIÀ su 996003 (12 published) → NON toccati.
* NB schema: consulting_firms non ha campi legale_rappresentante/sede_operativa →
* legale rappr. = user 103; sede operativa Modena vive nei documenti SGSI dell'org.
*
* Esegui: docker exec nis2-app php /var/www/nis2-agile/application/cli/build_agile_consulente.php
* Rollback: vedi fondo file.
*/
if (PHP_SAPI !== 'cli') { http_response_code(403); exit("CLI only\n"); }
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../config/database.php';
$pdo = Database::getInstance();
const FIRM_ID = 1;
const ORG_ID = 996003;
const ADMINS = [4, 103, 326, 330]; // Benassati(consulente) · Silvia(legale rappr) · Tagliavini · Fattori
const ASSIGNED_BY = 4; // il consulente
function one($pdo, $sql, $p = []) { $s = $pdo->prepare($sql); $s->execute($p); return $s->fetch(PDO::FETCH_ASSOC); }
$inList = implode(',', ADMINS);
echo "=== STATO PRIMA ===\n";
echo "firm 1 : " . json_encode(one($pdo, "SELECT name,vat_number,address,city,cap,pec FROM consulting_firms WHERE id=?", [FIRM_ID]), JSON_UNESCAPED_UNICODE) . "\n";
echo "org 996003 : cf_id=" . json_encode(one($pdo, "SELECT consulting_firm_id FROM organizations WHERE id=?", [ORG_ID])) . "\n";
echo "users : " . json_encode($pdo->query("SELECT id,consulting_firm_id FROM users WHERE id IN ($inList)")->fetchAll(PDO::FETCH_KEY_PAIR)) . "\n";
echo "assignments: " . (int)$pdo->query("SELECT COUNT(*) FROM firm_org_assignments WHERE consulting_firm_id=" . FIRM_ID . " AND organization_id=" . ORG_ID)->fetchColumn() . "\n\n";
$pdo->beginTransaction();
try {
$pdo->prepare("UPDATE consulting_firms SET vat_number=?, fiscal_code=?, forma_giuridica=?, address=?, city=?, province=?, cap=?, pec=?, website=?, updated_at=NOW() WHERE id=?")
->execute(['07776161213', '07776161213', 'SRL', 'Piazza di Campitelli, 2', 'Roma', 'RM', '00186', 'agile.software@pec.it', 'https://agile.software', FIRM_ID]);
echo "[1] consulting_firm 1: anagrafica reale aggiornata\n";
$pdo->prepare("UPDATE organizations SET consulting_firm_id=? WHERE id=?")->execute([FIRM_ID, ORG_ID]);
echo "[2] org 996003.consulting_firm_id=1 (azienda auto-gestita)\n";
$ins = 0;
foreach (ADMINS as $uid) {
$ex = (int)one($pdo, "SELECT COUNT(*) c FROM firm_org_assignments WHERE consulting_firm_id=? AND organization_id=? AND assigned_to=?", [FIRM_ID, ORG_ID, $uid])['c'];
if ($ex === 0) {
$pdo->prepare("INSERT INTO firm_org_assignments (consulting_firm_id,organization_id,assigned_to,assigned_by,created_at) VALUES (?,?,?,?,NOW())")
->execute([FIRM_ID, ORG_ID, $uid, ASSIGNED_BY]);
$ins++;
}
}
echo "[3] firm_org_assignments: +$ins nuove (su " . count(ADMINS) . " admin)\n";
$pdo->prepare("UPDATE users SET consulting_firm_id=? WHERE id IN ($inList)")->execute([FIRM_ID]);
echo "[4] users.consulting_firm_id=1 per $inList\n";
$pdo->commit();
echo "\n=== COMMIT OK ===\n";
} catch (Throwable $e) {
$pdo->rollBack();
echo "ROLLBACK (nessuna modifica): " . $e->getMessage() . "\n";
exit(1);
}
echo "\n=== STATO DOPO ===\n";
echo "firm 1 : " . json_encode(one($pdo, "SELECT name,vat_number,fiscal_code,address,city,province,cap,pec FROM consulting_firms WHERE id=?", [FIRM_ID]), JSON_UNESCAPED_UNICODE) . "\n";
echo "org 996003 : " . json_encode(one($pdo, "SELECT name,vat_number,consulting_firm_id FROM organizations WHERE id=?", [ORG_ID]), JSON_UNESCAPED_UNICODE) . "\n";
echo "users : " . json_encode($pdo->query("SELECT id,email,role,consulting_firm_id FROM users WHERE id IN ($inList)")->fetchAll(PDO::FETCH_ASSOC), JSON_UNESCAPED_UNICODE) . "\n";
echo "assignments: " . json_encode($pdo->query("SELECT assigned_to,assigned_by FROM firm_org_assignments WHERE consulting_firm_id=" . FIRM_ID . " AND organization_id=" . ORG_ID . " ORDER BY assigned_to")->fetchAll(PDO::FETCH_ASSOC)) . "\n";
echo "doc SGSI 996003 (published): " . (int)$pdo->query("SELECT COUNT(*) FROM isms_documents WHERE organization_id=" . ORG_ID . " AND status='published'")->fetchColumn() . "\n";
/* ROLLBACK manuale:
UPDATE consulting_firms SET vat_number='IT12345670962',fiscal_code='12345670962',address='Via dei Consulenti 42',city='Milano',province='MI',cap='20121',pec='agiletech@pec.it' WHERE id=1;
UPDATE organizations SET consulting_firm_id=NULL WHERE id=996003;
DELETE FROM firm_org_assignments WHERE consulting_firm_id=1 AND organization_id=996003;
UPDATE users SET consulting_firm_id=NULL WHERE id IN (4,103); -- (326,330 erano GIÀ firm 1)
*/