Advertisement
tool684

script.js

Apr 17th, 2025
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.19 KB | Source Code | 0 0
  1. // Função para converter YYYY-MM-DD para DD/MM/YYYY
  2. function formatDate(dateStr) {
  3.     const [year, month, day] = dateStr.split('-');
  4.     return `${day}/${month}/${year}`;
  5. }
  6.  
  7. // Carregar dados com filtros
  8. async function loadData() {
  9.     console.log('Carregando dados...');
  10.     const startDate = document.getElementById('startDate').value;
  11.     const endDate = document.getElementById('endDate').value;
  12.     const categoria = document.getElementById('categoria').value;
  13.     const subCategoria = document.getElementById('subCategoria').value;
  14.     const search = document.getElementById('search').value;
  15.  
  16.     const params = new URLSearchParams();
  17.     if (startDate) params.append('startDate', startDate);
  18.     if (endDate) params.append('endDate', endDate);
  19.     if (categoria) params.append('categoria', categoria);
  20.     if (subCategoria) params.append('sub_categoria', subCategoria);
  21.     if (search) params.append('search', search);
  22.  
  23.     try {
  24.         const response = await fetch(`/cashflow/api/transactions?${params.toString()}`, {
  25.             method: 'GET',
  26.             credentials: 'same-origin'
  27.         });
  28.         if (!response.ok) {
  29.             throw new Error(`Erro na requisição: ${response.status}`);
  30.         }
  31.         const data = await response.json();
  32.         console.log('Dados recebidos:', data);
  33.  
  34.         const tbody = document.getElementById('transactionsBody');
  35.         tbody.innerHTML = '';
  36.  
  37.         data.forEach(row => {
  38.             const tr = document.createElement('tr');
  39.             tr.innerHTML = `
  40.                 <td>${formatDate(row.data_operacao)}</td>
  41.                 <td>${row.descricao}</td>
  42.                 <td>${row.montante.toFixed(2)}</td>
  43.                 <td>${row.destino}</td>
  44.                 <td>${row.categoria}</td>
  45.                 <td>${row.sub_categoria}</td>
  46.             `;
  47.             tbody.appendChild(tr);
  48.         });
  49.     } catch (error) {
  50.         console.error('Erro ao carregar dados:', error);
  51.         alert('Erro ao carregar dados: ' + error.message);
  52.     }
  53. }
  54.  
  55. // Upload de ficheiro Excel
  56. async function uploadExcel() {
  57.     console.log('Iniciando upload...');
  58.     const fileInput = document.getElementById('excelFile');
  59.     const file = fileInput.files[0];
  60.     if (!file) {
  61.         console.error('Nenhum ficheiro selecionado');
  62.         alert('Selecione um ficheiro Excel!');
  63.         return;
  64.     }
  65.  
  66.     const formData = new FormData();
  67.     formData.append('excel', file);
  68.  
  69.     try {
  70.         const response = await fetch('/cashflow/api/import', {
  71.             method: 'POST',
  72.             body: formData,
  73.             credentials: 'same-origin'
  74.         });
  75.         if (!response.ok) {
  76.             throw new Error(`Erro no upload: ${response.status}`);
  77.         }
  78.         const result = await response.json();
  79.         console.log('Resultado do upload:', result);
  80.         alert(`Importados ${result.success} registos com ${result.errors} erros.`);
  81.         loadData();
  82.     } catch (error) {
  83.         console.error('Erro ao fazer upload:', error);
  84.         alert('Erro ao fazer upload: ' + error.message);
  85.     }
  86. }
  87.  
  88. // Carregar dados iniciais
  89. window.onload = () => {
  90.     console.log('Página carregada, inicializando...');
  91.     loadData();
  92. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement