custom_ui/custom_ui/events/estimate.py
2025-12-23 08:21:12 -06:00

49 lines
No EOL
2.5 KiB
Python

import frappe
from erpnext.selling.doctype.quotation.quotation import make_sales_order
def after_insert(doc, method):
try:
print("DEBUG: after_insert hook triggered for Quotation:", doc.name)
if not doc.custom_installation_address:
print("ERROR: custom_installation_address is empty")
return
address_doc = frappe.get_doc("Address", doc.custom_installation_address)
address_doc.custom_estimate_sent_status = "In Progress"
address_doc.save()
except Exception as e:
print("ERROR in after_insert hook:", str(e))
frappe.log_error(f"Error in estimate after_insert: {str(e)}", "Estimate Hook Error")
def after_save(doc, method):
print("DEBUG: after_save hook triggered for Quotation:", doc.name)
if doc.custom_sent and doc.custom_response:
print("DEBUG: Quotation has been sent, updating Address status")
address_doc = frappe.get_doc("Address", doc.custom_installation_address)
address_doc.custom_estimate_sent_status = "Completed"
address_doc.save()
def on_update_after_submit(doc, method):
print("DEBUG: on_update_after_submit hook triggered for Quotation:", doc.name)
print("DEBUG: Current custom_current_status:", doc.custom_current_status)
if doc.custom_current_status == "Estimate Accepted":
doc.custom_current_status = "Won"
print("DEBUG: Creating Sales Order from accepted Estimate")
address_doc = frappe.get_doc("Address", doc.customer_address)
address_doc.custom_estimate_sent_status = "Completed"
address_doc.save()
try:
new_sales_order = make_sales_order(doc.name)
new_sales_order.custom_requires_half_payment = doc.requires_half_payment
new_sales_order.payment_schedule = []
print("DEBUG: Setting payment schedule for Sales Order")
new_sales_order.set_payment_schedule()
print("DEBUG: Inserting Sales Order:", new_sales_order.as_dict())
new_sales_order.delivery_date = new_sales_order.transaction_date
new_sales_order.insert()
print("DEBUG: Submitting Sales Order")
new_sales_order.submit()
frappe.db.commit()
print("DEBUG: Sales Order created successfully:", new_sales_order.name)
except Exception as e:
print("ERROR creating Sales Order from Estimate:", str(e))
frappe.log_error(f"Error creating Sales Order from Estimate {doc.name}: {str(e)}", "Estimate on_update_after_submit Error")