121 lines
3.6 KiB
Python
121 lines
3.6 KiB
Python
import openpyxl
|
|
from datetime import datetime
|
|
import re
|
|
import requests
|
|
|
|
EXCEL_FILE = r"C:\Users\renet\.openclaw\workspace\buchhaltungs-app\Schulden Kerstin.xlsx"
|
|
KREDIT_ID = "bdcb8b1e-a0e6-4d49-b6ee-c16742e38403"
|
|
API_URL = f"http://localhost:3001/api/kredite/{KREDIT_ID}/zahlungen"
|
|
|
|
print(f"Lade: {EXCEL_FILE}")
|
|
wb = openpyxl.load_workbook(EXCEL_FILE, data_only=True)
|
|
ws = wb['Tabelle2']
|
|
|
|
zahlungen = []
|
|
|
|
for row_idx in range(1, min(ws.max_row + 1, 150)):
|
|
datum_val = ws.cell(row=row_idx, column=1).value # Spalte A
|
|
notiz = ws.cell(row=row_idx, column=2).value # Spalte B
|
|
betrag = ws.cell(row=row_idx, column=3).value # Spalte C
|
|
|
|
if betrag is None or not isinstance(betrag, (int, float)):
|
|
continue
|
|
|
|
# Überspringe Ebase-Daten (Zeile 2-4)
|
|
if row_idx in [2, 3, 4]:
|
|
print(f"Zeile {row_idx}: Überspringe Ebase: {notiz} - {betrag}€")
|
|
continue
|
|
|
|
# Überspringe Gesammtbetrag (Zeile 23)
|
|
if row_idx == 23:
|
|
print(f"Zeile {row_idx}: Überspringe Gesammtbetrag: {betrag}€")
|
|
continue
|
|
|
|
datum = None
|
|
|
|
# Fall 1: Echtes Datum-Objekt in Spalte A
|
|
if isinstance(datum_val, datetime):
|
|
datum = datum_val
|
|
|
|
# Fall 2: Datum als String in Notiz-Spalte (z.B. "2021-08-01" oder "Überweisung 17.05.2021")
|
|
elif notiz and isinstance(notiz, str):
|
|
# Versuche Datum aus String zu extrahieren
|
|
# Pattern: YYYY-MM-DD
|
|
match = re.search(r'(\d{4})-(\d{2})-(\d{2})', notiz)
|
|
if match:
|
|
try:
|
|
datum = datetime.strptime(match.group(0), '%Y-%m-%d')
|
|
except:
|
|
pass
|
|
|
|
# Pattern: DD.MM.YYYY
|
|
if not datum:
|
|
match = re.search(r'(\d{2})\.(\d{2})\.(\d{4})', notiz)
|
|
if match:
|
|
try:
|
|
datum = datetime.strptime(match.group(0), '%d.%m.%Y')
|
|
except:
|
|
pass
|
|
|
|
if datum is None:
|
|
print(f"Zeile {row_idx}: KEIN DATUM gefunden - Notiz: {notiz}, Betrag: {betrag}€")
|
|
continue
|
|
|
|
# Bestimme Typ
|
|
if betrag < 0:
|
|
typ = 'monatsrate'
|
|
betrag = abs(betrag) # Mache positiv für API
|
|
else:
|
|
typ = 'auslage'
|
|
|
|
zahlungen.append({
|
|
'zeile': row_idx,
|
|
'datum': datum,
|
|
'betrag': betrag,
|
|
'typ': typ,
|
|
'notiz': notiz if notiz else f'Zeile {row_idx}'
|
|
})
|
|
|
|
print(f"\n{'='*80}")
|
|
print(f"INSGESAMT: {len(zahlungen)} Zahlungen zum Importieren")
|
|
print(f"{'='*80}")
|
|
|
|
# Zeige ersten 10
|
|
for z in zahlungen[:10]:
|
|
print(f" Zeile {z['zeile']}: {z['datum'].strftime('%d.%m.%Y')} - {z['betrag']}€ - {z['typ']} - {z['notiz'][:30]}")
|
|
|
|
if len(zahlungen) > 10:
|
|
print(f" ... und {len(zahlungen) - 10} weitere")
|
|
|
|
# Import via API
|
|
print(f"\n{'='*80}")
|
|
print("IMPORTIERE...")
|
|
print(f"{'='*80}")
|
|
|
|
erfolgreich = 0
|
|
fehler = 0
|
|
|
|
for z in zahlungen:
|
|
data = {
|
|
'datum': z['datum'].strftime('%Y-%m-%d'),
|
|
'betrag': z['betrag'],
|
|
'typ': z['typ'],
|
|
'notiz': z['notiz']
|
|
}
|
|
|
|
try:
|
|
response = requests.post(API_URL, json=data, timeout=10)
|
|
if response.status_code == 201:
|
|
erfolgreich += 1
|
|
print(f"✅ {z['datum'].strftime('%d.%m.%Y')} - {z['betrag']}€")
|
|
else:
|
|
fehler += 1
|
|
print(f"❌ {z['datum'].strftime('%d.%m.%Y')} - {z['betrag']}€: {response.status_code} - {response.text[:100]}")
|
|
except Exception as e:
|
|
fehler += 1
|
|
print(f"❌ {z['datum'].strftime('%d.%m.%Y')} - {z['betrag']}€: {e}")
|
|
|
|
print(f"\n{'='*80}")
|
|
print(f"ERLEDIGT: {erfolgreich} erfolgreich, {fehler} fehlgeschlagen")
|
|
print(f"{'='*80}")
|