Merge branch 'responsive' of github.com:webnotes/erpnext into responsive
diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py
index bf5da8d..9ca68a7 100644
--- a/controllers/accounts_controller.py
+++ b/controllers/accounts_controller.py
@@ -17,8 +17,9 @@
 from __future__ import unicode_literals
 import webnotes
 from webnotes import _, msgprint
-from webnotes.utils import flt, cint
+from webnotes.utils import flt, cint, today
 from setup.utils import get_company_currency, get_price_list_currency
+from accounts.utils import get_fiscal_year
 from utilities.transaction_base import TransactionBase, validate_conversion_rate
 import json
 
@@ -36,6 +37,13 @@
 			self.validate_value("grand_total", ">=", 0)
 			self.set_total_in_words()
 			
+	def set_missing_values(self, for_validate=False):
+		for fieldname in ["posting_date", "transaction_date"]:
+			if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
+				self.doc.fields[fieldname] = today()
+				if not self.doc.fiscal_year:
+					self.doc.fiscal_year = get_fiscal_year(self.doc.fields[fieldname])[0]
+			
 	def set_price_list_currency(self, buying_or_selling):
 		# TODO - change this, since price list now has only one currency allowed
 		if self.meta.get_field("price_list_name") and self.doc.price_list_name and \
@@ -45,6 +53,14 @@
 					"buying_or_selling": buying_or_selling
 				}))
 				
+				if not self.doc.plc_conversion_rate:
+					self.doc.plc_conversion_rate = flt(webnotes.conn.get_value("Price List", 
+						self.doc.price_list_name, "conversion_rate"))
+						
+				if not self.doc.currency:
+					self.doc.currency = self.doc.price_list_currency
+					self.doc.conversion_rate = self.doc.plc_conversion_rate
+				
 	def set_missing_item_details(self, get_item_details):
 		"""set missing item values"""
 		for item in self.doclist.get({"parentfield": self.fname}):
diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py
index 36ce285..52dc3a8 100644
--- a/controllers/buying_controller.py
+++ b/controllers/buying_controller.py
@@ -28,11 +28,8 @@
 
 class BuyingController(StockController):
 	def onload_post_render(self):
-		self.set_price_list_currency("buying")
-
 		# contact, address, item details
 		self.set_missing_values()
-		
 		self.set_taxes("Purchase Taxes and Charges", "purchase_tax_details", "purchase_other_charges")
 	
 	def validate(self):
@@ -41,6 +38,10 @@
 		self.validate_warehouse_belongs_to_company()
 		
 	def set_missing_values(self, for_validate=False):
+		super(BuyingController, self).set_missing_values(for_validate)
+
+		self.set_price_list_currency("Buying")
+		
 		# set contact and address details for supplier, if they are not mentioned
 		if self.doc.supplier and not (self.doc.contact_person and self.doc.supplier_address):
 			for fieldname, val in self.get_default_address_and_contact("supplier").items():
diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py
index 3adc639..087df34 100644
--- a/controllers/selling_controller.py
+++ b/controllers/selling_controller.py
@@ -25,21 +25,41 @@
 
 class SellingController(StockController):
 	def onload_post_render(self):
-		self.set_price_list_currency("selling")
-
 		# contact, address, item details and pos details (if applicable)
 		self.set_missing_values()
 		
 		self.set_taxes("Sales Taxes and Charges", "other_charges", "charge")
 			
 	def set_missing_values(self, for_validate=False):
+		super(SellingController, self).set_missing_values(for_validate)
+		
+		self.set_price_list_currency("Selling")
+		
 		# set contact and address details for customer, if they are not mentioned
-		if self.doc.customer and not (self.doc.contact_person and self.doc.customer_address):
-			for fieldname, val in self.get_default_address_and_contact("customer").items():
-				if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
-					self.doc.fields[fieldname] = val
+		self.set_missing_lead_customer_details()
 					
 		self.set_missing_item_details(get_item_details)
