Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/accounts/doctype/account/test_account.py b/accounts/doctype/account/test_account.py
index ec72dbc..db2ca19 100644
--- a/accounts/doctype/account/test_account.py
+++ b/accounts/doctype/account/test_account.py
@@ -3,17 +3,19 @@
 		
 	accounts = [
 		# [account_name, parent_account, group_or_ledger]
+		["_Test Account Bank Account", "Bank Accounts - _TC", "Ledger"],
 		["_Test Account Stock Expenses", "Direct Expenses - _TC", "Group"],
 		["_Test Account Shipping Charges", "_Test Account Stock Expenses - _TC", "Ledger"],
 		["_Test Account Customs Duty", "_Test Account Stock Expenses - _TC", "Ledger"],
 		["_Test Account Tax Assets", "Current Assets - _TC", "Group"],
 		["_Test Account VAT", "_Test Account Tax Assets - _TC", "Ledger"],
+		["_Test Account Service Tax", "_Test Account Tax Assets - _TC", "Ledger"],
 		["_Test Account Cost for Goods Sold", "Expenses - _TC", "Ledger"],
 		["_Test Account Excise Duty", "_Test Account Tax Assets - _TC", "Ledger"],
 		["_Test Account Education Cess", "_Test Account Tax Assets - _TC", "Ledger"],
 		["_Test Account S&H Education Cess", "_Test Account Tax Assets - _TC", "Ledger"],
 		["_Test Account CST", "Direct Expenses - _TC", "Ledger"],
-		["_Test Account Discount", "Direct Expenses - _TC", "Ledger"]
+		["_Test Account Discount", "Direct Expenses - _TC", "Ledger"],
 	]
 
 	return make_test_objects("Account", [[{
diff --git a/accounts/doctype/journal_voucher/test_journal_voucher.py b/accounts/doctype/journal_voucher/test_journal_voucher.py
index 4c72092..67fd1c0 100644
--- a/accounts/doctype/journal_voucher/test_journal_voucher.py
+++ b/accounts/doctype/journal_voucher/test_journal_voucher.py
@@ -18,110 +18,147 @@
 from __future__ import unicode_literals
 import unittest
 import webnotes
-import webnotes.model
-from webnotes.utils import nowdate, flt, add_days
-from accounts.utils import get_fiscal_year, get_balance_on
 
-company = webnotes.conn.get_default("company")
-abbr = webnotes.conn.get_value("Company", company, "abbr")
+test_records = [[
+	{
+		"company": "_Test Company", 
+		"doctype": "Journal Voucher", 
+		"fiscal_year": "_Test Fiscal Year 2013", 
+		"naming_series": "_T-Journal Voucher-",
+		"posting_date": "2013-02-14", 
+		"tds_applicable": "No",
+		"user_remark": "test",
+		"voucher_type": "Bank Voucher",
+		"cheque_no": "33",
+		"cheque_date": "2013-02-14"
+	}, 
+	{
+		"account": "_Test Customer - _TC", 
+		"doctype": "Journal Voucher Detail", 
+		"credit": 400.0,
+		"debit": 0.0,
+		"parentfield": "entries"
+	}, 
+	{
+		"account": "_Test Account Bank Account - _TC", 
+		"doctype": "Journal Voucher Detail", 
+		"debit": 400.0,
+		"credit": 0.0,
+		"parentfield": "entries"
+	}
+]]
 
-data = {
-	"expense_account": {
-		"doctype": "Account",
-		"account_name": "Test Expense",
-		"parent_account": "Direct Expenses - %s" % abbr,
-		"company": company,
-		"debit_or_credit": "Debit",
-		"is_pl_account": "Yes",
-		"group_or_ledger": "Ledger"
-	},
-	"supplier_account": {
-		"doctype": "Account",
-		"account_name": "Test Supplier",
-		"parent_account": "Accounts Payable - %s" % abbr,
-		"company": company,
-		"debit_or_credit": "Credit",
-		"is_pl_account": "No",
-		"group_or_ledger": "Ledger"
-	},
-	"test_cost_center": {
-		"doctype": "Cost Center",
-		"cost_center_name": "Test Cost Center",
-		"parent_cost_center": "Root - %s" % abbr,
-		"company_name": company,
-		"group_or_ledger": "Ledger",
-		"company_abbr": abbr
-	},
-	"journal_voucher": [
-		{
-			"doctype": "Journal Voucher",
-			"voucher_type": "Journal Entry",
-			"naming_series": "JV",
-			"posting_date": nowdate(),
-			"remark": "Test Journal Voucher",
-			"fiscal_year": get_fiscal_year(nowdate())[0],
-			"company": company
-		},
-		{
-			"doctype": "Journal Voucher Detail",
-			"parentfield": "entries",
-			"account": "Test Expense - %s" % abbr,
-			"debit": 5000,
-			"cost_center": "Test Cost Center - %s" % abbr,
-		},
-		{
-			"doctype": "Journal Voucher Detail",
-			"parentfield": "entries",
-			"account": "Test Supplier - %s" % abbr,
-			"credit": 5000,
-		},
-	]
-}
 
-def get_name(s):
-	return s + " - " + abbr
 
-class TestJournalVoucher(unittest.TestCase):
-	def setUp(self):
-		webnotes.conn.begin()
-		
-		# create a dummy account
-		webnotes.model.insert([data["expense_account"]])
-		webnotes.model.insert([data["supplier_account"]])
-		webnotes.model.insert([data["test_cost_center"]])
-		
-	def tearDown(self):
-		webnotes.conn.rollback()
-		
-	def test_save_journal_voucher(self):
-		expense_ac_balance = get_balance_on(get_name("Test Expense"), nowdate())
-		supplier_ac_balance = get_balance_on(get_name("Test Supplier"), nowdate())
-		
-		dl = webnotes.model.insert(data["journal_voucher"])
-		dl.submit()
-		dl.load_from_db()
-		
-		# test submitted jv
-		self.assertTrue(webnotes.conn.exists("Journal Voucher", dl.doclist[0].name))
-		for d in dl.doclist[1:]:
-			self.assertEquals(webnotes.conn.get_value("Journal Voucher Detail",
-				d.name, "parent"), dl.doclist[0].name)
-		
-		# test gl entry
-		gle = webnotes.conn.sql("""select account, debit, credit
-			from `tabGL Entry` where voucher_no = %s order by account""",
-			 dl.doclist[0].name)
-		
-		self.assertEquals((gle[0][0], flt(gle[0][1]), flt(gle[0][2])), 
-			('Test Expense - %s' % abbr, 5000.0, 0.0))
-		self.assertEquals((gle[1][0], flt(gle[1][1]), flt(gle[1][2])), 
-			('Test Supplier - %s' % abbr, 0.0, 5000.0))
-	
-		# check balance as on today
-		self.assertEqual(get_balance_on(get_name("Test Expense"), nowdate()),
-			expense_ac_balance + 5000)
-		self.assertEqual(get_balance_on(get_name("Test Supplier"), nowdate()),
-			supplier_ac_balance + 5000)
-			
-		# check previous balance
-		self.assertEqual(get_balance_on(get_name("Test Expense"), add_days(nowdate(), -1)), 0)
\ No newline at end of file
+
+
+
+# 
+# 
+# import webnotes.model
+# from webnotes.utils import nowdate, flt, add_days
+# from accounts.utils import get_fiscal_year, get_balance_on
+# 
+# company = webnotes.conn.get_default("company")
+# abbr = webnotes.conn.get_value("Company", company, "abbr")
+# 
+# data = {
+# 	"expense_account": {
+# 		"doctype": "Account",
+# 		"account_name": "Test Expense",
+# 		"parent_account": "Direct Expenses - %s" % abbr,
+# 		"company": company,
+# 		"debit_or_credit": "Debit",
+# 		"is_pl_account": "Yes",
+# 		"group_or_ledger": "Ledger"
+# 	},
+# 	"supplier_account": {
+# 		"doctype": "Account",
+# 		"account_name": "Test Supplier",
+# 		"parent_account": "Accounts Payable - %s" % abbr,
+# 		"company": company,
+# 		"debit_or_credit": "Credit",
+# 		"is_pl_account": "No",
+# 		"group_or_ledger": "Ledger"
+# 	},
+# 	"test_cost_center": {
+# 		"doctype": "Cost Center",
+# 		"cost_center_name": "Test Cost Center",
+# 		"parent_cost_center": "Root - %s" % abbr,
+# 		"company_name": company,
+# 		"group_or_ledger": "Ledger",
+# 		"company_abbr": abbr
+# 	},
+# 	"journal_voucher": [
+# 		{
+# 			"doctype": "Journal Voucher",
+# 			"voucher_type": "Journal Entry",
+# 			"naming_series": "JV",
+# 			"posting_date": nowdate(),
+# 			"remark": "Test Journal Voucher",
+# 			"fiscal_year": get_fiscal_year(nowdate())[0],
+# 			"company": company
+# 		},
+# 		{
+# 			"doctype": "Journal Voucher Detail",
+# 			"parentfield": "entries",
+# 			"account": "Test Expense - %s" % abbr,
+# 			"debit": 5000,
+# 			"cost_center": "Test Cost Center - %s" % abbr,
+# 		},
+# 		{
+# 			"doctype": "Journal Voucher Detail",
+# 			"parentfield": "entries",
+# 			"account": "Test Supplier - %s" % abbr,
+# 			"credit": 5000,
+# 		},
+# 	]
+# }
+# 
+# def get_name(s):
+# 	return s + " - " + abbr
+# 
+# class TestJournalVoucher(unittest.TestCase):
+# 	def setUp(self):
+# 		webnotes.conn.begin()
+# 		
+# 		# create a dummy account
+# 		webnotes.model.insert([data["expense_account"]])
+# 		webnotes.model.insert([data["supplier_account"]])
+# 		webnotes.model.insert([data["test_cost_center"]])
+# 		
+# 	def tearDown(self):
+# 		webnotes.conn.rollback()
+# 		
+# 	def test_save_journal_voucher(self):
+# 		expense_ac_balance = get_balance_on(get_name("Test Expense"), nowdate())
+# 		supplier_ac_balance = get_balance_on(get_name("Test Supplier"), nowdate())
+# 		
+# 		dl = webnotes.model.insert(data["journal_voucher"])
+# 		dl.submit()
+# 		dl.load_from_db()
+# 		
+# 		# test submitted jv
+# 		self.assertTrue(webnotes.conn.exists("Journal Voucher", dl.doclist[0].name))
+# 		for d in dl.doclist[1:]:
+# 			self.assertEquals(webnotes.conn.get_value("Journal Voucher Detail",
+# 				d.name, "parent"), dl.doclist[0].name)
+# 		
+# 		# test gl entry
+# 		gle = webnotes.conn.sql("""select account, debit, credit
+# 			from `tabGL Entry` where voucher_no = %s order by account""",
+# 			 dl.doclist[0].name)
+# 		
+# 		self.assertEquals((gle[0][0], flt(gle[0][1]), flt(gle[0][2])), 
+# 			('Test Expense - %s' % abbr, 5000.0, 0.0))
+# 		self.assertEquals((gle[1][0], flt(gle[1][1]), flt(gle[1][2])), 
+# 			('Test Supplier - %s' % abbr, 0.0, 5000.0))
+# 	
+# 		# check balance as on today
+# 		self.assertEqual(get_balance_on(get_name("Test Expense"), nowdate()),
+# 			expense_ac_balance + 5000)
+# 		self.assertEqual(get_balance_on(get_name("Test Supplier"), nowdate()),
+# 			supplier_ac_balance + 5000)
+# 			
+# 		# check previous balance
+# 		self.assertEqual(get_balance_on(get_name("Test Expense"), add_days(nowdate(), -1)), 0)
\ 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
new file mode 100644
index 0000000..c222c1a
--- /dev/null
+++ b/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -0,0 +1,84 @@
+import webnotes
+import unittest
+
+class TestSalesInvoice(unittest.TestCase):
+	def make(self):
+		w = webnotes.model_wrapper(webnotes.copy_doclist(test_records[0]))
+		w.insert()
+		w.submit()
+		return w
+
+	def test_outstanding(self):
+		w = self.make()
+		self.assertEquals(w.doc.outstanding_amount, w.doc.grand_total)
+		
+	def test_payment(self):
+		w = self.make()
+		from accounts.doctype.journal_voucher.test_journal_voucher \
+			import test_records as jv_test_records
+			
+		jv = webnotes.model_wrapper(webnotes.copy_doclist(jv_test_records[0]))
+		jv.doclist[1].against_invoice = w.doc.name
+		jv.insert()
+		jv.submit()
+		
+		self.assertEquals(webnotes.conn.get_value("Sales Invoice", w.doc.name, "outstanding_amount"),
+			161.8)
+	
+		jv.cancel()
+		self.assertEquals(webnotes.conn.get_value("Sales Invoice", w.doc.name, "outstanding_amount"),
+			561.8)
+		
+test_dependencies = ["Journal Voucher"]
+
+test_records = [[
+	{
+		"naming_series": "_T-Sales Invoice-",
+		"company": "_Test Company", 
+		"conversion_rate": 1.0, 
+		"currency": "INR", 
+		"debit_to": "_Test Customer - _TC",
+		"customer": "_Test Customer",
+		"customer_name": "_Test Customer",
+		"doctype": "Sales Invoice", 
+		"due_date": "2013-01-23", 
+		"fiscal_year": "_Test Fiscal Year 2013", 
+		"grand_total": 561.8, 
+		"grand_total_export": 561.8, 
+		"net_total": 500.0, 
+		"plc_conversion_rate": 1.0, 
+		"posting_date": "2013-01-23", 
+		"price_list_currency": "INR", 
+		"price_list_name": "_Test Price List", 
+		"territory": "_Test Territory"
+	}, 
+	{
+		"amount": 500.0, 
+		"basic_rate": 500.0, 
+		"description": "138-CMS Shoe", 
+		"doctype": "Sales Invoice Item", 
+		"export_amount": 500.0, 
+		"export_rate": 500.0, 
+		"income_account": "Sales - _TC",
+		"cost_center": "_Test Cost Center - _TC",
+		"item_name": "138-CMS Shoe", 
+		"parentfield": "entries",
+		"qty": 1.0
+	}, 
+	{
+		"account_head": "_Test Account VAT - _TC", 
+		"charge_type": "On Net Total", 
+		"description": "VAT", 
+		"doctype": "Sales Taxes and Charges", 
+		"parentfield": "other_charges",
+		"tax_amount": 30.0,
+	}, 
+	{
+		"account_head": "_Test Account Service Tax - _TC", 
+		"charge_type": "On Net Total", 
+		"description": "Service Tax", 
+		"doctype": "Sales Taxes and Charges", 
+		"parentfield": "other_charges",
+		"tax_amount": 31.8,
+	}
+]]
\ No newline at end of file
diff --git a/accounts/general_ledger.py b/accounts/general_ledger.py
index 06c101b..215c351 100644
--- a/accounts/general_ledger.py
+++ b/accounts/general_ledger.py
@@ -24,11 +24,11 @@
 	if merge_entries:
 		gl_map = merge_similar_entries(gl_map)
 	
-	check_budget(gl_map, cancel)
-	save_entries(gl_map, cancel, adv_adj, update_outstanding)
-
 	if cancel:
 		set_as_cancel(gl_map[0]["voucher_type"], gl_map[0]["voucher_no"])
+
+	check_budget(gl_map, cancel)
+	save_entries(gl_map, cancel, adv_adj, update_outstanding)
 		
 def merge_similar_entries(gl_map):
 	merged_gl_map = []
diff --git a/accounts/search_criteria/trend_analyzer/trend_analyzer.py b/accounts/search_criteria/trend_analyzer/trend_analyzer.py
index 53a17ba..87e1e8e 100644
--- a/accounts/search_criteria/trend_analyzer/trend_analyzer.py
+++ b/accounts/search_criteria/trend_analyzer/trend_analyzer.py
@@ -79,7 +79,7 @@
 	cols.append([group_by,'Data','150px',''])
 
 for c in col_names:
-	cols.append([c,'Currency','150px',''])
+	cols.append([c, ("Amt" in c) and 'Currency' or 'Float','150px',''])
 
 for c in cols:
 	colnames.append(c[0])
diff --git a/patches/february_2013/fix_outstanding.py b/patches/february_2013/fix_outstanding.py
index 226b360..07ea51a 100644
--- a/patches/february_2013/fix_outstanding.py
+++ b/patches/february_2013/fix_outstanding.py
@@ -12,4 +12,4 @@
 			if flt(r[1]) != abs(flt(outstanding[0][0])):
 				# print r, outstanding
 				webnotes.conn.sql("update `tab%s` set outstanding_amount = %s where name = %s" %
-					(dt, '%s', '%s'), (abs(flt(outstanding[0][0])), si[0]))
\ No newline at end of file
+					(dt, '%s', '%s'), (abs(flt(outstanding[0][0])), r[0]))
\ No newline at end of file
diff --git a/patches/patch_list.py b/patches/patch_list.py
index c00744e..1a74839 100644
--- a/patches/patch_list.py
+++ b/patches/patch_list.py
@@ -169,4 +169,5 @@
 	"patches.february_2013.update_company_in_leave_application",
 	"execute:webnotes.conn.sql_ddl('alter table tabSeries change `name` `name` varchar(100)')",
 	"execute:webnotes.conn.sql('update tabUserRole set parentfield=\"user_roles\" where parentfield=\"userroles\"')",
+	"patches.february_2013.fix_outstanding"
 ]
\ No newline at end of file
diff --git a/selling/doctype/sales_common/sales_common.py b/selling/doctype/sales_common/sales_common.py
index 21287b0..49dc999 100644
--- a/selling/doctype/sales_common/sales_common.py
+++ b/selling/doctype/sales_common/sales_common.py
@@ -734,13 +734,14 @@
 		"""
 		# get unique transactions to update
 		for d in self.obj.doclist:
-			if d.doctype == args['source_dt']:
+			if d.doctype == args['source_dt'] and d.fields.get(args["join_field"]):
 				args['name'] = d.fields[args['join_field']]
 
 				# get all qty where qty > compare_field
-				item = webnotes.conn.sql("""
-					select item_code, `%(compare_ref_field)s`, `%(compare_field)s`, parenttype, parent from `tab%(target_dt)s` 
-					where `%(compare_ref_field)s` < `%(compare_field)s` and name="%(name)s" and docstatus=1
+				item = webnotes.conn.sql("""select item_code, `%(compare_ref_field)s`, 
+					`%(compare_field)s`, parenttype, parent from `tab%(target_dt)s` 
+						where `%(compare_ref_field)s` < `%(compare_field)s` 
+						and name="%(name)s" and docstatus=1
 					""" % args, as_dict=1)
 				if item:
 					item = item[0]
diff --git a/setup/doctype/print_heading/test_print_heading.py b/setup/doctype/print_heading/test_print_heading.py
new file mode 100644
index 0000000..d2ef7c3
--- /dev/null
+++ b/setup/doctype/print_heading/test_print_heading.py
@@ -0,0 +1 @@
+test_records = [[{"print_heading": "_Test Print Heading"}]]
\ No newline at end of file
diff --git a/setup/doctype/terms_and_conditions/test_terms_and_conditions.py b/setup/doctype/terms_and_conditions/test_terms_and_conditions.py
new file mode 100644
index 0000000..b0d1fee
--- /dev/null
+++ b/setup/doctype/terms_and_conditions/test_terms_and_conditions.py
@@ -0,0 +1 @@
+test_records = [[{"title": "_Test Terms and Conditions", "terms": "_Test Terms"}]]
\ No newline at end of file
diff --git a/startup/__init__.py b/startup/__init__.py
index 18ae932..c1f4660 100644
--- a/startup/__init__.py
+++ b/startup/__init__.py
@@ -53,3 +53,14 @@
 		return 999999
 	else:
 		return 500
+
+def get_url():
+	from webnotes.utils import get_request_site_address
+	url = get_request_site_address()
+	if not url or "localhost" in url:
+		subdomain = webnotes.conn.get_value("Website Settings", "Website Settings",
+			"subdomain")
+		if subdomain:
+			if "http" not in subdomain:
+				url = "http://" + subdomain
+	return url
\ No newline at end of file
diff --git a/utilities/page/messages/messages.py b/utilities/page/messages/messages.py
index 7fbc101..019cd071 100644
--- a/utilities/page/messages/messages.py
+++ b/utilities/page/messages/messages.py
@@ -91,6 +91,8 @@
 
 def notify(arg=None):
 	from webnotes.utils import cstr
+	from startup import get_url
+	
 	fn = webnotes.conn.sql('select first_name, last_name from tabProfile where name=%s', webnotes.user.name)[0]
 	if fn[0] or f[1]:
 		fn = cstr(fn[0]) + (fn[0] and ' ' or '') + cstr(fn[1])
@@ -110,14 +112,4 @@
 	
 	from webnotes.utils.email_lib import sendmail
 	sendmail([arg['contact']], sender, message, "You have a message from %s" % (fn,))
-	
-def get_url():
-	from webnotes.utils import get_request_site_address
-	url = get_request_site_address()
-	if not url or "localhost" in url:
-		subdomain = webnotes.conn.get_value("Website Settings", "Website Settings",
-			"subdomain")
-		if subdomain:
-			if "http" not in subdomain:
-				url = "http://" + subdomain
-	return url
+	
\ No newline at end of file