fix: handle percent-sign commission rates in migration patch

Values like "20%" or "20 %" parse to 0 via flt, which would wipe a real
rate. Strip a trailing percent sign before parsing so they migrate as 20.
This commit is contained in:
Nabin Hait
2026-06-22 11:19:34 +05:30
parent 2fe0601a2e
commit 0b1d06d46d

View File

@@ -6,8 +6,9 @@ def execute():
"""Sanitize the free-text commission_rate values before the Data -> Percent column change.
Sales Person and Sales Team stored ``commission_rate`` as Data (varchar). This runs in
pre_model_sync so the values are clean numeric strings by the time the schema sync alters
the column to Percent; empty or non-numeric values become 0.
pre_model_sync so the values are clean numbers by the time the schema sync alters the column
to Percent: a trailing percent sign (e.g. "20%" / "20 %") is stripped, and empty / NULL /
non-numeric values become 0.
"""
for doctype in ("Sales Person", "Sales Team"):
if not frappe.db.has_column(doctype, "commission_rate"):
@@ -17,6 +18,13 @@ def execute():
# as well, otherwise the column type change fails under strict SQL mode.
rows = frappe.db.get_all(doctype, fields=["name", "commission_rate"])
for row in rows:
cleaned = flt(row.commission_rate)
cleaned = flt(_strip_percent_sign(row.commission_rate))
if str(row.commission_rate) != str(cleaned):
frappe.db.set_value(doctype, row.name, "commission_rate", cleaned, update_modified=False)
def _strip_percent_sign(value):
"""Drop a trailing percent sign so "20%" / "20 %" parse as 20 instead of 0."""
if isinstance(value, str):
return value.replace("%", "").strip()
return value