Merge branch '1310' of github.com:webnotes/erpnext into 1310
diff --git a/accounts/doctype/account/account.py b/accounts/doctype/account/account.py
index fba51a9..37e165a 100644
--- a/accounts/doctype/account/account.py
+++ b/accounts/doctype/account/account.py
@@ -32,16 +32,6 @@
 		self.validate_root_details()
 		self.validate_mandatory()
 		self.validate_warehouse_account()
-	
-		if not self.doc.parent_account:
-			self.doc.parent_account = ''
-		
-	def validate(self): 
-		self.validate_master_name()
-		self.validate_parent()
-		self.validate_duplicate_account()
-		self.validate_root_details()
-		self.validate_mandatory()
 		self.validate_frozen_accounts_modifier()
 	
 		if not self.doc.parent_account:
@@ -177,24 +167,24 @@
 				in webnotes.user.get_roles():
 			return 1
 			
-	def check_credit_limit(self, account, company, tot_outstanding):
+	def check_credit_limit(self, total_outstanding):
 		# Get credit limit
 		credit_limit_from = 'Customer'
 
 		cr_limit = sql("""select t1.credit_limit from tabCustomer t1, `tabAccount` t2 
-			where t2.name=%s and t1.name = t2.master_name""", account)
+			where t2.name=%s and t1.name = t2.master_name""", self.doc.name)
 		credit_limit = cr_limit and flt(cr_limit[0][0]) or 0
 		if not credit_limit:
-			credit_limit = webnotes.conn.get_value('Company', company, 'credit_limit')
-			credit_limit_from = 'global settings in the Company'
+			credit_limit = webnotes.conn.get_value('Company', self.doc.company, 'credit_limit')
+			credit_limit_from = 'Company'
 		
 		# If outstanding greater than credit limit and not authorized person raise exception
-		if credit_limit > 0 and flt(tot_outstanding) > credit_limit \
+		if credit_limit > 0 and flt(total_outstanding) > credit_limit \
 				and not self.get_authorized_user():
 			msgprint("""Total Outstanding amount (%s) for <b>%s</b> can not be \
 				greater than credit limit (%s). To change your credit limit settings, \
-				please update the <b>%s</b>""" % (fmt_money(tot_outstanding), 
-				account, fmt_money(credit_limit), credit_limit_from), raise_exception=1)
+				please update in the <b>%s</b> master""" % (fmt_money(total_outstanding), 
+				self.doc.name, fmt_money(credit_limit), credit_limit_from), raise_exception=1)
 			
 	def validate_trash(self):
 		"""checks gl entries and if child exists"""
diff --git a/accounts/doctype/gl_entry/gl_entry.py b/accounts/doctype/gl_entry/gl_entry.py
index dfac6b6..e0babdf 100644
--- a/accounts/doctype/gl_entry/gl_entry.py
+++ b/accounts/doctype/gl_entry/gl_entry.py
@@ -16,7 +16,6 @@
 		self.check_mandatory()
 		self.pl_must_have_cost_center()
 		self.validate_posting_date()
-		self.check_credit_limit()
 		self.check_pl_account()
 		self.validate_cost_center()
 
@@ -55,21 +54,6 @@
 		from accounts.utils import validate_fiscal_year
 		validate_fiscal_year(self.doc.posting_date, self.doc.fiscal_year, "Posting Date")
 
-	def check_credit_limit(self):
-		master_type, master_name = webnotes.conn.get_value("Account", 
-			self.doc.account, ["master_type", "master_name"])
-			
-		tot_outstanding = 0	#needed when there is no GL Entry in the system for that acc head
-		if (self.doc.voucher_type=='Journal Voucher' or self.doc.voucher_type=='Sales Invoice') \
-				and (master_type =='Customer' and master_name):
-			dbcr = webnotes.conn.sql("""select sum(debit), sum(credit) from `tabGL Entry` 
-				where account = %s""", self.doc.account)
-			if dbcr:
-				tot_outstanding = flt(dbcr[0][0]) - flt(dbcr[0][1]) + \
-					flt(self.doc.debit) - flt(self.doc.credit)
-			get_obj('Account',self.doc.account).check_credit_limit(self.doc.account, 
-				self.doc.company, tot_outstanding)
-
 	def check_pl_account(self):
 		if self.doc.is_opening=='Yes' and \
 				webnotes.conn.get_value("Account", self.doc.account, "is_pl_account") == "Yes":
