add global loading state, update to use real data for clients table
This commit is contained in:
parent
2cfe7ed8e6
commit
464c62d1e5
12 changed files with 1075 additions and 194 deletions
49
custom_ui/api/db.py
Normal file
49
custom_ui/api/db.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import frappe, json
|
||||
|
||||
@frappe.whitelist()
|
||||
def upsert_client(data):
|
||||
data = json.loads(data)
|
||||
"""
|
||||
Upsert a document in the database.
|
||||
If a document with the same name exists, it will be updated.
|
||||
Otherwise, a new document will be created.
|
||||
"""
|
||||
customer = frappe.db.exists("Customer", {"customer_name": data.get("customer_name")})
|
||||
if not customer:
|
||||
customer_doc = frappe.get_doc({
|
||||
"doctype": "Customer",
|
||||
"customer_name": data.get("customer_name"),
|
||||
"customer_type": data.get("customer_type")
|
||||
}).insert(ignore_permissions=True)
|
||||
customer = customer_doc.name
|
||||
else:
|
||||
customer_doc = frappe.get_doc("Customer", customer)
|
||||
filters = {
|
||||
"address_line1": data.get("address_line1"),
|
||||
"city": data.get("city"),
|
||||
"state": data.get("state"),
|
||||
"country": "US",
|
||||
"pincode": data.get("pincode")
|
||||
}
|
||||
existing_address = frappe.db.exists("Address", filters)
|
||||
if existing_address:
|
||||
frappe.throw(f"Address already exists for customer {data.get('customer_name')}.", frappe.ValidationError)
|
||||
address_doc = frappe.get_doc({
|
||||
"doctype": "Address",
|
||||
"address_line1": data.get("address_line1"),
|
||||
"city": data.get("city"),
|
||||
"state": data.get("state"),
|
||||
"country": "US",
|
||||
"pincode": data.get("pincode"),
|
||||
}).insert(ignore_permissions=True)
|
||||
link = {
|
||||
"link_doctype": "Customer",
|
||||
"link_name": customer
|
||||
}
|
||||
address_doc.append("links", link)
|
||||
address_doc.save(ignore_permissions=True)
|
||||
|
||||
return {
|
||||
"customer": customer.name,
|
||||
"address": address_doc.name
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue