bmc_hub/scripts/match_all_customers.sh
Christian 42b766b31e Add scripts for managing e-conomic customer numbers
- Implemented fix_economic_numbers.sh to correct invalid 10-digit economic_customer_number entries in the database.
- Created import_economic_csv.py for importing customers from a CSV file into the Hub database, handling updates and new entries.
- Developed match_all_customers.sh to match all customers from a CSV file to the Hub database, updating or creating records as necessary.
- Added simple_match.sh for a straightforward matching process of customers from a CSV file to the Hub database.
2026-01-06 19:59:07 +01:00

146 lines
4.5 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Script til at matche ALLE kunder fra e-conomic CSV til Hub database
# Opdaterer economic_customer_number baseret på kunde navn match
# VIGTIGT: Ingen set -e her - vi vil fortsætte ved fejl
set +e
CONTAINER_NAME="bmc-hub-postgres-prod"
DB_USER="bmc_hub"
DB_NAME="bmc_hub"
CSV_FILE="$1"
# Farver
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
if [ -z "$CSV_FILE" ]; then
echo -e "${RED}❌ Brug: $0 <path-to-csv-file>${NC}"
echo ""
echo "Eksempel: $0 ~/Downloads/Kunder-2.csv"
exit 1
fi
if [ ! -f "$CSV_FILE" ]; then
echo -e "${RED}❌ Fil ikke fundet: $CSV_FILE${NC}"
exit 1
fi
echo -e "${GREEN}📂 Læser CSV fil: $CSV_FILE${NC}"
# Vis første 10 linjer for debugging
echo -e "${YELLOW}📋 Første linjer i CSV:${NC}"
head -10 "$CSV_FILE"
echo ""
# Statistik
TOTAL=0
MATCHED=0
UPDATED=0
SKIPPED=0
ERRORS=0
# Lav temp fil med data
TEMP_FILE=$(mktemp)
tail -n +5 "$CSV_FILE" | grep -v "^$" > "$TEMP_FILE"
echo -e "${GREEN}🔢 Antal linjer i CSV: $(wc -l < "$TEMP_FILE")${NC}"
echo ""
# Parse CSV og match kunder
# Format: Nr.;Navn;Gruppe;Attention;Saldo;Forfalden;E-mail
while IFS=';' read -r nummer navn gruppe attention saldo forfalden email rest || [ -n "$nummer" ]; do
# Skip tomme linjer og header
if [ -z "$nummer" ] || [ "$nummer" = "Nr." ]; then
continue
fi
# Trim whitespace og fjern BOM/special chars
nummer=$(echo "$nummer" | xargs | tr -cd '0-9')
navn=$(echo "$navn" | xargs)
if [ -z "$navn" ] || [ -z "$nummer" ]; then
echo -e "${RED}DEBUG: Skipping empty - nummer='$nummer', navn='$navn'${NC}"
continue
fi
TOTAL=$((TOTAL + 1))
# Debug ALLE linjer
echo -e "${YELLOW}[$TOTAL] Processing: $nummer - $navn${NC}"
# Escape single quotes i navn
navn_escaped=$(echo "$navn" | sed "s/'/''/g")
# Tjek om nummer er gyldigt (max 9 cifre)
if [ "$nummer" -gt 999999999 ] 2>/dev/null; then
echo -e "${YELLOW}⚠️ Springer over $navn - Nummer for stort: $nummer${NC}"
SKIPPED=$((SKIPPED + 1))
continue
fi
# Find matchende kunde i Hub
MATCH=$(sudo podman exec -i "$CONTAINER_NAME" psql -U "$DB_USER" -d "$DB_NAME" -t -c \
"SELECT id FROM customers WHERE LOWER(TRIM(name)) = LOWER(TRIM('$navn_escaped')) LIMIT 1;")
MATCH=$(echo "$MATCH" | xargs)
if [ -n "$MATCH" ]; then
MATCHED=$((MATCHED + 1))
# Opdater economic_customer_number (altid - tvungen opdatering)
UPDATE_RESULT=$(sudo podman exec -i "$CONTAINER_NAME" psql -U "$DB_USER" -d "$DB_NAME" -t -c \
"UPDATE customers SET economic_customer_number = $nummer, last_synced_at = NOW()
WHERE id = $MATCH
RETURNING id;" 2>&1)
if echo "$UPDATE_RESULT" | grep -q "^[0-9]"; then
echo -e "${GREEN}$navn → e-conomic #$nummer (opdateret)${NC}"
UPDATED=$((UPDATED + 1))
else
echo -e "${YELLOW}⏭️ $navn - Ingen ændring nødvendig${NC}"
fi
else
# Kunde findes ikke i Hub - opret den
CREATE_RESULT=$(sudo podman exec -i "$CONTAINER_NAME" psql -U "$DB_USER" -d "$DB_NAME" -t -c \
"INSERT INTO customers (name, economic_customer_number, created_at, updated_at)
VALUES ('$navn_escaped', $nummer, NOW(), NOW())
ON CONFLICT DO NOTHING
RETURNING id;" 2>&1)
if echo "$CREATE_RESULT" | grep -q "^[0-9]"; then
echo -e "${GREEN} OPRETTET: $navn → e-conomic #$nummer${NC}"
UPDATED=$((UPDATED + 1))
else
echo -e "${YELLOW}⚠️ Kunne ikke oprette: $navn (e-conomic #$nummer)${NC}"
SKIPPED=$((SKIPPED + 1))
fi
fi
# Progress hver 50. kunde
if [ $((TOTAL % 50)) -eq 0 ] && [ $TOTAL -gt 0 ]; then
echo -e "${YELLOW}📊 Progress: $TOTAL behandlet, $MATCHED matched, $UPDATED opdateret${NC}"
fi
done < "$TEMP_FILE"
# Cleanup
rm -f "$TEMP_FILE"
echo ""
echo -e "${GREEN}✅ Matching komplet!${NC}"
echo -e "${GREEN}📊 Statistik:${NC}"
echo -e " Total behandlet: $TOTAL"
echo -e " Matched i Hub: $MATCHED"
echo -e " Opdateret: $UPDATED"
echo -e " Sprunget over: $SKIPPED"
echo ""
# Vis kunder der mangler economic_customer_number
MISSING=$(sudo podman exec -i "$CONTAINER_NAME" psql -U "$DB_USER" -d "$DB_NAME" -t -c \
"SELECT COUNT(*) FROM customers WHERE economic_customer_number IS NULL;")
echo -e "${YELLOW}⚠️ $MISSING kunder mangler stadig economic_customer_number${NC}"