fix(bank-integration): reject group-node Customer/Supplier Group / Territory upfront

Customer/Supplier require a leaf node for customer_group/supplier_group/
territory; selecting a group node makes ERPNext throw "Cannot select a
Group type ...". The upfront validation now also checks is_group and
returns a clear message before any record is touched.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ali 2026-05-12 10:45:21 +00:00
parent 7e862e48e5
commit 94ce72e4fb
1 changed files with 14 additions and 2 deletions

View File

@ -19,8 +19,8 @@ def create_unmapped_customers(bank_integration):
if not unmapped: if not unmapped:
return {"success": True, "created_count": 0, "message": "No unmapped customers in table"} return {"success": True, "created_count": 0, "message": "No unmapped customers in table"}
# Validate config upfront — a clear message beats ERPNext's cryptic # Validate config upfront — clearer than ERPNext's errors thrown deep inside
# "Could not find Customer Group" thrown deep inside party creation. # party creation ("Could not find Customer Group" / "Cannot select a Group type").
for mapping, _bi_cust in unmapped: for mapping, _bi_cust in unmapped:
group = mapping.customer_group or bi.default_customer_group group = mapping.customer_group or bi.default_customer_group
if not group or not frappe.db.exists("Customer Group", group): if not group or not frappe.db.exists("Customer Group", group):
@ -28,12 +28,20 @@ def create_unmapped_customers(bank_integration):
"Set a Default Customer Group on the Bank Integration " "Set a Default Customer Group on the Bank Integration "
"(or a Customer Group on each customer mapping row) before creating customers." "(or a Customer Group on each customer mapping row) before creating customers."
)) ))
if frappe.db.get_value("Customer Group", group, "is_group"):
frappe.throw(_(
"Customer Group '{0}' is a group node — pick a leaf (non-group) Customer Group."
).format(group))
territory = mapping.territory or bi.default_territory territory = mapping.territory or bi.default_territory
if not territory or not frappe.db.exists("Territory", territory): if not territory or not frappe.db.exists("Territory", territory):
frappe.throw(_( frappe.throw(_(
"Set a Default Territory on the Bank Integration " "Set a Default Territory on the Bank Integration "
"(or a Territory on each customer mapping row) before creating customers." "(or a Territory on each customer mapping row) before creating customers."
)) ))
if frappe.db.get_value("Territory", territory, "is_group"):
frappe.throw(_(
"Territory '{0}' is a group node — pick a leaf (non-group) Territory."
).format(territory))
created = 0 created = 0
for mapping, bi_cust in unmapped: for mapping, bi_cust in unmapped:
@ -102,6 +110,10 @@ def create_unmapped_suppliers(bank_integration):
"Set a Default Supplier Group on the Bank Integration " "Set a Default Supplier Group on the Bank Integration "
"(or a Supplier Group on each supplier mapping row) before creating suppliers." "(or a Supplier Group on each supplier mapping row) before creating suppliers."
)) ))
if frappe.db.get_value("Supplier Group", group, "is_group"):
frappe.throw(_(
"Supplier Group '{0}' is a group node — pick a leaf (non-group) Supplier Group."
).format(group))
created = 0 created = 0
for mapping, bi_supp in unmapped: for mapping, bi_supp in unmapped: