Merge pull request #19097 from prssanna/shopify

fix(Integrations): Shopify Integration
This commit is contained in:
Saurabh 2019-09-30 20:07:04 +05:30 committed by GitHub
commit 509dcd9ad0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 274 additions and 1243 deletions

View File

@ -19,6 +19,7 @@ def store_request_data(order=None, event=None):
dump_request_data(order, event)
def sync_sales_order(order, request_id=None):
frappe.set_user('Administrator')
shopify_settings = frappe.get_doc("Shopify Settings")
frappe.flags.request_id = request_id
@ -33,6 +34,7 @@ def sync_sales_order(order, request_id=None):
make_shopify_log(status="Success")
def prepare_sales_invoice(order, request_id=None):
frappe.set_user('Administrator')
shopify_settings = frappe.get_doc("Shopify Settings")
frappe.flags.request_id = request_id
@ -45,6 +47,7 @@ def prepare_sales_invoice(order, request_id=None):
make_shopify_log(status="Error", exception=True)
def prepare_delivery_note(order, request_id=None):
frappe.set_user('Administrator')
shopify_settings = frappe.get_doc("Shopify Settings")
frappe.flags.request_id = request_id
@ -137,6 +140,7 @@ def create_sales_invoice(shopify_order, shopify_settings, so):
si.naming_series = shopify_settings.sales_invoice_series or "SI-Shopify-"
si.flags.ignore_mandatory = True
set_cost_center(si.items, shopify_settings.cost_center)
si.insert(ignore_mandatory=True)
si.submit()
make_payament_entry_against_sales_invoice(si, shopify_settings)
frappe.db.commit()
@ -151,6 +155,7 @@ def make_payament_entry_against_sales_invoice(doc, shopify_settings):
payemnt_entry.flags.ignore_mandatory = True
payemnt_entry.reference_no = doc.name
payemnt_entry.reference_date = nowdate()
payemnt_entry.insert(ignore_permissions=True)
payemnt_entry.submit()
def create_delivery_note(shopify_order, shopify_settings, so):
@ -168,6 +173,7 @@ def create_delivery_note(shopify_order, shopify_settings, so):
dn.items = get_fulfillment_items(dn.items, fulfillment.get("line_items"), shopify_settings)
dn.flags.ignore_mandatory = True
dn.save()
dn.submit()
frappe.db.commit()
def get_fulfillment_items(dn_items, fulfillment_items, shopify_settings):
@ -200,7 +206,7 @@ def get_order_items(order_items, shopify_settings):
"rate": shopify_item.get("price"),
"delivery_date": nowdate(),
"qty": shopify_item.get("quantity"),
"stock_uom": shopify_item.get("sku"),
"stock_uom": shopify_item.get("uom") or _("Nos"),
"warehouse": shopify_settings.warehouse
})
else:

View File

@ -21,33 +21,22 @@ class ShopifySettings(Document):
else:
self.unregister_webhooks()
self.validate_app_type()
def validate_access_credentials(self):
if self.app_type == "Private":
if not (self.get_password(raise_exception=False) and self.api_key and self.shopify_url):
frappe.msgprint(_("Missing value for Password, API Key or Shopify URL"), raise_exception=frappe.ValidationError)
else:
if not (self.access_token and self.shopify_url):
frappe.msgprint(_("Access token or Shopify URL missing"), raise_exception=frappe.ValidationError)
def validate_app_type(self):
if self.app_type == "Public":
frappe.throw(_("Support for public app is deprecated. Please setup private app, for more details refer user manual"))
if not (self.get_password(raise_exception=False) and self.api_key and self.shopify_url):
frappe.msgprint(_("Missing value for Password, API Key or Shopify URL"), raise_exception=frappe.ValidationError)
def register_webhooks(self):
webhooks = ["orders/create", "orders/paid", "orders/fulfilled"]
url = get_shopify_url('admin/webhooks.json', self)
# url = get_shopify_url('admin/webhooks.json', self)
created_webhooks = [d.method for d in self.webhooks]
url = get_shopify_url('admin/api/2019-04/webhooks.json', self)
print('url', url)
for method in webhooks:
if method in created_webhooks:
continue
print('method', method)
session = get_request_session()
print('session', session)
try:
print(get_header(self))
d = session.post(url, data=json.dumps({
"webhook": {
"topic": method,
@ -55,6 +44,7 @@ class ShopifySettings(Document):
"format": "json"
}
}), headers=get_header(self))
print('d', d.json())
d.raise_for_status()
self.update_webhook_table(method, d.json())
except Exception as e:
@ -65,7 +55,7 @@ class ShopifySettings(Document):
deleted_webhooks = []
for d in self.webhooks:
url = get_shopify_url('admin/webhooks/{0}.json'.format(d.webhook_id), self)
url = get_shopify_url('admin/api/2019-04/webhooks.json'.format(d.webhook_id), self)
try:
res = session.delete(url, headers=get_header(self))
res.raise_for_status()
@ -77,6 +67,7 @@ class ShopifySettings(Document):
self.remove(d)
def update_webhook_table(self, method, res):
print('update')
self.append("webhooks", {
"webhook_id": res['webhook']['id'],
"method": method
@ -84,6 +75,7 @@ class ShopifySettings(Document):
def get_shopify_url(path, settings):
if settings.app_type == "Private":
print(settings.api_key, settings.get_password('password'), settings.shopify_url, path)
return 'https://{}:{}@{}/{}'.format(settings.api_key, settings.get_password('password'), settings.shopify_url, path)
else:
return 'https://{}/{}'.format(settings.shopify_url, path)
@ -91,11 +83,7 @@ def get_shopify_url(path, settings):
def get_header(settings):
header = {'Content-Type': 'application/json'}
if settings.app_type == "Private":
return header
else:
header["X-Shopify-Access-Token"] = settings.access_token
return header
return header;
@frappe.whitelist()
def get_series():

View File

@ -21,7 +21,7 @@ def create_customer(shopify_customer, shopify_settings):
"customer_type": _("Individual")
})
customer.flags.ignore_mandatory = True
customer.insert()
customer.insert(ignore_permissions=True)
if customer:
create_customer_address(customer, shopify_customer)

