auditoría.js 13 KB

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