FSCash/html/update_kurse.php
fds2610-bot 6f377ea251 V.2.2: Unit-Tests für wichtige Funktionen, automatisch geprüft bei Update
Extrahiert die wichtigste DB-/netzwerkfreie Geschäftslogik (Monatsnavigation,
Admin-/Aktien-Rollenprüfung, Aktien-GuV-Berechnung, OpenFIGI-/Alpha-Vantage-
Antwortauswertung, Eingabevalidierung, CSV-Export, Migrations-Logik) nach
html/lib/fscash.php und deckt sie mit 31 PHPUnit-Tests ab. update.sh
installiert Composer-Abhängigkeiten bei Bedarf und führt die Tests jetzt vor
jedem Deploy aus - bei Fehlschlag wird nichts kopiert oder migriert.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-23 17:54:30 +02:00

102 lines
2.3 KiB
PHP

<?php
require 'config.php';
require 'get_symbol.php';
require_once 'lib/fscash.php';
try {
// Jetzt auch symbol mit abrufen!
$stmt = $pdo->prepare("
SELECT id, WKNISIN, symbol
FROM aktien
WHERE WKNISIN IS NOT NULL
");
$stmt->execute();
$aktien = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($aktien as $aktie) {
$id = $aktie['id'];
$isin = trim($aktie['WKNISIN']);
$symbol = $aktie['symbol'];
// 🔹 Wenn noch kein Symbol vorhanden → über OpenFIGI holen
if (empty($symbol)) {
echo "Hole Symbol für ISIN $isin...\n";
$symbol = get2symbol($isin, $openFigiApiKey);
if ($symbol) {
// Symbol in DB speichern
$symbolUpdate = $pdo->prepare("
UPDATE aktien
SET symbol = :symbol
WHERE id = :id
");
$symbolUpdate->bindParam(':symbol', $symbol);
$symbolUpdate->bindParam(':id', $id);
$symbolUpdate->execute();
echo "Symbol $symbol gespeichert.\n";
// OpenFIGI Limit beachten
sleep(1);
} else {
echo "Kein Symbol für ISIN $isin gefunden. Überspringe...\n";
continue;
}
}
// 🔹 Kurs über Alpha Vantage abrufen
$url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=$symbol&apikey=$alphaApiKey";
// echo " rufe: $url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, true);
$kurs = fscash_extract_quote_price($data ?? []);
if ($kurs !== null) {
$kursdatum = date('Y-m-d');
// 🔹 Kurs UND Symbol speichern
$updateStmt = $pdo->prepare("
UPDATE aktien
SET kurs = :kurs,
kursdatum = :kursdatum,
symbol = :symbol
WHERE id = :id
");
$updateStmt->bindParam(':kurs', $kurs);
$updateStmt->bindParam(':kursdatum', $kursdatum);
$updateStmt->bindParam(':symbol', $symbol);
$updateStmt->bindParam(':id', $id);
$updateStmt->execute();
echo "Aktie ID $id ($isin / $symbol): Kurs auf $kurs aktualisiert.\n";
} else {
echo "Kurs für $symbol konnte nicht abgerufen werden.\n";
}
// Alpha Vantage Limit: 5 Anfragen / Minute
sleep(13);
}
echo "Kursaktualisierung abgeschlossen.\n";
} catch (PDOException $e) {
echo "Datenbankfehler: " . $e->getMessage() . "\n";
} catch (Exception $e) {
echo "Fehler: " . $e->getMessage() . "\n";
}
?>