Logo mmoney

Acesse sua conta

Bem-vindo de volta! Por favor, insira seus dados.

Não tem uma conta? Registre-se

const querySnapshot = await getDocs(collection(db, "users", currentUser.uid, collectionName)); const data = querySnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })); if (data.length > 0) { const ws = XLSX.utils.json_to_sheet(data); XLSX.utils.book_append_sheet(wb, ws, collectionName); } } // Handle credit cards and their invoices (subcollections) const cardsSnapshot = await getDocs(collection(db, "users", currentUser.uid, 'credit_cards')); const cardsData = cardsSnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })); if (cardsData.length > 0) { const wsCards = XLSX.utils.json_to_sheet(cardsData); XLSX.utils.book_append_sheet(wb, wsCards, 'credit_cards'); let allInvoices = []; for (const card of cardsData) { const invoicesSnapshot = await getDocs(collection(db, "users", currentUser.uid, 'credit_cards', card.id, 'invoices')); const invoicesData = invoicesSnapshot.docs.map(doc => ({ id: doc.id, card_id: card.id, // Add parent card ID for re-importing ...doc.data() })); allInvoices.push(...invoicesData); } if (allInvoices.length > 0) { const wsInvoices = XLSX.utils.json_to_sheet(allInvoices); XLSX.utils.book_append_sheet(wb, wsInvoices, 'invoices'); } } XLSX.writeFile(wb, `findash_backup_${new Date().toISOString().slice(0, 10)}.xlsx`); } catch (error) { console.error("Erro ao exportar dados:", error); alert("Ocorreu um erro ao exportar os dados."); } } async function importData(file) { if (!currentUser) { alert('Você precisa estar logado para importar dados.'); return; } const reader = new FileReader(); reader.onload = async (e) => { try { const data = e.target.result; const workbook = XLSX.read(data, { type: 'array' }); const batch = writeBatch(db); // 1. Delete all existing data const collectionsToDelete = ['transactions', 'investments', 'subscriptions', 'credit_cards']; for (const collectionName of collectionsToDelete) { const existingDocsQuery = query(collection(db, "users", currentUser.uid, collectionName)); const existingDocs = await getDocs(existingDocsQuery); for(const docSnapshot of existingDocs.docs) { if (collectionName === 'credit_cards') { const invoicesSnapshot = await getDocs(collection(docSnapshot.ref, 'invoices')); invoicesSnapshot.forEach(invoiceDoc => batch.delete(invoiceDoc.ref)); } batch.delete(docSnapshot.ref); } } // 2. Import new data const sheetNames = workbook.SheetNames; // Process collections that don't depend on others first const standardCollections = ['transactions', 'investments', 'subscriptions', 'credit_cards']; for (const sheetName of sheetNames) { if (standardCollections.includes(sheetName)) { const sheetData = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]); for (const item of sheetData) { const { id, ...itemData } = item; // Firestore doesn't like undefined values from empty cells Object.keys(itemData).forEach(key => (itemData[key] === undefined || itemData[key] === null) && delete itemData[key]); const docRef = doc(db, "users", currentUser.uid, sheetName, id || crypto.randomUUID()); batch.set(docRef, itemData); } } } // Process invoices (subcollection of credit_cards) if (sheetNames.includes('invoices')) { const invoicesData = XLSX.utils.sheet_to_json(workbook.Sheets['invoices']); for (const invoice of invoicesData) { const { id, card_id, ...invoiceData } = invoice; Object.keys(invoiceData).forEach(key => (invoiceData[key] === undefined || invoiceData[key] === null) && delete invoiceData[key]); if (card_id) { const docRef = doc(db, "users", currentUser.uid, 'credit_cards', card_id, 'invoices', id || crypto.randomUUID()); batch.set(docRef, invoiceData); } } } await batch.commit(); alert('Dados importados com sucesso! A página será recarregada.'); window.location.reload(); } catch (error) { console.error("Erro ao importar dados:", error); alert("Ocorreu um erro ao importar os dados. Verifique se o arquivo está no formato correto."); } }; reader.readAsArrayBuffer(file); } // Close modals on outside click window.addEventListener('click', (e) => { const target = e.target; const userMenuDropdown = document.getElementById('user-menu-dropdown'); if (!userMenuDropdown.classList.contains('hidden') && !target.closest('#user-menu-button')) { userMenuDropdown.classList.add('hidden'); } if (target.matches('.fixed.inset-0') && !target.closest('.fixed.inset-0 > div')) { hideAllModals(); } });