create estimate

This commit is contained in:
Casey 2025-11-26 16:47:53 -06:00
parent 2ea20a86e3
commit afa161a0cf
15 changed files with 1234 additions and 435 deletions

View file

@ -1,5 +1,5 @@
import frappe, json
from custom_ui.db_utils import process_query_conditions, build_datatable_dict, get_count_or_filters, build_success_response
from custom_ui.db_utils import process_query_conditions, build_datatable_dict, get_count_or_filters, build_success_response, build_error_response
# ===============================================================================
# ESTIMATES & INVOICES API METHODS
@ -47,15 +47,72 @@ def get_estimate_table_data(filters={}, sortings=[], page=1, page_size=10):
return build_success_response(table_data_dict)
@frappe.whitelist()
def get_quotation_items():
"""Get all available quotation items."""
try:
items = frappe.get_all("Item", fields=["*"], filters={"item_group": "SNW-S"})
return build_success_response(items)
except Exception as e:
return build_error_response(str(e), 500)
@frappe.whitelist()
def get_estimate(estimate_name):
"""Get detailed information for a specific estimate."""
try:
estimate = frappe.get_doc("Quotation", estimate_name)
return build_success_response(estimate.as_dict())
except Exception as e:
return build_error_response(str(e), 500)
@frappe.whitelist()
def upsert_estimate(data):
"""Create or update an estimate."""
# TODO: Implement estimate creation/update logic
pass
@frappe.whitelist()
def get_estimate_items():
items = frappe.db.get_all("Quotation Item", fields=["*"])
return build_success_response(items)
@frappe.whitelist()
def upsert_invoice(data):
"""Create or update an invoice."""
# TODO: Implement invoice creation/update logic
pass
def get_estimate_from_address(full_address):
quotation = frappe.db.sql("""
SELECT q.name, q.custom_installation_address
FROM `tabQuotation` q
JOIN `tabAddress` a
ON q.custom_installation_address = a.name
WHERE a.full_address =%s
""", (full_address,), as_dict=True)
if quotation:
return build_success_response(quotation)
else:
return build_error_response("No quotation found for the given address.", 404)
@frappe.whitelist()
def upsert_estimate(data):
"""Create or update an estimate."""
print("DOIFJSEOFJISLFK")
try:
data = json.loads(data) if isinstance(data, str) else data
print("DEBUG: Upsert estimate data received:", data)
address_name = frappe.get_value("Address", fieldname="name", filters={"full_address": data.get("address")})
new_estimate = frappe.get_doc({
"doctype": "Quotation",
"custom_installation_address": address_name,
"contact_email": data.get("contact_email"),
"party_name": data.get("contact_name"),
"customer_name": data.get("customer_name"),
})
for item in data.get("items", []):
item = json.loads(item) if isinstance(item, str) else item
new_estimate.append("items", {
"item_code": item.get("item_code"),
"qty": item.get("qty"),
})
new_estimate.insert()
print("DEBUG: New estimate created with name:", new_estimate.name)
return build_success_response(new_estimate.as_dict())
except Exception as e:
return build_error_response(str(e), 500)