auditoría.ts 12 KB

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