From ce13f82ec7eeca970790cb1852589fa02f9ff85d Mon Sep 17 00:00:00 2001 From: Edwin Betancourt Date: Fri, 12 Mar 2021 18:17:44 -0400 Subject: [PATCH] fix: Export record's to zip, xls, json, xml, txt (#666) * fix: Export record's to zip * Add Zip export option in the context menu of the table (#8) Co-authored-by: Elsio Sanchez Co-authored-by: EdwinBetanc0urt Co-authored-by: Elsio Sanchez <45974454+elsiosanchez@users.noreply.github.com> Co-authored-by: Elsio Sanchez --- .../ADempiere/ContextMenu/contextMenuMixin.js | 10 +++- .../DataTable/menu/menuTableMixin.js | 34 ++++++++--- .../DataTable/menu/tableContextMenu.vue | 7 +++ src/utils/ADempiere/exportUtil.js | 58 +++++++++++-------- src/utils/ADempiere/valueFormat.js | 13 +++++ 5 files changed, 90 insertions(+), 32 deletions(-) diff --git a/src/components/ADempiere/ContextMenu/contextMenuMixin.js b/src/components/ADempiere/ContextMenu/contextMenuMixin.js index 492e7d9a..b6acf5ac 100644 --- a/src/components/ADempiere/ContextMenu/contextMenuMixin.js +++ b/src/components/ADempiere/ContextMenu/contextMenuMixin.js @@ -1,6 +1,6 @@ import { showNotification } from '@/utils/ADempiere/notification.js' import ItemsRelations from './itemsRelations' -import { convertFieldsListToShareLink, recursiveTreeSearch } from '@/utils/ADempiere/valueUtils.js' +import { clientDateTime, convertFieldsListToShareLink, recursiveTreeSearch } from '@/utils/ADempiere/valueUtils.js' import { supportedTypes, exportFileFromJson } from '@/utils/ADempiere/exportUtil.js' import ROUTES from '@/utils/ADempiere/constants/zoomWindow' import relationsMixin from './relationsMixin.js' @@ -342,11 +342,17 @@ export default { // TODO: Check usage as the selection is exported with the table menu list = this.getDataSelection } + + let title = this.metadataMenu.name + if (this.isEmptyValue(title)) { + title = this.$route.meta.title + } + const data = this.formatJson(filterVal, list) exportFileFromJson({ header: tHeader, data, - filename: '', + fileName: `${title} ${clientDateTime()}`, exportType: fotmatToExport }) }, diff --git a/src/components/ADempiere/DataTable/menu/menuTableMixin.js b/src/components/ADempiere/DataTable/menu/menuTableMixin.js index 06bec655..914e2820 100644 --- a/src/components/ADempiere/DataTable/menu/menuTableMixin.js +++ b/src/components/ADempiere/DataTable/menu/menuTableMixin.js @@ -1,5 +1,5 @@ -import { supportedTypes, exportFileFromJson, exportFileZip } from '@/utils/ADempiere/exportUtil.js' -import { recursiveTreeSearch } from '@/utils/ADempiere/valueUtils.js' +import { supportedTypes, exportFileFromJson, exportZipFile } from '@/utils/ADempiere/exportUtil.js' +import { clientDateTime, recursiveTreeSearch } from '@/utils/ADempiere/valueUtils.js' import { FIELDS_QUANTITY } from '@/utils/ADempiere/references' import TableMixin from '@/components/ADempiere/DataTable/mixin/tableMixin.js' @@ -197,28 +197,48 @@ export default { list = this.getDataSelection } + let title = this.panelMetadata.name + if (this.isEmptyValue(title)) { + title = this.$route.meta.title + } + const data = this.formatJson(filterVal, list) exportFileFromJson({ header, data, - filename: '', + fileName: `${title} ${clientDateTime()}`, exportType: formatToExport }) + this.closeMenu() }, - exporZipRecordTable() { + /** + * Export records as .txt into compressed .zip file + */ + exporZipRecordTable({ + recordContexMenu = false + }) { const header = this.getterFieldsListHeader const filterVal = this.getterFieldsListValue let list = this.getDataSelection if (this.getDataSelection.length <= 0) { list = this.recordsData } + if (recordContexMenu) { + list = [this.currentRow] + } const data = this.formatJson(filterVal, list) - exportFileZip({ + + let title = this.panelMetadata.name + if (this.isEmptyValue(title)) { + title = this.$route.meta.title + } + + exportZipFile({ header, data, - title: this.$route.meta.title, - exportType: 'zip' + txtName: title, + zipName: `${title} ${clientDateTime()}` }) }, formatJson(filterVal, jsonData) { diff --git a/src/components/ADempiere/DataTable/menu/tableContextMenu.vue b/src/components/ADempiere/DataTable/menu/tableContextMenu.vue index 8e8c2f5a..16c7abfa 100644 --- a/src/components/ADempiere/DataTable/menu/tableContextMenu.vue +++ b/src/components/ADempiere/DataTable/menu/tableContextMenu.vue @@ -18,6 +18,13 @@ {{ format }} + + {{ $t('table.dataTable.exportZip') }} + { - Object.keys(dataJson).forEach(key => { - if (typeof dataJson[key] === 'boolean') { - dataJson[key] = dataJson[key] - ? language.t('components.switchActiveText') - : language.t('components.switchInactiveText') + const jsonData = data.map(row => { + Object.keys(row).forEach(column => { + if (typeof row[column] === 'boolean') { + row[column] = convertBooleanToTranslationLang(row[column]) } }) - return dataJson + + return row }) + export_json_to_excel({ - header: header, - data: Json, - filename: '', + header, + data: jsonData, + filename: fileName, bookType: exportType }) } @@ -58,27 +62,35 @@ export function exportFileFromJson({ * @autor Edwin Betancourt * @param {array} header * @param {array} data - * @param {string} title + * @param {string} txtName .txt text file name + * @param {string} zipName .zip compressed file name */ -export function exportFileZip({ +export function exportZipFile({ header, data, - title + txtName = '', + zipName = '' }) { - const Json = data.map(dataJson => { - Object.keys(dataJson).forEach(key => { - if (typeof dataJson[key] === 'boolean') { - dataJson[key] = dataJson[key] - ? language.t('components.switchActiveText') - : language.t('components.switchInactiveText') + const jsonData = data.map(row => { + Object.keys(row).forEach(column => { + if (typeof row[column] === 'boolean') { + row[column] = convertBooleanToTranslationLang(row[column]) } }) - return dataJson + return row }) + if (isEmptyValue(zipName)) { + zipName = txtName + } + if (isEmptyValue(txtName)) { + txtName = zipName + } + export_txt_to_zip( header, - Json, - title + jsonData, + txtName, + zipName ) } diff --git a/src/utils/ADempiere/valueFormat.js b/src/utils/ADempiere/valueFormat.js index 70e72030..030859b0 100644 --- a/src/utils/ADempiere/valueFormat.js +++ b/src/utils/ADempiere/valueFormat.js @@ -1,6 +1,7 @@ // A util class for handle format for time, date and others values to beused to display information // Note that this file use moment library for a easy conversion import moment from 'moment' +import language from '@/lang' import { isEmptyValue } from '@/utils/ADempiere/valueUtils.js' import store from '@/store' import { DATE, DATE_PLUS_TIME, TIME, AMOUNT, COSTS_PLUS_PRICES, NUMBER, QUANTITY } from '@/utils/ADempiere/references.js' @@ -27,6 +28,18 @@ export const convertBooleanToString = (booleanValue) => { return 'N' } +/** + * Convert boolean value to current translation language + * @param {boolean} booleanValue + * @returns {string} true => 'Yes' or 'Si', false => 'Not' or 'No' + */ +export const convertBooleanToTranslationLang = (booleanValue) => { + if (booleanValue || booleanValue === 'true') { + return language.t('components.switchActiveText') + } + return language.t('components.switchInactiveText') +} + /** * Convert a object to array pairs * @param {object} object, object to convert