diff --git a/accounts/doctype/journal_voucher/journal_voucher.py b/accounts/doctype/journal_voucher/journal_voucher.py
index ed4a0d7..8fb47f7 100644
--- a/accounts/doctype/journal_voucher/journal_voucher.py
+++ b/accounts/doctype/journal_voucher/journal_voucher.py
@@ -44,6 +44,7 @@
 			self.check_credit_days()
 		self.check_account_against_entries()
 		self.make_gl_entries()
+		self.check_credit_limit()
 
 	def on_cancel(self):
 		from accounts.utils import remove_against_link_from_jv
@@ -259,6 +260,13 @@
 				)
 		if gl_map:
 			make_gl_entries(gl_map, cancel=cancel, adv_adj=adv_adj)
+			
+	def check_credit_limit(self):
+		for d in self.doclist.get({"parentfield": "entries"}):
+			master_type, master_name = webnotes.conn.get_value("Account", d.account, 
+				["master_type", "master_name"])
+			if master_type == "Customer" and master_name:
+				super(DocType, self).check_credit_limit(d.account)
 
 	def get_outstanding(self, args):
 		args = eval(args)
diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py
index 6c02bbe..eb46d1a 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/accounts/doctype/sales_invoice/sales_invoice.py
@@ -95,6 +95,7 @@
 		
 		# this sequence because outstanding may get -ve
 		self.make_gl_entries()
+		self.check_credit_limit(self.doc.debit_to)
 
 		if not cint(self.doc.is_pos) == 1:
 			self.update_against_document_in_jv()
@@ -533,32 +534,39 @@
 		
 		self.make_sl_entries(sl_entries)
 		
-	def make_gl_entries(self):
-		from accounts.general_ledger import make_gl_entries, merge_similar_entries
+	def make_gl_entries(self, update_gl_entries_after=True):
+		gl_entries = self.get_gl_entries()
+		
+		if gl_entries:
+			from accounts.general_ledger import make_gl_entries
+			
+			update_outstanding = cint(self.doc.is_pos) and self.doc.write_off_account \
+				and 'No' or 'Yes'
+			make_gl_entries(gl_entries, cancel=(self.doc.docstatus == 2), 
+				update_outstanding=update_outstanding, merge_entries=False)
+			
+			if update_gl_entries_after and cint(self.doc.update_stock) \
+				and cint(webnotes.defaults.get_global_default("auto_accounting_for_stock")):
+					self.update_gl_entries_after()
+				
+	def get_gl_entries(self, warehouse_account=None):
+		from accounts.general_ledger import merge_similar_entries
 		
 		gl_entries = []
 		
 		self.make_customer_gl_entry(gl_entries)
-	
+		
 		self.make_tax_gl_entries(gl_entries)
 		
 		self.make_item_gl_entries(gl_entries)
 		
 		# merge gl entries before adding pos entries
 		gl_entries = merge_similar_entries(gl_entries)
-						
+		
 		self.make_pos_gl_entries(gl_entries)
 		
-		update_outstanding = cint(self.doc.is_pos) and self.doc.write_off_account and 'No' or 'Yes'
+		return gl_entries
 		
