auditoría.ts 15 KB

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