def get_context(context): """This is a controller extension for erpnext.templates.pages.cart""" context["no_cache"] = 1 settings = frappe.db.get("Awc Settings") awc_session = get_awc_session() context["countries"] = [ x for x in frappe.get_list( "Country", fields=["country_name", "name"], ignore_permissions=1) ] default_country = frappe.get_value("System Settings", "System Settings", "country") default_country_doc = next( (x for x in context["countries"] if x.name == default_country), None) if frappe.session.user != "Guest": context["addresses"] = frappe.get_all("Address", filters={ "customer": get_current_customer().name, "disabled": False }, fields="*") country_idx = context["countries"].index(default_country_doc) context["countries"].pop(country_idx) context["countries"] = [default_country_doc] + context["countries"] context["shipping_rate_api"] = frappe.get_hooks("shipping_rate_api")[0] context["selected_customer"] = awc_session.get("selected_customer") # remove? shipping is essential here anyways context.shipping_enabled = 1 if settings.awc_shipping_enabled else 0 # flag to display login form context.is_logged = awc.is_logged_in() login.apply_context(context) if context.is_logged: # load gateway provider into context gateway_provider = frappe.get_hooks('awc_gateway_form_provider') if gateway_provider and len(gateway_provider) > 0: context['gateway_provider'] = frappe.call( gateway_provider[0], context=dict(use_address_same_as=1, address_same_as_label="Same as Shipping Address", address_same_as_source="#awc-shipping-form")) awc.reset_shipping() return context
def get_current_customer(): session_user = frappe.get_user() if session_user.name == "Guest": return None awc_session = session.get_awc_session() party = None if awc_session.get("selected_customer"): party = frappe.get_doc("Customer", awc_session["selected_customer"]) quotation = get_cart_quotation(party=party)["doc"] customer = frappe.get_doc("Customer", quotation.customer) return customer
def login(payment_request_name, payment_reference): pr = frappe.get_doc("Payment Request", payment_request_name.strip().upper()) if pr.get("status") != "Initiated": return "This Payment Request was Already Fulfilled" if pr.reference_name.strip().upper() == payment_reference.strip().upper(): awc_session = get_awc_session() awc_session["gateway_selector_pr_access"] = pr.name set_awc_session(awc_session) parts = pr.payment_url.split('/') frappe.response.redirec_url = "/integrations/gateway_selector/{0}".format(parts[-1]) return return "Sorry, Payment Request and/or Reference Number not found!"
def get_context(context): context.no_cache = 1 # get request name from query string proxy_name = frappe.local.form_dict.get("name") awc_session = get_awc_session() pr_access = awc_session.get("gateway_selector_pr_access") # or from pathname, this works better for redirection on auth errors if not proxy_name: path_parts = context.get("pathname", "").split('/') proxy_name = path_parts[-1] doc = None # attempt to fetch AuthorizeNet Request record try: proxy = frappe.get_doc("Gateway Selector Proxy", proxy_name) doc = frappe.get_doc(proxy.reference_doctype, proxy.reference_docname) if doc.get("status") != "Initiated": frappe.redirect_to_message( _("Payment Request Not Available"), _("This Payment Request is not available any longer")) frappe.local.flags.redirect_location = "/payments?e={0}".format( payments.ERROR_NOT_AVAILABLE) raise frappe.Redirect except Exception as ex: print(ex) proxy = None # Captured/Authorized transaction redirected to home page # TODO: Should we redirec to a "Payment already received" Page? if not proxy: frappe.redirect_to_message(_("Payment Request Not Available"), _("Invalid Payment Request")) frappe.local.flags.redirect_location = '/payments?e={0}'.format( payments.ERROR_NOT_AVAILABLE) raise frappe.Redirect if pr_access and doc.name != pr_access: frappe.redirect_to_message(_("Payment Request Not Available"), _("Invalid Request")) frappe.local.flags.redirect_location = "/payments/{0}?e={1}".format( proxy_name, payments.ERROR_INVALID_KEY) raise frappe.Redirect if not pr_access and frappe.session.user == 'Guest': print("Guest user") frappe.throw(_("You need to be logged in to access this page"), frappe.PermissionError) context["is_backend"] = 1 build_embed_context(context, is_backend=1) if proxy_name and proxy: context["data"] = {key: proxy.get(key) for key in expected_keys} context["billing_countries"] = [ x for x in frappe.get_list("Country", fields=["country_name", "name"], ignore_permissions=1) ] default_country = frappe.get_value("System Settings", "System Settings", "country") default_country_doc = next((x for x in context["billing_countries"] if x.name == default_country), None) country_idx = context["billing_countries"].index(default_country_doc) context["billing_countries"].pop(country_idx) context["billing_countries"] = [default_country_doc ] + context["billing_countries"] else: frappe.redirect_to_message( _('Some information is missing'), _('Looks like someone sent you to an incomplete URL. Please ask them to look into it.' )) frappe.local.flags.redirect_location = frappe.local.response.location raise frappe.Redirect return context
def get_context(context): """This is a controller extension for erpnext.templates.pages.cart""" context["no_cache"] = 1 settings = frappe.db.get("Awc Settings") awc_session = get_awc_session() customer = get_current_customer() context["countries"] = [ x for x in frappe.get_list("Country", fields=["country_name", "name"], ignore_permissions=1) ] default_country = frappe.get_value("System Settings", "System Settings", "country") default_country_doc = next((x for x in context["countries"] if x.name == default_country), None) if frappe.session.user != "Guest": address_links = frappe.get_all("Dynamic Link", filters={"link_name" : customer.name}, fields=["parent"]) addresses = [] for address in address_links: addresses.extend(frappe.get_all("Address", filters={"name" : address.parent, "disabled" : False}, fields="*")) context['addresses'] = addresses customer_info = frappe.db.get_values("Customer", customer.name, ["has_shipping_account", "fedex_account_number"])[0] context["customer_info"] = { "has_shipping_acc": customer_info[0], "fedex_acc_number": customer_info[1] } country_idx = context["countries"].index(default_country_doc) context["countries"].pop(country_idx) context["countries"] = [default_country_doc] + context["countries"] context["shipping_rate_api"] = frappe.get_hooks("shipping_rate_api")[0] context["selected_customer"] = awc_session.get("selected_customer") # ensures clearing address and method selection when visiting checkout page as # shipping address widget won't pre-select them. awc.reset_shipping(None, awc_session=awc_session, customer=customer) # remove? shipping is essential here anyways context.shipping_enabled = 1 if settings.awc_shipping_enabled else 0 related_items = [] skus_in_cart = [] # build upsell sku list using or building cache if necessary for item in awc_session.get("cart", {}).get("items", []): # build cart sku list to dedup later skus_in_cart += [item.get("sku")] # fetches related items to this sku item_related_items = awc.get_related_products_by_sku( item.get("sku"), awc_session=awc_session, customer=customer) # quick list deduping related_items = related_items + list(set(item_related_items) - set(related_items)) # builds upsell item objects using cache if necessary upsell = [] for sku in related_items: if sku not in skus_in_cart: # fetches product data to build upsell widget product_response = awc.get_product_by_sku(sku, awc_session=awc_session) if product_response.get("success"): upsell += [product_response.get("data")] # upsell widget data is made available to the context here context["upsell"] = dict(related_products=upsell) # flag to display login form context.is_logged = awc.is_logged_in() login.apply_context(context) if frappe.response.get("awc_alert"): context["awc_alert"] = frappe.response.get("awc_alert") if context.is_logged: # load gateway provider into context gateway_provider = frappe.get_hooks('awc_gateway_form_provider') if gateway_provider and len(gateway_provider) > 0: context['gateway_provider'] = frappe.call(gateway_provider[0], context=dict( use_address_same_as=1, address_same_as_label="Same as Shipping Address", address_same_as_source="#awc-shipping-form" )) return context
def get_context(context): """This is a controller extension for erpnext.templates.pages.cart""" context["no_cache"] = 1 settings = frappe.db.get("Awc Settings") awc_session = get_awc_session() customer = get_current_customer() context["countries"] = [ x for x in frappe.get_list( "Country", fields=["country_name", "name"], ignore_permissions=1) ] default_country = frappe.get_value("System Settings", "System Settings", "country") default_country_doc = next( (x for x in context["countries"] if x.name == default_country), None) if frappe.session.user != "Guest": address_links = frappe.get_all("Dynamic Link", filters={"link_name": customer.name}, fields=["parent"]) addresses = [] for address in address_links: addresses.extend( frappe.get_all("Address", filters={ "name": address.parent, "disabled": False }, fields="*")) context['addresses'] = addresses customer_info = frappe.db.get_values( "Customer", customer.name, ["has_shipping_account", "fedex_account_number"])[0] context["customer_info"] = { "has_shipping_acc": customer_info[0], "fedex_acc_number": customer_info[1] } country_idx = context["countries"].index(default_country_doc) context["countries"].pop(country_idx) context["countries"] = [default_country_doc] + context["countries"] context["shipping_rate_api"] = frappe.get_hooks("shipping_rate_api")[0] context["selected_customer"] = awc_session.get("selected_customer", False) # ensures clearing address and method selection when visiting checkout page as # shipping address widget won't pre-select them. awc.reset_shipping(None, awc_session=awc_session, customer=customer) # remove? shipping is essential here anyways context.shipping_enabled = 1 if settings.awc_shipping_enabled else 0 related_items = [] skus_in_cart = [] # build upsell sku list using or building cache if necessary for item in awc_session.get("cart", {}).get("items", []): # build cart sku list to dedup later skus_in_cart += [item.get("sku")] # fetches related items to this sku item_related_items = awc.get_related_products_by_sku( item.get("sku"), awc_session=awc_session, customer=customer) # quick list deduping related_items = related_items + list( set(item_related_items) - set(related_items)) # builds upsell item objects using cache if necessary upsell = [] for sku in related_items: if sku not in skus_in_cart: # fetches product data to build upsell widget product_response = awc.get_product_by_sku(sku, awc_session=awc_session) if product_response.get("success"): upsell += [product_response.get("data")] # upsell widget data is made available to the context here context["upsell"] = dict(related_products=upsell) # flag to display login form context.is_logged = frappe.session.user != "Guest" # get settings from site config context["disable_signup"] = cint( frappe.db.get_single_value("Website Settings", "disable_signup")) for provider in ("google", "github", "facebook", "frappe"): if get_oauth_keys(provider): context["{provider}_login".format( provider=provider)] = get_oauth2_authorize_url(provider, redirect_to="/") context["social_login"] = True ldap_settings = LDAPSettings.get_ldap_client_settings() context["ldap_settings"] = ldap_settings return context if frappe.response.get("awc_alert"): context["awc_alert"] = frappe.response.get("awc_alert") user_doc = frappe.get_doc("User", frappe.session.user) context['enable_contacts'] = False if context["selected_customer"] and (user_doc.get("is_power_user") or \ has_role([ "Administrator", "Sales User", "Sales Manager", "System Manager" ], user_doc.name)): # find olddest is_primary or oldest contact context['customer_contacts'] = frappe.db.sql( """ SELECT c.name as name, c.email_id as email_id, c.first_name as first_name, c.last_name as last_name, c.is_primary_contact as is_primary_contact FROM `tabContact` c LEFT JOIN `tabDynamic Link` dl ON dl.parent=c.name WHERE dl.link_doctype='Customer' AND dl.link_name=%(customer_name)s ORDER BY c.is_primary_contact DESC, c.creation asc """, {"customer_name": context["selected_customer"]}, as_dict=True) context['enable_contacts'] = True if len( context['customer_contacts']) > 1 else False if context.is_logged: # load gateway provider into context gateway_provider = frappe.get_hooks('awc_gateway_form_provider') if gateway_provider and len(gateway_provider) > 0: context['gateway_provider'] = frappe.call( gateway_provider[0], context=dict(use_address_same_as=1, address_same_as_label="Same as Shipping Address", address_same_as_source="#awc-shipping-form")) return context