mirror of
https://github.com/frappe/books.git
synced 2024-11-09 23:30:56 +00:00
feat: add function to pop toast
fix: use calls to ipcMain to save data refactor: remove redundant file and comments
This commit is contained in:
parent
04fb48faed
commit
f21aee6664
@ -1,14 +1,10 @@
|
||||
import { makeJSON, showMessageDialog } from '@/utils';
|
||||
import { showMessageDialog } from '@/utils';
|
||||
import frappe from 'frappejs';
|
||||
import { sleep, _ } from 'frappejs/utils';
|
||||
import { _ } from 'frappejs/utils';
|
||||
import { DateTime } from 'luxon';
|
||||
import { saveExportData } from '../reports/commonExporter';
|
||||
import { getSavePath } from '../src/utils';
|
||||
|
||||
/**
|
||||
* GST is a map which gives a final rate for any given gst item
|
||||
* eg: IGST-18 = 18
|
||||
* eg: GST-18 = CGST-9 + SGST-9 = 18
|
||||
*/
|
||||
const GST = {
|
||||
'GST-0': 0,
|
||||
'GST-0.25': 0.25,
|
||||
@ -28,10 +24,6 @@ const GST = {
|
||||
'IGST-28': 28,
|
||||
};
|
||||
|
||||
/**
|
||||
* CSGST is a map which return the tax rate component for state or central
|
||||
* eg: GST-12 = 6
|
||||
*/
|
||||
const CSGST = {
|
||||
'GST-0': 0,
|
||||
'GST-0.25': 0.125,
|
||||
@ -43,10 +35,6 @@ const CSGST = {
|
||||
'GST-28': 14,
|
||||
};
|
||||
|
||||
/**
|
||||
* IGST is a map which return the tax rate for the igst item
|
||||
* eg: IGST-18 = 18
|
||||
*/
|
||||
const IGST = {
|
||||
'IGST-0.25': 0.25,
|
||||
'IGST-3': 3,
|
||||
@ -85,7 +73,6 @@ export async function generateGstr1Json(getReportData) {
|
||||
fp: DateTime.fromISO(toDate).toFormat('MMyyyy'),
|
||||
};
|
||||
|
||||
// based condition we need to triggered different methods
|
||||
if (transferType === 'B2B') {
|
||||
gstData.b2b = await generateB2bData(rows);
|
||||
} else if (transferType === 'B2CL') {
|
||||
@ -94,16 +81,14 @@ export async function generateGstr1Json(getReportData) {
|
||||
gstData.b2cs = await generateB2csData(rows);
|
||||
}
|
||||
|
||||
await sleep(1);
|
||||
const jsonData = JSON.stringify(gstData);
|
||||
makeJSON(jsonData, filePath);
|
||||
await saveExportData(jsonData, filePath);
|
||||
}
|
||||
|
||||
async function generateB2bData(invoices) {
|
||||
const b2b = [];
|
||||
|
||||
invoices.forEach(async (row) => {
|
||||
// it's must for the customer to have a gstin, if not it should not be here
|
||||
const customer = {
|
||||
ctin: row.gstin,
|
||||
inv: [],
|
||||
|
@ -1,6 +1,10 @@
|
||||
import frappe from 'frappejs';
|
||||
import fs from 'fs/promises';
|
||||
import { getSavePath } from '../src/utils';
|
||||
import {
|
||||
getSavePath,
|
||||
saveData,
|
||||
showItemInFolder,
|
||||
showToast,
|
||||
} from '../src/utils';
|
||||
|
||||
function templateToInnerText(innerHTML) {
|
||||
const temp = document.createElement('template');
|
||||
@ -37,7 +41,8 @@ async function exportCsv(rows, columns, filePath) {
|
||||
labels.join(','),
|
||||
...rows.map((row) => fieldnames.map((f) => csvFormat(row[f])).join(',')),
|
||||
];
|
||||
await fs.writeFile(filePath, csvRows.join('\n'));
|
||||
|
||||
saveExportData(csvRows.join('\n'), filePath);
|
||||
}
|
||||
|
||||
async function exportJson(rows, columns, filePath, filters, reportName) {
|
||||
@ -68,7 +73,7 @@ async function exportJson(rows, columns, filePath, filters, reportName) {
|
||||
exportObject.softwareName = 'Frappe Books';
|
||||
exportObject.softwareVersion = frappe.store.appVersion;
|
||||
|
||||
await fs.writeFile(filePath, JSON.stringify(exportObject));
|
||||
await saveExportData(JSON.stringify(exportObject), filePath);
|
||||
}
|
||||
|
||||
async function exportReport(extention, reportName, getReportData) {
|
||||
@ -98,3 +103,14 @@ export default function getCommonExportActions(reportName) {
|
||||
await exportReport(ext, reportName, getReportData),
|
||||
}));
|
||||
}
|
||||
|
||||
export async function saveExportData(data, filePath) {
|
||||
await saveData(data, filePath);
|
||||
showToast({
|
||||
message: frappe._('Export Successful'),
|
||||
actionText: frappe._('Open Folder'),
|
||||
action: async () => {
|
||||
await showItemInFolder(filePath);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
@ -19,6 +19,9 @@
|
||||
@setup-canceled="setupCanceled"
|
||||
/>
|
||||
<portal-target name="popovers" multiple></portal-target>
|
||||
<div id="toast-container" class="absolute bottom-0 right-0 mr-6 mb-3">
|
||||
<div id="toast-target" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@ -120,9 +123,9 @@ export default {
|
||||
this.activeScreen = 'DatabaseSelector';
|
||||
},
|
||||
async setupCanceled() {
|
||||
const filePath = config.get('lastSelectedFilePath')
|
||||
await fs.unlink(filePath)
|
||||
this.changeDbFile()
|
||||
const filePath = config.get('lastSelectedFilePath');
|
||||
await fs.unlink(filePath);
|
||||
this.changeDbFile();
|
||||
},
|
||||
},
|
||||
};
|
||||
|
@ -12,11 +12,11 @@ import electron, {
|
||||
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer';
|
||||
import Store from 'electron-store';
|
||||
import { autoUpdater } from 'electron-updater';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib';
|
||||
import { IPC_ACTIONS, IPC_MESSAGES } from './messages';
|
||||
import saveHtmlAsPdf from './saveHtmlAsPdf';
|
||||
import saveReportAsJson from './saveReportAsJson';
|
||||
|
||||
const isDevelopment = process.env.NODE_ENV !== 'production';
|
||||
const isMac = process.platform === 'darwin';
|
||||
@ -149,6 +149,10 @@ ipcMain.on(IPC_MESSAGES.OPEN_EXTERNAL, (event, link) => {
|
||||
shell.openExternal(link);
|
||||
});
|
||||
|
||||
ipcMain.on(IPC_MESSAGES.SHOW_ITEM_IN_FOLDER, (event, filePath) => {
|
||||
return shell.showItemInFolder(filePath);
|
||||
});
|
||||
|
||||
/* ----------------------------------
|
||||
* Register ipcMain function handlers
|
||||
* ----------------------------------*/
|
||||
@ -194,8 +198,8 @@ ipcMain.handle(IPC_ACTIONS.SAVE_HTML_AS_PDF, async (event, html, savePath) => {
|
||||
return await saveHtmlAsPdf(html, savePath);
|
||||
});
|
||||
|
||||
ipcMain.handle(IPC_ACTIONS.SAVE_REPORT_AS_JSON, async (event, data, savePath) => {
|
||||
return await saveReportAsJson(data, savePath);
|
||||
ipcMain.handle(IPC_ACTIONS.SAVE_DATA, async (event, data, savePath) => {
|
||||
return await fs.writeFile(savePath, data);
|
||||
});
|
||||
|
||||
/* ------------------------------
|
||||
|
@ -2,6 +2,7 @@ export const IPC_MESSAGES = {
|
||||
OPEN_MENU: 'open-menu',
|
||||
OPEN_SETTINGS: 'open-settings',
|
||||
OPEN_EXTERNAL: 'open-external',
|
||||
SHOW_ITEM_IN_FOLDER: 'show-item-in-folder',
|
||||
CHECK_FOR_UPDATES: 'check-for-updates',
|
||||
RELOAD_MAIN_WINDOW: 'reload-main-window',
|
||||
RESIZE_MAIN_WINDOW: 'resize-main-window',
|
||||
@ -16,7 +17,7 @@ export const IPC_ACTIONS = {
|
||||
GET_DIALOG_RESPONSE: 'show-message-box',
|
||||
GET_PRIMARY_DISPLAY_SIZE: 'get-primary-display-size',
|
||||
SAVE_HTML_AS_PDF: 'save-html-as-pdf',
|
||||
SAVE_REPORT_AS_JSON: 'save-report-as-json',
|
||||
SAVE_DATA: 'save-data',
|
||||
SHOW_ERROR: 'show-error',
|
||||
};
|
||||
|
||||
|
@ -1,9 +0,0 @@
|
||||
import fs from 'fs';
|
||||
import { shell } from 'electron';
|
||||
|
||||
export default async function makeJSON(data, savePath) {
|
||||
fs.writeFile(savePath, data, (error) => {
|
||||
if (error) throw error;
|
||||
return shell.showItemInFolder(savePath);
|
||||
});
|
||||
}
|
23
src/utils.js
23
src/utils.js
@ -1,8 +1,10 @@
|
||||
import Avatar from '@/components/Avatar';
|
||||
import Toast from '@/components/Toast';
|
||||
import router from '@/router';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import frappe from 'frappejs';
|
||||
import { _ } from 'frappejs/utils';
|
||||
import Vue from 'vue';
|
||||
import { IPC_ACTIONS, IPC_MESSAGES } from './messages';
|
||||
|
||||
export async function showMessageDialog({
|
||||
@ -170,11 +172,15 @@ export function handleErrorWithDialog(e, doc) {
|
||||
}
|
||||
|
||||
export async function makePDF(html, savePath) {
|
||||
ipcRenderer.invoke(IPC_ACTIONS.SAVE_HTML_AS_PDF, html, savePath);
|
||||
await ipcRenderer.invoke(IPC_ACTIONS.SAVE_HTML_AS_PDF, html, savePath);
|
||||
}
|
||||
|
||||
export async function makeJSON(data, savePath) {
|
||||
ipcRenderer.invoke(IPC_ACTIONS.SAVE_REPORT_AS_JSON, data, savePath);
|
||||
export async function saveData(data, savePath) {
|
||||
await ipcRenderer.invoke(IPC_ACTIONS.SAVE_DATA, data, savePath);
|
||||
}
|
||||
|
||||
export async function showItemInFolder(filePath) {
|
||||
await ipcRenderer.send(IPC_MESSAGES.SHOW_ITEM_IN_FOLDER, filePath);
|
||||
}
|
||||
|
||||
export function getActionsForDocument(doc) {
|
||||
@ -322,3 +328,14 @@ export async function getSavePath(name, extention) {
|
||||
|
||||
return { canceled, filePath };
|
||||
}
|
||||
|
||||
export function showToast(props) {
|
||||
new Vue({
|
||||
el: '#toast-target',
|
||||
render(createElement) {
|
||||
return createElement(Toast, { props });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
window.showToast = showToast;
|
||||
|
Loading…
Reference in New Issue
Block a user