2
0

proweb.class.php 16 KB

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