auditoría.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. try {
  175. const res = await fetch(`action/action_auditoria.php?${paramsUrl}`, {
  176. method: 'GET',
  177. });
  178. this.data = await res.json();
  179. }
  180. catch (error) {
  181. alert('Error al cargar los datos');
  182. }
  183. this.loading = false;
  184. store.current.page = 1;
  185. },
  186. invertir() {
  187. this.data = this.data.reverse();
  188. },
  189. mostrarComentario(registro_id) {
  190. const registro = this.data.find((registro) => registro.registro_id === registro_id);
  191. store.current.comentario = registro.comentario;
  192. $('#ver-comentario').modal('show');
  193. },
  194. get relevant() {
  195. /*
  196. facultad_id: null,
  197. fecha: null,
  198. fecha_inicio: null,
  199. fecha_fin: null,
  200. profesor: null,
  201. asistencia: null,
  202. estado_id: null,
  203. if one of the filters is null, then it is not relevant
  204. */
  205. const filters = Object.keys(store.filters).filter((filtro) => store.filters[filtro] || store.filters[filtro]?.length > 0);
  206. return this.data.filter((registro) => {
  207. return filters.every((filtro) => {
  208. switch (filtro) {
  209. case 'fecha':
  210. return registro.registro_fecha_ideal === store.filters[filtro];
  211. case 'fecha_inicio':
  212. return registro.registro_fecha_ideal >= store.filters[filtro];
  213. case 'fecha_fin':
  214. return registro.registro_fecha_ideal <= store.filters[filtro];
  215. case 'profesor':
  216. const textoFiltro = store.filters[filtro].toLowerCase();
  217. if (/^\([^)]+\)\s[\s\S]+$/.test(textoFiltro)) {
  218. const clave = registro.profesor_clave.toLowerCase();
  219. const filtroClave = textoFiltro.match(/\((.*?)\)/)?.[1];
  220. // console.log(clave, filtroClave);
  221. return clave.includes(filtroClave);
  222. }
  223. else {
  224. const nombre = registro.profesor_nombre.toLowerCase();
  225. return nombre.includes(textoFiltro);
  226. }
  227. case 'facultad_id':
  228. return registro.facultad_id === store.filters[filtro];
  229. case 'estados':
  230. if (store.filters[filtro].length === 0)
  231. return true;
  232. else if (store.filters[filtro].includes(-1) && registro.estado_supervisor_id === null)
  233. return true;
  234. return store.filters[filtro].includes(registro.estado_supervisor_id);
  235. case 'bloque_horario':
  236. const bloque = store.bloques_horario.data.find((bloque) => bloque.id === store.filters[filtro]);
  237. return registro.horario_hora < bloque.hora_fin && registro.horario_fin > bloque.hora_inicio;
  238. default: return true;
  239. }
  240. });
  241. });
  242. },
  243. async descargar() {
  244. store.current.modal_state = 'Generando reporte en Excel...';
  245. $('div.modal#cargando').modal('show');
  246. this.loading = true;
  247. if (this.relevant.length === 0)
  248. return;
  249. try {
  250. const res = await fetch('export/supervisor_excel.php', {
  251. method: 'POST',
  252. headers: {
  253. 'Content-Type': 'application/json'
  254. },
  255. body: JSON.stringify(this.relevant)
  256. });
  257. const blob = await res.blob();
  258. window.saveAs(blob, `auditoria_${new Date().toISOString().slice(0, 10)}.xlsx`);
  259. }
  260. catch (error) {
  261. if (error.response && error.response.status === 413) {
  262. alert('Your request is too large! Please reduce the data size and try again.');
  263. }
  264. else {
  265. alert('An error occurred: ' + error.message);
  266. }
  267. }
  268. finally {
  269. $('#cargando').modal('hide');
  270. this.loading = false;
  271. }
  272. },
  273. loading: false,
  274. get pages() {
  275. return Math.ceil(this.relevant.length / store.current.perPage);
  276. }
  277. },
  278. });
  279. createApp({
  280. store,
  281. get clase_vista() {
  282. return store.current.clase_vista;
  283. },
  284. set_justificar(horario_id, profesor_id, registro_fecha_ideal) {
  285. 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);
  286. store.current.clone_justificada = JSON.parse(JSON.stringify(store.current.justificada));
  287. store.current.observaciones = false;
  288. },
  289. cancelar_justificacion() {
  290. Object.assign(store.current.justificada, store.current.clone_justificada);
  291. delete store.current.clone_justificada;
  292. },
  293. get profesores() {
  294. return store.registros.data
  295. .map((registro) => ({
  296. profesor_id: registro.profesor_id,
  297. profesor_nombre: registro.profesor_nombre,
  298. profesor_correo: registro.profesor_correo,
  299. profesor_clave: registro.profesor_clave,
  300. profesor_grado: registro.profesor_grado,
  301. }))
  302. .reduce((acc, current) => {
  303. if (!acc.some(item => item.profesor_id === current.profesor_id)) {
  304. acc.push(current);
  305. }
  306. return acc;
  307. }, [])
  308. .sort((a, b) => a.profesor_nombre.localeCompare(b.profesor_nombre));
  309. },
  310. async mounted() {
  311. $('div.modal#cargando').modal('show');
  312. // await store.registros.fetch()
  313. await store.facultades.fetch();
  314. await store.estados.fetch();
  315. await store.bloques_horario.fetch();
  316. await store.filters.switchFechas();
  317. $('div.modal#cargando').modal('hide');
  318. }
  319. }).mount('#app');