Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Função para converter YYYY-MM-DD para DD/MM/YYYY
- function formatDate(dateStr) {
- const [year, month, day] = dateStr.split('-');
- return `${day}/${month}/${year}`;
- }
- // Carregar dados com filtros
- async function loadData() {
- console.log('Carregando dados...');
- const startDate = document.getElementById('startDate').value;
- const endDate = document.getElementById('endDate').value;
- const categoria = document.getElementById('categoria').value;
- const subCategoria = document.getElementById('subCategoria').value;
- const search = document.getElementById('search').value;
- const params = new URLSearchParams();
- if (startDate) params.append('startDate', startDate);
- if (endDate) params.append('endDate', endDate);
- if (categoria) params.append('categoria', categoria);
- if (subCategoria) params.append('sub_categoria', subCategoria);
- if (search) params.append('search', search);
- try {
- const response = await fetch(`/cashflow/api/transactions?${params.toString()}`, {
- method: 'GET',
- credentials: 'same-origin'
- });
- if (!response.ok) {
- throw new Error(`Erro na requisição: ${response.status}`);
- }
- const data = await response.json();
- console.log('Dados recebidos:', data);
- const tbody = document.getElementById('transactionsBody');
- tbody.innerHTML = '';
- data.forEach(row => {
- const tr = document.createElement('tr');
- tr.innerHTML = `
- <td>${formatDate(row.data_operacao)}</td>
- <td>${row.descricao}</td>
- <td>${row.montante.toFixed(2)}</td>
- <td>${row.destino}</td>
- <td>${row.categoria}</td>
- <td>${row.sub_categoria}</td>
- `;
- tbody.appendChild(tr);
- });
- } catch (error) {
- console.error('Erro ao carregar dados:', error);
- alert('Erro ao carregar dados: ' + error.message);
- }
- }
- // Upload de ficheiro Excel
- async function uploadExcel() {
- console.log('Iniciando upload...');
- const fileInput = document.getElementById('excelFile');
- const file = fileInput.files[0];
- if (!file) {
- console.error('Nenhum ficheiro selecionado');
- alert('Selecione um ficheiro Excel!');
- return;
- }
- const formData = new FormData();
- formData.append('excel', file);
- try {
- const response = await fetch('/cashflow/api/import', {
- method: 'POST',
- body: formData,
- credentials: 'same-origin'
- });
- if (!response.ok) {
- throw new Error(`Erro no upload: ${response.status}`);
- }
- const result = await response.json();
- console.log('Resultado do upload:', result);
- alert(`Importados ${result.success} registos com ${result.errors} erros.`);
- loadData();
- } catch (error) {
- console.error('Erro ao fazer upload:', error);
- alert('Erro ao fazer upload: ' + error.message);
- }
- }
- // Carregar dados iniciais
- window.onload = () => {
- console.log('Página carregada, inicializando...');
- loadData();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement