style: format code with black
This commit is contained in:
@@ -12,26 +12,30 @@ def verify_request():
|
||||
woocommerce_settings = frappe.get_doc("Woocommerce Settings")
|
||||
sig = base64.b64encode(
|
||||
hmac.new(
|
||||
woocommerce_settings.secret.encode('utf8'),
|
||||
frappe.request.data,
|
||||
hashlib.sha256
|
||||
woocommerce_settings.secret.encode("utf8"), frappe.request.data, hashlib.sha256
|
||||
).digest()
|
||||
)
|
||||
|
||||
if frappe.request.data and \
|
||||
not sig == frappe.get_request_header("X-Wc-Webhook-Signature", "").encode():
|
||||
frappe.throw(_("Unverified Webhook Data"))
|
||||
if (
|
||||
frappe.request.data
|
||||
and not sig == frappe.get_request_header("X-Wc-Webhook-Signature", "").encode()
|
||||
):
|
||||
frappe.throw(_("Unverified Webhook Data"))
|
||||
frappe.set_user(woocommerce_settings.creation_user)
|
||||
|
||||
|
||||
@frappe.whitelist(allow_guest=True)
|
||||
def order(*args, **kwargs):
|
||||
try:
|
||||
_order(*args, **kwargs)
|
||||
except Exception:
|
||||
error_message = frappe.get_traceback()+"\n\n Request Data: \n"+json.loads(frappe.request.data).__str__()
|
||||
error_message = (
|
||||
frappe.get_traceback() + "\n\n Request Data: \n" + json.loads(frappe.request.data).__str__()
|
||||
)
|
||||
frappe.log_error(error_message, "WooCommerce Error")
|
||||
raise
|
||||
|
||||
|
||||
def _order(*args, **kwargs):
|
||||
woocommerce_settings = frappe.get_doc("Woocommerce Settings")
|
||||
if frappe.flags.woocomm_test_order_data:
|
||||
@@ -43,7 +47,7 @@ def _order(*args, **kwargs):
|
||||
try:
|
||||
order = json.loads(frappe.request.data)
|
||||
except ValueError:
|
||||
#woocommerce returns 'webhook_id=value' for the first request which is not JSON
|
||||
# woocommerce returns 'webhook_id=value' for the first request which is not JSON
|
||||
order = frappe.request.data
|
||||
event = frappe.get_request_header("X-Wc-Webhook-Event")
|
||||
|
||||
@@ -51,7 +55,7 @@ def _order(*args, **kwargs):
|
||||
return "success"
|
||||
|
||||
if event == "created":
|
||||
sys_lang = frappe.get_single("System Settings").language or 'en'
|
||||
sys_lang = frappe.get_single("System Settings").language or "en"
|
||||
raw_billing_data = order.get("billing")
|
||||
raw_shipping_data = order.get("shipping")
|
||||
customer_name = raw_billing_data.get("first_name") + " " + raw_billing_data.get("last_name")
|
||||
@@ -59,6 +63,7 @@ def _order(*args, **kwargs):
|
||||
link_items(order.get("line_items"), woocommerce_settings, sys_lang)
|
||||
create_sales_order(order, woocommerce_settings, customer_name, sys_lang)
|
||||
|
||||
|
||||
def link_customer_and_address(raw_billing_data, raw_shipping_data, customer_name):
|
||||
customer_woo_com_email = raw_billing_data.get("email")
|
||||
customer_exists = frappe.get_value("Customer", {"woocommerce_email": customer_woo_com_email})
|
||||
@@ -77,9 +82,14 @@ def link_customer_and_address(raw_billing_data, raw_shipping_data, customer_name
|
||||
|
||||
if customer_exists:
|
||||
frappe.rename_doc("Customer", old_name, customer_name)
|
||||
for address_type in ("Billing", "Shipping",):
|
||||
for address_type in (
|
||||
"Billing",
|
||||
"Shipping",
|
||||
):
|
||||
try:
|
||||
address = frappe.get_doc("Address", {"woocommerce_email": customer_woo_com_email, "address_type": address_type})
|
||||
address = frappe.get_doc(
|
||||
"Address", {"woocommerce_email": customer_woo_com_email, "address_type": address_type}
|
||||
)
|
||||
rename_address(address, customer)
|
||||
except (
|
||||
frappe.DoesNotExistError,
|
||||
@@ -92,6 +102,7 @@ def link_customer_and_address(raw_billing_data, raw_shipping_data, customer_name
|
||||
create_address(raw_shipping_data, customer, "Shipping")
|
||||
create_contact(raw_billing_data, customer)
|
||||
|
||||
|
||||
def create_contact(data, customer):
|
||||
email = data.get("email", None)
|
||||
phone = data.get("phone", None)
|
||||
@@ -111,14 +122,12 @@ def create_contact(data, customer):
|
||||
if email:
|
||||
contact.add_email(email, is_primary=1)
|
||||
|
||||
contact.append("links", {
|
||||
"link_doctype": "Customer",
|
||||
"link_name": customer.name
|
||||
})
|
||||
contact.append("links", {"link_doctype": "Customer", "link_name": customer.name})
|
||||
|
||||
contact.flags.ignore_mandatory = True
|
||||
contact.save()
|
||||
|
||||
|
||||
def create_address(raw_data, customer, address_type):
|
||||
address = frappe.new_doc("Address")
|
||||
|
||||
@@ -132,14 +141,12 @@ def create_address(raw_data, customer, address_type):
|
||||
address.pincode = raw_data.get("postcode")
|
||||
address.phone = raw_data.get("phone")
|
||||
address.email_id = customer.woocommerce_email
|
||||
address.append("links", {
|
||||
"link_doctype": "Customer",
|
||||
"link_name": customer.name
|
||||
})
|
||||
address.append("links", {"link_doctype": "Customer", "link_name": customer.name})
|
||||
|
||||
address.flags.ignore_mandatory = True
|
||||
address.save()
|
||||
|
||||
|
||||
def rename_address(address, customer):
|
||||
old_address_title = address.name
|
||||
new_address_title = customer.name + "-" + address.address_type
|
||||
@@ -148,12 +155,13 @@ def rename_address(address, customer):
|
||||
|
||||
frappe.rename_doc("Address", old_address_title, new_address_title)
|
||||
|
||||
|
||||
def link_items(items_list, woocommerce_settings, sys_lang):
|
||||
for item_data in items_list:
|
||||
item_woo_com_id = cstr(item_data.get("product_id"))
|
||||
|
||||
if not frappe.db.get_value("Item", {"woocommerce_id": item_woo_com_id}, 'name'):
|
||||
#Create Item
|
||||
if not frappe.db.get_value("Item", {"woocommerce_id": item_woo_com_id}, "name"):
|
||||
# Create Item
|
||||
item = frappe.new_doc("Item")
|
||||
item.item_code = _("woocommerce - {0}", sys_lang).format(item_woo_com_id)
|
||||
item.stock_uom = woocommerce_settings.uom or _("Nos", sys_lang)
|
||||
@@ -164,6 +172,7 @@ def link_items(items_list, woocommerce_settings, sys_lang):
|
||||
item.flags.ignore_mandatory = True
|
||||
item.save()
|
||||
|
||||
|
||||
def create_sales_order(order, woocommerce_settings, customer_name, sys_lang):
|
||||
new_sales_order = frappe.new_doc("Sales Order")
|
||||
new_sales_order.customer = customer_name
|
||||
@@ -185,12 +194,12 @@ def create_sales_order(order, woocommerce_settings, customer_name, sys_lang):
|
||||
|
||||
frappe.db.commit()
|
||||
|
||||
|
||||
def set_items_in_sales_order(new_sales_order, woocommerce_settings, order, sys_lang):
|
||||
company_abbr = frappe.db.get_value('Company', woocommerce_settings.company, 'abbr')
|
||||
company_abbr = frappe.db.get_value("Company", woocommerce_settings.company, "abbr")
|
||||
|
||||
default_warehouse = _("Stores - {0}", sys_lang).format(company_abbr)
|
||||
if not frappe.db.exists("Warehouse", default_warehouse) \
|
||||
and not woocommerce_settings.warehouse:
|
||||
if not frappe.db.exists("Warehouse", default_warehouse) and not woocommerce_settings.warehouse:
|
||||
frappe.throw(_("Please set Warehouse in Woocommerce Settings"))
|
||||
|
||||
for item in order.get("line_items"):
|
||||
@@ -199,28 +208,44 @@ def set_items_in_sales_order(new_sales_order, woocommerce_settings, order, sys_l
|
||||
|
||||
ordered_items_tax = item.get("total_tax")
|
||||
|
||||
new_sales_order.append("items", {
|
||||
"item_code": found_item.name,
|
||||
"item_name": found_item.item_name,
|
||||
"description": found_item.item_name,
|
||||
"delivery_date": new_sales_order.delivery_date,
|
||||
"uom": woocommerce_settings.uom or _("Nos", sys_lang),
|
||||
"qty": item.get("quantity"),
|
||||
"rate": item.get("price"),
|
||||
"warehouse": woocommerce_settings.warehouse or default_warehouse
|
||||
})
|
||||
new_sales_order.append(
|
||||
"items",
|
||||
{
|
||||
"item_code": found_item.name,
|
||||
"item_name": found_item.item_name,
|
||||
"description": found_item.item_name,
|
||||
"delivery_date": new_sales_order.delivery_date,
|
||||
"uom": woocommerce_settings.uom or _("Nos", sys_lang),
|
||||
"qty": item.get("quantity"),
|
||||
"rate": item.get("price"),
|
||||
"warehouse": woocommerce_settings.warehouse or default_warehouse,
|
||||
},
|
||||
)
|
||||
|
||||
add_tax_details(new_sales_order, ordered_items_tax, "Ordered Item tax", woocommerce_settings.tax_account)
|
||||
add_tax_details(
|
||||
new_sales_order, ordered_items_tax, "Ordered Item tax", woocommerce_settings.tax_account
|
||||
)
|
||||
|
||||
# shipping_details = order.get("shipping_lines") # used for detailed order
|
||||
|
||||
add_tax_details(new_sales_order, order.get("shipping_tax"), "Shipping Tax", woocommerce_settings.f_n_f_account)
|
||||
add_tax_details(new_sales_order, order.get("shipping_total"), "Shipping Total", woocommerce_settings.f_n_f_account)
|
||||
add_tax_details(
|
||||
new_sales_order, order.get("shipping_tax"), "Shipping Tax", woocommerce_settings.f_n_f_account
|
||||
)
|
||||
add_tax_details(
|
||||
new_sales_order,
|
||||
order.get("shipping_total"),
|
||||
"Shipping Total",
|
||||
woocommerce_settings.f_n_f_account,
|
||||
)
|
||||
|
||||
|
||||
def add_tax_details(sales_order, price, desc, tax_account_head):
|
||||
sales_order.append("taxes", {
|
||||
"charge_type":"Actual",
|
||||
"account_head": tax_account_head,
|
||||
"tax_amount": price,
|
||||
"description": desc
|
||||
})
|
||||
sales_order.append(
|
||||
"taxes",
|
||||
{
|
||||
"charge_type": "Actual",
|
||||
"account_head": tax_account_head,
|
||||
"tax_amount": price,
|
||||
"description": desc,
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user