Fixes for Estimate/Sales Order creation.

This commit is contained in:
rocketdebris 2026-01-09 13:29:51 -05:00
parent 016aa08b95
commit f7ce3a39d0
5 changed files with 34 additions and 33 deletions

View file

@ -67,7 +67,7 @@ def get_estimate(estimate_name):
try:
estimate = frappe.get_doc("Quotation", estimate_name)
est_dict = estimate.as_dict()
address_name = estimate.custom_installation_address or estimate.customer_address
if address_name:
# Fetch Address Doc
@ -84,19 +84,19 @@ def get_estimate(estimate_name):
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
est_dict["history"] = get_doc_history("Quotation", estimate_name)
return build_success_response(est_dict)
@ -269,7 +269,7 @@ def get_estimate_templates(company):
try:
print("DEBUG: Fetching estimate templates for company:", company)
templates = frappe.get_all("Quotation Template", fields=["*"], filters=filters)
result = []
if not templates:
print("DEBUG: No templates found.")
@ -277,11 +277,11 @@ def get_estimate_templates(company):
print(f"DEBUG: Found {len(templates)} templates.")
for template in templates:
print("DEBUG: Processing template:", template)
items = frappe.get_all("Quotation Template Item",
items = frappe.get_all("Quotation Template Item",
fields=["item_code", "item_name", "description", "quantity", "discount_percentage", "rate"],
filters={"parent": template.name},
order_by="idx")
# Map fields to camelCase as requested
mapped_items = []
for item in items:
@ -293,7 +293,7 @@ def get_estimate_templates(company):
"discountPercentage": item.discount_percentage,
"rate": item.rate
})
result.append({
"name": template.name,
"templateName": template.template_name,
@ -301,7 +301,7 @@ def get_estimate_templates(company):
"description": template.description,
"items": mapped_items
})
return build_success_response(result)
except Exception as e:
return build_error_response(str(e), 500)
@ -312,7 +312,7 @@ def create_estimate_template(data):
try:
print("DEBUG: Creating estimate template with data:", data)
data = json.loads(data) if isinstance(data, str) else data
doc_data = {
"doctype": "Quotation Template",
"is_active": 1,
@ -322,10 +322,10 @@ def create_estimate_template(data):
"template_name": data.get("template_name"),
"source_quotation": data.get("source_quotation", "")
}
new_template = frappe.get_doc(doc_data)
for item in data.get("items", []):
new_template.append("items", {
"item_code": item.get("item_code"),
@ -335,19 +335,19 @@ def create_estimate_template(data):
"rate": item.get("standard_rate") or item.get("rate"),
"discount_percentage": item.get("discount_percentage")
})
new_template.insert()
return build_success_response(new_template.name)
except Exception as e:
return build_error_response(str(e), 500)
# @frappe.whitelist()
# def create_template(data):
# """Create a new estimate template."""
# try:
# data = json.loads(data) if isinstance(data, str) else data
# print("DEBUG: Creating estimate template with data:", data)
# new_template = frappe.get_doc({
# "doctype": "Quotation Template",
# "template_name": data.get("templateName"),
@ -356,7 +356,7 @@ def create_estimate_template(data):
# "company": data.get("company", ""),
# "source_quotation": data.get("source_quotation", "")
# })
# for item in data.get("items", []):
# item = json.loads(item) if isinstance(item, str) else item
# new_template.append("items", {
@ -367,7 +367,7 @@ def create_estimate_template(data):
# "discount_percentage": item.get("discountPercentage"),
# "rate": item.get("rate")
# })
# new_template.insert()
# print("DEBUG: New estimate template created with name:", new_template.name)
# return build_success_response(new_template.as_dict())
@ -390,7 +390,7 @@ def upsert_estimate(data):
estimate = frappe.get_doc("Quotation", estimate_name)
# Update fields
estimate.custom_installation_address = data.get("address_name")
estimate.custom_installation_address = data.get("address")
estimate.party_name = data.get("customer")
estimate.contact_person = data.get("contact_name")
estimate.custom_requires_half_payment = data.get("requires_half_payment", 0)
@ -419,7 +419,7 @@ 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("customer"),
@ -445,12 +445,12 @@ def upsert_estimate(data):
except Exception as e:
print(f"DEBUG: Error in upsert_estimate: {str(e)}")
return build_error_response(str(e), 500)
def get_estimate_history(estimate_name):
"""Get the history of changes for a specific estimate."""
return history
# @frappe.whitelist()
# def get_estimate_counts():
# """Get specific counts of estimates based on their status."""
@ -459,4 +459,4 @@ def get_estimate_history(estimate_name):
# "total_estimates": frappe.db.count("Quotation"),
# "ready_to_"
# }