estimate handling, percentage discounts

This commit is contained in:
Casey 2025-12-23 08:12:37 -06:00
parent 9c837deb52
commit b8c264f779
10 changed files with 1365 additions and 31 deletions

View file

@ -65,7 +65,38 @@ def get_estimate(estimate_name):
"""Get detailed information for a specific estimate."""
try:
estimate = frappe.get_doc("Quotation", estimate_name)
return build_success_response(estimate.as_dict())
est_dict = estimate.as_dict()
address_name = estimate.custom_installation_address or estimate.customer_address
if address_name:
# Fetch Address Doc
address_doc = frappe.get_doc("Address", address_name).as_dict()
est_dict["full_address"] = address_doc.get("full_address")
# Logic from get_address_by_full_address to populate customer and contacts
customer_exists = frappe.db.exists("Customer", address_doc.get("custom_customer_to_bill"))
doctype = "Customer" if customer_exists else "Lead"
name = ""
if doctype == "Customer":
name = address_doc.get("custom_customer_to_bill")
else:
lead_links = address_doc.get("links", [])
lead_name = [link.link_name for link in lead_links if link.link_doctype == "Lead"]
name = lead_name[0] if lead_name else ""
if name:
address_doc["customer"] = frappe.get_doc(doctype, name).as_dict()
contacts = []
if address_doc.get("custom_linked_contacts"):
for contact_link in address_doc.get("custom_linked_contacts"):
contact_doc = frappe.get_doc("Contact", contact_link.contact)
contacts.append(contact_doc.as_dict())
address_doc["contacts"] = contacts
est_dict["address_details"] = address_doc
return build_success_response(est_dict)
except Exception as e:
return build_error_response(str(e), 500)
@ -202,7 +233,7 @@ def update_response(name, response):
estimate.custom_response = response
estimate.custom_current_status = new_status
estimate.custom_followup_needed = 1 if response == "Requested call" else 0
estimate.status = "Ordered" if accepted else "Closed"
# estimate.status = "Ordered" if accepted else "Closed"
estimate.flags.ignore_permissions = True
print("DEBUG: Updating estimate with response:", response, "and status:", new_status)
estimate.save()
@ -222,7 +253,7 @@ def update_response(name, response):
return Response(html, mimetype="text/html")
except Exception as e:
template = "custom_ui/templates/estimates/error.html"
html = frappe.render_template(template, {"error_message": str(e)})
html = frappe.render_template(template, {"error": str(e)})
return Response(html, mimetype="text/html")
@ -236,6 +267,7 @@ def upsert_estimate(data):
print("DEBUG: Upsert estimate data:", data)
estimate_name = data.get("estimate_name")
is_customer = True if frappe.db.exists("Customer", data.get("customer")) else False
# If estimate_name exists, update existing estimate
if estimate_name:
@ -244,7 +276,8 @@ def upsert_estimate(data):
# Update fields
estimate.custom_installation_address = data.get("address_name")
estimate.party_name = data.get("contact_name")
estimate.party_name = data.get("customer")
estimate.contact_person = data.get("contact_name")
estimate.custom_requires_half_payment = data.get("requires_half_payment", 0)
# Clear existing items and add new ones
@ -254,6 +287,8 @@ def upsert_estimate(data):
estimate.append("items", {
"item_code": item.get("item_code"),
"qty": item.get("qty"),
"discount_amount": item.get("discount_amount") or item.get("discountAmount", 0),
"discount_percentage": item.get("discount_percentage") or item.get("discountPercentage", 0)
})
estimate.save()
@ -267,18 +302,24 @@ def upsert_estimate(data):
new_estimate = frappe.get_doc({
"doctype": "Quotation",
"custom_requires_half_payment": data.get("requires_half_payment", 0),
"custom_installation_address": data.get("address_name"),
# "custom_installation_address": data.get("address_name"),
"custom_current_status": "Draft",
"contact_email": data.get("contact_email"),
"party_name": data.get("contact_name"),
"company": "Sprinklers Northwest",
"customer_name": data.get("customer_name"),
"party_name": data.get("customer"),
"quotation_to": "Customer" if is_customer else "Lead",
"company": data.get("company"),
"customer_name": data.get("customer"),
"customer_address": data.get("address_name"),
"contact_person": data.get("contact_name"),
"letter_head": "company",
})
for item in data.get("items", []):
item = json.loads(item) if isinstance(item, str) else item
new_estimate.append("items", {
"item_code": item.get("item_code"),
"qty": item.get("qty"),
"discount_amount": item.get("discount_amount") or item.get("discountAmount", 0),
"discount_percentage": item.get("discount_percentage") or item.get("discountPercentage", 0)
})
new_estimate.insert()
print("DEBUG: New estimate created with name:", new_estimate.name)
@ -286,4 +327,13 @@ def upsert_estimate(data):
except Exception as e:
print(f"DEBUG: Error in upsert_estimate: {str(e)}")
return build_error_response(str(e), 500)
# @frappe.whitelist()
# def get_estimate_counts():
# """Get specific counts of estimates based on their status."""
# try:
# counts = {
# "total_estimates": frappe.db.count("Quotation"),
# "ready_to_"
# }