-		if gl_entries:
-			make_gl_entries(gl_entries, cancel=(self.doc.docstatus == 2), 
-				update_outstanding=update_outstanding, merge_entries=False)
-				
-			if cint(webnotes.defaults.get_global_default("auto_accounting_for_stock")) \
-					and cint(self.doc.update_stock):
-				self.update_gl_entries_after()
-				
 	def make_customer_gl_entry(self, gl_entries):
 		if self.doc.grand_total:
 			gl_entries.append(
@@ -602,7 +610,7 @@
 		# expense account gl entries
 		if cint(webnotes.defaults.get_global_default("auto_accounting_for_stock")) \
 				and cint(self.doc.update_stock):
-			gl_entries += self.get_gl_entries_for_stock()
+			gl_entries += super(DocType, self).get_gl_entries()
 				
 	def make_pos_gl_entries(self, gl_entries):
 		if cint(self.doc.is_pos) and self.doc.cash_bank_account and self.doc.paid_amount:
@@ -866,7 +874,6 @@
 		
 def notify_errors(inv, owner):
 	import webnotes
-	import website
 		
 	exception_msg = """
 		Dear User,
diff --git a/accounts/doctype/sales_invoice/test_sales_invoice.py b/accounts/doctype/sales_invoice/test_sales_invoice.py
index fbb344e..435ba12 100644
--- a/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -3,7 +3,7 @@
 
 import webnotes
 import unittest, json
-from webnotes.utils import flt, cint
+from webnotes.utils import flt
 from webnotes.model.bean import DocstatusTransitionError, TimestampMismatchError
 from accounts.utils import get_stock_and_account_difference
 from stock.doctype.purchase_receipt.test_purchase_receipt import set_perpetual_inventory
@@ -364,6 +364,7 @@
 			from `tabGL Entry` where voucher_type='Sales Invoice' and voucher_no=%s
 			order by account asc, debit asc""", si.doc.name, as_dict=1)
 		self.assertTrue(gl_entries)
+		# print gl_entries
 		
 		stock_in_hand = webnotes.conn.get_value("Account", {"master_name": "_Test Warehouse - _TC"})
 				
@@ -382,9 +383,6 @@
 			self.assertEquals(expected_gl_entries[i][1], gle.debit)
 			self.assertEquals(expected_gl_entries[i][2], gle.credit)
 		
-		
-		
-		# cancel
 		si.cancel()
 		gle = webnotes.conn.sql("""select * from `tabGL Entry` 
 			where voucher_type='Sales Invoice' and voucher_no=%s""", si.doc.name)
@@ -395,6 +393,62 @@
 		
 		set_perpetual_inventory(0)
 		
+	def test_si_gl_entry_with_aii_and_update_stock_with_warehouse_but_no_account(self):
+		self.clear_stock_account_balance()
+		set_perpetual_inventory()
+		webnotes.delete_doc("Account", "_Test Warehouse No Account - _TC")
+		
+		# insert purchase receipt
+		from stock.doctype.purchase_receipt.test_purchase_receipt import test_records \
+			as pr_test_records
+		pr = webnotes.bean(copy=pr_test_records[0])
+		pr.doc.naming_series = "_T-Purchase Receipt-"
+		pr.doclist[1].warehouse = "_Test Warehouse No Account - _TC"
+		pr.run_method("calculate_taxes_and_totals")
+		pr.insert()
+		pr.submit()
+		
+		si_doclist = webnotes.copy_doclist(test_records[1])
+		si_doclist[0]["update_stock"] = 1
+		si_doclist[0]["posting_time"] = "12:05"
+		si_doclist[1]["warehouse"] = "_Test Warehouse No Account - _TC"
+
+		si = webnotes.bean(copy=si_doclist)
+		si.insert()
+		si.submit()
+		
+		# check stock ledger entries
+		sle = webnotes.conn.sql("""select * from `tabStock Ledger Entry` 
+			where voucher_type = 'Sales Invoice' and voucher_no = %s""", 
+			si.doc.name, as_dict=1)[0]
+		self.assertTrue(sle)
+		self.assertEquals([sle.item_code, sle.warehouse, sle.actual_qty], 
+			["_Test Item", "_Test Warehouse No Account - _TC", -1.0])
+		
+		# check gl entries
+		gl_entries = webnotes.conn.sql("""select account, debit, credit
+			from `tabGL Entry` where voucher_type='Sales Invoice' and voucher_no=%s
+			order by account asc, debit asc""", si.doc.name, as_dict=1)
+		self.assertTrue(gl_entries)
+		
+		expected_gl_entries = sorted([
+			[si.doc.debit_to, 630.0, 0.0],
+			[si_doclist[1]["income_account"], 0.0, 500.0],
+			[si_doclist[2]["account_head"], 0.0, 80.0],
+			[si_doclist[3]["account_head"], 0.0, 50.0],
+		])
+		for i, gle in enumerate(gl_entries):
+			self.assertEquals(expected_gl_entries[i][0], gle.account)
+			self.assertEquals(expected_gl_entries[i][1], gle.debit)
+			self.assertEquals(expected_gl_entries[i][2], gle.credit)
+				
+		si.cancel()
+		gle = webnotes.conn.sql("""select * from `tabGL Entry` 
+			where voucher_type='Sales Invoice' and voucher_no=%s""", si.doc.name)
+		
+		self.assertFalse(gle)
+		set_perpetual_inventory(0)
+		
 	def test_sales_invoice_gl_entry_with_aii_no_item_code(self):	
 		self.clear_stock_account_balance()
 		set_perpetual_inventory()
