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_"
# }

View File

@ -34,6 +34,7 @@ def on_update_after_submit(doc, method):
try:
new_sales_order = make_sales_order(doc.name)
new_sales_order.custom_requires_half_payment = doc.requires_half_payment
new_sales_order.custom_installation_address = doc.custom_installation_address
new_sales_order.payment_schedule = []
print("DEBUG: Setting payment schedule for Sales Order")
new_sales_order.set_payment_schedule()

View File

@ -1,12 +1,12 @@
import frappe
def after_insert(doc, method):
pass
def on_submit(doc, method):
print(doc.as_dict())
print("DEBUG: Info from Sales Order")
print(doc.custom_installation_address)
print(doc.company)
print(doc.transaction_date)
print(doc.customer)
# Create Invoice and Project from Sales Order
try:
print("Creating Project from Sales Order", doc.name)

View File

@ -173,7 +173,6 @@ doc_events = {
"on_update_after_submit": "custom_ui.events.estimate.on_update_after_submit"
},
"Sales Order": {
"after_insert": "custom_ui.events.sales_order.after_insert",
"on_submit": "custom_ui.events.sales_order.on_submit",
}
}

View File

@ -592,6 +592,7 @@ const saveDraft = async () => {
isSubmitting.value = true;
try {
const data = {
address: formData.address,
addressName: formData.addressName,
contactName: selectedContact.value.name,
customer: selectedAddress.value?.customer?.name,
@ -768,7 +769,7 @@ watch(
} else if (newAddressQuery) {
await selectAddress(newAddressQuery);
}
formData.contact = estimate.value.contactPerson;
selectedContact.value = contacts.value.find((c) => c.name === estimate.value.contactPerson) || null;
@ -811,7 +812,7 @@ onMounted(async () => {
console.error("Error loading quotation items:", error);
}
fetchProjectTemplates();
if (isNew.value) {
fetchTemplates();
}
@ -839,7 +840,7 @@ onMounted(async () => {
} else if (addressQuery.value) {
await selectAddress(addressQuery.value);
}
// Set the contact from the estimate
formData.contact = estimate.value.contactPerson;
selectedContact.value = contacts.value.find((c) => c.name === estimate.value.contactPerson) || null;