Merge pull request #175 from anandpdoshi/master

Added feature: Income Year To Date in Email Digest
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 3498a7f..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']:
@@ -179,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
@@ -203,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({
@@ -233,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
 			})
 
 
@@ -578,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',
diff --git a/erpnext/setup/doctype/email_digest/email_digest.txt b/erpnext/setup/doctype/email_digest/email_digest.txt
index 3d8de7b..6d8a3da 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 17:55:57',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
 
 	# These values are common for all DocType
 	{
-		'_last_update': '1323353260',
+		'_last_update': '1323937389',
 		'autoname': 'Prompt',
 		'colour': 'White:FFF',
 		'doctype': 'DocType',
@@ -21,7 +21,7 @@
 		'name': '__common__',
 		'section_style': 'Simple',
 		'show_in_menu': 0,
-		'version': 79
+		'version': 80
 	},
 
 	# These values are common for all DocField
@@ -137,6 +137,15 @@
 	# DocField
 	{
 		'doctype': 'DocField',
+		'fieldtype': 'Button',
+		'label': 'Add Recipients',
+		'permlevel': 0,
+		'trigger': 'Client'
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
 		'fieldtype': 'Column Break',
 		'permlevel': 0
 	},
@@ -294,6 +303,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',