Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/accounts/doctype/account/account.js b/accounts/doctype/account/account.js
index 14e35d3..7738682 100644
--- a/accounts/doctype/account/account.js
+++ b/accounts/doctype/account/account.js
@@ -116,19 +116,18 @@
   });
 }
 
-// Master name get query
-// -----------------------------------------
-cur_frm.fields_dict['master_name'].get_query=function(doc){
- if (doc.master_type){
-    return 'SELECT `tab'+doc.master_type+'`.name FROM `tab'+doc.master_type+'` WHERE `tab'+doc.master_type+'`.name LIKE "%s" and `tab'+doc.master_type+'`.docstatus != 2 ORDER BY `tab'+doc.master_type+'`.name LIMIT 50';
-  }
+cur_frm.fields_dict['master_name'].get_query = function(doc) {
+	if (doc.master_type) {
+		return {
+			query: "accounts.doctype.account.account.get_master_name",
+			args: {	"master_type": doc.master_type }
+		}
+	}
 }
 
-// parent account get query
-// -----------------------------------------
-cur_frm.fields_dict['parent_account'].get_query = function(doc){
-  return 'SELECT DISTINCT `tabAccount`.name FROM `tabAccount` WHERE \
-	`tabAccount`.group_or_ledger="Group" AND `tabAccount`.docstatus != 2 AND \
-	`tabAccount`.company="'+ doc.company+'" AND `tabAccount`.company is not NULL AND \
-	`tabAccount`.name LIKE "%s" ORDER BY `tabAccount`.name LIMIT 50';
+cur_frm.fields_dict['parent_account'].get_query = function(doc) {
+	return {
+		query: "accounts.doctype.account.account.get_parent_account",
+		args: { "company": doc.company}
+	}
 }
diff --git a/accounts/doctype/account/account.py b/accounts/doctype/account/account.py
index 04e13d8..0e3ace1 100644
--- a/accounts/doctype/account/account.py
+++ b/accounts/doctype/account/account.py
@@ -18,8 +18,6 @@
 import webnotes
 
 from webnotes.utils import flt, fmt_money
-from webnotes.model import db_exists
-from webnotes.model.wrapper import copy_doclist
 from webnotes import msgprint
 
 sql = webnotes.conn.sql
@@ -200,4 +198,13 @@
 		sql("update `tabAccount` set account_name = '%s' where name = '%s'" % \
 			(account_name, old))
 
-		return " - ".join(parts)
\ No newline at end of file
+		return " - ".join(parts)
+
+def get_master_name(doctype, txt, searchfield, start, page_len, args):
+	return webnotes.conn.sql("""select name from `tab%s` where name like '%%%s%%'""" %
+		(args["master_type"], txt), as_list=1)
+		
+def get_parent_account(doctype, txt, searchfield, start, page_len, args):
+	return webnotes.conn.sql("""select name from tabAccount 
+		where group_or_ledger = 'Group' and docstatus != 2 and company = '%s' 
+		and name like '%%%s%%'""" % (args["company"], txt))
diff --git a/accounts/doctype/journal_voucher/journal_voucher.py b/accounts/doctype/journal_voucher/journal_voucher.py
index 4f20308..eafd7ee 100644
--- a/accounts/doctype/journal_voucher/journal_voucher.py
+++ b/accounts/doctype/journal_voucher/journal_voucher.py
@@ -51,7 +51,7 @@
 			self.doc.posting_date, 'Posting Date')
 		
 		self.set_against_account()
-		self.create_remarks()		
+		self.create_remarks()
 		self.set_aging_date()
 		self.set_print_format_fields()
 
@@ -152,6 +152,8 @@
 
 		if r:
 			self.doc.remark = ("\n").join(r)
+		else:
+			webnotes.msgprint("Remarks is mandatory", raise_exception=1)
 
 	def set_aging_date(self):
 		if self.doc.is_opening != 'Yes':
diff --git a/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/accounts/doctype/purchase_invoice/test_purchase_invoice.py
index 606ebb9..f7cfc96 100644
--- a/accounts/doctype/purchase_invoice/test_purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/test_purchase_invoice.py
@@ -20,6 +20,7 @@
 import webnotes
 import webnotes.model
 from webnotes.utils import nowdate
+from webnotes.test_runner import make_test_records
 
 from stock.doctype.purchase_receipt import test_purchase_receipt
 
@@ -34,15 +35,15 @@
 		"group_or_ledger": "Ledger"})
 	
 	webnotes.insert({"doctype": "Account", "account_name": "Excise Duty",
-		"parent_account": "Tax Assets - %s" % abbr, "company": company,
+		"parent_account": "_Test Tax Assets - %s" % abbr, "company": company,
 		"group_or_ledger": "Ledger"})
 	
 	webnotes.insert({"doctype": "Account", "account_name": "Education Cess",
-		"parent_account": "Tax Assets - %s" % abbr, "company": company,
+		"parent_account": "_Test Tax Assets - %s" % abbr, "company": company,
 		"group_or_ledger": "Ledger"})
 	
 	webnotes.insert({"doctype": "Account", "account_name": "S&H Education Cess",
-		"parent_account": "Tax Assets - %s" % abbr, "company": company,
+		"parent_account": "_Test Tax Assets - %s" % abbr, "company": company,
 		"group_or_ledger": "Ledger"})
 		
 	webnotes.insert({"doctype": "Account", "account_name": "CST",
@@ -94,61 +95,66 @@
 	{
 		"doctype": "Purchase Taxes and Charges", "charge_type": "Actual",
 		"account_head": "Shipping Charges - %s" % abbr, "rate": 100, "tax_amount": 100, 
-		"category": "Valuation and Total", "parentfield": "other_charges",
-		"cost_center": "Default Cost Center - %s" % abbr
+		"category": "Valuation and Total", "parentfield": "purchase_tax_details",
+		"cost_center": "Default Cost Center - %s" % abbr, "add_deduct_tax": "Add"
 	},
 	{
 		"doctype": "Purchase Taxes and Charges", "charge_type": "On Net Total",
 		"account_head": "Customs Duty - %s" % abbr, "rate": 10, "tax_amount": 125.00,
-		"category": "Valuation", "parentfield": "other_charges",
-		"cost_center": "Default Cost Center - %s" % abbr
+		"category": "Valuation", "parentfield": "purchase_tax_details",
+		"cost_center": "Default Cost Center - %s" % abbr, "add_deduct_tax": "Add"
 	},
 	{
 		"doctype": "Purchase Taxes and Charges", "charge_type": "On Net Total",
 		"account_head": "Excise Duty - %s" % abbr, "rate": 12, "tax_amount": 140.00, 
-		"category": "Total", "parentfield": "other_charges"
+		"category": "Total", "parentfield": "purchase_tax_details", "add_deduct_tax": "Add"
 	},
 	{
 		"doctype": "Purchase Taxes and Charges", "charge_type": "On Previous Row Amount",
 		"account_head": "Education Cess - %s" % abbr, "rate": 2, "row_id": 3, "tax_amount": 2.80,
-		"category": "Total", "parentfield": "other_charges"
+		"category": "Total", "parentfield": "purchase_tax_details", "add_deduct_tax": "Add"
 	},
 	{
 		"doctype": "Purchase Taxes and Charges", "charge_type": "On Previous Row Amount",
 		"account_head": "S&H Education Cess - %s" % abbr, "rate": 1, "row_id": 3, 
-		"tax_amount": 1.4, "category": "Total", "parentfield": "other_charges"
+		"tax_amount": 1.4, "category": "Total", "parentfield": "purchase_tax_details",
+		"add_deduct_tax": "Add"
 	},
 	{
 		"doctype": "Purchase Taxes and Charges", "charge_type": "On Previous Row Total",
 		"account_head": "CST - %s" % abbr, "rate": 2, "row_id": 5, "tax_amount": 29.88, 
-		"category": "Total", "parentfield": "other_charges",
-		"cost_center": "Default Cost Center - %s" % abbr
+		"category": "Total", "parentfield": "purchase_tax_details",
+		"cost_center": "Default Cost Center - %s" % abbr, "add_deduct_tax": "Add"
 	},
 	{
 		"doctype": "Purchase Taxes and Charges", "charge_type": "On Net Total",
 		"account_head": "VAT - Test - %s" % abbr, "rate": 12.5, "tax_amount": 156.25, 
-		"category": "Total", "parentfield": "other_charges"
+		"category": "Total", "parentfield": "purchase_tax_details", "add_deduct_tax": "Add"
 	},
 	{
 		"doctype": "Purchase Taxes and Charges", "charge_type": "On Previous Row Total",
-		"account_head": "Discount - %s" % abbr, "rate": -10, "row_id": 7, "tax_amount": -168.03, 
-		"category": "Total", "parentfield": "other_charges",
-		"cost_center": "Default Cost Center - %s" % abbr
+		"account_head": "Discount - %s" % abbr, "rate": 10, "row_id": 7, "tax_amount": 168.03, 
+		"category": "Total", "parentfield": "purchase_tax_details",
+		"cost_center": "Default Cost Center - %s" % abbr, "add_deduct_tax": "Deduct"
 	},
 ]
 
-class TestPurchaseReceipt(unittest.TestCase):
+class TestPurchaseInvoice(unittest.TestCase):
 	def setUp(self):
 		webnotes.conn.begin()
 		load_data()
 		# webnotes.conn.set_value("Global Defaults", None, "automatic_inventory_accounting", 1)
+		self.load_test_data()
+		
+	def load_test_data(self):
+		make_test_records("Company")
+		
 			
 	def test_gl_entries(self):
-		from webnotes.model.doclist import DocList
-		controller = webnotes.insert(DocList(purchase_invoice_doclist))
-		controller.submit()
-		controller.load_from_db()
-		dl = controller.doclist
+		wrapper = webnotes.model_wrapper(purchase_invoice_doclist).insert()
+		wrapper.submit()
+		wrapper.load_from_db()
+		dl = wrapper.doclist
 		
 		expected_gl_entries = {
 			"East Wind Inc. - %s" % abbr : [0, 1512.30],
@@ -165,6 +171,76 @@
 			where voucher_type = 'Purchase Invoice' and voucher_no = %s""", dl[0].name, as_dict=1)
 		for d in gl_entries:
 			self.assertEqual([d.debit, d.credit], expected_gl_entries.get(d.account))
+			
+	def test_purchase_invoice_calculation(self):
+		test_doclist = [] + purchase_invoice_doclist
+		for doc in test_doclist:
+			if doc["doctype"] == "Purchase Taxes and Charges":
+				del doc["tax_amount"]
+				
+		wrapper = webnotes.model_wrapper(test_doclist).insert()
+		wrapper.load_from_db()
+		
+		# tax amounts
+		expected_values = [
+			["Shipping Charges - %s" % abbr, 100, 1350],
+			["Customs Duty - %s" % abbr, 125, 1350],
+			["Excise Duty - %s" % abbr, 140, 1490],
+			["Education Cess - %s" % abbr, 2.8, 1492.8],
+			["S&H Education Cess - %s" % abbr, 1.4, 1494.2],
+			["CST - %s" % abbr, 29.88, 1524.08],
+			["VAT - Test - %s" % abbr, 156.25, 1680.33],
+			["Discount - %s" % abbr, 168.03, 1512.30],
+		]
+		
+		for i, tax in enumerate(wrapper.doclist.get({"parentfield": "purchase_tax_details"})):
+			self.assertEqual(tax.account_head, expected_values[i][0])
+			self.assertEqual(tax.tax_amount, expected_values[i][1])
+			self.assertEqual(tax.total, expected_values[i][2])
+
+		expected_values = [
+			["Home Desktop 100", 90],
+			["Home Desktop 200", 135]
+		]
+		for i, item in enumerate(wrapper.doclist.get({"parentfield": "entries"})):
+			self.assertEqual(item.item_code, expected_values[i][0])
+			self.assertEqual(item.item_tax_amount, expected_values[i][1])
+		
+		
+	# def test_purchase_invoice_calculation(self):
+	# 	from webnotes.model.doclist import DocList
+	# 	wrapper = webnotes.insert(DocList(purchase_invoice_doclist))
+	# 	wrapper.load_from_db()
+	# 	dl = wrapper.doclist
+	# 
+	# 	# test net total
+	# 	self.assertEqual(dl[0].net_total, 1250)
+	# 
+	# 	# test tax amounts and totals
+		# expected_values = [
+		# 	["Shipping Charges - %s" % abbr, 100, 1350],
+		# 	["Customs Duty - %s" % abbr, 125, 1350],
+		# 	["Excise Duty - %s" % abbr, 140, 1490],
+		# 	["Education Cess - %s" % abbr, 2.8, 1492.8],
+		# 	["S&H Education Cess - %s" % abbr, 1.4, 1494.2],
+		# 	["CST - %s" % abbr, 29.88, 1524.08],
+		# 	["VAT - Test - %s" % abbr, 156.25, 1680.33],
+		# 	["Discount - %s" % abbr, -168.03, 1512.30],
+		# ]		
+	# 	for i, tax in enumerate(dl.get({"parentfield": "taxes_and_charges"})):
+	# 		# print tax.account_head, tax.tax_amount, tax.total
+			# self.assertEqual(tax.account_head, expected_values[i][0])
+			# self.assertEqual(tax.tax_amount, expected_values[i][1])
+			# self.assertEqual(tax.total, expected_values[i][2])
+	# 
+	# 	# test item tax amount
+	# 	expected_values = [
+	# 		["Home Desktop 100", 90],
+	# 		["Home Desktop 200", 135]
+	# 	]
+	# 	for i, item in enumerate(dl.get({"parentfield": "purchase_invoice_items"})):
+	# 		self.assertEqual(item.item_code, expected_values[i][0])
+	# 		self.assertEqual(item.valuation_tax_amount, expected_values[i][1])
 
 	def tearDown(self):
 		webnotes.conn.rollback()
\ No newline at end of file
diff --git a/accounts/doctype/sales_invoice/test_sales_invoice.py b/accounts/doctype/sales_invoice/test_sales_invoice.py
index bb2157c..06ca887 100644
--- a/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -32,16 +32,16 @@
 	
 	# create customer group
 	webnotes.insert({"doctype": "Customer Group",
-		"customer_group_name": "Default Customer Group",
+		"customer_group_name": "_Test Customer Group",
 		"parent_customer_group": "All Customer Groups", "is_group": "No"})
 	
 	# create customer
 	webnotes.insert({"doctype": "Customer", "customer_name": "West Wind Inc.",
 		"customer_type": "Company", "territory": "Default",
-		"customer_group": "Default Customer Group", "company": company,
+		"customer_group": "_Test Customer Group", "company": company,
 		"credit_days": 50, "credit_limit": 0})
 	
-	webnotes.insert({"doctype": "Account", "account_name": "Sales",
+	webnotes.insert({"doctype": "Account", "account_name": "_Test Account Sales",
 		"parent_account": "Income - %s" % abbr, "company": company,
 		"group_or_ledger": "Ledger"})
 	
@@ -93,15 +93,15 @@
 		"doctype": "Sales Invoice Item", "warehouse": "Default Warehouse",
 		"item_code": "Home Desktop 100", "qty": 10, "basic_rate": 50, "amount": 500, 
 		"parentfield": "entries", "so_detail": None, "dn_detail": None,
-		"uom": "Nos", "item_tax_rate": json.dumps({"Excise Duty - %s" % abbr: 10}),
-		"income_account": "Sales - %s" % abbr, 
+		"stock_uom": "Nos", "item_tax_rate": json.dumps({"Excise Duty - %s" % abbr: 10}),
+		"income_account": "_Test Account Sales - %s" % abbr, 
 		"cost_center": "Default Cost Center - %s" % abbr
 	},
 	{
 		"doctype": "Sales Invoice Item", "warehouse": "Default Warehouse",
 		"item_code": "Home Desktop 200", "qty": 5, "basic_rate": 150, "amount": 750,
 		"so_detail": None, "dn_detail": None, 
-		"parentfield": "entries", "uom": "Nos", "income_account": "Sales - %s" % abbr, 
+		"parentfield": "entries", "stock_uom": "Nos", "income_account": "_Test Account Sales - %s" % abbr, 
 		"cost_center": "Default Cost Center - %s" % abbr
 	},
 	# taxes
@@ -211,7 +211,7 @@
 			
 		expected_gl_entries = {
 			"West Wind Inc. - %s" % abbr : [1627.05, 0.0],
-			"Sales - %s" % abbr: [0.0, 1250.00],
+			"_Test Account Sales - %s" % abbr: [0.0, 1250.00],
 			"Shipping Charges - %s" % abbr: [0.0, 100],
 			"Customs Duty - %s" % abbr: [0, 125.0],
 			"Excise Duty - %s" % abbr: [0, 140],
diff --git a/accounts/page/voucher_import_tool/voucher_import_tool.py b/accounts/page/voucher_import_tool/voucher_import_tool.py
index 0d3a7f7..f99a078 100644
--- a/accounts/page/voucher_import_tool/voucher_import_tool.py
+++ b/accounts/page/voucher_import_tool/voucher_import_tool.py
@@ -168,7 +168,7 @@
 		webnotes.conn.commit()
 	except Exception, e:
 		webnotes.conn.rollback()
-		err_msg = webnotes.message_log and webnotes.message_log[0] or unicode(e)
+		err_msg = webnotes.message_log and "<br>".join(webnotes.message_log) or unicode(e)
 		messages.append("""<p style='color: red'>[row #%s] %s failed: %s</p>"""
 			% ((start_idx + 1) + i, jv.name or "", err_msg or "No message"))
 		messages.append("<p style='color: red'>All transactions rolled back</p>")
diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py
index a80afc5..60535cc 100644
--- a/controllers/accounts_controller.py
+++ b/controllers/accounts_controller.py
@@ -17,7 +17,6 @@
 from __future__ import unicode_literals
 import webnotes
 from webnotes.utils import flt
-from webnotes.model.doc import addchild
 from utilities.transaction_base import TransactionBase
 
 class AccountsController(TransactionBase):
@@ -47,15 +46,13 @@
 		
 		return stock_in_hand
 		
-	def clear_unallocated_advances(self, parenttype, parentfield):
-		for d in self.doclist:
-			if d.parentfield == parentfield and flt(d.allocated_amount) == 0:
-				self.doclist.remove(d)
+	def clear_unallocated_advances(self, childtype, parentfield):
+		self.doclist.remove_items({"parentfield": parentfield, "allocated_amount": ["in", [0, None, ""]]})
 			
-		webnotes.conn.sql("""delete from `tab%s` where parent = %s 
-			and ifnull(allocated_amount, 0) = 0""" % (parenttype, '%s'), self.doc.name)			
+		webnotes.conn.sql("""delete from `tab%s` where parentfield=%s and parent = %s 
+			and ifnull(allocated_amount, 0) = 0""" % (childtype, '%s', '%s'), (parentfield, self.doc.name))
 		
-	def get_advances(self, account_head, parenttype, parentfield, dr_or_cr):
+	def get_advances(self, account_head, child_doctype, parentfield, dr_or_cr):
 		res = webnotes.conn.sql("""select t1.name as jv_no, t1.remark, 
 			t2.%s as amount, t2.name as jv_detail_no
 			from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2 
@@ -68,9 +65,12 @@
 			
 		self.doclist = self.doc.clear_table(self.doclist, parentfield)
 		for d in res:
-			add = addchild(self.doc, parentfield, parenttype, self.doclist)
-			add.journal_voucher = d.jv_no
-			add.jv_detail_no = d.jv_detail_no
-			add.remarks = d.remark
-			add.advance_amount = flt(d.amount)
-			add.allocate_amount = 0
\ No newline at end of file
+			self.doclist.append({
+				"doctype": child_doctype,
+				"parentfield": parentfield,
+				"journal_voucher": d.jv_no,
+				"jv_detail_no": d.jv_detail_no,
+				"remarks": d.remark,
+				"advance_amount": flt(d.amount),
+				"allocate_amount": 0
+			})
\ No newline at end of file
diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py
index 8a51e3a..f416b85 100644
--- a/controllers/buying_controller.py
+++ b/controllers/buying_controller.py
@@ -83,4 +83,7 @@
 			self.doc.in_words = money_in_words(self.doc.grand_total, company_currency)
 		if self.meta.get_field("in_words_import"):
 			self.doc.in_words_import = money_in_words(self.doc.grand_total_import,
-		 		self.doc.currency)
\ No newline at end of file
+		 		self.doc.currency)
+		
+	def calculate_taxes_and_totals(self):
+		pass
\ No newline at end of file
diff --git a/setup/doctype/supplier_type/supplier_type.py b/setup/doctype/supplier_type/supplier_type.py
index 7f48feb..4a8fdff 100644
--- a/setup/doctype/supplier_type/supplier_type.py
+++ b/setup/doctype/supplier_type/supplier_type.py
@@ -19,4 +19,12 @@
 
 class DocType:
 	def __init__(self, d, dl):
-		self.doc, self.doclist = d, dl
\ No newline at end of file
+		self.doc, self.doclist = d, dl
+		
+
+test_records = [
+	[{
+		"doctype": "Supplier Type",
+		"supplier_type": "_Test Supplier Type",
+	}]
+]
\ No newline at end of file
diff --git a/stock/doctype/purchase_receipt/test_purchase_receipt.py b/stock/doctype/purchase_receipt/test_purchase_receipt.py
index b619cbd..f91455f 100644
--- a/stock/doctype/purchase_receipt/test_purchase_receipt.py
+++ b/stock/doctype/purchase_receipt/test_purchase_receipt.py
@@ -56,7 +56,7 @@
 		webnotes.insert({"doctype": "Cost Center", "group_or_ledger": "Ledger",
 			"cost_center_name": "Default Cost Center", 
 			"parent_cost_center": "Root - %s" % abbr,
-			"company_name": company, "company_abbr": abbr})
+			"company_name": company})
 		
 	# create account heads for taxes
 	
@@ -67,11 +67,11 @@
 	webnotes.insert({"doctype": "Account", "account_name": "Customs Duty",
 		"parent_account": "Stock Expenses - %s" % abbr, "company": company,
 		"group_or_ledger": "Ledger"})
-	webnotes.insert({"doctype": "Account", "account_name": "Tax Assets",
+	webnotes.insert({"doctype": "Account", "account_name": "_Test Tax Assets",
 		"parent_account": "Current Assets - %s" % abbr, "company": company,
 		"group_or_ledger": "Group"})
 	webnotes.insert({"doctype": "Account", "account_name": "VAT - Test",
-		"parent_account": "Tax Assets - %s" % abbr, "company": company,
+		"parent_account": "_Test Tax Assets - %s" % abbr, "company": company,
 		"group_or_ledger": "Ledger"})
 		
 	# create BOM
diff --git a/stock/utils.py b/stock/utils.py
index 87f4c02..3b98cf0 100644
--- a/stock/utils.py
+++ b/stock/utils.py
@@ -148,7 +148,7 @@
 		
 	return valid_serial_nos
 	
-def get_warehouse_list(doctype, txt, searchfield, start, page_len):
+def get_warehouse_list(doctype, txt, searchfield, start, page_len, args):
 	"""used in search queries"""
 	wlist = []
 	for w in webnotes.conn.sql_list("""select name from tabWarehouse