Fix portal orders: disable broken sidebar, fix customer lookup, add email match

- Disable sidebar (was auto-linking order names as broken relative URLs)
- Remove broken portal_user_name lookup (field doesn't exist)
- Match orders by Customer record OR by custom_contact_email on Sales Order
- Merges both result sets without duplicates

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
justin 2026-05-04 20:40:28 -05:00
parent 97dd08c821
commit 924b9e792b

View file

@ -66,39 +66,63 @@ def get_context(context):
raise frappe.Redirect raise frappe.Redirect
context.no_cache = 1 context.no_cache = 1
context.show_sidebar = True context.show_sidebar = False
context.title = "My Orders" context.title = "My Orders"
# Find the Customer linked to this portal user # Find the Customer linked to this portal user
user_email = frappe.session.user
customer_name = frappe.db.get_value( customer_name = frappe.db.get_value(
"Customer", {"portal_user_name": frappe.session.user}, "name" "Customer", {"email_id": user_email}, "name"
) )
if not customer_name: if not customer_name:
# Try matching by email_id # Try matching via Contact linked to Customer
customer_name = frappe.db.get_value( contact_name = frappe.db.get_value("Contact", {"email_id": user_email}, "name")
"Customer", {"email_id": frappe.session.user}, "name" if contact_name:
link = frappe.db.get_value(
"Dynamic Link",
{"parent": contact_name, "link_doctype": "Customer"},
"link_name",
)
if link:
customer_name = link
context.customer_name = customer_name or ""
context.user_email = user_email
# Fetch Sales Orders — by Customer link first, then by contact email
so_fields = [
"name", "custom_external_order_id", "custom_order_type",
"workflow_state", "transaction_date", "grand_total",
"custom_mailbox_address", "custom_payment_gateway",
"status",
]
if customer_name:
raw_orders = frappe.get_all(
"Sales Order",
filters={"customer": customer_name, "docstatus": ["!=", 2]},
fields=so_fields,
order_by="transaction_date desc",
limit=50,
) )
else:
raw_orders = []
if not customer_name: # Also fetch orders by contact email (catches orders where Customer
context.orders = [] # record wasn't linked, e.g. compliance orders placed via PW checkout)
context.message = "No customer account found. Please contact support." email_orders = frappe.get_all(
return
context.customer_name = customer_name
# Fetch all Sales Orders for this customer (CRTC + Formation)
raw_orders = frappe.get_all(
"Sales Order", "Sales Order",
filters={"customer": customer_name, "docstatus": ["!=", 2]}, filters={"custom_contact_email": user_email, "docstatus": ["!=", 2]},
fields=[ fields=so_fields,
"name", "custom_external_order_id", "custom_order_type",
"workflow_state", "transaction_date", "grand_total",
"custom_mailbox_address", "custom_payment_gateway",
"status",
],
order_by="transaction_date desc", order_by="transaction_date desc",
limit=50, limit=50,
) )
# Merge without duplicates
seen = {so["name"] for so in raw_orders}
for so in email_orders:
if so["name"] not in seen:
raw_orders.append(so)
seen.add(so["name"])
orders = [] orders = []
for so in raw_orders: for so in raw_orders: