Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/erpnext/accounts/doctype/c_form/c_form.js b/erpnext/accounts/doctype/c_form/c_form.js
index 4fb3227..d9e5c68 100644
--- a/erpnext/accounts/doctype/c_form/c_form.js
+++ b/erpnext/accounts/doctype/c_form/c_form.js
@@ -17,10 +17,13 @@
 //c-form js file
 // -----------------------------
 cur_frm.fields_dict.invoice_details.grid.get_field("invoice_no").get_query = function(doc) {
-	return 'SELECT `tabSales Invoice`.`name` FROM `tabSales Invoice` WHERE `tabSales Invoice`.`company` = "' +doc.company+'" AND `tabSales Invoice`.%(key)s LIKE "%s" AND `tabSales Invoice`.`customer` = "' + doc.customer + '" AND `tabSales Invoice`.`docstatus` = 1 and `tabSales Invoice`.`c_form_applicable` = "Yes" and ifnull(`tabSales Invoice`.c_form_no, "") = "" ORDER BY `tabSales Invoice`.`name` ASC LIMIT 50';
+	cond = ""
+	if (doc.customer) cond += ' AND `tabSales Invoice`.`customer` = "' + cstr(doc.customer) + '"';
+	if (doc.company) cond += ' AND `tabSales Invoice`.`company` = "' + cstr(doc.company) + '"';
+	return 'SELECT `tabSales Invoice`.`name` FROM `tabSales Invoice` WHERE `tabSales Invoice`.`docstatus` = 1 and `tabSales Invoice`.`c_form_applicable` = "Yes" and ifnull(`tabSales Invoice`.c_form_no, "") = ""'+cond+' AND `tabSales Invoice`.%(key)s LIKE "%s" ORDER BY `tabSales Invoice`.`name` ASC LIMIT 50';
 }
 
 cur_frm.cscript.invoice_no = function(doc, cdt, cdn) {
 	var d = locals[cdt][cdn];
 	get_server_fields('get_invoice_details', d.invoice_no, 'invoice_details', doc, cdt, cdn, 1);
-}
+}
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index 668d2ef..9e2eb73 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -173,15 +173,13 @@
 		self.doclist = get_obj('GL Control').get_advances(self, self.doc.credit_to, 'Purchase Invoice Advance','advance_allocation_details','debit')
 		
 		
-	# ============= OTHER CHARGES ====================
-	
-	# Get Tax rate if account type is TAX
-	# ------------------------------------
 	def get_rate(self,arg):
 		return get_obj('Purchase Common').get_rate(arg,self)
 
-	# Get Purchase Taxes and Charges Master
-	# -----------------------------------------------------------
+	def load_default_taxes(self):
+		self.doclist = get_obj('Purchase Common').load_default_taxes(self)
+	
+	
 	def get_purchase_tax_details(self):
 		self.doclist =  get_obj('Purchase Common').get_purchase_tax_details(self)
 
diff --git a/erpnext/accounts/doctype/purchase_taxes_and_charges_master/purchase_taxes_and_charges_master.txt b/erpnext/accounts/doctype/purchase_taxes_and_charges_master/purchase_taxes_and_charges_master.txt
index 1ecac40..15b4dfa 100644
--- a/erpnext/accounts/doctype/purchase_taxes_and_charges_master/purchase_taxes_and_charges_master.txt
+++ b/erpnext/accounts/doctype/purchase_taxes_and_charges_master/purchase_taxes_and_charges_master.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-03-27 14:35:51',
+		'creation': '2012-05-15 12:14:32',
 		'docstatus': 0,
-		'modified': '2012-03-27 14:35:51',
+		'modified': '2012-08-17 09:57:04',
 		'modified_by': u'Administrator',
 		'owner': u'wasim@webnotestech.com'
 	},
@@ -24,7 +24,7 @@
 		'section_style': u'Simple',
 		'server_code_error': u' ',
 		'show_in_menu': 0,
-		'version': 18
+		'version': 1
 	},
 
 	# These values are common for all DocField
