mirror of
https://github.com/docusealco/docuseal
synced 2026-11-16 08:12:32 -05:00
formula condition
This commit is contained in:
1 parent
06c44e8b2f
commit
0490ff4835
4 files changed
+61
-19
No files matched your search
@@ -158,7 +158,9 @@ module Api
|
|||||||
|
|
||||||
if attrs[:completed]
|
if attrs[:completed]
|
||||||
submitter.values = Submitters::SubmitValues.merge_default_values(submitter)
|
submitter.values = Submitters::SubmitValues.merge_default_values(submitter)
|
||||||
submitter.values = Submitters::SubmitValues.maybe_remove_condition_values(submitter)
|
submitter.values = Submitters::SubmitValues.maybe_remove_condition_values(
|
||||||
|
submitter, formula_values: Submitters::SubmitValues.build_formula_values(submitter)
|
||||||
|
)
|
||||||
|
|
||||||
formula_values = Submitters::SubmitValues.build_formula_values(submitter)
|
formula_values = Submitters::SubmitValues.build_formula_values(submitter)
|
||||||
|
|
||||||
|
|||||||
@@ -1093,7 +1093,8 @@ export default {
|
|||||||
submittedValues: {},
|
submittedValues: {},
|
||||||
isFormStarted: false,
|
isFormStarted: false,
|
||||||
recalculateButtonDisabledKey: '',
|
recalculateButtonDisabledKey: '',
|
||||||
isAccessibilityMode: false
|
isAccessibilityMode: false,
|
||||||
|
isMathLoaded: false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
@@ -1197,6 +1198,24 @@ export default {
|
|||||||
return acc
|
return acc
|
||||||
}, {})
|
}, {})
|
||||||
},
|
},
|
||||||
|
formulaFieldsUuidIndex () {
|
||||||
|
return this.fields.reduce((acc, f) => {
|
||||||
|
if (f.preferences?.formula && f.type !== 'payment') {
|
||||||
|
acc[f.uuid] = f
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc
|
||||||
|
}, {})
|
||||||
|
},
|
||||||
|
readonlyValues () {
|
||||||
|
return this.fields.reduce((acc, f) => {
|
||||||
|
if (f.readonly) {
|
||||||
|
acc[f.uuid] = isEmpty(this.values[f.uuid]) ? f.default_value : this.values[f.uuid]
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc
|
||||||
|
}, {})
|
||||||
|
},
|
||||||
previousInitialsValue () {
|
previousInitialsValue () {
|
||||||
if (this.reuseSignature !== false) {
|
if (this.reuseSignature !== false) {
|
||||||
const initialsField = this.fields.findLast
|
const initialsField = this.fields.findLast
|
||||||
@@ -1449,6 +1468,16 @@ export default {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
normalizeFormula: FormulaFieldAreas.methods.normalizeFormula,
|
||||||
|
calculateFormula: FormulaFieldAreas.methods.calculateFormula,
|
||||||
|
dateFormulaValue: FormulaFieldAreas.methods.dateFormulaValue,
|
||||||
|
evalTextFormula: FormulaFieldAreas.methods.evalTextFormula,
|
||||||
|
loadCalculator () {
|
||||||
|
this.calculatorPromise ||= import('./calculator').then(({ Calculator }) => {
|
||||||
|
this.math = new Calculator()
|
||||||
|
this.isMathLoaded = true
|
||||||
|
})
|
||||||
|
},
|
||||||
t (key) {
|
t (key) {
|
||||||
return this.i18n[key] || i18n[this.language?.toLowerCase()]?.[key] || i18n[this.browserLanguage]?.[key] || i18n.en[key] || key
|
return this.i18n[key] || i18n[this.language?.toLowerCase()]?.[key] || i18n[this.browserLanguage]?.[key] || i18n.en[key] || key
|
||||||
},
|
},
|
||||||
@@ -1497,17 +1526,25 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const defaultValue = !field || isEmpty(field.default_value) ? null : field.default_value
|
const defaultValue = !field || isEmpty(field.default_value) ? null : field.default_value
|
||||||
|
const isFormulaField = !!field?.preferences?.formula && field.type !== 'payment'
|
||||||
|
|
||||||
|
if (isFormulaField && !this.isMathLoaded) {
|
||||||
|
this.loadCalculator()
|
||||||
|
}
|
||||||
|
|
||||||
|
const formulaValue = isFormulaField && this.isMathLoaded
|
||||||
|
? (field.type === 'text' ? this.evalTextFormula(field) : this.calculateFormula(field))
|
||||||
|
: null
|
||||||
|
const fieldValue = formulaValue ?? this.values[condition.field_uuid] ?? defaultValue
|
||||||
|
|
||||||
if (['empty', 'unchecked'].includes(condition.action)) {
|
if (['empty', 'unchecked'].includes(condition.action)) {
|
||||||
return isEmpty(this.values[condition.field_uuid] ?? defaultValue)
|
return isEmpty(fieldValue)
|
||||||
} else if (['not_empty', 'checked'].includes(condition.action)) {
|
} else if (['not_empty', 'checked'].includes(condition.action)) {
|
||||||
return !isEmpty(this.values[condition.field_uuid] ?? defaultValue)
|
return !isEmpty(fieldValue)
|
||||||
} else if (field?.type === 'number' && ['equal', 'not_equal', 'greater_than', 'less_than'].includes(condition.action)) {
|
} else if (field?.type === 'number' && ['equal', 'not_equal', 'greater_than', 'less_than'].includes(condition.action)) {
|
||||||
const value = this.values[condition.field_uuid] ?? defaultValue
|
if (isEmpty(fieldValue) || isEmpty(condition.value)) return false
|
||||||
|
|
||||||
if (isEmpty(value) || isEmpty(condition.value)) return false
|
const actual = parseFloat(fieldValue)
|
||||||
|
|
||||||
const actual = parseFloat(value)
|
|
||||||
const expected = parseFloat(condition.value)
|
const expected = parseFloat(condition.value)
|
||||||
|
|
||||||
if (Number.isNaN(actual) || Number.isNaN(expected)) return false
|
if (Number.isNaN(actual) || Number.isNaN(expected)) return false
|
||||||
@@ -1523,21 +1560,21 @@ export default {
|
|||||||
const option = field.options.find((o) => o.uuid === condition.value)
|
const option = field.options.find((o) => o.uuid === condition.value)
|
||||||
|
|
||||||
if (option) {
|
if (option) {
|
||||||
const values = [this.values[condition.field_uuid] ?? defaultValue].flat()
|
const values = [fieldValue].flat()
|
||||||
|
|
||||||
return values.includes(this.optionValue(option, field.options.indexOf(option)))
|
return values.includes(this.optionValue(option, field.options.indexOf(option)))
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return [this.values[condition.field_uuid] ?? defaultValue].flat().includes(condition.value)
|
return [fieldValue].flat().includes(condition.value)
|
||||||
}
|
}
|
||||||
} else if (['not_equal', 'does_not_contain'].includes(condition.action) && field) {
|
} else if (['not_equal', 'does_not_contain'].includes(condition.action) && field) {
|
||||||
if (field.options) {
|
if (field.options) {
|
||||||
const option = field.options.find((o) => o.uuid === condition.value)
|
const option = field.options.find((o) => o.uuid === condition.value)
|
||||||
|
|
||||||
if (option) {
|
if (option) {
|
||||||
const values = [this.values[condition.field_uuid] ?? defaultValue].flat()
|
const values = [fieldValue].flat()
|
||||||
|
|
||||||
return !values.includes(this.optionValue(option, field.options.indexOf(option)))
|
return !values.includes(this.optionValue(option, field.options.indexOf(option)))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -459,7 +459,9 @@ module Submissions
|
|||||||
|
|
||||||
def assign_completed_attributes(submitter, with_verification: true)
|
def assign_completed_attributes(submitter, with_verification: true)
|
||||||
submitter.values = Submitters::SubmitValues.merge_default_values(submitter, with_verification:)
|
submitter.values = Submitters::SubmitValues.merge_default_values(submitter, with_verification:)
|
||||||
submitter.values = Submitters::SubmitValues.maybe_remove_condition_values(submitter)
|
submitter.values = Submitters::SubmitValues.maybe_remove_condition_values(
|
||||||
|
submitter, formula_values: Submitters::SubmitValues.build_formula_values(submitter)
|
||||||
|
)
|
||||||
|
|
||||||
formula_values = Submitters::SubmitValues.build_formula_values(submitter)
|
formula_values = Submitters::SubmitValues.build_formula_values(submitter)
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,8 @@ module Submitters
|
|||||||
|
|
||||||
required_field_uuids_acc = Set.new
|
required_field_uuids_acc = Set.new
|
||||||
|
|
||||||
submitter.values = maybe_remove_condition_values(submitter, required_field_uuids_acc:)
|
submitter.values = maybe_remove_condition_values(submitter, formula_values: build_formula_values(submitter),
|
||||||
|
required_field_uuids_acc:)
|
||||||
|
|
||||||
formula_values = build_formula_values(submitter)
|
formula_values = build_formula_values(submitter)
|
||||||
|
|
||||||
@@ -204,10 +205,10 @@ module Submitters
|
|||||||
default_values.compact_blank.merge(submitter.values)
|
default_values.compact_blank.merge(submitter.values)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_formula_values(submitter)
|
def build_formula_values(submitter, submission_values: nil)
|
||||||
submission_values = nil
|
submission = submitter.submission
|
||||||
|
|
||||||
computed_values = submitter.submission.template_fields.each_with_object({}) do |field, acc|
|
computed_values = (submission.template_fields || submission.template.fields).each_with_object({}) do |field, acc|
|
||||||
next if field['submitter_uuid'] != submitter.uuid
|
next if field['submitter_uuid'] != submitter.uuid
|
||||||
next if field['type'] == 'payment'
|
next if field['type'] == 'payment'
|
||||||
|
|
||||||
@@ -297,17 +298,17 @@ module Submitters
|
|||||||
field:)
|
field:)
|
||||||
end
|
end
|
||||||
|
|
||||||
def maybe_remove_condition_values(submitter, required_field_uuids_acc: nil)
|
def maybe_remove_condition_values(submitter, formula_values: {}, required_field_uuids_acc: nil)
|
||||||
submission = submitter.submission
|
submission = submitter.submission
|
||||||
|
|
||||||
submitters_values = nil
|
submitters_values = formula_values.merge(merge_submitters_values(submitter)) if formula_values.present?
|
||||||
has_other_submitters = submission.template_submitters.size > 1
|
has_other_submitters = submission.template_submitters.size > 1
|
||||||
|
|
||||||
has_document_conditions = submission_has_document_conditions?(submission)
|
has_document_conditions = submission_has_document_conditions?(submission)
|
||||||
|
|
||||||
attachments_index =
|
attachments_index =
|
||||||
if has_document_conditions
|
if has_document_conditions
|
||||||
submitters_values = merge_submitters_values(submitter)
|
submitters_values ||= merge_submitters_values(submitter)
|
||||||
|
|
||||||
Submissions.filtered_conditions_schema(submission, values: submitters_values)
|
Submissions.filtered_conditions_schema(submission, values: submitters_values)
|
||||||
.index_by { |i| i['attachment_uuid'] }
|
.index_by { |i| i['attachment_uuid'] }
|
||||||
|
|||||||
Reference in new issue
Block a user