2
0

proweb.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. // Utiliser !empty() au lieu de isset() pour vérifier si jourSortie a une valeur non-nulle
  125. $_return["jourSortie"] = (!empty($verifRH["jourSortie"])) ? $verifRH["jourSortie"] : $_return["jourSortie"];
  126. $_return["RHBase"] = (!empty($verifRH["jourSortie"]) or empty($verifRH["id"])) ? 0 : 1;
  127. $_return["actif"] = ($_return["meyclubSubv"] == 1 or $_return["meyclubAccess"] == 1 or $_return["Webaccess"] == 1) ? 1 : 0;
  128. $_return["contrat"] = (isset($verifRH["contrat"])) ? $verifRH["contrat"] : 0;
  129. // ----
  130. if (!self::checkValuesNom($_return["nom"])) {
  131. $_return["error"] = 1;
  132. array_push($_return["errorJson"], "nom");
  133. }
  134. if (!self::checkValuesPrenom($_return["prenom"])) {
  135. $_return["error"] = 1;
  136. array_push($_return["errorJson"], "prenom");
  137. }
  138. if (!self::checkValuesloginId($_return["loginId"], $_return["loginIdRH"])) {
  139. $_return["error"] = 1;
  140. array_push($_return["errorJson"], "loginId");
  141. }
  142. if (!self::checkValuesNaissance($_return["dateNaissance"])) {
  143. $_return["error"] = 1;
  144. array_push($_return["errorJson"], "dateNaissance");
  145. }
  146. if (!self::checkValuesJourEntree($_return["jourEntree"])) {
  147. $_return["error"] = 1;
  148. array_push($_return["errorJson"], "jourEntree");
  149. }
  150. if ($_return["jourEntreeRH"] != NULL and $_return["jourEntree"] != "N/A") {
  151. $_return["error"] = 1;
  152. array_push($_return["errorJson"], "jourEntree");
  153. }
  154. if ($_return["jourSortie"] != "N/A" and $_return["RHBase"] == 1) {
  155. $_return["error"] = 1;
  156. array_push($_return["errorJson"], "jourSortie");
  157. }
  158. if ($_return["jourEntree"] != "N/A") { // On ignore les nouveaux salariés qui se sont inscrits post import du fichier RH
  159. if ($dateDataRH > $dateDataProWeb) {
  160. if (!self::checkValuesRHBase($_return["RHBase"], $_return["actif"])) {
  161. $_return["error"] = 1;
  162. array_push($_return["errorJson"], "RHBase");
  163. }
  164. }
  165. }
  166. }
  167. $_return["errorJson"] = (!empty($_return["errorJson"][0])) ? json_encode($_return["errorJson"]) : NULL;
  168. return $_return;
  169. }
  170. /**
  171. * Vérifie les données d'un salarié par son identifiant de connexion.
  172. *
  173. * @param string $_loginId Identifiant de connexion du salarié.
  174. * @param string|null $_nom Nom du salarié (optionnel).
  175. * @param string|null $_prenom Prénom du salarié (optionnel).
  176. * @return array Données du salarié vérifié.
  177. */
  178. public static function checkSalarieByLoginId(string $_loginId, ?string $_nom = NULL, ?string $_prenom = NULL)
  179. {
  180. // Récupération des données de l'excel au format Json
  181. if ($_nom == NULL) {
  182. db::query("SELECT id, loginId, jourEntree, jourSortie, contrat FROM " . DB_T_SALARIES . " WHERE loginId = :loginId");
  183. } else {
  184. db::query("SELECT id, loginId, jourEntree, jourSortie, contrat FROM " . DB_T_SALARIES . " WHERE loginId = :loginId OR (nom = :nom AND prenom = :prenom)");
  185. db::bind(':nom', $_nom);
  186. db::bind(':prenom', $_prenom);
  187. }
  188. db::bind(':loginId', $_loginId);
  189. return db::single();
  190. }
  191. /**
  192. * Télécharge un fichier Excel Proweb par son identifiant.
  193. *
  194. * @param int $_id Identifiant du fichier Excel.
  195. * @param string $_file Nom du fichier temporaire.
  196. * @return string Chemin du fichier téléchargé.
  197. */
  198. public static function downloadExcel(int $_id, string $_file)
  199. {
  200. $data = base64_decode(self::getExcelById($_id)["file"]);
  201. $fileTemp = fopen(DIR_TEMP . $_file, "w");
  202. file_put_contents(DIR_TEMP . $_file, $data);
  203. fclose($fileTemp);
  204. return DIR_TEMP . $_file;
  205. }
  206. /**
  207. * Convertit une date en format approprié pour l'archivage.
  208. *
  209. * @param string $_string La date à convertir.
  210. * @return string La date convertie ou "N/A" si invalide.
  211. */
  212. private static function archiveExcelValuesDate(string $_string)
  213. {
  214. if (!isset($_string) or $_string == "00-00-0000") {
  215. return "N/A";
  216. } else {
  217. return str_replace(" 00:00:00", "", $_string);
  218. }
  219. }
  220. /**
  221. * Convertit le sexe en format approprié pour l'archivage.
  222. *
  223. * @param string $_string Le sexe à convertir.
  224. * @return string Le sexe converti ou "N/A" si invalide.
  225. */
  226. private static function archiveExcelValuesSexe(string $_string)
  227. {
  228. return (empty($_string)) ? "N/A" : $_string;
  229. }
  230. /**
  231. * Nettoie et convertit un identifiant de connexion pour l'archivage.
  232. *
  233. * @param string $_string L'identifiant à convertir.
  234. * @return string L'identifiant nettoyé.
  235. */
  236. private static function archiveExcelValuesloginId(string $_string)
  237. {
  238. return core::cleanAccent($_string);
  239. }
  240. /**
  241. * Nettoie et convertit un nom pour l'archivage.
  242. *
  243. * @param string $_string Le nom à convertir.
  244. * @return string Le nom nettoyé.
  245. */
  246. private static function archiveExcelValuesNom(string $_string)
  247. {
  248. return core::cleanAccent($_string);
  249. }
  250. /**
  251. * Nettoie et convertit un prénom pour l'archivage.
  252. *
  253. * @param string $_string Le prénom à convertir.
  254. * @return string Le prénom nettoyé.
  255. */
  256. private static function archiveExcelValuesPrenom(string $_string)
  257. {
  258. return core::cleanAccent($_string);
  259. }
  260. /**
  261. * Convertit une valeur d'activité pour l'archivage.
  262. *
  263. * @param string $_string La valeur à convertir.
  264. * @return int|string La valeur convertie ou "N/A" si invalide.
  265. */
  266. private static function archiveExcelValuesActif(string $_string)
  267. {
  268. if ($_string == "O") {
  269. return 1;
  270. } elseif ($_string == "N") {
  271. return 0;
  272. } else {
  273. return "N/A";
  274. }
  275. }
  276. /**
  277. * Nettoie et convertit un lieu pour l'archivage.
  278. *
  279. * @param string $_string Le lieu à convertir.
  280. * @return string Le lieu nettoyé ou "N/A" si invalide.
  281. */
  282. private static function archiveExcelValuesLieu(string $_string)
  283. {
  284. return (empty($_string)) ? "N/A" : $_string;
  285. }
  286. /**
  287. * Vérifie si une valeur d'activité est valide.
  288. *
  289. * @param string $_string La valeur à vérifier.
  290. * @return bool TRUE si valide, FALSE sinon.
  291. */
  292. private static function checkValuesActif(string $_string)
  293. {
  294. return ($_string == "N/A") ? FALSE : TRUE;
  295. }
  296. /**
  297. * Vérifie si un identifiant de connexion est valide.
  298. *
  299. * @param string $_idLogin Identifiant de connexion.
  300. * @param string|null $_idLoginRH Identifiant RH (optionnel).
  301. * @return bool TRUE si valide, FALSE sinon.
  302. */
  303. private static function checkValuesloginId(string $_idLogin, ?string $_idLoginRH = NULL)
  304. {
  305. if ($_idLoginRH != NULL) {
  306. if (empty(self::checkSalarieByLoginId($_idLogin)["id"])) {
  307. return FALSE;
  308. } elseif (core::checkStringOnly($_idLogin)) {
  309. return FALSE;
  310. } elseif (strlen($_idLogin) > 10) {
  311. return FALSE;
  312. } elseif (empty($_idLogin)) {
  313. return FALSE;
  314. } else {
  315. return TRUE;
  316. }
  317. } else {
  318. return TRUE;
  319. }
  320. }
  321. /**
  322. * Vérifie si une valeur RHBase est valide.
  323. *
  324. * @param int $_RH Valeur RHBase.
  325. * @param int $_actif Valeur actif.
  326. * @return bool TRUE si valide, FALSE sinon.
  327. */
  328. private static function checkValuesRHBase(int $_RH, int $_actif)
  329. {
  330. return ($_RH == 0 and $_actif == 1) ? FALSE : TRUE;
  331. }
  332. /**
  333. * Vérifie si une date d'entrée est valide.
  334. *
  335. * @param string $_string La date d'entrée.
  336. * @return bool TRUE si valide, FALSE sinon.
  337. */
  338. private static function checkValuesJourEntree(string $_string)
  339. {
  340. return ($_string == "N/A") ? FALSE : TRUE;
  341. }
  342. /**
  343. * Vérifie si une date de sortie est valide.
  344. *
  345. * @param string $_string La date de sortie.
  346. * @param int $_actif Valeur actif.
  347. * @return bool TRUE si valide, FALSE sinon.
  348. */
  349. private static function checkValuesJourSortie(string $_string, int $_actif)
  350. {
  351. return ($_string != "N/A" and $_actif == 1) ? FALSE : TRUE;
  352. }
  353. /**
  354. * Vérifie si un nom est valide.
  355. *
  356. * @param string $_string Le nom à vérifier.
  357. * @return bool TRUE si valide, FALSE sinon.
  358. */
  359. private static function checkValuesNom(string $_string)
  360. {
  361. return (empty($_string)) ? FALSE : TRUE;
  362. }
  363. /**
  364. * Vérifie si un prénom est valide.
  365. *
  366. * @param string $_string Le prénom à vérifier.
  367. * @return bool TRUE si valide, FALSE sinon.
  368. */
  369. private static function checkValuesPrenom(string $_string)
  370. {
  371. return (empty($_string)) ? FALSE : TRUE;
  372. }
  373. /**
  374. * Vérifie si un sexe est valide.
  375. *
  376. * @param string $_string Le sexe à vérifier.
  377. * @return bool TRUE si valide, FALSE sinon.
  378. */
  379. private static function checkValuesSexe(string $_string)
  380. {
  381. return ($_string == NULL or ($_string != "M." and $_string != "Mme")) ? FALSE : TRUE;
  382. }
  383. /**
  384. * Vérifie si un lieu est valide.
  385. *
  386. * @param string $_string Le lieu à vérifier.
  387. * @return bool TRUE si valide, FALSE sinon.
  388. */
  389. private static function checkValuesLieu(string $_string)
  390. {
  391. return (empty($_string)) ? FALSE : TRUE;
  392. }
  393. /**
  394. * Vérifie si une date de naissance est valide.
  395. *
  396. * @param string $_string La date de naissance.
  397. * @return bool TRUE si valide, FALSE sinon.
  398. */
  399. private static function checkValuesNaissance(string $_string)
  400. {
  401. if (empty($_string) or $_string == "N/A") {
  402. return FALSE;
  403. } elseif (self::calculAge($_string) > core::getConfig("AGE_MAX_ERROR")) {
  404. return FALSE;
  405. } else {
  406. return TRUE;
  407. }
  408. }
  409. /**
  410. * Calcule l'âge à partir d'une date de naissance.
  411. *
  412. * @param string $_date La date de naissance.
  413. * @return int L'âge calculé.
  414. */
  415. private static function calculAge(string $_date)
  416. {
  417. $pieces = explode(" ", $_date);
  418. $birthDate = explode("-", $pieces[0]);
  419. return (date("md", date("U", mktime(0, 0, 0, $birthDate[2], $birthDate[1], $birthDate[0]))) > date("md")
  420. ? ((date("Y") - $birthDate[0]) - 1)
  421. : (date("Y") - $birthDate[0]));
  422. }
  423. }