import frappe from 'frappejs'; import fs from 'fs'; import { _ } from 'frappejs/utils'; import migrate from './migrate'; import { remote, shell, ipcRenderer } from 'electron'; import SQLite from 'frappejs/backends/sqlite'; import postStart from '../server/postStart'; import router from '@/router'; import Avatar from '@/components/Avatar'; import config from '@/config'; export function createNewDatabase() { return new Promise(resolve => { remote.dialog.showSaveDialog( remote.getCurrentWindow(), { title: _('Select folder'), defaultPath: 'frappe-books.db' }, filePath => { if (filePath) { if (!filePath.endsWith('.db')) { filePath = filePath + '.db'; } if (fs.existsSync(filePath)) { showMessageDialog({ // prettier-ignore message: _('A file exists with the same name and it will be overwritten. Are you sure you want to continue?'), buttons: [ { label: _('Overwrite'), action() { fs.unlinkSync(filePath); resolve(filePath); } }, { label: _('Cancel'), action() {} } ] }); } else { resolve(filePath); } } } ); }); } export function loadExistingDatabase() { return new Promise(resolve => { remote.dialog.showOpenDialog( remote.getCurrentWindow(), { title: _('Select file'), properties: ['openFile'], filters: [{ name: 'SQLite DB File', extensions: ['db'] }] }, files => { if (files && files[0]) { resolve(files[0]); } } ); }); } export async function connectToLocalDatabase(filepath) { frappe.login('Administrator'); frappe.db = new SQLite({ dbPath: filepath }); await frappe.db.connect(); await migrate(); await postStart(); // set file info in config let files = config.get('files') || []; if (!files.find(file => file.filePath === filepath)) { files = [ { companyName: frappe.AccountingSettings.companyName, filePath: filepath }, ...files ]; config.set('files', files); } // set last selected file config.set('lastSelectedFilePath', filepath); } export function showMessageDialog({ message, description, buttons = [] }) { let buttonLabels = buttons.map(a => a.label); remote.dialog.showMessageBox( remote.getCurrentWindow(), { message, detail: description, buttons: buttonLabels }, response => { let button = buttons[response]; if (button && button.action) { button.action(); } } ); } export function deleteDocWithPrompt(doc) { return new Promise(resolve => { showMessageDialog({ message: _('Are you sure you want to delete {0} "{1}"?', [ doc.doctype, doc.name ]), description: _('This action is permanent'), buttons: [ { label: _('Delete'), action: () => { doc .delete() .then(() => resolve(true)) .catch(e => { handleErrorWithDialog(e, doc); }); } }, { label: _('Cancel'), action() { resolve(false); } } ] }); }); } export function partyWithAvatar(party) { return { data() { return { imageURL: null, label: null }; }, components: { Avatar }, async mounted() { this.imageURL = await frappe.db.getValue('Party', party, 'image'); this.label = party; }, template: `