fix formula condition

This commit is contained in:
Pete Matsyburka
2025-09-25 13:12:36 +03:00
parent 1b1cf36839
commit b898708306
2 changed files with 45 additions and 5 deletions
@@ -92,10 +92,20 @@ export default {
type: Object,
required: true
},
readonlyValues: {
type: Object,
required: false,
default: () => ({})
},
values: {
type: Object,
required: true
},
fields: {
type: Array,
required: false,
default: () => []
},
submitterSlug: {
type: String,
required: true
@@ -109,6 +119,13 @@ export default {
}
},
computed: {
fieldsUuidIndex () {
return this.fields.reduce((acc, field) => {
acc[field.uuid] = field
return acc
}, {})
},
queryParams () {
return new URLSearchParams(window.location.search)
},
@@ -182,12 +199,23 @@ export default {
},
methods: {
calculateFormula () {
const transformedFormula = this.field.preferences.formula.replace(/{{(.*?)}}/g, (match, uuid) => {
return this.values[uuid] || 0.0
const transformedFormula = this.normalizeFormula(this.field.preferences.formula).replace(/{{(.*?)}}/g, (match, uuid) => {
return this.readonlyValues[uuid] || this.values[uuid] || 0.0
})
return this.math.evaluate(transformedFormula.toLowerCase())
},
normalizeFormula (formula, depth = 0) {
if (depth > 10) return formula
return formula.replace(/{{(.*?)}}/g, (match, uuid) => {
if (this.fieldsUuidIndex[uuid]) {
return `(${this.normalizeFormula(this.fieldsUuidIndex[uuid].preferences.formula, depth + 1)})`
} else {
return match
}
})
},
async submit () {
if (this.sessionId) {
return fetch(this.baseUrl + '/api/stripe_payments/' + this.sessionId, {