Fixes for Estimate/Sales Order creation.
This commit is contained in:
parent
016aa08b95
commit
f7ce3a39d0
5 changed files with 34 additions and 33 deletions
|
|
@ -67,7 +67,7 @@ def get_estimate(estimate_name):
|
||||||
try:
|
try:
|
||||||
estimate = frappe.get_doc("Quotation", estimate_name)
|
estimate = frappe.get_doc("Quotation", estimate_name)
|
||||||
est_dict = estimate.as_dict()
|
est_dict = estimate.as_dict()
|
||||||
|
|
||||||
address_name = estimate.custom_installation_address or estimate.customer_address
|
address_name = estimate.custom_installation_address or estimate.customer_address
|
||||||
if address_name:
|
if address_name:
|
||||||
# Fetch Address Doc
|
# Fetch Address Doc
|
||||||
|
|
@ -84,19 +84,19 @@ def get_estimate(estimate_name):
|
||||||
lead_links = address_doc.get("links", [])
|
lead_links = address_doc.get("links", [])
|
||||||
lead_name = [link.link_name for link in lead_links if link.link_doctype == "Lead"]
|
lead_name = [link.link_name for link in lead_links if link.link_doctype == "Lead"]
|
||||||
name = lead_name[0] if lead_name else ""
|
name = lead_name[0] if lead_name else ""
|
||||||
|
|
||||||
if name:
|
if name:
|
||||||
address_doc["customer"] = frappe.get_doc(doctype, name).as_dict()
|
address_doc["customer"] = frappe.get_doc(doctype, name).as_dict()
|
||||||
|
|
||||||
contacts = []
|
contacts = []
|
||||||
if address_doc.get("custom_linked_contacts"):
|
if address_doc.get("custom_linked_contacts"):
|
||||||
for contact_link in 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)
|
contact_doc = frappe.get_doc("Contact", contact_link.contact)
|
||||||
contacts.append(contact_doc.as_dict())
|
contacts.append(contact_doc.as_dict())
|
||||||
address_doc["contacts"] = contacts
|
address_doc["contacts"] = contacts
|
||||||
|
|
||||||
est_dict["address_details"] = address_doc
|
est_dict["address_details"] = address_doc
|
||||||
|
|
||||||
est_dict["history"] = get_doc_history("Quotation", estimate_name)
|
est_dict["history"] = get_doc_history("Quotation", estimate_name)
|
||||||
|
|
||||||
return build_success_response(est_dict)
|
return build_success_response(est_dict)
|
||||||
|
|
@ -269,7 +269,7 @@ def get_estimate_templates(company):
|
||||||
try:
|
try:
|
||||||
print("DEBUG: Fetching estimate templates for company:", company)
|
print("DEBUG: Fetching estimate templates for company:", company)
|
||||||
templates = frappe.get_all("Quotation Template", fields=["*"], filters=filters)
|
templates = frappe.get_all("Quotation Template", fields=["*"], filters=filters)
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
if not templates:
|
if not templates:
|
||||||
print("DEBUG: No templates found.")
|
print("DEBUG: No templates found.")
|
||||||
|
|
@ -277,11 +277,11 @@ def get_estimate_templates(company):
|
||||||
print(f"DEBUG: Found {len(templates)} templates.")
|
print(f"DEBUG: Found {len(templates)} templates.")
|
||||||
for template in templates:
|
for template in templates:
|
||||||
print("DEBUG: Processing template:", template)
|
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"],
|
fields=["item_code", "item_name", "description", "quantity", "discount_percentage", "rate"],
|
||||||
filters={"parent": template.name},
|
filters={"parent": template.name},
|
||||||
order_by="idx")
|
order_by="idx")
|
||||||
|
|
||||||
# Map fields to camelCase as requested
|
# Map fields to camelCase as requested
|
||||||
mapped_items = []
|
mapped_items = []
|
||||||
for item in items:
|
for item in items:
|
||||||
|
|
@ -293,7 +293,7 @@ def get_estimate_templates(company):
|
||||||
"discountPercentage": item.discount_percentage,
|
"discountPercentage": item.discount_percentage,
|
||||||
"rate": item.rate
|
"rate": item.rate
|
||||||
})
|
})
|
||||||
|
|
||||||
result.append({
|
result.append({
|
||||||
"name": template.name,
|
"name": template.name,
|
||||||
"templateName": template.template_name,
|
"templateName": template.template_name,
|
||||||
|
|
@ -301,7 +301,7 @@ def get_estimate_templates(company):
|
||||||
"description": template.description,
|
"description": template.description,
|
||||||
"items": mapped_items
|
"items": mapped_items
|
||||||
})
|
})
|
||||||
|
|
||||||
return build_success_response(result)
|
return build_success_response(result)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return build_error_response(str(e), 500)
|
return build_error_response(str(e), 500)
|
||||||
|
|
@ -312,7 +312,7 @@ def create_estimate_template(data):
|
||||||
try:
|
try:
|
||||||
print("DEBUG: Creating estimate template with data:", data)
|
print("DEBUG: Creating estimate template with data:", data)
|
||||||
data = json.loads(data) if isinstance(data, str) else data
|
data = json.loads(data) if isinstance(data, str) else data
|
||||||
|
|
||||||
doc_data = {
|
doc_data = {
|
||||||
"doctype": "Quotation Template",
|
"doctype": "Quotation Template",
|
||||||
"is_active": 1,
|
"is_active": 1,
|
||||||
|
|
@ -322,10 +322,10 @@ def create_estimate_template(data):
|
||||||
"template_name": data.get("template_name"),
|
"template_name": data.get("template_name"),
|
||||||
"source_quotation": data.get("source_quotation", "")
|
"source_quotation": data.get("source_quotation", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
new_template = frappe.get_doc(doc_data)
|
new_template = frappe.get_doc(doc_data)
|
||||||
|
|
||||||
for item in data.get("items", []):
|
for item in data.get("items", []):
|
||||||
new_template.append("items", {
|
new_template.append("items", {
|
||||||
"item_code": item.get("item_code"),
|
"item_code": item.get("item_code"),
|
||||||
|
|
@ -335,19 +335,19 @@ def create_estimate_template(data):
|
||||||
"rate": item.get("standard_rate") or item.get("rate"),
|
"rate": item.get("standard_rate") or item.get("rate"),
|
||||||
"discount_percentage": item.get("discount_percentage")
|
"discount_percentage": item.get("discount_percentage")
|
||||||
})
|
})
|
||||||
|
|
||||||
new_template.insert()
|
new_template.insert()
|
||||||
return build_success_response(new_template.name)
|
return build_success_response(new_template.name)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return build_error_response(str(e), 500)
|
return build_error_response(str(e), 500)
|
||||||
|
|
||||||
# @frappe.whitelist()
|
# @frappe.whitelist()
|
||||||
# def create_template(data):
|
# def create_template(data):
|
||||||
# """Create a new estimate template."""
|
# """Create a new estimate template."""
|
||||||
# try:
|
# try:
|
||||||
# data = json.loads(data) if isinstance(data, str) else data
|
# data = json.loads(data) if isinstance(data, str) else data
|
||||||
# print("DEBUG: Creating estimate template with data:", data)
|
# print("DEBUG: Creating estimate template with data:", data)
|
||||||
|
|
||||||
# new_template = frappe.get_doc({
|
# new_template = frappe.get_doc({
|
||||||
# "doctype": "Quotation Template",
|
# "doctype": "Quotation Template",
|
||||||
# "template_name": data.get("templateName"),
|
# "template_name": data.get("templateName"),
|
||||||
|
|
@ -356,7 +356,7 @@ def create_estimate_template(data):
|
||||||
# "company": data.get("company", ""),
|
# "company": data.get("company", ""),
|
||||||
# "source_quotation": data.get("source_quotation", "")
|
# "source_quotation": data.get("source_quotation", "")
|
||||||
# })
|
# })
|
||||||
|
|
||||||
# for item in data.get("items", []):
|
# for item in data.get("items", []):
|
||||||
# item = json.loads(item) if isinstance(item, str) else item
|
# item = json.loads(item) if isinstance(item, str) else item
|
||||||
# new_template.append("items", {
|
# new_template.append("items", {
|
||||||
|
|
@ -367,7 +367,7 @@ def create_estimate_template(data):
|
||||||
# "discount_percentage": item.get("discountPercentage"),
|
# "discount_percentage": item.get("discountPercentage"),
|
||||||
# "rate": item.get("rate")
|
# "rate": item.get("rate")
|
||||||
# })
|
# })
|
||||||
|
|
||||||
# new_template.insert()
|
# new_template.insert()
|
||||||
# print("DEBUG: New estimate template created with name:", new_template.name)
|
# print("DEBUG: New estimate template created with name:", new_template.name)
|
||||||
# return build_success_response(new_template.as_dict())
|
# return build_success_response(new_template.as_dict())
|
||||||
|
|
@ -390,7 +390,7 @@ def upsert_estimate(data):
|
||||||
estimate = frappe.get_doc("Quotation", estimate_name)
|
estimate = frappe.get_doc("Quotation", estimate_name)
|
||||||
|
|
||||||
# Update fields
|
# Update fields
|
||||||
estimate.custom_installation_address = data.get("address_name")
|
estimate.custom_installation_address = data.get("address")
|
||||||
estimate.party_name = data.get("customer")
|
estimate.party_name = data.get("customer")
|
||||||
estimate.contact_person = data.get("contact_name")
|
estimate.contact_person = data.get("contact_name")
|
||||||
estimate.custom_requires_half_payment = data.get("requires_half_payment", 0)
|
estimate.custom_requires_half_payment = data.get("requires_half_payment", 0)
|
||||||
|
|
@ -419,7 +419,7 @@ def upsert_estimate(data):
|
||||||
new_estimate = frappe.get_doc({
|
new_estimate = frappe.get_doc({
|
||||||
"doctype": "Quotation",
|
"doctype": "Quotation",
|
||||||
"custom_requires_half_payment": data.get("requires_half_payment", 0),
|
"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",
|
"custom_current_status": "Draft",
|
||||||
"contact_email": data.get("contact_email"),
|
"contact_email": data.get("contact_email"),
|
||||||
"party_name": data.get("customer"),
|
"party_name": data.get("customer"),
|
||||||
|
|
@ -445,12 +445,12 @@ def upsert_estimate(data):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"DEBUG: Error in upsert_estimate: {str(e)}")
|
print(f"DEBUG: Error in upsert_estimate: {str(e)}")
|
||||||
return build_error_response(str(e), 500)
|
return build_error_response(str(e), 500)
|
||||||
|
|
||||||
def get_estimate_history(estimate_name):
|
def get_estimate_history(estimate_name):
|
||||||
"""Get the history of changes for a specific estimate."""
|
"""Get the history of changes for a specific estimate."""
|
||||||
|
|
||||||
return history
|
return history
|
||||||
|
|
||||||
# @frappe.whitelist()
|
# @frappe.whitelist()
|
||||||
# def get_estimate_counts():
|
# def get_estimate_counts():
|
||||||
# """Get specific counts of estimates based on their status."""
|
# """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"),
|
# "total_estimates": frappe.db.count("Quotation"),
|
||||||
# "ready_to_"
|
# "ready_to_"
|
||||||
# }
|
# }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ def on_update_after_submit(doc, method):
|
||||||
try:
|
try:
|
||||||
new_sales_order = make_sales_order(doc.name)
|
new_sales_order = make_sales_order(doc.name)
|
||||||
new_sales_order.custom_requires_half_payment = doc.requires_half_payment
|
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 = []
|
new_sales_order.payment_schedule = []
|
||||||
print("DEBUG: Setting payment schedule for Sales Order")
|
print("DEBUG: Setting payment schedule for Sales Order")
|
||||||
new_sales_order.set_payment_schedule()
|
new_sales_order.set_payment_schedule()
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
import frappe
|
import frappe
|
||||||
|
|
||||||
|
|
||||||
def after_insert(doc, method):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def on_submit(doc, method):
|
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
|
# Create Invoice and Project from Sales Order
|
||||||
try:
|
try:
|
||||||
print("Creating Project from Sales Order", doc.name)
|
print("Creating Project from Sales Order", doc.name)
|
||||||
|
|
|
||||||
|
|
@ -173,7 +173,6 @@ doc_events = {
|
||||||
"on_update_after_submit": "custom_ui.events.estimate.on_update_after_submit"
|
"on_update_after_submit": "custom_ui.events.estimate.on_update_after_submit"
|
||||||
},
|
},
|
||||||
"Sales Order": {
|
"Sales Order": {
|
||||||
"after_insert": "custom_ui.events.sales_order.after_insert",
|
|
||||||
"on_submit": "custom_ui.events.sales_order.on_submit",
|
"on_submit": "custom_ui.events.sales_order.on_submit",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -592,6 +592,7 @@ const saveDraft = async () => {
|
||||||
isSubmitting.value = true;
|
isSubmitting.value = true;
|
||||||
try {
|
try {
|
||||||
const data = {
|
const data = {
|
||||||
|
address: formData.address,
|
||||||
addressName: formData.addressName,
|
addressName: formData.addressName,
|
||||||
contactName: selectedContact.value.name,
|
contactName: selectedContact.value.name,
|
||||||
customer: selectedAddress.value?.customer?.name,
|
customer: selectedAddress.value?.customer?.name,
|
||||||
|
|
@ -768,7 +769,7 @@ watch(
|
||||||
} else if (newAddressQuery) {
|
} else if (newAddressQuery) {
|
||||||
await selectAddress(newAddressQuery);
|
await selectAddress(newAddressQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
formData.contact = estimate.value.contactPerson;
|
formData.contact = estimate.value.contactPerson;
|
||||||
selectedContact.value = contacts.value.find((c) => c.name === estimate.value.contactPerson) || null;
|
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);
|
console.error("Error loading quotation items:", error);
|
||||||
}
|
}
|
||||||
fetchProjectTemplates();
|
fetchProjectTemplates();
|
||||||
|
|
||||||
if (isNew.value) {
|
if (isNew.value) {
|
||||||
fetchTemplates();
|
fetchTemplates();
|
||||||
}
|
}
|
||||||
|
|
@ -839,7 +840,7 @@ onMounted(async () => {
|
||||||
} else if (addressQuery.value) {
|
} else if (addressQuery.value) {
|
||||||
await selectAddress(addressQuery.value);
|
await selectAddress(addressQuery.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the contact from the estimate
|
// Set the contact from the estimate
|
||||||
formData.contact = estimate.value.contactPerson;
|
formData.contact = estimate.value.contactPerson;
|
||||||
selectedContact.value = contacts.value.find((c) => c.name === estimate.value.contactPerson) || null;
|
selectedContact.value = contacts.value.find((c) => c.name === estimate.value.contactPerson) || null;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue