2019-12-03 08:15:12 +00:00
|
|
|
import frappe from 'frappejs';
|
2019-12-06 18:43:37 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import { _ } from 'frappejs/utils';
|
2020-01-02 17:06:57 +00:00
|
|
|
import { remote, shell, ipcRenderer } from 'electron';
|
2020-01-01 08:11:57 +00:00
|
|
|
import SQLite from 'frappejs/backends/sqlite';
|
|
|
|
import postStart from '../server/postStart';
|
2019-12-03 13:10:21 +00:00
|
|
|
import router from '@/router';
|
2019-12-03 10:23:54 +00:00
|
|
|
import Avatar from '@/components/Avatar';
|
2019-12-06 18:43:37 +00:00
|
|
|
|
2019-10-26 14:46:04 +00:00
|
|
|
export function createNewDatabase() {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
remote.dialog.showSaveDialog(
|
|
|
|
remote.getCurrentWindow(),
|
|
|
|
{
|
|
|
|
title: _('Select folder'),
|
|
|
|
defaultPath: 'frappe-accounting.db'
|
|
|
|
},
|
|
|
|
filePath => {
|
|
|
|
if (filePath) {
|
|
|
|
if (!filePath.endsWith('.db')) {
|
|
|
|
filePath = filePath + '.db';
|
|
|
|
}
|
2019-12-27 10:23:13 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-10-26 14:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2019-11-27 18:39:16 +00:00
|
|
|
|
2020-01-01 08:11:57 +00:00
|
|
|
export async function connectToLocalDatabase(filepath) {
|
|
|
|
frappe.login('Administrator');
|
|
|
|
frappe.db = new SQLite({
|
|
|
|
dbPath: filepath
|
|
|
|
});
|
|
|
|
await frappe.db.connect();
|
|
|
|
await frappe.db.migrate();
|
|
|
|
await postStart();
|
|
|
|
// cache dbpath in localstorage
|
|
|
|
localStorage.dbPath = filepath;
|
|
|
|
}
|
|
|
|
|
2019-12-03 08:15:12 +00:00
|
|
|
export function showMessageDialog({ message, description, buttons = [] }) {
|
2019-11-27 18:39:16 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2019-12-03 08:15:12 +00:00
|
|
|
|
|
|
|
export function deleteDocWithPrompt(doc) {
|
2019-12-20 06:44:31 +00:00
|
|
|
return new Promise(resolve => {
|
2019-12-03 08:15:12 +00:00
|
|
|
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 => {
|
2019-12-23 10:37:30 +00:00
|
|
|
handleErrorWithDialog(e, doc);
|
2019-12-03 08:15:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: _('Cancel'),
|
|
|
|
action() {
|
|
|
|
resolve(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
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: `
|
|
|
|
<div class="flex items-center" v-if="label">
|
|
|
|
<Avatar class="flex-shrink-0" :imageURL="imageURL" :label="label" size="sm" />
|
|
|
|
<span class="ml-2 truncate">{{ label }}</span>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
};
|
|
|
|
}
|
2019-12-03 13:10:21 +00:00
|
|
|
|
|
|
|
export function openQuickEdit({ doctype, name, hideFields, defaults = {} }) {
|
|
|
|
let currentRoute = router.currentRoute;
|
2019-12-04 17:26:17 +00:00
|
|
|
let query = currentRoute.query;
|
|
|
|
let method = 'push';
|
|
|
|
if (query.edit && query.doctype === doctype) {
|
|
|
|
// replace the current route if we are
|
|
|
|
// editing another document of the same doctype
|
|
|
|
method = 'replace';
|
|
|
|
}
|
|
|
|
router[method]({
|
2019-12-03 13:10:21 +00:00
|
|
|
query: {
|
|
|
|
edit: 1,
|
|
|
|
doctype,
|
|
|
|
name,
|
|
|
|
hideFields,
|
|
|
|
values: defaults,
|
|
|
|
lastRoute: currentRoute
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-12-06 18:43:37 +00:00
|
|
|
|
2019-12-10 09:25:11 +00:00
|
|
|
export function handleErrorWithDialog(e, doc) {
|
2019-12-27 10:23:13 +00:00
|
|
|
let errorMessage = e.message || _('An error occurred');
|
2019-12-23 10:37:30 +00:00
|
|
|
if (e.type === frappe.errors.LinkValidationError) {
|
|
|
|
errorMessage = _('{0} {1} is linked with existing records.', [
|
|
|
|
doc.doctype,
|
|
|
|
doc.name
|
|
|
|
]);
|
|
|
|
} else if (e.type === frappe.errors.DuplicateEntryError) {
|
2019-12-10 09:25:11 +00:00
|
|
|
errorMessage = _('{0} {1} already exists.', [doc.doctype, doc.name]);
|
|
|
|
}
|
2019-12-23 10:37:30 +00:00
|
|
|
|
2019-12-10 09:25:11 +00:00
|
|
|
showMessageDialog({
|
|
|
|
message: errorMessage
|
|
|
|
});
|
2019-12-23 10:37:30 +00:00
|
|
|
|
2019-12-10 09:25:11 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2019-12-06 18:43:37 +00:00
|
|
|
export function makePDF(html, destination) {
|
|
|
|
const { BrowserWindow } = remote;
|
|
|
|
|
|
|
|
let printWindow = new BrowserWindow({
|
2019-12-12 17:37:43 +00:00
|
|
|
width: 595,
|
|
|
|
height: 842,
|
2019-12-06 18:43:37 +00:00
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-12-12 17:37:43 +00:00
|
|
|
let webpackDevServerURL = remote.getGlobal('WEBPACK_DEV_SERVER_URL');
|
|
|
|
if (webpackDevServerURL) {
|
|
|
|
// Load the url of the dev server if in development mode
|
|
|
|
printWindow.loadURL(webpackDevServerURL + 'print');
|
2019-12-06 18:43:37 +00:00
|
|
|
} else {
|
2019-12-12 17:37:43 +00:00
|
|
|
// Load the index.html when not in development
|
|
|
|
printWindow.loadURL(`app://./print.html`);
|
2019-12-06 18:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printWindow.on('closed', () => {
|
|
|
|
printWindow = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
const code = `
|
|
|
|
document.body.innerHTML = \`${html}\`;
|
|
|
|
`;
|
|
|
|
|
|
|
|
printWindow.webContents.executeJavaScript(code);
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
printWindow.webContents.on('did-finish-load', () => {
|
|
|
|
printWindow.webContents.printToPDF(
|
|
|
|
{
|
|
|
|
marginsType: 1, // no margin
|
|
|
|
pageSize: 'A4',
|
|
|
|
printBackground: true
|
|
|
|
},
|
|
|
|
(error, data) => {
|
|
|
|
if (error) throw error;
|
|
|
|
printWindow.close();
|
|
|
|
fs.writeFile(destination, data, error => {
|
|
|
|
if (error) throw error;
|
|
|
|
resolve(shell.openItem(destination));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-12-20 06:44:31 +00:00
|
|
|
|
|
|
|
export function getActionsForDocument(doc) {
|
|
|
|
if (!doc) return [];
|
|
|
|
|
|
|
|
let deleteAction = {
|
|
|
|
component: {
|
|
|
|
template: `<span class="text-red-700">{{ _('Delete') }}</span>`
|
|
|
|
},
|
|
|
|
condition: doc => !doc.isNew() && !doc.submitted,
|
|
|
|
action: () =>
|
|
|
|
deleteDocWithPrompt(doc).then(res => {
|
|
|
|
if (res) {
|
|
|
|
router.push(`/list/${doc.doctype}`);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let actions = [...(doc.meta.actions || []), deleteAction]
|
|
|
|
.filter(d => (d.condition ? d.condition(doc) : true))
|
|
|
|
.map(d => {
|
|
|
|
return {
|
|
|
|
label: d.label,
|
|
|
|
component: d.component,
|
|
|
|
action: d.action.bind(this, doc, router)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return actions;
|
|
|
|
}
|
2020-01-02 17:06:57 +00:00
|
|
|
|
|
|
|
export function openSettings(tab = 'General') {
|
|
|
|
ipcRenderer.send('open-settings-window', tab);
|
|
|
|
}
|