I can not share the repository due to some contract with the client but I can share one example code.
from __future__ import unicode_literals
import frappe
import erpnext
import json
from frappe.utils import getdate, today, add_days,cstr,cint
@frappe.whitelist()
def generate_response(_type, status=None, message=None, data=None, error=None):
response = {}
if _type == "S":
if status:
frappe.response["status_code"] = int(status)
else:
frappe.response["status_code"] = 200
frappe.response["msg"] = message
frappe.response["data"] = data
else:
error_log = frappe.log_error(frappe.get_traceback())
if status:
frappe.response["status_code"] = status
else:
frappe.response["status_code"] = 500
if message:
frappe.response["msg"] = message
else:
frappe.response["msg"] = "Something Went Wrong"
frappe.response["msg"] = message
frappe.response["data"] = []
@frappe.whitelist()
def create_customer(customer_data=None,address_data=None):
try:
customer_data = json.loads(customer_data)
res = get_customer(customer_data.get("mobile_no"))
if not customer_data.get("customer_name"):
return generate_response("S","400",message="Customer Name Required For Customer Registration",data=[])
if not customer_data.get("mobile_no"):
return generate_response("S","400",message="Mobile No Is Required For Customer Registration",data=[])
if res:
return generate_response("S","409",message="Customer Already Registered",data=res)
doc = frappe.get_doc(dict(
doctype = "Customer",
customer_name = customer_data.get("customer_name"),
mobile_no = customer_data.get("mobile_no"),
email_id = customer_data.get("email_id")
)).insert(ignore_permissions = True)
if address_data:
address_data = json.loads(address_data)
if address_data.values():
create_address(doc.name,address_data)
return generate_response("S","200",message="Customer Successfully Created",data=doc)
else:
return generate_response("S","200",message="Customer Successfully Created",data=doc)
except Exception as e:
return generate_response("F", message="Something Wrong Please Check With Administrator",error=e)
def create_address(customer,address_data=None):
address_doc = frappe.get_doc(dict(
doctype = "Address",
address_line1 = address_data.get("address_line1"),
address_line2 = address_data.get("address_line2"),
city = address_data.get("city"),
state = address_data.get("state"),
country = address_data.get("country"),
pincode = address_data.get("pincode")
))
address_doc.append("links",{
"link_doctype" : "Customer",
"link_name" : customer
})
address_doc.insert(ignore_permissions = True)
@frappe.whitelist()
def create_order(order_data=None):
try:
customer_id = ""
order_data = json.loads(order_data)
validation_result = validate_order(order_data)
if not validation_result:
return validation_result
if order_data.get("mobile_no"):
customer_id = get_customer(order_data)
if not customer_id:
customer_id = make_customer(customer_data=order_data)
order = frappe.get_doc(dict(
doctype = "Sales Order",
customer = customer_id,
delivery_date = add_days(order_data.get("posting_date"),5),
company = frappe.db.get_value("Global Defaults","Global Defaults","default_company"),
posting_date = today(),
items = order_data.get("items"),
selling_price_list = "Standard"
))
order.run_method("set_missing_values")
order.ignore_mandatory = True
order.insert(ignore_permissions = True)
if order:
return generate_response("S","200",message="Order Successfully Places",data=order)
except Exception as e:
return generate_response("F", message="Something Wrong Please Check With Administrator",error=e)
def validate_order(order_data):
field_lable_map = {
"mobile_no":"Mobile Number",
"customer_name" : "Customer Name",
"items" : "Items"
}
field_val_list = ["mobile_no","customer_name","items"]
for row in field_val_list:
if not order_data.get(row):
msg = "{0} Is Required For Place Order".format(field_lable_map.get(row))
return generate_response("S","400",message=msg,data=order_data)
item_exist = True
item_code = ''
for item in order_data.get("items"):
# return item.get("item_code")
if frappe.db.exists("Item",item.get("item_code")):
continue
else:
item_exist = False
item_code = item.get("item_code")
break
if not item_exist:
return generate_response("S","404",message="{0} Item Code(SKU) Not Match".format(item_code),data=order_data)
return False
@frappe.whitelist()
def get_customer(mobile_no):
customers = frappe.get_all("Customer",filters={"mobile_no":mobile_no},fields=["name"])
if customers:
return customers[0].name
else:
return False
def make_customer(customer_data):
doc = frappe.get_doc(dict(
doctype = "Customer",
customer_name = customer_data.get("customer_name"),
mobile_no = customer_data.get("mobile_no"),
email_id = customer_data.get("email_id")
)).insert(ignore_permissions = True)
return doc.name