banque.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. class banque
  3. {
  4. public static function getInitial(int $_id)
  5. {
  6. $tmp = self::getInitialCompte($_id );
  7. $return = array(
  8. "num" => 0,
  9. "id" => 0,
  10. "date" => $tmp["solde_date"],
  11. "label" => "SOLDE CREDITEUR AU " . core::convertDate($tmp["solde_date"], FALSE),
  12. "debit" => 0,
  13. "credit" => 0,
  14. "solde" => $tmp["solde"],
  15. );
  16. return $return;
  17. }
  18. public static function getInitialCompte(int $_id)
  19. {
  20. db::query("SELECT "
  21. . "* "
  22. . "FROM " . DB_T_BANQUE_COMPTES . " "
  23. . "WHERE " . DB_T_BANQUE_COMPTES . ".id = :id");
  24. db::bind(':id', $_id);
  25. return db::single();
  26. }
  27. public static function getLignes(int $_id)
  28. {
  29. db::query("SELECT "
  30. . "ROW_NUMBER() OVER (ORDER BY id) num, "
  31. . "" . DB_T_BANQUE_LIGNES . ".id, "
  32. . "" . DB_T_BANQUE_LIGNES . ".date, "
  33. . "" . DB_T_BANQUE_LIGNES . ".label, "
  34. . "" . DB_T_BANQUE_LIGNES . ".debit, "
  35. . "" . DB_T_BANQUE_LIGNES . ".credit, "
  36. . "" . DB_T_BANQUE_LIGNES . ".solde "
  37. . "FROM " . DB_T_BANQUE_LIGNES . " "
  38. . "WHERE " . DB_T_BANQUE_LIGNES . ".id_compte = :id_compte");
  39. db::bind(':id_compte', $_id);
  40. return db::resultset();
  41. }
  42. public static function getEtatCompte(int $_id)
  43. {
  44. db::query("SELECT "
  45. . "" . DB_T_BANQUE_LIGNES . ".solde, "
  46. . "" . DB_T_BANQUE_LIGNES . ".date "
  47. . "FROM " . DB_T_BANQUE_LIGNES . " "
  48. . "WHERE " . DB_T_BANQUE_LIGNES . ".id_compte = :id_compte "
  49. . "ORDER BY " . DB_T_BANQUE_LIGNES . ".id DESC");
  50. db::bind(':id_compte', $_id);
  51. return db::single();
  52. }
  53. public static function getHistoriqueCSV()
  54. {
  55. db::query("SELECT "
  56. . "" . DB_T_FILES . ".id, "
  57. . "" . DB_T_FILES . ".name, "
  58. . "" . DB_T_FILES . ".size, "
  59. . "" . DB_T_FILES . ".creer, "
  60. . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user' "
  61. . "FROM " . DB_T_FILES . " "
  62. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_FILES . ".id_user = " . DB_T_USER . ".id "
  63. . "INNER JOIN " . DB_T_BANQUE_CSV . " ON " . DB_T_BANQUE_CSV . ".md5 = " . DB_T_FILES . ".id");
  64. return db::resultset();
  65. }
  66. public static function addMd5CSV(string $_md5, int $_id_compte)
  67. {
  68. db::query("INSERT INTO " . DB_T_BANQUE_CSV . " (md5, id_compte) VALUES (:md5, :id_compte)");
  69. db::bind(':md5', $_md5);
  70. db::bind(':id_compte', $_id_compte);
  71. try {
  72. db::execute();
  73. return TRUE;
  74. } catch (Exception $ex) {
  75. return FALSE;
  76. }
  77. }
  78. public static function findMd5(string $_md5)
  79. {
  80. db::query("SELECT "
  81. . "id "
  82. . "FROM " . DB_T_BANQUE_LIGNES . " "
  83. . "WHERE " . DB_T_BANQUE_LIGNES . ".md5 = :md5");
  84. db::bind(':md5', $_md5);
  85. if(empty(db::single()["id"])){
  86. return FALSE;
  87. } else {
  88. return TRUE;
  89. }
  90. return db::single()["id"];
  91. }
  92. public static function recordLigne(array $_array, int $_id){
  93. if(self::findMd5($_array["md5"]) == FALSE){
  94. db::query("INSERT INTO " . DB_T_BANQUE_LIGNES . " (id_compte, date, label, debit, credit, solde, md5) VALUES (:id_compte, :date, :label, :debit, :credit, :solde, :md5)");
  95. db::bind(':id_compte', $_id);
  96. db::bind(':date', $_array["date"]);
  97. db::bind(':label', $_array["label"]);
  98. db::bind(':debit', $_array["debit"]);
  99. db::bind(':credit', $_array["credit"]);
  100. db::bind(':solde', $_array["solde"]);
  101. db::bind(':md5', $_array["md5"]);
  102. try {
  103. db::execute();
  104. return TRUE;
  105. } catch (Exception $ex) {
  106. return FALSE;
  107. }
  108. }
  109. return FALSE;
  110. }
  111. public static function recordLignes(array $_array, int $_id){
  112. $z = 0;
  113. foreach ($_array as $values) {
  114. if(self::recordLigne($values, $_id) == TRUE){
  115. $z++;
  116. }
  117. }
  118. return $z;
  119. }
  120. public static function readCompte(array $_temp) {
  121. $data = [];
  122. if (!file_exists($_temp['tmp_name']) || !is_readable($_temp['tmp_name'])) {
  123. return false;
  124. }
  125. if (($handle = fopen($_temp['tmp_name'], 'r')) !== false) {
  126. $header = NULL;
  127. $a = -1;
  128. while (($row = fgetcsv($handle, 1000, ";")) !== false) {
  129. $a++;
  130. $convertedRow = array_map(function ($field) {
  131. return mb_convert_encoding($field, 'UTF-8', 'ISO-8859-1');
  132. }, $row);
  133. if ($header == NULL) {
  134. $header = TRUE;
  135. if(
  136. $convertedRow[0] != "Date" OR
  137. $convertedRow[1] != "Date de valeur" OR
  138. $convertedRow[2] != "Débit" OR
  139. $convertedRow[3] != "Crédit" OR
  140. $convertedRow[4] != "Libellé" OR
  141. $convertedRow[5] != "Solde"
  142. ){
  143. ($convertedRow[0] != "Date") ? alert::recError("La 1er colonne doit se nommer Date") : "";
  144. ($convertedRow[1] != "Date de valeur") ? alert::recError("La 2em colonne doit se nommer Date de valeur") : "";
  145. ($convertedRow[2] != "Débit") ? alert::recError("La 3em colonne doit se nommer Débit") : "";
  146. ($convertedRow[3] != "Crédit") ? alert::recError("La 4em colonne doit se nommer Crédit") : "";
  147. ($convertedRow[4] != "Libellé") ? alert::recError("La 5em colonne doit se nommer Libellé") : "";
  148. ($convertedRow[5] != "Solde") ? alert::recError("La 6em colonne doit se nommer Solde") : "";
  149. alert::recError("Le fichier " . $_temp['name'] . " n'est pas un CSV de la banque Crédit Mutuel.");
  150. return FALSE;
  151. }
  152. } else {
  153. $data[$a]["date"] = self::convertToMySqlDate($convertedRow[0]);
  154. $data[$a]["label"] = $convertedRow[4];
  155. $data[$a]["debit"] = self::transformerChaineEnNombre($convertedRow[2]);
  156. $data[$a]["credit"] = self::transformerChaineEnNombre($convertedRow[3]);
  157. $data[$a]["solde"] = self::transformerChaineEnNombre($convertedRow[5], TRUE);
  158. $data[$a]["md5"] = md5($data[$a]["date"].$data[$a]["debit"].$data[$a]["credit"].$data[$a]["solde"]);
  159. }
  160. }
  161. fclose($handle);
  162. }
  163. return $data;
  164. }
  165. public static function transformerChaineEnNombre(string $_chaine, bool $_negatif = FALSE) {
  166. // Supprimer les tirets "-"
  167. if($_negatif == 0) {
  168. $chaine = str_replace('-', '', $_chaine);
  169. } else {
  170. $chaine = $_chaine;
  171. }
  172. // Remplacer les virgules par des points
  173. $chaine = str_replace(',', '.', $chaine);
  174. // Convertir en nombre à virgule flottante (float)
  175. $nombre = (float) $chaine;
  176. return $nombre;
  177. }
  178. public static function convertToMySqlDate($date) {
  179. // Crée un objet DateTime à partir de la date au format dd/mm/yyyy
  180. $dateTime = DateTime::createFromFormat('d/m/Y', $date);
  181. // Vérifie si la date est valide
  182. if ($dateTime === false) {
  183. return false; // Retourne false si la date n'est pas valide
  184. }
  185. // Retourne la date au format MySQL (yyyy-mm-dd)
  186. return $dateTime->format('Y-m-d');
  187. }
  188. public static function getEuro(float $_decimal){
  189. return number_format($_decimal, 2, ',', '.') . " €";
  190. }
  191. public static function fileName(string $_compte){
  192. $n = explode(" ", $_compte);
  193. return $n[1].$n[2];
  194. }
  195. }