This commit is contained in:
Casey 2025-12-03 11:51:59 -06:00
parent 07c1181d6e
commit 041e9f5461
9 changed files with 371 additions and 249 deletions

View file

@ -1,4 +1,5 @@
import frappe, json
from frappe.utils.pdf import get_pdf
from custom_ui.db_utils import process_query_conditions, build_datatable_dict, get_count_or_filters, build_success_response, build_error_response
# ===============================================================================
@ -88,6 +89,69 @@ def get_estimate_from_address(full_address):
# return build_success_response(quotation)
# else:
# return build_error_response("No quotation found for the given address.", 404)
# @frappe.whitelist()
# def send_estimate_email(estimate_name):
# print("DEBUG: Queuing email send job for estimate:", estimate_name)
# frappe.enqueue(
# "custom_ui.api.db.estimates.send_estimate_email_job",
# estimate_name=estimate_name,
# queue="long", # or "default"
# timeout=600,
# )
# return build_success_response("Email queued for sending.")
@frappe.whitelist()
def send_estimate_email(estimate_name):
# def send_estimate_email_job(estimate_name):
print("DEBUG: Sending estimate email for:", estimate_name)
quotation = frappe.get_doc("Quotation", estimate_name)
party_exists = frappe.db.exists(quotation.quotation_to, quotation.party_name)
if not party_exists:
return build_error_response("No email found for the customer.", 400)
party = frappe.get_doc(quotation.quotation_to, quotation.party_name)
email = None
if (getattr(party, 'email_id', None)):
email = party.email_id
elif (getattr(party, 'contact_ids', None) and len(party.email_ids) > 0):
primary = next((e for e in party.email_ids if e.is_primary), None)
email = primary.email_id if primary else party.email_ids[0].email_id
if not email and quotation.custom_installation_address:
address = frappe.get_doc("Address", quotation.custom_installation_address)
email = getattr(address, 'email_id', None)
if not email:
return build_error_response("No email found for the customer or address.", 400)
# email = "casey@shilohcode.com"
template_name = "Quote with Actions - SNW"
template = frappe.get_doc("Email Template", template_name)
message = frappe.render_template(template.response, {"doc": quotation})
subject = frappe.render_template(template.subject, {"doc": quotation})
html = frappe.get_print("Quotation", quotation.name, print_format="Quotation - SNW - Standard", letterhead=True)
print("DEBUG: Generated HTML for PDF.")
pdf = get_pdf(html)
print("DEBUG: Generated PDF for email attachment.")
frappe.sendmail(
recipients=email,
subject=subject,
content=message,
doctype="Quotation",
name=quotation.name,
read_receipt=1,
print_letterhead=1,
attachments=[{"fname": f"{quotation.name}.pdf", "fcontent": pdf}]
)
print(f"DEBUG: Email sent to {email} successfully.")
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())
@frappe.whitelist()
def upsert_estimate(data):
@ -129,6 +193,7 @@ def upsert_estimate(data):
"custom_installation_address": data.get("address_name"),
"contact_email": data.get("contact_email"),
"party_name": data.get("contact_name"),
"company": "Sprinklers Northwest",
"customer_name": data.get("customer_name"),
})
for item in data.get("items", []):
@ -142,4 +207,15 @@ def upsert_estimate(data):
return build_success_response(new_estimate.as_dict())
except Exception as e:
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

@ -9,7 +9,6 @@ def after_insert(doc, method):
address_doc = frappe.get_doc("Address", doc.custom_installation_address)
address_doc.custom_estimate_sent_status = "In Progress"
address_doc.save()
print("DEBUG: Address status updated successfully")
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")

View file

@ -10,4 +10,11 @@ def after_insert(doc, method):
if doc.status == "Completed":
address_doc = frappe.get_doc("Address", doc.address)
address_doc.custom_onsite_meeting_scheduled = "Completed"
address_doc.save()
address_doc.save()
def on_update(doc, method):
print("DEBUG: On Update 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"

View file

@ -10,9 +10,9 @@ def after_insert(doc, method):
def after_save(doc, method):
address_title = doc.custom_installation_address
address_name = frappe.db.get_value("Address", fieldname="name", filters={"address_title": address_title})
if doc.custome_sent and address_name:
address_doc = frappe.get_doc("Address", address_name)
address_doc.custom_quotation_sent = "Completed"
address_doc.save()
if not doc.custom_sent:
return
print("DEBUG: Quotation has been sent, updating Address status")
address_doc = frappe.get_doc("Address", doc.custom_installation_address)
address_doc.custom_quotation_sent = "Completed"
address_doc.save()

View file

@ -6,7 +6,6 @@ from .utils import create_module
def after_install():
create_module()
add_custom_fields()
update_onsite_meeting_fields()
frappe.db.commit()
# Proper way to refresh metadata
@ -14,6 +13,7 @@ def after_install():
frappe.reload_doctype("Address")
frappe.clear_cache(doctype="On-Site Meeting")
frappe.reload_doctype("On-Site Meeting")
update_onsite_meeting_fields()
update_address_fields()
build_frontend()