@@ -109,6 +109,14 @@
 	# DocField
 	{
 		'doctype': u'DocField',
+		'fieldname': u'is_default',
+		'fieldtype': u'Check',
+		'label': u'Default'
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
 		'fieldname': u'company',
 		'fieldtype': u'Link',
 		'in_filter': 1,
@@ -127,4 +135,4 @@
 		'oldfieldtype': u'Table',
 		'options': u'Purchase Taxes and Charges'
 	}
-]
+]
\ No newline at end of file
diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.js b/erpnext/buying/doctype/purchase_common/purchase_common.js
index 522a704..eedc59b 100644
--- a/erpnext/buying/doctype/purchase_common/purchase_common.js
+++ b/erpnext/buying/doctype/purchase_common/purchase_common.js
@@ -29,10 +29,26 @@
 		}
 }
 
+cur_frm.cscript.load_taxes = function(doc, cdt, cdn, callback) {
+	// run if this is not executed from dt_map...
+	doc = locals[doc.doctype][doc.name];
+	if(doc.supplier || getchildren('Purchase Taxes and Charges', doc.name, 'purchase_tax_details', doc.doctype).length) {
+		if(callback) {
+			callback(doc, cdt, cdn);
+		}
+	} else {
+		$c_obj(make_doclist(doc.doctype, doc.name),'load_default_taxes','',function(r,rt){
+			refresh_field('purchase_tax_details');
+			if(callback) callback(doc, cdt, cdn);
+		});
+	}
+}
+
+
 
 // Gets called after existing item details are update to fill in
 // remaining default values
-cur_frm.cscript.load_defaults = function(doc, dt, dn) {
+cur_frm.cscript.load_defaults = function(doc, dt, dn, callback) {
 	if(!cur_frm.doc.__islocal) { return; }
 
 	doc = locals[doc.doctype][doc.name];
@@ -46,6 +62,7 @@
 		LocalDB.set_default_values(children[i]);
 	}
 	refresh_field(cur_frm.cscript.fname);
+	cur_frm.cscript.load_taxes(doc, dt, dn, callback);
 }
 
 // Update existing item details
@@ -58,10 +75,11 @@
 			if(!r.exc) {
 				refresh_field(cur_frm.cscript.fname);
 				doc = locals[doc.doctype][doc.name];
-				cur_frm.cscript.load_defaults(doc, dt, dn);
-				if(callback) callback(doc, dt, dn);
+				cur_frm.cscript.load_defaults(doc, dt, dn, callback);
 			}
 		});
+	} else {
+		cur_frm.cscript.load_taxes(doc, dt, dn, callback);
 	}
 }
 
diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.py b/erpnext/buying/doctype/purchase_common/purchase_common.py
index 5a65d52..8d1d3b5 100644
--- a/erpnext/buying/doctype/purchase_common/purchase_common.py
+++ b/erpnext/buying/doctype/purchase_common/purchase_common.py
@@ -623,15 +623,23 @@
 			msgprint("'%s' Not Within The Fiscal Year"%(dn))
 			raise Exception			
 
-
-#---Other charges starting point------------------------
-
-	# Get other charges from Master
-	# =================================================================================
-	def get_purchase_tax_details(self,obj):
+	def load_default_taxes(self, obj):
+		return self.get_purchase_tax_details(obj, 1)
+	
+	def get_purchase_tax_details(self,obj, default = 0):
 		obj.doclist = self.doc.clear_table(obj.doclist,'purchase_tax_details')
+		
+		if default: add_cond = " and ifnull(t2.is_default,0) = 1"
+		else: add_cond = " and t1.parent = '"+cstr(obj.doc.purchase_other_charges)+"'"
+
+		other_charge = sql("""
+			select t1.*
+			from `tabPurchase Taxes and Charges` t1, `tabPurchase Taxes and Charges Master` t2
+			where t1.parent = t2.name %s
+			order by t1.idx
+		"""% add_cond, as_dict = 1)
+		
 		idx = 0
