def get_product_info_for_website(item_code): """get product price / stock info for website""" if not is_cart_enabled(): return {} cart_quotation = _get_cart_quotation() cart_settings = get_shopping_cart_settings() price = get_price( item_code, cart_quotation.selling_price_list, cart_settings.default_customer_group, cart_settings.company ) stock_status = get_qty_in_stock(item_code, "website_warehouse") product_info = { "price": price, "stock_qty": stock_status.stock_qty, "in_stock": stock_status.in_stock if stock_status.is_stock_item else 1, "qty": 0, "uom": frappe.db.get_value("Item", item_code, "stock_uom"), "show_stock_qty": show_quantity_in_website() if stock_status.is_stock_item else 0 } if product_info["price"]: if frappe.session.user != "Guest": item = cart_quotation.get({"item_code": item_code}) if item: product_info["qty"] = item[0].qty return product_info
def get_price(item_code, template_item_code, price_list, qty=1): if price_list: cart_settings = get_shopping_cart_settings() price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"], filters={"price_list": price_list, "item_code": item_code}) if not price: price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"], filters={"price_list": price_list, "item_code": template_item_code}) if price: pricing_rule = get_pricing_rule_for_item(frappe._dict({ "item_code": item_code, "qty": qty, "transaction_type": "selling", "price_list": price_list, "customer_group": cart_settings.default_customer_group, "company": cart_settings.company, "conversion_rate": 1, "for_shopping_cart": True })) if pricing_rule: if pricing_rule.pricing_rule_for == "Discount Percentage": price[0].price_list_rate = flt(price[0].price_list_rate * (1.0 - (pricing_rule.discount_percentage / 100.0))) if pricing_rule.pricing_rule_for == "Price": price[0].price_list_rate = pricing_rule.price_list_rate return price[0]
def _get_cart_quotation(party=None): if not party: party = get_customer() quotation = frappe.get_all("Quotation", fields=["name"], filters= {party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0}, order_by="modified desc", limit_page_length=1) if quotation: qdoc = frappe.get_doc("Quotation", quotation[0].name) else: qdoc = frappe.get_doc({ "doctype": "Quotation", "naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-", "quotation_to": party.doctype, "company": frappe.db.get_value("Shopping Cart Settings", None, "company"), "order_type": "Shopping Cart", "status": "Draft", "docstatus": 0, "__islocal": 1, (party.doctype.lower()): party.name }) qdoc.contact_person = frappe.db.get_value("Contact", {"email_id": frappe.session.user, "customer": party.name}) qdoc.contact_email = frappe.session.user qdoc.flags.ignore_permissions = True qdoc.run_method("set_missing_values") apply_cart_settings(party, qdoc) return qdoc
def _get_cart_quotation(party=None): '''Return the open Quotation of type "Shopping Cart" or make a new one''' if not party: party = get_party() quotation = frappe.get_all("Quotation", fields=["name"], filters= {"party_name": party.name, "order_type": "Shopping Cart", "docstatus": 0}, order_by="modified desc", limit_page_length=1) if quotation: qdoc = frappe.get_doc("Quotation", quotation[0].name) else: [company, price_list] = frappe.db.get_value("Shopping Cart Settings", None, ["company", "price_list"]) qdoc = frappe.get_doc({ "doctype": "Quotation", "naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-", "quotation_to": party.doctype, "company": company, "selling_price_list": price_list, "order_type": "Shopping Cart", "status": "Draft", "docstatus": 0, "__islocal": 1, "party_name": party.name }) qdoc.contact_person = frappe.db.get_value("Contact", {"email_id": frappe.session.user}) qdoc.contact_email = frappe.session.user qdoc.flags.ignore_permissions = True qdoc.run_method("set_missing_values") apply_cart_settings(party, qdoc) return qdoc
def get_customer(user=None): if not user: user = frappe.session.user customer = frappe.db.get_value("Contact", {"email_id": user}, "customer") if customer: return frappe.get_doc("Customer", customer) else: customer = frappe.new_doc("Customer") fullname = get_fullname(user) customer.update({ "customer_name": fullname, "customer_type": "Individual", "customer_group": get_shopping_cart_settings().default_customer_group, "territory": get_root_of("Territory") }) customer.insert(ignore_permissions=True) contact = frappe.new_doc("Contact") contact.update({ "customer": customer.name, "first_name": fullname, "email_id": user }) contact.insert(ignore_permissions=True) return customer
def _get_cart_quotation(party=None): if not party: party = get_party() quotation = frappe.get_all("Quotation", fields=["name"], filters= {party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0}, order_by="modified desc", limit_page_length=1) if quotation: qdoc = frappe.get_doc("Quotation", quotation[0].name) else: qdoc = frappe.get_doc({ "doctype": "Quotation", "naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-", "quotation_to": party.doctype, "company": frappe.db.get_value("Shopping Cart Settings", None, "company"), "order_type": "Shopping Cart", "status": "Draft", "docstatus": 0, "__islocal": 1, (party.doctype.lower()): party.name }) qdoc.contact_person = frappe.db.get_value("Contact", {"email_id": frappe.session.user, "customer": party.name}) qdoc.contact_email = frappe.session.user qdoc.flags.ignore_permissions = True qdoc.run_method("set_missing_values") apply_cart_settings(party, qdoc) return qdoc
def request_for_quotation(): quotation = _get_cart_quotation() quotation.flags.ignore_permissions = True quotation.save() if not get_shopping_cart_settings().save_quotations_as_draft: quotation.submit() return quotation.name
def get_product_info_for_website(item_code): """get product price / stock info for website""" cart_quotation = _get_cart_quotation() cart_settings = get_shopping_cart_settings() price = get_price( item_code, cart_quotation.selling_price_list, cart_settings.default_customer_group, cart_settings.company ) stock_status = get_qty_in_stock(item_code, "website_warehouse") product_info = { "price": price, "stock_qty": stock_status.stock_qty, "in_stock": stock_status.in_stock if stock_status.is_stock_item else 1, "qty": 0, "uom": frappe.db.get_value("Item", item_code, "stock_uom"), "show_stock_qty": show_quantity_in_website(), "sales_uom": frappe.db.get_value("Item", item_code, "sales_uom") } if product_info["price"]: if frappe.session.user != "Guest": item = cart_quotation.get({"item_code": item_code}) if item: product_info["qty"] = item[0].qty return { "product_info": product_info, "cart_settings": cart_settings }
def _get_cart_quotation(party=None): if not party: party = get_lead_or_customer() quotation = frappe.db.get_value("Quotation", {party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0}) if quotation: qdoc = frappe.get_doc("Quotation", quotation) else: qdoc = frappe.get_doc({ "doctype": "Quotation", "naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-", "quotation_to": party.doctype, "company": frappe.db.get_value("Shopping Cart Settings", None, "company"), "order_type": "Shopping Cart", "status": "Draft", "docstatus": 0, "__islocal": 1, (party.doctype.lower()): party.name }) if party.doctype == "Customer": qdoc.contact_person = frappe.db.get_value("Contact", {"email_id": frappe.session.user, "customer": party.name}) qdoc.flags.ignore_permissions = True qdoc.run_method("set_missing_values") apply_cart_settings(party, qdoc) return qdoc
def get_party(user=None): if not user: user = frappe.session.user contact_name = frappe.db.get_value("Contact", {"user": user}) if not contact_name: contact_name = get_contact_name(user) party = None if contact_name: contact = frappe.get_doc('Contact', contact_name) if contact.links: party_doctype = contact.links[0].link_doctype party = contact.links[0].link_name cart_settings = frappe.get_doc("Shopping Cart Settings") debtors_account = '' if cart_settings.enable_checkout: debtors_account = get_debtors_account(cart_settings) if party: return frappe.get_doc(party_doctype, party) else: if not cart_settings.enabled: frappe.local.flags.redirect_location = "/contact" raise frappe.Redirect customer = frappe.new_doc("Customer") fullname = get_fullname(user) customer.update({ "customer_name": fullname, "customer_type": "Individual", "customer_group": get_shopping_cart_settings().default_customer_group, "territory": get_root_of("Territory") }) if debtors_account: customer.update({ "accounts": [{ "company": cart_settings.company, "account": debtors_account }] }) customer.flags.ignore_mandatory = True customer.insert(ignore_permissions=True) contact = frappe.new_doc("Contact") contact.update({"first_name": fullname}) contact.add_email(user, is_primary=True) contact.append('links', dict(link_doctype='Customer', link_name=customer.name)) contact.flags.ignore_mandatory = True contact.insert(ignore_permissions=True) return customer
def get_party(user=None): if not user: user = frappe.session.user contact_name = frappe.db.get_value("Contact", {"email_id": user}) party = None if contact_name: contact = frappe.get_doc('Contact', contact_name) if contact.links: party_doctype = contact.links[0].link_doctype party = contact.links[0].link_name cart_settings = frappe.get_doc("Shopping Cart Settings") debtors_account = '' if cart_settings.enable_checkout: debtors_account = get_debtors_account(cart_settings) if party: return frappe.get_doc(party_doctype, party) else: if not cart_settings.enabled: frappe.local.flags.redirect_location = "/contact" raise frappe.Redirect customer = frappe.new_doc("Customer") fullname = get_fullname(user) customer.update({ "customer_name": fullname, "customer_type": "Individual", "customer_group": get_shopping_cart_settings().default_customer_group, "territory": get_root_of("Territory") }) if debtors_account: customer.update({ "accounts": [{ "company": cart_settings.company, "account": debtors_account }] }) customer.flags.ignore_mandatory = True customer.insert(ignore_permissions=True) contact = frappe.new_doc("Contact") contact.update({ "first_name": fullname, "email_id": user }) contact.append('links', dict(link_doctype='Customer', link_name=customer.name)) contact.flags.ignore_mandatory = True contact.insert(ignore_permissions=True) return customer
def get_party(user=None): if not user: user = frappe.session.user party = frappe.db.get_value("Contact", {"email_id": user}, ["customer", "supplier"], as_dict=1) if party: party_doctype = 'Customer' if party.customer else 'Supplier' party = party.customer or party.supplier cart_settings = frappe.get_doc("Shopping Cart Settings") debtors_account = '' if cart_settings.enable_checkout: debtors_account = get_debtors_account(cart_settings) if party: return frappe.get_doc(party_doctype, party) else: if not cart_settings.enabled: return None customer = frappe.new_doc("Customer") fullname = get_fullname(user) customer.update({ "customer_name": fullname, "customer_type": "Individual", "customer_group": get_shopping_cart_settings().default_customer_group, "territory": get_root_of("Territory") }) if debtors_account: customer.update({ "accounts": [{ "company": cart_settings.company, "account": debtors_account }] }) customer.flags.ignore_mandatory = True customer.insert(ignore_permissions=True) contact = frappe.new_doc("Contact") contact.update({ "customer": customer.name, "first_name": fullname, "email_id": user }) contact.flags.ignore_mandatory = True contact.insert(ignore_permissions=True) return customer
def get_product_info_for_website(item_code, skip_quotation_creation=False): """get product price / stock info for website""" cart_settings = get_shopping_cart_settings() if not cart_settings.enabled: return frappe._dict() cart_quotation = frappe._dict() if not skip_quotation_creation: cart_quotation = _get_cart_quotation() selling_price_list = cart_quotation.get( "selling_price_list") if cart_quotation else _set_price_list( cart_settings, None) price = get_price( item_code, #cart_quotation.selling_price_list, selling_price_list, cart_settings.default_customer_group, cart_settings.company) stock_status = get_qty_in_stock(item_code, "website_warehouse") product_info = { "price": price, "stock_qty": stock_status.stock_qty, "in_stock": stock_status.in_stock if stock_status.is_stock_item else get_non_stock_item_status(item_code, "website_warehouse"), "qty": 0, "uom": frappe.db.get_value("Item", item_code, "stock_uom"), "show_stock_qty": show_quantity_in_website(), "sales_uom": frappe.db.get_value("Item", item_code, "sales_uom") } if product_info["price"]: if frappe.session.user != "Guest": # item = cart_quotation.get({"item_code": item_code}) item = cart_quotation.get({"item_code": item_code }) if cart_quotation else None if item: product_info["qty"] = item[0].qty return frappe._dict({ "product_info": product_info, "cart_settings": cart_settings })
def get_party(user=None): if not user: user = frappe.session.user party = frappe.db.get_value("Contact", {"email_id": user}, ["customer", "supplier"], as_dict=1) if party: party_doctype = 'Customer' if party.customer else 'Supplier' party = party.customer or party.supplier cart_settings = frappe.get_doc("Shopping Cart Settings") debtors_account = '' if cart_settings.enable_checkout: debtors_account = get_debtors_account(cart_settings) if party: return frappe.get_doc(party_doctype, party) else: if not cart_settings.enabled: return None customer = frappe.new_doc("Customer") fullname = get_fullname(user) customer.update({ "customer_name": fullname, "customer_type": "Individual", "customer_group": get_shopping_cart_settings().default_customer_group, "territory": get_root_of("Territory") }) if debtors_account: customer.update({ "accounts": [{ "company": cart_settings.company, "account": debtors_account }] }) customer.flags.ignore_mandatory = True customer.insert(ignore_permissions=True) contact = frappe.new_doc("Contact") contact.update({ "customer": customer.name, "first_name": fullname, "email_id": user }) contact.flags.ignore_mandatory = True contact.insert(ignore_permissions=True) return customer
def get_slot_price(item_code, qty, price_list=None): from erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings import get_shopping_cart_settings from erpnext.utilities.product import get_price settings = get_shopping_cart_settings() if not price_list: price_list = settings.price_list price = get_price(item_code, price_list, settings.default_customer_group, settings.company, qty) return fmt_money(float(price.price_list_rate) * float(qty), currency=price.currency)
def get_product_info_for_website1(item_code): # custom implementation print(item_code,'--------------------------------------') print(""" = overriden get_product_info_for_website = """) # original cart_settings = get_shopping_cart_settings() if not cart_settings.enabled: return frappe._dict() cart_quotation = _get_cart_quotation() price = get_price( item_code, cart_quotation.selling_price_list, cart_settings.default_customer_group, cart_settings.company ) stock_status = get_qty_in_stock(item_code, "website_warehouse") product_info = { "price": price, "stock_qty": stock_status.stock_qty, "in_stock": stock_status.in_stock if stock_status.is_stock_item else 1, "qty": 0, "uom": "ll", "show_stock_qty": show_quantity_in_website(), "sales_uom": frappe.db.get_value("Item", item_code, "sales_uom"), "white_list":"ashish" } if product_info["price"]: if frappe.session.user != "Guest": item = cart_quotation.get({"item_code": item_code}) if item: product_info["qty"] = 99999999999 return frappe._dict({ "product_info": product_info, "cart_settings": cart_settings })
def _get_cart_quotation(party=None): if not party: party = get_lead_or_customer() quotation = frappe.db.get_value( "Quotation", { party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0 }) if quotation: qdoc = frappe.get_doc("Quotation", quotation) else: qdoc = frappe.get_doc({ "doctype": "Quotation", "naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-", "quotation_to": party.doctype, "company": frappe.db.get_value("Shopping Cart Settings", None, "company"), "order_type": "Shopping Cart", "status": "Draft", "docstatus": 0, "__islocal": 1, (party.doctype.lower()): party.name }) if party.doctype == "Customer": qdoc.contact_person = frappe.db.get_value( "Contact", { "email_id": frappe.session.user, "customer": party.name }) qdoc.flags.ignore_permissions = True qdoc.run_method("set_missing_values") apply_cart_settings(party, qdoc) return qdoc
def get_context(context): context.no_cache = 1 context.show_sidebar = True # setup cart context shopping_cart_settings = get_shopping_cart_settings() context.payment_gateway_list = shopping_cart_settings.gateways context.enabled_checkout = shopping_cart_settings.enable_checkout # setup document context context.doc = frappe.get_doc(frappe.form_dict.doctype, frappe.form_dict.name) if not frappe.has_website_permission(context.doc): frappe.throw(_("Not Permitted"), frappe.PermissionError) context.parents = frappe.form_dict.parents context.title = frappe.form_dict.name context.payment_ref = frappe.db.get_value( "Payment Request", {"reference_name": frappe.form_dict.name}, "name") default_print_format = frappe.db.get_value( 'Property Setter', dict(property='default_print_format', doc_type=frappe.form_dict.doctype), "value") context.print_format = default_print_format or "Standard" if hasattr(context.doc, "set_indicator"): context.doc.set_indicator() if show_attachments(): context.attachments = get_attachments(frappe.form_dict.doctype, frappe.form_dict.name) # setup loyalty program for the customer, if available customer_loyalty_program = frappe.db.get_value("Customer", context.doc.customer, "loyalty_program") if customer_loyalty_program: from erpnext.accounts.doctype.loyalty_program.loyalty_program import get_loyalty_program_details_with_points loyalty_program_details = get_loyalty_program_details_with_points( context.doc.customer, customer_loyalty_program) context.available_loyalty_points = int( loyalty_program_details.get("loyalty_points"))
def get_item_price(item_code, uom): cart_settings = get_shopping_cart_settings() if not cart_settings.enabled: return frappe._dict() cart_quotation = _get_cart_quotation() price = get_price(item_code=item_code, price_list=cart_quotation.selling_price_list, customer_group=cart_settings.default_customer_group, company=cart_settings.company, uom=uom) return { "item_name": frappe.db.get_value("Item", item_code, "item_name"), "price": price }
def get_price(item_code, template_item_code, price_list, qty=1): if price_list: cart_settings = get_shopping_cart_settings() price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"], filters={ "price_list": price_list, "item_code": item_code }) if template_item_code and not price: price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"], filters={ "price_list": price_list, "item_code": template_item_code }) if price: pricing_rule = get_pricing_rule_for_item( frappe._dict({ "item_code": item_code, "qty": qty, "transaction_type": "selling", "price_list": price_list, "customer_group": cart_settings.default_customer_group, "company": cart_settings.company, "conversion_rate": 1, "for_shopping_cart": True })) if pricing_rule: if pricing_rule.pricing_rule_for == "Discount Percentage": price[0].price_list_rate = flt( price[0].price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0))) if pricing_rule.pricing_rule_for == "Price": price[0].price_list_rate = pricing_rule.price_list_rate return price[0]
def get_context(context): doctype = frappe.local.request.args.get('doctype') name = frappe.local.request.args.get("name") if not doctype or not name: context.error = "Provided Payment URL is invalid." # setup cart context shopping_cart_settings = get_shopping_cart_settings() context.payment_gateway_list = shopping_cart_settings.gateways context.enabled_checkout = shopping_cart_settings.enable_checkout if not context.enabled_checkout: context.error = "Please enable checkout to continue." # setup document context context.doc = frappe.get_doc(doctype, name) if not frappe.has_website_permission(context.doc): frappe.throw(_("Not Permitted"), frappe.PermissionError)
def get_slot_price(item_code, qty, uom, price_list=None): from erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings import get_shopping_cart_settings from shared_place.shared_place.utils import get_price, get_sp_uom settings = get_shopping_cart_settings() if not price_list: price_list = settings.price_list uom = get_sp_uom(uom) price = get_price(item_code, price_list, uom, settings.default_customer_group, settings.company, qty) if not price: price = 0 currency = frappe.get_cached_value('Global Defaults', 'None', 'default_currency') else: currency = price.currency price = price.price_list_rate return fmt_money(float(price) * float(qty), currency=currency)
def get_customer(user=None): if not user: user = frappe.session.user customer = frappe.db.get_value("Contact", {"email_id": user}, "customer") cart_settings = frappe.get_doc("Shopping Cart Settings") debtors_account = "" if cart_settings.enable_checkout: debtors_account = get_debtors_account(cart_settings) if customer: return frappe.get_doc("Customer", customer) else: customer = frappe.new_doc("Customer") fullname = get_fullname(user) customer.update( { "customer_name": fullname, "customer_type": "Individual", "customer_group": get_shopping_cart_settings().default_customer_group, "territory": get_root_of("Territory"), } ) if debtors_account: customer.update({"accounts": [{"company": cart_settings.company, "account": debtors_account.name}]}) customer.flags.ignore_mandatory = True customer.insert(ignore_permissions=True) contact = frappe.new_doc("Contact") contact.update({"customer": customer.name, "first_name": fullname, "email_id": user}) contact.flags.ignore_mandatory = True contact.insert(ignore_permissions=True) return customer
def reset_website_customer(): settings = get_shopping_cart_settings() #flag order_for feature as disabled if stopped by the sales team user frappe.session.data.order_for['enabled'] = False customer_name = frappe.session.data.order_for.get("customer_name") if frappe.session.data.order_for.get("customer_name"): del frappe.session.data.order_for["customer_name"] if frappe.session.data.order_for.get("customer_primary_contact_name"): del frappe.session.data.order_for["customer_primary_contact_name"] if settings.get("stop_order_for_behavior") == "Reload": url = "Reload" if settings.get("stop_order_for_behavior" ) == "Back to Customer Record" and customer_name: url = "/desk#Form/Customer/{}".format(customer_name) else: url = settings.get("stop_order_for_url", "") or "Reload" # Hook: Allows overriding the routing url after a user resets the website customer # # Signature: # override_stop_order_for_url(url) # # Args: # url: The current route # # Returns: # Hook expects a string or None to override the route hooks = frappe.get_hooks("override_stop_order_for_url") or [] for method in hooks: url = frappe.call(method, url=url) or url if not url: url = "Reload" return url
def _get_cart_quotation(party=None): '''Return the open Quotation of type "Shopping Cart" or make a new one''' if not party: party = get_party() quotation = frappe.get_all("Quotation", fields=["name"], filters={ "party_name": party.name, "order_type": "Shopping Cart", "docstatus": 0 }, order_by="modified desc", limit_page_length=1) if quotation: qdoc = frappe.get_doc("Quotation", quotation[0].name) else: company = frappe.db.get_value("Shopping Cart Settings", None, ["company"]) qdoc = frappe.get_doc({ "doctype": "Quotation", "naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-", "quotation_to": party.doctype, "company": company, "order_type": "Shopping Cart", "status": "Draft", "docstatus": 0, "__islocal": 1, "party_name": party.name }) contact_email_parent = frappe.db.get_value( "Contact Email", {"email_id": frappe.session.user}, ["parent"]) contacts = frappe.db.get_value("Contact", contact_email_parent, ["name", "title"], as_dict=True) if contacts: qdoc.contact_person = contacts.name qdoc.contact_display = contacts.title qdoc.contact_email = frappe.session.user qdoc.flags.ignore_permissions = True # Order For feature: if "order_for" in frappe.session.data: customer_name = frappe.session.data.order_for.get("customer_name") if customer_name: # override contact person primary_contact = frappe.session.data.order_for.get( "customer_primary_contact_name") contact_details = frappe.db.get_value("Contact", primary_contact, ["title", "email_id"], as_dict=True) qdoc.contact_person = primary_contact qdoc.contact_display = contact_details.title qdoc.contact_email = contact_details.email_id # override customer qdoc.party_name = customer_name # Hook: Allows overriding cart quotation creation for shopping cart # # Signature: # override_shopping_cart_get_quotation(party, qdoc) # # Args: # party: A doctype object(ex: Customer) # qdoc: Quotation doctype hooks = frappe.get_hooks("override_shopping_cart_get_quotation") or [] for method in hooks: frappe.call(method, party=party, qdoc=qdoc) qdoc.run_method("set_missing_values") apply_cart_settings(party, qdoc) return qdoc
def _get_cart_quotation(party=None): '''Return the open Quotation of type "Shopping Cart" or make a new one''' if not party: party = get_party() quotation = frappe.get_all("Quotation", fields=["name"], filters={ "party_name": party.name, "order_type": "Shopping Cart", "docstatus": 0 }, order_by="modified desc", limit_page_length=1) if quotation: qdoc = frappe.get_doc("Quotation", quotation[0].name) else: company = frappe.db.get_value("Shopping Cart Settings", None, ["company"]) qdoc = frappe.get_doc({ "doctype": "Quotation", "naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-", "quotation_to": party.doctype, "company": company, "order_type": "Shopping Cart", "status": "Draft", "docstatus": 0, "__islocal": 1, "party_name": party.name }) qdoc.contact_person = frappe.db.get_value( "Contact", {"email_id": frappe.session.user}) qdoc.contact_email = frappe.session.user qdoc.flags.ignore_permissions = True # Hook: Allows overriding cart quotation creation for shopping cart # # Signature: # override_shopping_cart_get_quotation(party, qdoc) # # Args: # party: A doctype object(ex: Customer) # qdoc: Quotation doctype hooks = frappe.get_hooks("override_shopping_cart_get_quotation") or [] for method in hooks: frappe.call(method, party=party, qdoc=qdoc) qdoc.run_method("set_missing_values") apply_cart_settings(party, qdoc) return qdoc
def create_customer_or_supplier(): '''Based on the default Role (Customer, Supplier), create a Customer / Supplier. Called on_session_creation hook. ''' user = frappe.session.user if frappe.db.get_value('User', user, 'user_type') != 'Website User': return user_roles = frappe.get_roles() portal_settings = frappe.get_single('Portal Settings') default_role = portal_settings.default_role if default_role not in ['Customer', 'Supplier']: return # create customer / supplier if the user has that role if portal_settings.default_role and portal_settings.default_role in user_roles: doctype = portal_settings.default_role else: doctype = None if not doctype: return if party_exists(doctype, user): return party = frappe.new_doc(doctype) fullname = frappe.utils.get_fullname(user) if doctype == 'Customer': cart_settings = get_shopping_cart_settings() if cart_settings.enable_checkout: debtors_account = get_debtors_account(cart_settings) else: debtors_account = '' party.update({ "customer_name": fullname, "customer_type": "Individual", "customer_group": cart_settings.default_customer_group, "territory": get_root_of("Territory") }) if debtors_account: party.update({ "accounts": [{ "company": cart_settings.company, "account": debtors_account }] }) else: party.update({ "supplier_name": fullname, "supplier_group": "All Supplier Groups", "supplier_type": "Individual" }) party.flags.ignore_mandatory = True party.insert(ignore_permissions=True) alternate_doctype = "Customer" if doctype == "Supplier" else "Supplier" if party_exists(alternate_doctype, user): # if user is both customer and supplier, alter fullname to avoid contact name duplication fullname += "-" + doctype create_party_contact(doctype, fullname, user, party.name) return party
def create_customer_or_supplier(): '''Based on the default Role (Customer, Supplier), create a Customer / Supplier. Called on_session_creation hook. ''' user = frappe.session.user if frappe.db.get_value('User', user, 'user_type') != 'Website User': return user_roles = frappe.get_roles() portal_settings = frappe.get_single('Portal Settings') default_role = portal_settings.default_role if default_role not in ['Customer', 'Supplier']: return # create customer / supplier if the user has that role if portal_settings.default_role and portal_settings.default_role in user_roles: doctype = portal_settings.default_role else: doctype = None if not doctype: return if party_exists(doctype, user): return party = frappe.new_doc(doctype) fullname = frappe.utils.get_fullname(user) if doctype == 'Customer': cart_settings = get_shopping_cart_settings() if cart_settings.enable_checkout: debtors_account = get_debtors_account(cart_settings) else: debtors_account = '' party.update({ "customer_name": fullname, "customer_type": "Individual", "customer_group": cart_settings.default_customer_group, "territory": get_root_of("Territory") }) if debtors_account: party.update({ "accounts": [{ "company": cart_settings.company, "account": debtors_account }] }) else: party.update({ "supplier_name": fullname, "supplier_group": "All Supplier Groups", "supplier_type": "Individual" }) party.flags.ignore_mandatory = True party.insert(ignore_permissions=True) contact = frappe.new_doc("Contact") contact.update({"first_name": fullname, "email_id": user}) contact.append('links', dict(link_doctype=doctype, link_name=party.name)) contact.flags.ignore_mandatory = True contact.insert(ignore_permissions=True) return party
def _get_cart_quotation(party=None): ''' Return the open Quotation of type "Shopping Cart" or make a new one ''' lead = None lead_name = None if not party: session = get_extra_cart_session() lead = create_lead_if_needed(session['token']) party = _party(session['token']) elif party and party['name']: lead = create_lead_if_needed(party['name']) if not lead: lead_name = frappe.db.sql(''' select name from `tabLead` where lead_name = %s order by creation desc limit 1 ''', (party['name'], ), as_dict=True) quotation = None if lead_name or lead.lead_name: quotation = frappe.get_all( "Quotation", fields=["name"], filters={ "party_name": lead.name if lead else lead_name[0].name, "order_type": "Shopping Cart", "docstatus": 0 }, order_by="modified desc", limit_page_length=1) if quotation: qdoc = frappe.get_doc("Quotation", quotation[0].name) else: company = frappe.db.get_value("Shopping Cart Settings", None, ["company"]) qdoc = frappe.get_doc({ "doctype": "Quotation", "naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-", "quotation_to": party['doctype'], "company": company, "order_type": "Shopping Cart", "status": "Draft", "docstatus": 0, "__islocal": 1, "party_name": lead_name[0].name if lead_name else lead.name }) # qdoc.contact_person = frappe.db.get_value("Contact", {"email_id": frappe.session.user}) # qdoc.contact_email = frappe.session.user qdoc.flags.ignore_permissions = True qdoc.run_method("set_missing_values") apply_cart_settings(party, qdoc) return qdoc
def get_next_attribute_and_values(item_code, selected_attributes): '''Find the count of Items that match the selected attributes. Also, find the attribute values that are not applicable for further searching. If less than equal to 10 items are found, return item_codes of those items. If one item is matched exactly, return item_code of that item. ''' selected_attributes = frappe.parse_json(selected_attributes) item_cache = ItemVariantsCacheManager(item_code) item_variants_data = item_cache.get_item_variants_data() attributes = get_item_attributes(item_code) attribute_list = [a.attribute for a in attributes] filtered_items = get_items_with_selected_attributes( item_code, selected_attributes) next_attribute = None for attribute in attribute_list: if attribute not in selected_attributes: next_attribute = attribute break valid_options_for_attributes = frappe._dict({}) for a in attribute_list: valid_options_for_attributes[a] = set() selected_attribute = selected_attributes.get(a, None) if selected_attribute: # already selected attribute values are valid options valid_options_for_attributes[a].add(selected_attribute) for row in item_variants_data: item_code, attribute, attribute_value = row if item_code in filtered_items and attribute not in selected_attributes and attribute in attribute_list: valid_options_for_attributes[attribute].add(attribute_value) optional_attributes = item_cache.get_optional_attributes() exact_match = [] shopping_cart_settings = get_shopping_cart_settings() allow_items_not_in_stock = cint( shopping_cart_settings.allow_items_not_in_stock) # search for exact match if all selected attributes are required attributes if len(selected_attributes.keys()) >= (len(attribute_list) - len(optional_attributes)): item_attribute_value_map = item_cache.get_item_attribute_value_map() for item_code, attr_dict in item_attribute_value_map.items(): if item_code in filtered_items and set(attr_dict.keys()) == set( selected_attributes.keys()): exact_match.append(item_code) filtered_items_count = len(filtered_items) # get product info if exact match from erpnext.shopping_cart.product_info import get_product_info_for_website if exact_match: data = get_product_info_for_website(exact_match[0]) product_info = data.product_info if not data.cart_settings.show_price: product_info = None else: product_info = None return { 'next_attribute': next_attribute, 'valid_options_for_attributes': valid_options_for_attributes, 'filtered_items_count': filtered_items_count, 'filtered_items': filtered_items if filtered_items_count < 10 else [], 'exact_match': exact_match, 'allow_items_not_in_stock': allow_items_not_in_stock, 'product_info': product_info }
def get_party(user=None): if not user: user = frappe.session.user contact_name = get_contact_name(user) party = None # Order For feature. Sets current customer's primary contact for this session. if "order_for" in frappe.session.data: customer_name = frappe.session.data.order_for.get("customer_name") if customer_name and "customer_primary_contact_name" in frappe.session.data.order_for: contact_name = frappe.session.data.order_for.customer_primary_contact_name # Hook: Allows overriding the contact person used by the shopping cart # # Signature: # override_shopping_cart_get_party_contact(contact_name) # # Args: # contact_name: The contact name that will be used to fetch a party # # Returns: # Hook expects a string or None to override the contact_name hooks = frappe.get_hooks("override_shopping_cart_get_party_contact") or [] for method in hooks: contact_name = frappe.call(method, contact_name=contact_name) or contact_name if contact_name: contact = frappe.get_doc('Contact', contact_name) if contact.links: party_doctype, party = _get_customer_or_lead(contact) cart_settings = frappe.get_doc("Shopping Cart Settings") debtors_account = '' if cart_settings.enable_checkout: debtors_account = get_debtors_account(cart_settings) if party: return frappe.get_doc(party_doctype, party) else: if not cart_settings.enabled: frappe.local.flags.redirect_location = "/contact" raise frappe.Redirect fullname = get_fullname(user) contact = frappe.db.get_value("Contact Email", {"email_id": user}, "parent") customer = frappe.new_doc("Customer") customer.update({ "customer_name": fullname, "customer_type": "Individual", "customer_group": get_shopping_cart_settings().default_customer_group, "territory": get_root_of("Territory"), "customer_primary_contact": contact }) if debtors_account: customer.update({ "accounts": [{ "company": cart_settings.company, "account": debtors_account }] }) customer.customer_primary_contact = contact customer.flags.ignore_mandatory = True customer.save(ignore_permissions=True) if not contact: contact = frappe.new_doc("Contact") contact.update({ "first_name": fullname, "email_ids": [{ "email_id": user, "is_primary": 1 }] }) else: contact = frappe.get_doc("Contact", contact) contact.append('links', dict(link_doctype='Customer', link_name=customer.name)) contact.flags.ignore_mandatory = True contact.save(ignore_permissions=True) if not customer.customer_primary_contact: customer.customer_primary_contact = contact.name return customer