Files
bolsa/php/compteJFG.php

180 lines
8.9 KiB
PHP
Raw Permalink 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.
<?php
declare(strict_types=1);
require_once __DIR__ . '/session.php';
require_once __DIR__ . '/db.php';
// Redirection si l'utilisateur n'est pas authentifié.
if (empty($_SESSION['user_id'])) {
header('Location: ../index.php');
exit;
}
$pageStylesheets = ['/css/compteJFG.css'];
$bodyClass = 'comptejfg-page';
$database = new App\Database\Database();
$compteJFGManager = new App\Database\CompteJFGManager($database);
$creationMessage = null;
$creationError = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['comptejfg_create'])) {
$newEntry = [
'date_operation' => trim($_POST['date_operation'] ?? ''),
'debit' => trim($_POST['debit'] ?? '0.00'),
'credit' => trim($_POST['credit'] ?? '0.00'),
'libelle' => trim($_POST['libelle'] ?? ''),
];
if ($newEntry['date_operation'] === '' || $newEntry['libelle'] === '') {
$creationError = 'Veuillez remplir la date et le libellé.';
} else {
if ($compteJFGManager->createCompteJFG($newEntry)) {
$creationMessage = 'Nouvelle écriture ajoutée avec succès.';
} else {
$creationError = 'Impossible dajouter l’écriture, veuillez réessayer.';
}
}
}
$updateMessage = null;
$updateError = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['comptejfg_update'], $_POST['comptejfg_id'])) {
$entryId = (int) $_POST['comptejfg_id'];
$updateData = [
'date_operation' => trim($_POST['date_operation'] ?? ''),
'debit' => trim($_POST['debit'] ?? '0.00'),
'credit' => trim($_POST['credit'] ?? '0.00'),
'libelle' => trim($_POST['libelle'] ?? ''),
];
if ($entryId <= 0 || $updateData['date_operation'] === '' || $updateData['libelle'] === '') {
$updateError = 'Tous les champs requis doivent être remplis pour mettre à jour cette écriture.';
} else {
if ($compteJFGManager->updateCompteJFG($entryId, $updateData)) {
$updateMessage = 'Écriture mise à jour avec succès.';
} else {
$updateError = 'Impossible de mettre à jour l’écriture. Vérifiez les données et réessayez.';
}
}
}
$entries = $compteJFGManager->listCompteJFG();
$totalCredit = 0.0;
$totalDebit = 0.0;
foreach ($entries as $entry) {
$totalCredit += (float) $entry['credit'];
$totalDebit += (float) $entry['debit'];
}
require_once __DIR__ . '/header.php';
?>
<section class="comptegf-shell">
<div class="comptegf-container">
<div class="comptegf-header">
<h1 class="comptegf-title">Compte JFG</h1>
<a href="/php/menu.php" class="comptegf-button">Retour au menu</a>
</div>
<?php if ($creationMessage): ?>
<div class="comptegf-card action-notice action-success"><?= htmlspecialchars($creationMessage, ENT_QUOTES, 'UTF-8') ?></div>
<?php endif; ?>
<?php if ($creationError): ?>
<div class="comptegf-card action-notice action-error"><?= htmlspecialchars($creationError, ENT_QUOTES, 'UTF-8') ?></div>
<?php endif; ?>
<?php if ($updateMessage): ?>
<div class="comptegf-card action-notice action-success"><?= htmlspecialchars($updateMessage, ENT_QUOTES, 'UTF-8') ?></div>
<?php endif; ?>
<?php if ($updateError): ?>
<div class="comptegf-card action-notice action-error"><?= htmlspecialchars($updateError, ENT_QUOTES, 'UTF-8') ?></div>
<?php endif; ?>
<section class="comptegf-panel" id="formulaire-creation">
<form id="comptejfg-form" method="post" action="" autocomplete="off">
<input type="hidden" name="comptejfg_id" value="">
<div class="comptegf-form-row">
<label class="field-date_operation">
Date opération
<input id="comptejfg-date_operation" type="date" name="date_operation" required>
</label>
<label class="field-libelle">
Libellé
<input id="comptejfg-libelle" type="text" name="libelle" placeholder="Description" required>
</label>
<label class="field-credit">
Crédit
<input id="comptejfg-credit" type="number" name="credit" step="0.01" min="0" value="0.00" required>
</label>
<label class="field-debit">
Débit
<input id="comptejfg-debit" type="number" name="debit" step="0.01" min="0" value="0.00" required>
</label>
</div>
<div id="comptejfg-form-feedback" class="form-error" aria-live="polite" style="display:none;"></div>
<div class="comptegf-panel-footer">
<button type="submit" name="comptejfg_create" class="btn-action btn-primary create-button">Créer une écriture</button>
<button type="submit" name="comptejfg_update" class="btn-action btn-primary update-button" hidden>Modifier l'écriture</button>
<button type="button" class="btn-action btn-cancel cancel-edit-button" hidden>Annuler</button>
</div>
</form>
</section>
<section class="comptegf-card table-container">
<div class="comptegf-table-wrapper">
<table class="comptegf-table">
<thead>
<tr class="totals-row">
<th colspan="2" class="totals-label">Totaux :</th>
<th class="numeric-cell totals-value"><?= htmlspecialchars(number_format($totalCredit, 2, ',', ' '), ENT_QUOTES, 'UTF-8') ?></th>
<th class="numeric-cell totals-value"><?= htmlspecialchars(number_format($totalDebit, 2, ',', ' '), ENT_QUOTES, 'UTF-8') ?></th>
<th class="numeric-cell totals-value"><?= htmlspecialchars(number_format($totalCredit - $totalDebit, 2, ',', ' '), ENT_QUOTES, 'UTF-8') ?></th>
</tr>
<tr>
<th>Date opération</th>
<th>Libellé</th>
<th>Crédit</th>
<th>Débit</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($entries as $entry): ?>
<?php
$formattedDate = $entry['date_operation'];
if (!empty($entry['date_operation'])) {
$dateTime = DateTime::createFromFormat('Y-m-d', $entry['date_operation']);
if ($dateTime !== false) {
$formattedDate = $dateTime->format('d/m/Y');
}
}
?>
<tr class="comptegf-row"
data-id="<?= htmlspecialchars((string) $entry['id'], ENT_QUOTES, 'UTF-8') ?>"
data-date_operation="<?= htmlspecialchars((string) $entry['date_operation'], ENT_QUOTES, 'UTF-8') ?>"
data-debit="<?= htmlspecialchars((string) $entry['debit'], ENT_QUOTES, 'UTF-8') ?>"
data-credit="<?= htmlspecialchars((string) $entry['credit'], ENT_QUOTES, 'UTF-8') ?>"
data-libelle="<?= htmlspecialchars((string) $entry['libelle'], ENT_QUOTES, 'UTF-8') ?>"
>
<td><?= htmlspecialchars((string) $formattedDate, ENT_QUOTES, 'UTF-8') ?></td>
<td><?= htmlspecialchars((string) $entry['libelle'], ENT_QUOTES, 'UTF-8') ?></td>
<td class="numeric-cell"><?= htmlspecialchars((string) $entry['credit'], ENT_QUOTES, 'UTF-8') ?></td>
<td class="numeric-cell"><?= htmlspecialchars((string) $entry['debit'], ENT_QUOTES, 'UTF-8') ?></td>
<td>
<button type="button" class="btn-action btn-edit edit-comptejfg-button">Modifier</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</section>
</div>
</section>
<script src="/js/compteJFG.js" defer></script>
<?php require_once __DIR__ . '/footer.php';