def get_item(product, setting, order, conn): item = frappe.db.get_value( "Item", filters={ "bcommerce_product_id": product.product_id, "has_variants": 0 }, fieldname=["default_warehouse", "item_code", "item_name"], as_dict=True) if item: item.update({"qty": product.quantity}) else: sync_product(product.product_id, setting) item = frappe.db.get_value( "Item", filters={"bcommerce_product_id": product.product_id}, fieldname=["default_warehouse", "item_code", "item_name"], as_dict=True) if not item: msg = _( "Item {0} does not exists in ERPNext System, Please try to sync the Item {1}, and then \ try to sync Order {2} manually".format(product.name, product.name, order.id)) make_logs("Failed", "Product", msg, order.id) return item
def get_options_values(optionset): options_values = frappe._dict() for option in optionset.options(): if option and (isinstance(option, dict)): flag = frappe.db.get_value("Bcommerce Option", {"option_id": option.option_id}, as_dict=True) if not flag: sync_options(id=option.option_id) flag = frappe.db.get_value("Bcommerce Option", {"option_id": option.option_id}, as_dict=True) if not flag: msg = "{0} {1} {2}".format("Option with id ", option.option_id, " Does not exists") make_logs("Failed", "Option", message=msg) continue values = frappe.db.get_values("Bcommerce Option Value", {"parent": flag.get("name")}, "bcommerce_option_value_name", as_dict=1) if not options_values.has_key(option.option_id): options_values[option.option_id] = values values = [] if options_values: for key, val in options_values.iteritems(): values.extend(val) return values
def sync_store_setting(): setting = frappe.get_doc("Bcommerce Setting", "Bcommerce Setting") try: from bcommerce.utils.store import sync_with_store enqueue("bcommerce.utils.store.sync_with_store", queue="long") except Exception as e: make_logs("Failed", "Sync Error", message=frappe.get_traceback())
def get_optionset(id): try: resource = get_resource("OptionSets", id) options_values = get_options_values(resource) create_item_attribute(resource, options_values) except Exception as e: msg = "{0} {1} {2}".format("OptionSet with id ", id, "Doesn't exist") make_logs("Failed", "OptionSet", message=msg)
def sync_bulk_orders(orders, setting, conn): ordr = None for order in orders: try: ordr = order save_order(order, setting, conn) except: msg = _("Error while saving Order {0}".format(ordr.id)) make_logs("Failed", "Order", msg, ordr.id)
def sync_bulk_customers(customers, setting, conn): temp = None for customer in customers: temp = customer if not frappe.db.get_value("Customer", {"bcommerce_customer_id": customer.id}, as_dict=True): try: save_customer(customer, setting) except Exception, e: msg = _("Error while saving Customer {0}, Frappe traceback {1}".\ format(temp.id,frappe.get_traceback())) make_logs("Failed", "Customer", msg, temp.id)
def sync_bulk_products(products, setting, conn): sync_options() sync_bulk_brands() temp = None for product in products: temp = product if not frappe.db.get_value("Item", {"bcommerce_product_id": product.id}): try: save_product(product, setting, conn) except: msg = _("Error while saving Product {0}, Frappe traceback {1}".\ format(temp.id, frappe.get_traceback())) make_logs("Failed", "Product", msg, temp.id)
def sync_order(order_id, setting): if not order_id: return sales_order = frappe.db.get_value("Sales Order", filters={"bcommerce_order_id": order_id}, as_dict=True) if sales_order: return sales_order.get("name") else: try: conn = get_connection() order = conn.Orders.get(id=order_id) save_order(order, setting, conn) except Exception as e: msg = "{0}, {1}".format("Error while sycing order", frappe.get_traceback()) make_logs("Failed", "Order", message=msg)
def sync_options(id=None, optionset_id=None): try: conn = get_connection() if id and not frappe.db.get_value("Bcommerce Option", filters={"option_id": id}): option = conn.Options.get(id) values = get_option_values(option) if len(values) >= 1: save_option(option, values) else: options = conn.Options.all(limit=250) for option in options: id = option.id if not frappe.db.get_value("Bcommerce Option", {"option_id": option.id}): values = get_option_values(option) if len(values) >= 1: save_option(option, values) except: msg = _("Error while saving Option {0}, Frappe traceback {1}".format( id, frappe.get_traceback())) make_logs("Failed", "Option", message=msg)
def create_scope(setting): conn = get_connection() for scope in [ "store/order/created", "store/order/updated", "store/order/archived", "store/order/statusUpdated", "store/product/created", "store/product/updated", "store/product/deleted", "store/customer/created", "store/customer/updated", "store/customer/deleted", "store/shipment/created", "store/shipment/updated", "store/shipment/deleted", "store/information/updated" ]: try: if not frappe.db.get_value("Bcommerce Webhook", {"webhook_scope": scope}): res = conn.Webhooks.create(scope=scope, destination=setting.webhook_url) save_web_hook(res) except Exception as e: msg = "Scope = {0}, URL = {1}, Traceback = {2}".format( scope, setting.webhook_url, frappe.get_traceback()) make_logs("Failed", "Webhooks", message=msg) frappe.db.commit()
def sync_bulk(setting): """ Function used to sync all the prodcut from provide Min ID and Max ID """ if not get_queue_status(): frappe.msgprint(_("Syncing already in progress")) return {} try: make_logs("Queued", "Syncing", message="Syncing in progress") sync_customers(setting) sync_products(setting) sync_orders(setting) make_logs("Syncing Completed", "Syncing", message="Syncing complete successfully") frappe.db.commit() ''' Exceptions are more important for tracking the logs the error For Future you can even Sync the same resource/create new resource using manual sync ''' except Exception, e: make_logs("Failed", "Syncing", message="Syncing Failed")
def sync_with_store(): try: setting = frappe.get_doc("Bcommerce Setting", "Bcommerce Setting") if not get_queue_status(): msgprint(_("Sync is already in progress")) return make_logs("Queued", "Syncing", message="Queued for syncing, Store Setting") sync_store() sync_currencies(setting) sync_countries(setting) sync_payments(setting) make_logs("Sync Completed", "Syncing", "Syncing complete successfully") frappe.db.commit() except Exception as e: msg = "{0}, {1}".format("Error while syncing store setting", frappe.get_traceback()) make_logs("Failed", "Syncing", message=msg)
def create_item(product, setting, has_variants=False, attributes=None, conn=None, update=False): brand = get_brand(product.brand_id) if has_variants and not attributes: message = _( "There is problem with Product {0}, traceback: {1}".format( product.name, "Item has no attribute")) make_logs("Failed", "Product", message, product.id, False) return doc = None filters = {} if has_variants: filters["has_variants"] = 1 filters["bcommerce_product_id"] = product.id product_name = frappe.db.get_value("Item", filters, as_dict=True) if not product_name: image = get_image_url(product) doc = frappe.get_doc({ "doctype": "Item", "uom": _("Nos"), "title": product.page_title, "item_code": product.name, "is_stock_item": 1, "image": image, "stock_keeping_unit": product.sku, "height": product.height, "net_weight": product.weight, "width": product.width, "depth": product.depth, "bcommerce_product_id": product.id, "sync_with_bcommerce": 1, "default_warehouse": setting.warehouse, "item_group": setting.item_group, "has_variants": has_variants, "attributes": attributes, "brand": brand, "description": product.description, }) doc.save(ignore_permissions=True) else: doc = frappe.get_doc("Item", product_name.get("name")) image = get_image_url(product) doc.update({ "title": product.page_title, "attributes": attributes, "brand": brand, "image": image, "stock_keeping_unit": product.sku, "depth": product.depth, "width": product.width, "height": product.height, "net_weight": product.weight, "item_group": setting.item_group, "default_warehouse": setting.warehouse, "description": product.description }) doc.save(ignore_permissions=True) if has_variants: create_variants(attributes, doc, product, setting) else: create_item_price(product, doc, setting)