add template get

This commit is contained in:
Casey 2026-01-02 15:55:27 -06:00
parent 702e718431
commit cb59dd65ca
5 changed files with 246 additions and 24 deletions

View file

@ -260,8 +260,81 @@ def update_response(name, response):
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."""
filters = {"is_active": 1}
if company:
filters["company"] = company
try:
print("DEBUG: Fetching estimate templates for company:", company)
templates = frappe.get_all("Quotation Template", fields=["name", "is_active", "description"], filters=filters)
result = []
if not templates:
print("DEBUG: No templates found.")
return build_success_response(result)
for template in templates:
items = frappe.get_all("Quotation Template Item",
fields=["item_code", "item_name", "description", "qty", "discount_percentage", "rate"],
filters={"parent": template.name},
order_by="idx")
# Map fields to camelCase as requested
mapped_items = []
for item in items:
mapped_items.append({
"itemCode": item.item_code,
"itemName": item.item_name,
"description": item.description,
"quantity": item.qty,
"discountPercentage": item.discount_percentage,
"rate": item.rate
})
result.append({
"templateName": template.name,
"active": template.active,
"description": template.description,
"items": mapped_items
})
return build_success_response(result)
except Exception as e:
return build_error_response(str(e), 500)
@frappe.whitelist()
def create_template(data):
"""Create a new estimate template."""
try:
data = json.loads(data) if isinstance(data, str) else data
print("DEBUG: Creating estimate template with data:", data)
new_template = frappe.get_doc({
"doctype": "Quotation Template",
"template_name": data.get("templateName"),
"is_active": data.get("active", 1),
"description": data.get("description", ""),
"company": data.get("company", ""),
"source_quotation": data.get("source_quotation", "")
})
for item in data.get("items", []):
item = json.loads(item) if isinstance(item, str) else item
new_template.append("items", {
"item_code": item.get("itemCode"),
"item_name": item.get("itemName"),
"description": item.get("description"),
"qty": item.get("quantity"),
"discount_percentage": item.get("discountPercentage"),
"rate": item.get("rate")
})
new_template.insert()
print("DEBUG: New estimate template created with name:", new_template.name)
return build_success_response(new_template.as_dict())
except Exception as e:
return build_error_response(str(e), 500)
@frappe.whitelist()
def upsert_estimate(data):