+		
+	def set_missing_lead_customer_details(self):
+		if self.doc.customer:
+			if not (self.doc.contact_person and self.doc.customer_address):
+				for fieldname, val in self.get_default_address_and_contact("customer").items():
+					if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
+						self.doc.fields[fieldname] = val
+		
+			customer_fetch = webnotes.conn.get_value("Customer", self.doc.customer,
+				['customer_name', 'customer_group', 'territory'], as_dict=True)
+			for fieldname in ['customer_name', 'customer_group', 'territory']:
+				if not self.doc.fields.get(fieldname):
+					self.doc.fields[fieldname] = customer_fetch[fieldname]
+			
+		elif self.doc.lead:
+			lead_fetch = webnotes.conn.get_value("Lead", self.doc.lead,
+				['company_name', 'lead_name', 'territory'], as_dict=True)
+			if not self.doc.customer_name:
+				self.doc.customer_name = lead_fetch.company_name or lead_fetch.lead_name
+			if not self.doc.territory:
+				self.doc.territory = lead_fetch.territory
 							
 	def get_other_charges(self):
 		self.doclist = self.doc.clear_table(self.doclist, "other_charges")
@@ -241,7 +261,7 @@
 				" " + _("should be 100%"), raise_exception=True)
 			
 	def validate_order_type(self):
-		valid_types = ["Sales", "Maintenance"]
+		valid_types = ["Sales", "Maintenance", "Shopping Cart"]
 		if self.doc.order_type not in valid_types:
 			msgprint(_(self.meta.get_label("order_type")) + " " + 
 				_("must be one of") + ": " + comma_or(valid_types),
diff --git a/public/js/website_utils.js b/public/js/website_utils.js
index 73fb04b..1484f00 100644
--- a/public/js/website_utils.js
+++ b/public/js/website_utils.js
@@ -201,5 +201,13 @@
 	
 	update_display: function() {
 		$(".cart-count").text("( " + wn.cart.get_count() + " )");
+	},
+	
+	set_value_in_cart: function(item_code, fieldname, value) {
+		var cart = this.get_cart();
+		if(cart[item_code]) {
+			cart[item_code][fieldname] = value;
+			this.set_cart(cart);
+		}
 	}
 });
\ No newline at end of file
diff --git a/selling/doctype/lead/lead.txt b/selling/doctype/lead/lead.txt
index 2463f01..0b602ea 100644
--- a/selling/doctype/lead/lead.txt
+++ b/selling/doctype/lead/lead.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-04-10 11:45:37", 
   "docstatus": 0, 
-  "modified": "2013-04-10 11:49:11", 
+  "modified": "2013-06-14 16:20:17", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -345,6 +345,13 @@
  }, 
  {
   "doctype": "DocField", 
+  "fieldname": "default_price_list", 
+  "fieldtype": "Link", 
+  "label": "Default Price List", 
+  "options": "Price List"
+ }, 
+ {
+  "doctype": "DocField", 
   "fieldname": "fiscal_year", 
   "fieldtype": "Select", 
   "hidden": 1, 
@@ -428,17 +435,6 @@
  }, 
  {
   "doctype": "DocField", 
-  "fieldname": "trash_reason", 
-  "fieldtype": "Small Text", 
-  "label": "Trash Reason", 
-  "no_copy": 1, 
-  "oldfieldname": "trash_reason", 
-  "oldfieldtype": "Small Text", 
-  "print_hide": 1, 
-  "read_only": 1
- }, 
- {
-  "doctype": "DocField", 
   "fieldname": "unsubscribed", 
   "fieldtype": "Check", 
   "label": "Unsubscribed"
diff --git a/selling/doctype/quotation/quotation.py b/selling/doctype/quotation/quotation.py
index c474d99..d33cd6c 100644
--- a/selling/doctype/quotation/quotation.py
+++ b/selling/doctype/quotation/quotation.py
@@ -170,9 +170,6 @@
 	def on_update(self):
 		# Set Quotation Status
 		webnotes.conn.set(self.doc, 'status', 'Draft')
-
-		# subject for follow
-		self.doc.subject = '[%(status)s] To %(customer)s worth %(currency)s %(grand_total)s' % self.doc.fields
 	
 	#update enquiry
 	#------------------
diff --git a/setup/doctype/price_list/price_list.py b/setup/doctype/price_list/price_list.py
index eae6a3c..c4feb60 100644
--- a/setup/doctype/price_list/price_list.py
+++ b/setup/doctype/price_list/price_list.py
@@ -18,12 +18,9 @@
 import webnotes
 from webnotes import msgprint, _
 from webnotes.utils import cint, comma_or
+from webnotes.model.controller import DocListController
 
-
-class DocType:
-	def __init__(self, d, dl):
-		self.doc, self.doclist = d, dl
-		
+class DocType(DocListController):
 	def onload(self):
 		self.doclist.extend(webnotes.conn.sql("""select * from `tabItem Price` 
 			where price_list_name=%s""", self.doc.name, as_dict=True, update={"doctype": "Item Price"}))
diff --git a/setup/doctype/price_list/price_list.txt b/setup/doctype/price_list/price_list.txt
index ed432ec..daffece 100644
--- a/setup/doctype/price_list/price_list.txt
+++ b/setup/doctype/price_list/price_list.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-25 11:35:09", 
   "docstatus": 0, 
-  "modified": "2013-06-11 20:00:07", 
+  "modified": "2013-06-14 16:07:25", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -61,6 +61,13 @@
   "reqd": 1
  }, 
  {
+  "doctype": "DocField", 
+  "fieldname": "conversion_rate", 
+  "fieldtype": "Float", 
+  "label": "Conversion Rate", 
+  "reqd": 1
+ }, 
+ {
   "default": "Selling", 
   "doctype": "DocField", 
   "fieldname": "buying_or_selling", 
diff --git a/setup/doctype/price_list/test_price_list.py b/setup/doctype/price_list/test_price_list.py
index fe87821..dc727c5 100644
--- a/setup/doctype/price_list/test_price_list.py
+++ b/setup/doctype/price_list/test_price_list.py
@@ -3,6 +3,9 @@
 		"doctype": "Price List",
 		"price_list_name": "_Test Price List",
 		"currency": "INR",
-		"valid_for_all_countries": 1
+		"valid_for_all_countries": 1,
+		"buying_or_selling": "Selling",
+		"use_for_website": 1,
+		"conversion_rate": 1.0
 	}]
 ]
\ No newline at end of file
diff --git a/setup/utils.py b/setup/utils.py
index 6948117..b4f38ce 100644
--- a/setup/utils.py
+++ b/setup/utils.py
@@ -41,8 +41,8 @@
 		args = json.loads(args)
 	
 	result = webnotes.conn.sql("""select distinct ref_currency from `tabItem Price`
-		where price_list_name=%s and buying_or_selling=%s""" % ("%s", args.get("buying_or_selling")),
-		(args.get("price_list_name"),))
+		where price_list_name=%s and buying_or_selling=%s""",
+		(args.get("price_list_name"), args.get("buying_or_selling")))
 	if result and len(result)==1:
 		return {"price_list_currency": result[0][0]}
 	else:
diff --git a/stock/doctype/item/item.txt b/stock/doctype/item/item.txt
index 611ae5e..e23bbe0 100644
--- a/stock/doctype/item/item.txt
+++ b/stock/doctype/item/item.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-05-03 10:45:46", 
   "docstatus": 0, 
-  "modified": "2013-05-22 15:49:27", 
+  "modified": "2013-06-13 16:17:42", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -812,16 +812,6 @@
  }, 
  {
   "depends_on": "show_in_website", 
-  "description": "Show Price in Website (if set)", 
-  "doctype": "DocField", 
-  "fieldname": "website_price_list", 
-  "fieldtype": "Link", 
-  "label": "Website Price List", 
-  "options": "Price List", 
-  "read_only": 0
- }, 
- {
-  "depends_on": "show_in_website", 
   "description": "Show \"In Stock\" or \"Not in Stock\" based on stock available in this warehouse.", 
   "doctype": "DocField", 
   "fieldname": "website_warehouse", 
diff --git a/stock/doctype/item/test_item.py b/stock/doctype/item/test_item.py
index f5a688c..e4f1144 100644
--- a/stock/doctype/item/test_item.py
+++ b/stock/doctype/item/test_item.py
@@ -66,7 +66,8 @@
 		"parentfield": "ref_rate_details",
 		"price_list_name": "_Test Price List",
 		"ref_rate": 100,
-		"ref_currency": "INR"
+		"ref_currency": "INR",
+		"buying_or_selling": "Selling"
 	}
 	],
 	[{
diff --git a/website/helpers/cart.py b/website/helpers/cart.py
new file mode 100644
index 0000000..bcea3c2
--- /dev/null
+++ b/website/helpers/cart.py
@@ -0,0 +1,173 @@
+# Copyright (c) 2012 Web Notes Technologies Pvt Ltd.
+# License: GNU General Public License (v3). For more information see license.txt
+
+from __future__ import unicode_literals
+import webnotes
+from webnotes import _, msgprint
+import webnotes.defaults
+from webnotes.utils import today, get_fullname
+import json
+
+@webnotes.whitelist()
+def checkout(cart):
+	# webnotes.msgprint(cart);
+	if isinstance(cart, basestring):
+		cart = json.loads(cart) or {}
+	
+	if webnotes.session.user == "Guest":
+		msgprint(_("Please login before you checkout!"), raise_exception=True)
+	elif webnotes.conn.get_value("Profile", webnotes.session.user, "user_type") != "Partner":
+		msgprint(_("Illegal User"), raise_exception=True)
+	
+	# make_quotation(cart)
+
+def make_quotation(cart):
+	from accounts.utils import get_fiscal_year
+	
+	quotation_defaults = webnotes._dict({
+		"doctype": "Quotation",
+		"naming_series": "QTN-13-14-",
+		"quotation_to": "Customer",
+		"company": webnotes.defaults.get_user_default("company"),
+		"order_type": "Sales",
+		"status": "Draft",
+	})
+	
+	quotation = webnotes.bean(quotation_defaults)
+	quotation.doc.fields.update({
+		"transaction_date": today(),
+		"fiscal_year": get_fiscal_year(today()),
+		
+		# TODO
+		"price_list_name": "fetch",
+		"price_list_currency": "fetch",
+		"plc_conversion_rate": "something",
+		"currency": "same as price_list_currency",
+		"conversion_rate": "same as plc_converion_rate",
+		"territory": "fetch",
+		
+		
+	})
+	
+	# TODO add items
+	for item_code, item in cart.items():
+		pass
+		
+	# TODO apply taxes
+	
+	# save and submit
+
+@webnotes.whitelist()
+def add_to_cart(item_code):
+	party = get_lead_or_customer()
+	quotation = get_shopping_cart_quotation(party)
+	
+	quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": item_code})
+	if not quotation_items:
+		quotation.doclist.append({
+			"doctype": "Quotation Item",
+			"parentfield": "quotation_details",
+			"item_code": item_code,
+			"qty": 1
+		})
+	
+	quotation.ignore_permissions = True
+	quotation.save()
+	
+	return quotation.doc.name
+
+def get_lead_or_customer():
+	customer = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user}, "customer")
+	if customer:
+		return webnotes.doc("Customer", customer)
+	
+	lead = webnotes.conn.get_value("Lead", {"email_id": webnotes.session.user})
+	if lead:
+		return webnotes.doc("Lead", lead)
+	else:
+		lead_bean = webnotes.bean({
+			"doctype": "Lead",
+			"email_id": webnotes.session.user,
+			"lead_name": get_fullname(webnotes.session.user),
+			"status": "Open" # TODO: set something better???
+		})
+		lead_bean.ignore_permissions = True
+		lead_bean.insert()
+		
+		return lead_bean.doc
+		
+def get_shopping_cart_quotation(party):
+	quotation = webnotes.conn.get_value("Quotation", 
+		{party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0})
+	
+	if quotation:
+		qbean = webnotes.bean("Quotation", quotation)
+	else:
+		qbean = webnotes.bean({
+			"doctype": "Quotation",
+			"naming_series": "QTN-CART-",
+			"quotation_to": "Customer",
+			"company": webnotes.defaults.get_user_default("company"),
+			"order_type": "Shopping Cart",
+			"status": "Draft",
+			"__islocal": 1,
+			"price_list_name": get_price_list(party),
+			(party.doctype.lower()): party.name
+		})
+	
+	return qbean
+
+@webnotes.whitelist()
+def remove_from_cart(item_code):
+	pass
+	
+@webnotes.whitelist()
+def update_qty(item_code, qty):
+	pass
+	
+def get_price_list(party):
+	if not party.default_price_list:
+		party.default_price_list = get_price_list_using_geoip()
+		party.save()
+		
+	return party.default_price_list
+
+def get_price_list_using_geoip():
+	country = webnotes.session.get("session_country")
+	price_list_name = None
+
+	if country:
+		price_list_name = webnotes.conn.sql("""select parent 
+			from `tabPrice List Country` plc
+			where country=%s and exists (select name from `tabPrice List` pl
+				where use_for_website=1 and pl.name = plc.parent)""", country)
+	
+	if price_list_name:
+		price_list_name = price_list_name[0][0]
+	else:
+		price_list_name = webnotes.conn.get_value("Price List", 
+			{"use_for_website": 1, "valid_for_all_countries": 1})
+			
+	if not price_list_name:
+		raise Exception, "No website Price List specified"
+	
+	return price_list_name
+
+import unittest
+
+test_dependencies = ["Item", "Price List"]
+
+class TestCart(unittest.TestCase):
+	def test_add_to_cart(self):
+		webnotes.session.user = "test@example.com"
+		add_to_cart("_Test Item")
+		
+	def test_change_qty(self):
+		pass
+		
+	def test_remove_from_cart(self):
+		pass
+		
+	def test_checkout(self):
+		pass
+		
\ No newline at end of file
diff --git a/website/helpers/product.py b/website/helpers/product.py
index a8b60fd..fb4d4e4 100644
--- a/website/helpers/product.py
+++ b/website/helpers/product.py
@@ -4,13 +4,13 @@
 from __future__ import unicode_literals
 
 import webnotes
-from webnotes.utils import cstr, cint
+from webnotes.utils import cstr, cint, fmt_money
 from webnotes.webutils import build_html, delete_page_cache
 
 @webnotes.whitelist(allow_guest=True)
 def get_product_info(item_code):
 	"""get product price / stock info"""
-	price_list = webnotes.conn.get_value("Item", item_code, "website_price_list")
+	price_list = webnotes.conn.get_value("Price List", {"use_for_website": 1})
 	warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse")
 	if warehouse:
 		in_stock = webnotes.conn.sql("""select actual_qty from tabBin where
