prowebDossiers.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * Classe prowebDossiers
  4. *
  5. * Cette classe gère les opérations liées aux dossiers Proweb, telles que la récupération, l'ajout, la suppression et la vérification des données.
  6. */
  7. class prowebDossiers
  8. {
  9. /**
  10. * Récupère les informations d'un fichier Excel Proweb par son identifiant.
  11. *
  12. * @param int $_id Identifiant du fichier Excel.
  13. * @return array Informations du fichier Excel.
  14. */
  15. public static function getExcelById(int $_id)
  16. {
  17. db::query("SELECT * FROM " . DB_T_EXCEL_PROWEB_DOSSIERS . " WHERE id = :id");
  18. db::bind(':id', $_id);
  19. return db::single();
  20. }
  21. /**
  22. * Récupère tous les dossiers Proweb.
  23. *
  24. * @return array Liste des dossiers Proweb.
  25. */
  26. public static function all()
  27. {
  28. db::query("SELECT * FROM " . DB_T_DOSSIERS_PROWEB);
  29. return db::resultset();
  30. }
  31. /**
  32. * Supprime un fichier Excel Proweb par son identifiant.
  33. *
  34. * @param int $_id Identifiant du fichier Excel.
  35. * @return bool Succès ou échec de la suppression.
  36. */
  37. public static function deleteExcel(int $_id)
  38. {
  39. $tmp = self::getExcelById($_id);
  40. file::delete($tmp["md5"]);
  41. db::query("DELETE FROM " . DB_T_EXCEL_PROWEB_DOSSIERS . " WHERE id = :id");
  42. db::bind(':id', $_id);
  43. try {
  44. db::execute();
  45. return TRUE;
  46. } catch (Exception $ex) {
  47. return FALSE;
  48. }
  49. }
  50. /**
  51. * Vérifie si les données d'un fichier Excel sont conformes aux attentes.
  52. *
  53. * @param array $_array Données du fichier Excel.
  54. * @return bool TRUE si les données sont conformes, FALSE sinon.
  55. */
  56. public static function checkExcel(array $_array)
  57. {
  58. $expectedValues = [
  59. 'exe_ref',
  60. 'prest_id',
  61. 'prest_lib',
  62. 'prest_lib2',
  63. 'catprest_id',
  64. 'catprest_lib',
  65. 'prest_period',
  66. 'prest_closed',
  67. 'doss_id',
  68. 'doss_lib',
  69. 'etdoss_id',
  70. 'etdoss_lib',
  71. 'doss_nb_inscrit',
  72. 'od_meyclub_subv',
  73. 'doss_partce',
  74. 'doss_partod',
  75. 'doss_montant_total',
  76. 'comm_ref',
  77. 'od_matricule',
  78. 'od_nom',
  79. 'od_prenom',
  80. 'hismo_date_crea'
  81. ];
  82. $firstEntry = $_array[0] ?? [];
  83. $missingValues = array_diff($expectedValues, $firstEntry);
  84. $extraValues = array_diff($firstEntry, $expectedValues);
  85. if (empty($missingValues) && empty($extraValues)) {
  86. return TRUE;
  87. } else {
  88. alert::recError("Les données présentes dans ce fichier ne correspondent pas avec l'attendu");
  89. if (!empty($missingValues)) {
  90. alert::recError("Clés manquantes : " . implode(', ', $missingValues) . "\n");
  91. }
  92. if (!empty($extraValues)) {
  93. alert::recError("Clés supplémentaires : " . implode(', ', $extraValues) . "\n");
  94. }
  95. return FALSE;
  96. }
  97. }
  98. /**
  99. * Ajoute des données provenant d'un fichier Excel dans la base de données.
  100. *
  101. * @param array $_array Données à ajouter.
  102. * @return bool TRUE en cas de succès, FALSE en cas d'échec.
  103. */
  104. public static function add(array $_array)
  105. {
  106. if (empty($_array)) {
  107. return false;
  108. }
  109. $placeholders = [];
  110. $params = [];
  111. foreach ($_array as $index => $entry) {
  112. if ($index > 0) {
  113. $placeholders[] = "(
  114. :id_$index, :exe_ref_$index, :prest_id_$index, :prest_lib_$index, :prest_lib2_$index,
  115. :catprest_id_$index, :catprest_lib_$index, :prest_period_$index, :prest_closed_$index,
  116. :doss_id_$index, :doss_lib_$index, :etdoss_id_$index, :etdoss_lib_$index,
  117. :doss_nb_inscrit_$index, :od_meyclub_subv_$index, :doss_partce_$index, :doss_partod_$index, :doss_montant_total_$index, :comm_ref_$index,
  118. :od_matricule_$index, :od_nom_$index, :od_prenom_$index, :hismo_date_crea_$index)";
  119. $params[":id_$index"] = $entry[1] . $entry[8];
  120. if (preg_match('/\b(\d{4})\b/', $entry[0], $match)) {
  121. $params[":exe_ref_$index"] = (int)$match[1];
  122. } else {
  123. $params[":exe_ref_$index"] = null;
  124. }
  125. $params[":prest_id_$index"] = !empty($entry[1]) ? (int)$entry[1] : null;
  126. $params[":prest_lib_$index"] = $entry[2];
  127. $params[":prest_lib2_$index"] = $entry[3];
  128. $params[":catprest_id_$index"] = !empty($entry[4]) ? (int)$entry[4] : null;
  129. $params[":catprest_lib_$index"] = $entry[5];
  130. $params[":prest_period_$index"] = $entry[6];
  131. $params[":prest_closed_$index"] = $entry[7];
  132. $params[":doss_id_$index"] = $entry[8];
  133. $params[":doss_lib_$index"] = $entry[9];
  134. $params[":etdoss_id_$index"] = $entry[10];
  135. $params[":etdoss_lib_$index"] = $entry[11];
  136. $params[":doss_nb_inscrit_$index"] = is_numeric($entry[12]) ? (int)$entry[12] : null;
  137. $params[":od_meyclub_subv_$index"] = $entry[13];
  138. $params[":doss_partce_$index"] = $entry[14];
  139. $params[":doss_partod_$index"] = $entry[15];
  140. $params[":doss_montant_total_$index"] = $entry[16];
  141. $params[":comm_ref_$index"] = $entry[17];
  142. $params[":od_matricule_$index"] = $entry[18];
  143. $params[":od_nom_$index"] = $entry[19];
  144. $params[":od_prenom_$index"] = $entry[20];
  145. $params[":hismo_date_crea_$index"] = $entry[21];
  146. }
  147. }
  148. $sql = "INSERT INTO " . DB_T_DOSSIERS_PROWEB . " (
  149. id, exe_ref, prest_id, prest_lib, prest_lib2,
  150. catprest_id, catprest_lib, prest_period, prest_closed,
  151. doss_id, doss_lib, etdoss_id, etdoss_lib,
  152. doss_nb_inscrit, od_meyclub_subv, doss_partce, doss_partod, doss_montant_total, comm_ref,
  153. od_matricule, od_nom, od_prenom, hismo_date_crea
  154. ) VALUES " . implode(", ", $placeholders) . "
  155. ON DUPLICATE KEY UPDATE
  156. exe_ref = VALUES(exe_ref),
  157. prest_id = VALUES(prest_id),
  158. prest_lib = VALUES(prest_lib),
  159. prest_lib2 = VALUES(prest_lib2),
  160. catprest_id = VALUES(catprest_id),
  161. catprest_lib = VALUES(catprest_lib),
  162. prest_period = VALUES(prest_period),
  163. prest_closed = VALUES(prest_closed),
  164. doss_id = VALUES(doss_id),
  165. doss_lib = VALUES(doss_lib),
  166. etdoss_id = VALUES(etdoss_id),
  167. etdoss_lib = VALUES(etdoss_lib),
  168. doss_nb_inscrit = VALUES(doss_nb_inscrit),
  169. od_meyclub_subv = VALUES(od_meyclub_subv),
  170. doss_partce = VALUES(doss_partce),
  171. doss_partod = VALUES(doss_partod),
  172. doss_montant_total = VALUES(doss_montant_total),
  173. comm_ref = VALUES(comm_ref),
  174. od_matricule = VALUES(od_matricule),
  175. od_nom = VALUES(od_nom),
  176. od_prenom = VALUES(od_prenom),
  177. hismo_date_crea = VALUES(hismo_date_crea)";
  178. db::query($sql);
  179. foreach ($params as $key => $value) {
  180. db::bind($key, $value);
  181. }
  182. try {
  183. db::execute();
  184. return TRUE;
  185. } catch (Exception $ex) {
  186. alert::recError("Erreur lors de l'importation des données : " . $ex->getMessage());
  187. if (debug::isFile("debug")) {
  188. alert::recError("Stack : " . $ex);
  189. }
  190. return FALSE;
  191. }
  192. }
  193. /**
  194. * Récupère les informations du dernier fichier Excel.
  195. *
  196. * @return array Informations du dernier fichier Excel.
  197. */
  198. public static function getExcel()
  199. {
  200. db::query("SELECT * FROM " . DB_T_EXCEL . " ORDER BY id DESC LIMIT 0, 1");
  201. return db::single();
  202. }
  203. /**
  204. * Télécharge un fichier Excel Proweb par son identifiant.
  205. *
  206. * @param int $_id Identifiant du fichier Excel.
  207. * @param string $_file Nom du fichier temporaire.
  208. * @return string Chemin du fichier téléchargé.
  209. */
  210. public static function downloadExcel(int $_id, string $_file)
  211. {
  212. $data = base64_decode(self::getExcelById($_id)["file"]);
  213. $fileTemp = fopen(DIR_TEMP . $_file, "w");
  214. file_put_contents(DIR_TEMP . $_file, $data);
  215. fclose($fileTemp);
  216. return DIR_TEMP . $_file;
  217. }
  218. /**
  219. * Génère un lien vers les dossiers Proweb avec une date formatée.
  220. *
  221. * @return string Lien formaté vers les dossiers Proweb.
  222. */
  223. public static function getLinkProweb() {
  224. $date = new DateTime();
  225. $date->modify('-12 months');
  226. $dateFormatee = $date->format('d-m-Y');
  227. return str_replace('##DD-MM-YYYY##', $dateFormatee, PROWEB_DOSSIERS);
  228. }
  229. }