auditoría.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import { Alert } from 'bootstrap';
  2. import { createApp, reactive } from 'https://unpkg.com/petite-vue?module'
  3. type Registro = {
  4. carrera: string;
  5. carrera_id: number;
  6. comentario: null;
  7. dia: string;
  8. duracion: string;
  9. duracion_id: number;
  10. estado_supervisor_id: number;
  11. facultad: string;
  12. facultad_id: number;
  13. horario_dia: number;
  14. horario_fin: string;
  15. horario_grupo: string;
  16. horario_hora: string;
  17. horario_id: number;
  18. materia: string;
  19. materia_id: number;
  20. nombre: string;
  21. periodo: string;
  22. periodo_id: number;
  23. profesor_clave: string;
  24. profesor_correo: string;
  25. profesor_grado: null;
  26. profesor_id: number;
  27. profesor_nombre: string;
  28. registro_fecha: null;
  29. registro_fecha_ideal: Date;
  30. registro_fecha_supervisor: Date;
  31. registro_id: number;
  32. registro_justificada: null;
  33. registro_retardo: null;
  34. salon: string;
  35. salon_id: number;
  36. supervisor_id: number;
  37. }
  38. type Estado = {
  39. color: string;
  40. icon: string;
  41. estado_supervisor_id: number;
  42. }
  43. type Facultad = {
  44. clave_dependencia: string;
  45. facultad_id: number;
  46. facultad_nombre: string;
  47. }
  48. type Bloque_Horario = {
  49. hora_fin: string;
  50. hora_inicio: string;
  51. id: number;
  52. selected: boolean;
  53. }
  54. type Periodo = {
  55. created_at: Date;
  56. estado_id: number;
  57. fecha_final: Date;
  58. id_periodo_sgu: number;
  59. nivel_id: number;
  60. periodo_clave: string;
  61. periodo_fecha_fin: Date;
  62. periodo_fecha_inicio: Date;
  63. periodo_id: number;
  64. periodo_nombre: string;
  65. }
  66. declare var $: any
  67. $('div.modal#cargando').modal({
  68. backdrop: 'static',
  69. keyboard: false,
  70. show: false,
  71. })
  72. const store = reactive({
  73. loading: false,
  74. current: {
  75. comentario: '',
  76. clase_vista: null,
  77. empty: '',
  78. page: 1,
  79. maxPages: 10,
  80. perPage: 10,
  81. modal_state: "Cargando datos...",
  82. justificada: null,
  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. sin_registro: false,
  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. });
  119. const fecha = $("#fecha"), inicio = $("#fecha_inicio"), fin = $("#fecha_fin")
  120. fecha.datepicker("setDate", new Date(`${periodo_data.fecha_final}:00:00:00`));
  121. inicio.on("change", function () {
  122. store.filters.fecha_inicio = inicio.val()
  123. fin.datepicker("option", "minDate", inicio.val());
  124. });
  125. fin.on("change", function () {
  126. store.filters.fecha_fin = fin.val()
  127. inicio.datepicker("option", "maxDate", fin.val());
  128. });
  129. fecha.on("change", async function () {
  130. store.filters.fecha = fecha.val()
  131. $('div.modal#cargando').modal('show');
  132. await store.registros.fetch(store.filters.fecha)
  133. $('div.modal#cargando').modal('hide');
  134. });
  135. });
  136. },
  137. async fetchByDate() {
  138. $('div.modal#cargando').modal('show');
  139. await store.registros.fetch(undefined, store.filters.fecha_inicio, store.filters.fecha_fin);
  140. store.current.page = 1;
  141. $('div.modal#cargando').modal('hide');
  142. }
  143. },
  144. estados: {
  145. data: [] as Estado[],
  146. async fetch() {
  147. this.data = [] as Estado[]
  148. const res = await fetch('action/action_estado_supervisor.php')
  149. this.data = await res.json()
  150. },
  151. getEstado(id: number): Estado {
  152. return this.data.find((estado: Estado) => estado.estado_supervisor_id === id)
  153. },
  154. printEstados() {
  155. if (store.filters.estados.length > 0)
  156. document.querySelector('#estados')!.innerHTML = store.filters.estados.map((estado: number) =>
  157. `<span class="mx-2 badge badge-${store.estados.getEstado(estado).estado_color}">
  158. <i class="${store.estados.getEstado(estado).estado_icon}"></i> ${store.estados.getEstado(estado).nombre}
  159. </span>`
  160. ).join('')
  161. else
  162. document.querySelector('#estados')!.innerHTML = `Todos los registros`
  163. }
  164. },
  165. bloques_horario: {
  166. data: [] as Bloque_Horario[],
  167. async fetch() {
  168. this.data = [] as Bloque_Horario[]
  169. const res = await fetch('action/action_grupo_horario.php')
  170. this.data = await res.json()
  171. if (this.data.every((bloque: Bloque_Horario) => !bloque.selected))
  172. this.data[0].selected = true
  173. },
  174. },
  175. toggle(arr: any, element: any) {
  176. const newArray = arr.includes(element) ? arr.filter((item: any) => item !== element) : [...arr, element]
  177. // if all are selected, then unselect all
  178. if (newArray.length === this.estados.data.length) {
  179. setTimeout(() => {
  180. document.querySelectorAll('#dlAsistencia>ul>li.selected')!.forEach(element => element.classList.remove('selected'));
  181. }, 100)
  182. return []
  183. }
  184. return newArray
  185. },
  186. async justificar() {
  187. if (!store.current.justificada) return;
  188. let data;
  189. try {
  190. const res = await fetch('action/action_justificar.php', {
  191. method: 'PUT',
  192. headers: {
  193. 'Content-Type': 'application/json'
  194. },
  195. body: JSON.stringify(store.current.justificada)
  196. })
  197. data = await res.json()
  198. } catch (error) {
  199. alert('Error al justificar')
  200. store.current.justificada = store.current.clone_justificada
  201. } finally {
  202. delete store.current.clone_justificada
  203. }
  204. store.current.justificada.justificador_nombre = data.justificador_nombre
  205. store.current.justificada.justificador_clave = data.justificador_clave
  206. store.current.justificada.justificador_facultad = data.justificador_facultad
  207. store.current.justificada.justificador_rol = data.justificador_rol
  208. store.current.justificada.registro_fecha_justificacion = data.registro_fecha_justificacion
  209. },
  210. registros: {
  211. data: [] as Registro[],
  212. async fetch(fecha?: Date, fecha_inicio?: Date, fecha_fin?: Date) {
  213. // if (!store.filters.facultad_id || !store.filters.periodo_id) return
  214. this.loading = true
  215. this.data = [] as Registro[]
  216. const params = {
  217. facultad_id: 19,
  218. periodo_id: 2,
  219. }
  220. if (fecha) params['fecha'] = fecha
  221. if (fecha_inicio) params['fecha_inicio'] = fecha_inicio
  222. if (fecha_fin) params['fecha_fin'] = fecha_fin
  223. const paramsUrl = new URLSearchParams(params as any).toString()
  224. const res = await fetch(`action/action_auditoria.php?${paramsUrl}`, {
  225. method: 'GET',
  226. })
  227. this.data = await res.json()
  228. this.loading = false
  229. },
  230. invertir() {
  231. this.data = this.data.reverse()
  232. },
  233. mostrarComentario(registro_id: number) {
  234. const registro = this.data.find((registro: Registro) => registro.registro_id === registro_id)
  235. store.current.comentario = registro.comentario
  236. $('#ver-comentario').modal('show')
  237. },
  238. get relevant() {
  239. /*
  240. facultad_id: null,
  241. fecha: null,
  242. fecha_inicio: null,
  243. fecha_fin: null,
  244. profesor: null,
  245. asistencia: null,
  246. estado_id: null,
  247. if one of the filters is null, then it is not relevant
  248. */
  249. const filters = Object.keys(store.filters).filter((filtro) => store.filters[filtro] || store.filters[filtro]?.length > 0)
  250. return this.data.filter((registro: Registro) => {
  251. return filters.every((filtro) => {
  252. switch (filtro) {
  253. case 'fecha':
  254. return registro.registro_fecha_ideal === store.filters[filtro];
  255. case 'fecha_inicio':
  256. return registro.registro_fecha_ideal >= store.filters[filtro];
  257. case 'fecha_fin':
  258. return registro.registro_fecha_ideal <= store.filters[filtro];
  259. case 'profesor':
  260. const textoFiltro = store.filters[filtro].toLowerCase();
  261. if (/^\([^)]+\)\s[\s\S]+$/.test(textoFiltro)) {
  262. const clave = registro.profesor_clave.toLowerCase();
  263. const filtroClave = textoFiltro.match(/\((.*?)\)/)?.[1];
  264. // console.log(clave, filtroClave);
  265. return clave.includes(filtroClave);
  266. } else {
  267. const nombre = registro.profesor_nombre.toLowerCase();
  268. return nombre.includes(textoFiltro);
  269. }
  270. case 'facultad_id':
  271. return registro.facultad_id === store.filters[filtro];
  272. case 'estados':
  273. if (store.filters[filtro].length === 0) return true;
  274. return store.filters[filtro].includes(registro.estado_supervisor_id);
  275. case 'bloque_horario':
  276. const bloque = store.bloques_horario.data.find((bloque: Bloque_Horario) => bloque.id === store.filters[filtro]) as Bloque_Horario;
  277. return registro.horario_hora < bloque.hora_fin && registro.horario_fin > bloque.hora_inicio;
  278. default: {
  279. if (store.filters.sin_registro) return !registro.registro_fecha_supervisor
  280. else return registro.registro_fecha_supervisor
  281. }
  282. }
  283. })
  284. })
  285. },
  286. async descargar() {
  287. store.current.modal_state = 'Generando reporte en Excel...'
  288. $('div.modal#cargando').modal('show');
  289. this.loading = true;
  290. if (this.relevant.length === 0) return;
  291. try {
  292. const res = await fetch('export/supervisor_excel.php', {
  293. method: 'POST',
  294. headers: {
  295. 'Content-Type': 'application/json'
  296. },
  297. body: JSON.stringify(this.relevant)
  298. });
  299. const blob = await res.blob();
  300. (window as any).saveAs(blob, `auditoria_${new Date().toISOString().slice(0, 10)}.xlsx`);
  301. } catch (error) {
  302. if (error.response && error.response.status === 413) {
  303. alert('Your request is too large! Please reduce the data size and try again.');
  304. } else {
  305. alert('An error occurred: ' + error.message);
  306. }
  307. }
  308. finally {
  309. $('#cargando').modal('hide');
  310. this.loading = false;
  311. }
  312. },
  313. loading: false,
  314. get pages() {
  315. return Math.ceil(this.relevant.length / store.current.perPage)
  316. }
  317. },
  318. })
  319. type Profesor = {
  320. profesor_id: number;
  321. profesor_nombre: string;
  322. profesor_correo: string;
  323. profesor_clave: string;
  324. profesor_grado: string;
  325. }
  326. createApp({
  327. store,
  328. get clase_vista() {
  329. return store.current.clase_vista
  330. },
  331. set_justificar(horario_id: Number, profesor_id: Number, registro_fecha_ideal: Date) {
  332. 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)
  333. store.current.clone_justificada = JSON.parse(JSON.stringify(store.current.justificada))
  334. },
  335. cancelar_justificacion() {
  336. Object.assign(store.current.justificada, store.current.clone_justificada)
  337. delete store.current.clone_justificada
  338. },
  339. get profesores() {
  340. return store.registros.data
  341. .map((registro: Registro) => ({
  342. profesor_id: registro.profesor_id,
  343. profesor_nombre: registro.profesor_nombre,
  344. profesor_correo: registro.profesor_correo,
  345. profesor_clave: registro.profesor_clave,
  346. profesor_grado: registro.profesor_grado,
  347. }))
  348. .reduce((acc: Profesor[], current: Profesor) => {
  349. if (!acc.some(item => item.profesor_id === current.profesor_id)) {
  350. acc.push(current);
  351. }
  352. return acc;
  353. }, [])
  354. .sort((a: Profesor, b: Profesor) =>
  355. a.profesor_nombre.localeCompare(b.profesor_nombre)
  356. );
  357. },
  358. async mounted() {
  359. $('div.modal#cargando').modal('show');
  360. await store.registros.fetch()
  361. await store.facultades.fetch()
  362. await store.estados.fetch()
  363. await store.bloques_horario.fetch()
  364. await store.filters.switchFechas()
  365. $('div.modal#cargando').modal('hide');
  366. }
  367. }).mount('#app')