pdf_horarioadministrativo.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /* AJAX
  3. * Genera nombramientos en pdf
  4. * Recibe:
  5. * id (de usuario)
  6. * tipo (2 o 3)
  7. * Return:
  8. * imprime pdf
  9. */
  10. setlocale(LC_TIME, 'es_MX.UTF-8');
  11. require_once("../../include/nocache.php");
  12. require_once("../../include/constantes.php");
  13. require_once("../../include/bd_pdo.php");
  14. require_once("../../include/util.php");
  15. require_once("../../classes/ValidaSesion.php");
  16. include_once('../../include/xTemplate/xtemplate.class.php'); // including mpdf.php
  17. include_once('../../include/mpdf/autoload.php'); // including mpdf.php
  18. function validaHorarios(& $arr){
  19. $erroresArr = array();
  20. for($i = 0; $i < count($arr)-1; $i++){
  21. for($j = $i+1; $j < count($arr); $j++){
  22. if($arr[$i]["Dia_id"] == $arr[$j]["Dia_id"] && !($arr[$i]["TipoHorario_id"] == 3 && $arr[$i]["TipoHorario_id"] == $arr[$j]["TipoHorario_id"])){
  23. if( (date('H:i', strtotime($arr[$i]["Hora_inicio"])) <= date('H:i', strtotime($arr[$j]["Hora_inicio"])) && date('H:i', strtotime($arr[$i]["Hora_final"])) > date('H:i', strtotime($arr[$j]["Hora_inicio"])) )
  24. || (date('H:i', strtotime($arr[$j]["Hora_inicio"])) <= date('H:i', strtotime($arr[$i]["Hora_inicio"])) && date('H:i', strtotime($arr[$j]["Hora_final"])) > date('H:i', strtotime($arr[$i]["Hora_inicio"])) )
  25. ){
  26. if($arr[$i]["TipoHorario_id"] < $arr[$j]["TipoHorario_id"]){
  27. $erroresArr[] = array('id'=> $arr[$i]["Horario_id"], 'nombre'=>$arr[$i]["TipoHorario_desc"], 'dia'=> diaNombre($arr[$i]["Dia_id"]), 'hora_i'=>date('H:i', strtotime($arr[$i]["Hora_inicio"])), 'hora_f'=> date('H:i', strtotime($arr[$i]["Hora_final"])));
  28. $arr[$i]["Duracion"] = (strtotime('2019-01-01 '.$arr[$j]["Hora_inicio"]) - strtotime('2019-01-01 '.$arr[$i]["Hora_inicio"]))/60;
  29. if($arr[$i]["Duracion"] <= 0) array_splice($arr, $i, 1);
  30. }else{ //if($arr[$i]["TipoHorario_id"] < $arr[$j]["TipoHorario_id"])
  31. $erroresArr[] = array('id'=> $arr[$j]["Horario_id"], 'nombre'=>$arr[$j]["TipoHorario_desc"], 'dia'=> diaNombre($arr[$j]["Dia_id"]), 'hora_i'=>date('H:i', strtotime($arr[$j]["Hora_inicio"])), 'hora_f'=> date('H:i', strtotime($arr[$j]["Hora_final"])));
  32. $arr[$j]["Duracion"] = (strtotime('2019-01-01 '.$arr[$i]["Hora_inicio"]) - strtotime('2019-01-01 '.$arr[$j]["Hora_inicio"]))/60;
  33. if($arr[$j]["Duracion"] <= 0) array_splice($arr, $j, 1);
  34. }
  35. }
  36. }
  37. }
  38. }
  39. return $erroresArr;
  40. }
  41. $pag = "../horarioautorizacion.php";
  42. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  43. $objSesion = new ValidaSesion($pdo, NULL, APSA);
  44. if(!$objSesion->tieneAcceso()){
  45. header("Location: ".$pag."?error=3");
  46. exit();
  47. }
  48. unset($objValida);
  49. if(!isset($_POST["id"]) || !isset($_POST["tipo"]) || !isset($_POST["fecha"]) || $_POST["fecha"] == ""){
  50. header("Location: ".$pag."?error=0");
  51. exit();
  52. }
  53. $usr = filter_input(INPUT_POST, "id", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  54. $tipo = filter_input(INPUT_POST, "tipo", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  55. $filter_fecha = fechaGuion(trim(filter_input(INPUT_POST, "fecha", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  56. if(isset($_POST["sup"]))
  57. $sup = filter_input(INPUT_POST, "sup", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  58. $puesto ="";
  59. $area = "";
  60. $stmt = $pdo->prepare('Select * from fs_subordinadoHorario(:usr) WHERE "Usuario_id" = :usr_sub');//con horarios en autorizacion
  61. if($usr != $_SESSION["usuario_id"]){//No se busca a si mismo, busca subordinados
  62. //--- Valida que sea subordinado ---
  63. $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
  64. }else{
  65. $stmt->bindParam(":usr", $sup);
  66. }
  67. $stmt->bindParam(":usr_sub", $usr);
  68. if(!$stmt->execute()){
  69. //$t = $stmt->errorInfo();
  70. echo "Ocurrió un error al leer tus subordinados.";
  71. exit();
  72. }
  73. $rs = $stmt->fetch();
  74. $stmt->closeCursor();
  75. $stmt = $pdo->prepare('Select * from fs_director()');
  76. if(!$stmt->execute()){
  77. $return["error"] .= "Ocurrió un error al obtener datos del director.";
  78. $error = true;
  79. }
  80. $director_rs = $stmt->fetch();
  81. $stmt->closeCursor();
  82. if($director_rs["Usuario_id"] != $_SESSION["usuario_id"] && (!isset($rs) || !is_array($rs) || count($rs) == 0 || $rs["Usuario_id"] != $usr)){
  83. //print_r($rs);
  84. echo "No puedes revisar el horario para ese usuario.";
  85. exit();
  86. }
  87. if(!isset($_SESSION["periodo_id"]) || $_SESSION["periodo_id"] ==""){
  88. echo "Necesitas seleccionar un periodo.";
  89. exit();
  90. }
  91. $puesto = $rs["Puesto_desc"];
  92. $puesto_id = $rs["Puesto_id"];
  93. $area = $rs["AreaAdministrativa_desc"];
  94. if(isset($_POST["self"]) && $director_rs["Usuario_id"] == $_SESSION["usuario_id"]){
  95. $puesto = $director_rs["Puesto_desc"];
  96. $puesto_id = $director_rs["Puesto_id"];
  97. $area = $director_rs["AreaAdministrativa_desc"];
  98. }
  99. unset($rs);
  100. if($tipo != 3 && $tipo != 2){
  101. echo "Los parámetros del horario son incorrectos.";
  102. exit();
  103. }
  104. //Obtiene horario del usuario
  105. $stmt = $pdo->prepare('Select * from fs_mihorariofull(:usr, :fecha, :tipoauto)');//Obtiene todo el calendario
  106. $stmt->bindParam(":fecha", $filter_fecha);
  107. $stmt->bindParam(":usr", $usr);
  108. $stmt->bindParam(":tipoauto", $tipo);
  109. if(!$stmt->execute()){
  110. //$t = $stmt->errorInfo();
  111. echo "Ocurrió un error al obtener los horarios ";
  112. exit();
  113. }
  114. $horario_rs = $stmt->fetchAll();
  115. /*$horaMin = horaMin($horario_rs, "Hora_inicio");
  116. $horaMax = horaMax($horario_rs, "Hora_final");*/
  117. $horaMin = HORA_INICIO.":00";
  118. $horaMax = HORA_FINAL.":00";
  119. $stmt->closeCursor();
  120. $erroresArr = validaHorarios($horario_rs);
  121. if(count($erroresArr) > 0){
  122. print_r($erroresArr);
  123. echo '<p>El calendario tiene conflitos con el periodo actual y no se puede mostrar.</p>';
  124. foreach($erroresArr as $err){
  125. echo '<p>El horario administrativo se sobrepone al bloque <b>'.$err['nombre'].' ( '.$err['dia'].' '.$err['hora_i'].' - '.$err['hora_f'].' )</b>';
  126. }
  127. echo '<p>Elige un periodo diferente, o solicita al usuario cambie su horario administrativo.</p>';
  128. echo '<p><button type="button" onclick="window.close();">Regresar</button></p>';
  129. exit();
  130. }
  131. //---
  132. $stmt = $pdo->prepare('Select * from fs_dia(NULL)');
  133. if(!$stmt->execute()){
  134. echo "Error al obtener los días";
  135. exit();
  136. }
  137. $dias_rs = $stmt->fetchAll();
  138. $stmt->closeCursor();
  139. //---
  140. $stmt = $pdo->prepare('Select * from fs_usuario(:usr)');
  141. $stmt->bindParam(":usr", $usr);
  142. if(!$stmt->execute()){
  143. echo "Error al obtener los datos del usuario";
  144. exit();
  145. }
  146. $usr_rs = $stmt->fetch();
  147. $stmt->closeCursor();
  148. $nombre_usr = $usr_rs["Usuario_apellidos"]." ".$usr_rs["Usuario_nombre"];
  149. unset($usr_rs);
  150. //Obtiene 2 corrreos
  151. $stmt = $pdo->prepare('Select * from fs_contacto(:id, 3 , NULL) ORDER BY "PerfilContacto_id"');//3 correo, Null todos
  152. $stmt->bindParam(":id", $usr);
  153. if(!$stmt->execute()){
  154. echo "Error al obtener los datos del correo";
  155. exit();
  156. }else{
  157. $contacto_rs = $stmt->fetchAll();
  158. $correos = array();
  159. $c_i = 0;
  160. $c_j = 0;
  161. for($c_j=0; $c_j< count($contacto_rs); $c_j++ ){
  162. if( strpos($contacto_rs[$c_j]["Contacto_valor"], "lasalle.mx") !== false || strpos($contacto_rs[$c_j]["Contacto_valor"], "lasallistas.org.mx") !== false ){
  163. $correos[] = $contacto_rs[$c_j]["Contacto_valor"];
  164. $c_i++;
  165. }
  166. }
  167. $datos["correo1"] = "";
  168. $datos["correo2"] = "";
  169. if($c_i>0){
  170. $datos["correo1"] = $correos[0];
  171. }
  172. if($c_i>1){
  173. $datos["correo2"] = $correos[1];
  174. }
  175. /*$i = 1;
  176. foreach($contacto_rs as $correo){
  177. $datos["correo".$i] = $correo["Contacto_valor"];
  178. $i++;
  179. }
  180. //Si el primer correo no es institucional, es personal y se va a campo 2
  181. if( strpos($datos["correo1"], "lasalle.mx") === false && strpos($datos["correo1"], "lasallistas.org.mx") === false ){
  182. $tmp = $datos["correo2"];
  183. $datos["correo2"] = $datos["correo1"];
  184. $datos["correo1"] = $tmp;
  185. }*/
  186. }
  187. $stmt->closeCursor(); // cierra conexion de resultado
  188. //---
  189. if(!isset($sup)){
  190. $stmt = $pdo->prepare('Select * from fs_superiorusuario(:usr, :puesto)');
  191. $stmt->bindParam(":usr", $usr);
  192. $stmt->bindParam(":puesto", $puesto_id);
  193. if(!$stmt->execute()){
  194. echo "Error al obtener los datos del usuario superior";
  195. exit();
  196. }
  197. $usr_rs = $stmt->fetch();
  198. $stmt->closeCursor();
  199. }else{
  200. $stmt = $pdo->prepare('Select * from fs_usuario(:usr)');
  201. $stmt->bindParam(":usr", $sup);
  202. if(!$stmt->execute()){
  203. echo "Error al obtener los datos del usuario";
  204. exit();
  205. }
  206. $usr_rs = $stmt->fetch();
  207. $stmt->closeCursor();
  208. }
  209. $nombreSup_usr = $usr_rs["Usuario_apellidos"]." ".$usr_rs["Usuario_nombre"];
  210. unset($usr_rs);
  211. $stmt = $pdo->prepare('Select * from fs_tipoautorizacionhorario(:tipoauto)');
  212. $stmt->bindParam(":tipoauto", $tipo);
  213. $stmt->execute();
  214. $tipo_rs = $stmt->fetch();
  215. $stmt->closeCursor();
  216. $tipo_nombre = $tipo_rs["TipoAutorizacionHorario_desc"];
  217. unset($tipo_rs);
  218. //-----
  219. $mpdf = new \Mpdf\Mpdf([
  220. 'mode' => 'utf-8',
  221. 'format' => [215, 279],
  222. 'orientation' => 'P'
  223. ]);
  224. //$mpdf->SetDisplayMode('fullpage');
  225. $stylesheet = "<style>";
  226. $stylesheet .= file_get_contents('../../css/indivisa.css'); // external css
  227. $stylesheet .= file_get_contents('../css/mihorario_reporte.css'); // external css
  228. $stylesheet .= "</style>";
  229. //echo $stylesheet;
  230. $xtpl = new XTemplate('../tpl/mihorario_reporte.tpl.html');
  231. $xtpl->assign("NOMBRE", $nombre_usr);
  232. $xtpl->assign("PUESTO", $puesto);
  233. $xtpl->assign("AREA", $area);
  234. if($datos["correo1"] != ""){
  235. $xtpl->assign("CORREO", $datos["correo1"]);
  236. $xtpl->parse("main.datos.correo");
  237. }
  238. if($datos["correo2"] != ""){
  239. $xtpl->assign("CORREO", $datos["correo2"]);
  240. $xtpl->parse("main.datos.correo");
  241. }
  242. $xtpl->assign("SUPERIOR", $nombreSup_usr);
  243. $xtpl->assign("HOY", date("d/m/Y"));
  244. $xtpl->assign("PERIODO", $_SESSION["periodo_desc"]);
  245. //$xtpl->assign("RIGHT", "Horario: ".$tipo_nombre);
  246. $xtpl->parse("main.datos");
  247. foreach($dias_rs as $dia){
  248. $xtpl->assign("DIA", $dia["Dia_desc"]);
  249. $xtpl->parse("main.header");
  250. }
  251. $rs_i = 0;
  252. $spacerArr = array(1=>0,0,0,0,0,0);
  253. $total_tipo = array(1=>0,0,0);
  254. //$horas_dif = intval(date('H', strtotime($horaMax))) - intval(date('H', strtotime($horaMin)));
  255. for($h = date('H', strtotime($horaMin)); $h < date('H', strtotime($horaMax)); $h++){
  256. $xtpl->assign("HORA", date('H', strtotime($h.":00")));
  257. $xtpl->assign("FRACCIONES", FRACCION_HORA);
  258. $xtpl->parse("main.hora_row.hora");
  259. for($f = 0; $f < FRACCION_HORA; $f++){
  260. foreach($dias_rs as $dia){
  261. if($rs_i < count($horario_rs) && date('H:i', strtotime($horario_rs[$rs_i]["Hora_inicio"])) == date('H:i', strtotime($h.":".($f * (60/FRACCION_HORA)))) && $horario_rs[$rs_i]["Dia_id"] == $dia["Dia_id"]){
  262. $size = $horario_rs[$rs_i]["Duracion"]/(60/FRACCION_HORA);
  263. $spacerArr[$dia["Dia_id"]] = $size -1;
  264. $xtpl->assign("DURACION_SIZE", $size);
  265. $xtpl->assign("COLOR", $horario_rs[$rs_i]["TipoHorario_color"]);
  266. $xtpl->assign("TIPO", $horario_rs[$rs_i]["TipoHorario_desc"]);
  267. $total_tipo[$horario_rs[$rs_i]["TipoHorario_id"]]+= $horario_rs[$rs_i]["Duracion"]/60;
  268. $xtpl->assign("HORA_I", date('H:i', strtotime($horario_rs[$rs_i]["Hora_inicio"])));
  269. $xtpl->assign("HORA_F", date('H:i', strtotime($horario_rs[$rs_i]["Hora_final"])));
  270. $xtpl->parse("main.hora_row.td.td_mihorario.hora");
  271. $salon = "";
  272. do{
  273. if($horario_rs[$rs_i]["Salon_desc"] != "") $salon = "Salón: ".$horario_rs[$rs_i]["Salon_desc"];
  274. else if($horario_rs[$rs_i]["Materia_desc"] != "") $salon = "<em>Pendiente</em>";
  275. $xtpl->assign("MATERIA", $horario_rs[$rs_i]["Materia_desc"]);
  276. $xtpl->assign("GRUPO", $horario_rs[$rs_i]["Grupo_desc"]." ".$horario_rs[$rs_i]["Carrera_prefijo"]);
  277. if($horario_rs[$rs_i]["Materia_desc"] != "")
  278. $xtpl->parse("main.hora_row.td.td_mihorario.texto");
  279. $rs_i++;
  280. }while($rs_i < count($horario_rs) && date('H:i', strtotime($horario_rs[$rs_i-1]["Hora_inicio"])) == date('H:i', strtotime($horario_rs[$rs_i]["Hora_inicio"])) && $horario_rs[$rs_i]["Dia_id"] == $dia["Dia_id"]);
  281. $xtpl->assign("SALON", $salon);
  282. $xtpl->parse("main.hora_row.td.td_mihorario");
  283. }else{
  284. if(!isset($spacerArr[$dia["Dia_id"]]) || $spacerArr[$dia["Dia_id"]] == 0)
  285. $xtpl->parse("main.hora_row.td.td_vacio");
  286. else
  287. $spacerArr[$dia["Dia_id"]]--;
  288. }
  289. $xtpl->parse("main.hora_row.td");
  290. }
  291. $xtpl->parse("main.hora_row");
  292. }
  293. }
  294. $xtpl->parse("main.logo");
  295. $stmt = $pdo->prepare('Select * from fs_tipohorario(NULL, NULL)');
  296. $stmt->execute();
  297. $tipo_rs = $stmt->fetchAll();
  298. $stmt->closeCursor();
  299. foreach($tipo_rs as $tipo){
  300. $xtpl->assign("COLOR", $tipo["TipoHorario_color"]);
  301. $xtpl->assign("DESC", $tipo["TipoHorario_desc"]);
  302. $xtpl->assign("TOTAL", $total_tipo[$tipo["TipoHorario_id"]]);
  303. $xtpl->parse("main.tipo_row");
  304. }
  305. $xtpl->assign("COLOR", '#fff');
  306. $xtpl->assign("DESC", "Total de horas");
  307. $xtpl->assign("TOTAL", $total_tipo[1]+$total_tipo[2]+$total_tipo[3]);
  308. $xtpl->parse("main.tipo_row");
  309. $xtpl->parse("main");
  310. //$xtpl->out("main");
  311. $mpdf->WriteHTML($stylesheet);
  312. if(!isset($errorDesc))
  313. $mpdf->WriteHTML($xtpl->text("main"));
  314. else
  315. $mpdf->WriteHTML($errorDesc);
  316. $mpdf->Output("horariousuario.pdf", 'I');
  317. ?>