View File

@ -1,13 +1,14 @@
from __future__ import unicode_literals
import frappe
from frappe import _
from erpnext import get_default_company
from frappe.utils import cstr, cint, get_request_session
from erpnext.erpnext_integrations.doctype.shopify_settings.shopify_settings import get_shopify_url, get_header
shopify_variants_attr_list = ["option1", "option2", "option3"]
def sync_item_from_shopify(shopify_settings, item):
url = get_shopify_url("/admin/products/{0}.json".format(item.get("product_id")), shopify_settings)
url = get_shopify_url("admin/api/2019-04/products/{0}.json".format(item.get("product_id")), shopify_settings)
session = get_request_session()
try:
@ -107,7 +108,12 @@ def create_item(shopify_item, warehouse, has_variant=0, attributes=None,variant_
"image": get_item_image(shopify_item),
"weight_uom": shopify_item.get("weight_unit"),
"weight_per_unit": shopify_item.get("weight"),
"default_supplier": get_supplier(shopify_item)
"default_supplier": get_supplier(shopify_item),
"item_defaults": [
{
"company": get_default_company()
}
]
}
if not is_item_exists(item_dict, attributes, variant_of=variant_of):
@ -116,7 +122,7 @@ def create_item(shopify_item, warehouse, has_variant=0, attributes=None,variant_
if not item_details:
new_item = frappe.get_doc(item_dict)
new_item.insert()
new_item.insert(ignore_permissions=True, ignore_mandatory=True)
name = new_item.name
if not name:

View File

@ -40,6 +40,7 @@ class ShopifySettings(unittest.TestCase):
"price_list": "_Test Price List",
"warehouse": "_Test Warehouse - _TC",
"cash_bank_account": "Cash - _TC",
"account": "Cash - _TC",
"customer_group": "_Test Customer Group",
"cost_center": "Main - _TC",
"taxes": [

View File

@ -40,4 +40,4 @@ def get_webhook_address(connector_name, method, exclude_uri=False):
server_url = '{uri.scheme}://{uri.netloc}/api/method/{endpoint}'.format(uri=urlparse(url), endpoint=endpoint)
return server_url
return server_url

View File

@ -638,3 +638,5 @@ erpnext.patches.v12_0.add_variant_of_in_item_attribute_table
erpnext.patches.v12_0.rename_bank_account_field_in_journal_entry_account
erpnext.patches.v12_0.create_default_energy_point_rules
erpnext.patches.v12_0.set_produced_qty_field_in_sales_order_for_work_order
erpnext.patches.v12_0.generate_leave_ledger_entries
erpnext.patches.v12_0.set_default_shopify_app_type

View File

@ -0,0 +1,6 @@
from __future__ import unicode_literals
import frappe
def execute():
frappe.reload_doc('erpnext_integrations', 'doctype', 'shopify_settings')
frappe.db.set_value('Shopify Settings', None, 'app_type', 'Private')