netgescon-day0/docs/00-IMPORT/extract_gescon_complete.sh

173 lines
8.0 KiB
Bash
Executable File

#!/bin/bash
# GESCON EXTRACTION & VALIDATION SCRIPT
# Per Stabile 148 - SUPERCONDOMINIO MILIZIE 3
# Mantiene integrità relazionale completa
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GESCON_DIR="/home/michele/netgescon/docs/02-architettura-laravel/Gescon/0021/0001"
OUTPUT_DIR="/home/michele/netgescon/docs/000-IMPORT/estratti"
VALIDATION_LOG="/home/michele/netgescon/docs/000-IMPORT/validazione_gescon_148.log"
# Creazione directory output
mkdir -p "$OUTPUT_DIR"
echo "=== GESCON EXTRACTION SCRIPT ===" | tee "$VALIDATION_LOG"
echo "Source: $GESCON_DIR/singolo_anno.mdb" | tee -a "$VALIDATION_LOG"
echo "Target: $OUTPUT_DIR" | tee -a "$VALIDATION_LOG"
echo "Started: $(date)" | tee -a "$VALIDATION_LOG"
echo "" | tee -a "$VALIDATION_LOG"
cd "$GESCON_DIR"
# 1. ESTRAZIONE TABELLE MASTER
echo "=== PHASE 1: MASTER TABLES ===" | tee -a "$VALIDATION_LOG"
echo "Extracting tabelle (millesimi types)..." | tee -a "$VALIDATION_LOG"
mdb-export singolo_anno.mdb tabelle > "$OUTPUT_DIR/tabelle_millesimali.csv"
TABELLE_COUNT=$(cat "$OUTPUT_DIR/tabelle_millesimali.csv" | wc -l)
echo "- tabelle: $((TABELLE_COUNT - 1)) records" | tee -a "$VALIDATION_LOG"
echo "Extracting voc_spe (expense vocabulary)..." | tee -a "$VALIDATION_LOG"
mdb-export singolo_anno.mdb voc_spe > "$OUTPUT_DIR/vocabolario_spese.csv"
VOC_SPE_COUNT=$(cat "$OUTPUT_DIR/vocabolario_spese.csv" | wc -l)
echo "- voc_spe: $((VOC_SPE_COUNT - 1)) records" | tee -a "$VALIDATION_LOG"
echo "Extracting straordinarie (extraordinary works)..." | tee -a "$VALIDATION_LOG"
mdb-export singolo_anno.mdb straordinarie > "$OUTPUT_DIR/straordinarie.csv"
STRA_COUNT=$(cat "$OUTPUT_DIR/straordinarie.csv" | wc -l)
echo "- straordinarie: $((STRA_COUNT - 1)) records" | tee -a "$VALIDATION_LOG"
# 2. ESTRAZIONE ANAGRAFICA CONDOMINI
echo "" | tee -a "$VALIDATION_LOG"
echo "=== PHASE 2: CONDOMINI REGISTRY ===" | tee -a "$VALIDATION_LOG"
echo "Extracting condomin (main registry)..." | tee -a "$VALIDATION_LOG"
mdb-export singolo_anno.mdb condomin > "$OUTPUT_DIR/condomini_completo.csv"
COND_COUNT=$(cat "$OUTPUT_DIR/condomini_completo.csv" | wc -l)
echo "- condomin: $((COND_COUNT - 1)) records" | tee -a "$VALIDATION_LOG"
# Analisi scala distribution
echo "Analyzing unit distribution..." | tee -a "$VALIDATION_LOG"
tail -n +2 "$OUTPUT_DIR/condomini_completo.csv" | cut -d',' -f3 | sort | uniq -c | while read count scala; do
echo " - Scala $scala: $count units" | tee -a "$VALIDATION_LOG"
done
# 3. ESTRAZIONE MILLESIMI
echo "" | tee -a "$VALIDATION_LOG"
echo "=== PHASE 3: MILLESIMI DETAILS ===" | tee -a "$VALIDATION_LOG"
echo "Extracting dett_tab (millesimi per condomino)..." | tee -a "$VALIDATION_LOG"
mdb-export singolo_anno.mdb dett_tab > "$OUTPUT_DIR/millesimi_dettaglio.csv"
MILL_COUNT=$(cat "$OUTPUT_DIR/millesimi_dettaglio.csv" | wc -l)
echo "- dett_tab: $((MILL_COUNT - 1)) records" | tee -a "$VALIDATION_LOG"
# Analisi millesimi types
echo "Analyzing millesimi types..." | tee -a "$VALIDATION_LOG"
tail -n +2 "$OUTPUT_DIR/millesimi_dettaglio.csv" | cut -d',' -f2 | sort | uniq -c | while read count tipo; do
echo " - Type $tipo: $count entries" | tee -a "$VALIDATION_LOG"
done
# 4. ESTRAZIONE OPERAZIONI CONTABILI
echo "" | tee -a "$VALIDATION_LOG"
echo "=== PHASE 4: ACCOUNTING OPERATIONS ===" | tee -a "$VALIDATION_LOG"
echo "Extracting operazioni (accounting movements)..." | tee -a "$VALIDATION_LOG"
mdb-export singolo_anno.mdb operazioni > "$OUTPUT_DIR/operazioni_contabili.csv"
OPER_COUNT=$(cat "$OUTPUT_DIR/operazioni_contabili.csv" | wc -l)
echo "- operazioni: $((OPER_COUNT - 1)) records" | tee -a "$VALIDATION_LOG"
echo "Extracting dett_pers (personal details)..." | tee -a "$VALIDATION_LOG"
mdb-export singolo_anno.mdb dett_pers > "$OUTPUT_DIR/dettaglio_ripartizioni.csv"
DETT_COUNT=$(cat "$OUTPUT_DIR/dettaglio_ripartizioni.csv" | wc -l)
echo "- dett_pers: $((DETT_COUNT - 1)) records" | tee -a "$VALIDATION_LOG"
# 5. ESTRAZIONE RATE
echo "" | tee -a "$VALIDATION_LOG"
echo "=== PHASE 5: CONDOMINIUM FEES ===" | tee -a "$VALIDATION_LOG"
echo "Extracting rate (condominium fees)..." | tee -a "$VALIDATION_LOG"
mdb-export singolo_anno.mdb rate > "$OUTPUT_DIR/rate_condominiali.csv"
RATE_COUNT=$(cat "$OUTPUT_DIR/rate_condominiali.csv" | wc -l)
echo "- rate: $((RATE_COUNT - 1)) records" | tee -a "$VALIDATION_LOG"
# 6. VALIDAZIONI RELAZIONALI
echo "" | tee -a "$VALIDATION_LOG"
echo "=== PHASE 6: RELATIONAL VALIDATION ===" | tee -a "$VALIDATION_LOG"
# Validation 1: E_lostesso_Di relationships
echo "Validating E_lostesso_Di relationships..." | tee -a "$VALIDATION_LOG"
E_STESSO_COUNT=$(tail -n +2 "$OUTPUT_DIR/condomini_completo.csv" | cut -d',' -f119 | grep -v '^0$' | grep -v '^$' | wc -l)
echo "- E_lostesso_Di relations found: $E_STESSO_COUNT" | tee -a "$VALIDATION_LOG"
# Validation 2: N_SPE consistency between operazioni and dett_pers
echo "Validating N_SPE consistency..." | tee -a "$VALIDATION_LOG"
OPER_N_SPE=$(tail -n +2 "$OUTPUT_DIR/operazioni_contabili.csv" | cut -d',' -f2 | sort | uniq | wc -l)
DETT_N_SPE=$(tail -n +2 "$OUTPUT_DIR/dettaglio_ripartizioni.csv" | cut -d',' -f3 | sort | uniq | wc -l)
echo "- Unique N_SPE in operazioni: $OPER_N_SPE" | tee -a "$VALIDATION_LOG"
echo "- Unique N_SPE in dett_pers: $DETT_N_SPE" | tee -a "$VALIDATION_LOG"
# Validation 3: ID_COND consistency
echo "Validating ID_COND consistency..." | tee -a "$VALIDATION_LOG"
COND_IDS=$(tail -n +2 "$OUTPUT_DIR/condomini_completo.csv" | cut -d',' -f1 | sort | uniq | wc -l)
DETT_IDS=$(tail -n +2 "$OUTPUT_DIR/dettaglio_ripartizioni.csv" | cut -d',' -f4 | sort | uniq | wc -l)
MILL_IDS=$(tail -n +2 "$OUTPUT_DIR/millesimi_dettaglio.csv" | cut -d',' -f3 | sort | uniq | wc -l)
RATE_IDS=$(tail -n +2 "$OUTPUT_DIR/rate_condominiali.csv" | cut -d',' -f2 | sort | uniq | wc -l)
echo "- Unique ID_COND in condomin: $COND_IDS" | tee -a "$VALIDATION_LOG"
echo "- Unique ID_COND in dett_pers: $DETT_IDS" | tee -a "$VALIDATION_LOG"
echo "- Unique ID_COND in dett_tab: $MILL_IDS" | tee -a "$VALIDATION_LOG"
echo "- Unique ID_COND in rate: $RATE_IDS" | tee -a "$VALIDATION_LOG"
# 7. CREAZIONE REPORT RELAZIONI
echo "" | tee -a "$VALIDATION_LOG"
echo "=== PHASE 7: RELATIONSHIP MAPPING ===" | tee -a "$VALIDATION_LOG"
# Estrazione relazioni E_lostesso_Di specifiche
echo "Creating E_lostesso_Di mapping..." | tee -a "$VALIDATION_LOG"
cat > "$OUTPUT_DIR/relazioni_e_stesso_di.csv" << 'EOF'
id_cond,cod_cond,scala,int,tipo_pr,nom_cond,e_lostesso_di,relazione_tipo
EOF
# Estrarre record con E_lostesso_Di non zero
tail -n +2 "$OUTPUT_DIR/condomini_completo.csv" | while IFS=',' read -r id_cond cod_cond scala int tipo_pr nom_cond resto; do
e_stesso=$(echo "$resto" | cut -d',' -f113) # Campo 119 - 6 precedenti
if [[ "$e_stesso" != "0" && "$e_stesso" != "" ]]; then
echo "$id_cond,$cod_cond,$scala,$int,$tipo_pr,\"$nom_cond\",$e_stesso,riferimento" >> "$OUTPUT_DIR/relazioni_e_stesso_di.csv"
fi
done
REL_COUNT=$(cat "$OUTPUT_DIR/relazioni_e_stesso_di.csv" | wc -l)
echo "- E_lostesso_Di relations exported: $((REL_COUNT - 1))" | tee -a "$VALIDATION_LOG"
# 8. SUMMARY REPORT
echo "" | tee -a "$VALIDATION_LOG"
echo "=== EXTRACTION SUMMARY ===" | tee -a "$VALIDATION_LOG"
echo "Total files created: $(ls -1 "$OUTPUT_DIR"/*.csv | wc -l)" | tee -a "$VALIDATION_LOG"
echo "Total data size: $(du -sh "$OUTPUT_DIR" | cut -f1)" | tee -a "$VALIDATION_LOG"
echo "" | tee -a "$VALIDATION_LOG"
echo "Files created:" | tee -a "$VALIDATION_LOG"
for file in "$OUTPUT_DIR"/*.csv; do
filename=$(basename "$file")
lines=$(cat "$file" | wc -l)
size=$(du -h "$file" | cut -f1)
echo "- $filename: $((lines - 1)) records, $size" | tee -a "$VALIDATION_LOG"
done
echo "" | tee -a "$VALIDATION_LOG"
echo "Completed: $(date)" | tee -a "$VALIDATION_LOG"
echo "=== END EXTRACTION ===" | tee -a "$VALIDATION_LOG"
echo ""
echo "✅ Extraction completed successfully!"
echo "📁 Files saved to: $OUTPUT_DIR"
echo "📊 Validation log: $VALIDATION_LOG"
echo ""
echo "Next steps:"
echo "1. Review validation log for any inconsistencies"
echo "2. Import data into netgescon_stabile_GES00148 database"
echo "3. Verify relational integrity post-import"