def get_current_item_qty(config, site_doc): ''' Get all items are root group and its descendant. Sync with Opencart DB ''' # Validate name = frappe.local.form_dict.get('name') if not name: return {"status": -1, "error": "Missing compulsory parameter 'name'"} # Get Item item = frappe.get_doc("Item", name) if not item: return {"status": -2, "error": "Item does not exist"} # Compare Item's Opencart Site with the Opencart Site that this user tight to if (item.get('oc_site')!=site_doc.get('name')): return {"status": -2, "error": "Mismatched opencart site user"} return get_item_qty(item)
def get_current_item_qty(config, site_doc): ''' Get all items are root group and its descendant. Sync with Opencart DB ''' # Validate name = frappe.local.form_dict.get('name') if not name: return {"status": -1, "error": "Missing compulsory parameter 'name'"} # Get Item item = frappe.get_doc("Item", name) if not item: return {"status": -2, "error": "Item does not exist"} # Compare Item's Opencart Site with the Opencart Site that this user tight to if (item.get('oc_site') != site_doc.get('name')): return {"status": -2, "error": "Mismatched opencart site user"} return get_item_qty(item)
def get_current_multi_item_qty(config, site_doc): ''' Get all items are root group and its descendant. Sync with Opencart DB ''' # Validate ids = frappe.local.form_dict.get('ids') if not ids: return {"status": -1, "error": "Missing compulsory parameter 'ids'"} results = {} for prod_id in ids.split(','): # Get Item results[prod_id] = 0 try: item = frappe.get_doc("Item", {'oc_product_id': prod_id}) except: pass if not item: # Log ? continue # Compare Item's Opencart Site with the Opencart Site that this user tight to if (item.get('oc_site')!=site_doc.get('name')): # Log ? continue results[prod_id] = get_item_qty(item) return results
def get_current_multi_item_qty(config, site_doc): ''' Get all items are root group and its descendant. Sync with Opencart DB ''' # Validate ids = frappe.local.form_dict.get('ids') if not ids: return {"status": -1, "error": "Missing compulsory parameter 'ids'"} results = {} for prod_id in ids.split(','): # Get Item results[prod_id] = 0 try: item = frappe.get_doc("Item", {'oc_product_id': prod_id}) except: pass if not item: # Log ? continue # Compare Item's Opencart Site with the Opencart Site that this user tight to if (item.get('oc_site') != site_doc.get('name')): # Log ? continue results[prod_id] = get_item_qty(item) return results
def oc_validate_item(doc, site_doc, api_map, headers, method=None): # Get the group product_categories = [] root_group = site_doc.get('root_item_group') if (doc.get('item_group') == root_group): product_categories.append(0) else: # Check valid group valid_groups = [x[0] for x in get_child_groups(root_group)] valid_groups.append(root_group) # Check if current group is valid if (doc.get('item_group') not in valid_groups): raise Exception( 'To be able to sold on selected Ecommerce site, Item Group must be one of the followings: %s' % str(valid_groups)) # Check if the group already synced first time item_group = frappe.get_doc("Item Group", doc.get('item_group')) if (not item_group.get(OC_CAT_ID)): raise Exception( 'Category you selected has not been synced to opencart. Please do a manual sync <a href="%s">here</a> ' % str(site_doc.get_url())) product_categories.append(item_group.get(OC_CAT_ID)) # Pass validation is_updating = doc.get(OC_PROD_ID) and doc.get(OC_PROD_ID) > 0 # Get quantity qty = get_item_qty(doc) data = { "model": doc.get('item_code'), "sku": doc.get('item_code'), "price": doc.get('oc_price'), "status": doc.get('oc_enable'), "quantity": str(cint(qty)), "product_store": ["0"], "product_category": product_categories, "product_description": { "1": { "name": doc.get('item_name'), "meta_keyword": doc.get('oc_meta_keyword') or '', "meta_description": doc.get('oc_meta_description') or '', "description": doc.get('description') or '' } }, # Irrelevant "sort_order": "1", "tax_class_id": "1", "manufacturer_id": "1" } # Get API obj api_name = 'Product Edit' if is_updating else 'Product Add' api_params = {'id': doc.get(OC_PROD_ID)} if is_updating else None # Push change to server res = oc_requests(site_doc.get('server_base_url'), headers, api_map, api_name, data=data, url_params=api_params) if res: # Check success action = 'updated' if is_updating else 'added' if (not res.get('success')): frappe.msgprint('Product not %s on Opencart. Error: %s' % (action, res.get('error'))) else: doc.update({'last_sync_oc': datetime.now()}) if (not is_updating): doc.update({OC_PROD_ID: res.get('product_id')}) frappe.msgprint('Product successfully %s on Opencart' % action)
def oc_validate_item (doc, site_doc, api_map, headers, method=None): # Get the group product_categories = [] root_group = site_doc.get('root_item_group') if (doc.get('item_group') == root_group): product_categories.append(0) else: # Check valid group valid_groups = [x[0] for x in get_child_groups(root_group)] valid_groups.append(root_group) # Check if current group is valid if (doc.get('item_group') not in valid_groups): raise Exception('To be able to sold on selected Ecommerce site, Item Group must be one of the followings: %s'%str(valid_groups)) # Check if the group already synced first time item_group = frappe.get_doc("Item Group", doc.get('item_group')) if (not item_group.get(OC_CAT_ID)): raise Exception('Category you selected has not been synced to opencart. Please do a manual sync <a href="%s">here</a> '%str(site_doc.get_url())) product_categories.append(item_group.get(OC_CAT_ID)) # Pass validation is_updating = doc.get(OC_PROD_ID) and doc.get(OC_PROD_ID) > 0 # Get quantity qty = get_item_qty(doc) data = { "model": doc.get('item_code'), "sku": doc.get('item_code'), "price": doc.get('oc_price'), "status": doc.get('oc_enable'), "quantity": str(cint(qty)), "product_store": ["0"], "product_category": product_categories, "product_description": { "1":{ "name": doc.get('item_name'), "meta_keyword" : doc.get('oc_meta_keyword') or '', "meta_description" : doc.get('oc_meta_description') or '', "description" : doc.get('description') or '' } }, # Irrelevant "sort_order": "1", "tax_class_id": "1", "manufacturer_id": "1" } # Get API obj api_name = 'Product Edit' if is_updating else 'Product Add' api_params = {'id': doc.get(OC_PROD_ID)} if is_updating else None # Push change to server res = oc_requests(site_doc.get('server_base_url'), headers, api_map, api_name, data=data, url_params = api_params) if res: # Check success action = 'updated' if is_updating else 'added' if (not res.get('success')): frappe.msgprint('Product not %s on Opencart. Error: %s' %(action, res.get('error'))) else: doc.update({'last_sync_oc': datetime.now()}) if (not is_updating): doc.update({OC_PROD_ID: res.get('product_id')}) frappe.msgprint('Product successfully %s on Opencart'%action)