Spaces:
Running
Running
test
Browse files- scripts.js +130 -186
scripts.js
CHANGED
|
@@ -1,186 +1,130 @@
|
|
| 1 |
-
$(document).ready(function () {
|
| 2 |
-
//
|
| 3 |
-
$
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
});
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
function
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
function
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
function
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
const option = document.createElement('option');
|
| 132 |
-
option.value = categoria;
|
| 133 |
-
option.textContent = categoria;
|
| 134 |
-
categoriaSelect.appendChild(option);
|
| 135 |
-
});
|
| 136 |
-
}
|
| 137 |
-
}
|
| 138 |
-
|
| 139 |
-
// Llenar el select de subcategorías según la categoría seleccionada
|
| 140 |
-
function cargarSubcategorias() {
|
| 141 |
-
const tipoSelect = document.getElementById('tipo');
|
| 142 |
-
const categoriaSelect = document.getElementById('categoria');
|
| 143 |
-
const subcategoriaSelect = document.getElementById('subcategoria');
|
| 144 |
-
// Limpiar subcategorías anteriores
|
| 145 |
-
subcategoriaSelect.innerHTML = '<option value="">Selecciona una subcategoría</option>';
|
| 146 |
-
|
| 147 |
-
if (categoriaSelect.value) {
|
| 148 |
-
const subcategorias = data.tipo[tipoSelect.value][categoriaSelect.value];
|
| 149 |
-
subcategorias.forEach(subcategoria => {
|
| 150 |
-
const option = document.createElement('option');
|
| 151 |
-
option.value = subcategoria;
|
| 152 |
-
option.textContent = subcategoria;
|
| 153 |
-
subcategoriaSelect.appendChild(option);
|
| 154 |
-
});
|
| 155 |
-
}
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
-
/**
|
| 159 |
-
* Cargar usuarios desde un archivo JSON.
|
| 160 |
-
*/
|
| 161 |
-
let usuarios;
|
| 162 |
-
|
| 163 |
-
// Cargar el archivo JSON y asignar los datos a la variable global `usuarios`
|
| 164 |
-
fetch('usuarios.json')
|
| 165 |
-
.then(response => response.json())
|
| 166 |
-
.then(jsonData => {
|
| 167 |
-
usuarios = jsonData; // Asignar los usuarios del JSON a la variable
|
| 168 |
-
cargarUsuarios(); // Llamar a la función para cargar los usuarios en el select
|
| 169 |
-
})
|
| 170 |
-
.catch(error => console.error('Error al cargar el archivo JSON:', error));
|
| 171 |
-
|
| 172 |
-
// Cargar los usuarios en el select
|
| 173 |
-
function cargarUsuarios() {
|
| 174 |
-
const usuarioSelect = document.getElementById('usuario');
|
| 175 |
-
|
| 176 |
-
// Limpiar las opciones existentes antes de agregar nuevas
|
| 177 |
-
usuarioSelect.innerHTML = '<option value="">Selecciona un usuario</option>';
|
| 178 |
-
|
| 179 |
-
// Agregar las nuevas opciones de usuario
|
| 180 |
-
for (const usuario of usuarios.nombre_usuarios) {
|
| 181 |
-
const option = document.createElement('option');
|
| 182 |
-
option.value = usuario;
|
| 183 |
-
option.textContent = usuario;
|
| 184 |
-
usuarioSelect.appendChild(option);
|
| 185 |
-
}
|
| 186 |
-
}
|
|
|
|
| 1 |
+
$(document).ready(function () {
|
| 2 |
+
// Cacheo de selectores
|
| 3 |
+
const $selectors = {
|
| 4 |
+
tipo: $('#tipo'),
|
| 5 |
+
categoria: $('#categoria'),
|
| 6 |
+
subcategoria: $('#subcategoria'),
|
| 7 |
+
usuario: $('#usuario'),
|
| 8 |
+
form: $('#transaccion-form'),
|
| 9 |
+
mensaje: $('#mensaje'),
|
| 10 |
+
loading: $('#loading')
|
| 11 |
+
};
|
| 12 |
+
|
| 13 |
+
// Variables de estado
|
| 14 |
+
let appData = {
|
| 15 |
+
tipos: null,
|
| 16 |
+
usuarios: null
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
// Carga inicial de datos
|
| 20 |
+
Promise.all([
|
| 21 |
+
fetch('tipos_categoria_subcategoria.json').then(r => r.json()),
|
| 22 |
+
fetch('usuarios.json').then(r => r.json())
|
| 23 |
+
])
|
| 24 |
+
.then(([tipos, usuarios]) => {
|
| 25 |
+
appData.tipos = tipos;
|
| 26 |
+
appData.usuarios = usuarios;
|
| 27 |
+
initSelects();
|
| 28 |
+
})
|
| 29 |
+
.catch(error => showError('Error cargando datos iniciales', error));
|
| 30 |
+
|
| 31 |
+
// Inicialización de selects
|
| 32 |
+
function initSelects() {
|
| 33 |
+
populateSelect($selectors.tipo, Object.keys(appData.tipos.tipo), 'Selecciona un tipo');
|
| 34 |
+
populateSelect($selectors.usuario, appData.usuarios.nombre_usuarios, 'Selecciona un usuario');
|
| 35 |
+
initEventHandlers();
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// Configuración de manejadores de eventos
|
| 39 |
+
function initEventHandlers() {
|
| 40 |
+
$selectors.tipo.on('change', handleTipoChange);
|
| 41 |
+
$selectors.categoria.on('change', handleCategoriaChange);
|
| 42 |
+
$selectors.form.on('submit', handleFormSubmit);
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// Manejador de cambio de tipo
|
| 46 |
+
function handleTipoChange() {
|
| 47 |
+
const tipo = $(this).val();
|
| 48 |
+
const categorias = tipo ? Object.keys(appData.tipos.tipo[tipo]) : [];
|
| 49 |
+
|
| 50 |
+
populateSelect($selectors.categoria, categorias, 'Selecciona una categoría');
|
| 51 |
+
populateSelect($selectors.subcategoria, [], 'Selecciona una subcategoría');
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// Manejador de cambio de categoría
|
| 55 |
+
function handleCategoriaChange() {
|
| 56 |
+
const tipo = $selectors.tipo.val();
|
| 57 |
+
const categoria = $(this).val();
|
| 58 |
+
const subcategorias = (tipo && categoria) ?
|
| 59 |
+
appData.tipos.tipo[tipo][categoria] : [];
|
| 60 |
+
|
| 61 |
+
populateSelect($selectors.subcategoria, subcategorias, 'Selecciona una subcategoría');
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// Manejador de envío de formulario
|
| 65 |
+
async function handleFormSubmit(e) {
|
| 66 |
+
e.preventDefault();
|
| 67 |
+
|
| 68 |
+
try {
|
| 69 |
+
$selectors.loading.show();
|
| 70 |
+
const formData = getFormData();
|
| 71 |
+
|
| 72 |
+
await sendData(formData);
|
| 73 |
+
|
| 74 |
+
showSuccess('¡Transacción registrada exitosamente!');
|
| 75 |
+
setTimeout(() => location.reload(), 4000);
|
| 76 |
+
} catch (error) {
|
| 77 |
+
showError('Error al enviar formulario', error);
|
| 78 |
+
} finally {
|
| 79 |
+
$selectors.loading.hide();
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
// Helpers
|
| 84 |
+
function populateSelect($select, items, defaultText) {
|
| 85 |
+
const options = items.length > 0 ?
|
| 86 |
+
items.map(item => `<option value="${item}">${item}</option>`) :
|
| 87 |
+
[`<option value="">${defaultText}</option>`];
|
| 88 |
+
|
| 89 |
+
$select.html([`<option value="">${defaultText}</option>`, ...options]);
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
function getFormData() {
|
| 93 |
+
const formData = Object.fromEntries($selectors.form.serializeArray().map(x => [x.name, x.value]));
|
| 94 |
+
|
| 95 |
+
// Formato de fecha para América/Lima
|
| 96 |
+
const now = new Date().toLocaleString('es-PE', {
|
| 97 |
+
timeZone: 'America/Lima',
|
| 98 |
+
year: 'numeric',
|
| 99 |
+
month: '2-digit',
|
| 100 |
+
day: '2-digit',
|
| 101 |
+
hour: '2-digit',
|
| 102 |
+
minute: '2-digit',
|
| 103 |
+
second: '2-digit',
|
| 104 |
+
hour12: false
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
const [fecha, hora] = now.split(', ');
|
| 108 |
+
return { ...formData, fecha: `${fecha.split('/').reverse().join('-')} ${hora}` };
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
async function sendData(data) {
|
| 112 |
+
const response = await fetch('https://script.google.com/macros/s/AKfycbyoS_q9j7Hixi1-dxVg6S7LKqb9o_Pma2I0mnBPmZ_vlKKSWyPY8lpw66woTMkyaD04/exec', {
|
| 113 |
+
method: 'POST',
|
| 114 |
+
headers: { 'Content-Type': 'application/json' },
|
| 115 |
+
body: JSON.stringify([data]),
|
| 116 |
+
mode: 'no-cors'
|
| 117 |
+
});
|
| 118 |
+
|
| 119 |
+
if (!response.ok) throw new Error('Error en la respuesta del servidor');
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
function showSuccess(message) {
|
| 123 |
+
$selectors.mensaje.html(`<div class="bg-green-500 p-3 rounded-lg">${message}</div>`);
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
function showError(context, error) {
|
| 127 |
+
console.error(`${context}:`, error);
|
| 128 |
+
$selectors.mensaje.html(`<div class="bg-red-500 p-3 rounded-lg">${error.message}</div>`);
|
| 129 |
+
}
|
| 130 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|