Merge pull request #1371 from nabinhait/hotfix

Minor fix in opening purchase invoice and AR report
diff --git a/public/js/queries.js b/public/js/queries.js
index 3c60a91..6fa9ef5 100644
--- a/public/js/queries.js
+++ b/public/js/queries.js
@@ -7,35 +7,35 @@
 	profile: function() {
 		return { query: "core.doctype.profile.profile.profile_query" };
 	},
-	
+
 	lead: function() {
 		return { query: "controllers.queries.lead_query" };
 	},
-	
+
 	customer: function() {
 		return { query: "controllers.queries.customer_query" };
 	},
-	
+
 	supplier: function() {
 		return { query: "controllers.queries.supplier_query" };
 	},
-	
+
 	account: function() {
 		return { query: "controllers.queries.account_query" };
 	},
-	
+
 	item: function() {
 		return { query: "controllers.queries.item_query" };
 	},
-	
+
 	bom: function() {
 		return { query: "controllers.queries.bom" };
 	},
-	
+
 	task: function() {
 		return { query: "projects.utils.query_task" };
 	},
-	
+
 	customer_filter: function(doc) {
 		if(!doc.customer) {
 			wn.throw(wn._("Please specify a") + " " + 
@@ -44,7 +44,7 @@
 		
 		return { filters: { customer: doc.customer } };
 	},
-	
+
 	supplier_filter: function(doc) {
 		if(!doc.supplier) {
 			wn.throw(wn._("Please specify a") + " " + 
@@ -53,9 +53,17 @@
 		
 		return { filters: { supplier: doc.supplier } };
 	},
-	
+
+	lead_filter: function(doc) {
+		if(!doc.lead) {
+			wn.throw(wn._("Please specify a") + " " + 
+				wn._(wn.meta.get_label(doc.doctype, "lead", doc.name)));
+		}
+
+		return { filters: { lead: doc.lead } };
+	},
+
 	not_a_group_filter: function() {
 		return { filters: { is_group: "No" } };
 	},
-	
 });
\ No newline at end of file
diff --git a/selling/doctype/opportunity/opportunity.js b/selling/doctype/opportunity/opportunity.js
index 05970fc..cc093d2 100644
--- a/selling/doctype/opportunity/opportunity.js
+++ b/selling/doctype/opportunity/opportunity.js
@@ -15,13 +15,13 @@
 		if(!this.frm.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(!this.frm.doc.status) 
-			set_multiple(cdt,cdn,{status:'Draft'});
+			set_multiple(cdt, cdn, { status:'Draft' });
 		if(!this.frm.doc.date) 
 			this.frm.doc.transaction_date = date.obj_to_str(new Date());
 		if(!this.frm.doc.company && wn.defaults.get_default("company")) 
-			set_multiple(cdt,cdn,{company:wn.defaults.get_default("company")});
-		if(!this.frm.doc.fiscal_year && sys_defaults.fiscal_year) 
-			set_multiple(cdt,cdn,{fiscal_year:sys_defaults.fiscal_year});		
+			set_multiple(cdt, cdn, { company:wn.defaults.get_default("company") });
+		if(!this.frm.doc.fiscal_year && sys_defaults.fiscal_year)
+			set_multiple(cdt, cdn, { fiscal_year:sys_defaults.fiscal_year });
 	
 		if(this.frm.doc.enquiry_from) {
 			if(this.frm.doc.enquiry_from == 'Customer') {
@@ -99,15 +99,15 @@
 
 $.extend(cur_frm.cscript, new erpnext.selling.Opportunity({frm: cur_frm}));
 
-cur_frm.cscript.refresh = function(doc, cdt, cdn){
+cur_frm.cscript.refresh = function(doc, cdt, cdn) {
 	erpnext.hide_naming_series();
 	cur_frm.clear_custom_buttons();
 	
 	if(doc.docstatus === 1 && doc.status!=="Lost") {
 		cur_frm.add_custom_button(wn._('Create Quotation'), cur_frm.cscript.create_quotation);
-		if(doc.status!=="Quotation") {
+		if(doc.status!=="Quotation")
 			cur_frm.add_custom_button(wn._('Opportunity Lost'), cur_frm.cscript['Declare Opportunity Lost']);
-		}
+
 		cur_frm.add_custom_button(wn._('Send SMS'), cur_frm.cscript.send_sms, "icon-mobile-phone");
 	}
 	
@@ -116,31 +116,29 @@
 }
 
 cur_frm.cscript.onload_post_render = function(doc, cdt, cdn) {
-	if(doc.enquiry_from == 'Lead' && doc.lead) {
-	 	cur_frm.cscript.lead(doc,cdt,cdn);
-	}
+	if(doc.enquiry_from == 'Lead' && doc.lead)
+		cur_frm.cscript.lead(doc, cdt, cdn);
 }
 
 cur_frm.cscript.item_code = function(doc, cdt, cdn) {
 	var d = locals[cdt][cdn];
-	if (d.item_code) {
-		return get_server_fields('get_item_details',d.item_code, 'enquiry_details',doc, cdt,cdn,1);
-	}
+	if (d.item_code)
+		return get_server_fields('get_item_details', d.item_code, 'enquiry_details', doc, cdt, cdn, 1);
 }
 
 // hide - unhide fields on basis of enquiry_from lead or customer
-cur_frm.cscript.enquiry_from = function(doc,cdt,cdn){
-	cur_frm.cscript.lead_cust_show(doc,cdt,cdn);
+cur_frm.cscript.enquiry_from = function(doc, cdt, cdn) {
+	cur_frm.cscript.lead_cust_show(doc, cdt, cdn);
 }
 
 // hide - unhide fields based on lead or customer
-cur_frm.cscript.lead_cust_show = function(doc,cdt,cdn){	
-	if(doc.enquiry_from == 'Lead'){
+cur_frm.cscript.lead_cust_show = function(doc, cdt, cdn) {
+	if(doc.enquiry_from == 'Lead') {
 		unhide_field(['lead']);
 		hide_field(['customer','customer_address','contact_person','customer_name','address_display','contact_display','contact_mobile','contact_email','territory','customer_group']);
 		doc.lead = doc.customer = doc.customer_address = doc.contact_person = doc.address_display = doc.contact_display = doc.contact_mobile = doc.contact_email = doc.territory = doc.customer_group = "";
 	}
-	else if(doc.enquiry_from == 'Customer'){		
+	else if(doc.enquiry_from == 'Customer') {
 		unhide_field(['customer']);
 		hide_field(['lead', 'address_display', 'contact_display', 'contact_mobile', 
 			'contact_email', 'territory', 'customer_group']);		
@@ -164,15 +162,13 @@
 	wn.model.map_current_doc({
 		method: "selling.doctype.lead.lead.make_opportunity",
 		source_name: cur_frm.doc.lead
-	})
+	});
 	
 	unhide_field(['customer_name', 'address_display','contact_mobile', 'customer_address', 
-		'contact_email', 'territory']);	
+		'contact_email', 'territory']);
 }
 
-
-
-cur_frm.cscript['Declare Opportunity Lost'] = function(){
+cur_frm.cscript['Declare Opportunity Lost'] = function() {
 	var dialog = new wn.ui.Dialog({
 		title: wn._("Set as Lost"),
 		fields: [
@@ -200,5 +196,4 @@
 		})
 	});
 	dialog.show();
-	
 }
\ No newline at end of file
diff --git a/selling/doctype/quotation/quotation.js b/selling/doctype/quotation/quotation.js
index c7bf447..c8c919e 100644
--- a/selling/doctype/quotation/quotation.js
+++ b/selling/doctype/quotation/quotation.js
@@ -15,12 +15,21 @@
 
 erpnext.selling.QuotationController = erpnext.selling.SellingController.extend({
 	onload: function(doc, dt, dn) {
+		var me = this;
 		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";
-	
+
+		// 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);
@@ -68,6 +77,12 @@
 	quotation_to: function() {
 		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);
+		}
+		else if (this.frm.doc.quotation_to == "Customer")
+			this.frm.set_value("lead", null);
 	},
 
 	tc_name: function() {
@@ -89,7 +104,7 @@
 
 cur_frm.script_manager.make(erpnext.selling.QuotationController);
 
-cur_frm.fields_dict.lead.get_query = function(doc,cdt,cdn) {
+cur_frm.fields_dict.lead.get_query = function(doc, cdt, cdn) {
 	return{	query:"controllers.queries.lead_query" } }
 
 cur_frm.cscript.lead = function(doc, cdt, cdn) {
@@ -152,7 +167,6 @@
 }
 
 cur_frm.cscript.on_submit = function(doc, cdt, cdn) {
-	if(cint(wn.boot.notification_settings.quotation)) {
+	if(cint(wn.boot.notification_settings.quotation))
 		cur_frm.email_doc(wn.boot.notification_settings.quotation_message);
-	}
 }
\ No newline at end of file
diff --git a/selling/doctype/quotation/quotation.py b/selling/doctype/quotation/quotation.py
index f2546b9..7a68cce 100644
--- a/selling/doctype/quotation/quotation.py
+++ b/selling/doctype/quotation/quotation.py
@@ -3,14 +3,10 @@
 
 from __future__ import unicode_literals
 import webnotes
-
 from webnotes.utils import cstr
 from webnotes.model.bean import getlist
 from webnotes.model.code import get_obj
 from webnotes import _, msgprint
-
-	
-
 from controllers.selling_controller import SellingController
 
 class DocType(SellingController):
diff --git a/selling/doctype/quotation/quotation.txt b/selling/doctype/quotation/quotation.txt
index 93346d3..2bb1c20 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-12-14 17:25:46", 
+  "modified": "2014-01-29 19:42:32", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -665,6 +665,7 @@
   "read_only": 0
  }, 
  {
+  "depends_on": "eval:doc.customer", 
   "doctype": "DocField", 
   "fieldname": "contact_person", 
   "fieldtype": "Link", 
diff --git a/utilities/doctype/address/address.py b/utilities/doctype/address/address.py
index ad6e049..e371b51 100644
--- a/utilities/doctype/address/address.py
+++ b/utilities/doctype/address/address.py
@@ -4,7 +4,7 @@
 from __future__ import unicode_literals
 import webnotes
 
-from webnotes import msgprint
+from webnotes import msgprint, throw, _
 from webnotes.utils import cstr, cint
 
 class DocType:
@@ -16,11 +16,11 @@
 		if not self.doc.address_title:
 			self.doc.address_title = self.doc.customer \
 				or self.doc.supplier or self.doc.sales_partner or self.doc.lead
-				
+
 		if self.doc.address_title:
 			self.doc.name = cstr(self.doc.address_title).strip() + "-" + cstr(self.doc.address_type).strip()
 		else:
-			webnotes.msgprint("""Address Title is mandatory.""" + self.doc.customer, raise_exception=True)
+			throw(_("Address Title is mandatory."))
 		
 	def validate(self):
 		self.validate_primary_address()