brotherton-erpnext/erpnext/erpnext_integrations/connectors/woocommerce_connection.py

225 lines
7.9 KiB
Python
Raw Normal View History

Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
from __future__ import unicode_literals
import frappe, base64, hashlib, hmac, json
from frappe import _
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
).digest()
)
if frappe.request.data and \
frappe.get_request_header("X-Wc-Webhook-Signature") and \
not sig == bytes(frappe.get_request_header("X-Wc-Webhook-Signature").encode()):
frappe.throw(_("Unverified Webhook Data"))
frappe.set_user(woocommerce_settings.creation_user)
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
@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__()
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:
order = frappe.flags.woocomm_test_order_data
event = "created"
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
elif frappe.request and frappe.request.data:
verify_request()
try:
order = json.loads(frappe.request.data)
except ValueError:
#woocommerce returns 'webhook_id=value' for the first request which is not JSON
order = frappe.request.data
2018-06-14 03:43:39 +00:00
event = frappe.get_request_header("X-Wc-Webhook-Event")
2018-06-14 03:43:39 +00:00
else:
return "success"
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
if event == "created":
sys_lang = frappe.get_single("System Settings").language or 'en'
raw_billing_data = order.get("billing")
raw_shipping_data = order.get("shipping")
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
customer_name = raw_billing_data.get("first_name") + " " + raw_billing_data.get("last_name")
link_customer_and_address(raw_billing_data, raw_shipping_data, customer_name)
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})
if not customer_exists:
# Create Customer
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
customer = frappe.new_doc("Customer")
else:
# Edit Customer
customer = frappe.get_doc("Customer", {"woocommerce_email": customer_woo_com_email})
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
old_name = customer.customer_name
customer.customer_name = customer_name
customer.woocommerce_email = customer_woo_com_email
customer.flags.ignore_mandatory = True
customer.save()
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
if customer_exists:
frappe.rename_doc("Customer", old_name, customer_name)
billing_address = frappe.get_doc("Address", {"woocommerce_email": customer_woo_com_email, "address_type": "Billing"})
shipping_address = frappe.get_doc("Address", {"woocommerce_email": customer_woo_com_email, "address_type": "Shipping"})
else:
billing_address = create_address(raw_billing_data, customer, "Billing")
shipping_address = create_address(raw_shipping_data, customer, "Shipping")
2020-05-16 13:30:00 +00:00
contact = create_contact(raw_billing_data, customer)
if customer_exists:
rename_address(billing_address, customer)
rename_address(shipping_address, customer)
2020-05-16 13:30:00 +00:00
def create_contact(data, customer):
email = data.get("email", None)
phone = data.get("phone", None)
if not email and not phone:
return
contact = frappe.new_doc("Contact")
contact.first_name = data.get("first_name")
contact.last_name = data.get("last_name")
contact.is_primary_contact = 1
contact.is_billing_contact = 1
if phone:
contact.add_phone(phone, is_primary_mobile_no=1, is_primary_phone=1)
if email:
contact.add_email(email, is_primary=1)
contact.append("links", {
"link_doctype": "Customer",
"link_name": customer.customer_name
})
contact.flags.ignore_mandatory = True
contact.save()
return contact
def create_address(raw_data, customer, address_type):
address = frappe.new_doc("Address")
address.address_line1 = raw_data.get("address_1", "Not Provided")
address.address_line2 = raw_data.get("address_2", "Not Provided")
address.city = raw_data.get("city", "Not Provided")
address.woocommerce_email = customer.woocommerce_email
address.address_type = address_type
address.country = frappe.get_value("Country", {"code": raw_data.get("country", "IN").lower()})
address.state = raw_data.get("state")
address.pincode = raw_data.get("postcode")
address.phone = raw_data.get("phone")
address.email_id = customer.woocommerce_email
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
address.append("links", {
"link_doctype": "Customer",
"link_name": customer.customer_name
})
address.flags.ignore_mandatory = True
2020-05-16 13:30:00 +00:00
address.save()
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
return address
def rename_address(address, customer):
old_address_title = address.name
new_address_title = customer.customer_name + "-" + address.address_type
address.address_title = customer.customer_name
address.save()
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
frappe.rename_doc("Address", old_address_title, new_address_title)
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
def link_items(items_list, woocommerce_settings, sys_lang):
for item_data in items_list:
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
item_woo_com_id = item_data.get("product_id")
if frappe.get_value("Item", {"woocommerce_id": item_woo_com_id}):
#Edit Item
item = frappe.get_doc("Item", {"woocommerce_id": item_woo_com_id})
else:
#Create Item
item = frappe.new_doc("Item")
item.item_name = item_data.get("name")
item.item_code = _("woocommerce - {0}", sys_lang).format(item_data.get("product_id"))
item.woocommerce_id = item_data.get("product_id")
item.item_group = _("WooCommerce Products", sys_lang)
item.stock_uom = woocommerce_settings.uom or _("Nos", 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
new_sales_order.po_no = new_sales_order.woocommerce_id = order.get("id")
new_sales_order.naming_series = woocommerce_settings.sales_order_series or "SO-WOO-"
created_date = order.get("date_created").split("T")
new_sales_order.transaction_date = created_date[0]
delivery_after = woocommerce_settings.delivery_after_days or 7
new_sales_order.delivery_date = frappe.utils.add_days(created_date[0], delivery_after)
new_sales_order.company = woocommerce_settings.company
set_items_in_sales_order(new_sales_order, woocommerce_settings, order, sys_lang)
new_sales_order.flags.ignore_mandatory = True
new_sales_order.insert()
new_sales_order.submit()
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
frappe.db.commit()
Woocommerce Integration (#13217) * WIP for WooCommerce Integration * WIP for WooComm Integration * WIP WooComm * Added Woocommerce Settings * Woocommerce Settings Enable Sync Makes fields mandatory * Woocommerce whitelisted endpoints * [Clean Up] Woocommerce Settings * Verify Webhook Data * Fix Verify Webhook * WIP WooComm * WIP and working few modules * Customer creating partially * Customer creation successful * Refactored Customer code * WIP Address * Fixed address and customer bug. Now working fine * WIP for products. Creation of items successful * Handling json for product * Products creating and updating properly * Custom checkbox for required doctypes * Product feature fully completed * WIP orders * Sales order working properly * Orders mapping successfully * New version Customer Working Properly * Items creation properly * Working on sales order * Orders comming successfully * Bug fixes * Fixed date format for delivery date * Code Cleanup * Woo setting page modified * Fixed minor bug * Fixes * Minimum Viable Product * Cleanup * Removed duplicate file from erpnext config * Added more default changes to woo settings * Fixes as per required * Fixes * Bug fix * few changes and fix * Fixing * Fixes with test * Added Test for Woocommerce - Emulates request * Fix woocommerce test * fix woocommerce test * verify_request: py3 ready * Codacy fixes * WooCommerce Integration Docs * Codacy changes * Codacy fixes * User set tax account * Fixes for account * Fix for warehouse issue * Docs updated * Codacy fix * Updated Docs * Minor changes * Tested added for repeat order and cleanup * Minor change * docs and gifs renamed according to convention * Doc updated
2018-04-03 05:47:03 +00:00
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')
default_warehouse = _("Stores - {0}", sys_lang).format(company_abbr)
if not frappe.db.exists("Warehouse", default_warehouse):
frappe.throw(_("Please set Warehouse in Woocommerce Settings"))
for item in order.get("line_items"):
woocomm_item_id = item.get("product_id")
found_item = frappe.get_doc("Item", {"woocommerce_id": woocomm_item_id})
ordered_items_tax = item.get("total_tax")
new_sales_order.append("items",{
"item_code": found_item.item_code,
"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)
# 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)
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
})