Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py
index 466b2f9..79ca322 100644
--- a/erpnext/accounts/doctype/account/account.py
+++ b/erpnext/accounts/doctype/account/account.py
@@ -83,7 +83,7 @@
 	
 	# Account name must be unique
 	def validate_duplicate_account(self):
-		if (self.doc.__islocal or (not self.doc.name)) and sql("select name from tabAccount where account_name=%s and company=%s", (self.doc.account_name, self.doc.company)):
+		if (self.doc.fields.get('__islocal') or (not self.doc.name)) and sql("select name from tabAccount where account_name=%s and company=%s", (self.doc.account_name, self.doc.company)):
 			msgprint("Account Name already exists, please rename", raise_exception=1)
 				
 	# validate root details
diff --git a/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.py b/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.py
index 03acc0a..4bf5e97 100644
--- a/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.py
+++ b/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.py
@@ -63,6 +63,13 @@
 		vouchers = []
 		for d in getlist(self.doclist, 'entries'):
 			if d.clearance_date:
+				if getdate(d.clearance_date) < getdate(d.cheque_date):
+					msgprint("Clearance Date can not be before Cheque Date (Row #%s)" % 
+						d.idx, raise_exception=1)
+				if getdate(d.clearance_date) < getdate(d.posting_date):
+					msgprint("Clearance Date can not be before Posting Date (Row #%s)" %
+						d.idx, raise_exception=1)
+					
 				sql("update `tabJournal Voucher` set clearance_date = %s, modified = %s where name=%s", (d.clearance_date, nowdate(), d.voucher_id))
 				vouchers.append(d.voucher_id)
 
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice_list.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice_list.js
index f9dd4cd..7bb2558 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice_list.js
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice_list.js
@@ -16,9 +16,8 @@
 
 	prepare_data: function(data) {
 		this._super(data);
-		data.paid = flt(
-			((data.grand_total - data.outstanding_amount) / data.grand_total) * 100,
-			2);
+		data.paid = data.docstatus == 1 ?
+			flt(((data.grand_total - data.outstanding_amount) / data.grand_total) * 100, 2) : 0;
 	},
 
 	columns: [
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice_list.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice_list.js
index 1c3b2d3..cbe1741 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice_list.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice_list.js
@@ -14,7 +14,8 @@
 	},
 	prepare_data: function(data) {
 		this._super(data);
-		data.paid = flt((data.grand_total - data.outstanding_amount) / data.grand_total * 100, 2);
+		data.paid = (data.docstatus == 1) ? 
+				flt((data.grand_total - data.outstanding_amount) / data.grand_total * 100, 2) : 0;
 	},
 	columns: [
 		{width: '3%', content: 'check'},
diff --git a/erpnext/patches/september_2012/add_stock_ledger_entry_index.py b/erpnext/patches/september_2012/add_stock_ledger_entry_index.py
index bac9a08..7153a7c 100644
--- a/erpnext/patches/september_2012/add_stock_ledger_entry_index.py
+++ b/erpnext/patches/september_2012/add_stock_ledger_entry_index.py
@@ -3,7 +3,8 @@
 def execute():
 	webnotes.conn.commit()
 	try:
-		webnotes.conn.sql("""alter table `tabStock Ledger Entry` add index posting_sort_index(posting_date, posting_time, name)""")webnotes.conn.commit()
+		webnotes.conn.sql("""alter table `tabStock Ledger Entry` add index posting_sort_index(posting_date, posting_time, name)""")
+		webnotes.conn.commit()
 	except Exception, e:
 		if e.args[0]!=1061: raise e
 	webnotes.conn.begin()
diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py
index 38c689e..4386f2e 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -37,6 +37,11 @@
 class DocType:
 	def __init__(self,d,dl):
 		self.doc, self.doclist = d,dl
+		
+	def validate(self):
+		if self.doc.fields.get('__islocal') and len(self.doc.abbr) > 5:
+			webnotes.msgprint("Abbreviation cannot have more than 5 characters",
+				raise_exception=1)
 	
 	# Create default accounts
 	# ---------------------------------------------------
diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js
index 0a255d1..96c0339 100644
--- a/erpnext/stock/doctype/item/item.js
+++ b/erpnext/stock/doctype/item/item.js
@@ -18,6 +18,10 @@
 	// make sensitive fields(has_serial_no, is_stock_item, valuation_method)
 	// read only if any stock ledger entry exists
 
+	if (!doc.__islocal) {
+		set_field_permlevel("item_code", 1);
+	}
+
 	if ((!doc.__islocal) && (doc.is_stock_item == 'Yes')) {
 		var callback = function(r, rt) {
 			if (r.message == 'exists') permlevel = 1;
diff --git a/erpnext/support/doctype/maintenance_visit/maintenance_visit.py b/erpnext/support/doctype/maintenance_visit/maintenance_visit.py
index 5175ca9..89fc2f0 100644
--- a/erpnext/support/doctype/maintenance_visit/maintenance_visit.py
+++ b/erpnext/support/doctype/maintenance_visit/maintenance_visit.py
@@ -136,6 +136,7 @@
 
 	def check_if_last_visit(self):
 		"""check if last maintenance visit against same sales order/ customer issue"""
+		check_for_docname = check_for_doctype = None
 		for d in getlist(self.doclist, 'maintenance_visit_details'):
 			if d.prevdoc_docname:
 				check_for_docname = d.prevdoc_docname