370 lines
18 KiB
PHP
370 lines
18 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/session.php';
|
|
require_once __DIR__ . '/db.php';
|
|
|
|
if (empty($_SESSION['user_id'])) {
|
|
header('Location: ../index.php');
|
|
exit;
|
|
}
|
|
|
|
/*A supprimer quand la page fonctionnera correctement*/
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
/**************************************************** */
|
|
|
|
|
|
|
|
$pageStylesheets = ['/css/ordreGF.css'];
|
|
$bodyClass = 'ordregf-page';
|
|
|
|
$database = new App\Database\Database();
|
|
$connection = $database->getConnection();
|
|
|
|
function fetchOrdreGF(PDO $connection): array
|
|
{
|
|
$stmt = $connection->query('SELECT * FROM `ordreGF` ORDER BY `ID` DESC');
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
|
|
}
|
|
|
|
function fetchOrdreGFPied(PDO $connection, string $masterId): array
|
|
{
|
|
$stmt = $connection->prepare('SELECT * FROM `ordreGF_pied` WHERE `Id_entete` = :masterId ORDER BY `Id` ASC');
|
|
$stmt->execute([':masterId' => $masterId]);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
|
|
}
|
|
|
|
$ordres = fetchOrdreGF($connection);
|
|
|
|
require_once __DIR__ . '/header.php';
|
|
?>
|
|
|
|
<section class="ordregf-shell">
|
|
<div class="ordregf-card">
|
|
<header class="ordregf-card-header" style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 28px;">
|
|
<h1 class="ordregf-title">Gestion des ordres GF</h1>
|
|
<div style="display: flex; gap: 10px;">
|
|
<button type="button" class="ordregf-btn-creer" onclick="document.getElementById('modalCreerOrdre').showModal();">Créer un ordre</button>
|
|
<button type="button" class="ordregf-btn-menu" onclick="window.location.href='/php/menu.php';">Retour au menu</button>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="ordregf-table-wrapper">
|
|
<table class="ordregf-table">
|
|
<tbody>
|
|
<?php foreach ($ordres as $ordre): ?>
|
|
<?php
|
|
$details = fetchOrdreGFPied($connection, (string)$ordre['ID']);
|
|
$totalQ = 0;
|
|
|
|
// Détection d'un ordre annulé
|
|
$isCanceled = false;
|
|
|
|
foreach ($details as $d) {
|
|
if (strtolower($d['Etat'] ?? '') === 'annulé') {
|
|
$isCanceled = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
// 1. Calcul du total des quantités actives
|
|
foreach ($details as $d) {
|
|
if (strtolower($d['Etat'] ?? '') === 'actif') {
|
|
$q = (int)($d['Quantite'] ?? 0);
|
|
$oType = strtolower($d['Ordre'] ?? '');
|
|
$tOp = strtolower($d['Type'] ?? '');
|
|
$signe = ($tOp === 'long') ? ($oType === 'achat' ? 1 : -1) : ($oType === 'achat' ? -1 : 1);
|
|
$totalQ += ($q * $signe);
|
|
}
|
|
}
|
|
|
|
|
|
// 2. Calcul Plus-Value et Mise à jour statut
|
|
$plusvalue = 0.0000;
|
|
|
|
if ($isCanceled) {
|
|
$plusvalue = 0;
|
|
|
|
if ((float)$ordre['PlusValue'] != 0) {
|
|
$connection->prepare(
|
|
"UPDATE ordreGF SET PlusValue = 0 WHERE ID = ?"
|
|
)->execute([$ordre['ID']]);
|
|
|
|
$ordre['PlusValue'] = 0;
|
|
}
|
|
}
|
|
|
|
|
|
$isSold = (!$isCanceled && $totalQ === 0 && count($details) > 0);
|
|
|
|
if ($isSold) {
|
|
foreach ($details as $detail) {
|
|
$qte = (int)$detail['Quantite'];
|
|
$fraisDetail = (float)$detail['Frais_brocker'] + (float)$detail['Frais_etat'] + (float)$detail['Frais_autre'];
|
|
$oType = strtolower($detail['Ordre']);
|
|
$prixNet = ($oType === 'vente') ? (float)$detail['Prix_brut'] - $fraisDetail : (float)$detail['Prix_brut'] + $fraisDetail;
|
|
$tOp = strtolower($detail['Type']);
|
|
$signePV = ($oType === 'vente') ? 1 : -1;
|
|
$plusvalue += ($qte * $prixNet * $signePV);
|
|
}
|
|
$plusvalue = round($plusvalue, 4);
|
|
|
|
// Mise à jour de la plus-value uniquement si nécessaire
|
|
if (abs((float)$ordre['PlusValue'] - $plusvalue) > 0.0001) {
|
|
$connection->prepare(
|
|
"UPDATE ordreGF SET PlusValue = ? WHERE ID = ?"
|
|
)->execute([$plusvalue, $ordre['ID']]);
|
|
|
|
$ordre['PlusValue'] = $plusvalue;
|
|
}
|
|
|
|
// Si la quantité totale est à zéro, toutes les lignes actives passent en soldé
|
|
$connection->prepare(
|
|
"UPDATE ordreGF_pied
|
|
SET Etat = 'soldé'
|
|
WHERE Id_entete = ?
|
|
AND Etat = 'actif'"
|
|
)->execute([$ordre['ID']]);
|
|
|
|
// Recharge les données après modification
|
|
$details = fetchOrdreGFPied($connection, (string)$ordre['ID']);
|
|
}
|
|
|
|
// Désactivation si ordre soldé ou annulé
|
|
$isDisabled = $isCanceled || abs((float)$ordre['PlusValue']) > 0.0001;
|
|
|
|
$disabledAttr = $isDisabled
|
|
? 'disabled style="opacity:0.5; cursor:not-allowed;"'
|
|
: '';
|
|
?>
|
|
|
|
<tr class="ordregf-master-row ordregf-header-style">
|
|
<td colspan="11" style="font-weight: bold; font-size: 1.1em; color: #fff;">
|
|
<?php echo htmlspecialchars((string)$ordre['isin']) . ' --- ' . htmlspecialchars((string)$ordre['Nom']) . ' ( ' . htmlspecialchars((string)$ordre['ticker']) . ' )'; ?>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr class="ordregf-detail-block">
|
|
<td colspan="11">
|
|
<table class="ordregf-detail-table">
|
|
<thead>
|
|
<tr>
|
|
<th>Date</th>
|
|
<th>Qté</th>
|
|
<th>Prix Brut</th>
|
|
<th>Frais</th>
|
|
<th>Prix Net</th>
|
|
<th>Eng. Brut</th>
|
|
<th>Eng. Net</th>
|
|
<th>Ordre</th>
|
|
<th>Type</th>
|
|
<th>Etat</th>
|
|
<th style="text-align: right;"><button type="button" class="ordregf-btn-creer-petit ordregf-btn-petit" <?= $disabledAttr ?> onclick="document.getElementById('Id_entete_detail').value='<?= $ordre['ID'] ?>'; document.getElementById('modalCreerDetail').showModal();">Créer</button></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
$engaBrut = 0;
|
|
$engaNet = 0;
|
|
foreach ($details as $detail):
|
|
$qte = (int)($detail['Quantite'] ?? 0);
|
|
$prix = (float)($detail['Prix_brut'] ?? 0);
|
|
$frais = (float)($detail['Frais_brocker'] ?? 0) + (float)($detail['Frais_etat'] ?? 0) + (float)($detail['Frais_autre'] ?? 0);
|
|
$ordreType = strtolower($detail['Ordre'] ?? '');
|
|
$prixNet = ($ordreType === 'vente') ? $prix - $frais : $prix + $frais;
|
|
$date = !empty($detail['Date_op']) ? date('d/m/Y', strtotime($detail['Date_op'])) : '--/--/----';
|
|
if (strtolower($detail['Etat'] ?? '') === 'actif') {
|
|
$mult = (strtolower($detail['Type'] ?? '') === 'long' ? 1 : -1) * (strtolower($detail['Ordre'] ?? '') === 'achat' ? 1 : -1);
|
|
$engaBrut += ($qte * $prix) * $mult;
|
|
$engaNet += ($qte * $prixNet) * $mult;
|
|
}
|
|
?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($date) ?></td>
|
|
<td class="text-right"><?= $qte ?></td>
|
|
<td class="text-right"><?= number_format($prix, 3, ',', ' ') ?></td>
|
|
<td class="text-right"><?= number_format($frais, 3, ',', ' ') ?></td>
|
|
<td class="text-right"><?= number_format($prixNet, 3, ',', ' ') ?></td>
|
|
<td class="text-right"><?= number_format($qte * $prix, 3, ',', ' ') ?></td>
|
|
<td class="text-right"><?= number_format($qte * $prixNet, 3, ',', ' ') ?></td>
|
|
<td style="text-align: center"><?= htmlspecialchars($detail['Ordre'] ?? '') ?></td>
|
|
<td style="text-align: center"><?= htmlspecialchars($detail['Type'] ?? '') ?></td>
|
|
<td style="text-align: center"><?= htmlspecialchars($detail['Etat'] ?? '') ?></td>
|
|
<td style="text-align: center"><button type=" button" class="ordregf-btn-petit ordregf-btn-modifier-petit" <?= $disabledAttr ?> data-detail='<?= htmlspecialchars(json_encode($detail), ENT_QUOTES, 'UTF-8') ?>'>Modifier</button></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
|
|
<?php if ($isDisabled): ?>
|
|
<tr style="background-color: rgba(255, 255, 255, 0.08); font-weight: bold;">
|
|
<td colspan="6" style="text-align: right;">Plus value :</td>
|
|
<td style="color: <?= (float)$ordre['PlusValue'] < 0 ? 'red' : 'green' ?>;"><?= number_format((float)$ordre['PlusValue'], 3, ',', ' ') ?></td>
|
|
<td colspan="4"></td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<tr style="background-color: rgba(255, 255, 255, 0.08); font-weight: bold;">
|
|
<td style="text-align: right;">TOTAL :</td>
|
|
<td class="text-right"><?= number_format($totalQ, 0, ',', ' ') ?></td>
|
|
<?php if ($totalQ == 0): ?>
|
|
<td class="text-right">0</td>
|
|
<td class="text-right">0</td>
|
|
<td class="text-right">0</td>
|
|
<?php else: ?>
|
|
<td class="text-right"><?= number_format($engaBrut / $totalQ, 3, ',', ' ') ?></td>
|
|
<td class="text-right"><?= number_format($engaNet / $totalQ - $engaBrut / $totalQ, 3, ',', ' ') ?></td>
|
|
<td class="text-right"><?= number_format($engaNet / $totalQ, 3, ',', ' ') ?></td>
|
|
<?php endif; ?>
|
|
<td class="text-right"><?= number_format($engaBrut, 3, ',', ' ') ?></td>
|
|
<td class="text-right"><?= number_format($engaNet, 3, ',', ' ') ?></td>
|
|
<td colspan="4"></td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- Modals -->
|
|
<dialog id="modalCreerOrdre" class="ordregf-modal">
|
|
<form id="formCreerOrdre">
|
|
<h2>Créer un nouvel ordre</h2>
|
|
<div class="form-group">
|
|
<label>ISIN</label>
|
|
<input type="text" id="isinRecherche" name="isin" required>
|
|
<button type="button" id="btnRechercher">Rechercher</button>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Nom :</label>
|
|
<input type="text" name="Nom" id="Nom" readonly>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Ticker :</label>
|
|
<input type="text" name="ticker" id="ticker" readonly>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Code Yahoo :</label>
|
|
<input type="text" name="code_yahoo" id="code_yahoo" readonly>
|
|
</div>
|
|
<div class="modal-actions" style="margin-top: 20px;">
|
|
<button type="button" id="btnAnnuler">Annuler</button>
|
|
<button type="submit" id="btnValider" disabled>Valider</button>
|
|
</div>
|
|
</form>
|
|
</dialog>
|
|
|
|
<dialog id="modalCreerDetail" class="ordregf-modal">
|
|
<form id="formCreerDetail">
|
|
<input type="hidden" name="Id_entete" id="Id_entete_detail">
|
|
<h2>Ajouter un mouvement</h2>
|
|
<div class="form-group">
|
|
<label>Date :</label>
|
|
<input type="date" name="Date_op" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Quantité :</label>
|
|
<input type="number" name="Quantite" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Prix Brut :</label>
|
|
<input type="number" step="0.0001" name="Prix_brut" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Frais</label>
|
|
<div class="triple">
|
|
<input type="number" step="0.0001" name="Frais_brocker" placeholder="Broker">
|
|
<input type="number" step="0.0001" name="Frais_etat" placeholder="État">
|
|
<input type="number" step="0.0001" name="Frais_autre" placeholder="Autre">
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Ordre (Achat/Vente) :</label>
|
|
<select name="Ordre">
|
|
<option value="achat">achat</option>
|
|
<option value="vente">vente</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Type (Long/Short) :</label>
|
|
<select name="Type">
|
|
<option value="long">long</option>
|
|
<option value="short">short</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Etat :</label>
|
|
<select name="Etat">
|
|
<option value="actif">actif</option>
|
|
<option value="annulé">annulé</option>
|
|
</select>
|
|
</div>
|
|
<div class="modal-actions" style="margin-top: 20px;">
|
|
<button type="button" onclick="document.getElementById('modalCreerDetail').close()">Annuler</button>
|
|
<button type="submit">Valider</button>
|
|
</div>
|
|
</form>
|
|
</dialog>
|
|
|
|
<dialog id="modalModifierDetail" class="ordregf-modal">
|
|
<form id="formModifierDetail">
|
|
<input type="hidden" name="id" id="edit_id">
|
|
<input type="hidden" name="Id_entete" id="edit_Id_entete">
|
|
<h2>Modifier le mouvement</h2>
|
|
<div class="form-group">
|
|
<label>Date :</label>
|
|
<input type="date" name="Date_op" id="edit_date" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Quantité :</label>
|
|
<input type="number" name="Quantite" id="edit_quantite" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Prix Brut :</label>
|
|
<input type="number" step="0.0001" name="Prix_brut" id="edit_prix" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Frais</label>
|
|
<div class="triple">
|
|
<input type="number" step="0.0001" name="Frais_brocker" id="edit_frais_broker" placeholder="Broker">
|
|
<input type="number" step="0.0001" name="Frais_etat" id="edit_frais_etat" placeholder="État">
|
|
<input type="number" step="0.0001" name="Frais_autre" id="edit_frais_autre" placeholder="Autre">
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Ordre :</label>
|
|
<select name="Ordre" id="edit_ordre">
|
|
<option value="achat">achat</option>
|
|
<option value="vente">vente</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Type :</label>
|
|
<select name="Type" id="edit_type">
|
|
<option value="long">long</option>
|
|
<option value="short">short</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Etat :</label>
|
|
<select name="Etat" id="edit_etat">
|
|
<option value="actif">actif</option>
|
|
<option value="annulé">annulé</option>
|
|
</select>
|
|
</div>
|
|
<div class="modal-actions" style="margin-top: 20px;">
|
|
<button type="button" onclick="document.getElementById('modalModifierDetail').close()">Annuler</button>
|
|
<button type="submit">Enregistrer</button>
|
|
</div>
|
|
</form>
|
|
</dialog>
|
|
|
|
<script src="/js/ordreGF.js" defer></script>
|
|
|
|
<?php require_once __DIR__ . '/footer.php'; ?>
|