[forms] cleanup, fixing usability and added form dashboard
diff --git a/manufacturing/doctype/production_order/production_order.txt b/manufacturing/doctype/production_order/production_order.txt
index 53b7f1c..38a98fe 100644
--- a/manufacturing/doctype/production_order/production_order.txt
+++ b/manufacturing/doctype/production_order/production_order.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-10 16:34:16", 
   "docstatus": 0, 
-  "modified": "2013-01-29 17:17:31", 
+  "modified": "2013-07-02 11:56:54", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -108,6 +108,14 @@
  }, 
  {
   "doctype": "DocField", 
+  "fieldname": "wip_warehouse", 
+  "fieldtype": "Link", 
+  "label": "Work-in-Progress Warehouse", 
+  "options": "Warehouse", 
+  "reqd": 1
+ }, 
+ {
+  "doctype": "DocField", 
   "fieldname": "column_break1", 
   "fieldtype": "Column Break", 
   "oldfieldtype": "Column Break", 
@@ -174,6 +182,13 @@
  }, 
  {
   "doctype": "DocField", 
+  "fieldname": "description", 
+  "fieldtype": "Small Text", 
+  "label": "Item Description", 
+  "read_only": 1
+ }, 
+ {
+  "doctype": "DocField", 
   "fieldname": "project_name", 
   "fieldtype": "Link", 
   "in_filter": 1, 
diff --git a/manufacturing/doctype/production_planning_tool/production_planning_tool.py b/manufacturing/doctype/production_planning_tool/production_planning_tool.py
index ed7f7bf..26644ad 100644
--- a/manufacturing/doctype/production_planning_tool/production_planning_tool.py
+++ b/manufacturing/doctype/production_planning_tool/production_planning_tool.py
@@ -210,7 +210,6 @@
 				"wip_warehouse"		: "",
 				"fg_warehouse"		: "",
 				"status"			: "Draft",
-				"fiscal_year"		: webnotes.conn.get_default("fiscal_year")
 			}
 		return bom_dict, item_dict
 		
diff --git a/selling/doctype/customer/customer.js b/selling/doctype/customer/customer.js
index 35a6f59..c0f6eab 100644
--- a/selling/doctype/customer/customer.js
+++ b/selling/doctype/customer/customer.js
@@ -32,6 +32,7 @@
 cur_frm.add_fetch('default_sales_partner','commission_rate','default_commission_rate');
 
 cur_frm.cscript.refresh = function(doc,dt,dn) {
+	cur_frm.layout.clear_dashboard();
 	if(sys_defaults.cust_master_name == 'Customer Name')
 		hide_field('naming_series');
 	else
@@ -39,7 +40,8 @@
 
 	if(doc.__islocal){		
 		hide_field(['address_html','contact_html']);
-	}else{
+	}else{		
+		cur_frm.cscript.setup_dashboard(doc);
 		unhide_field(['address_html','contact_html']);
 		// make lists
 		cur_frm.cscript.make_address(doc,dt,dn);
@@ -53,6 +55,35 @@
 	}
 }
 
