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:
parent
853c01e3e9
commit
d9d033bfe1
|
|
@ -26,22 +26,22 @@ def _api_error_message(resp):
|
|||
|
||||
|
||||
def _resolve_leaf_node(doctype, configured, label):
|
||||
"""Resolve `configured` to a valid leaf node of an NSM tree doctype
|
||||
(Customer Group / Supplier Group / Territory). Empty config falls back to
|
||||
the first leaf node so creation works without a default set.
|
||||
"""Validate `configured` as a leaf node of an NSM tree doctype
|
||||
(Customer Group / Supplier Group / Territory).
|
||||
|
||||
- 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.
|
||||
"""
|
||||
if configured:
|
||||
if not configured:
|
||||
return None, None
|
||||
if not frappe.db.exists(doctype, configured):
|
||||
return None, f"{label} '{configured}' does not exist."
|
||||
if frappe.db.get_value(doctype, configured, "is_group"):
|
||||
return None, f"{label} '{configured}' is a group node — pick a leaf (non-group) {label}."
|
||||
return configured, None
|
||||
leaf = frappe.db.get_value(doctype, {"is_group": 0}, "name", order_by="lft asc")
|
||||
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.customer_name = customer_name
|
||||
party.customer_type = "Company"
|
||||
party.customer_group = customer_group
|
||||
party.territory = territory
|
||||
party.customer_group = customer_group or None
|
||||
party.territory = territory or None
|
||||
|
||||
if 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.supplier_name = supplier_name
|
||||
party.supplier_type = "Company"
|
||||
party.supplier_group = supplier_group
|
||||
party.supplier_group = supplier_group or None
|
||||
|
||||
if kb_supplier.tax_id:
|
||||
party.tax_id = kb_supplier.tax_id
|
||||
|
|
|
|||
Loading…
Reference in New Issue