2
0
mirror of https://github.com/frappe/books.git synced 2024-11-09 23:30:56 +00:00

feat: add Export > JSON for General Ledger

This commit is contained in:
18alantom 2021-12-21 14:07:06 +05:30 committed by Alan
parent afa261c592
commit b4fbbd9bb7
2 changed files with 66 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import { partyWithAvatar } from '@/utils';
import getCommonExportActions from '../commonExporter';
let title = 'General Ledger';
@ -67,7 +68,7 @@ const viewConfig = {
},
],
method: 'general-ledger',
actions: [],
actions: getCommonExportActions('general-ledger'),
getColumns() {
return [
{

64
reports/commonExporter.js Normal file
View File

@ -0,0 +1,64 @@
import fs from 'fs/promises';
import { getSavePath } from '../src/utils';
function templateToInnerText(innerHTML) {
const temp = document.createElement('template');
temp.innerHTML = innerHTML.trim();
return temp.content.firstChild.innerText;
}
function csvFormat(value) {
if (typeof value === 'string') {
return `"${value}"`;
} else if (value === null) {
return '';
} else if (typeof value === 'object') {
const innerHTML = value.template;
if (!innerHTML) return '';
return csvFormat(templateToInnerText(innerHTML));
}
return value;
}
async function exportCsv(reportName, getReportData) {
const { rows, columns } = getReportData();
const { filePath, canceled } = await getSavePath(reportName, 'csv');
if (canceled || !filePath) return;
const fieldnames = columns.map(({ fieldname }) => fieldname);
const labels = columns.map(({ label }) => csvFormat(label));
const csvRows = [
labels.join(','),
...rows.map((row) => fieldnames.map((f) => csvFormat(row[f])).join(',')),
];
await fs.writeFile(filePath, csvRows.join('\n'));
}
async function exportJson(reportName, getReportData) {
const { rows, columns } = getReportData();
const { filePath, canceled } = await getSavePath(reportName, 'json');
if (canceled || !filePath) return;
}
export default function getCommonExportActions(reportName) {
return [
{
group: 'Export',
label: 'CSV',
type: 'primary',
action: async (getReportData) => {
await exportCsv(reportName, getReportData);
},
},
{
group: 'Export',
label: 'JSON',
type: 'primary',
action: async (getReportData) => {
await exportJson(reportName, getReportData);
},
},
];
}