2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 03:19:01 +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:
18alantom 2021-12-21 18:03:30 +05:30 committed by Alan
parent 04fb48faed
commit f21aee6664
7 changed files with 59 additions and 42 deletions

View File

@ -1,14 +1,10 @@
import { makeJSON, showMessageDialog } from '@/utils'; import { showMessageDialog } from '@/utils';
import frappe from 'frappejs'; import frappe from 'frappejs';
import { sleep, _ } from 'frappejs/utils'; import { _ } from 'frappejs/utils';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import { saveExportData } from '../reports/commonExporter';
import { getSavePath } from '../src/utils'; 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 = { const GST = {
'GST-0': 0, 'GST-0': 0,
'GST-0.25': 0.25, 'GST-0.25': 0.25,
@ -28,10 +24,6 @@ const GST = {
'IGST-28': 28, 'IGST-28': 28,
}; };
/**
* CSGST is a map which return the tax rate component for state or central
* eg: GST-12 = 6
*/
const CSGST = { const CSGST = {
'GST-0': 0, 'GST-0': 0,
'GST-0.25': 0.125, 'GST-0.25': 0.125,
@ -43,10 +35,6 @@ const CSGST = {
'GST-28': 14, 'GST-28': 14,
}; };
/**
* IGST is a map which return the tax rate for the igst item
* eg: IGST-18 = 18
*/
const IGST = { const IGST = {
'IGST-0.25': 0.25, 'IGST-0.25': 0.25,
'IGST-3': 3, 'IGST-3': 3,
@ -85,7 +73,6 @@ export async function generateGstr1Json(getReportData) {
fp: DateTime.fromISO(toDate).toFormat('MMyyyy'), fp: DateTime.fromISO(toDate).toFormat('MMyyyy'),
}; };
// based condition we need to triggered different methods
if (transferType === 'B2B') { if (transferType === 'B2B') {
gstData.b2b = await generateB2bData(rows); gstData.b2b = await generateB2bData(rows);
} else if (transferType === 'B2CL') { } else if (transferType === 'B2CL') {
@ -94,16 +81,14 @@ export async function generateGstr1Json(getReportData) {
gstData.b2cs = await generateB2csData(rows); gstData.b2cs = await generateB2csData(rows);
} }
await sleep(1);
const jsonData = JSON.stringify(gstData); const jsonData = JSON.stringify(gstData);
makeJSON(jsonData, filePath); await saveExportData(jsonData, filePath);
} }
async function generateB2bData(invoices) { async function generateB2bData(invoices) {
const b2b = []; const b2b = [];
invoices.forEach(async (row) => { invoices.forEach(async (row) => {
// it's must for the customer to have a gstin, if not it should not be here
const customer = { const customer = {
ctin: row.gstin, ctin: row.gstin,
inv: [], inv: [],

View File

@ -1,6 +1,10 @@
import frappe from 'frappejs'; import frappe from 'frappejs';
import fs from 'fs/promises'; import {
import { getSavePath } from '../src/utils'; getSavePath,
saveData,
showItemInFolder,
showToast,
} from '../src/utils';
function templateToInnerText(innerHTML) { function templateToInnerText(innerHTML) {
const temp = document.createElement('template'); const temp = document.createElement('template');
@ -37,7 +41,8 @@ async function exportCsv(rows, columns, filePath) {
labels.join(','), labels.join(','),
...rows.map((row) => fieldnames.map((f) => csvFormat(row[f])).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) { 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.softwareName = 'Frappe Books';
exportObject.softwareVersion = frappe.store.appVersion; exportObject.softwareVersion = frappe.store.appVersion;
await fs.writeFile(filePath, JSON.stringify(exportObject)); await saveExportData(JSON.stringify(exportObject), filePath);
} }
async function exportReport(extention, reportName, getReportData) { async function exportReport(extention, reportName, getReportData) {
@ -98,3 +103,14 @@ export default function getCommonExportActions(reportName) {
await exportReport(ext, reportName, getReportData), 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);
},
});
}

View File

@ -19,6 +19,9 @@
@setup-canceled="setupCanceled" @setup-canceled="setupCanceled"
/> />
<portal-target name="popovers" multiple></portal-target> <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> </div>
</template> </template>
@ -120,9 +123,9 @@ export default {
this.activeScreen = 'DatabaseSelector'; this.activeScreen = 'DatabaseSelector';
}, },
async setupCanceled() { async setupCanceled() {
const filePath = config.get('lastSelectedFilePath') const filePath = config.get('lastSelectedFilePath');
await fs.unlink(filePath) await fs.unlink(filePath);
this.changeDbFile() this.changeDbFile();
}, },
}, },
}; };

View File

@ -12,11 +12,11 @@ import electron, {
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'; import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer';
import Store from 'electron-store'; import Store from 'electron-store';
import { autoUpdater } from 'electron-updater'; import { autoUpdater } from 'electron-updater';
import fs from 'fs/promises';
import path from 'path'; import path from 'path';
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'; import { createProtocol } from 'vue-cli-plugin-electron-builder/lib';
import { IPC_ACTIONS, IPC_MESSAGES } from './messages'; import { IPC_ACTIONS, IPC_MESSAGES } from './messages';
import saveHtmlAsPdf from './saveHtmlAsPdf'; import saveHtmlAsPdf from './saveHtmlAsPdf';
import saveReportAsJson from './saveReportAsJson';
const isDevelopment = process.env.NODE_ENV !== 'production'; const isDevelopment = process.env.NODE_ENV !== 'production';
const isMac = process.platform === 'darwin'; const isMac = process.platform === 'darwin';
@ -149,6 +149,10 @@ ipcMain.on(IPC_MESSAGES.OPEN_EXTERNAL, (event, link) => {
shell.openExternal(link); shell.openExternal(link);
}); });
ipcMain.on(IPC_MESSAGES.SHOW_ITEM_IN_FOLDER, (event, filePath) => {
return shell.showItemInFolder(filePath);
});
/* ---------------------------------- /* ----------------------------------
* Register ipcMain function handlers * 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); return await saveHtmlAsPdf(html, savePath);
}); });
ipcMain.handle(IPC_ACTIONS.SAVE_REPORT_AS_JSON, async (event, data, savePath) => { ipcMain.handle(IPC_ACTIONS.SAVE_DATA, async (event, data, savePath) => {
return await saveReportAsJson(data, savePath); return await fs.writeFile(savePath, data);
}); });
/* ------------------------------ /* ------------------------------

View File

@ -2,6 +2,7 @@ export const IPC_MESSAGES = {
OPEN_MENU: 'open-menu', OPEN_MENU: 'open-menu',
OPEN_SETTINGS: 'open-settings', OPEN_SETTINGS: 'open-settings',
OPEN_EXTERNAL: 'open-external', OPEN_EXTERNAL: 'open-external',
SHOW_ITEM_IN_FOLDER: 'show-item-in-folder',
CHECK_FOR_UPDATES: 'check-for-updates', CHECK_FOR_UPDATES: 'check-for-updates',
RELOAD_MAIN_WINDOW: 'reload-main-window', RELOAD_MAIN_WINDOW: 'reload-main-window',
RESIZE_MAIN_WINDOW: 'resize-main-window', RESIZE_MAIN_WINDOW: 'resize-main-window',
@ -16,7 +17,7 @@ export const IPC_ACTIONS = {
GET_DIALOG_RESPONSE: 'show-message-box', GET_DIALOG_RESPONSE: 'show-message-box',
GET_PRIMARY_DISPLAY_SIZE: 'get-primary-display-size', GET_PRIMARY_DISPLAY_SIZE: 'get-primary-display-size',
SAVE_HTML_AS_PDF: 'save-html-as-pdf', SAVE_HTML_AS_PDF: 'save-html-as-pdf',
SAVE_REPORT_AS_JSON: 'save-report-as-json', SAVE_DATA: 'save-data',
SHOW_ERROR: 'show-error', SHOW_ERROR: 'show-error',
}; };

View File

@ -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);
});
}

View File

@ -1,8 +1,10 @@
import Avatar from '@/components/Avatar'; import Avatar from '@/components/Avatar';
import Toast from '@/components/Toast';
import router from '@/router'; import router from '@/router';
import { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron';
import frappe from 'frappejs'; import frappe from 'frappejs';
import { _ } from 'frappejs/utils'; import { _ } from 'frappejs/utils';
import Vue from 'vue';
import { IPC_ACTIONS, IPC_MESSAGES } from './messages'; import { IPC_ACTIONS, IPC_MESSAGES } from './messages';
export async function showMessageDialog({ export async function showMessageDialog({
@ -170,11 +172,15 @@ export function handleErrorWithDialog(e, doc) {
} }
export async function makePDF(html, savePath) { 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) { export async function saveData(data, savePath) {
ipcRenderer.invoke(IPC_ACTIONS.SAVE_REPORT_AS_JSON, 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) { export function getActionsForDocument(doc) {
@ -322,3 +328,14 @@ export async function getSavePath(name, extention) {
return { canceled, filePath }; return { canceled, filePath };
} }
export function showToast(props) {
new Vue({
el: '#toast-target',
render(createElement) {
return createElement(Toast, { props });
},
});
}
window.showToast = showToast;