fix bugs and stripe

This commit is contained in:
Casey 2026-02-04 16:14:17 -06:00
parent 9a7e3fe740
commit 21a256a26f
17 changed files with 542 additions and 88 deletions

View file

@ -2,9 +2,9 @@ import frappe, json
from frappe.utils.pdf import get_pdf
from custom_ui.api.db.general import get_doc_history
from custom_ui.db_utils import DbUtils, process_query_conditions, build_datatable_dict, get_count_or_filters, build_success_response, build_error_response
from werkzeug.wrappers import Response
from custom_ui.api.db.clients import check_if_customer, convert_lead_to_customer
from custom_ui.services import DbService, ClientService, AddressService, ContactService, EstimateService, ItemService
from frappe.email.doctype.email_template.email_template import get_email_template
# ===============================================================================
# ESTIMATES & INVOICES API METHODS
@ -191,7 +191,7 @@ def send_estimate_email(estimate_name):
print("DEBUG: Sending estimate email for:", estimate_name)
quotation = frappe.get_doc("Quotation", estimate_name)
# Get recipient email
if not DbService.exists("Contact", quotation.contact_person):
return build_error_response("No email found for the customer.", 400)
party = ContactService.get_or_throw(quotation.contact_person)
@ -210,21 +210,71 @@ def send_estimate_email(estimate_name):
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, {"name": quotation.name})
subject = frappe.render_template(template.subject, {"doc": quotation})
print("DEBUG: Message: ", message)
print("DEBUG: Subject: ", subject)
# Get customer name
customer_name = party.first_name or party.name or "Valued Customer"
if party.last_name:
customer_name = f"{party.first_name} {party.last_name}"
# Get full address
full_address = "Address not specified"
if quotation.custom_job_address:
address_doc = frappe.get_doc("Address", quotation.custom_job_address)
full_address = address_doc.full_address or address_doc.address_line1 or "Address not specified"
# Format price
price = frappe.utils.fmt_money(quotation.grand_total, currency=quotation.currency)
# Get additional notes
additional = quotation.terms or ""
# Get company phone
company_phone = ""
if quotation.company:
company_doc = frappe.get_doc("Company", quotation.company)
company_phone = getattr(company_doc, 'phone_no', '') or getattr(company_doc, 'phone', '')
# Get base URL
base_url = frappe.utils.get_url()
# Get letterhead image
letterhead_image = ""
if quotation.letter_head:
letterhead_doc = frappe.get_doc("Letter Head", quotation.letter_head)
if letterhead_doc.image:
letterhead_image = frappe.utils.get_url() + letterhead_doc.image
# Prepare template context
template_context = {
"company": quotation.company,
"customer_name": customer_name,
"price": price,
"address": full_address,
"additional": additional,
"company_phone": company_phone,
"base_url": base_url,
"estimate_name": quotation.name,
"letterhead_image": letterhead_image
}
# Render the email template
template_path = "custom_ui/templates/emails/general_estimation.html"
message = frappe.render_template(template_path, template_context)
subject = f"Estimate from {quotation.company} - {quotation.name}"
print("DEBUG: Subject:", subject)
print("DEBUG: Sending email to:", email)
# Generate PDF attachment
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.")
# Send email
frappe.sendmail(
recipients=email,
subject=subject,
content=message,
message=message,
doctype="Quotation",
name=quotation.name,
read_receipt=1,
@ -232,11 +282,14 @@ def send_estimate_email(estimate_name):
attachments=[{"fname": f"{quotation.name}.pdf", "fcontent": pdf}]
)
print(f"DEBUG: Email sent to {email} successfully.")
# Update quotation status
quotation.custom_current_status = "Submitted"
quotation.custom_sent = 1
quotation.save()
quotation.submit()
frappe.db.commit()
updated_quotation = frappe.get_doc("Quotation", estimate_name)
return build_success_response(updated_quotation.as_dict())
except Exception as e:
@ -269,45 +322,6 @@ def manual_response(name, response):
return build_error_response(str(e), 500)
@frappe.whitelist(allow_guest=True)
def update_response(name, response):
"""Update the response for a given estimate."""
print("DEBUG: RESPONSE RECEIVED:", name, response)
try:
if not frappe.db.exists("Quotation", name):
raise Exception("Estimate not found.")
estimate = frappe.get_doc("Quotation", name)
if estimate.docstatus != 1:
raise Exception("Estimate must be submitted to update response.")
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.status = "Ordered" if accepted else "Closed"
estimate.flags.ignore_permissions = True
print("DEBUG: Updating estimate with response:", response, "and status:", new_status)
estimate.save()
if accepted:
template = "custom_ui/templates/estimates/accepted.html"
# if check_if_customer(estimate.party_name):
# print("DEBUG: Party is already a customer:", estimate.party_name)
# else:
# print("DEBUG: Converting lead to customer for party:", estimate.party_name)
# convert_lead_to_customer(estimate.party_name)
elif response == "Requested call":
template = "custom_ui/templates/estimates/request-call.html"
else:
template = "custom_ui/templates/estimates/rejected.html"
html = frappe.render_template(template, {"doc": estimate})
return Response(html, mimetype="text/html")
except Exception as e:
template = "custom_ui/templates/estimates/error.html"
html = frappe.render_template(template, {"error": str(e)})
return Response(html, mimetype="text/html")
@frappe.whitelist()
def get_estimate_templates(company):
"""Get available estimate templates."""
@ -462,6 +476,7 @@ def upsert_estimate(data):
estimate.append("items", {
"item_code": item.get("item_code"),
"qty": item.get("qty"),
"rate": item.get("rate"),
"discount_amount": item.get("discount_amount") or item.get("discountAmount", 0),
"discount_percentage": item.get("discount_percentage") or item.get("discountPercentage", 0)
})
@ -506,6 +521,7 @@ def upsert_estimate(data):
new_estimate.append("items", {
"item_code": item.get("item_code"),
"qty": item.get("qty"),
"rate": item.get("rate"),
"discount_amount": item.get("discount_amount") or item.get("discountAmount", 0),
"discount_percentage": item.get("discount_percentage") or item.get("discountPercentage", 0)
})