From b898708306627720eb3c1feddebdeab1de6874b1 Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Thu, 25 Sep 2025 13:12:36 +0300 Subject: [PATCH] fix formula condition --- app/javascript/submission_form/form.vue | 18 +++++++++-- .../submission_form/payment_step.vue | 32 +++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/app/javascript/submission_form/form.vue b/app/javascript/submission_form/form.vue index 761ad0f9..7a825141 100644 --- a/app/javascript/submission_form/form.vue +++ b/app/javascript/submission_form/form.vue @@ -23,7 +23,7 @@ { - acc[f.uuid] = (this.values[f.uuid] || f.default_value) + acc[f.uuid] = isEmpty(this.values[f.uuid]) ? f.default_value : this.values[f.uuid] + + return acc + }, {}) + }, + readonlyFieldValues () { + return this.readonlyFields.reduce((acc, f) => { + acc[f.uuid] = isEmpty(this.values[f.uuid]) ? f.default_value : this.values[f.uuid] return acc }, {}) @@ -972,7 +981,10 @@ export default { return this.currentStepFields[0] }, readonlyConditionalFields () { - return this.fields.filter((f) => f.readonly && f.conditions?.length && this.checkFieldConditions(f) && this.checkFieldDocumentsConditions(f)) + return this.readonlyFields.filter((f) => f.conditions?.length) + }, + readonlyFields () { + return this.fields.filter((f) => f.readonly && this.checkFieldConditions(f) && this.checkFieldDocumentsConditions(f)) }, stepFields () { const verificationFields = [] diff --git a/app/javascript/submission_form/payment_step.vue b/app/javascript/submission_form/payment_step.vue index 6bbeb146..e4f2e232 100644 --- a/app/javascript/submission_form/payment_step.vue +++ b/app/javascript/submission_form/payment_step.vue @@ -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, {