2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00

refactor: move gstSavePath to utils

This commit is contained in:
18alantom 2021-12-21 13:24:13 +05:30 committed by Alan
parent ca8739f8c0
commit afa261c592
3 changed files with 24 additions and 45 deletions

View File

@ -1,9 +1,8 @@
import { IPC_ACTIONS } from '@/messages';
import { makeJSON, showMessageDialog } from '@/utils';
import { ipcRenderer } from 'electron';
import frappe from 'frappejs';
import { sleep, _ } from 'frappejs/utils';
import { DateTime } from 'luxon';
import { getSavePath } from '../src/utils';
/**
* GST is a map which gives a final rate for any given gst item
@ -72,8 +71,9 @@ export async function generateGstr1Json(getReportData) {
rows,
filters: { transferType, toDate },
} = getReportData();
const savePath = await getSavePath('gstr-1');
if (!savePath) return;
const { filePath, canceled } = await getSavePath('gstr-1', 'json');
if (canceled || !filePath) return;
const gstData = {
version: 'GST3.0.4',
@ -96,7 +96,7 @@ export async function generateGstr1Json(getReportData) {
await sleep(1);
const jsonData = JSON.stringify(gstData);
makeJSON(jsonData, savePath);
makeJSON(jsonData, filePath);
}
async function generateB2bData(invoices) {
@ -161,23 +161,3 @@ async function generateB2clData(invoices) {
async function generateB2csData(invoices) {
return [];
}
async function getSavePath(name) {
const options = {
title: _('Select folder'),
defaultPath: `${name}.json`,
};
let { filePath } = await ipcRenderer.invoke(
IPC_ACTIONS.GET_SAVE_FILEPATH,
options
);
if (filePath) {
if (!filePath.endsWith('.json')) {
filePath = filePath + '.json';
}
}
return filePath;
}

View File

@ -1,36 +1,19 @@
import config from '@/config';
import { ipcRenderer } from 'electron';
import { _ } from 'frappejs';
import SQLiteDatabase from 'frappejs/backends/sqlite';
import fs from 'fs';
import models from '../models';
import regionalModelUpdates from '../models/regionalModelUpdates';
import postStart from '../server/postStart';
import { DB_CONN_FAILURE, IPC_ACTIONS } from './messages';
import { DB_CONN_FAILURE } from './messages';
import migrate from './migrate';
import { getSavePath } from './utils';
export async function createNewDatabase() {
const options = {
title: _('Select folder'),
defaultPath: 'books.db',
};
let { canceled, filePath } = await ipcRenderer.invoke(
IPC_ACTIONS.GET_SAVE_FILEPATH,
options
);
const { canceled, filePath } = await getSavePath('books', 'db');
if (canceled || filePath.length === 0) {
return '';
}
if (!filePath.endsWith('.db')) {
showMessageDialog({
message: "Please select a filename ending with '.db'.",
});
return '';
}
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}

View File

@ -306,3 +306,19 @@ export function fuzzyMatch(keyword, candidate) {
export function openSettings(tab) {
routeTo({ path: '/settings', query: { tab } });
}
export async function getSavePath(name, extention) {
let { canceled, filePath } = await ipcRenderer.invoke(
IPC_ACTIONS.GET_SAVE_FILEPATH,
{
title: _('Select Folder'),
defaultPath: `${name}.${extention}`,
}
);
if (filePath && !filePath.endsWith(extention)) {
filePath = filePath + extention;
}
return { canceled, filePath };
}