auditoría.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. <<<<<<< HEAD
  103. document.querySelector('#estados').innerHTML = store.filters.estados.map((estado) => `<span class="mx-2 badge badge-${store.estados.getEstado(estado).estado_color}">
  104. <i class="${store.estados.getEstado(estado).estado_icon}"></i> ${store.estados.getEstado(estado).nombre}
  105. =======
  106. document.querySelector('#estados').innerHTML = store.filters.estados.map((estado) => `<span class="mx-2 badge badge-${store.estados.getEstado(estado).estado_color}">
  107. <i class="${store.estados.getEstado(estado).estado_icon}"></i> ${store.estados.getEstado(estado).nombre}
  108. >>>>>>> 7688f1aac1824c234bc5f19b154e9ad1f4808d4f
  109. </span>`).join('');
  110. else
  111. document.querySelector('#estados').innerHTML = `Todos los registros`;
  112. }
  113. },
  114. bloques_horario: {
  115. data: [],
  116. async fetch() {
  117. this.data = [];
  118. const res = await fetch('action/action_grupo_horario.php');
  119. this.data = await res.json();
  120. if (this.data.every((bloque) => !bloque.selected))
  121. this.data[0].selected = true;
  122. },
  123. },
  124. toggle(arr, element) {
  125. const newArray = arr.includes(element) ? arr.filter((item) => item !== element) : [...arr, element];
  126. // if all are selected, then unselect all
  127. if (newArray.length === (this.estados.data.length + 1)) {
  128. setTimeout(() => {
  129. document.querySelectorAll('#dlAsistencia>ul>li.selected').forEach(element => element.classList.remove('selected'));
  130. }, 100);
  131. return [];
  132. }
  133. return newArray;
  134. },
  135. async justificar() {
  136. if (!store.current.justificada)
  137. return;
  138. store.current.justificada.registro_justificada = true;
  139. let data;
  140. try {
  141. const res = await fetch('action/justificar.php', {
  142. method: 'POST',
  143. headers: {
  144. 'Content-Type': 'application/json'
  145. },
  146. body: JSON.stringify(store.current.justificada)
  147. });
  148. data = await res.json();
  149. }
  150. catch (error) {
  151. alert('Error al justificar');
  152. store.current.justificada = store.current.clone_justificada;
  153. }
  154. finally {
  155. delete store.current.clone_justificada;
  156. }
  157. store.current.justificada.justificador_nombre = data.justificador_nombre;
  158. store.current.justificada.justificador_clave = data.justificador_clave;
  159. store.current.justificada.justificador_facultad = data.justificador_facultad;
  160. store.current.justificada.justificador_rol = data.justificador_rol;
  161. store.current.justificada.registro_fecha_justificacion = data.registro_fecha_justificacion;
  162. },
  163. async justificarBloque(fecha, bloques, justificacion) {
  164. if (bloques.length === 0) {
  165. alert('No se ha seleccionado ningún bloque');
  166. return;
  167. }
  168. if (!justificacion) {
  169. alert('No se ha ingresado ninguna observación');
  170. return;
  171. }
  172. try {
  173. const res = await fetch('action/action_justificar.php', {
  174. method: 'PUT',
  175. headers: {
  176. 'Content-Type': 'application/json'
  177. },
  178. body: JSON.stringify({
  179. fecha,
  180. bloques,
  181. justificacion,
  182. })
  183. });
  184. const resData = await res.json();
  185. if (resData.status === 'success') {
  186. alert('Se ha justificado el bloque');
  187. store.current.modal_state = 'Cargando datos...';
  188. $('div.modal#cargando').modal('show');
  189. await store.registros.fetch();
  190. $('div.modal#cargando').modal('hide');
  191. }
  192. else {
  193. alert('No se ha podido justificar el bloque');
  194. }
  195. }
  196. catch (error) {
  197. alert('Error al justificar');
  198. }
  199. },
  200. registros: {
  201. data: [],
  202. async fetch(fecha, fecha_inicio, fecha_fin) {
  203. // if (!store.filters.facultad_id || !store.filters.periodo_id) return
  204. this.loading = true;
  205. this.data = [];
  206. const params = {
  207. facultad_id: 19,
  208. periodo_id: 2,
  209. };
  210. if (fecha)
  211. params['fecha'] = fecha;
  212. if (fecha_inicio)
  213. params['fecha_inicio'] = fecha_inicio;
  214. if (fecha_fin)
  215. params['fecha_fin'] = fecha_fin;
  216. <<<<<<< HEAD
  217. params['periodo_id'] = store.filters.todos_los_periodos ? 1 : 0;
  218. =======
  219. >>>>>>> 7688f1aac1824c234bc5f19b154e9ad1f4808d4f
  220. const paramsUrl = new URLSearchParams(params).toString();
  221. try {
  222. const res = await fetch(`action/action_auditoria.php?${paramsUrl}`, {
  223. method: 'GET',
  224. });
  225. this.data = await res.json();
  226. }
  227. catch (error) {
  228. alert('Error al cargar los datos');
  229. }
  230. this.loading = false;
  231. store.current.page = 1;
  232. },
  233. invertir() {
  234. this.data = this.data.reverse();
  235. },
  236. mostrarComentario(registro_id) {
  237. const registro = this.data.find((registro) => registro.registro_id === registro_id);
  238. store.current.comentario = registro.comentario;
  239. $('#ver-comentario').modal('show');
  240. },
  241. get relevant() {
  242. /*
  243. facultad_id: null,
  244. fecha: null,
  245. fecha_inicio: null,
  246. fecha_fin: null,
  247. profesor: null,
  248. asistencia: null,
  249. estado_id: null,
  250. if one of the filters is null, then it is not relevant
  251. */
  252. const filters = Object.keys(store.filters).filter((filtro) => store.filters[filtro] !== null || store.filters[filtro]?.length > 0);
  253. return this.data.filter((registro) => {
  254. return filters.every((filtro) => {
  255. switch (filtro) {
  256. case 'fecha':
  257. return registro.registro_fecha_ideal === store.filters[filtro];
  258. case 'fecha_inicio':
  259. return registro.registro_fecha_ideal >= store.filters[filtro];
  260. case 'fecha_fin':
  261. return registro.registro_fecha_ideal <= store.filters[filtro];
  262. case 'profesor':
  263. const textoFiltro = store.filters[filtro].toLowerCase();
  264. if (/^\([^)]+\)\s[\s\S]+$/.test(textoFiltro)) {
  265. const clave = registro.profesor_clave.toLowerCase();
  266. const filtroClave = textoFiltro.match(/\((.*?)\)/)?.[1];
  267. // console.log(clave, filtroClave);
  268. return clave.includes(filtroClave);
  269. }
  270. else {
  271. const nombre = registro.profesor_nombre.toLowerCase();
  272. return nombre.includes(textoFiltro);
  273. }
  274. case 'facultad_id':
  275. return registro.facultad_id === store.filters[filtro];
  276. case 'estados':
  277. if (store.filters[filtro].length === 0)
  278. return true;
  279. else if (store.filters[filtro].includes(-1) && registro.estado_supervisor_id === null)
  280. return true;
  281. return store.filters[filtro].includes(registro.estado_supervisor_id);
  282. case 'bloque_horario':
  283. const bloque = store.bloques_horario.data.find((bloque) => bloque.id === store.filters[filtro]);
  284. return registro.horario_hora < bloque.hora_fin && registro.horario_fin > bloque.hora_inicio;
  285. default: return true;
  286. }
  287. });
  288. });
  289. },
  290. async descargar() {
  291. store.current.modal_state = 'Generando reporte en Excel...';
  292. $('div.modal#cargando').modal('show');
  293. this.loading = true;
  294. if (this.relevant.length === 0)
  295. return;
  296. try {
  297. const res = await fetch('export/supervisor_excel.php', {
  298. method: 'POST',
  299. headers: {
  300. 'Content-Type': 'application/json'
  301. },
  302. body: JSON.stringify(this.relevant)
  303. });
  304. const blob = await res.blob();
  305. window.saveAs(blob, `auditoria_${new Date().toISOString().slice(0, 10)}.xlsx`);
  306. }
  307. catch (error) {
  308. if (error.response && error.response.status === 413) {
  309. alert('Your request is too large! Please reduce the data size and try again.');
  310. }
  311. else {
  312. alert('An error occurred: ' + error.message);
  313. }
  314. }
  315. finally {
  316. $('#cargando').modal('hide');
  317. this.loading = false;
  318. }
  319. },
  320. loading: false,
  321. get pages() {
  322. return Math.ceil(this.relevant.length / store.current.perPage);
  323. }
  324. },
  325. });
  326. createApp({
  327. store,
  328. messages: [],
  329. get clase_vista() {
  330. return store.current.clase_vista;
  331. },
  332. set_justificar(horario_id, profesor_id, registro_fecha_ideal) {
  333. 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);
  334. store.current.clone_justificada = JSON.parse(JSON.stringify(store.current.justificada));
  335. store.current.observaciones = false;
  336. },
  337. cancelar_justificacion() {
  338. Object.assign(store.current.justificada, store.current.clone_justificada);
  339. delete store.current.clone_justificada;
  340. },
  341. profesores: [],
  342. async mounted() {
  343. $('div.modal#cargando').modal('show');
  344. try {
  345. // await store.registros.fetch()
  346. await store.facultades.fetch();
  347. await store.estados.fetch();
  348. await store.bloques_horario.fetch();
  349. await store.filters.switchFechas();
  350. store.periodo = await fetch('action/periodo_datos.php').then(res => res.json());
  351. this.profesores = await (await fetch('action/action_profesor.php')).json();
  352. this.messages.push({ title: 'Datos cargados', text: 'Los datos se han cargado correctamente', type: 'success', timestamp: new Date() });
  353. }
  354. catch (error) {
  355. this.messages.push({ title: 'Error al cargar datos', text: 'No se pudieron cargar los datos', type: 'danger', timestamp: new Date() });
  356. }
  357. finally {
  358. $('div.modal#cargando').modal('hide');
  359. }
  360. }
  361. }).mount('#app');