auditoría.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. current: {
  74. comentario: '',
  75. clase_vista: null,
  76. empty: '',
  77. page: 1,
  78. maxPages: 10,
  79. perPage: 10,
  80. modal_state: "Cargando datos...",
  81. justificada: null as Registro | null,
  82. fechas_clicked: false,
  83. observaciones: false,
  84. },
  85. facultades: {
  86. data: [] as Facultad[],
  87. async fetch() {
  88. this.data = [] as Facultad[]
  89. const res = await fetch('action/action_facultad.php')
  90. this.data = await res.json()
  91. },
  92. },
  93. filters: {
  94. facultad_id: null,
  95. fecha: null,
  96. fecha_inicio: null,
  97. fecha_fin: null,
  98. profesor: null,
  99. periodo_id: null,
  100. bloque_horario: null,
  101. estados: [],
  102. switchFecha: false,
  103. async switchFechas() {
  104. const periodo = await fetch('action/periodo_datos.php');
  105. const periodo_data = await periodo.json() as Periodo;
  106. if (!store.filters.switchFecha) {
  107. $('div.modal#cargando').modal('show');
  108. await store.registros.fetch()
  109. $('div.modal#cargando').modal('hide');
  110. }
  111. $(function () {
  112. store.filters.fecha_inicio = store.filters.fecha_fin = store.filters.fecha = null
  113. $("#fecha, #fecha_inicio, #fecha_fin").datepicker({
  114. minDate: new Date(`${periodo_data.periodo_fecha_inicio}:00:00:00`),
  115. maxDate: new Date(`${periodo_data.fecha_final}:00:00:00`),
  116. dateFormat: "yy-mm-dd",
  117. showAnim: "slide",
  118. beforeShowDay: (date: Date) => [(date.getDay() != 0), ""]
  119. });
  120. const fecha = $("#fecha"), inicio = $("#fecha_inicio"), fin = $("#fecha_fin")
  121. fecha.datepicker("setDate", new Date(`${periodo_data.fecha_final}:00:00:00`));
  122. inicio.on("change", function () {
  123. store.current.fechas_clicked = false;
  124. store.filters.fecha_inicio = inicio.val()
  125. fin.datepicker("option", "minDate", inicio.val());
  126. });
  127. fin.on("change", function () {
  128. store.current.fechas_clicked = false;
  129. store.filters.fecha_fin = fin.val()
  130. inicio.datepicker("option", "maxDate", fin.val());
  131. });
  132. fecha.on("change", async function () {
  133. store.filters.fecha = fecha.val()
  134. $('div.modal#cargando').modal('show');
  135. await store.registros.fetch(store.filters.fecha)
  136. $('div.modal#cargando').modal('hide');
  137. });
  138. });
  139. },
  140. async fetchByDate() {
  141. store.current.fechas_clicked = true;
  142. $('div.modal#cargando').modal('show');
  143. await store.registros.fetch(undefined, store.filters.fecha_inicio, store.filters.fecha_fin);
  144. store.current.page = 1;
  145. $('div.modal#cargando').modal('hide');
  146. }
  147. },
  148. estados: {
  149. data: [] as Estado[],
  150. async fetch() {
  151. this.data = [] as Estado[]
  152. const res = await fetch('action/action_estado_supervisor.php')
  153. this.data = await res.json()
  154. },
  155. getEstado(id: number): Estado {
  156. return this.data.find((estado: Estado) => estado.estado_supervisor_id === id) ?? {
  157. estado_color: 'dark',
  158. estado_icon: 'ing-cancelar',
  159. nombre: 'Sin registro',
  160. estado_supervisor_id: -1,
  161. }
  162. },
  163. printEstados() {
  164. if (store.filters.estados.length > 0)
  165. document.querySelector('#estados')!.innerHTML = store.filters.estados.map((estado: number) =>
  166. `<span class="mx-2 badge badge-${store.estados.getEstado(estado).estado_color}">
  167. <i class="${store.estados.getEstado(estado).estado_icon}"></i> ${store.estados.getEstado(estado).nombre}
  168. </span>`
  169. ).join('')
  170. else
  171. document.querySelector('#estados')!.innerHTML = `Todos los registros`
  172. }
  173. },
  174. bloques_horario: {
  175. data: [] as Bloque_Horario[],
  176. async fetch() {
  177. this.data = [] as Bloque_Horario[]
  178. const res = await fetch('action/action_grupo_horario.php')
  179. this.data = await res.json()
  180. if (this.data.every((bloque: Bloque_Horario) => !bloque.selected))
  181. this.data[0].selected = true
  182. },
  183. },
  184. toggle(arr: any, element: any) {
  185. const newArray = arr.includes(element) ? arr.filter((item: any) => item !== element) : [...arr, element]
  186. // if all are selected, then unselect all
  187. if (newArray.length === (this.estados.data.length + 1)) {
  188. setTimeout(() => {
  189. document.querySelectorAll('#dlAsistencia>ul>li.selected')!.forEach(element => element.classList.remove('selected'));
  190. }, 100)
  191. return []
  192. }
  193. return newArray
  194. },
  195. async justificar() {
  196. if (!store.current.justificada) return;
  197. store.current.justificada.registro_justificada = true
  198. let data: {
  199. justificador_nombre: string;
  200. justificador_clave: string;
  201. justificador_facultad: string;
  202. justificador_rol: string;
  203. registro_fecha_justificacion: Date;
  204. };
  205. try {
  206. const res = await fetch('action/action_justificar.php', {
  207. method: 'PUT',
  208. headers: {
  209. 'Content-Type': 'application/json'
  210. },
  211. body: JSON.stringify(store.current.justificada)
  212. })
  213. data = await res.json()
  214. } catch (error) {
  215. alert('Error al justificar')
  216. store.current.justificada = store.current.clone_justificada
  217. } finally {
  218. delete store.current.clone_justificada
  219. }
  220. store.current.justificada.justificador_nombre = data.justificador_nombre
  221. store.current.justificada.justificador_clave = data.justificador_clave
  222. store.current.justificada.justificador_facultad = data.justificador_facultad
  223. store.current.justificada.justificador_rol = data.justificador_rol
  224. store.current.justificada.registro_fecha_justificacion = data.registro_fecha_justificacion
  225. },
  226. registros: {
  227. data: [] as Registro[],
  228. async fetch(fecha?: Date, fecha_inicio?: Date, fecha_fin?: Date) {
  229. // if (!store.filters.facultad_id || !store.filters.periodo_id) return
  230. this.loading = true
  231. this.data = [] as Registro[]
  232. const params = {
  233. facultad_id: 19,
  234. periodo_id: 2,
  235. }
  236. if (fecha) params['fecha'] = fecha
  237. if (fecha_inicio) params['fecha_inicio'] = fecha_inicio
  238. if (fecha_fin) params['fecha_fin'] = fecha_fin
  239. const paramsUrl = new URLSearchParams(params as any).toString()
  240. const res = await fetch(`action/action_auditoria.php?${paramsUrl}`, {
  241. method: 'GET',
  242. })
  243. this.data = await res.json()
  244. this.loading = false
  245. store.current.page = 1
  246. },
  247. invertir() {
  248. this.data = this.data.reverse()
  249. },
  250. mostrarComentario(registro_id: number) {
  251. const registro = this.data.find((registro: Registro) => registro.registro_id === registro_id)
  252. store.current.comentario = registro.comentario
  253. $('#ver-comentario').modal('show')
  254. },
  255. get relevant() {
  256. /*
  257. facultad_id: null,
  258. fecha: null,
  259. fecha_inicio: null,
  260. fecha_fin: null,
  261. profesor: null,
  262. asistencia: null,
  263. estado_id: null,
  264. if one of the filters is null, then it is not relevant
  265. */
  266. const filters = Object.keys(store.filters).filter((filtro) => store.filters[filtro] || store.filters[filtro]?.length > 0)
  267. return this.data.filter((registro: Registro) => {
  268. return filters.every((filtro) => {
  269. switch (filtro) {
  270. case 'fecha':
  271. return registro.registro_fecha_ideal === store.filters[filtro];
  272. case 'fecha_inicio':
  273. return registro.registro_fecha_ideal >= store.filters[filtro];
  274. case 'fecha_fin':
  275. return registro.registro_fecha_ideal <= store.filters[filtro];
  276. case 'profesor':
  277. const textoFiltro = store.filters[filtro].toLowerCase();
  278. if (/^\([^)]+\)\s[\s\S]+$/.test(textoFiltro)) {
  279. const clave = registro.profesor_clave.toLowerCase();
  280. const filtroClave = textoFiltro.match(/\((.*?)\)/)?.[1];
  281. // console.log(clave, filtroClave);
  282. return clave.includes(filtroClave);
  283. } else {
  284. const nombre = registro.profesor_nombre.toLowerCase();
  285. return nombre.includes(textoFiltro);
  286. }
  287. case 'facultad_id':
  288. return registro.facultad_id === store.filters[filtro];
  289. case 'estados':
  290. if (store.filters[filtro].length === 0) return true;
  291. else if (store.filters[filtro].includes(-1) && registro.estado_supervisor_id === null) return true;
  292. return store.filters[filtro].includes(registro.estado_supervisor_id);
  293. case 'bloque_horario':
  294. const bloque = store.bloques_horario.data.find((bloque: Bloque_Horario) => bloque.id === store.filters[filtro]) as Bloque_Horario;
  295. return registro.horario_hora < bloque.hora_fin && registro.horario_fin > bloque.hora_inicio;
  296. default: return true;
  297. }
  298. })
  299. })
  300. },
  301. async descargar() {
  302. store.current.modal_state = 'Generando reporte en Excel...'
  303. $('div.modal#cargando').modal('show');
  304. this.loading = true;
  305. if (this.relevant.length === 0) return;
  306. try {
  307. const res = await fetch('export/supervisor_excel.php', {
  308. method: 'POST',
  309. headers: {
  310. 'Content-Type': 'application/json'
  311. },
  312. body: JSON.stringify(this.relevant)
  313. });
  314. const blob = await res.blob();
  315. (window as any).saveAs(blob, `auditoria_${new Date().toISOString().slice(0, 10)}.xlsx`);
  316. } catch (error) {
  317. if (error.response && error.response.status === 413) {
  318. alert('Your request is too large! Please reduce the data size and try again.');
  319. } else {
  320. alert('An error occurred: ' + error.message);
  321. }
  322. }
  323. finally {
  324. $('#cargando').modal('hide');
  325. this.loading = false;
  326. }
  327. },
  328. loading: false,
  329. get pages() {
  330. return Math.ceil(this.relevant.length / store.current.perPage)
  331. }
  332. },
  333. })
  334. type Profesor = {
  335. profesor_id: number;
  336. profesor_nombre: string;
  337. profesor_correo: string;
  338. profesor_clave: string;
  339. profesor_grado: string;
  340. }
  341. createApp({
  342. store,
  343. get clase_vista() {
  344. return store.current.clase_vista
  345. },
  346. set_justificar(horario_id: Number, profesor_id: Number, registro_fecha_ideal: Date) {
  347. 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)
  348. store.current.clone_justificada = JSON.parse(JSON.stringify(store.current.justificada))
  349. store.current.observaciones = false
  350. },
  351. cancelar_justificacion() {
  352. Object.assign(store.current.justificada, store.current.clone_justificada)
  353. delete store.current.clone_justificada
  354. },
  355. get profesores() {
  356. return store.registros.data
  357. .map((registro: Registro) => ({
  358. profesor_id: registro.profesor_id,
  359. profesor_nombre: registro.profesor_nombre,
  360. profesor_correo: registro.profesor_correo,
  361. profesor_clave: registro.profesor_clave,
  362. profesor_grado: registro.profesor_grado,
  363. }))
  364. .reduce((acc: Profesor[], current: Profesor) => {
  365. if (!acc.some(item => item.profesor_id === current.profesor_id)) {
  366. acc.push(current);
  367. }
  368. return acc;
  369. }, [])
  370. .sort((a: Profesor, b: Profesor) =>
  371. a.profesor_nombre.localeCompare(b.profesor_nombre)
  372. );
  373. },
  374. async mounted() {
  375. $('div.modal#cargando').modal('show');
  376. // await store.registros.fetch()
  377. await store.facultades.fetch()
  378. await store.estados.fetch()
  379. await store.bloques_horario.fetch()
  380. await store.filters.switchFechas()
  381. $('div.modal#cargando').modal('hide');
  382. }
  383. }).mount('#app')