Merge github.com:frappe/erpnext into develop
diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py
index c216e27..b60aa86 100644
--- a/erpnext/accounts/party.py
+++ b/erpnext/accounts/party.py
@@ -6,6 +6,7 @@
 import frappe
 from frappe import _
 from frappe.defaults import get_restrictions
+from frappe.utils import add_days
 from erpnext.utilities.doctype.address.address import get_address_display
 from erpnext.utilities.doctype.contact.contact import get_contact_details
 
@@ -22,7 +23,8 @@
 	party_bean = frappe.bean(party_type, party)
 	party = party_bean.doc
 
-	set_address_and_contact(out, party, party_type)
+	set_address_details(out, party, party_type)
+	set_contact_details(out, party, party_type)
 	set_other_values(out, party, party_type)
 	set_price_list(out, party, price_list)
 	
@@ -38,20 +40,26 @@
 	
 	return out
 
-def set_address_and_contact(out, party, party_type):
-	out.update({
-		party_type.lower() + "_address": frappe.conn.get_value("Address", 
-			{party_type.lower(): party.name, "is_primary_address":1}, "name"),
-		"contact_person": frappe.conn.get_value("Contact", 
-			{party_type.lower(): party.name, "is_primary_contact":1}, "name")
-	})
+def set_address_details(out, party, party_type):
+	billing_address_field = "customer_address" if party_type == "Lead" \
+		else party_type.lower() + "_address"
+	out[billing_address_field] = frappe.conn.get_value("Address", 
+		{party_type.lower(): party.name, "is_primary_address":1}, "name")
 	
 	# address display
-	out.address_display = get_address_display(out[party_type.lower() + "_address"])
+	out.address_display = get_address_display(out[billing_address_field])
 	
-	# primary contact details
+	# shipping address
+	if party_type in ["Customer", "Lead"]:
+		out.shipping_address_name = frappe.conn.get_value("Address", 
+			{party_type.lower(): party.name, "is_shipping_address":1}, "name")
+		out.shipping_address = get_address_display(out["shipping_address_name"])
+	
+def set_contact_details(out, party, party_type):
+	out.contact_person = frappe.conn.get_value("Contact", 
+		{party_type.lower(): party.name, "is_primary_contact":1}, "name")
+	
 	out.update(get_contact_details(out.contact_person))
-	
 
 def set_other_values(out, party, party_type):
 	# copy
diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py
index 5494011..962a182 100644
--- a/erpnext/controllers/selling_controller.py
+++ b/erpnext/controllers/selling_controller.py
@@ -32,12 +32,13 @@
 			self.set_taxes("other_charges", "taxes_and_charges")
 					
 	def set_missing_lead_customer_details(self):
-		from erpnext.accounts.party import get_party_details
 		if self.doc.customer:
+			from erpnext.accounts.party import get_party_details
 			self.doc.update_if_missing(get_party_details(self.doc.customer))
 		
 		elif self.doc.lead:
-			self.doc.update_if_missing(self.get_lead_defaults())
+			from erpnext.selling.doctype.lead.lead import get_lead_details
+			self.doc.update_if_missing(get_lead_details(self.doc.lead))
 						
 	def set_price_list_and_item_details(self):
 		self.set_price_list_currency("Selling")
diff --git a/erpnext/hr/doctype/hr_settings/hr_settings.txt b/erpnext/hr/doctype/hr_settings/hr_settings.txt
index cd1cf05..d30dc0a 100644
--- a/erpnext/hr/doctype/hr_settings/hr_settings.txt
+++ b/erpnext/hr/doctype/hr_settings/hr_settings.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-08-02 13:45:23", 
   "docstatus": 0, 
-  "modified": "2013-12-20 19:23:14", 
+  "modified": "2014-02-19 17:40:18", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -47,6 +47,7 @@
   "label": "Employee Settings"
  }, 
  {
+  "default": "Naming Series", 
   "description": "Employee record is created using selected field. ", 
   "doctype": "DocField", 
   "fieldname": "emp_created_by", 
diff --git a/erpnext/public/js/utils/party.js b/erpnext/public/js/utils/party.js
index cbd9186..9063b00 100644
--- a/erpnext/public/js/utils/party.js
+++ b/erpnext/public/js/utils/party.js
@@ -36,22 +36,23 @@
 	});
 }
 