@@ -599,7 +653,7 @@
 		self._test_recurring_invoice(si7, True)
 
 	def _test_recurring_invoice(self, base_si, first_and_last_day):
-		from webnotes.utils import add_months, get_last_day, getdate
+		from webnotes.utils import add_months, get_last_day
 		from accounts.doctype.sales_invoice.sales_invoice import manage_recurring_invoices
 		
 		no_of_months = ({"Monthly": 1, "Quarterly": 3, "Yearly": 12})[base_si.doc.recurring_type]
diff --git a/accounts/general_ledger.py b/accounts/general_ledger.py
index b0c585a..9afcf94 100644
--- a/accounts/general_ledger.py
+++ b/accounts/general_ledger.py
@@ -3,9 +3,8 @@
 
 from __future__ import unicode_literals
 import webnotes
-from webnotes.utils import flt, cstr, now
-from webnotes.model.doc import Document
-from webnotes import msgprint, _
+from webnotes.utils import flt, cstr
+from webnotes import _
 from accounts.utils import validate_expense_against_budget
 
 
diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py
index 2b6a21f..0c5ce2a 100644
--- a/controllers/accounts_controller.py
+++ b/controllers/accounts_controller.py
@@ -5,6 +5,7 @@
 import webnotes
 from webnotes import _, msgprint
 from webnotes.utils import flt, cint, today, cstr
+from webnotes.model.code import get_obj
 from setup.utils import get_company_currency
 from accounts.utils import get_fiscal_year, validate_fiscal_year
 from utilities.transaction_base import TransactionBase, validate_conversion_rate
@@ -422,3 +423,12 @@
 			self._abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr")
 			
 		return self._abbr
+		
+	def check_credit_limit(self, account):
+		total_outstanding = webnotes.conn.sql("""
+			select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) 
+			from `tabGL Entry` where account = %s""", account)
+		
+		total_outstanding = flt(total_outstanding[0][0]) if total_outstanding else 0
+		if total_outstanding:
+			get_obj('Account', account).check_credit_limit(total_outstanding)
\ No newline at end of file
diff --git a/controllers/stock_controller.py b/controllers/stock_controller.py
index 359dc9e..e07e752 100644
--- a/controllers/stock_controller.py
+++ b/controllers/stock_controller.py
@@ -11,7 +11,7 @@
 from accounts.general_ledger import make_gl_entries, delete_gl_entries
 
 class StockController(AccountsController):
-	def make_gl_entries(self):
+	def make_gl_entries(self, update_gl_entries_after=True):
 		if self.doc.docstatus == 2:
 			delete_gl_entries(voucher_type=self.doc.doctype, voucher_no=self.doc.name)
 			
@@ -19,12 +19,13 @@
 			warehouse_account = self.get_warehouse_account()
 		
 			if self.doc.docstatus==1:
