auditoría.ts 12 KB

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