-erpnext.utils.get_address_display = function(frm, address_field) {
+erpnext.utils.get_address_display = function(frm, address_field, display_field) {
 	if(frm.updating_party_details) return;
 	if(!address_field) {
 		if(frm.doc.customer) {
 			address_field = "customer_address";
-		} else {
+		} else if(frm.doc.supplier) {
 			address_field = "supplier_address";
-		}
+		} else return;
 	} 
+	if(!display_field) display_field = "address_display";
 	if(frm.doc[address_field]) {
 		frappe.call({
 			method: "erpnext.utilities.doctype.address.address.get_address_display",
-			args: {address: frm.doc[address_field] },
+			args: {"address_dict": frm.doc[address_field] },
 			callback: function(r) {
 				if(r.message)
-					frm.set_value("address_display", r.message)
+					frm.set_value(display_field, r.message)
 			}
 		})
 	}
@@ -60,7 +61,7 @@
 erpnext.utils.get_contact_details = function(frm) {
 	if(frm.updating_party_details) return;
 	
-	if(frm.doc[address_field]) {
+	if(frm.doc["contact_person"]) {
 		frappe.call({
 			method: "erpnext.utilities.doctype.contact.contact.get_contact_details",
 			args: {contact: frm.doc.contact_person },
diff --git a/erpnext/selling/doctype/lead/lead.py b/erpnext/selling/doctype/lead/lead.py
index f0374da..f8f4b47 100644
--- a/erpnext/selling/doctype/lead/lead.py
+++ b/erpnext/selling/doctype/lead/lead.py
@@ -4,8 +4,8 @@
 from __future__ import unicode_literals
 import frappe
 from frappe import _
-from frappe.utils import cstr, validate_email_add, cint, extract_email_id
-from frappe import session, msgprint
+from frappe.utils import cstr, validate_email_add, cint
+from frappe import session
 
 	
 from erpnext.controllers.selling_controller import SellingController
@@ -121,4 +121,27 @@
 			}
 		}}, target_doclist)
 		
-	return [d if isinstance(d, dict) else d.fields for d in doclist]
\ No newline at end of file
+	return [d if isinstance(d, dict) else d.fields for d in doclist]
+	
+@frappe.whitelist()
+def get_lead_details(lead):
+	if not lead: return {}
+	
+	from erpnext.accounts.party import set_address_details
+	out = frappe._dict()
+	
+	lead_bean = frappe.bean("Lead", lead)
+	lead = lead_bean.doc
+		
+	out.update({
+		"territory": lead.territory,
+		"customer_name": lead.company_name or lead.lead_name,
+		"contact_display": lead.lead_name,
+		"contact_email": lead.email_id,
+		"contact_mobile": lead.mobile_no,
+		"contact_phone": lead.phone,
+	})
+	
+	set_address_details(out, lead, "Lead")
+
+	return out
\ No newline at end of file
diff --git a/erpnext/selling/doctype/quotation/quotation.js b/erpnext/selling/doctype/quotation/quotation.js
index 75a6312..28a588a 100644
--- a/erpnext/selling/doctype/quotation/quotation.js
+++ b/erpnext/selling/doctype/quotation/quotation.js
@@ -21,15 +21,6 @@
 			doc.quotation_to = "Customer";
 		else if(doc.lead && !doc.quotation_to)
 			doc.quotation_to = "Lead";
-
-		// to overwrite the customer_filter trigger from queries.js
-		if (doc.lead) {
-			$.each(["customer_address", "shipping_address_name"], 
-				function(i, opts) {
-					me.frm.set_query(opts, erpnext.queries["lead_filter"]);
-				}
-			);
-		}
 	},
 	refresh: function(doc, dt, dn) {
 		this._super(doc, dt, dn);
@@ -75,11 +66,19 @@
 	},
 	
 	quotation_to: function() {
+		var me = this;
 		this.frm.toggle_reqd("lead", this.frm.doc.quotation_to == "Lead");
 		this.frm.toggle_reqd("customer", this.frm.doc.quotation_to == "Customer");
 		if (this.frm.doc.quotation_to == "Lead") {
 			this.frm.set_value("customer", null);
 			this.frm.set_value("contact_person", null);
+			
+			// to overwrite the customer_filter trigger from queries.js
+			$.each(["customer_address", "shipping_address_name"], 
+				function(i, opts) {
+					me.frm.set_query(opts, erpnext.queries["lead_filter"]);
+				}
+			);
 		}
 		else if (this.frm.doc.quotation_to == "Customer")
 			this.frm.set_value("lead", null);
@@ -100,6 +99,23 @@
 			return this._super(party_field);
 		}
 	},
+	
+	lead: function() {
+		var me = this;
+		frappe.call({
+			method: "erpnext.selling.doctype.lead.lead.get_lead_details",
+			args: { "lead": this.frm.doc.lead },
+			callback: function(r) {
+				if(r.message) {
+					me.frm.updating_party_details = true;
+					me.frm.set_value(r.message);
+					me.frm.refresh();
+					me.frm.updating_party_details = false;
+					
+				}
+			}
+		})
+	}
 });
 
 cur_frm.script_manager.make(erpnext.selling.QuotationController);
