2
0

proweb.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. /**
  3. * Classe proweb
  4. *
  5. * Cette classe gère les opérations liées aux données Proweb, telles que la récupération, la suppression et la vérification des données.
  6. */
  7. class proweb
  8. {
  9. /**
  10. * Récupère les données de base des salariés Proweb.
  11. *
  12. * @return array Résultats des salariés Proweb.
  13. */
  14. public static function getBase()
  15. {
  16. db::query("SELECT *,
  17. IF(contrat = 1, 'Actif', 'Désactivé') AS texteContrat,
  18. IF(actif = 1, 'Actif', 'Désactivé') AS texteActif,
  19. IF(RHBase = 1, 'Présent', 'Absent') AS texteRHBase,
  20. IF(error = 1, 'Erreur', 'Valide') AS texteError
  21. FROM " . DB_T_SALARIES_PROWEB);
  22. return db::resultset();
  23. }
  24. /**
  25. * Récupère les informations d'un fichier Excel Proweb par son identifiant.
  26. *
  27. * @param int $_id Identifiant du fichier Excel.
  28. * @return array Informations du fichier Excel.
  29. */
  30. public static function getExcelById(int $_id)
  31. {
  32. db::query("SELECT * FROM " . DB_T_EXCEL_PROWEB . " WHERE id = :id");
  33. db::bind(':id', $_id);
  34. return db::single();
  35. }
  36. /**
  37. * Supprime un fichier Excel Proweb par son identifiant.
  38. *
  39. * @param int $_id Identifiant du fichier Excel.
  40. * @return bool Succès ou échec de la suppression.
  41. */
  42. public static function deleteExcel(int $_id)
  43. {
  44. $tmp = self::getExcelById($_id);
  45. file::delete($tmp["md5"]);
  46. db::query("DELETE FROM " . DB_T_EXCEL_PROWEB . " WHERE id = :id");
  47. db::bind(':id', $_id);
  48. try {
  49. db::execute();
  50. return TRUE;
  51. } catch (Exception $ex) {
  52. return FALSE;
  53. }
  54. }
  55. /**
  56. * Récupère les informations du dernier fichier Excel.
  57. *
  58. * @return array Informations du dernier fichier Excel.
  59. */
  60. public static function getExcel()
  61. {
  62. db::query("SELECT * FROM " . DB_T_EXCEL . " ORDER BY id DESC LIMIT 0, 1");
  63. return db::single();
  64. }
  65. /**
  66. * Convertit une date en format "MoisAnnée".
  67. *
  68. * @param string $datetime La date à convertir.
  69. * @return string La date convertie.
  70. */
  71. public static function convertDateMoisAn(string $datetime)
  72. {
  73. $pieces = explode(" ", $datetime);
  74. $pieces2 = explode("-", $pieces[0]);
  75. return $pieces2[0] . $pieces2[1];
  76. }
  77. /**
  78. * Convertit une date en format "JourMoisAnnée".
  79. *
  80. * @param string $datetime La date à convertir.
  81. * @return string La date convertie.
  82. */
  83. public static function convertDateMoisAnJour(string $datetime)
  84. {
  85. $pieces = explode(" ", $datetime);
  86. return $pieces[0];
  87. }
  88. /**
  89. * Archive les valeurs d'un fichier Excel Proweb.
  90. *
  91. * @param array $_array Données à archiver.
  92. * @return array Données archivées avec les erreurs éventuelles.
  93. */
  94. public static function archiveExcelValues(array $_array)
  95. {
  96. $_return["error"] = 0;
  97. $_return["errorJson"] = array();
  98. // ----
  99. $_return["idProweb"] = $_array[0];
  100. $_return["nom"] = self::archiveExcelValuesNom($_array[1]);
  101. $_return["prenom"] = self::archiveExcelValuesPrenom($_array[2]);
  102. $_return["sexe"] = self::archiveExcelValuesSexe($_array[3]);
  103. $_return["dateNaissance"] = self::archiveExcelValuesDate($_array[4]);
  104. $_return["jourEntree"] = self::archiveExcelValuesDate($_array[5]);
  105. $_return["jourSortie"] = self::archiveExcelValuesDate($_array[6]);
  106. $_return["meyclubSubv"] = self::archiveExcelValuesActif($_array[7]);
  107. $_return["meyclubAccess"] = self::archiveExcelValuesActif($_array[8]);
  108. $_return["Webaccess"] = self::archiveExcelValuesActif($_array[9]);
  109. $_return["lieu"] = self::archiveExcelValuesLieu($_array[10]);
  110. $_return["loginId"] = self::archiveExcelValuesloginId($_array[11]);
  111. $verifRH = self::checksalarieByLoginId($_return["loginId"], $_return["nom"], $_return["prenom"]);
  112. if ($_return["jourEntree"] != "N/A") { // On ignore les nouveaux salariés qui se sont inscrits post import du fichier RH
  113. $dateDataRH = intval(self::convertDateMoisAn(self::getExcel()["dateData"]));
  114. $dateDataProWeb = intval(self::convertDateMoisAn($_return["jourEntree"]));
  115. } else {
  116. $dateDataRH = $dateDataProWeb = 0;
  117. }
  118. if (empty($verifRH["id"]) and $_return["jourSortie"] == "N/A" and $dateDataRH > $dateDataProWeb) {
  119. $_return["contrat"] = $_return["actif"] = $_return["RHBase"] = $_return["loginIdRH"] = $_return["jourEntreeRH"] = "N/A";
  120. array_push($_return["errorJson"], "absent");
  121. } else {
  122. $_return["loginIdRH"] = (empty($verifRH["loginId"]) or $verifRH["loginId"] == $_return["loginId"]) ? NULL : $verifRH["loginId"];
  123. $_return["jourEntreeRH"] = (empty($verifRH["jourEntree"]) or self::convertDateMoisAnJour($verifRH["jourEntree"]) == $_return["jourEntree"]) ? NULL : self::convertDateMoisAnJour($verifRH["jourEntree"]);
  124. // Vérifier si jourSortie est une vraie date valide (pas NULL, pas vide, pas 0000-00-00)
  125. $jourSortieRH = $verifRH["jourSortie"] ?? null;
  126. $isValidJourSortie = !empty($jourSortieRH) && $jourSortieRH !== "0000-00-00" && $jourSortieRH !== "0000-00-00 00:00:00" && strpos($jourSortieRH, "0000") !== 0;
  127. $_return["jourSortie"] = $isValidJourSortie ? $jourSortieRH : $_return["jourSortie"];
  128. $_return["RHBase"] = ($isValidJourSortie or empty($verifRH["id"])) ? 0 : 1;
  129. $_return["actif"] = ($_return["meyclubSubv"] == 1 or $_return["meyclubAccess"] == 1 or $_return["Webaccess"] == 1) ? 1 : 0;
  130. $_return["contrat"] = (isset($verifRH["contrat"])) ? $verifRH["contrat"] : 0;
  131. // ----
  132. if (!self::checkValuesNom($_return["nom"])) {
  133. $_return["error"] = 1;
  134. array_push($_return["errorJson"], "nom");
  135. }
  136. if (!self::checkValuesPrenom($_return["prenom"])) {
  137. $_return["error"] = 1;
  138. array_push($_return["errorJson"], "prenom");
  139. }
  140. if (!self::checkValuesloginId($_return["loginId"], $_return["loginIdRH"])) {
  141. $_return["error"] = 1;
  142. array_push($_return["errorJson"], "loginId");
  143. }
  144. if (!self::checkValuesNaissance($_return["dateNaissance"])) {
  145. $_return["error"] = 1;
  146. array_push($_return["errorJson"], "dateNaissance");
  147. }
  148. if (!self::checkValuesJourEntree($_return["jourEntree"])) {
  149. $_return["error"] = 1;
  150. array_push($_return["errorJson"], "jourEntree");
  151. }
  152. if ($_return["jourEntreeRH"] != NULL and $_return["jourEntree"] != "N/A") {
  153. $_return["error"] = 1;
  154. array_push($_return["errorJson"], "jourEntree");
  155. }
  156. if ($_return["jourSortie"] != "N/A" and $_return["RHBase"] == 1) {
  157. $_return["error"] = 1;
  158. array_push($_return["errorJson"], "jourSortie");
  159. }
  160. if ($_return["jourEntree"] != "N/A") { // On ignore les nouveaux salariés qui se sont inscrits post import du fichier RH
  161. if ($dateDataRH > $dateDataProWeb) {
  162. if (!self::checkValuesRHBase($_return["RHBase"], $_return["actif"])) {
  163. $_return["error"] = 1;
  164. array_push($_return["errorJson"], "RHBase");
  165. }
  166. }
  167. }
  168. }
  169. $_return["errorJson"] = (!empty($_return["errorJson"][0])) ? json_encode($_return["errorJson"]) : NULL;
  170. return $_return;
  171. }
  172. /**
  173. * Vérifie les données d'un salarié par son identifiant de connexion.
  174. *
  175. * @param string $_loginId Identifiant de connexion du salarié.
  176. * @param string|null $_nom Nom du salarié (optionnel).
  177. * @param string|null $_prenom Prénom du salarié (optionnel).
  178. * @return array Données du salarié vérifié.
  179. */
  180. public static function checkSalarieByLoginId(string $_loginId, ?string $_nom = NULL, ?string $_prenom = NULL)
  181. {
  182. // Récupération des données de l'excel au format Json
  183. if ($_nom == NULL) {
  184. db::query("SELECT id, loginId, jourEntree, jourSortie, contrat FROM " . DB_T_SALARIES . " WHERE loginId = :loginId");
  185. } else {
  186. db::query("SELECT id, loginId, jourEntree, jourSortie, contrat FROM " . DB_T_SALARIES . " WHERE loginId = :loginId OR (nom = :nom AND prenom = :prenom)");
  187. db::bind(':nom', $_nom);
  188. db::bind(':prenom', $_prenom);
  189. }
  190. db::bind(':loginId', $_loginId);
  191. return db::single();
  192. }
  193. /**
  194. * Télécharge un fichier Excel Proweb par son identifiant.
  195. *
  196. * @param int $_id Identifiant du fichier Excel.
  197. * @param string $_file Nom du fichier temporaire.
  198. * @return string Chemin du fichier téléchargé.
  199. */
  200. public static function downloadExcel(int $_id, string $_file)
  201. {
  202. $data = base64_decode(self::getExcelById($_id)["file"]);
  203. $fileTemp = fopen(DIR_TEMP . $_file, "w");
  204. file_put_contents(DIR_TEMP . $_file, $data);
  205. fclose($fileTemp);
  206. return DIR_TEMP . $_file;
  207. }
  208. /**
  209. * Convertit une date en format approprié pour l'archivage.
  210. *
  211. * @param string $_string La date à convertir.
  212. * @return string La date convertie ou "N/A" si invalide.
  213. */
  214. private static function archiveExcelValuesDate(string $_string)
  215. {
  216. if (!isset($_string) or $_string == "00-00-0000" or $_string == "" or empty($_string)) {
  217. return "N/A";
  218. } else {
  219. return str_replace(" 00:00:00", "", $_string);
  220. }
  221. }
  222. /**
  223. * Convertit le sexe en format approprié pour l'archivage.
  224. *
  225. * @param string $_string Le sexe à convertir.
  226. * @return string Le sexe converti ou "N/A" si invalide.
  227. */
  228. private static function archiveExcelValuesSexe(string $_string)
  229. {
  230. return (empty($_string)) ? "N/A" : $_string;
  231. }
  232. /**
  233. * Nettoie et convertit un identifiant de connexion pour l'archivage.
  234. *
  235. * @param string $_string L'identifiant à convertir.
  236. * @return string L'identifiant nettoyé.
  237. */
  238. private static function archiveExcelValuesloginId(string $_string)
  239. {
  240. return core::cleanAccent($_string);
  241. }
  242. /**
  243. * Nettoie et convertit un nom pour l'archivage.
  244. *
  245. * @param string $_string Le nom à convertir.
  246. * @return string Le nom nettoyé.
  247. */
  248. private static function archiveExcelValuesNom(string $_string)
  249. {
  250. return core::cleanAccent($_string);
  251. }
  252. /**
  253. * Nettoie et convertit un prénom pour l'archivage.
  254. *
  255. * @param string $_string Le prénom à convertir.
  256. * @return string Le prénom nettoyé.
  257. */
  258. private static function archiveExcelValuesPrenom(string $_string)
  259. {
  260. return core::cleanAccent($_string);
  261. }
  262. /**
  263. * Convertit une valeur d'activité pour l'archivage.
  264. *
  265. * @param string $_string La valeur à convertir.
  266. * @return int|string La valeur convertie ou "N/A" si invalide.
  267. */
  268. private static function archiveExcelValuesActif(string $_string)
  269. {
  270. if ($_string == "O") {
  271. return 1;
  272. } elseif ($_string == "N") {
  273. return 0;
  274. } else {
  275. return "N/A";
  276. }
  277. }
  278. /**
  279. * Nettoie et convertit un lieu pour l'archivage.
  280. *
  281. * @param string $_string Le lieu à convertir.
  282. * @return string Le lieu nettoyé ou "N/A" si invalide.
  283. */
  284. private static function archiveExcelValuesLieu(string $_string)
  285. {
  286. return (empty($_string)) ? "N/A" : $_string;
  287. }
  288. /**
  289. * Vérifie si une valeur d'activité est valide.
  290. *
  291. * @param string $_string La valeur à vérifier.
  292. * @return bool TRUE si valide, FALSE sinon.
  293. */
  294. private static function checkValuesActif(string $_string)
  295. {
  296. return ($_string == "N/A") ? FALSE : TRUE;
  297. }
  298. /**
  299. * Vérifie si un identifiant de connexion est valide.
  300. *
  301. * @param string $_idLogin Identifiant de connexion.
  302. * @param string|null $_idLoginRH Identifiant RH (optionnel).
  303. * @return bool TRUE si valide, FALSE sinon.
  304. */
  305. private static function checkValuesloginId(string $_idLogin, ?string $_idLoginRH = NULL)
  306. {
  307. if ($_idLoginRH != NULL) {
  308. if (empty(self::checkSalarieByLoginId($_idLogin)["id"])) {
  309. return FALSE;
  310. } elseif (core::checkStringOnly($_idLogin)) {
  311. return FALSE;
  312. } elseif (strlen($_idLogin) > 10) {
  313. return FALSE;
  314. } elseif (empty($_idLogin)) {
  315. return FALSE;
  316. } else {
  317. return TRUE;
  318. }
  319. } else {
  320. return TRUE;
  321. }
  322. }
  323. /**
  324. * Vérifie si une valeur RHBase est valide.
  325. *
  326. * @param int $_RH Valeur RHBase.
  327. * @param int $_actif Valeur actif.
  328. * @return bool TRUE si valide, FALSE sinon.
  329. */
  330. private static function checkValuesRHBase(int $_RH, int $_actif)
  331. {
  332. return ($_RH == 0 and $_actif == 1) ? FALSE : TRUE;
  333. }
  334. /**
  335. * Vérifie si une date d'entrée est valide.
  336. *
  337. * @param string $_string La date d'entrée.
  338. * @return bool TRUE si valide, FALSE sinon.
  339. */
  340. private static function checkValuesJourEntree(string $_string)
  341. {
  342. return ($_string == "N/A") ? FALSE : TRUE;
  343. }
  344. /**
  345. * Vérifie si une date de sortie est valide.
  346. *
  347. * @param string $_string La date de sortie.
  348. * @param int $_actif Valeur actif.
  349. * @return bool TRUE si valide, FALSE sinon.
  350. */
  351. private static function checkValuesJourSortie(string $_string, int $_actif)
  352. {
  353. return ($_string != "N/A" and $_actif == 1) ? FALSE : TRUE;
  354. }
  355. /**
  356. * Vérifie si un nom est valide.
  357. *
  358. * @param string $_string Le nom à vérifier.
  359. * @return bool TRUE si valide, FALSE sinon.
  360. */
  361. private static function checkValuesNom(string $_string)
  362. {
  363. return (empty($_string)) ? FALSE : TRUE;
  364. }
  365. /**
  366. * Vérifie si un prénom est valide.
  367. *
  368. * @param string $_string Le prénom à vérifier.
  369. * @return bool TRUE si valide, FALSE sinon.
  370. */
  371. private static function checkValuesPrenom(string $_string)
  372. {
  373. return (empty($_string)) ? FALSE : TRUE;
  374. }
  375. /**
  376. * Vérifie si un sexe est valide.
  377. *
  378. * @param string $_string Le sexe à vérifier.
  379. * @return bool TRUE si valide, FALSE sinon.
  380. */
  381. private static function checkValuesSexe(string $_string)
  382. {
  383. return ($_string == NULL or ($_string != "M." and $_string != "Mme")) ? FALSE : TRUE;
  384. }
  385. /**
  386. * Vérifie si un lieu est valide.
  387. *
  388. * @param string $_string Le lieu à vérifier.
  389. * @return bool TRUE si valide, FALSE sinon.
  390. */
  391. private static function checkValuesLieu(string $_string)
  392. {
  393. return (empty($_string)) ? FALSE : TRUE;
  394. }
  395. /**
  396. * Vérifie si une date de naissance est valide.
  397. *
  398. * @param string $_string La date de naissance.
  399. * @return bool TRUE si valide, FALSE sinon.
  400. */
  401. private static function checkValuesNaissance(string $_string)
  402. {
  403. if (empty($_string) or $_string == "N/A") {
  404. return FALSE;
  405. } elseif (self::calculAge($_string) > core::getConfig("AGE_MAX_ERROR")) {
  406. return FALSE;
  407. } else {
  408. return TRUE;
  409. }
  410. }
  411. /**
  412. * Calcule l'âge à partir d'une date de naissance.
  413. *
  414. * @param string $_date La date de naissance.
  415. * @return int L'âge calculé.
  416. */
  417. private static function calculAge(string $_date)
  418. {
  419. $pieces = explode(" ", $_date);
  420. $birthDate = explode("-", $pieces[0]);
  421. return (date("md", date("U", mktime(0, 0, 0, $birthDate[2], $birthDate[1], $birthDate[0]))) > date("md")
  422. ? ((date("Y") - $birthDate[0]) - 1)
  423. : (date("Y") - $birthDate[0]));
  424. }
  425. }