1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| <?php
try{
$bdd=new PDO('mysql:host=localhost;dbname=pcg;charset=utf8', 'root','');
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);// afficher les erreurs php
}catch(Exception $e){
die('Erreur :' . $e->getMessage());
}
$response = $bdd->query('SELECT * FROM ecriturescomptables');
$ecriturescomptables = $response->fetchAll();
$response->closeCursor();
if(!empty($_POST)){
if(empty($erreurs)){
$dateoperation = $_POST['date_operation'];
$journal = $_POST['journal'];
$compte = $_POST['numerocompte'];
$libelle = $_POST['libelle_comptable'];
$debit = $_POST['debit'];
$credit = $_POST['credit'];
}
}
if (!empty($_POST)) {
// On traite le formulaire
$errors = [];
// On valide les données champs par champ
$journal = '';
if (!empty($_POST['journal'])){
if(!preg_match('#^[a-zA-Z\-\'ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ ]{1,50}$#', $_POST['journal'])) {
$errors[] = 'Champ journal invalide';
} else {
$nom = $_POST['journal'];
}
} else {
$errors[] = 'Champ journal non renseigné';
}
// on n'utilise pas !empty pour gérer le cas où la valeur est 0
$compte = '';
if (!empty($_POST['numerocompte'])){
if(!preg_match('#^[0-9]{1,10}?$#', $_POST['numerocompte'])) {
$errors[] = 'Champ numéro de compte invalide';
} else {
$compte = $_POST['numerocompte'];
}
} else {
$errors[] = 'Champ compte non renseigné';
}
$libelle = '';
if (!empty($_POST['libelle_comptable'])){
if(!preg_match('#^[a-zA-Z\-\'ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ ]{1,50}$#', $_POST['libelle_comptable'])) {
$errors[] = 'Champ libellé invalide';
} else {
$libelle = $_POST['libelle_comptable'];
}
} else {
$errors[] = 'Champ libellé non renseigné';
}
$debit = '';
if (isset($_POST['debit'])){
if(!preg_match('#^[0-9]{1,10}([.,][0-9]{1,2})?$#', $_POST['debit'])) {
$errors[] = 'Champ débit invalide';
} else {
$debit = str_replace(',', '.', $_POST['debit']);
}
} else {
$errors[] = 'Champ débit non renseigné';
}
$credit = '';
if (isset($_POST['credit'])){
if(!preg_match('#^[0-9]{1,10}([.,][0-9]{1,2})?$#', $_POST['credit'])) {
$errors[] = 'Champ crédit invalide';
} else {
$crebit = str_replace(',', '.', $_POST['credit']);
}
} else {
$errors[] = 'Champ crédit non renseigné';
}
// On insère si il n'y a pas d'erreurs :
if(empty($errors)){
// insertion dans la BDD
$ecriturescomptables = $bdd->prepare("INSERT INTO ecriturescomptables(date_operation, journal, numerocompte, libelle_comptable, debit, credit) VALUES(?,?,?,?,?,?)");
$ecriturescomptables->execute(array(
$_POST['date_operation'],
$_POST['journal'],
$_POST['numerocompte'],
$_POST['libelle_comptable'],
$_POST['debit'],
$_POST['credit']));
if($ecriturescomptables->rowCount()>0){
$successMsg = 'L\'écriture a bien été ajouté !';
}else{
$errors[] = 'Suite à un problème dans la base de données, le client douteux n\'a pas pu être ajouté !';
}
$ecriturescomptables->closeCursor();
}
}
//Calcul des totaux débit et crédit et solde respectif
$response1=$bdd->query('SELECT SUM(debit) AS soldedebiteur, SUM(credit) AS soldecrediteur FROM ecriturescomptables');
$totaux=$response1->fetchAll();
if(!empty($_POST)){
echo'<script>window.location.href("ecriturescomptables.php")</script>';
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/bootstrap.css">
<link rel="stylesheet" href="../css/style.css">
<link rel="stylesheet" href="../css/carousel.css">
<title>Comptabilité</title>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<body>
<?php
if (!empty($errors)) :
foreach ($errors as $error) :
?>
<div class="alert alert-danger" role="alert"><?php echo $error;?></div>
<?php
endforeach;
endif;
?>
<?php
if (!empty($successMsg)) :
?>
<div class="alert alert-success" role="alert"><?php echo $successMsg;?></div>
<?php
endif;
?>
<div id="ecriturescomptables">
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1 style="font-size:25px;text-align:center;color:black;">Journal des écritures</h1>
<form name="ecriturescomptables" method="POST" action="ecriturescomptables.php">
<input name="date_operation" id="date_operation" type="date" placeholder="date">
<input name="journal" id="journal" type="text" placeholder="journal">
<input name="numerocompte" id="numerocompte" type="text" placeholder="numéro de compte">
<input name="libelle_comptable" id="libelle_comptable" type="text" placeholder="libellé">
<input name="debit" id="debit" type="text" placeholder="débit">
<input name="credit" id="credit" type="text" placeholder="crédit">
<input type="submit" value="Envoyer">
</form><br>
<?php
if (!empty($ecriturescomptables)){
$response = $bdd->query('SELECT * FROM ecriturescomptables');
$ecriturescomptables = $response->fetchAll();
setlocale(LC_ALL, ['fr_FR.UTF8','fra', 'french']);
}
?>
<div>
<table>
<thead>
<tr>
<th>Date de l'opération</th>
<th>Journal</th>
<th>N° de compte</th>
<th>Libellé</th>
<th>Débit</th>
<th>Crédit</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($ecriturescomptables)){
foreach($ecriturescomptables as $data){
$dateoperation = new DateTime($data['date_operation']);
$debit = htmlspecialchars($data['debit']);
$credit = htmlspecialchars($data['credit']);
if ($debit == 0){
echo '<tr><td>'.utf8_encode(strftime('%d %B %Y', $dateoperation->format('U'))).'</td><td>'.htmlspecialchars($data['journal']).'</td><td>'.htmlspecialchars($data['numerocompte']).'</td><td>'.htmlspecialchars($data['libelle_comptable']).'</td><td>'.''.'</td><td>'.number_format($credit, 2, ',', ' ').'</td></tr>';
}else {
echo '<tr><td>'.utf8_encode(strftime('%d %B %Y', $dateoperation->format('U'))).'</td><td>'.htmlspecialchars($data['journal']).'</td><td>'.htmlspecialchars($data['numerocompte']).'</td><td>'.htmlspecialchars($data['libelle_comptable']).'</td><td>'.number_format($debit, 2, ',', ' ').'</td><td>'.''.'</td></tr>';
}
}
}
?>
</tbody>
<tfoot>
<?php
if (!empty($totaux)){
foreach($totaux as $total){
$totaldebit = $total['soldedebiteur'];
$totalcredit = $total['soldecrediteur'];
echo '<tr><td class="bg">'.''.'</td><td class="bg">'.''.'</td><td class="bg">'.''.'</td><td class="bg">'.''.'</td><td class="totaux">'.number_format($totaldebit, 2, ',', ' ').'</td><td class="totaux">'.number_format($totalcredit, 2, ',', ' ').'</td></tr>';
}
}
?>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
<script type="application/javascript" src="js/jquery-3.3.1.min.js"></script>
<script src="js/script.js"></script>
</body>
</html> |