@@ -108,24 +124,6 @@
 	return{	query: "erpnext.controllers.queries.lead_query" }
 }
 
-cur_frm.cscript.lead = function(doc, cdt, cdn) {
-	if(doc.lead) {
-		unhide_field('territory');
-		return cur_frm.call({
-			doc: cur_frm.doc,
-			method: "set_lead_defaults",
-			callback: function(r) {
-				if(!r.exc) {
-					cur_frm.refresh_fields();
-				}
-			}
-		});
-	}
-}
-
-
-// Make Sales Order
-// =====================================================================================
 cur_frm.cscript['Make Sales Order'] = function() {
 	frappe.model.open_mapped_doc({
 		method: "erpnext.selling.doctype.quotation.quotation.make_sales_order",
@@ -133,8 +131,6 @@
 	})
 }
 
-// declare order lost
-//-------------------------
 cur_frm.cscript['Declare Order Lost'] = function(){
 	var dialog = new frappe.ui.Dialog({
 		title: "Set as Lost",
diff --git a/erpnext/selling/doctype/quotation/quotation.py b/erpnext/selling/doctype/quotation/quotation.py
index eb135f6..ead242b 100644
--- a/erpnext/selling/doctype/quotation/quotation.py
+++ b/erpnext/selling/doctype/quotation/quotation.py
@@ -158,7 +158,7 @@
 			try:
 				customer.insert()
 				return customer
-			except NameError, e:
+			except NameError:
 				if frappe.defaults.get_global_default('cust_master_name') == "Customer Name":
 					customer.run_method("autoname")
 					customer.doc.name += "-" + lead_name
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index 6eb2136..1b48a8e 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -111,6 +111,10 @@
 		erpnext.utils.get_address_display(this.frm, "customer_address");
 	},
 	
+	shipping_address_name: function() {
+		erpnext.utils.get_address_display(this.frm, "shipping_address_name", "shipping_address");
+	},
+	
 	contact_person: function() {
 		erpnext.utils.get_contact_details(this.frm);
 	},
@@ -552,28 +556,6 @@
 			if(df) df.label = label;
 		});
 	},
-	
-	shipping_address_name: function () {
-		var me = this;
-		if(this.frm.doc.shipping_address_name) {
-			frappe.model.with_doc("Address", this.frm.doc.shipping_address_name, function(name) {
-				var address = frappe.model.get_doc("Address", name);
-			
-				var out = $.map(["address_line1", "address_line2", "city"], 
-					function(f) { return address[f]; });
-
-				var state_pincode = $.map(["state", "pincode"], function(f) { return address[f]; }).join(" ");
-				if(state_pincode) out.push(state_pincode);
-			
-				if(address["country"]) out.push(address["country"]);
-			
-				out.concat($.map([["Phone:", address["phone"]], ["Fax:", address["fax"]]], 
-					function(val) { return val[1] ? val.join(" ") : null; }));
-			
-				me.frm.set_value("shipping_address", out.join("\n"));
-			});
-		}
-	}
 });
 
 // Help for Sales BOM items
diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py
index df67c9c..f211cae 100644
--- a/erpnext/stock/get_item_details.py
+++ b/erpnext/stock/get_item_details.py
@@ -34,7 +34,7 @@
 	args = frappe._dict(args)
 	
 	if not args.get("transaction_type"):
-		args.transaction_type = "selling" if args.get("customer") else "buying"
+		args.transaction_type = "buying" if args.get("supplier") else "selling"
 		
 	if not args.get("price_list"):
 		args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
diff --git a/erpnext/utilities/transaction_base.py b/erpnext/utilities/transaction_base.py
index 079b9d1..d8085f2 100644
--- a/erpnext/utilities/transaction_base.py
+++ b/erpnext/utilities/transaction_base.py
@@ -31,20 +31,6 @@
 					break
 
 		return self._party_type_and_name
-				
-	def get_lead_defaults(self):
-		out = self.get_default_address_and_contact("lead")
-		
-		lead = frappe.conn.get_value("Lead", self.doc.lead, 
-			["territory", "company_name", "lead_name"], as_dict=True) or {}
-
-		out["territory"] = lead.get("territory")
-		out["customer_name"] = lead.get("company_name") or lead.get("lead_name")
-
-		return out
-		
-	def set_lead_defaults(self):
-		self.doc.fields.update(self.get_lead_defaults())
 
 	def load_notification_message(self):
 		dt = self.doc.doctype.lower().replace(" ", "_")