2
0

prowebDossiers.class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. class prowebDossiers
  3. {
  4. public static function getExcelById(int $_id)
  5. {
  6. db::query("SELECT * FROM " . DB_T_EXCEL_PROWEB_DOSSIERS . " WHERE id = :id");
  7. db::bind(':id', $_id);
  8. return db::single();
  9. }
  10. public static function all()
  11. {
  12. db::query("SELECT * FROM " . DB_T_DOSSIERS_PROWEB);
  13. return db::resultset();
  14. }
  15. public static function deleteExcel(int $_id)
  16. {
  17. $tmp = self::getExcelById($_id);
  18. file::delete($tmp["md5"]);
  19. db::query("DELETE FROM " . DB_T_EXCEL_PROWEB_DOSSIERS . " WHERE id = :id");
  20. db::bind(':id', $_id);
  21. try {
  22. db::execute();
  23. return TRUE;
  24. } catch (Exception $ex) {
  25. return FALSE;
  26. }
  27. }
  28. public static function checkExcel(array $_array)
  29. {
  30. $expectedValues = [
  31. 'exe_ref',
  32. 'prest_id',
  33. 'prest_lib',
  34. 'prest_lib2',
  35. 'catprest_id',
  36. 'catprest_lib',
  37. 'prest_period',
  38. 'prest_closed',
  39. 'doss_id',
  40. 'doss_lib',
  41. 'etdoss_id',
  42. 'etdoss_lib',
  43. 'doss_nb_inscrit',
  44. 'od_meyclub_subv',
  45. 'doss_partce',
  46. 'doss_partod',
  47. 'doss_montant_total',
  48. 'comm_ref',
  49. 'od_matricule',
  50. 'od_nom',
  51. 'od_prenom',
  52. 'hismo_date_crea'
  53. ];
  54. $firstEntry = $_array[0] ?? [];
  55. $missingValues = array_diff($expectedValues, $firstEntry);
  56. $extraValues = array_diff($firstEntry, $expectedValues);
  57. if (empty($missingValues) && empty($extraValues)) {
  58. return TRUE;
  59. } else {
  60. alert::recError("Les données présentes dans ce fichier ne correspondent pas avec l'attendu");
  61. if (!empty($missingValues)) {
  62. alert::recError("Clés manquantes : " . implode(', ', $missingValues) . "\n");
  63. }
  64. if (!empty($extraValues)) {
  65. alert::recError("Clés supplémentaires : " . implode(', ', $extraValues) . "\n");
  66. }
  67. return FALSE;
  68. }
  69. }
  70. public static function add(array $_array)
  71. {
  72. if (empty($_array)) {
  73. return false;
  74. }
  75. $placeholders = [];
  76. $params = [];
  77. foreach ($_array as $index => $entry) {
  78. if ($index > 0) {
  79. $placeholders[] = "(
  80. :id_$index, :exe_ref_$index, :prest_id_$index, :prest_lib_$index, :prest_lib2_$index,
  81. :catprest_id_$index, :catprest_lib_$index, :prest_period_$index, :prest_closed_$index,
  82. :doss_id_$index, :doss_lib_$index, :etdoss_id_$index, :etdoss_lib_$index,
  83. :doss_nb_inscrit_$index, :od_meyclub_subv_$index, :doss_partce_$index, :doss_partod_$index, :doss_montant_total_$index, :comm_ref_$index,
  84. :od_matricule_$index, :od_nom_$index, :od_prenom_$index, :hismo_date_crea_$index)";
  85. $params[":id_$index"] = $entry[1] . $entry[8];
  86. if (preg_match('/\b(\d{4})\b/', $entry[0], $match)) {
  87. $params[":exe_ref_$index"] = (int)$match[1];
  88. } else {
  89. $params[":exe_ref_$index"] = null;
  90. }
  91. $params[":prest_id_$index"] = !empty($entry[1]) ? (int)$entry[1] : null;
  92. $params[":prest_lib_$index"] = $entry[2];
  93. $params[":prest_lib2_$index"] = $entry[3];
  94. $params[":catprest_id_$index"] = !empty($entry[4]) ? (int)$entry[4] : null;
  95. $params[":catprest_lib_$index"] = $entry[5];
  96. $params[":prest_period_$index"] = $entry[6];
  97. $params[":prest_closed_$index"] = $entry[7];
  98. $params[":doss_id_$index"] = $entry[8];
  99. $params[":doss_lib_$index"] = $entry[9];
  100. $params[":etdoss_id_$index"] = $entry[10];
  101. $params[":etdoss_lib_$index"] = $entry[11];
  102. $params[":doss_nb_inscrit_$index"] = is_numeric($entry[12]) ? (int)$entry[12] : null;
  103. $params[":od_meyclub_subv_$index"] = $entry[13];
  104. $params[":doss_partce_$index"] = $entry[14];
  105. $params[":doss_partod_$index"] = $entry[15];
  106. $params[":doss_montant_total_$index"] = $entry[16];
  107. $params[":comm_ref_$index"] = $entry[17];
  108. $params[":od_matricule_$index"] = $entry[18];
  109. $params[":od_nom_$index"] = $entry[19];
  110. $params[":od_prenom_$index"] = $entry[20];
  111. $params[":hismo_date_crea_$index"] = $entry[21];
  112. }
  113. }
  114. $sql = "INSERT INTO " . DB_T_DOSSIERS_PROWEB . " (
  115. id, exe_ref, prest_id, prest_lib, prest_lib2,
  116. catprest_id, catprest_lib, prest_period, prest_closed,
  117. doss_id, doss_lib, etdoss_id, etdoss_lib,
  118. doss_nb_inscrit, od_meyclub_subv, doss_partce, doss_partod, doss_montant_total, comm_ref,
  119. od_matricule, od_nom, od_prenom, hismo_date_crea
  120. ) VALUES " . implode(", ", $placeholders) . "
  121. ON DUPLICATE KEY UPDATE
  122. exe_ref = VALUES(exe_ref),
  123. prest_id = VALUES(prest_id),
  124. prest_lib = VALUES(prest_lib),
  125. prest_lib2 = VALUES(prest_lib2),
  126. catprest_id = VALUES(catprest_id),
  127. catprest_lib = VALUES(catprest_lib),
  128. prest_period = VALUES(prest_period),
  129. prest_closed = VALUES(prest_closed),
  130. doss_id = VALUES(doss_id),
  131. doss_lib = VALUES(doss_lib),
  132. etdoss_id = VALUES(etdoss_id),
  133. etdoss_lib = VALUES(etdoss_lib),
  134. doss_nb_inscrit = VALUES(doss_nb_inscrit),
  135. od_meyclub_subv = VALUES(od_meyclub_subv),
  136. doss_partce = VALUES(doss_partce),
  137. doss_partod = VALUES(doss_partod),
  138. doss_montant_total = VALUES(doss_montant_total),
  139. comm_ref = VALUES(comm_ref),
  140. od_matricule = VALUES(od_matricule),
  141. od_nom = VALUES(od_nom),
  142. od_prenom = VALUES(od_prenom),
  143. hismo_date_crea = VALUES(hismo_date_crea)";
  144. db::query($sql);
  145. foreach ($params as $key => $value) {
  146. db::bind($key, $value);
  147. }
  148. try {
  149. db::execute();
  150. return TRUE;
  151. } catch (Exception $ex) {
  152. alert::recError("Erreur lors de l'importation des données : " . $ex->getMessage());
  153. if (debug::isFile("debug")) {
  154. alert::recError("Stack : " . $ex);
  155. }
  156. return FALSE;
  157. }
  158. }
  159. public static function getExcel()
  160. {
  161. db::query("SELECT * FROM " . DB_T_EXCEL . " ORDER BY id DESC LIMIT 0, 1");
  162. return db::single();
  163. }
  164. public static function downloadExcel(int $_id, string $_file)
  165. {
  166. $data = base64_decode(self::getExcelById($_id)["file"]);
  167. $fileTemp = fopen(DIR_TEMP . $_file, "w");
  168. file_put_contents(DIR_TEMP . $_file, $data);
  169. fclose($fileTemp);
  170. return DIR_TEMP . $_file;
  171. }
  172. public static function getLinkProweb() {
  173. $date = new DateTime();
  174. $date->modify('-12 months');
  175. $dateFormatee = $date->format('d-m-Y');
  176. return str_replace('##DD-MM-YYYY##', $dateFormatee, PROWEB_DOSSIERS);
  177. }
  178. }