bmc_hub/scripts/simple_match.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

59 lines
1.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
# Simpel version - ingen fancy features
CSV_FILE="$1"
CONTAINER="bmc-hub-postgres-prod"
if [ -z "$CSV_FILE" ]; then
echo "Brug: $0 <csv-file>"
exit 1
fi
echo "Reading $CSV_FILE..."
LINE_NO=0
UPDATED=0
while IFS=';' read -r nummer navn rest; do
LINE_NO=$((LINE_NO + 1))
# Skip første 4 linjer
if [ $LINE_NO -le 4 ]; then
continue
fi
# Clean nummer og navn
nummer=$(echo "$nummer" | tr -cd '0-9')
navn=$(echo "$navn" | xargs)
# Skip hvis tomt
if [ -z "$nummer" ] || [ -z "$navn" ]; then
continue
fi
echo "[$LINE_NO] $nummer - $navn"
# Escape quotes
navn_esc=$(echo "$navn" | sed "s/'/''/g")
# Find kunde
ID=$(sudo podman exec -i "$CONTAINER" psql -U bmc_hub -d bmc_hub -t -c \
"SELECT id FROM customers WHERE LOWER(name) = LOWER('$navn_esc') LIMIT 1;" | xargs)
if [ -n "$ID" ]; then
# Update
sudo podman exec -i "$CONTAINER" psql -U bmc_hub -d bmc_hub -c \
"UPDATE customers SET economic_customer_number = $nummer WHERE id = $ID;" > /dev/null
echo " ✅ Updated ID $ID"
UPDATED=$((UPDATED + 1))
else
# Create
sudo podman exec -i "$CONTAINER" psql -U bmc_hub -d bmc_hub -c \
"INSERT INTO customers (name, economic_customer_number, created_at, updated_at) VALUES ('$navn_esc', $nummer, NOW(), NOW());" > /dev/null
echo " Created"
UPDATED=$((UPDATED + 1))
fi
done < "$CSV_FILE"
echo ""
echo "Done! Updated: $UPDATED customers"