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:
parent
afa261c592
commit
b4fbbd9bb7
@ -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
64
reports/commonExporter.js
Normal 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);
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
Loading…
Reference in New Issue
Block a user