auditoría.js 13 KB

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