From b63adcbac72b39b7769a17482b3b58d0f69f74b7 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Tue, 9 Apr 2019 18:41:31 +0530 Subject: [PATCH] feat: Allow items not in stock to be added in cart --- erpnext/shopping_cart/cart.py | 20 +++--- .../shopping_cart_settings.json | 61 +++++++++++++++++-- erpnext/templates/includes/product_page.js | 4 +- 3 files changed, 72 insertions(+), 13 deletions(-) diff --git a/erpnext/shopping_cart/cart.py b/erpnext/shopping_cart/cart.py index cc3205c7d70..0abe057af9a 100644 --- a/erpnext/shopping_cart/cart.py +++ b/erpnext/shopping_cart/cart.py @@ -51,7 +51,9 @@ def get_cart_quotation(doc=None): @frappe.whitelist() def place_order(): quotation = _get_cart_quotation() - quotation.company = frappe.db.get_value("Shopping Cart Settings", None, "company") + cart_settings = frappe.db.get_value("Shopping Cart Settings", None, + ["company", "allow_items_not_in_stock"], as_dict=1) + quotation.company = cart_settings.company if not quotation.get("customer_address"): throw(_("{0} is required").format(_(quotation.meta.get_label("customer_address")))) @@ -64,14 +66,16 @@ def place_order(): from erpnext.selling.doctype.quotation.quotation import _make_sales_order sales_order = frappe.get_doc(_make_sales_order(quotation.name, ignore_permissions=True)) - for item in sales_order.get("items"): - item.reserved_warehouse, is_stock_item = frappe.db.get_value("Item", - item.item_code, ["website_warehouse", "is_stock_item"]) - if is_stock_item: - item_stock = get_qty_in_stock(item.item_code, "website_warehouse") - if item.qty > item_stock.stock_qty[0][0]: - throw(_("Only {0} in stock for item {1}").format(item_stock.stock_qty[0][0], item.item_code)) + if not cart_settings.allow_items_not_in_stock: + for item in sales_order.get("items"): + item.reserved_warehouse, is_stock_item = frappe.db.get_value("Item", + item.item_code, ["website_warehouse", "is_stock_item"]) + + if is_stock_item: + item_stock = get_qty_in_stock(item.item_code, "website_warehouse") + if item.qty > item_stock.stock_qty[0][0]: + throw(_("Only {0} in stock for item {1}").format(item_stock.stock_qty[0][0], item.item_code)) sales_order.flags.ignore_permissions = True sales_order.insert() diff --git a/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.json b/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.json index 724c1e9ba26..898a1c39be8 100644 --- a/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.json +++ b/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.json @@ -1,5 +1,6 @@ { "allow_copy": 0, + "allow_events_in_timeline": 0, "allow_guest_to_view": 0, "allow_import": 0, "allow_rename": 0, @@ -19,6 +20,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "enabled", "fieldtype": "Check", "hidden": 0, @@ -51,6 +53,7 @@ "collapsible": 0, "columns": 0, "description": "", + "fetch_if_empty": 0, "fieldname": "display_settings", "fieldtype": "Section Break", "hidden": 0, @@ -84,6 +87,7 @@ "collapsible": 0, "columns": 0, "description": "", + "fetch_if_empty": 0, "fieldname": "show_attachments", "fieldtype": "Check", "hidden": 0, @@ -118,6 +122,7 @@ "columns": 0, "depends_on": "eval:doc.enabled==0", "description": "", + "fetch_if_empty": 0, "fieldname": "show_price", "fieldtype": "Check", "hidden": 0, @@ -150,6 +155,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "column_break_5", "fieldtype": "Column Break", "hidden": 0, @@ -181,6 +187,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "show_stock_availability", "fieldtype": "Check", "hidden": 0, @@ -214,6 +221,7 @@ "collapsible": 0, "columns": 0, "depends_on": "show_stock_availability", + "fetch_if_empty": 0, "fieldname": "show_quantity_in_website", "fieldtype": "Check", "hidden": 0, @@ -246,6 +254,40 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, + "fieldname": "allow_items_not_in_stock", + "fieldtype": "Check", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "label": "Allow items not in stock to be added to cart", + "length": 0, + "no_copy": 0, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "translatable": 0, + "unique": 0 + }, + { + "allow_bulk_edit": 0, + "allow_in_quick_entry": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "fetch_if_empty": 0, "fieldname": "section_break_2", "fieldtype": "Section Break", "hidden": 0, @@ -276,6 +318,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "company", "fieldtype": "Link", "hidden": 0, @@ -309,6 +352,7 @@ "collapsible": 0, "columns": 0, "description": "Prices will not be shown if Price List is not set", + "fetch_if_empty": 0, "fieldname": "price_list", "fieldtype": "Link", "hidden": 0, @@ -342,6 +386,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "column_break_4", "fieldtype": "Column Break", "hidden": 0, @@ -373,6 +418,7 @@ "collapsible": 0, "columns": 0, "description": "", + "fetch_if_empty": 0, "fieldname": "default_customer_group", "fieldtype": "Link", "hidden": 0, @@ -405,6 +451,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "quotation_series", "fieldtype": "Select", "hidden": 0, @@ -437,6 +484,7 @@ "collapsible": 1, "collapsible_depends_on": "eval:doc.enable_checkout", "columns": 0, + "fetch_if_empty": 0, "fieldname": "section_break_8", "fieldtype": "Section Break", "hidden": 0, @@ -469,6 +517,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "enable_checkout", "fieldtype": "Check", "hidden": 0, @@ -503,6 +552,7 @@ "columns": 0, "default": "Orders", "description": "After payment completion redirect user to selected page.", + "fetch_if_empty": 0, "fieldname": "payment_success_url", "fieldtype": "Select", "hidden": 0, @@ -536,6 +586,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "column_break_11", "fieldtype": "Column Break", "hidden": 0, @@ -567,6 +618,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "payment_gateway_account", "fieldtype": "Link", "hidden": 0, @@ -605,8 +657,8 @@ "issingle": 1, "istable": 0, "max_attachments": 0, - "modified": "2018-05-31 03:11:58.911732", - "modified_by": "sushant@digithinkit.com", + "modified": "2019-04-09 18:29:43.270862", + "modified_by": "Administrator", "module": "Shopping Cart", "name": "Shopping Cart Settings", "owner": "Administrator", @@ -637,5 +689,6 @@ "show_name_in_global_search": 0, "sort_order": "ASC", "track_changes": 0, - "track_seen": 0 -} + "track_seen": 0, + "track_views": 0 +} \ No newline at end of file diff --git a/erpnext/templates/includes/product_page.js b/erpnext/templates/includes/product_page.js index ef69e200d9c..af98fc7987a 100644 --- a/erpnext/templates/includes/product_page.js +++ b/erpnext/templates/includes/product_page.js @@ -14,7 +14,9 @@ frappe.ready(function() { callback: function(r) { if(r.message) { if(r.message.cart_settings.enabled) { - $(".item-cart, .item-price, .item-stock").toggleClass("hide", (!!!r.message.product_info.price || !!!r.message.product_info.in_stock)); + let hide_add_to_cart = !r.message.product_info.price + || (!r.message.product_info.in_stock && !r.message.cart_settings.allow_items_not_in_stock); + $(".item-cart, .item-price, .item-stock").toggleClass('hide', hide_add_to_cart); } if(r.message.cart_settings.show_price) { $(".item-price").toggleClass("hide", false);