banque.class.php 8.8 KB

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