auditoría.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. const paramsUrl = new URLSearchParams(params).toString();
  212. try {
  213. const res = await fetch(`action/action_auditoria.php?${paramsUrl}`, {
  214. method: 'GET',
  215. });
  216. this.data = await res.json();
  217. }
  218. catch (error) {
  219. alert('Error al cargar los datos');
  220. }
  221. this.loading = false;
  222. store.current.page = 1;
  223. },
  224. invertir() {
  225. this.data = this.data.reverse();
  226. },
  227. mostrarComentario(registro_id) {
  228. const registro = this.data.find((registro) => registro.registro_id === registro_id);
  229. store.current.comentario = registro.comentario;
  230. $('#ver-comentario').modal('show');
  231. },
  232. get relevant() {
  233. /*
  234. facultad_id: null,
  235. fecha: null,
  236. fecha_inicio: null,
  237. fecha_fin: null,
  238. profesor: null,
  239. asistencia: null,
  240. estado_id: null,
  241. if one of the filters is null, then it is not relevant
  242. */
  243. const filters = Object.keys(store.filters).filter((filtro) => store.filters[filtro] !== null || store.filters[filtro]?.length > 0);
  244. return this.data.filter((registro) => {
  245. return filters.every((filtro) => {
  246. switch (filtro) {
  247. case 'fecha':
  248. return registro.registro_fecha_ideal === store.filters[filtro];
  249. case 'fecha_inicio':
  250. return registro.registro_fecha_ideal >= store.filters[filtro];
  251. case 'fecha_fin':
  252. return registro.registro_fecha_ideal <= store.filters[filtro];
  253. case 'profesor':
  254. const textoFiltro = store.filters[filtro].toLowerCase();
  255. if (/^\([^)]+\)\s[\s\S]+$/.test(textoFiltro)) {
  256. const clave = registro.profesor_clave.toLowerCase();
  257. const filtroClave = textoFiltro.match(/\((.*?)\)/)?.[1];
  258. // console.log(clave, filtroClave);
  259. return clave.includes(filtroClave);
  260. }
  261. else {
  262. const nombre = registro.profesor_nombre.toLowerCase();
  263. return nombre.includes(textoFiltro);
  264. }
  265. case 'facultad_id':
  266. return registro.facultad_id === store.filters[filtro];
  267. case 'estados':
  268. if (store.filters[filtro].length === 0)
  269. return true;
  270. else if (store.filters[filtro].includes(-1) && registro.estado_supervisor_id === null)
  271. return true;
  272. return store.filters[filtro].includes(registro.estado_supervisor_id);
  273. case 'bloque_horario':
  274. const bloque = store.bloques_horario.data.find((bloque) => bloque.id === store.filters[filtro]);
  275. return registro.horario_hora < bloque.hora_fin && registro.horario_fin > bloque.hora_inicio;
  276. default: return true;
  277. }
  278. });
  279. });
  280. },
  281. async descargar() {
  282. store.current.modal_state = 'Generando reporte en Excel...';
  283. $('div.modal#cargando').modal('show');
  284. this.loading = true;
  285. if (this.relevant.length === 0)
  286. return;
  287. try {
  288. const res = await fetch('export/supervisor_excel.php', {
  289. method: 'POST',
  290. headers: {
  291. 'Content-Type': 'application/json'
  292. },
  293. body: JSON.stringify(this.relevant)
  294. });
  295. const blob = await res.blob();
  296. window.saveAs(blob, `auditoria_${new Date().toISOString().slice(0, 10)}.xlsx`);
  297. }
  298. catch (error) {
  299. if (error.response && error.response.status === 413) {
  300. alert('Your request is too large! Please reduce the data size and try again.');
  301. }
  302. else {
  303. alert('An error occurred: ' + error.message);
  304. }
  305. }
  306. finally {
  307. $('#cargando').modal('hide');
  308. this.loading = false;
  309. }
  310. },
  311. loading: false,
  312. get pages() {
  313. return Math.ceil(this.relevant.length / store.current.perPage);
  314. }
  315. },
  316. });
  317. createApp({
  318. store,
  319. messages: [],
  320. get clase_vista() {
  321. return store.current.clase_vista;
  322. },
  323. set_justificar(horario_id, profesor_id, registro_fecha_ideal) {
  324. 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);
  325. store.current.clone_justificada = JSON.parse(JSON.stringify(store.current.justificada));
  326. store.current.observaciones = false;
  327. },
  328. cancelar_justificacion() {
  329. Object.assign(store.current.justificada, store.current.clone_justificada);
  330. delete store.current.clone_justificada;
  331. },
  332. profesores: [],
  333. async mounted() {
  334. $('div.modal#cargando').modal('show');
  335. try {
  336. // await store.registros.fetch()
  337. await store.facultades.fetch();
  338. await store.estados.fetch();
  339. await store.bloques_horario.fetch();
  340. await store.filters.switchFechas();
  341. store.periodo = await fetch('action/periodo_datos.php').then(res => res.json());
  342. this.profesores = await (await fetch('action/action_profesor.php')).json();
  343. this.messages.push({ title: 'Datos cargados', text: 'Los datos se han cargado correctamente', type: 'success', timestamp: new Date() });
  344. }
  345. catch (error) {
  346. this.messages.push({ title: 'Error al cargar datos', text: 'No se pudieron cargar los datos', type: 'danger', timestamp: new Date() });
  347. }
  348. finally {
  349. $('div.modal#cargando').modal('hide');
  350. }
  351. }
  352. }).mount('#app');