Skip to content

Commit

Permalink
fix: Export record's to zip, xls, json, xml, txt (#666)
Browse files Browse the repository at this point in the history
* fix: Export record's to zip

* Add Zip export option in the context menu of the table (#8)

Co-authored-by: Elsio Sanchez <elsiosanche@gmail.com>

Co-authored-by: EdwinBetanc0urt <EdwinBetanco0urt@outlook.com>
Co-authored-by: Elsio Sanchez <45974454+elsiosanchez@users.noreply.github.com>
Co-authored-by: Elsio Sanchez <elsiosanche@gmail.com>
  • Loading branch information
4 people authored Mar 12, 2021
1 parent 7bf5bcc commit ce13f82
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 32 deletions.
10 changes: 8 additions & 2 deletions src/components/ADempiere/ContextMenu/contextMenuMixin.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
})
},
Expand Down
34 changes: 27 additions & 7 deletions src/components/ADempiere/DataTable/menu/menuTableMixin.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions src/components/ADempiere/DataTable/menu/tableContextMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
{{ format }}
</el-menu-item>
</el-submenu>
<el-menu-item
@click="exporZipRecordTable({
recordContexMenu: true
})"
>
{{ $t('table.dataTable.exportZip') }}
</el-menu-item>
<el-menu-item
v-if="panelType === 'window'"
index="delete"
Expand Down
58 changes: 35 additions & 23 deletions src/utils/ADempiere/exportUtil.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { export_json_to_excel } from '@/vendor/Export2Excel'
import { export_txt_to_zip } from '@/vendor/Export2Zip'
import language from '@/lang'
import { convertBooleanToTranslationLang } from '@/utils/ADempiere/valueFormat.js'
import { isEmptyValue } from '@/utils/ADempiere/valueUtils.js'

export const reportFormatsList = [
'ps',
Expand Down Expand Up @@ -29,26 +31,28 @@ export const supportedTypes = {
* @param {array} header
* @param {array} data
* @param {string} exportType, supportedTypes array
* @param {string} fileName .xlsx file name
*/
export function exportFileFromJson({
header,
data,
exportType
exportType,
fileName = ''
}) {
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
})

export_json_to_excel({
header: header,
data: Json,
filename: '',
header,
data: jsonData,
filename: fileName,
bookType: exportType
})
}
Expand All @@ -58,27 +62,35 @@ export function exportFileFromJson({
* @autor Edwin Betancourt <EdwinBetanc0urt@outlook.com>
* @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
)
}
13 changes: 13 additions & 0 deletions src/utils/ADempiere/valueFormat.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down

0 comments on commit ce13f82

Please sign in to comment.