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-28 11:10:01 +00:00
|
|
|
import migrate from './migrate';
|
2021-11-04 09:33:51 +00:00
|
|
|
import { ipcRenderer } from 'electron';
|
|
|
|
import { IPC_MESSAGES, IPC_ACTIONS } from './messages';
|
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';
|
2020-01-28 08:20:01 +00:00
|
|
|
import config from '@/config';
|
2019-12-06 18:43:37 +00:00
|
|
|
|
2021-08-24 20:10:16 +00:00
|
|
|
export async function createNewDatabase() {
|
|
|
|
const options = {
|
|
|
|
title: _('Select folder'),
|
2021-11-04 09:33:51 +00:00
|
|
|
defaultPath: 'frappe-books.db',
|
2021-08-24 20:10:16 +00:00
|
|
|
};
|
|
|
|
|
2021-11-09 10:38:25 +00:00
|
|
|
let { canceled, filePath } = await ipcRenderer.invoke(
|
2021-11-04 09:33:51 +00:00
|
|
|
IPC_ACTIONS.GET_SAVE_FILEPATH,
|
|
|
|
options
|
|
|
|
);
|
2021-11-09 10:38:25 +00:00
|
|
|
|
|
|
|
if (canceled || filePath.length === 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!filePath.endsWith('.db')) {
|
2021-11-09 11:14:05 +00:00
|
|
|
showMessageDialog({
|
|
|
|
message: "Please select a filename ending with '.db'.",
|
|
|
|
});
|
|
|
|
return '';
|
2021-08-24 20:10:16 +00:00
|
|
|
}
|
2021-11-09 10:38:25 +00:00
|
|
|
|
|
|
|
if (fs.existsSync(filePath)) {
|
|
|
|
fs.unlinkSync(filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return filePath;
|
2019-10-26 14:46:04 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 15:57:58 +00:00
|
|
|
export async function loadExistingDatabase() {
|
|
|
|
const options = {
|
|
|
|
title: _('Select file'),
|
|
|
|
properties: ['openFile'],
|
2021-11-04 09:33:51 +00:00
|
|
|
filters: [{ name: 'SQLite DB File', extensions: ['db'] }],
|
2021-09-04 15:57:58 +00:00
|
|
|
};
|
|
|
|
|
2021-11-04 09:33:51 +00:00
|
|
|
const { filePaths } = await ipcRenderer.invoke(
|
|
|
|
IPC_ACTIONS.GET_OPEN_FILEPATH,
|
|
|
|
options
|
|
|
|
);
|
2021-10-25 16:16:12 +00:00
|
|
|
|
2021-09-04 15:57:58 +00:00
|
|
|
if (filePaths && filePaths[0]) {
|
2021-10-25 16:16:12 +00:00
|
|
|
return filePaths[0];
|
2021-09-04 15:57:58 +00:00
|
|
|
}
|
2019-10-26 14:46:04 +00:00
|
|
|
}
|
2019-11-27 18:39:16 +00:00
|
|
|
|
2021-11-09 10:38:25 +00:00
|
|
|
export async function connectToLocalDatabase(filePath) {
|
|
|
|
if (!filePath) {
|
2021-11-05 19:41:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-01 08:11:57 +00:00
|
|
|
frappe.login('Administrator');
|
2021-11-05 19:41:39 +00:00
|
|
|
try {
|
|
|
|
frappe.db = new SQLite({
|
2021-11-09 10:38:25 +00:00
|
|
|
dbPath: filePath,
|
2021-11-05 19:41:39 +00:00
|
|
|
});
|
|
|
|
await frappe.db.connect();
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-09 10:38:25 +00:00
|
|
|
|
2020-01-28 11:10:01 +00:00
|
|
|
await migrate();
|
2020-01-01 08:11:57 +00:00
|
|
|
await postStart();
|
2020-01-28 08:20:01 +00:00
|
|
|
|
|
|
|
// set file info in config
|
|
|
|
let files = config.get('files') || [];
|
2021-11-09 10:38:25 +00:00
|
|
|
if (!files.find((file) => file.filePath === filePath)) {
|
2020-01-28 08:20:01 +00:00
|
|
|
files = [
|
|
|
|
{
|
|
|
|
companyName: frappe.AccountingSettings.companyName,
|
2021-11-09 10:38:25 +00:00
|
|
|
filePath: filePath,
|
2020-01-28 08:20:01 +00:00
|
|
|
},
|
2021-11-04 09:33:51 +00:00
|
|
|
...files,
|
2020-01-28 08:20:01 +00:00
|
|
|
];
|
|
|
|
config.set('files', files);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set last selected file
|
2021-11-09 10:38:25 +00:00
|
|
|
config.set('lastSelectedFilePath', filePath);
|
2021-11-05 19:41:39 +00:00
|
|
|
return true;
|
2020-01-01 08:11:57 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 07:01:05 +00:00
|
|
|
export async function showMessageDialog({
|
|
|
|
message,
|
|
|
|
description,
|
2021-11-04 09:33:51 +00:00
|
|
|
buttons = [],
|
2021-08-18 07:01:05 +00:00
|
|
|
}) {
|
2021-11-04 09:33:51 +00:00
|
|
|
const options = {
|
|
|
|
message,
|
|
|
|
detail: description,
|
|
|
|
buttons: buttons.map((a) => a.label),
|
|
|
|
};
|
|
|
|
|
|
|
|
const { response } = await ipcRenderer.invoke(
|
|
|
|
IPC_ACTIONS.GET_DIALOG_RESPONSE,
|
|
|
|
options
|
2019-11-27 18:39:16 +00:00
|
|
|
);
|
2021-08-18 06:32:45 +00:00
|
|
|
|
|
|
|
let button = buttons[response];
|
|
|
|
if (button && button.action) {
|
|
|
|
button.action();
|
|
|
|
}
|
2019-11-27 18:39:16 +00:00
|
|
|
}
|
2019-12-03 08:15:12 +00:00
|
|
|
|
|
|
|
export function deleteDocWithPrompt(doc) {
|
2021-11-04 09:33:51 +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,
|
2021-11-04 09:33:51 +00:00
|
|
|
doc.name,
|
2019-12-03 08:15:12 +00:00
|
|
|
]),
|
|
|
|
description: _('This action is permanent'),
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
label: _('Delete'),
|
|
|
|
action: () => {
|
|
|
|
doc
|
|
|
|
.delete()
|
|
|
|
.then(() => resolve(true))
|
2021-11-04 09:33:51 +00:00
|
|
|
.catch((e) => {
|
2019-12-23 10:37:30 +00:00
|
|
|
handleErrorWithDialog(e, doc);
|
2019-12-03 08:15:12 +00:00
|
|
|
});
|
2021-11-04 09:33:51 +00:00
|
|
|
},
|
2019-12-03 08:15:12 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: _('Cancel'),
|
|
|
|
action() {
|
|
|
|
resolve(false);
|
2021-11-04 09:33:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2019-12-03 08:15:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-12-03 10:23:54 +00:00
|
|
|
|
2021-11-21 13:38:04 +00:00
|
|
|
export function cancelDocWithPrompt(doc) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
showMessageDialog({
|
|
|
|
message: _('Are you sure you want to cancel {0} "{1}"?', [
|
|
|
|
doc.doctype,
|
|
|
|
doc.name,
|
|
|
|
]),
|
|
|
|
description: _('This action is permanent'),
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
label: _('Yes'),
|
|
|
|
async action() {
|
|
|
|
const entryDoc = await frappe.getDoc(doc.doctype, doc.name);
|
|
|
|
entryDoc.cancelled = 1;
|
|
|
|
await entryDoc.update();
|
|
|
|
entryDoc
|
|
|
|
.revert()
|
|
|
|
.then(() => resolve(true))
|
|
|
|
.catch((e) => {
|
|
|
|
handleErrorWithDialog(e, doc);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: _('No'),
|
|
|
|
action() {
|
|
|
|
resolve(false);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-03 10:23:54 +00:00
|
|
|
export function partyWithAvatar(party) {
|
|
|
|
return {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
imageURL: null,
|
2021-11-04 09:33:51 +00:00
|
|
|
label: null,
|
2019-12-03 10:23:54 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
components: {
|
2021-11-04 09:33:51 +00:00
|
|
|
Avatar,
|
2019-12-03 10:23:54 +00:00
|
|
|
},
|
|
|
|
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>
|
2021-11-04 09:33:51 +00:00
|
|
|
`,
|
2019-12-03 10:23:54 +00:00
|
|
|
};
|
|
|
|
}
|
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';
|
|
|
|
}
|
2021-11-22 14:14:52 +00:00
|
|
|
if (query.name === name) return
|
2019-12-04 17:26:17 +00:00
|
|
|
router[method]({
|
2019-12-03 13:10:21 +00:00
|
|
|
query: {
|
|
|
|
edit: 1,
|
|
|
|
doctype,
|
|
|
|
name,
|
|
|
|
hideFields,
|
|
|
|
values: defaults,
|
2021-11-04 09:33:51 +00:00
|
|
|
lastRoute: currentRoute,
|
|
|
|
},
|
2019-12-03 13:10:21 +00:00
|
|
|
});
|
|
|
|
}
|
2019-12-06 18:43:37 +00:00
|
|
|
|
2020-01-29 11:01:45 +00:00
|
|
|
export function getErrorMessage(e, doc) {
|
2019-12-27 10:23:13 +00:00
|
|
|
let errorMessage = e.message || _('An error occurred');
|
2021-11-05 19:41:39 +00:00
|
|
|
const { doctype, name } = doc;
|
|
|
|
const canElaborate = doctype && name;
|
|
|
|
if (e.type === frappe.errors.LinkValidationError && canElaborate) {
|
2019-12-23 10:37:30 +00:00
|
|
|
errorMessage = _('{0} {1} is linked with existing records.', [
|
2021-11-05 19:41:39 +00:00
|
|
|
doctype,
|
|
|
|
name,
|
2019-12-23 10:37:30 +00:00
|
|
|
]);
|
2021-11-05 19:41:39 +00:00
|
|
|
} else if (e.type === frappe.errors.DuplicateEntryError && canElaborate) {
|
|
|
|
errorMessage = _('{0} {1} already exists.', [doctype, name]);
|
2019-12-10 09:25:11 +00:00
|
|
|
}
|
2020-01-29 11:01:45 +00:00
|
|
|
return errorMessage;
|
|
|
|
}
|
2019-12-23 10:37:30 +00:00
|
|
|
|
2020-01-29 11:01:45 +00:00
|
|
|
export function handleErrorWithDialog(e, doc) {
|
|
|
|
let errorMessage = getErrorMessage(e, doc);
|
|
|
|
showMessageDialog({ message: errorMessage });
|
2019-12-10 09:25:11 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:33:51 +00:00
|
|
|
export async function makePDF(html, savePath) {
|
|
|
|
ipcRenderer.invoke(IPC_ACTIONS.SAVE_HTML_AS_PDF, html, savePath);
|
2019-12-06 18:43:37 +00:00
|
|
|
}
|
2019-12-20 06:44:31 +00:00
|
|
|
|
|
|
|
export function getActionsForDocument(doc) {
|
|
|
|
if (!doc) return [];
|
|
|
|
|
|
|
|
let deleteAction = {
|
|
|
|
component: {
|
2021-11-04 09:33:51 +00:00
|
|
|
template: `<span class="text-red-700">{{ _('Delete') }}</span>`,
|
2019-12-20 06:44:31 +00:00
|
|
|
},
|
2021-11-21 13:38:04 +00:00
|
|
|
condition: (doc) => !doc.isNew() && !doc.submitted && !doc.meta.isSingle && !doc.cancelled,
|
2019-12-20 06:44:31 +00:00
|
|
|
action: () =>
|
2021-11-04 09:33:51 +00:00
|
|
|
deleteDocWithPrompt(doc).then((res) => {
|
2019-12-20 06:44:31 +00:00
|
|
|
if (res) {
|
2021-11-21 16:15:27 +00:00
|
|
|
routeTo(`/list/${doc.doctype}`);
|
2019-12-20 06:44:31 +00:00
|
|
|
}
|
2021-11-04 09:33:51 +00:00
|
|
|
}),
|
2019-12-20 06:44:31 +00:00
|
|
|
};
|
|
|
|
|
2021-11-21 13:38:04 +00:00
|
|
|
let cancelAction = {
|
|
|
|
component: {
|
|
|
|
template: `<span class="text-red-700">{{ _('Cancel') }}</span>`,
|
|
|
|
},
|
|
|
|
condition: (doc) => doc.submitted && !doc.cancelled,
|
|
|
|
action: () => {
|
|
|
|
cancelDocWithPrompt(doc).then((res) => {
|
|
|
|
if (res) {
|
|
|
|
router.push(`/list/${doc.doctype}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let actions = [...(doc.meta.actions || []), deleteAction, cancelAction]
|
2021-11-04 09:33:51 +00:00
|
|
|
.filter((d) => (d.condition ? d.condition(doc) : true))
|
|
|
|
.map((d) => {
|
2019-12-20 06:44:31 +00:00
|
|
|
return {
|
|
|
|
label: d.label,
|
|
|
|
component: d.component,
|
2021-11-04 09:33:51 +00:00
|
|
|
action: d.action.bind(this, doc, router),
|
2019-12-20 06:44:31 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return actions;
|
|
|
|
}
|
2020-01-02 17:06:57 +00:00
|
|
|
|
|
|
|
export function openSettings(tab = 'General') {
|
2021-11-04 09:33:51 +00:00
|
|
|
ipcRenderer.send(IPC_MESSAGES.OPEN_SETTINGS, tab);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function runWindowAction(name) {
|
|
|
|
switch (name) {
|
|
|
|
case 'close':
|
|
|
|
ipcRenderer.send(IPC_MESSAGES.CLOSE_CURRENT_WINDOW);
|
|
|
|
break;
|
|
|
|
case 'minimize':
|
|
|
|
ipcRenderer.send(IPC_MESSAGES.MINIMIZE_CURRENT_WINDOW);
|
|
|
|
break;
|
|
|
|
case 'maximize':
|
|
|
|
const maximizing = await ipcRenderer.invoke(
|
|
|
|
IPC_ACTIONS.TOGGLE_MAXIMIZE_CURRENT_WINDOW
|
|
|
|
);
|
|
|
|
name = maximizing ? name : 'unmaximize';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return name;
|
2021-11-11 08:11:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 09:35:34 +00:00
|
|
|
export const statusColor = {
|
|
|
|
Draft: 'gray',
|
|
|
|
Unpaid: 'orange',
|
|
|
|
Paid: 'green',
|
2021-11-21 13:38:04 +00:00
|
|
|
Cancelled: 'red',
|
2021-11-11 09:35:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function getInvoiceStatus(doc) {
|
2021-11-11 08:11:33 +00:00
|
|
|
let status = 'Unpaid';
|
|
|
|
if (!doc.submitted) {
|
|
|
|
status = 'Draft';
|
|
|
|
}
|
|
|
|
if (doc.submitted === 1 && doc.outstandingAmount === 0.0) {
|
|
|
|
status = 'Paid';
|
|
|
|
}
|
2021-11-21 13:38:04 +00:00
|
|
|
if (doc.cancelled === 1) {
|
|
|
|
status = 'Cancelled';
|
|
|
|
}
|
2021-11-11 09:35:34 +00:00
|
|
|
return status;
|
2021-11-11 08:11:33 +00:00
|
|
|
}
|
2021-11-21 16:15:27 +00:00
|
|
|
|
|
|
|
export function routeTo(route) {
|
|
|
|
if (route !== router.currentRoute.fullPath) {
|
|
|
|
router.push(route);
|
|
|
|
}
|
|
|
|
}
|