+cur_frm.cscript.setup_dashboard = function(doc) {
+	cur_frm.layout.dashboard.toggle(true);
+	var headline = $('<div class="form-headline col col-lg-12">\
+		<span class="text-muted">Loading...</span></div>')
+		.appendTo(cur_frm.layout.dashboard);
+	
+	cur_frm.layout.add_doctype_badge(wn._("Opportunities"), "Opportunity", "customer");
+	cur_frm.layout.add_doctype_badge(wn._("Quotations"), "Quotation", "customer");
+	cur_frm.layout.add_doctype_badge(wn._("Sales Orders"), "Sales Order", "customer");
+	cur_frm.layout.add_doctype_badge(wn._("Delivery Notes"), "Delivery Note", "customer");
+	cur_frm.layout.add_doctype_badge(wn._("Sales Invoices"), "Sales Invoice", "customer");
+	
+	wn.call({
+		type: "GET",
+		method:"selling.doctype.customer.customer.get_dashboard_info",
+		args: {
+			customer: cur_frm.doc.name
+		},
+		callback: function(r) {
+			cur_frm.layout.dashboard.find(".form-headline")
+				.html(wn._("Total Billing This Year: ") + "<b>" 
+					+ format_currency(r.message.total_billing, cur_frm.doc.default_currency)
+					+ '</b> / <span class="text-muted">' + wn._("Unpaid") + ": <b>" + 
+				 		format_currency(r.message.total_unpaid, cur_frm.doc.default_currency) + '</b></span>');
+			cur_frm.layout.set_badge_count(r.message);
+		}
+	})
+}
+
 cur_frm.cscript.make_address = function() {
 	if(!cur_frm.address_list) {
 		cur_frm.address_list = new wn.ui.Listing({
@@ -95,4 +126,4 @@
 }
 
 
-cur_frm.fields_dict.lead_name.get_query = erpnext.utils.lead_query;
+cur_frm.fields_dict.lead_name.get_query = erpnext.utils.lead_query;
\ No newline at end of file
diff --git a/selling/doctype/customer/customer.py b/selling/doctype/customer/customer.py
index 10d2ce5..83c10a5 100644
--- a/selling/doctype/customer/customer.py
+++ b/selling/doctype/customer/customer.py
@@ -173,4 +173,25 @@
 
 		#update master_name in doctype account
 		webnotes.conn.sql("""update `tabAccount` set master_name = %s, 
-			master_type = 'Customer' where master_name = %s""", (new,old))
\ No newline at end of file
+			master_type = 'Customer' where master_name = %s""", (new,old))
+
+@webnotes.whitelist()
+def get_dashboard_info(customer):
+	if not webnotes.has_permission("Customer", customer):
+		webnotes.msgprint("No Permission", raise_exception=True)
+	
+	out = {}
+	for doctype in ["Opportunity", "Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
+		out[doctype] = webnotes.conn.get_value(doctype, 
+			{"customer": customer, "docstatus": ["!=", 2] }, "count(*)")
+	
+	billing = webnotes.conn.sql("""select sum(grand_total), sum(outstanding_amount) 
+		from `tabSales Invoice` 
+		where customer=%s 
+			and docstatus = 1
+			and fiscal_year = %s""", (customer, webnotes.conn.get_default("fiscal_year")))
+	
+	out["total_billing"] = billing[0][0]
+	out["total_unpaid"] = billing[0][1]
+	
+	return out
\ No newline at end of file
diff --git a/selling/doctype/opportunity/opportunity.js b/selling/doctype/opportunity/opportunity.js
index 3a56d4d..a4c84af 100644
--- a/selling/doctype/opportunity/opportunity.js
+++ b/selling/doctype/opportunity/opportunity.js
@@ -34,11 +34,21 @@
 // ===============================================================
 cur_frm.cscript.onload = function(doc, cdt, cdn) {
 
-	if(!doc.enquiry_from) hide_field(['customer', 'customer_address', 'contact_person', 'customer_name','lead', 'address_display', 'contact_display', 'contact_mobile', 'contact_email', 'territory', 'customer_group']);
-	if(!doc.status) set_multiple(cdt,cdn,{status:'Draft'});
-	if(!doc.date) doc.transaction_date = date.obj_to_str(new Date());
-	if(!doc.company && sys_defaults.company) set_multiple(cdt,cdn,{company:sys_defaults.company});
-	if(!doc.fiscal_year && sys_defaults.fiscal_year) set_multiple(cdt,cdn,{fiscal_year:sys_defaults.fiscal_year});		
+	if(!doc.enquiry_from && doc.customer)
+		doc.enquiry_from = "Customer";
+	if(!doc.enquiry_from && doc.lead)
+		doc.enquiry_from = "Lead";
+
+	if(!doc.enquiry_from) 
+		hide_field(['customer', 'customer_address', 'contact_person', 'customer_name','lead', 'address_display', 'contact_display', 'contact_mobile', 'contact_email', 'territory', 'customer_group']);
+	if(!doc.status) 
+		set_multiple(cdt,cdn,{status:'Draft'});
+	if(!doc.date) 
+		doc.transaction_date = date.obj_to_str(new Date());
+	if(!doc.company && sys_defaults.company) 
+		set_multiple(cdt,cdn,{company:sys_defaults.company});
+	if(!doc.fiscal_year && sys_defaults.fiscal_year) 
+		set_multiple(cdt,cdn,{fiscal_year:sys_defaults.fiscal_year});		
 	
 	if(doc.enquiry_from) {
 		if(doc.enquiry_from == 'Customer') {
@@ -61,6 +71,8 @@
 	if(cur_frm.fields_dict.contact_by.df.options.match(/^Profile/)) {
 		cur_frm.fields_dict.contact_by.get_query = erpnext.utils.profile_query;
 	}
+	
+	if(doc.customer && !doc.customer_name) cur_frm.cscript.customer(doc);
 }
 
 cur_frm.cscript.onload_post_render = function(doc, cdt, cdn) {
diff --git a/selling/doctype/opportunity/opportunity.txt b/selling/doctype/opportunity/opportunity.txt
index fc4d041..7b86df2 100644
--- a/selling/doctype/opportunity/opportunity.txt
+++ b/selling/doctype/opportunity/opportunity.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-03-07 18:50:30", 
   "docstatus": 0, 
-  "modified": "2013-06-11 16:03:41", 
+  "modified": "2013-07-02 17:22:21", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -44,6 +44,12 @@
   "name": "Opportunity"
  }, 
  {
+  "doctype": "DocField", 
+  "fieldname": "from_section", 
+  "fieldtype": "Section Break", 
+  "label": "From"
+ }, 
+ {
   "description": "To manage multiple series please go to Setup > Manage Series", 
   "doctype": "DocField", 
   "fieldname": "naming_series", 
diff --git a/selling/doctype/quotation/quotation.js b/selling/doctype/quotation/quotation.js
index 4276193..654779c 100644
--- a/selling/doctype/quotation/quotation.js
+++ b/selling/doctype/quotation/quotation.js
@@ -26,8 +26,16 @@
 wn.require('app/selling/doctype/sales_common/sales_common.js');
 
 erpnext.selling.QuotationController = erpnext.selling.SellingController.extend({
+	onload: function(doc, dt, dn) {
+		this._super(doc, dt, dn);
+		if(doc.customer && !doc.quotation_to)
+			doc.quotation_to = "Customer";
+		else if(doc.lead && !doc.quotation_to)
+			doc.quotation_to = "Lead";
+	
+	},
 	refresh: function(doc, dt, dn) {
-		this._super();
+		this._super(doc, dt, dn);
 		
 		if(doc.docstatus == 1 && doc.status!='Order Lost') {
 			cur_frm.add_custom_button('Make Sales Order', cur_frm.cscript['Make Sales Order']);
diff --git a/selling/doctype/quotation/quotation.txt b/selling/doctype/quotation/quotation.txt
index b9772c8..433ccf3 100644
--- a/selling/doctype/quotation/quotation.txt
+++ b/selling/doctype/quotation/quotation.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-05-24 19:29:08", 
   "docstatus": 0, 
-  "modified": "2013-06-28 12:47:10", 
+  "modified": "2013-07-02 16:49:52", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -44,6 +44,12 @@
  }, 
  {
   "doctype": "DocField", 
+  "fieldname": "customer_section", 
+  "fieldtype": "Section Break", 
+  "label": "Customer"
+ }, 
+ {
+  "doctype": "DocField", 
   "fieldname": "column_break0", 
   "fieldtype": "Column Break", 
   "read_only": 0, 
diff --git a/selling/page/selling_home/selling_home.js b/selling/page/selling_home/selling_home.js
index 8eae737..be0d9dd 100644
--- a/selling/page/selling_home/selling_home.js
+++ b/selling/page/selling_home/selling_home.js
@@ -8,6 +8,11 @@
 		icon: "icon-copy",
 		items: [
 			{
+				label: wn._("Customer"),
+				description: wn._("Customer database."),
+				doctype:"Customer"
+			},
+			{
 				label: wn._("Lead"),
 				description: wn._("Database of potential customers."),
 				doctype:"Lead"
@@ -34,11 +39,6 @@
 		icon: "icon-book",
 		items: [
 			{
-				label: wn._("Customer"),
-				description: wn._("Customer database."),
-				doctype:"Customer"
-			},
-			{
 				label: wn._("Contact"),
 				description: wn._("All Contacts."),
 				doctype:"Contact"