Skip to content

Commit

Permalink
fix: set integration details for Shopify Items + fix test imports (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alchez authored Mar 24, 2021
1 parent c9e5feb commit a8f650c
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 201 deletions.
1 change: 0 additions & 1 deletion shopify_integration/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def create_customer(shopify_customer):
"name": shopify_customer.get("id"),
"customer_name": cust_name,
"shopify_customer_id": shopify_customer.get("id"),
"sync_with_shopify": 1,
"customer_group": shopify_settings.customer_group,
"territory": get_root_of("Territory"),
"customer_type": _("Individual")
Expand Down
4 changes: 2 additions & 2 deletions shopify_integration/fulfilments.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def create_delivery_notes(shopify_order, so):

delivery_notes = []
for fulfillment in shopify_order.get("fulfillments"):
if not frappe.db.get_value("Delivery Note", {"shopify_fulfillment_id": fulfillment.get("id")}, "name")\
and so.docstatus == 1:
if so.docstatus == 1 and not frappe.db.get_value("Delivery Note",
{"shopify_fulfillment_id": fulfillment.get("id")}, "name"):

dn = make_delivery_note(so.name)
dn.shopify_order_id = shopify_order.get("id")
Expand Down
1 change: 1 addition & 0 deletions shopify_integration/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

# before_install = "shopify_integration.install.before_install"
# after_install = "shopify_integration.install.after_install"
before_migrate = "shopify_integration.setup.setup_custom_fields"

# Desk Notifications
# ------------------
Expand Down
84 changes: 52 additions & 32 deletions shopify_integration/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,28 @@ def set_new_attribute_values(item_attr, values):

def create_item(shopify_item, warehouse, has_variant=False, attributes=None, variant_of=None):
item_title = shopify_item.get("title", "").strip()
item_description = shopify_item.get("body_html") or item_title
item_name = f"{variant_of} - {item_title}" if variant_of else item_title

if variant_of:
variant_name = frappe.db.get_value("Item", variant_of, "item_name")
item_name = f"{variant_name} - {item_title}"
else:
item_name = item_title

item_dict = {
"doctype": "Item",
"shopify_product_id": shopify_item.get("product_id"),
"shopify_variant_id": shopify_item.get("variant_id"),
"disabled_on_shopify": not shopify_item.get("product_exists"),
"disabled_on_shopify": not shopify_item.get("product_exists", True),
"variant_of": variant_of,
"sync_with_shopify": 1,
"is_stock_item": 1,
"item_code": cstr(shopify_item.get("item_code")) or item_title,
"item_code": cstr(shopify_item.get("item_code") or shopify_item.get("id") or item_title),
"item_name": item_name,
"description": shopify_item.get("body_html") or item_title,
"shopify_description": shopify_item.get("body_html") or item_title,
"item_group": get_item_group(shopify_item.get("product_type")),
"description": item_description,
"shopify_description": item_description,
"item_group": frappe.db.get_single_value("Shopify Settings", "default_item_group"),
"marketplace_item_group": get_item_group(shopify_item.get("product_type")),
"has_variants": has_variant,
"attributes": attributes or [],
"stock_uom": WEIGHT_UOM_MAP.get(shopify_item.get("uom")) or _("Nos"),
Expand All @@ -182,18 +189,22 @@ def create_item(shopify_item, warehouse, has_variant=False, attributes=None, var
"weight_uom": WEIGHT_UOM_MAP.get(shopify_item.get("weight_unit")),
"weight_per_unit": shopify_item.get("weight"),
"default_supplier": get_supplier(shopify_item),
"item_defaults": [
{
"company": get_default_company()
}
]
"integration_doctype": "Shopify Settings",
"integration_doc": "Shopify Settings",
"item_defaults": [{
"company": get_default_company()
}]
}

if not is_item_exists(item_dict, attributes, variant_of=variant_of):
item_details = get_item_details(shopify_item)
item_code = ""
item_code = None
existing_item = get_existing_item(shopify_item)

if not item_details:
if existing_item:
existing_item_doc = frappe.get_doc("Item", existing_item)
existing_item_doc.update(item_dict)
existing_item_doc.save(ignore_permissions=True)
else:
new_item = frappe.get_doc(item_dict)

# this fails during the `validate_name_with_item_group` call in item.py
Expand All @@ -206,7 +217,7 @@ def create_item(shopify_item, warehouse, has_variant=False, attributes=None, var
item_code = new_item.name

if not item_code:
item_code = item_details.name
item_code = existing_item

if not has_variant:
add_to_price_list(shopify_item, item_code)
Expand All @@ -215,8 +226,10 @@ def create_item(shopify_item, warehouse, has_variant=False, attributes=None, var


def create_item_variants(shopify_item, warehouse, attributes):
template_item = frappe.db.get_value("Item", filters={"shopify_product_id": shopify_item.get("product_id")},
fieldname=["name", "stock_uom"], as_dict=True)
template_item = frappe.db.get_value("Item",
filters={"shopify_product_id": shopify_item.get("product_id")},
fieldname=["name", "stock_uom"],
as_dict=True)

if template_item:
for variant in shopify_item.get("variants", []):
Expand Down Expand Up @@ -333,16 +346,13 @@ def get_supplier_group():
return supplier_group


def get_item_details(shopify_item):
item_details = frappe.db.get_value("Item", {"shopify_product_id": shopify_item.get("product_id")},
["name", "stock_uom", "item_name"], as_dict=1)

if item_details:
return item_details
def get_existing_item(shopify_item):
existing_item = frappe.db.get_value("Item", {"shopify_product_id": shopify_item.get("product_id")})
if existing_item:
return existing_item

item_details = frappe.db.get_value("Item", {"shopify_variant_id": shopify_item.get("variant_id")},
["name", "stock_uom", "item_name"], as_dict=1)
return item_details
existing_item = frappe.db.get_value("Item", {"shopify_variant_id": shopify_item.get("variant_id")})
return existing_item


def is_item_exists(shopify_item, attributes=None, variant_of=None):
Expand Down Expand Up @@ -372,19 +382,29 @@ def is_item_exists(shopify_item, attributes=None, variant_of=None):
# as we are putting condition basis on OR it will fetch all items matching either of conditions
# thus comparing matching conditions with len(attributes)
# which will give exact matching variant item.

conditions = ["(iv.attribute='{0}' and iv.attribute_value = '{1}')"
.format(attr.get("attribute"), attr.get("attribute_value")) for attr in attributes]

conditions = "( {0} ) and iv.parent = it.name ) = {1}".format(" or ".join(conditions), len(attributes))

parent = frappe.db.sql(""" select * from tabItem it where
( select count(*) from `tabItem Variant Attribute` iv
where {conditions} and it.variant_of = %s """.format(conditions=conditions),
variant_of, as_list=1)
parent = frappe.db.sql_list("""
SELECT
name
FROM
tabItem it
WHERE
(
SELECT
COUNT(*)
FROM
`tabItem Variant Attribute` iv
WHERE
{conditions}
AND it.variant_of = %s
""".format(conditions=conditions), variant_of)

if parent:
variant = frappe.get_doc("Item", parent[0][0])
variant = frappe.get_doc("Item", parent[0])
variant.flags.ignore_mandatory = True

variant.shopify_product_id = shopify_item.get("shopify_product_id")
Expand Down
19 changes: 16 additions & 3 deletions shopify_integration/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_setup_stages(args=None):
]


def setup_custom_fields(args):
def setup_custom_fields(args=None):
custom_fields = {
"Customer": [
dict(fieldname='shopify_customer_id', label='Shopify Customer ID',
Expand All @@ -33,14 +33,27 @@ def setup_custom_fields(args):
fieldtype='Data', insert_after='fax', read_only=1, print_hide=1)
],
"Item": [
# Shopify details
dict(fieldname='shopify_variant_id', label='Shopify Variant ID',
fieldtype='Data', insert_after='item_code', read_only=1, print_hide=1),
dict(fieldname='shopify_product_id', label='Shopify Product ID',
fieldtype='Data', insert_after='item_code', read_only=1, print_hide=1),
dict(fieldname='disabled_on_shopify', label='Disabled on Shopify',
fieldtype='Check', insert_after='disabled', read_only=1, print_hide=1),
dict(fieldname='marketplace_item_group', label='Marketplace Item Group',
fieldtype='Data', insert_after='item_group', read_only=1, print_hide=1),
dict(fieldname='shopify_description', label='Shopify Description',
fieldtype='Text Editor', insert_after='description', read_only=1, print_hide=1),
dict(fieldname='disabled_on_shopify', label='Disabled on Shopify',
fieldtype='Check', insert_after='disabled', read_only=1, print_hide=1)

# Integration section
dict(fieldname='integration_details', label='Shopify', fieldtype='Section Break',
insert_after='description'),
dict(fieldname='integration_doctype', label='Integration DocType',
fieldtype='Link', options='DocType', insert_after='integration_details',
hidden=1, print_hide=1),
dict(fieldname='cb_shopify', fieldtype='Column Break', insert_after='integration_doctype'),
dict(fieldname='integration_doc', label='Integration Doc', fieldtype='Data',
insert_after='cb_shopify', hidden=1, print_hide=1),
],
"Sales Order": [
dict(fieldname='shopify_order_id', label='Shopify Order ID',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"webhooks",
"sb_company",
"company",
"default_item_group",
"cb_company",
"cost_center",
"sb_customer",
Expand Down Expand Up @@ -281,10 +282,17 @@
"fieldtype": "Link",
"label": "Payment Fee Account",
"options": "Account"
},
{
"default": "All Item Groups",
"fieldname": "default_item_group",
"fieldtype": "Link",
"label": "Default Item Group",
"options": "Item Group"
}
],
"issingle": 1,
"modified": "2021-01-27 00:55:35.414257",
"modified": "2021-03-15 03:28:40.516690",
"modified_by": "Administrator",
"module": "Shopify Integration",
"name": "Shopify Settings",
Expand Down
Loading

0 comments on commit a8f650c

Please sign in to comment.