-				gl_entries = self.get_gl_entries_for_stock(warehouse_account)
+				gl_entries = self.get_gl_entries(warehouse_account)
 				make_gl_entries(gl_entries)
 
-			self.update_gl_entries_after(warehouse_account)
+			if update_gl_entries_after:
+				self.update_gl_entries_after(warehouse_account)
 	
-	def get_gl_entries_for_stock(self, warehouse_account=None, default_expense_account=None,
+	def get_gl_entries(self, warehouse_account=None, default_expense_account=None,
 			default_cost_center=None):
 		from accounts.general_ledger import process_gl_map
 		if not warehouse_account:
@@ -99,12 +100,10 @@
 		gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
 		if not warehouse_account:
 			warehouse_account = self.get_warehouse_account()
-		
 		for voucher_type, voucher_no in future_stock_vouchers:
 			existing_gle = gle.get((voucher_type, voucher_no), [])
 			voucher_obj = webnotes.get_obj(voucher_type, voucher_no)
-			expected_gle = voucher_obj.get_gl_entries_for_stock(warehouse_account)
-			
+			expected_gle = voucher_obj.get_gl_entries(warehouse_account)
 			if expected_gle:
 				matched = True
 				if existing_gle:
@@ -121,7 +120,7 @@
 									
 				if not matched:
 					self.delete_gl_entries(voucher_type, voucher_no)
-					make_gl_entries(expected_gle)
+					voucher_obj.make_gl_entries(update_gl_entries_after=False)
 			else:
 				self.delete_gl_entries(voucher_type, voucher_no)
 				
diff --git a/patches/november_2013/__init__.py b/patches/november_2013/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/patches/november_2013/__init__.py
diff --git a/patches/november_2013/p01_make_gl_entries_for_si.py b/patches/november_2013/p01_make_gl_entries_for_si.py
new file mode 100644
index 0000000..cfe107a
--- /dev/null
+++ b/patches/november_2013/p01_make_gl_entries_for_si.py
@@ -0,0 +1,12 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
+# License: GNU General Public License v3. See license.txt
+
+import webnotes
+
+def execute():
+	si_no_gle = webnotes.conn.sql("""select si.name from `tabSales Invoice` si 
+		where docstatus=1 and not exists(select name from `tabGL Entry` 
+			where voucher_type='Sales Invoice' and voucher_no=si.name)""")
+
+	for si in si_no_gle:
+		webnotes.get_obj("Sales Invoice", si[0]).make_gl_entries()
\ No newline at end of file
diff --git a/patches/patch_list.py b/patches/patch_list.py
index 0cc393e..82eb6e4 100644
--- a/patches/patch_list.py
+++ b/patches/patch_list.py
@@ -230,4 +230,5 @@
 	"patches.october_2013.p07_rename_for_territory",
 	"patches.june_2013.p07_taxes_price_list_for_territory",
 	"patches.october_2013.p08_cleanup_after_item_price_module_change",
+	"patches.november_2013.p01_make_gl_entries_for_si",
 ]
\ 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 ad626da..3be27b2 100644
--- a/selling/doctype/sales_common/sales_common.py
+++ b/selling/doctype/sales_common/sales_common.py
@@ -9,9 +9,6 @@
 from webnotes.model.bean import getlist
 from webnotes.model.code import get_obj
 from webnotes import msgprint, _
-from setup.utils import get_company_currency
-
-get_value = webnotes.conn.get_value
 
 from utilities.transaction_base import TransactionBase
 
@@ -295,14 +292,12 @@
 	def check_credit(self,obj,grand_total):
 		acc_head = webnotes.conn.sql("select name from `tabAccount` where company = '%s' and master_name = '%s'"%(obj.doc.company, obj.doc.customer))
 		if acc_head:
-			tot_outstanding = 0
 			dbcr = webnotes.conn.sql("""select sum(debit), sum(credit) from `tabGL Entry` 
 				where account = %s""", acc_head[0][0])
