update doctype workflows and events for Sales Order creation from Quotation

This commit is contained in:
Casey 2025-12-08 16:58:16 -06:00
parent 483942d2ca
commit 02c48e6108
7 changed files with 106 additions and 81 deletions

View File

@ -104,6 +104,7 @@ def get_estimate_from_address(full_address):
@frappe.whitelist()
def send_estimate_email(estimate_name):
# def send_estimate_email_job(estimate_name):
try:
print("DEBUG: Sending estimate email for:", estimate_name)
quotation = frappe.get_doc("Quotation", estimate_name)
@ -150,10 +151,29 @@ def send_estimate_email(estimate_name):
quotation.custom_current_status = "Submitted"
quotation.custom_sent = 1
quotation.save()
quotation.submit()
updated_quotation = frappe.get_doc("Quotation", estimate_name)
print("DEBUG: Quotation submitted successfully.")
return build_success_response(updated_quotation.as_dict())
except Exception as e:
print(f"DEBUG: Error in send_estimate_email: {str(e)}")
return build_error_response(str(e), 500)
@frappe.whitelist(allow_guest=True)
def update_response(name, response):
"""Update the response for a given estimate."""
estimate = frappe.get_doc("Quotation", name)
accepted = True if response == "Accepted" else False
new_status = "Estimate Accepted" if accepted else "Lost"
estimate.custom_response = response
estimate.custom_current_status = new_status
estimate.custom_followup_needed = 1 if response == "Requested call" else 0
estimate.flags.ignore_permissions = True
estimate.save()
frappe.db.commit()
@frappe.whitelist()
def upsert_estimate(data):
@ -193,6 +213,7 @@ def upsert_estimate(data):
new_estimate = frappe.get_doc({
"doctype": "Quotation",
"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",
@ -211,13 +232,3 @@ def upsert_estimate(data):
print(f"DEBUG: Error in upsert_estimate: {str(e)}")
return build_error_response(str(e), 500)
@frappe.whitelist()
def lock_estimate(estimate_name):
"""Lock an estimate to prevent further edits."""
try:
estimate = frappe.get_doc("Quotation", estimate_name)
estimate.submit()
final_estimate = frappe.get_doc("Quotation", estimate_name)
return build_success_response(final_estimate.as_dict())
except Exception as e:
return build_error_response(str(e), 500)

View File

@ -0,0 +1,17 @@
import frappe
from custom_ui.db_utils import build_success_response, build_error_response
from erpnext.selling.doctype.quotation.quotation import make_sales_order
@frappe.whitelist()
def create_sales_order_from_estimate(estimate_name):
"""Create a Sales Order from a given Estimate (Quotation)."""
try:
estimate = frappe.get_doc("Quotation", estimate_name)
if estimate.custom_current_status != "Estimate Accepted":
raise Exception("Estimate must be accepted to create a Sales Order.")
new_sales_order = make_sales_order(estimate_name)
new_sales_order.custom_requires_half_payment = estimate.requires_half_payment
new_sales_order.insert()
return build_success_response(new_sales_order.as_dict())
except Exception as e:
return build_error_response(str(e), 500)

View File

@ -1,4 +1,5 @@
import frappe
from erpnext.selling.doctype.quotation.quotation import make_sales_order
def after_insert(doc, method):
try:
@ -14,7 +15,7 @@ def after_insert(doc, method):
frappe.log_error(f"Error in estimate after_insert: {str(e)}", "Estimate Hook Error")
def after_save(doc, method):
if not doc.custom_sent:
if not doc.custom_sent or not doc.custom_response:
return
print("DEBUG: Quotation has been sent, updating Address status")
address_doc = frappe.get_doc("Address", doc.custom_installation_address)

View File

@ -7,14 +7,11 @@ def after_insert(doc, method):
address_doc = frappe.get_doc("Address", doc.address)
address_doc.custom_onsite_meeting_scheduled = "In Progress"
address_doc.save()
if doc.status == "Completed":
address_doc = frappe.get_doc("Address", doc.address)
address_doc.custom_onsite_meeting_scheduled = "Completed"
address_doc.save()
def on_update(doc, method):
print("DEBUG: On Update Triggered for On-Site Meeting")
def after_save(doc, method):
print("DEBUG: After Save Triggered for On-Site Meeting")
if doc.status == "Completed":
print("DEBUG: Meeting marked as Completed, updating Address status")
address_doc = frappe.get_doc("Address", doc.address)
address_doc.custom_onsite_meeting_scheduled = "Completed"
address_doc.save()

View File

@ -1,10 +0,0 @@
import frappe
def after_insert(doc, method):
address_title = doc.custom_installation_address
address_name = frappe.db.get_value("Address", fieldname="name", filters={"address_title": address_title})
if address_name:
address_doc = frappe.get_doc("Address", address_name)
address_doc.custom_estimate_sent_status = "In Progress"
address_doc.save()

View File

@ -161,7 +161,7 @@ add_to_apps_screen = [
doc_events = {
"On-Site Meeting": {
"after_insert": "custom_ui.events.onsite_meeting.after_insert",
"on_update": "custom_ui.events.onsite_meeting.on_update"
"after_save": "custom_ui.events.onsite_meeting.after_save"
},
"Address": {
"after_insert": "custom_ui.events.address.after_insert"

View File

@ -169,6 +169,15 @@ def add_custom_fields():
options="Employee",
insert_after="status"
)
],
"Quotation": [
dict(
fieldname="requires_half_payment",
label="Requires Half Payment",
fieldtype="Check",
default=0,
insert_after="custom_installation_address"
)
]
}