예제 #1
0
def sync_bulk_brands():

    try:
        min_id = get_last_sync_id("bcommerce_brand_id", "Brand")
        max_id = min_id + 250  #250 is limit of resource list
        brands = get_connection().Brands.all(min_id=min_id,
                                             max_id=max_id,
                                             limit=250)
        if brands:
            for brand in brands:
                if validate_resource(brand):
                    if not frappe.db.get_value(
                            "Brand", {"bcommerce_brand_id": brand.id}):
                        doc = frappe.get_doc({
                            "doctype": "Brand",
                            "description": brand.meta_description,
                            "brand": brand.name,
                            "bcommerce_brand_id": brand.id
                        })
                        doc.flags.ignore_mandatory = 1
                        doc.save(ignore_permissions=True)

    except Exception as e:
        print "Exception  raised while syncing brand from bigcommerce"
        print frappe.get_traceback()
예제 #2
0
def sync_countries(setting):
    """
		There are less countries in the world than limit of record
		provided as with parameter in request

	"""
    countries = get_connection().Countries.all(limit=250)
    for country in countries if countries else []:
        try:
            update_country(country, setting)
            make_states(country)
        except:
            pass
예제 #3
0
def sync_product(id, setting):

    flag = frappe.db.get_value("Item", {"bcommerce_product_id": id}, as_dict=1)
    if flag:
        return flag.get("name")

    else:
        sync_options()
        sync_bulk_brands()
        conn = get_connection()
        product = get_resource("Products", id)
        if not product:
            return
        save_product(product, setting, conn)
예제 #4
0
def sync_store(store=None, save=True, doc=None):

    if not doc:
        doc = frappe.get_doc("Bcommerce Setting", "Bcommerce Setting")
    d = doc.as_dict()
    if not store:
        store = get_connection().Store.all()
    for key, val in store.iteritems():
        if key in KEYS:
            doc.set(key, val)
    doc.set("default_currency", store.currency)

    if save:
        doc.save(ignore_permissions=True)
예제 #5
0
def get_resource(resource, id, multiple=False):

    try:
        conn = get_connection()
        data = {}
        if hasattr(conn, resource):
            resource = getattr(conn, resource)
            data = resource.get(id)
            if not validate_resource(data):
                data = {}
        return data
    except Exception as e:
        print e
        print frappe.get_traceback()
예제 #6
0
def sync_currencies(setting, id=None):

    setting = frappe.get_doc("Bcommerce Setting", "Bcommerce Setting")

    if id and not frappe.db.get_value("Currency",
                                      {"bcommerce_currency_id": id}):

        currency = get_resource("Currencies", id)
        if not currency:
            return
        update_currency(currency, setting)

    else:
        currencies = get_connection().Currencies.all()
        for currency in currencies:
            if validate_resource(currency):
                update_currency(currency, setting)
예제 #7
0
def create_customer(doc):

    if doc.sync_with_bcommerce:
        conn = get_connection()
        name = doc.name
        if not doc.bcommerce_customer_id:
            try:
                customer = conn.Customers.create(
                    "first_name" =doc.customer_name,
                    "last_name" ="",
                    "email" =doc.email)
                doc = frappe.get_doc("Customer", name)
                doc.bcommerce_customer_id = customer.id
                doc.flags.ignore_mandatory = 1
                doc.save(ignore_permissions=True)
                frappe.db.commit()
            except Exception as e:
                print e.message
예제 #8
0
def sync_bulk_customer_group():

    try:
        customer_groups = get_connection().CustomerGroups.all(limit=250)
        if customer_groups:
            for cg in customer_groups:
                if validate_resource(cg):
                    doc = frappe.get_doc({
                        "doctype": "Customer Group",
                        "is_group": 0,
                        "parent_customer_group": setting.customer_group,
                        "customer_group_name": cg.name,
                        "bcommerce_customer_group_id": cg.id
                    })
                    doc.save(ignore_permissions=True)

    except Exception as e:
        print frappe.get_traceback()
예제 #9
0
def delete_webhooks(id=None):

    if id:
        webhook = get_resource("Webhooks", id)
        flag = frappe.db.get_value("Bcommerce Webhook", {"webhook_id": id},
                                   as_dict=True)
        if flag:
            frappe.get_doc("Bcommerce Webhook", flag.get("name")).delete()
        webhook.delete()

    else:
        for webhook in get_connection().Webhooks.all(limit=250):
            if not validate_resource(webhook):
                continue
            flag = frappe.db.get("Bcommerce Webhook",
                                 {"webhook_id": webhook.id},
                                 as_dict=True)
            if flag:
                frappe.get_doc("Bcommerce Webhook", flag.get("name")).delete()
            webhook.delete()
예제 #10
0
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)
예제 #11
0
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)
예제 #12
0
def sync_payments(setting):

    payment_methods = get_connection().PaymentMethods.all()

    for pay_method in payment_methods:
        if validate_resource(pay_method, "name"):
            flag = frappe.db.get_value("Mode of Payment",
                                       {"mode_of_payment": pay_method.name})
            doc = None
            if flag:
                doc = frappe.get_doc("Mode of Payment",
                                     {"mode_of_payment": pay_method.name})
                doc.update({"mode_of_payment": pay_method.name})
            else:
                doc = frappe.get_doc({
                    "doctype": "Mode of Payment",
                    "mode_of_payment": pay_method.name
                })

            if doc:
                doc.flags.ignore_mandatory = 1
                doc.save(ignore_permissions=True)
        frappe.db.commit()
예제 #13
0
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()