Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/erpnext/patches/edigest_enable_income_year_to_date.py b/erpnext/patches/edigest_enable_income_year_to_date.py
new file mode 100644
index 0000000..380de83
--- /dev/null
+++ b/erpnext/patches/edigest_enable_income_year_to_date.py
@@ -0,0 +1,11 @@
+import webnotes
+from webnotes.model.doc import Document
+
+def execute():
+	companies_list = webnotes.conn.sql("SELECT company_name FROM `tabCompany`", as_list=1)
+	for company in companies_list:
+		if company and company[0]:
+			edigest = Document('Email Digest', "Default Weekly Digest - " + company[0])
+			if edigest:
+				edigest.income_year_to_date = 1
+				edigest.save()
diff --git a/erpnext/patches/remove_previous_field_property_setter.py b/erpnext/patches/remove_previous_field_property_setter.py
new file mode 100644
index 0000000..677188e
--- /dev/null
+++ b/erpnext/patches/remove_previous_field_property_setter.py
@@ -0,0 +1,6 @@
+import webnotes
+def execute():
+	webnotes.conn.sql("""\
+		DELETE FROM `tabProperty Setter`
+		WHERE property='previous_field'
+	""")
diff --git a/erpnext/patches/update_0_idx.py b/erpnext/patches/update_0_idx.py
new file mode 100644
index 0000000..9c31f1d
--- /dev/null
+++ b/erpnext/patches/update_0_idx.py
@@ -0,0 +1,9 @@
+import webnotes
+def execute():
+	doc_type_list = webnotes.conn.sql("""SELECT DISTINCT parent FROM `tabDocField` where idx=0""")
+	for doc_type in doc_type_list:
+		if doc_type and doc_type[0]:
+			webnotes.conn.sql("""\
+				UPDATE `tabDocField` SET idx=idx+1
+				WHERE parent=%s
+			""", doc_type[0])
diff --git a/erpnext/setup/doctype/email_digest/email_digest.js b/erpnext/setup/doctype/email_digest/email_digest.js
index 728c8b4..81b37e4 100644
--- a/erpnext/setup/doctype/email_digest/email_digest.js
+++ b/erpnext/setup/doctype/email_digest/email_digest.js
@@ -4,6 +4,7 @@
 	var err_msg = "There was an error. One probable reason could be that you haven't saved the form. Please contact support@erpnext.com if the problem persists."
 	
 	cur_frm.add_custom_button('View Now', function() {
+		doc = locals[dt][dn];
 		if(doc.__unsaved != 1) {
 			$c_obj(make_doclist(dt, dn), 'get', '', function(r, rt) {
 				if(r.exc) {
@@ -26,6 +27,7 @@
 		}	
 	}, 1);
 	cur_frm.add_custom_button('Send Now', function() {
+		doc = locals[dt][dn];
 		if(doc.__unsaved != 1) {
 			$c_obj(make_doclist(dt, dn), 'send', '', function(r, rt) {
 				if(r.exc) {
diff --git a/erpnext/setup/doctype/email_digest/email_digest.py b/erpnext/setup/doctype/email_digest/email_digest.py
index 5b76024..cd84d10 100644
--- a/erpnext/setup/doctype/email_digest/email_digest.py
+++ b/erpnext/setup/doctype/email_digest/email_digest.py
@@ -1,4 +1,5 @@
 import webnotes
+import webnotes.utils
 
 class DocType:
 	def __init__(self, doc, doclist=[]):
@@ -61,6 +62,11 @@
 				'debit_or_credit': 'Credit'
 			}),
 
+			'income_year_to_date': self.generate_gle_query({
+				'type': 'income_year_to_date',
+				'debit_or_credit': 'Credit'
+			}),
+
 			'expenses_booked': self.generate_gle_query({
 				'type': 'expenses_booked',
 				'debit_or_credit': 'Debit'
@@ -112,7 +118,7 @@
 			if self.doc.fields[query] and query_dict[query]:
 				#webnotes.msgprint(query)
 				res = webnotes.conn.sql(query_dict[query], as_dict=1)
-				if query == 'income':
+				if query in ['income', 'income_year_to_date']:
 					for r in res:
 						r['value'] = float(r['credit'] - r['debit'])
 				elif query in ['expenses_booked', 'bank_balance']:
@@ -121,6 +127,8 @@
 				#webnotes.msgprint(query)
 				#webnotes.msgprint(res)
 				result[query] = (res and len(res)==1) and res[0] or (res and res or None)
+				if result[query] is None:
+					del result[query]
 		
 		return result
 
@@ -177,6 +185,21 @@
 					%(start_date_condition)s AND
 					%(end_date_condition)s""" % args
 
+		elif args['type'] == 'income_year_to_date':
+			query = """
+				SELECT
+					IFNULL(SUM(IFNULL(gle.debit, 0)), 0) AS 'debit',
+					IFNULL(SUM(IFNULL(gle.credit, 0)), 0) AS 'credit',
+					%(common_select)s
+				FROM
+					%(common_from)s
+				WHERE
+					%(common_where)s AND
+					ac.is_pl_account = 'Yes' AND
+					ac.debit_or_credit = '%(debit_or_credit)s' AND					
+					%(fiscal_start_date_condition)s AND
+					%(end_date_condition)s""" % args
+
 		elif args['type'] == 'bank_balance':
 			query = """
 				SELECT
@@ -201,6 +224,7 @@
 			Adds common conditions in dictionary "args"
 		"""
 		start_date, end_date = self.get_start_end_dates()
+		fiscal_start_date = webnotes.utils.get_defaults()['year_start_date']
 
 		if 'new' in args['type']:
 			args.update({
@@ -231,7 +255,9 @@
 
 				'start_date_condition': "gle.posting_date >= '%s'" % start_date,
 
-				'end_date_condition': "gle.posting_date <= '%s'" % end_date
+				'end_date_condition': "gle.posting_date <= '%s'" % end_date,
+
+				'fiscal_start_date_condition': "gle.posting_date >= '%s'" % fiscal_start_date
 			})
 
 
@@ -576,6 +602,15 @@
 				'idx': 302
 			},
 
+			'income_year_to_date': {
+				'table': 'income_year_to_date' in result and table({
+					'head': 'Income Year To Date',
+					'body': currency_amount_str \
+						% (currency, fmt_money(result['income_year_to_date']['value']))
+				}),
+				'idx': 303
+			},
+
 			'expenses_booked': {
 				'table': 'expenses_booked' in result and table({
 					'head': 'Expenses Booked',
@@ -586,7 +621,7 @@
 			},
 
 			'bank_balance': {
-				'table': 'bank_balance' in result and table({
+				'table': 'bank_balance' in result and result['bank_balance'] and table({
 					'head': 'Bank Balance',
 					'body': [
 						[
@@ -662,8 +697,15 @@
 					table_list.append(\
 						"<div style='font-size: 16px; color: grey'>[" + \
 							k.capitalize() + \
-							"]<br />Missing: Ledger of type 'Bank or Cash'\
+							"]<br />Missing: Account of type 'Bank or Cash'\
 						</div>")
+				elif k=='bank_balance':
+					table_list.append(\
+						"<div style='font-size: 16px; color: grey'>[" + \
+							"Bank Balance" + \
+							"]<br />Alert: GL Entry not found for Account of type 'Bank or Cash'\
+						</div>")
+					
 		
 		i = 0
 		result = []
diff --git a/erpnext/setup/doctype/email_digest/email_digest.txt b/erpnext/setup/doctype/email_digest/email_digest.txt
index 3d8de7b..897adad 100644
--- a/erpnext/setup/doctype/email_digest/email_digest.txt
+++ b/erpnext/setup/doctype/email_digest/email_digest.txt
@@ -3,16 +3,16 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2011-12-12 10:41:40',
+		'creation': '2011-12-14 12:15:09',
 		'docstatus': 0,
-		'modified': '2011-12-15 13:53:08',
+		'modified': '2011-12-22 19:01:33',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
 
 	# These values are common for all DocType
 	{
-		'_last_update': '1323353260',
+		'_last_update': '1324556758',
 		'autoname': 'Prompt',
 		'colour': 'White:FFF',
 		'doctype': 'DocType',
@@ -21,7 +21,7 @@
 		'name': '__common__',
 		'section_style': 'Simple',
 		'show_in_menu': 0,
-		'version': 79
+		'version': 81
 	},
 
 	# These values are common for all DocField
@@ -294,6 +294,16 @@
 	{
 		'depends_on': 'eval:doc.use_standard',
 		'doctype': 'DocField',
+		'fieldname': 'income_year_to_date',
+		'fieldtype': 'Check',
+		'label': 'Income Year to Date',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'depends_on': 'eval:doc.use_standard',
+		'doctype': 'DocField',
 		'fieldname': 'bank_balance',
 		'fieldtype': 'Check',
 		'label': 'Bank Balance',