2022-01-20 20:57:29 +00:00
|
|
|
import frappe from 'frappe';
|
2022-03-09 10:48:26 +00:00
|
|
|
import telemetry from '../src/telemetry/telemetry';
|
|
|
|
import { Verb } from '../src/telemetry/types';
|
2022-01-19 06:43:42 +00:00
|
|
|
import { getSavePath, saveData, showExportInFolder } from '../src/utils';
|
2021-12-21 08:37:06 +00:00
|
|
|
|
|
|
|
function templateToInnerText(innerHTML) {
|
|
|
|
const temp = document.createElement('template');
|
|
|
|
temp.innerHTML = innerHTML.trim();
|
|
|
|
return temp.content.firstChild.innerText;
|
|
|
|
}
|
|
|
|
|
2021-12-21 09:05:24 +00:00
|
|
|
function deObjectify(value) {
|
|
|
|
if (typeof value !== 'object') return value;
|
|
|
|
if (value === null) return '';
|
|
|
|
|
|
|
|
const innerHTML = value.template;
|
|
|
|
if (!innerHTML) return '';
|
|
|
|
return templateToInnerText(innerHTML);
|
|
|
|
}
|
|
|
|
|
2021-12-21 08:37:06 +00:00
|
|
|
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 '';
|
2021-12-21 09:05:24 +00:00
|
|
|
return csvFormat(deObjectify(value));
|
2021-12-21 08:37:06 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-01-10 13:24:32 +00:00
|
|
|
export async function exportCsv(rows, columns, filePath) {
|
2021-12-21 08:37:06 +00:00
|
|
|
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(',')),
|
|
|
|
];
|
2021-12-21 12:33:30 +00:00
|
|
|
|
|
|
|
saveExportData(csvRows.join('\n'), filePath);
|
2021-12-21 08:37:06 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 09:05:24 +00:00
|
|
|
async function exportJson(rows, columns, filePath, filters, reportName) {
|
|
|
|
const exportObject = {};
|
|
|
|
const fieldnames = columns.map(({ fieldname }) => fieldname);
|
|
|
|
|
|
|
|
exportObject.columns = columns.map(({ fieldname, label }) => ({
|
|
|
|
fieldname,
|
|
|
|
label,
|
|
|
|
}));
|
|
|
|
|
|
|
|
exportObject.rows = rows.map((row) =>
|
|
|
|
fieldnames.reduce((acc, f) => {
|
|
|
|
acc[f] = deObjectify(row[f]);
|
|
|
|
return acc;
|
|
|
|
}, {})
|
|
|
|
);
|
2021-12-21 08:37:06 +00:00
|
|
|
|
2021-12-21 09:05:24 +00:00
|
|
|
exportObject.filters = Object.keys(filters)
|
|
|
|
.filter((name) => filters[name] !== null && filters[name] !== undefined)
|
|
|
|
.reduce((acc, name) => {
|
|
|
|
acc[name] = filters[name];
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
exportObject.timestamp = new Date().toISOString();
|
|
|
|
exportObject.reportName = reportName;
|
|
|
|
exportObject.softwareName = 'Frappe Books';
|
|
|
|
exportObject.softwareVersion = frappe.store.appVersion;
|
|
|
|
|
2021-12-21 12:33:30 +00:00
|
|
|
await saveExportData(JSON.stringify(exportObject), filePath);
|
2021-12-21 09:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function exportReport(extention, reportName, getReportData) {
|
|
|
|
const { rows, columns, filters } = getReportData();
|
|
|
|
|
|
|
|
const { filePath, canceled } = await getSavePath(reportName, extention);
|
2021-12-21 08:37:06 +00:00
|
|
|
if (canceled || !filePath) return;
|
2021-12-21 09:05:24 +00:00
|
|
|
|
|
|
|
switch (extention) {
|
|
|
|
case 'csv':
|
|
|
|
await exportCsv(rows, columns, filePath);
|
2022-03-09 10:48:26 +00:00
|
|
|
break;
|
2021-12-21 09:05:24 +00:00
|
|
|
case 'json':
|
|
|
|
await exportJson(rows, columns, filePath, filters, reportName);
|
2022-03-09 10:48:26 +00:00
|
|
|
break;
|
2021-12-21 09:05:24 +00:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2022-03-09 10:48:26 +00:00
|
|
|
|
|
|
|
telemetry.log(Verb.Exported, reportName, { extention });
|
2021-12-21 08:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function getCommonExportActions(reportName) {
|
2021-12-21 09:05:24 +00:00
|
|
|
return ['csv', 'json'].map((ext) => ({
|
|
|
|
group: 'Export',
|
|
|
|
label: ext.toUpperCase(),
|
|
|
|
type: 'primary',
|
|
|
|
action: async (getReportData) =>
|
|
|
|
await exportReport(ext, reportName, getReportData),
|
|
|
|
}));
|
2021-12-21 08:37:06 +00:00
|
|
|
}
|
2021-12-21 12:33:30 +00:00
|
|
|
|
|
|
|
export async function saveExportData(data, filePath) {
|
|
|
|
await saveData(data, filePath);
|
2022-02-09 06:37:33 +00:00
|
|
|
showExportInFolder(frappe.t`Export Successful`, filePath);
|
2021-12-21 12:33:30 +00:00
|
|
|
}
|