Skip to content

Commit

Permalink
fix: Date value parse error. (adempiere#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinBetanc0urt committed Nov 3, 2023
1 parent d52d4c6 commit eed89d6
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 129 deletions.
120 changes: 27 additions & 93 deletions src/components/ADempiere/FieldDefinition/FieldDate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ import { DATE_PLUS_TIME } from '@/utils/ADempiere/references'
import {
MULTIPLE_VALUES_OPERATORS_LIST, RANGE_VALUE_OPERATORS_LIST
} from '@/utils/ADempiere/dataUtils'
import {
SHORCUTS_DATE, SHORCUTS_DATE_RANGE
} from '@/utils/ADempiere/componentUtils'
// Utils and Helper Methods
import { isEmptyValue } from '@/utils/ADempiere/valueUtils.js'
import { getTypeOfValue, isEmptyValue } from '@/utils/ADempiere/valueUtils.js'
import { changeTimeZone } from '@/utils/ADempiere/formatValue/dateFormat'
/**
Expand All @@ -63,86 +66,10 @@ export default {
data() {
return {
pickerOptionsDate: {
shortcuts: [{
text: this.$t('component.date.today'),
onClick(picker) {
picker.$emit('pick', new Date())
}
}, {
text: this.$t('component.date.yesterday'),
onClick(picker) {
const date = new Date()
date.setTime(date.getTime() - 3600 * 1000 * 24)
picker.$emit('pick', date)
}
}, {
text: this.$t('component.date.week'),
onClick(picker) {
const date = new Date()
const monthEndDay = new Date(date.getFullYear(), date.getMonth() + 1, 0)
picker.$emit('pick', monthEndDay)
}
}]
shortcuts: SHORCUTS_DATE
},
pickerOptionsDateRange: {
shortcuts: [{
text: this.$t('component.date.today'),
onClick(picker) {
const currentDay = new Date()
picker.$emit('pick', [currentDay, currentDay])
}
}, {
text: this.$t('component.date.yesterday'),
onClick(picker) {
const start = new Date()
start.setTime(start.getTime() - 3600 * 1000 * 24)
picker.$emit('pick', [start, start])
}
}, {
text: this.$t('component.date.week'),
onClick(picker) {
const start_date = new Date()
start_date.setHours(0, 0, 0, 0)
const end_date = new Date()
const date = null
const currenDate = date ? new Date(date) : new Date()
const first = currenDate.getDate() - currenDate.getDay('monday')
const last = first - 7
start_date.setDate(last)
end_date.setDate(first - 1)
picker.$emit('pick', [start_date, end_date])
}
}, {
text: this.$t('component.date.currentWeek'),
onClick(picker) {
const start_date = new Date()
start_date.setHours(0, 0, 0, 0)
const end_date = new Date()
const date = null
const currenDate = date ? new Date(date) : new Date()
const first = currenDate.getDate() - currenDate.getDay('monday')
const last = first
start_date.setDate(last)
end_date.setDate(first + 6)
picker.$emit('pick', [start_date, end_date])
}
}, {
text: this.$t('component.date.lastMonth'),
onClick(picker) {
const date = new Date()
const monthEndDay = new Date(date.getFullYear(), date.getMonth(), 0)
const monthStartDay = new Date(date.getFullYear(), date.getMonth() - 1, 1)
picker.$emit('pick', [monthStartDay, monthEndDay])
}
}, {
text: this.$t('component.date.currentMonth'),
onClick(picker) {
const date = new Date()
const monthEndDay = new Date(date.getFullYear(), date.getMonth() + 1, 0)
const monthStartDay = new Date(date.getFullYear(), date.getMonth(), 1)
picker.$emit('pick', [monthStartDay, monthEndDay])
}
}]
shortcuts: SHORCUTS_DATE_RANGE
}
}
},
Expand Down Expand Up @@ -313,41 +240,48 @@ export default {
},
methods: {
parseValue(value) {
parseValue(valueToParse) {
let currentValue = valueToParse
// types `decimal` and `date` is a object struct
if ((getTypeOfValue(valueToParse) === 'OBJECT') && !isEmptyValue(valueToParse.type)) {
currentValue = valueToParse.value
}
// not return undefined to v-model
if (isEmptyValue(value)) {
if (isEmptyValue(currentValue)) {
if (this.isMultipleValues) {
return []
}
return null
}
if (this.isMultipleValues) {
if (Array.isArray(value)) {
value = value.map(itemValue => {
if (Array.isArray(currentValue)) {
currentValue = currentValue.map(itemValue => {
if (typeof itemValue === 'object') {
return itemValue.toUTCString()
}
return itemValue
})
} else {
const tempValue = []
if (!isEmptyValue(value)) {
tempValue.push(value)
if (!isEmptyValue(currentValue)) {
tempValue.push(currentValue)
}
value = tempValue
currentValue = tempValue
}
return value
return currentValue
}
// instance date from long value
if (typeof value === 'number') {
value = new Date(value).toUTCString()
if (typeof currentValue === 'number') {
currentValue = new Date(currentValue).toUTCString()
}
// generate range value
if (this.isRenderRange && !this.metadata.inTable) {
let valueTo
let value = currentValue
if (Array.isArray(value)) {
valueTo = value.at(1)
value = value.at(0)
Expand All @@ -358,13 +292,13 @@ export default {
if (isEmptyValue(valueTo)) {
valueTo = undefined
}
value = [value, valueTo]
if (isEmptyValue(value.at(0)) || isEmptyValue(value.at(1))) {
value = []
currentValue = [value, valueTo]
if (isEmptyValue(currentValue.at(0)) || isEmptyValue(currentValue.at(1))) {
currentValue = []
}
}
return value
return currentValue
},
// validate values before send values to store or server
preHandleChange(value) {
Expand Down
36 changes: 19 additions & 17 deletions src/store/modules/ADempiere/sessionContext.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// ADempiere-Vue (Frontend) for ADempiere ERP & CRM Smart Business Solution
// Copyright (C) 2017-Present E.R.P. Consultores y Asociados, C.A.
// Contributor(s): Edwin Betancourt EdwinBetanc0urt@outlook.com www.erpya.com
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/**
* ADempiere-Vue (Frontend) for ADempiere ERP & CRM Smart Business Solution
* Copyright (C) 2018-Present E.R.P. Consultores y Asociados, C.A. www.erpya.com
* Contributor(s): Edwin Betancourt EdwinBetanc0urt@outlook.com https://github.com/EdwinBetanc0urt
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import Vue from 'vue'

Expand All @@ -21,7 +23,7 @@ import { CLIENT, COUNTRY, ORGANIZATION } from '@/utils/ADempiere/constants/syste
import { ACCOUNTING_CONTEXT_PREFIX, GLOBAL_CONTEXT_PREFIX } from '@/utils/ADempiere/contextUtils'

// utils and helper methods
import { isEmptyValue, typeValue } from '@/utils/ADempiere/valueUtils.js'
import { isEmptyValue, getTypeOfValue } from '@/utils/ADempiere/valueUtils.js'

const sessionContext = {
state: {
Expand Down Expand Up @@ -80,7 +82,7 @@ const sessionContext = {
containerUuid,
values
}) {
const typeOfValue = typeValue(values)
const typeOfValue = getTypeOfValue(values)

let actionToDispatch = 'setMultiplePreferenceObject'
if (typeOfValue === 'MAP') {
Expand Down
91 changes: 91 additions & 0 deletions src/utils/ADempiere/componentUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import lang from '@/lang'
import router from '@/router'
import store from '@/store'

Expand Down Expand Up @@ -67,3 +68,93 @@ export const closeTagView = function(currentRoute) {
}, () => {})
}
}

export const SHORCUTS_DATE = [
{
text: lang.t('component.date.today'),
onClick(picker) {
picker.$emit('pick', new Date())
}
},
{
text: lang.t('component.date.yesterday'),
onClick(picker) {
const date = new Date()
date.setTime(date.getTime() - 3600 * 1000 * 24)
picker.$emit('pick', date)
}
},
{
text: lang.t('component.date.week'),
onClick(picker) {
const date = new Date()
const monthEndDay = new Date(date.getFullYear(), date.getMonth() + 1, 0)
picker.$emit('pick', monthEndDay)
}
}
]

export const SHORCUTS_DATE_RANGE = [
{
text: lang.t('component.date.today'),
onClick(picker) {
const currentDay = new Date()
picker.$emit('pick', [currentDay, currentDay])
}
},
{
text: lang.t('component.date.yesterday'),
onClick(picker) {
const start = new Date()
start.setTime(start.getTime() - 3600 * 1000 * 24)
picker.$emit('pick', [start, start])
}
},
{
text: lang.t('component.date.week'),
onClick(picker) {
const start_date = new Date()
start_date.setHours(0, 0, 0, 0)
const end_date = new Date()
const date = null
const currenDate = date ? new Date(date) : new Date()
const first = currenDate.getDate() - currenDate.getDay('monday')
const last = first - 7
start_date.setDate(last)
end_date.setDate(first - 1)
picker.$emit('pick', [start_date, end_date])
}
}, {
text: lang.t('component.date.currentWeek'),
onClick(picker) {
const start_date = new Date()
start_date.setHours(0, 0, 0, 0)
const end_date = new Date()
const date = null
const currenDate = date ? new Date(date) : new Date()
const first = currenDate.getDate() - currenDate.getDay('monday')
const last = first
start_date.setDate(last)
end_date.setDate(first + 6)
picker.$emit('pick', [start_date, end_date])
}
},
{
text: lang.t('component.date.lastMonth'),
onClick(picker) {
const date = new Date()
const monthEndDay = new Date(date.getFullYear(), date.getMonth(), 0)
const monthStartDay = new Date(date.getFullYear(), date.getMonth() - 1, 1)
picker.$emit('pick', [monthStartDay, monthEndDay])
}
},
{
text: lang.t('component.date.currentMonth'),
onClick(picker) {
const date = new Date()
const monthEndDay = new Date(date.getFullYear(), date.getMonth() + 1, 0)
const monthStartDay = new Date(date.getFullYear(), date.getMonth(), 1)
picker.$emit('pick', [monthStartDay, monthEndDay])
}
}
]
4 changes: 2 additions & 2 deletions src/utils/ADempiere/formatValue/dateFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import store from '@/store'
import moment from 'moment'

// utils and helper methods
import { isEmptyValue, typeValue } from '@/utils/ADempiere/valueUtils.js'
import { isEmptyValue, getTypeOfValue } from '@/utils/ADempiere/valueUtils.js'
import { zeroPad } from '@/utils/ADempiere/formatValue/numberFormat.js'

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ export function formatDate({ value, isTime = false, isDate = false, format }) {
if (isEmptyValue(value)) {
return undefined
}
if (typeValue(value) === 'DATE') {
if (getTypeOfValue(value) === 'DATE') {
value = value.getTime()
}

Expand Down
6 changes: 3 additions & 3 deletions src/utils/ADempiere/formatValue/stringFormat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* ADempiere-Vue (Frontend) for ADempiere ERP & CRM Smart Business Solution
* Copyright (C) 2017-Present E.R.P. Consultores y Asociados, C.A. www.erpya.com
* Copyright (C) 2018-Present E.R.P. Consultores y Asociados, C.A. www.erpya.com
* Contributor(s): Edwin Betancourt EdwinBetanc0urt@outlook.com https://github.com/EdwinBetanc0urt
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -17,7 +17,7 @@
*/

// Utils and Helper Methods
import { isEmptyValue, typeValue } from '@/utils/ADempiere/valueUtils'
import { isEmptyValue, getTypeOfValue } from '@/utils/ADempiere/valueUtils'

/**
* Capitalize value
Expand Down Expand Up @@ -55,7 +55,7 @@ export function removeQuotationMark(stringValue) {
if (isEmptyValue(stringValue)) {
return stringValue
}
if (typeValue(stringValue) !== 'STRING') {
if (getTypeOfValue(stringValue) !== 'STRING') {
return stringValue
}

Expand Down
Loading

0 comments on commit eed89d6

Please sign in to comment.