@@ -27,13 +27,16 @@
 	price = price and price[0] or None
 	
 	if price:
+		price["formatted_price"] = fmt_money(price["ref_rate"], currency=price["ref_currency"])
+		
 		price["ref_currency"] = not cint(webnotes.conn.get_default("hide_currency_symbol")) \
 			and (webnotes.conn.get_value("Currency", price.ref_currency, "symbol") or price.ref_currency) \
 			or ""
 
 	return {
 		"price": price,
-		"stock": in_stock
+		"stock": in_stock,
+		"uom": webnotes.conn.get_value("Item", item_code, "stock_uom")
 	}
 
 @webnotes.whitelist(allow_guest=True)
diff --git a/website/templates/js/cart.js b/website/templates/js/cart.js
index 8746dd6..eaa1fab 100644
--- a/website/templates/js/cart.js
+++ b/website/templates/js/cart.js
@@ -19,6 +19,7 @@
 $(document).ready(function() {
 	// make list of items in the cart
 	wn.cart.render();
+	wn.cart.bind_events();
 });
 
 // shopping cart
@@ -28,23 +29,23 @@
 		var $cart_wrapper = $("#cart-added-items").empty();
 		if(Object.keys(wn.cart.get_cart()).length) {
 			$('<div class="row">\
-				<div class="col col-lg-10 col-sm-10">\
+				<div class="col col-lg-9 col-sm-9">\
 					<div class="row">\
 						<div class="col col-lg-3"></div>\
 						<div class="col col-lg-9"><strong>Item Details</strong></div>\
 					</div>\
 				</div>\
-				<div class="col col-lg-2 col-sm-2"><strong>Qty</strong></div>\
+				<div class="col col-lg-3 col-sm-3"><strong>Qty</strong></div>\
 			</div><hr>').appendTo($cart_wrapper);
 			
 			$.each(wn.cart.get_cart(), function(item_code, item) {
 				item.image_html = item.image ?
 					'<div style="height: 120px; overflow: hidden;"><img src="' + item.image + '" /></div>' :
 					'{% include "app/website/templates/html/product_missing_image.html" %}';
-				item.price_html = item.price ? ('<p>@ ' + item.price + '</p>') : "";
+				item.price_html = item.price ? ('<p>at ' + item.price + '</p>') : "";
 
 				$(repl('<div class="row">\
-					<div class="col col-lg-10 col-sm-10">\
+					<div class="col col-lg-9 col-sm-9">\
 						<div class="row">\
 							<div class="col col-lg-3">%(image_html)s</div>\
 							<div class="col col-lg-9">\
@@ -53,14 +54,47 @@
 							</div>\
 						</div>\
 					</div>\
-					<div class="col col-lg-2 col-sm-2">\
-						<input type="text" placeholder="Qty" value="%(qty)s">\
+					<div class="col col-lg-3 col-sm-3">\
+						<p><input type="text" placeholder="Qty" value="%(qty)s" \
+							item_code="%(item_code)s" class="cart-input-qty"></p>\
 						%(price_html)s\
 					</div>\
 				</div><hr>', item)).appendTo($cart_wrapper);
 			});
+			
+			$('<p class="text-right"><button type="button" class="btn btn-success checkout-btn">\
+				<span class="icon-ok"></span> Checkout</button></p>')
+				.appendTo($cart_wrapper);
+			
 		} else {
 			$('<p class="alert">No Items added to cart.</p>').appendTo($cart_wrapper);
 		}
+	},
+	
+	bind_events: function() {
+		// on change of qty
+		$(".cart-input-qty").on("change", function on_change_of_qty() {
+			wn.cart.set_value_in_cart($(this).attr("item_code"), "qty", $(this).val());
+		});
+		
+		// shopping cart button
+		$(".checkout-btn").on("click", function() {
+			console.log("checkout!");
+			console.log(wn.cart.get_cart());
+			
+			var user_is_logged_in = getCookie("full_name");
+			if(user_is_logged_in) {
+				wn.call({
+					method: "website.helpers.cart.checkout",
+					args: {cart: wn.cart.get_cart()},
+					btn: this,
+					callback: function(r) {
+						console.log(r);
+					}
+				});
+			} else {
+				window.location.href = "login?from=cart";
+			}
+		});
 	}
 });
\ No newline at end of file
diff --git a/website/templates/js/product_page.js b/website/templates/js/product_page.js
index 338f253..e3e4c61 100644
--- a/website/templates/js/product_page.js
+++ b/website/templates/js/product_page.js
@@ -26,8 +26,9 @@
 		success: function(data) {
 			if(data.message) {
 				if(data.message.price) {
-					$("<h4>").html(data.message.price.ref_currency + " " 
-						+ data.message.price.ref_rate).appendTo(".item-price");
+					$("<h4>")
+						.html(data.message.price.formatted_price + " per " + data.message.uom)
+						.appendTo(".item-price");
 					$(".item-price").removeClass("hide");
 				}
 				if(data.message.stock==0) {
diff --git a/website/templates/pages/partners.html b/website/templates/pages/partners.html
index 978987b..93cef3c 100644
--- a/website/templates/pages/partners.html
+++ b/website/templates/pages/partners.html
@@ -4,7 +4,7 @@
 
 {% block content %}
 	<div class="col col-lg-12">
-		<h2 id="blog-title">Sales Partners</h2>
+		<h2 id="blog-title">Partners</h2>
 		<hr>
 		{% for partner_info in partners %}
 		<div class="row">