-			if dbcr:
-				tot_outstanding = flt(dbcr[0][0])-flt(dbcr[0][1])
+			tot_outstanding = flt(dbcr[0][0])-flt(dbcr[0][1]) if dbcr else 0
 
 			exact_outstanding = flt(tot_outstanding) + flt(grand_total)
-			get_obj('Account',acc_head[0][0]).check_credit_limit(acc_head[0][0], obj.doc.company, exact_outstanding)
+			get_obj('Account',acc_head[0][0]).check_credit_limit(exact_outstanding)
 
 	def get_prevdoc_date(self, obj):
 		for d in getlist(obj.doclist, obj.fname):
diff --git a/stock/doctype/purchase_receipt/purchase_receipt.py b/stock/doctype/purchase_receipt/purchase_receipt.py
index 6d4320f..1d91c4b 100644
--- a/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -294,11 +294,10 @@
 	def get_rate(self,arg):
 		return get_obj('Purchase Common').get_rate(arg,self)
 		
-	def get_gl_entries_for_stock(self, warehouse_account=None):
+	def get_gl_entries(self, warehouse_account=None):
 		against_stock_account = self.get_company_default("stock_received_but_not_billed")
 		
-		gl_entries = super(DocType, self).get_gl_entries_for_stock(warehouse_account, 
-			against_stock_account)
+		gl_entries = super(DocType, self).get_gl_entries(warehouse_account, against_stock_account)
 		return gl_entries
 		
 	
diff --git a/stock/doctype/stock_entry/stock_entry.py b/stock/doctype/stock_entry/stock_entry.py
index 12b0f51..c91239d 100644
--- a/stock/doctype/stock_entry/stock_entry.py
+++ b/stock/doctype/stock_entry/stock_entry.py
@@ -478,7 +478,7 @@
 					self.doc.from_warehouse = ""
 					
 				item = webnotes.conn.sql("""select name, item_name, description, 
-					uom, purchase_account, cost_center from `tabItem` 
+					stock_uom, purchase_account, cost_center from `tabItem` 
 					where name=(select item from tabBOM where name=%s)""", 
 					self.doc.bom_no, as_dict=1)
 				self.add_to_stock_entry_detail({
@@ -486,7 +486,7 @@
 						"qty": self.doc.fg_completed_qty,
 						"item_name": item[0].item_name,
 						"description": item[0]["description"],
-						"stock_uom": item[0]["uom"],
+						"stock_uom": item[0]["stock_uom"],
 						"from_warehouse": "",
 						"expense_account": item[0].purchase_account,
 						"cost_center": item[0].cost_center,
diff --git a/stock/doctype/stock_reconciliation/stock_reconciliation.py b/stock/doctype/stock_reconciliation/stock_reconciliation.py
index 9feb57e..22d0117 100644
--- a/stock/doctype/stock_reconciliation/stock_reconciliation.py
+++ b/stock/doctype/stock_reconciliation/stock_reconciliation.py
@@ -275,11 +275,11 @@
 				"posting_time": self.doc.posting_time
 			})
 			
-	def get_gl_entries_for_stock(self, warehouse_account=None):
+	def get_gl_entries(self, warehouse_account=None):
 		if not self.doc.cost_center:
 			msgprint(_("Please enter Cost Center"), raise_exception=1)
 			
-		return super(DocType, self).get_gl_entries_for_stock(warehouse_account, 		
+		return super(DocType, self).get_gl_entries(warehouse_account, 		
 			self.doc.expense_account, self.doc.cost_center)
 		
 			
diff --git a/stock/doctype/warehouse/test_warehouse.py b/stock/doctype/warehouse/test_warehouse.py
index 76b1818..43b2600 100644
--- a/stock/doctype/warehouse/test_warehouse.py
+++ b/stock/doctype/warehouse/test_warehouse.py
@@ -23,5 +23,10 @@
 		"doctype": "Warehouse User",
 		"parentfield": "warehouse_users",
 		"user": "test2@example.com"
-	}]	
+	}],
+	[{
+		"doctype": "Warehouse",
+		"warehouse_name": "_Test Warehouse No Account",
+		"company": "_Test Company",
+	}],
 ]