2
0

cms.rh-import-excel.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. if (core::ifPost("from") and core::getPost("from") == "rh-import-excel") {
  3. if (isset($_FILES[core::getPost("from")]['error']) and $_FILES[core::getPost("from")]['error'] > 0) {
  4. switch ($_FILES[core::getPost("from")]['error']) {
  5. case 1: // UPLOAD_ERR_INI_SIZE
  6. alert::recError("Le fichier dépasse la limite autorisée par le serveur (fichier php.ini) !");
  7. break;
  8. case 2: // UPLOAD_ERR_FORM_SIZE
  9. alert::recError("Le fichier dépasse la limite autorisée dans le formulaire HTML !");
  10. break;
  11. case 3: // UPLOAD_ERR_PARTIAL
  12. alert::recError("L'envoi du fichier a été interrompu pendant le transfert !");
  13. break;
  14. case 4: // UPLOAD_ERR_NO_FILE
  15. alert::recError("Vous n'avez pas chargé de fichier");
  16. break;
  17. }
  18. } elseif ($_FILES[core::getPost("from")]['type'] != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
  19. alert::recError("Seuls les fichiers Excel au format xlsx sont acceptés");
  20. header("Location: /rh-upload-excel.html");
  21. exit();
  22. } else {
  23. if (file_exists($_FILES[core::getPost("from")]['tmp_name'])) {
  24. $data = array();
  25. $simpleXLSX = new simpleXLSX();
  26. $xlsx = $simpleXLSX->parse($_FILES[core::getPost("from")]['tmp_name']);
  27. if ($xlsx === false) {
  28. alert::recError("Erreur lors de la lecture du fichier Excel : " . $_FILES[core::getPost("from")]['name']);
  29. header("Location: /rh-upload-excel.html");
  30. exit();
  31. }
  32. $returnXlsx = $xlsx->rows();
  33. if (
  34. $returnXlsx[0][0] != "ID LOCAL"
  35. or $returnXlsx[0][1] != "NOM"
  36. or $returnXlsx[0][2] != "PRENOM"
  37. or $returnXlsx[0][3] != "SEXE"
  38. or $returnXlsx[0][4] != "STATUT ACTIVITE"
  39. or $returnXlsx[0][5] != "PREMIERE EMBAUCHE"
  40. or $returnXlsx[0][6] != "SOUS-DOMAINE DU PERSONNEL - TE"
  41. or $returnXlsx[0][7] != "LOGIN ID"
  42. ) {
  43. alert::recError("Le fichier " . $_FILES[core::getPost("from")]['name'] . " n'est pas un fichier RH.");
  44. ($returnXlsx[0][0] != "ID LOCAL") ? alert::recError("La 1er colonne doit se nommer ID LOCAL") : "";
  45. ($returnXlsx[0][1] != "NOM") ? alert::recError("La 2em colonne doit se nommer NOM") : "";
  46. ($returnXlsx[0][2] != "PRENOM") ? alert::recError("La 3em colonne doit se nommer PRENOM") : "";
  47. ($returnXlsx[0][3] != "SEXE") ? alert::recError("La 4em colonne doit se nommer SEXE") : "";
  48. ($returnXlsx[0][4] != "STATUT ACTIVITE") ? alert::recError("La 5em colonne doit se nommer STATUT ACTIVITE") : "";
  49. ($returnXlsx[0][5] != "PREMIERE EMBAUCHE") ? alert::recError("La 6em colonne doit se nommer PREMIERE EMBAUCHE") : "";
  50. ($returnXlsx[0][6] != "SOUS-DOMAINE DU PERSONNEL - TE") ? alert::recError("La 7em colonne doit se nommer SOUS-DOMAINE DU PERSONNEL - TE") : "";
  51. ($returnXlsx[0][7] != "LOGIN ID") ? alert::recError("La 8em colonne doit se nommer LOGIN ID") : "";
  52. header("Location: /rh-upload-excel.html");
  53. exit();
  54. }
  55. $data["md5File"] = file::record($_FILES[core::getPost("from")]);
  56. if ($data["md5File"] == FALSE) {
  57. header("Location: /rh-upload-excel.html");
  58. exit();
  59. }
  60. $data["nbSalaries"] = count($returnXlsx) - 1;
  61. $data["json"] = json_encode($returnXlsx);
  62. $data["date"] = core::getPost("date");
  63. if (salaries::insertExcel($data) == FALSE) {
  64. alert::recError("Erreur #submit sur l'import du fichier " . $_FILES[core::getPost("from")]['name']);
  65. header("Location: /rh-upload-excel.html");
  66. exit();
  67. }
  68. json::create("excel");
  69. historique::recRef("/rh-historique-excel.html");
  70. historique::add(array(
  71. "idType" => historique::getIdRef("ACTION"),
  72. "idUser" => session::getId(),
  73. "idPage" => historique::getIdRef("/rh-historique-excel.html"),
  74. "log" => "Import du fichier " . $_FILES[core::getPost("from")]['name']
  75. ));
  76. alert::recSuccess("Le fichier " . $_FILES[core::getPost("from")]['name'] . " a été chargé avec succès");
  77. header("Location: /rh-historique-excel.html");
  78. exit();
  79. } else {
  80. alert::recError("Erreur lors du chargement du fichier : " . $_FILES[core::getPost("from")]['name']);
  81. }
  82. }
  83. header("Location: /rh-upload-excel.html");
  84. exit();
  85. } else {
  86. header('HTTP/1.0 401 Unauthorized');
  87. exit();
  88. }