Files
2026-07-02 14:15:39 +00:00

126 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/session.php';
require_once __DIR__ . '/db.php';
use App\Database\Database;
use App\Database\ActionsManager;
if (empty($_SESSION['user_id'])) {
header('Location: ../index.php');
exit;
}
$pageStylesheets = ['/css/csv.css'];
$pageScripts = ['/js/csv.js'];
$bodyClass = 'csv-page';
$database = new Database();
$actionsManager = new ActionsManager($database);
$csvFilePath = __DIR__ . '/../assets/csv/actions.csv';
$csvRecords = [];
$csvReadError = null;
$csvExists = is_readable($csvFilePath);
if ($csvExists) {
$handle = fopen($csvFilePath, 'r');
if ($handle === false) {
$csvExists = false;
$csvReadError = 'Impossible de lire le fichier CSV.';
} else {
$header = fgetcsv($handle);
while (($row = fgetcsv($handle)) !== false) {
if (count($row) < 4) {
continue;
}
$isin = trim((string) ($row[0] ?? ''));
if ($isin === '') {
continue;
}
$csvRecords[$isin] = [
'ticker' => trim((string) ($row[1] ?? '')),
'yahoo_code' => trim((string) ($row[2] ?? '')),
'company_name' => trim((string) ($row[3] ?? '')),
];
}
fclose($handle);
}
}
$actions = $actionsManager->listActions();
$differences = [];
foreach ($actions as $action) {
$isin = trim((string) ($action['isin'] ?? ''));
if ($isin === '') {
continue;
}
$expected = [
'ticker' => trim((string) ($action['ticker'] ?? '')),
'yahoo_code' => trim((string) ($action['yahoo_code'] ?? '')),
'company_name' => trim((string) ($action['company_name'] ?? '')),
];
if (!isset($csvRecords[$isin])) {
$differences[] = ['isin' => $isin, 'reason' => 'absent'];
continue;
}
$csvRow = $csvRecords[$isin];
foreach ($expected as $field => $value) {
if ($csvRow[$field] !== $value) {
$differences[] = ['isin' => $isin, 'reason' => $field];
break;
}
}
}
$csvReady = $csvExists && empty($differences);
if ($csvReadError !== null) {
$statusMessage = 'Impossible de vérifier le fichier actions.csv : ' . $csvReadError;
} elseif ($csvReady) {
$statusMessage = 'Le fichier actions.csv est à jour.';
} else {
$statusMessage = 'Le fichier actions.csv peut être mis à jour.';
if ($csvExists) {
$statusMessage .= ' ' . count($differences) . ' différence(s) détectée(s).';
}
}
require_once __DIR__ . '/header.php';
?>
<section class="csv-shell">
<div class="csv-card">
<div class="csv-content">
<h1>Synchronisation CSV</h1>
<p class="csv-status <?= $csvReady ? 'status-ok' : 'status-warning' ?>"><?= htmlspecialchars($statusMessage, ENT_QUOTES, 'UTF-8') ?></p>
</div>
<div class="csv-actions">
<?php if ($csvReady): ?>
<a href="/php/menu.php" class="btn-action btn-primary">Retour au menu</a>
<?php else: ?>
<button id="csv-update-yes" type="button" class="btn-action btn-primary">Mettre à jour</button>
<button id="csv-update-no" type="button" class="btn-action btn-secondary">Retour au menu</button>
<?php endif; ?>
<?php if ($csvExists): ?>
<a id="csv-download-link" href="/php/download_csv.php" class="btn-action btn-secondary">Télécharger CSV</a>
<?php endif; ?>
</div>
<div id="csv-feedback" class="csv-feedback" role="status" aria-live="polite"></div>
</div>
</section>
<script src="/js/csv.js"></script>
<?php require_once __DIR__ . '/footer.php';