auditoría.js 14 KB

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