auditoría.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import { createApp, reactive } from 'https://unpkg.com/petite-vue?module'
  2. type Registro = {
  3. carrera: string;
  4. carrera_id: number;
  5. comentario: null;
  6. dia: string;
  7. duracion: string;
  8. duracion_id: number;
  9. estado_supervisor_id: number;
  10. facultad: string;
  11. facultad_id: number;
  12. horario_dia: number;
  13. horario_fin: string;
  14. horario_grupo: string;
  15. horario_hora: string;
  16. horario_id: number;
  17. materia: string;
  18. materia_id: number;
  19. nombre: string;
  20. periodo: string;
  21. periodo_id: number;
  22. profesor_clave: string;
  23. profesor_correo: string;
  24. profesor_grado: null;
  25. profesor_id: number;
  26. profesor_nombre: string;
  27. registro_fecha: null;
  28. registro_fecha_ideal: Date;
  29. registro_fecha_supervisor: Date;
  30. registro_id: number;
  31. registro_justificada: null;
  32. registro_retardo: null;
  33. salon: string;
  34. salon_id: number;
  35. supervisor_id: number;
  36. }
  37. type Estado = {
  38. color: string;
  39. icon: string;
  40. estado_supervisor_id: number;
  41. }
  42. type Facultad = {
  43. clave_dependencia: string;
  44. facultad_id: number;
  45. facultad_nombre: string;
  46. }
  47. type Bloque_Horario = {
  48. hora_fin: string;
  49. hora_inicio: string;
  50. id: number;
  51. selected: boolean;
  52. }
  53. type Periodo = {
  54. created_at: Date;
  55. estado_id: number;
  56. fecha_final: Date;
  57. id_periodo_sgu: number;
  58. nivel_id: number;
  59. periodo_clave: string;
  60. periodo_fecha_fin: Date;
  61. periodo_fecha_inicio: Date;
  62. periodo_id: number;
  63. periodo_nombre: string;
  64. }
  65. declare var $: any
  66. $('div.modal#cargando').modal({
  67. backdrop: 'static',
  68. keyboard: false,
  69. show: false,
  70. })
  71. const store = reactive({
  72. loading: false,
  73. <<<<<<< HEAD
  74. =======
  75. perido: null as Periodo | null,
  76. >>>>>>> 7688f1aac1824c234bc5f19b154e9ad1f4808d4f
  77. current: {
  78. comentario: '',
  79. clase_vista: null,
  80. empty: '',
  81. page: 1,
  82. maxPages: 10,
  83. perPage: 10,
  84. modal_state: "Cargando datos...",
  85. justificada: null as Registro | null,
  86. fechas_clicked: false,
  87. observaciones: false,
  88. },
  89. facultades: {
  90. data: [] as Facultad[],
  91. async fetch() {
  92. this.data = [] as Facultad[]
  93. const res = await fetch('action/action_facultad.php')
  94. this.data = await res.json()
  95. },
  96. },
  97. filters: {
  98. facultad_id: null,
  99. fecha: null,
  100. fecha_inicio: null,
  101. fecha_fin: null,
  102. profesor: null,
  103. periodo_id: null,
  104. bloque_horario: null,
  105. estados: [],
  106. switchFecha: false,
  107. async switchFechas() {
  108. const periodo = await fetch('action/periodo_datos.php');
  109. const periodo_data = await periodo.json() as Periodo;
  110. if (!store.filters.switchFecha) {
  111. $('div.modal#cargando').modal('show');
  112. await store.registros.fetch()
  113. $('div.modal#cargando').modal('hide');
  114. }
  115. $(function () {
  116. store.filters.fecha_inicio = store.filters.fecha_fin = store.filters.fecha = null
  117. $("#fecha, #fecha_inicio, #fecha_fin").datepicker({
  118. minDate: new Date(`${periodo_data.periodo_fecha_inicio}:00:00:00`),
  119. maxDate: new Date(`${periodo_data.fecha_final}:00:00:00`),
  120. dateFormat: "yy-mm-dd",
  121. showAnim: "slide",
  122. beforeShowDay: (date: Date) => [(date.getDay() != 0), ""]
  123. });
  124. const fecha = $("#fecha"), inicio = $("#fecha_inicio"), fin = $("#fecha_fin")
  125. fecha.datepicker("setDate", new Date(`${periodo_data.fecha_final}:00:00:00`));
  126. inicio.on("change", function () {
  127. store.current.fechas_clicked = false;
  128. store.filters.fecha_inicio = inicio.val()
  129. fin.datepicker("option", "minDate", inicio.val());
  130. });
  131. fin.on("change", function () {
  132. store.current.fechas_clicked = false;
  133. store.filters.fecha_fin = fin.val()
  134. inicio.datepicker("option", "maxDate", fin.val());
  135. });
  136. fecha.on("change", async function () {
  137. store.filters.fecha = fecha.val()
  138. $('div.modal#cargando').modal('show');
  139. await store.registros.fetch(store.filters.fecha)
  140. $('div.modal#cargando').modal('hide');
  141. });
  142. });
  143. },
  144. async fetchByDate() {
  145. store.current.fechas_clicked = true;
  146. $('div.modal#cargando').modal('show');
  147. await store.registros.fetch(undefined, store.filters.fecha_inicio, store.filters.fecha_fin);
  148. store.current.page = 1;
  149. $('div.modal#cargando').modal('hide');
  150. }
  151. },
  152. estados: {
  153. data: [] as Estado[],
  154. async fetch() {
  155. this.data = [] as Estado[]
  156. const res = await fetch('action/action_estado_supervisor.php')
  157. this.data = await res.json()
  158. },
  159. getEstado(id: number): Estado {
  160. return this.data.find((estado: Estado) => estado.estado_supervisor_id === id) ?? {
  161. estado_color: 'dark',
  162. estado_icon: 'ing-cancelar',
  163. nombre: 'Sin registro',
  164. estado_supervisor_id: -1,
  165. }
  166. },
  167. printEstados() {
  168. if (store.filters.estados.length > 0)
  169. document.querySelector('#estados')!.innerHTML = store.filters.estados.map((estado: number) =>
  170. `<span class="mx-2 badge badge-${store.estados.getEstado(estado).estado_color}">
  171. <i class="${store.estados.getEstado(estado).estado_icon}"></i> ${store.estados.getEstado(estado).nombre}
  172. </span>`
  173. ).join('')
  174. else
  175. document.querySelector('#estados')!.innerHTML = `Todos los registros`
  176. }
  177. },
  178. bloques_horario: {
  179. data: [] as Bloque_Horario[],
  180. async fetch() {
  181. this.data = [] as Bloque_Horario[]
  182. const res = await fetch('action/action_grupo_horario.php')
  183. this.data = await res.json()
  184. if (this.data.every((bloque: Bloque_Horario) => !bloque.selected))
  185. this.data[0].selected = true
  186. },
  187. },
  188. toggle(arr: any, element: any) {
  189. const newArray = arr.includes(element) ? arr.filter((item: any) => item !== element) : [...arr, element]
  190. // if all are selected, then unselect all
  191. if (newArray.length === (this.estados.data.length + 1)) {
  192. setTimeout(() => {
  193. document.querySelectorAll('#dlAsistencia>ul>li.selected')!.forEach(element => element.classList.remove('selected'));
  194. }, 100)
  195. return []
  196. }
  197. return newArray
  198. },
  199. async justificar() {
  200. if (!store.current.justificada) return;
  201. store.current.justificada.registro_justificada = true
  202. let data: {
  203. justificador_nombre: string;
  204. justificador_clave: string;
  205. justificador_facultad: string;
  206. justificador_rol: string;
  207. registro_fecha_justificacion: Date;
  208. };
  209. try {
  210. <<<<<<< HEAD
  211. const res = await fetch('action/action_justificar.php', {
  212. method: 'PUT',
  213. =======
  214. const res = await fetch('action/justificar.php', {
  215. method: 'POST',
  216. >>>>>>> 7688f1aac1824c234bc5f19b154e9ad1f4808d4f
  217. headers: {
  218. 'Content-Type': 'application/json'
  219. },
  220. body: JSON.stringify(store.current.justificada)
  221. })
  222. data = await res.json()
  223. } catch (error) {
  224. alert('Error al justificar')
  225. store.current.justificada = store.current.clone_justificada
  226. } finally {
  227. delete store.current.clone_justificada
  228. }
  229. store.current.justificada.justificador_nombre = data.justificador_nombre
  230. store.current.justificada.justificador_clave = data.justificador_clave
  231. store.current.justificada.justificador_facultad = data.justificador_facultad
  232. store.current.justificada.justificador_rol = data.justificador_rol
  233. store.current.justificada.registro_fecha_justificacion = data.registro_fecha_justificacion
  234. },
  235. <<<<<<< HEAD
  236. =======
  237. async justificarBloque(fecha: Date, bloques: Array<number>, justificacion: string) {
  238. if (bloques.length === 0) {
  239. alert('No se ha seleccionado ningún bloque');
  240. return;
  241. }
  242. if (!justificacion) {
  243. alert('No se ha ingresado ninguna observación');
  244. return;
  245. }
  246. try {
  247. const res = await fetch('action/action_justificar.php', {
  248. method: 'PUT',
  249. headers: {
  250. 'Content-Type': 'application/json'
  251. },
  252. body: JSON.stringify({
  253. fecha,
  254. bloques,
  255. justificacion,
  256. })
  257. })
  258. const resData = await res.json();
  259. if (resData.status === 'success') {
  260. alert('Se ha justificado el bloque');
  261. store.current.modal_state = 'Cargando datos...';
  262. $('div.modal#cargando').modal('show');
  263. await store.registros.fetch();
  264. $('div.modal#cargando').modal('hide');
  265. } else {
  266. alert('No se ha podido justificar el bloque');
  267. }
  268. } catch (error) {
  269. alert('Error al justificar');
  270. }
  271. },
  272. >>>>>>> 7688f1aac1824c234bc5f19b154e9ad1f4808d4f
  273. registros: {
  274. data: [] as Registro[],
  275. async fetch(fecha?: Date, fecha_inicio?: Date, fecha_fin?: Date) {
  276. // if (!store.filters.facultad_id || !store.filters.periodo_id) return
  277. this.loading = true
  278. this.data = [] as Registro[]
  279. const params = {
  280. facultad_id: 19,
  281. periodo_id: 2,
  282. }
  283. if (fecha) params['fecha'] = fecha
  284. if (fecha_inicio) params['fecha_inicio'] = fecha_inicio
  285. if (fecha_fin) params['fecha_fin'] = fecha_fin
  286. const paramsUrl = new URLSearchParams(params as any).toString()
  287. try {
  288. const res = await fetch(`action/action_auditoria.php?${paramsUrl}`, {
  289. method: 'GET',
  290. })
  291. this.data = await res.json()
  292. } catch (error) {
  293. alert('Error al cargar los datos')
  294. }
  295. this.loading = false
  296. store.current.page = 1
  297. },
  298. invertir() {
  299. this.data = this.data.reverse()
  300. },
  301. mostrarComentario(registro_id: number) {
  302. const registro = this.data.find((registro: Registro) => registro.registro_id === registro_id)
  303. store.current.comentario = registro.comentario
  304. $('#ver-comentario').modal('show')
  305. },
  306. get relevant() {
  307. /*
  308. facultad_id: null,
  309. fecha: null,
  310. fecha_inicio: null,
  311. fecha_fin: null,
  312. profesor: null,
  313. asistencia: null,
  314. estado_id: null,
  315. if one of the filters is null, then it is not relevant
  316. */
  317. <<<<<<< HEAD
  318. const filters = Object.keys(store.filters).filter((filtro) => store.filters[filtro] !== null || store.filters[filtro]?.length > 0 )
  319. =======
  320. const filters = Object.keys(store.filters).filter((filtro) => store.filters[filtro] !== null || store.filters[filtro]?.length > 0)
  321. >>>>>>> 7688f1aac1824c234bc5f19b154e9ad1f4808d4f
  322. return this.data.filter((registro: Registro) => {
  323. return filters.every((filtro) => {
  324. switch (filtro) {
  325. case 'fecha':
  326. return registro.registro_fecha_ideal === store.filters[filtro];
  327. case 'fecha_inicio':
  328. return registro.registro_fecha_ideal >= store.filters[filtro];
  329. case 'fecha_fin':
  330. return registro.registro_fecha_ideal <= store.filters[filtro];
  331. case 'profesor':
  332. const textoFiltro = store.filters[filtro].toLowerCase();
  333. if (/^\([^)]+\)\s[\s\S]+$/.test(textoFiltro)) {
  334. const clave = registro.profesor_clave.toLowerCase();
  335. const filtroClave = textoFiltro.match(/\((.*?)\)/)?.[1];
  336. // console.log(clave, filtroClave);
  337. return clave.includes(filtroClave);
  338. } else {
  339. const nombre = registro.profesor_nombre.toLowerCase();
  340. return nombre.includes(textoFiltro);
  341. }
  342. case 'facultad_id':
  343. return registro.facultad_id === store.filters[filtro];
  344. case 'estados':
  345. if (store.filters[filtro].length === 0) return true;
  346. else if (store.filters[filtro].includes(-1) && registro.estado_supervisor_id === null) return true;
  347. return store.filters[filtro].includes(registro.estado_supervisor_id);
  348. case 'bloque_horario':
  349. const bloque = store.bloques_horario.data.find((bloque: Bloque_Horario) => bloque.id === store.filters[filtro]) as Bloque_Horario;
  350. return registro.horario_hora < bloque.hora_fin && registro.horario_fin > bloque.hora_inicio;
  351. default: return true;
  352. }
  353. })
  354. })
  355. },
  356. async descargar() {
  357. store.current.modal_state = 'Generando reporte en Excel...'
  358. $('div.modal#cargando').modal('show');
  359. this.loading = true;
  360. if (this.relevant.length === 0) return;
  361. try {
  362. const res = await fetch('export/supervisor_excel.php', {
  363. method: 'POST',
  364. headers: {
  365. 'Content-Type': 'application/json'
  366. },
  367. body: JSON.stringify(this.relevant)
  368. });
  369. const blob = await res.blob();
  370. (window as any).saveAs(blob, `auditoria_${new Date().toISOString().slice(0, 10)}.xlsx`);
  371. } catch (error) {
  372. if (error.response && error.response.status === 413) {
  373. alert('Your request is too large! Please reduce the data size and try again.');
  374. } else {
  375. alert('An error occurred: ' + error.message);
  376. }
  377. }
  378. finally {
  379. $('#cargando').modal('hide');
  380. this.loading = false;
  381. }
  382. },
  383. loading: false,
  384. get pages() {
  385. return Math.ceil(this.relevant.length / store.current.perPage)
  386. }
  387. },
  388. })
  389. type Profesor = {
  390. profesor_id: number;
  391. profesor_nombre: string;
  392. profesor_correo: string;
  393. profesor_clave: string;
  394. profesor_grado: string;
  395. }
  396. createApp({
  397. store,
  398. <<<<<<< HEAD
  399. =======
  400. messages: [] as Array<{ title: string, text: string, type: string, timestamp: string }>,
  401. >>>>>>> 7688f1aac1824c234bc5f19b154e9ad1f4808d4f
  402. get clase_vista() {
  403. return store.current.clase_vista
  404. },
  405. set_justificar(horario_id: Number, profesor_id: Number, registro_fecha_ideal: Date) {
  406. store.current.justificada = store.registros.relevant.find((registro: Registro) => registro.horario_id === horario_id && registro.profesor_id === profesor_id && registro.registro_fecha_ideal === registro_fecha_ideal)
  407. store.current.clone_justificada = JSON.parse(JSON.stringify(store.current.justificada))
  408. store.current.observaciones = false
  409. },
  410. cancelar_justificacion() {
  411. Object.assign(store.current.justificada, store.current.clone_justificada)
  412. delete store.current.clone_justificada
  413. },
  414. profesores: [] as Profesor[],
  415. async mounted() {
  416. $('div.modal#cargando').modal('show');
  417. <<<<<<< HEAD
  418. // await store.registros.fetch()
  419. await store.facultades.fetch()
  420. await store.estados.fetch()
  421. await store.bloques_horario.fetch()
  422. await store.filters.switchFechas()
  423. this.profesores = await (await fetch('action/action_profesor.php')).json() as Profesor[];
  424. $('div.modal#cargando').modal('hide');
  425. =======
  426. try {
  427. // await store.registros.fetch()
  428. await store.facultades.fetch()
  429. await store.estados.fetch()
  430. await store.bloques_horario.fetch()
  431. await store.filters.switchFechas();
  432. store.periodo = await fetch('action/periodo_datos.php').then(res => res.json()) as Periodo;
  433. this.profesores = await (await fetch('action/action_profesor.php')).json() as Profesor[];
  434. this.messages.push({ title: 'Datos cargados', text: 'Los datos se han cargado correctamente', type: 'success', timestamp: new Date() })
  435. } catch (error) {
  436. this.messages.push({ title: 'Error al cargar datos', text: 'No se pudieron cargar los datos', type: 'danger', timestamp: new Date() })
  437. }
  438. finally {
  439. $('div.modal#cargando').modal('hide');
  440. }
  441. >>>>>>> 7688f1aac1824c234bc5f19b154e9ad1f4808d4f
  442. }
  443. }).mount('#app')