mirror of
https://github.com/frappe/erpnext.git
synced 2026-09-09 23:39:28 +00:00
fix: minor improvements to financial report template validation (#58724)
* fix: address review comments on financial report template validation
* refactor: minor fixes
(cherry picked from commit 1b7da82669)
This commit is contained in:
committed by
Mergify
parent
770726d8f5
commit
d23b407ec7
@@ -193,8 +193,10 @@ class TemplateStructureValidator(Validator):
|
|||||||
if not row.calculation_formula:
|
if not row.calculation_formula:
|
||||||
result.add_error(
|
result.add_error(
|
||||||
ValidationIssue(
|
ValidationIssue(
|
||||||
message=_("{0} is required for {1}").format(
|
message=_("{0} is required when {1} is {2}").format(
|
||||||
get_formula_field_label(row.data_source), row.data_source
|
get_formula_field_label(row.data_source),
|
||||||
|
row.meta.get_translated_label("data_source"),
|
||||||
|
_(row.data_source),
|
||||||
),
|
),
|
||||||
row_idx=row.idx,
|
row_idx=row.idx,
|
||||||
)
|
)
|
||||||
@@ -222,7 +224,14 @@ class DependencyValidator(Validator):
|
|||||||
|
|
||||||
for row in self.template.rows:
|
for row in self.template.rows:
|
||||||
if row.reference_code and row.data_source == "Calculated Amount" and row.calculation_formula:
|
if row.reference_code and row.data_source == "Calculated Amount" and row.calculation_formula:
|
||||||
deps = extract_reference_codes_from_formula(row.calculation_formula, list(available_codes))
|
# skip self-reference, `CalculationFormulaValidator` already reports it
|
||||||
|
deps = [
|
||||||
|
code
|
||||||
|
for code in extract_reference_codes_from_formula(
|
||||||
|
row.calculation_formula, list(available_codes)
|
||||||
|
)
|
||||||
|
if code != row.reference_code
|
||||||
|
]
|
||||||
if deps:
|
if deps:
|
||||||
graph[row.reference_code] = deps
|
graph[row.reference_code] = deps
|
||||||
|
|
||||||
@@ -284,7 +293,9 @@ class DependencyValidator(Validator):
|
|||||||
row_idx = self._get_row_idx(ref_code)
|
row_idx = self._get_row_idx(ref_code)
|
||||||
result.add_error(
|
result.add_error(
|
||||||
ValidationIssue(
|
ValidationIssue(
|
||||||
message=_("Line References undefined in Formula: {0}").format(", ".join(undefined)),
|
message=_("Line references undefined in {0}: {1}").format(
|
||||||
|
get_formula_field_label("Calculated Amount"), ", ".join(undefined)
|
||||||
|
),
|
||||||
row_idx=row_idx,
|
row_idx=row_idx,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -311,17 +322,6 @@ class CalculationFormulaValidator(Validator):
|
|||||||
if row.data_source != "Calculated Amount":
|
if row.data_source != "Calculated Amount":
|
||||||
return result
|
return result
|
||||||
|
|
||||||
if not row.calculation_formula:
|
|
||||||
result.add_error(
|
|
||||||
ValidationIssue(
|
|
||||||
message=_("{0} is required for Calculated Amount").format(
|
|
||||||
get_formula_field_label(row.data_source)
|
|
||||||
),
|
|
||||||
row_idx=row.idx,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return result
|
|
||||||
|
|
||||||
formula = self._preprocess_formula(row.calculation_formula)
|
formula = self._preprocess_formula(row.calculation_formula)
|
||||||
row.calculation_formula = formula
|
row.calculation_formula = formula
|
||||||
|
|
||||||
@@ -346,16 +346,6 @@ class CalculationFormulaValidator(Validator):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check undefined references
|
|
||||||
undefined = set(refs) - set(available_codes)
|
|
||||||
if undefined:
|
|
||||||
result.add_error(
|
|
||||||
ValidationIssue(
|
|
||||||
message=_("Formula references undefined codes: {0}").format(", ".join(undefined)),
|
|
||||||
row_idx=row.idx,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Try to evaluate with dummy values
|
# Try to evaluate with dummy values
|
||||||
eval_error = self._test_formula_evaluation(formula, available_codes)
|
eval_error = self._test_formula_evaluation(formula, available_codes)
|
||||||
if eval_error:
|
if eval_error:
|
||||||
@@ -418,17 +408,6 @@ class AccountFilterValidator(Validator):
|
|||||||
if row.data_source != "Account Data":
|
if row.data_source != "Account Data":
|
||||||
return result
|
return result
|
||||||
|
|
||||||
if not row.calculation_formula:
|
|
||||||
result.add_error(
|
|
||||||
ValidationIssue(
|
|
||||||
message=_("{0} is required for Account Data").format(
|
|
||||||
get_formula_field_label(row.data_source)
|
|
||||||
),
|
|
||||||
row_idx=row.idx,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return result
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
filter_config = json.loads(row.calculation_formula)
|
filter_config = json.loads(row.calculation_formula)
|
||||||
error = self._validate_filter_structure(
|
error = self._validate_filter_structure(
|
||||||
@@ -440,7 +419,9 @@ class AccountFilterValidator(Validator):
|
|||||||
if error:
|
if error:
|
||||||
result.add_error(
|
result.add_error(
|
||||||
ValidationIssue(
|
ValidationIssue(
|
||||||
message=_("{0}: {1}").format(get_formula_field_label(row.data_source), error),
|
message=_("[{0}] {1}", context="Financial Report Template").format(
|
||||||
|
get_formula_field_label(row.data_source), error
|
||||||
|
),
|
||||||
row_idx=row.idx,
|
row_idx=row.idx,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -448,8 +429,9 @@ class AccountFilterValidator(Validator):
|
|||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
result.add_error(
|
result.add_error(
|
||||||
ValidationIssue(
|
ValidationIssue(
|
||||||
message=_("{0}: Invalid JSON format: {1}").format(
|
message=_("[{0}] {1}", context="Financial Report Template").format(
|
||||||
get_formula_field_label(row.data_source), str(e)
|
get_formula_field_label(row.data_source),
|
||||||
|
_("Invalid JSON format: {0}").format(str(e)),
|
||||||
),
|
),
|
||||||
row_idx=row.idx,
|
row_idx=row.idx,
|
||||||
)
|
)
|
||||||
@@ -555,8 +537,9 @@ class FormulaValidator(Validator):
|
|||||||
frappe.clear_last_message()
|
frappe.clear_last_message()
|
||||||
|
|
||||||
if isinstance(e, frappe.PermissionError):
|
if isinstance(e, frappe.PermissionError):
|
||||||
message = _("{0}: Method '{1}' must be whitelisted and permit GET requests").format(
|
message = _("[{0}] {1}", context="Financial Report Template").format(
|
||||||
get_formula_field_label(row.data_source), api_path
|
get_formula_field_label(row.data_source),
|
||||||
|
_("Method '{0}' must be whitelisted and permit GET requests").format(api_path),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
message = _("Could not validate {0}: {1}").format(
|
message = _("Could not validate {0}: {1}").format(
|
||||||
|
|||||||
Reference in New Issue
Block a user