-		other_charge = sql("select category, add_deduct_tax, charge_type,row_id,description,account_head,rate,tax_amount from `tabPurchase Taxes and Charges` where parent = '%s' order by idx" %(obj.doc.purchase_other_charges), as_dict = 1)
 		for other in other_charge:
 			d =	addchild(obj.doc, 'purchase_tax_details', 'Purchase Taxes and Charges', 1, obj.doclist)
 			d.category = other['category']
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js
index a1983c3..4e8c2c0 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.js
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.js
@@ -36,7 +36,10 @@
 }
 
 cur_frm.cscript.onload_post_render = function(doc, dt, dn) {
-	if(doc.__islocal) cur_frm.cscript.get_default_schedule_date(doc);
+	var callback = function(doc, dt, dn) {
+		if(doc.__islocal) cur_frm.cscript.get_default_schedule_date(doc);
+	}
+	cur_frm.cscript.update_item_details(doc, dt, dn, callback);
 }
 
 cur_frm.cscript.refresh = function(doc, cdt, cdn) { 
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index d84d06e..55f59b5 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -56,6 +56,7 @@
 
 	# Get Item Details
 	def get_item_details(self, arg =''):
+		import json
 		if arg:
 			return get_obj(dt='Purchase Common').get_item_details(self,arg)
 		else:
@@ -359,13 +360,11 @@
 		self.update_rw_material_detail()
 		
 
-# OTHER CHARGES TRIGGER FUNCTIONS
-# ====================================================================================
-	
-	# *********** Get Tax rate if account type is TAX ********************
 	def get_rate(self,arg):
-		return get_obj('Purchase Common').get_rate(arg,self)
+		return get_obj('Purchase Common').get_rate(arg,self)	
+	
+	def load_default_taxes(self):
+		self.doclist = get_obj('Purchase Common').load_default_taxes(self)
 
-	# **** Pull details from other charges master (Get Other Charges) ****
 	def get_purchase_tax_details(self):
 		self.doclist = get_obj('Purchase Common').get_purchase_tax_details(self)
diff --git a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
index 430d1ee..6135ad4 100644
--- a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
+++ b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.js
@@ -38,6 +38,7 @@
 cur_frm.cscript.refresh = function(doc, dt, dn) {
 	erpnext.hide_naming_series();
 	cur_frm.cscript.dynamic_label(doc, dt, dn);
+	cur_frm.cscript.load_taxes(doc, dt, dn);
 	
 	cur_frm.cscript.toggle_contact_section(doc);
 	
diff --git a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.py b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.py
index f5b9335..8821976 100644
--- a/erpnext/buying/doctype/supplier_quotation/supplier_quotation.py
+++ b/erpnext/buying/doctype/supplier_quotation/supplier_quotation.py
@@ -72,6 +72,9 @@
 					d.purchase_ref_rate = d.discount_rate = d.purchase_rate = 0.0
 					d.import_ref_rate = d.import_rate = 0.0
 	
+	def load_default_taxes(self):
+		self.doclist = get_obj('Purchase Common').load_default_taxes(self)
+	
 	def get_purchase_tax_details(self):
 		self.doclist = get_obj('Purchase Common').get_purchase_tax_details(self)
 
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index 10fecbf..807b365 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -468,15 +468,11 @@
 				d.current_stock = bin and flt(bin[0]['actual_qty']) or 0
 
 
-
-
-# OTHER CHARGES TRIGGER FUNCTIONS
-# ====================================================================================
-
-	# *********** Get Tax rate if account type is TAX ********************
 	def get_rate(self,arg):
 		return get_obj('Purchase Common').get_rate(arg,self)
-
-	# **** Pull details from other charges master (Get Other Charges) ****
+	
+	def load_default_taxes(self):
+		self.doclist = get_obj('Purchase Common').load_default_taxes(self)
+	
 	def get_purchase_tax_details(self):
 		self.doclist = get_obj('Purchase Common').get_purchase_tax_details(self)