Merge pull request #3642 from anandpdoshi/anand-july-13

Fixes to Cart
diff --git a/erpnext/change_log/current/shopping_cart.md b/erpnext/change_log/current/shopping_cart.md
new file mode 100644
index 0000000..77a5e5d
--- /dev/null
+++ b/erpnext/change_log/current/shopping_cart.md
@@ -0,0 +1,3 @@
+- Fixed inconsistent visibility of 'Add to Cart' button
+- Use Customer's Price List in Shopping Cart if found
+- Fixed Address creation from Shopping Cart
diff --git a/erpnext/shopping_cart/cart.py b/erpnext/shopping_cart/cart.py
index 18bdba6..cfba98b 100644
--- a/erpnext/shopping_cart/cart.py
+++ b/erpnext/shopping_cart/cart.py
@@ -7,6 +7,7 @@
 import frappe.defaults
 from frappe.utils import cint, flt, get_fullname, fmt_money, cstr
 from erpnext.utilities.doctype.address.address import get_address_display
+from erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings import get_shopping_cart_settings
 from frappe.utils.nestedset import get_root_of
 
 class WebsitePriceListMissingError(frappe.ValidationError): pass
@@ -162,7 +163,7 @@
 	else:
 		qdoc = frappe.get_doc({
 			"doctype": "Quotation",
-			"naming_series": frappe.defaults.get_user_default("shopping_cart_quotation_series") or "QTN-CART-",
+			"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",
@@ -236,7 +237,9 @@
 
 def set_price_list_and_rate(quotation, cart_settings, billing_territory):
 	"""set price list based on billing territory"""
-	quotation.selling_price_list = cart_settings.get_price_list(billing_territory)
+
+	_set_price_list(quotation, cart_settings, billing_territory)
+
 	# reset values
 	quotation.price_list_currency = quotation.currency = \
 		quotation.plc_conversion_rate = quotation.conversion_rate = None
@@ -249,6 +252,18 @@
 	# set it in cookies for using in product page
 	frappe.local.cookie_manager.set_cookie("selling_price_list", quotation.selling_price_list)
 
+def _set_price_list(quotation, cart_settings, billing_territory):
+	# check if customer price list exists
+	selling_price_list = None
+	if quotation.customer:
+		selling_price_list = frappe.db.get_value("Customer", quotation.customer, "default_price_list")
+
+	# else check for territory based price list
+	if not selling_price_list:
+		selling_price_list = cart_settings.get_price_list(billing_territory)
+
+	quotation.selling_price_list = selling_price_list
+
 def set_taxes(quotation, cart_settings, billing_territory):
 	"""set taxes based on billing territory"""
 	quotation.taxes_and_charges = cart_settings.get_tax_master(billing_territory)
diff --git a/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.py b/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.py
index cdfe0fd..8fbf4a4 100644
--- a/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.py
+++ b/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.py
@@ -23,10 +23,6 @@
 			self.validate_tax_masters()
 			self.validate_exchange_rates_exist()
 
-	def on_update(self):
-		frappe.db.set_default("shopping_cart_enabled", self.get("enabled") or 0)
-		frappe.db.set_default("shopping_cart_quotation_series", self.get("quotation_series"))
-
 	def validate_overlapping_territories(self, parentfield, fieldname):
 		# for displaying message
 		doctype = self.meta.get_field(parentfield).options
diff --git a/erpnext/shopping_cart/product.py b/erpnext/shopping_cart/product.py
index 85faa03..d7795d2 100644
--- a/erpnext/shopping_cart/product.py
+++ b/erpnext/shopping_cart/product.py
@@ -4,19 +4,19 @@
 from __future__ import unicode_literals
 
 import frappe
-from frappe.utils import cint, fmt_money, cstr
+from frappe.utils import cint, fmt_money
 from erpnext.shopping_cart.cart import _get_cart_quotation
-from urllib import unquote
+from erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings import is_cart_enabled
 
 @frappe.whitelist(allow_guest=True)
 def get_product_info(item_code):
 	"""get product price / stock info"""
-	if not cint(frappe.db.get_default("shopping_cart_enabled")):
+	if not is_cart_enabled():
 		return {}
 
 	cart_quotation = _get_cart_quotation()
 
-	price_list = cstr(unquote(frappe.local.request.cookies.get("selling_price_list")))
+	price_list = cart_quotation.selling_price_list
 
 	warehouse = frappe.db.get_value("Item", item_code, "website_warehouse")
 	if warehouse:
diff --git a/erpnext/shopping_cart/utils.py b/erpnext/shopping_cart/utils.py
index 09bfa43..7794a8f 100644
--- a/erpnext/shopping_cart/utils.py
+++ b/erpnext/shopping_cart/utils.py
@@ -9,7 +9,7 @@
 from erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings import is_cart_enabled
 
 def show_cart_count():
-	if (frappe.db.get_default("shopping_cart_enabled") and
+	if (is_cart_enabled() and
 		frappe.db.get_value("User", frappe.session.user, "user_type") == "Website User"):
 		return True
 
diff --git a/erpnext/templates/includes/cart.js b/erpnext/templates/includes/cart.js
index b6f36e6..d5956f9 100644
--- a/erpnext/templates/includes/cart.js
+++ b/erpnext/templates/includes/cart.js
@@ -32,11 +32,11 @@
 		});
 
 		$("#cart-add-shipping-address").on("click", function() {
-			window.location.href = "address?address_fieldname=shipping_address_name";
+			window.location.href = "addresses";
 		});
 
 		$("#cart-add-billing-address").on("click", function() {
-			window.location.href = "address?address_fieldname=customer_address";
+			window.location.href = "address";
 		});
 
 		$(".btn-place-order").on("click", function() {
diff --git a/erpnext/templates/pages/cart.html b/erpnext/templates/pages/cart.html
index 2ffdd5e..e4e4a6a 100644
--- a/erpnext/templates/pages/cart.html
+++ b/erpnext/templates/pages/cart.html
@@ -35,14 +35,14 @@
 					<div id="cart-shipping-address" class="panel-group"
 						data-fieldname="shipping_address_name"></div>
 					<button class="btn btn-default" type="button" id="cart-add-shipping-address">
-						<span class="icon icon-plus"></span> {{ _("New Shipping Address") }}</button>
+						<span class="icon icon-list"></span> {{ _("Manage Addresses") }}</button>
 				</div>
 				<div class="col-md-6">
 					<h4>Billing Address</h4>
 					<div id="cart-billing-address" class="panel-group"
 						data-fieldname="customer_address"></div>
 					<button class="btn btn-default" type="button" id="cart-add-billing-address">
-						<span class="icon icon-plus"></span> {{ _("New Billing Address") }}</button>
+						<span class="icon icon-list"></span> {{ _("Manage Addresses") }}</button>
 				</div>
 			</div>
 			<hr>