proweb.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. class proweb
  3. {
  4. public static function getBase()
  5. {
  6. db::query("SELECT *,
  7. IF(contrat = 1, 'Actif', 'Désactivé') AS texteContrat,
  8. IF(actif = 1, 'Actif', 'Désactivé') AS texteActif,
  9. IF(RHBase = 1, 'Présent', 'Absent') AS texteRHBase,
  10. IF(error = 1, 'Erreur', 'Valide') AS texteError
  11. FROM " . DB_T_SALARIES_PROWEB);
  12. return db::resultset();
  13. }
  14. public static function getExcelById(int $_id)
  15. {
  16. db::query("SELECT * FROM " . DB_T_EXCEL_PROWEB . " WHERE id = :id");
  17. db::bind(':id', $_id);
  18. return db::single();
  19. }
  20. public static function deleteExcel(int $_id)
  21. {
  22. $tmp = self::getExcelById($_id);
  23. file::delete($tmp["md5"]);
  24. db::query("DELETE FROM " . DB_T_EXCEL_PROWEB . " WHERE id = :id");
  25. db::bind(':id', $_id);
  26. try {
  27. db::execute();
  28. return TRUE;
  29. } catch (Exception $ex) {
  30. return FALSE;
  31. }
  32. }
  33. public static function getExcel()
  34. {
  35. db::query("SELECT * FROM " . DB_T_EXCEL . " ORDER BY id DESC LIMIT 0, 1");
  36. return db::single();
  37. }
  38. public static function convertDateMoisAn(string $datetime)
  39. {
  40. $pieces = explode(" ", $datetime);
  41. $pieces2 = explode("-", $pieces[0]);
  42. return $pieces2[0] . $pieces2[1];
  43. }
  44. public static function convertDateMoisAnJour(string $datetime)
  45. {
  46. $pieces = explode(" ", $datetime);
  47. return $pieces[0];
  48. }
  49. public static function archiveExcelValues(array $_array)
  50. {
  51. $_return["error"] = 0;
  52. $_return["errorJson"] = array();
  53. // ----
  54. $_return["idProweb"] = $_array[0];
  55. $_return["nom"] = self::archiveExcelValuesNom($_array[1]);
  56. $_return["prenom"] = self::archiveExcelValuesPrenom($_array[2]);
  57. $_return["sexe"] = self::archiveExcelValuesSexe($_array[3]);
  58. $_return["dateNaissance"] = self::archiveExcelValuesDate($_array[4]);
  59. $_return["jourEntree"] = self::archiveExcelValuesDate($_array[5]);
  60. $_return["jourSortie"] = self::archiveExcelValuesDate($_array[6]);
  61. $_return["actif"] = self::archiveExcelValuesActif($_array[7]);
  62. $_return["lieu"] = self::archiveExcelValuesLieu($_array[8]);
  63. $_return["loginId"] = self::archiveExcelValuesloginId($_array[9]);
  64. $verifRH = self::checksalarieByLoginId($_return["loginId"], $_return["nom"], $_return["prenom"]);
  65. if ($_return["jourEntree"] != "N/A") { // On ignore les nouveaux salariés qui se sont inscrits post import du fichier RH
  66. $dateDataRH = intval(self::convertDateMoisAn(self::getExcel()["dateData"]));
  67. $dateDataProWeb = intval(self::convertDateMoisAn($_return["jourEntree"]));
  68. } else {
  69. $dateDataRH = $dateDataProWeb = 0;
  70. }
  71. if (empty($verifRH["id"]) and $_return["jourSortie"] == "N/A" and $dateDataRH > $dateDataProWeb) {
  72. $_return["contrat"] = $_return["actif"] = $_return["RHBase"] = $_return["loginIdRH"] = $_return["jourEntreeRH"] = "N/A";
  73. array_push($_return["errorJson"], "absent");
  74. } else {
  75. $_return["loginIdRH"] = (empty($verifRH["loginId"]) or $verifRH["loginId"] == $_return["loginId"]) ? NULL : $verifRH["loginId"];
  76. $_return["jourEntreeRH"] = (empty($verifRH["jourEntree"]) or self::convertDateMoisAnJour($verifRH["jourEntree"]) == $_return["jourEntree"]) ? NULL : self::convertDateMoisAnJour($verifRH["jourEntree"]);
  77. $_return["jourSortie"] = (isset($verifRH["jourSortie"])) ? $verifRH["jourSortie"] : $_return["jourSortie"];
  78. $_return["RHBase"] = (isset($verifRH["jourSortie"]) or empty($verifRH["id"])) ? 0 : 1;
  79. $_return["actif"] = (isset($verifRH["jourSortie"]) or empty($verifRH["id"])) ? 0 : 1;
  80. $_return["contrat"] = (isset($verifRH["contrat"])) ? $verifRH["contrat"] : 0;
  81. // ----
  82. if (!self::checkValuesNom($_return["nom"])) {
  83. $_return["error"] = 1;
  84. array_push($_return["errorJson"], "nom");
  85. }
  86. if (!self::checkValuesPrenom($_return["prenom"])) {
  87. $_return["error"] = 1;
  88. array_push($_return["errorJson"], "prenom");
  89. }
  90. if (!self::checkValuesloginId($_return["loginId"], $_return["loginIdRH"])) {
  91. $_return["error"] = 1;
  92. array_push($_return["errorJson"], "loginId");
  93. }
  94. if (!self::checkValuesNaissance($_return["dateNaissance"])) {
  95. $_return["error"] = 1;
  96. array_push($_return["errorJson"], "dateNaissance");
  97. }
  98. if (!self::checkValuesJourEntree($_return["jourEntree"])) {
  99. $_return["error"] = 1;
  100. array_push($_return["errorJson"], "jourEntree");
  101. }
  102. if ($_return["jourEntreeRH"] != NULL and $_return["jourEntree"] != "N/A") {
  103. $_return["error"] = 1;
  104. array_push($_return["errorJson"], "jourEntree");
  105. }
  106. if ($_return["jourEntree"] != "N/A") { // On ignore les nouveaux salariés qui se sont inscrits post import du fichier RH
  107. if ($dateDataRH > $dateDataProWeb) {
  108. if (!self::checkValuesRHBase($_return["RHBase"], $_return["actif"], $_return["jourSortie"])) {
  109. $_return["error"] = 1;
  110. array_push($_return["errorJson"], "RHBase");
  111. }
  112. }
  113. }
  114. // if ($_return["contrat"] == 0 AND $_return["jourSortie"] == "N/A") {
  115. // $_return["error"] = 1;
  116. // array_push($_return["errorJson"], "contrat");
  117. // }
  118. }
  119. $_return["errorJson"] = (!empty($_return["errorJson"][0])) ? json_encode($_return["errorJson"]) : NULL;
  120. return $_return;
  121. }
  122. // Les initialisations
  123. private static function archiveExcelValuesDate(string $_string)
  124. {
  125. if(!isset($_string) or $_string == "00-00-0000"){
  126. return "N/A";
  127. } else {
  128. return str_replace(" 00:00:00", "", $_string);
  129. }
  130. }
  131. private static function archiveExcelValuesSexe(string $_string)
  132. {
  133. return (empty($_string)) ? "N/A" : $_string;
  134. }
  135. private static function archiveExcelValuesloginId(string $_string)
  136. {
  137. return core::cleanAccent($_string);
  138. }
  139. private static function archiveExcelValuesNom(string $_string)
  140. {
  141. return core::cleanAccent($_string);
  142. }
  143. private static function archiveExcelValuesPrenom(string $_string)
  144. {
  145. return core::cleanAccent($_string);
  146. }
  147. private static function archiveExcelValuesActif(string $_string)
  148. {
  149. if ($_string == "Actif") {
  150. return 1;
  151. } elseif ($_string == "Inactif") {
  152. return 0;
  153. } else {
  154. return "N/A";
  155. }
  156. }
  157. private static function archiveExcelValuesLieu(string $_string)
  158. {
  159. return (empty($_string)) ? "N/A" : $_string;
  160. }
  161. // Les checks
  162. public static function checkSalarieByLoginId(string $_loginId, string $_nom = NULL, string $_prenom = NULL)
  163. {
  164. // Récupération des données de l'excel au format Json
  165. if ($_nom == NULL) {
  166. db::query("SELECT id, loginId, jourEntree, jourSortie, contrat FROM " . DB_T_SALARIES . " WHERE loginId = :loginId");
  167. } else {
  168. db::query("SELECT id, loginId, jourEntree, jourSortie, contrat FROM " . DB_T_SALARIES . " WHERE loginId = :loginId OR (nom = :nom AND prenom = :prenom)");
  169. db::bind(':nom', $_nom);
  170. db::bind(':prenom', $_prenom);
  171. }
  172. db::bind(':loginId', $_loginId);
  173. return db::single();
  174. }
  175. private static function checkValuesActif(string $_string)
  176. {
  177. return ($_string == "N/A") ? FALSE : TRUE;
  178. }
  179. private static function checkValuesloginId(string $_idLogin, string $_idLoginRH = NULL)
  180. {
  181. if ($_idLoginRH != NULL) {
  182. if (empty(self::checkSalarieByLoginId($_idLogin)["id"])) {
  183. return FALSE;
  184. } elseif (core::checkStringOnly($_idLogin)) {
  185. return FALSE;
  186. } elseif (strlen($_idLogin) > 10) {
  187. return FALSE;
  188. } elseif (empty($_idLogin)) {
  189. return FALSE;
  190. } else {
  191. return TRUE;
  192. }
  193. } else {
  194. return TRUE;
  195. }
  196. }
  197. private static function checkValuesRHBase(string $_string, int $_actif, string $_sortie)
  198. {
  199. return ($_string == 0 and $_actif == 1 and $_sortie == "N/A") ? FALSE : TRUE;
  200. }
  201. private static function checkValuesJourEntree(string $_string)
  202. {
  203. return ($_string == "N/A") ? FALSE : TRUE;
  204. }
  205. private static function checkValuesJourSortie(string $_string, int $_actif)
  206. {
  207. return ($_string != "N/A" and $_actif == 1) ? FALSE : TRUE;
  208. }
  209. private static function checkValuesNom(string $_string)
  210. {
  211. return (empty($_string)) ? FALSE : TRUE;
  212. }
  213. private static function checkValuesPrenom(string $_string)
  214. {
  215. return (empty($_string)) ? FALSE : TRUE;
  216. }
  217. private static function checkValuesSexe(string $_string)
  218. {
  219. return ($_string == NULL or ($_string != "M." and $_string != "Mme")) ? FALSE : TRUE;
  220. }
  221. private static function checkValuesLieu(string $_string)
  222. {
  223. return (empty($_string)) ? FALSE : TRUE;
  224. }
  225. private static function checkValuesNaissance(string $_string)
  226. {
  227. if(empty($_string) OR $_string == "N/A") {
  228. return FALSE;
  229. } elseif(self::calculAge($_string) > core::getConfig("AGE_MAX_ERROR")) {
  230. return FALSE;
  231. } else {
  232. return TRUE;
  233. }
  234. }
  235. private static function calculAge(string $_date)
  236. {
  237. $pieces = explode(" ", $_date);
  238. $birthDate = explode("-", $pieces[0]);
  239. return (date("md", date("U", mktime(0, 0, 0, $birthDate[2], $birthDate[1], $birthDate[0]))) > date("md")
  240. ? ((date("Y") - $birthDate[0]) - 1)
  241. : (date("Y") - $birthDate[0]));
  242. }
  243. public static function downloadExcel(int $_id, string $_file)
  244. {
  245. $data = base64_decode(self::getExcelById($_id)["file"]);
  246. $fileTemp = fopen(DIR_TEMP . $_file, "w");
  247. file_put_contents(DIR_TEMP . $_file, $data);
  248. fclose($fileTemp);
  249. return DIR_TEMP . $_file;
  250. }
  251. }