auditoría.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. /*
  251. store.current
  252. page: 1,
  253. maxPages: 10,
  254. perPage: 10,
  255. */
  256. return this.data.filter((registro: Registro) => {
  257. return filters.every((filtro) => {
  258. switch (filtro) {
  259. case 'fecha':
  260. return registro.registro_fecha_ideal === store.filters[filtro];
  261. case 'fecha_inicio':
  262. return registro.registro_fecha_ideal >= store.filters[filtro];
  263. case 'fecha_fin':
  264. return registro.registro_fecha_ideal <= store.filters[filtro];
  265. case 'profesor':
  266. const textoFiltro = store.filters[filtro].toLowerCase();
  267. if (/^\([^)]+\)\s[\s\S]+$/.test(textoFiltro)) {
  268. const clave = registro.profesor_clave.toLowerCase();
  269. const filtroClave = textoFiltro.match(/\((.*?)\)/)?.[1];
  270. // console.log(clave, filtroClave);
  271. return clave.includes(filtroClave);
  272. } else {
  273. const nombre = registro.profesor_nombre.toLowerCase();
  274. return nombre.includes(textoFiltro);
  275. }
  276. case 'facultad_id':
  277. return registro.facultad_id === store.filters[filtro];
  278. case 'estados':
  279. if (store.filters[filtro].length === 0) return true;
  280. return store.filters[filtro].includes(registro.estado_supervisor_id);
  281. case 'bloque_horario':
  282. const bloque = store.bloques_horario.data.find((bloque: Bloque_Horario) => bloque.id === store.filters[filtro]) as Bloque_Horario;
  283. return registro.horario_hora < bloque.hora_fin && registro.horario_fin > bloque.hora_inicio;
  284. default: {
  285. if (store.filters.sin_registro && !registro.registro_fecha_supervisor) return true
  286. else if (store.filters.sin_registro) return false
  287. return true
  288. }
  289. }
  290. })
  291. })
  292. },
  293. async descargar() {
  294. store.current.modal_state = 'Generando reporte en Excel...'
  295. $('div.modal#cargando').modal('show');
  296. this.loading = true;
  297. if (this.relevant.length === 0) return;
  298. try {
  299. const res = await fetch('export/supervisor_excel.php', {
  300. method: 'POST',
  301. headers: {
  302. 'Content-Type': 'application/json'
  303. },
  304. body: JSON.stringify(this.relevant)
  305. });
  306. const blob = await res.blob();
  307. (window as any).saveAs(blob, `auditoria_${new Date().toISOString().slice(0, 10)}.xlsx`);
  308. } catch (error) {
  309. if (error.response && error.response.status === 413) {
  310. alert('Your request is too large! Please reduce the data size and try again.');
  311. } else {
  312. alert('An error occurred: ' + error.message);
  313. }
  314. }
  315. finally {
  316. $('#cargando').modal('hide');
  317. this.loading = false;
  318. }
  319. },
  320. loading: false,
  321. get pages() {
  322. return Math.ceil(this.relevant.length / store.current.perPage)
  323. }
  324. },
  325. })
  326. type Profesor = {
  327. profesor_id: number;
  328. profesor_nombre: string;
  329. profesor_correo: string;
  330. profesor_clave: string;
  331. profesor_grado: string;
  332. }
  333. createApp({
  334. store,
  335. get clase_vista() {
  336. return store.current.clase_vista
  337. },
  338. set_justificar(horario_id: Number, profesor_id: Number, registro_fecha_ideal: Date) {
  339. 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)
  340. store.current.clone_justificada = JSON.parse(JSON.stringify(store.current.justificada))
  341. },
  342. cancelar_justificacion() {
  343. Object.assign(store.current.justificada, store.current.clone_justificada)
  344. delete store.current.clone_justificada
  345. },
  346. get profesores() {
  347. return store.registros.data
  348. .map((registro: Registro) => ({
  349. profesor_id: registro.profesor_id,
  350. profesor_nombre: registro.profesor_nombre,
  351. profesor_correo: registro.profesor_correo,
  352. profesor_clave: registro.profesor_clave,
  353. profesor_grado: registro.profesor_grado,
  354. }))
  355. .reduce((acc: Profesor[], current: Profesor) => {
  356. if (!acc.some(item => item.profesor_id === current.profesor_id)) {
  357. acc.push(current);
  358. }
  359. return acc;
  360. }, [])
  361. .sort((a: Profesor, b: Profesor) =>
  362. a.profesor_nombre.localeCompare(b.profesor_nombre)
  363. );
  364. },
  365. async mounted() {
  366. $('div.modal#cargando').modal('show');
  367. await store.registros.fetch()
  368. await store.facultades.fetch()
  369. await store.estados.fetch()
  370. await store.bloques_horario.fetch()
  371. await store.filters.switchFechas()
  372. $('div.modal#cargando').modal('hide');
  373. }
  374. }).mount('#app')