fix: create party without a group when none is configured

The first-leaf fallback meant customers always landed in "Individual"
even when no Customer Group was chosen. Customer/Supplier/Territory
aren't actually mandatory in ERPNext, so now: a configured value is
validated (exists + leaf) and used; nothing configured -> the party is
created with an empty customer_group / supplier_group / territory.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ali 2026-05-12 11:05:09 +00:00
parent 853c01e3e9
commit d9d033bfe1
1 changed files with 16 additions and 16 deletions

View File

@ -26,22 +26,22 @@ def _api_error_message(resp):
def _resolve_leaf_node(doctype, configured, label): def _resolve_leaf_node(doctype, configured, label):
"""Resolve `configured` to a valid leaf node of an NSM tree doctype """Validate `configured` as a leaf node of an NSM tree doctype
(Customer Group / Supplier Group / Territory). Empty config falls back to (Customer Group / Supplier Group / Territory).
the first leaf node so creation works without a default set.
- If configured: it must exist and NOT be a group node.
- If not configured: returns (None, None) the party is then created
without that field (ERPNext allows an empty group / territory).
Returns (value, error_message). On success error_message is None. Returns (value, error_message). On success error_message is None.
""" """
if configured: if not configured:
if not frappe.db.exists(doctype, configured): return None, None
return None, f"{label} '{configured}' does not exist." if not frappe.db.exists(doctype, configured):
if frappe.db.get_value(doctype, configured, "is_group"): return None, f"{label} '{configured}' does not exist."
return None, f"{label} '{configured}' is a group node — pick a leaf (non-group) {label}." if frappe.db.get_value(doctype, configured, "is_group"):
return configured, None return None, f"{label} '{configured}' is a group node — pick a leaf (non-group) {label}."
leaf = frappe.db.get_value(doctype, {"is_group": 0}, "name", order_by="lft asc") return configured, None
if not leaf:
return None, f"No non-group {label} exists. Create one first."
return leaf, None
# ═══════════════════════════════════════════════════════════════════════════════ # ═══════════════════════════════════════════════════════════════════════════════
@ -700,8 +700,8 @@ def create_unmapped_customers(settings_name=None):
party = frappe.new_doc("Customer") party = frappe.new_doc("Customer")
party.customer_name = customer_name party.customer_name = customer_name
party.customer_type = "Company" party.customer_type = "Company"
party.customer_group = customer_group party.customer_group = customer_group or None
party.territory = territory party.territory = territory or None
if kb_customer.tax_id: if kb_customer.tax_id:
party.tax_id = kb_customer.tax_id party.tax_id = kb_customer.tax_id
@ -774,7 +774,7 @@ def create_unmapped_suppliers(settings_name=None):
party = frappe.new_doc("Supplier") party = frappe.new_doc("Supplier")
party.supplier_name = supplier_name party.supplier_name = supplier_name
party.supplier_type = "Company" party.supplier_type = "Company"
party.supplier_group = supplier_group party.supplier_group = supplier_group or None
if kb_supplier.tax_id: if kb_supplier.tax_id:
party.tax_id = kb_supplier.tax_id party.tax_id = kb_supplier.tax_id