Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/home/page/dashboard/__init__.py b/home/page/dashboard/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/home/page/dashboard/__init__.py
diff --git a/home/page/dashboard/dashboard.css b/home/page/dashboard/dashboard.css
new file mode 100644
index 0000000..d8e8b54
--- /dev/null
+++ b/home/page/dashboard/dashboard.css
@@ -0,0 +1,12 @@
+div.dashboard_table td {
+	width: 50%;
+}
+
+div.dashboard-title {
+	font-weight: bold;
+	padding: '3px 0px';
+}
+
+div.dashboard-graph {
+	height: 180px;
+}
\ No newline at end of file
diff --git a/home/page/dashboard/dashboard.html b/home/page/dashboard/dashboard.html
new file mode 100644
index 0000000..5ffef2f
--- /dev/null
+++ b/home/page/dashboard/dashboard.html
@@ -0,0 +1,8 @@
+<div class="layout_wrapper dashboard">
+	<div class="header"></div>
+	<div class="body">
+		<!-- 4x2 table to show the dashboards-->
+		<div class="help_box">Loading...</div>
+		<div class="dashboard_table"></div>
+	</div>
+</div>
\ No newline at end of file
diff --git a/home/page/dashboard/dashboard.js b/home/page/dashboard/dashboard.js
new file mode 100644
index 0000000..c5dd361
--- /dev/null
+++ b/home/page/dashboard/dashboard.js
@@ -0,0 +1,139 @@
+pscript.onload_dashboard = function() {
+	// load jqplot
+	$.scriptPath = 'js/'
+	$.require(['jquery/jquery.jqplot.min.js', 
+	'jquery/jqplot-plugins/jqplot.barRenderer.js', 
+	'jquery/jqplot-plugins/jqplot.canvasAxisTickRenderer.min.js',
+	'jquery/jqplot-plugins/jqplot.canvasTextRenderer.min.js',
+	'jquery/jqplot-plugins/jqplot.categoryAxisRenderer.min.js']);
+
+
+	pscript.dashboard_settings = {
+		company: sys_defaults.company,
+		start: dateutil.obj_to_str(dateutil.add_days(new Date(), -60)),
+		end: dateutil.obj_to_str(new Date()),
+		interval: 7
+	}
+	
+	var ph = new PageHeader($('.dashboard .header').get(0), 'Dashboards');
+	var db = new Dashboard();
+
+	ph.add_button('Settings', db.show_settings);
+	
+	db.refresh();
+	
+}
+
+Dashboard = function() {
+	var me = this;
+	$.extend(me, {
+		refresh: function() {
+			$('.dashboard .help_box').css('display', 'block');
+			$c_page('home', 'dashboard', 'load_dashboard', JSON.stringify(pscript.dashboard_settings), function(r,rt) {
+				$('.dashboard .help_box').css('display', 'none');
+				me.render(r.message);
+			})			
+		},
+		
+		render: function(data) {
+			$('.dashboard_table').html('');
+			var t = make_table($('.dashboard_table').get(0), 4, 2, '100%', ['50%', '50%'], {padding: '5px'});
+			var ridx=0; var cidx=0;
+			for(var i=0; i< data.length; i++) {
+				// switch columns and rows
+				if(cidx==2) { cidx=0; ridx++}
+				
+				// give an id!
+				var cell = $td(t,ridx,cidx);
+				var title = $a(cell, 'div', 'dashboard-title', '', data[i][0].title);
+				var parent = $a(cell, 'div', 'dashboard-graph')
+				
+				parent.id = '_dashboard' + ridx + '-' + cidx;
+				
+				// render graph
+				me.render_graph(parent.id, data[i][1]);
+				cidx++;
+			}
+		},
+		
+		render_graph: function(parent, values) {
+			var vl = [];
+			$.each(values, function(i,v) { 
+				vl.push([dateutil.str_to_user(v[0]), v[1]]);
+			});
+			$.jqplot(parent, [vl], {
+				seriesDefaults:{
+					renderer:$.jqplot.BarRenderer,
+					rendererOptions: {fillToZero: true},
+				},
+				axes: {
+					// Use a category axis on the x axis and use our custom ticks.
+					xaxis: {
+						min: 0,
+						renderer: $.jqplot.CategoryAxisRenderer,
+						tickRenderer: $.jqplot.CanvasAxisTickRenderer,
+						tickOptions: {
+							angle: -30,
+							fontSize: '8pt'
+						}
+					},
+					// Pad the y axis just a little so bars can get close to, but
+					// not touch, the grid boundaries.  1.2 is the default padding.
+					yaxis: {
+						min: 0,
+						pad: 1.05,
+						tickOptions: {formatString: '%d'}
+					}
+				}
+			});
+		},
+		
+		show_settings: function() {
+			var d = new wn.widgets.Dialog({
+				title: 'Set Company Settings',
+				width: 500,
+				fields: [
+					{
+						label:'Company', 
+						reqd: 1,
+						fieldname:'company', 
+						fieldtype:'Link',
+						options: 'Company'
+					},
+					{
+						label:'Start Date', 
+						reqd: 1,
+						fieldname:'start', 
+						fieldtype:'Date',
+					},
+					{
+						label:'End Date', 
+						reqd: 1,
+						fieldname:'end', 
+						fieldtype:'Date',
+					},
+					{
+						label:'Interval', 
+						reqd: 1,
+						fieldname:'interval', 
+						fieldtype:'Int'
+					},
+					{
+						label:'Regenerate', 
+						fieldname:'refresh', 
+						fieldtype:'Button'
+					}
+				]
+			});
+			d.onshow = function() {
+				d.set_values(pscript.dashboard_settings);
+			}
+			d.fields_dict.refresh.input.onclick = function() {
+				pscript.dashboard_settings = d.get_values();
+				me.refresh();
+				d.hide();
+			}
+			d.show();
+		}
+	})
+}
diff --git a/home/page/dashboard/dashboard.py b/home/page/dashboard/dashboard.py
new file mode 100644
index 0000000..2c76b15
--- /dev/null
+++ b/home/page/dashboard/dashboard.py
@@ -0,0 +1,220 @@
+dashboards = [
+	{
+		'type': 'account',
+		'account': 'Income',
+		'title': 'Income'
+	},
+	
+	{
+		'type': 'account',
+		'account': 'Expenses',
+		'title': 'Expenses'
+	},
+
+	{
+		'type': 'from_company',
+		'account': 'receivables_group',
+		'title': 'Receivables'
+	},
+
+	{
+		'type': 'from_company',
+		'account': 'payables_group',
+		'title': 'Payables'
+	},
+
+	{
+		'type': 'cash',
+		'debit_or_credit': 'Debit',
+		'title': 'Cash Inflow'
+	},
+
+	{
+		'type': 'cash',
+		'debit_or_credit': 'Credit',
+		'title': 'Cash Outflow'
+	},
+
+	{
+		'type': 'creation',
+		'doctype': 'Quotation',
+		'title': 'New Quotations'
+	},
+	
+	{
+		'type': 'creation',
+		'doctype': 'Sales Order',
+		'title': 'New Orders'
+	}
+]
+
+
+class DashboardWidget:
+	def __init__(self, company, start, end, interval):
+		import webnotes
+		from webnotes.utils import getdate
+		from webnotes.model.code import get_obj
+		
+		self.company = company
+		self.abbr = webnotes.conn.get_value('Company', company, 'abbr')
+		self.start = getdate(start)
+		self.end = getdate(end)
+		
+		self.interval = interval
+		self.fiscal_year = webnotes.conn.sql("""
+			select name from `tabFiscal Year` 
+			where year_start_date <= %s and
+			DATE_ADD(year_start_date, INTERVAL 1 YEAR) >= %s
+			""", (start, start))[0][0]
+		self.glc = get_obj('GL Control')
+		self.cash_accounts = [d[0] for d in webnotes.conn.sql("""
+			select name from tabAccount 
+			where account_type='Bank or Cash'
+			and company = %s and docstatus = 0 
+			""", company)]
+		
+	def timeline(self):
+		"""
+			get the timeline for the dashboard
+		"""
+		import webnotes
+		from webnotes.utils import add_days
+		tl = []
+	
+		if self.start > self.end:
+			webnotes.msgprint("Start must be before end", raise_exception=1)
+
+		curr = self.start
+		tl.append(curr)
+	
+		while curr < self.end:
+			curr = add_days(curr, self.interval, 'date')
+			tl.append(curr)
+
+		tl.append(self.end)
+
+		return tl
+		
+	def generate(self, opts):
+		"""
+			Generate the dasboard
+		"""
+		tl = self.timeline()
+		self.out = []
+		
+		for i in range(len(tl)-1):
+			self.out.append([tl[i+1].strftime('%Y-%m-%d'), self.value(opts, tl[i], tl[i+1]) or 0])
+			
+		return self.out
+
+	def get_account_balance(self, acc, start):
+		"""
+			Get as on account balance
+		"""
+		import webnotes
+		# add abbreviation to company
+		
+		if not acc.endswith(self.abbr):
+			acc += ' - ' + self.abbr
+
+		# get other reqd parameters
+		try:
+			globals().update(webnotes.conn.sql('select debit_or_credit, lft, rgt from tabAccount where name=%s', acc, as_dict=1)[0])
+		except Exception,e:
+			webnotes.msgprint('Wrongly defined account: ' + acc)
+			print acc
+			raise e
+		
+		return self.glc.get_as_on_balance(acc, self.fiscal_year, start, debit_or_credit, lft, rgt)
+
+	def get_creation_trend(self, doctype, start, end):
+		"""
+			Get creation # of creations in period
+		"""
+		import webnotes
+		return int(webnotes.conn.sql("""
+			select count(*) from `tab%s` where creation between %s and %s and docstatus=1
+		""" % (doctype, '%s','%s'), (start, end))[0][0])
+
+	def get_account_amt(self, acc, start, end, debit_or_credit):
+		"""
+			Get debit, credit over a period
+		"""
+		import webnotes
+		# add abbreviation to company
+		
+		if not acc.endswith(self.abbr):
+			acc += ' - ' + self.abbr
+			
+		ret = webnotes.conn.sql("""
+			select ifnull(sum(ifnull(t1.debit,0)),0), ifnull(sum(ifnull(t1.credit,0)),0)
+			from `tabGL Entry` t1, tabAccount t2
+			where t1.account = t2.name
+			and t2.is_pl_account = 'Yes'
+			and t2.debit_or_credit=%s
+			and ifnull(t1.is_cancelled, 'No')='No'
+			and t1.posting_date between %s and %s
+		""", (debit_or_credit, start, end))[0]
+		
+		return debit_or_credit=='Credit' and float(ret[1]-ret[0]) or float(ret[0]-ret[1])
+
+	def value(self, opts, start, end):
+		"""
+			Value of the series on a particular date
+		"""
+		import webnotes
+		if opts['type']=='account':
+			debit_or_credit = 'Debit'
+			if opts['account']=='Income':
+				debit_or_credit = 'Credit'
+
+			return self.get_account_amt(opts['account'], start, end, debit_or_credit)
+		
+		elif opts['type']=='from_company':
+			acc = webnotes.conn.get_value('Company', self.company, \
+				opts['account'].split('.')[-1])
+			
+			return self.get_account_balance(acc, start)[2]
+						
+		elif opts['type']=='cash':
+			if opts['debit_or_credit']=='Credit':
+				return sum([self.get_account_amt(acc, start, end, opts['debit_or_credit']) for acc in self.cash_accounts]) or 0
+			elif opts['debit_or_credit']=='Debit':
+				return sum([self.get_account_amt(acc, start, end, opts['debit_or_credit']) for acc in self.cash_accounts]) or 0
+			
+		elif opts['type']=='creation':
+			return self.get_creation_trend(opts['doctype'], start, end)
+
+
+def load_dashboard(args):
+	"""
+		Get dashboard based on
+		1. Company (default company)
+		2. Start Date (last 3 months)
+		3. End Date (today)
+		4. Interval (7 days)
+	"""
+	dl = []
+	import json
+	args = json.loads(args)
+	dw = DashboardWidget(args['company'], args['start'], args['end'], int(args['interval']))
+
+	# render the dashboards
+	for d in dashboards:
+		dl.append([d, dw.generate(d)])
+
+	return dl
+
+if __name__=='__main__':
+	import sys
+	sys.path.append('/var/www/webnotes/wnframework/cgi-bin')
+	from webnotes.db import Database
+	import webnotes
+	webnotes.conn = Database(use_default=1)
+	webnotes.session = {'user':'Administrator'}
+	print load_dashboard("""{
+		"company": "My Test",
+		"start": "2011-05-01",
+		"end": "2011-08-01",
+		"interval": "7"
+	}""")
\ No newline at end of file
diff --git a/home/page/dashboard/dashboard.txt b/home/page/dashboard/dashboard.txt
new file mode 100644
index 0000000..46bc2bc
--- /dev/null
+++ b/home/page/dashboard/dashboard.txt
@@ -0,0 +1,49 @@
+# Page, dashboard
+[
+
+	# These values are common in all dictionaries
+	{
+		'creation': '2011-08-25 16:22:44',
+		'docstatus': 0,
+		'modified': '2011-08-25 16:22:54',
+		'modified_by': 'Administrator',
+		'owner': 'Administrator'
+	},
+
+	# These values are common for all Page
+	{
+		'category': 'Standard',
+		'doctype': 'Page',
+		'module': 'Home',
+		'name': '__common__',
+		'page_name': 'Dashboard',
+		'standard': 'Yes'
+	},
+
+	# These values are common for all Page Role
+	{
+		'doctype': 'Page Role',
+		'name': '__common__',
+		'parent': 'dashboard',
+		'parentfield': 'roles',
+		'parenttype': 'Page'
+	},
+
+	# Page, dashboard
+	{
+		'doctype': 'Page',
+		'name': 'dashboard'
+	},
+
+	# Page Role
+	{
+		'doctype': 'Page Role',
+		'role': 'System Manager'
+	},
+
+	# Page Role
+	{
+		'doctype': 'Page Role',
+		'role': 'Accounts Manager'
+	}
+]
\ No newline at end of file
diff --git a/hr/doctype/salary_manager/salary_manager.py b/hr/doctype/salary_manager/salary_manager.py
index ec8fba2..5071056 100644
--- a/hr/doctype/salary_manager/salary_manager.py
+++ b/hr/doctype/salary_manager/salary_manager.py
@@ -68,9 +68,9 @@
 		emp_list = self.get_emp_list()
 		log = ""
 		if emp_list:
-			log = "<table><tr><td>Following Salary Slip has been created: </td></tr><tr><td><u>SAL SLIP ID</u></td><td><u>EMPLOYEE NAME</u></td></tr>"
+			log = "<table><tr><td colspan = 2>Following Salary Slip has been created: </td></tr><tr><td><u>SAL SLIP ID</u></td><td><u>EMPLOYEE NAME</u></td></tr>"
 		else:
-			log = "<table><tr><td>No employee found for the above selected criteria</td></tr>"
+			log = "<table><tr><td colspan = 2>No employee found for the above selected criteria</td></tr>"
 			
 		for emp in emp_list:
 			if not sql("""select name from `tabSalary Slip` 
@@ -119,9 +119,17 @@
 		ss_list = self.get_sal_slip_list()
 		log = ""
 		if ss_list:
-			log = "<table><tr><td>Following Salary Slip has been submitted: </td></tr><tr><td><u>SAL SLIP ID</u></td><td><u>EMPLOYEE NAME</u></td></tr>"
+			log = 	"""<table>
+						<tr>
+							<td colspan = 2>Following Salary Slip has been submitted: </td>
+						</tr>
+						<tr>
+							<td><u>SAL SLIP ID</u></td>
+							<td><u>EMPLOYEE NAME</u></td>
+						</tr>
+					"""
 		else:
-			log = "<table><tr><td>No salary slip found to submit for the above selected criteria</td></tr>"
+			log = "<table><tr><td colspan = 2>No salary slip found to submit for the above selected criteria</td></tr>"
 			
 		for ss in ss_list:
 			ss_obj = get_obj("Salary Slip",ss[0],with_children=1)
diff --git a/hr/doctype/salary_manager/test_salary_manager.py b/hr/doctype/salary_manager/test_salary_manager.py
index 1a0b48a..e3df804 100644
--- a/hr/doctype/salary_manager/test_salary_manager.py
+++ b/hr/doctype/salary_manager/test_salary_manager.py
@@ -57,7 +57,7 @@
 		self.assertTrue(ss[0][0]==67)
 
 	def test_submit(self):
-		self.sm.submit_sal_slip()
+		self.sm.submit_salary_slip()
 		ss = webnotes.conn.sql("""
 			select docstatus 
 			from `tabSalary Slip` 
@@ -112,7 +112,8 @@
 	'grade':'grade1',
 	'naming_series':'EMP/',
 	'status':'Active',
-	'docstatus':0
+	'docstatus':0,
+	'employee_name':'emp1'
 })
 
 emp2 = Document(fielddata={
@@ -135,7 +136,8 @@
 		'employee':'emp1',
 		'is_active':'Yes',
 		'department': 'dep1',
-		'designation' : 'des1'
+		'designation' : 'des1',
+		'employee_name': 'emp1'
 	}),
 	Document(fielddata={
 		'parenttype':'Salary Structure',
diff --git a/hr/doctype/salary_slip/salary_slip.py b/hr/doctype/salary_slip/salary_slip.py
index 0cdc898..6a17050 100644
--- a/hr/doctype/salary_slip/salary_slip.py
+++ b/hr/doctype/salary_slip/salary_slip.py
@@ -175,9 +175,9 @@
 		emailid_ret=sql("select company_email from `tabEmployee` where name = '%s'"%self.doc.employee)
 		if emailid_ret:
 			receiver = cstr(emailid_ret[0][0]) 
-			subj = 'Salary Slip ' + cstr(self.doc.month) +' '+cstr(self.doc.year)
-			earn_ret=sql("select e_type,e_amount from `tabSS Earning Detail` where parent = '%s'"%self.doc.name)
-			ded_ret=sql("select d_type,d_amount from `tabSS Deduction Detail` where parent = '%s'"%self.doc.name)
+			subj = 'Salary Slip - ' + cstr(self.doc.month) +'/'+cstr(self.doc.fiscal_year)
+			earn_ret=sql("select e_type,e_modified_amount from `tabSS Earning Detail` where parent = '%s'"%self.doc.name)
+			ded_ret=sql("select d_type,d_modified_amount from `tabSS Deduction Detail` where parent = '%s'"%self.doc.name)
 		 
 			earn_table = ''
 			ded_table = ''
@@ -207,62 +207,60 @@
 			if not letter_head:
 				letter_head = ''
 			
-			msg = ''' %s <br>
+			msg = '''<div> %s <br>
+			<table cellspacing= "5" cellpadding="5"  width = "100%%">
+				<tr>
+					<td colspan = 4 width = "100%%"><h4>Salary Slip</h4></td>
+				</tr>
+				<tr>
+					<td colspan = 2 width = "50%%"><b>Employee Code : %s</b></td>
+					<td colspan = 2 width = "50%%"><b>Employee Name : %s</b></td>
+				</tr>
+				<tr>
+					<td colspan = 2 width = "50%%">Month : %s</td>
+					<td colspan = 2 width = "50%%">Fiscal Year : %s</td>
+				</tr>
+			</table>
 			<table cellspacing= "5" cellpadding="5" >
-			<tr>
-				<td colspan = 4><h4>Salary Slip</h4></td>
-			</tr>
-			<tr>
-				<td colspan = 2><b>Employee Code : %s</b></td>
-				<td colspan = 2><b>Employee Name : %s</b></td>
-			</tr>
-			<tr>
-				<td>Month : %s</td>
-				<td>Year : %s</td>
-				<td colspan = 2>Fiscal Year : %s</td>
-			</tr>
-			<tr>
-				<td>Department : %s</td>
-				<td>Branch : %s</td>
-				<td colspan = 2>Designation : %s</td>
+				<tr>
+					<td>Department : %s</td>
+					<td>Branch : %s</td>
+					<td colspan = 2>Designation : %s</td>
+				</tr>
+				<tr>
+					<td>Grade : %s</td>
+					<td>Bank Account No. : %s</td>
+					<td colspan = 2>Bank Name : %s</td>
 				
-			</tr>
-			<tr>
-				<td>Grade : %s</td>
-				<td>Bank Account No. : %s</td>
-				<td colspan = 2>Bank Name : %s</td>
+				</tr>
+				<tr>
+					<td colspan = 2>Arrear Amount : <b>%s</b></td>
+					<td colspan = 2>Payment days : %s</td>
 				
-			</tr>
-			<tr>
-				<td>PF No. : %s</td>
-				<td>ESIC No. : %s</td>
-				<td colspan = 2>Arrear Amount : <b>%s</b></td>
-			</tr>
-			<tr>
-				<td>Total days in month : %s</td>
-				<td>Leave Without Pay : %s</td>
-				<td colspan = 2>Payment days : %s</td>
-				
-			</tr>
-			<br><br>
-			<tr>
-				<td colspan = 2><b>Earning</b></td>
-				<td colspan = 2><b>Deduction</b></td>
-			</tr>
-			<tr>
-				<td colspan = 2>%s</td>
-				<td colspan = 2>%s</td>
-			</tr>
-			<br>
-			<tr>
-				<td colspan = 2><b>Gross Pay :</b> %s</td>
-				<td colspan = 2><b>Total Deduction :</b> %s</td>
-			</tr>
-			<tr>
-				<td><b>Net Pay : %s</b></td>
-				<td colspan = 3><b>Net Pay (in words) : %s</b></td>
-			</tr>
-			</table>'''%(cstr(letter_head[0][0]),cstr(self.doc.employee),self.doc.employee_name,cstr(self.doc.month),cstr(self.doc.year),cstr(self.doc.fiscal_year),self.doc.department,self.doc.branch,self.doc.designation,self.doc.grade,cstr(self.doc.bank_account_no),self.doc.bank_name,cstr(self.doc.pf_no),cstr(self.doc.esic_no),cstr(self.doc.arrear_amount),cstr(self.doc.total_days_in_month),cstr(self.doc.leave_without_pay),cstr(self.doc.payment_days),earn_table,ded_table,cstr(self.doc.gross_pay),cstr(self.doc.total_deduction),cstr(self.doc.net_pay),cstr(self.doc.net_pay_in_words))
-			sendmail([receiver], sender='automail@webnotestech.com', subject=subj, parts=[['text/plain', msg]])
+				</tr>
+			</table>
+			<table border="1px solid #CCC" width="100%%" cellpadding="0" cellspacing= "0" >
+				<tr>
+					<td colspan = 2 width = "50%%"><b>Earning</b></td>
+					<td colspan = 2 width = "50%%"><b>Deduction</b></td>
+				</tr>
+				<tr>
+					<td colspan = 2 width = "50%%">%s</td>
+					<td colspan = 2 width = "50%%">%s</td>
+				</tr>
+			</table>
+			<table cellspacing= "5" cellpadding="5">
+				<tr>
+					<td colspan = 2><b>Gross Pay :</b> %s</td>
+					<td colspan = 2><b>Total Deduction :</b> %s</td>
+				</tr>
+				<tr>
+					<td><b>Net Pay : %s</b></td>
+				</tr>
+				<tr>
+					<td><b>Net Pay(in words) : %s</b></td>
+				</tr>
+			</table></div>'''%(cstr(letter_head[0][0]),cstr(self.doc.employee), cstr(self.doc.employee_name), cstr(self.doc.month), cstr(self.doc.fiscal_year), cstr(self.doc.department), cstr(self.doc.branch), cstr(self.doc.designation), cstr(self.doc.grade), cstr(self.doc.bank_account_no), cstr(self.doc.bank_name), cstr(self.doc.arrear_amount), cstr(self.doc.payment_days), earn_table, ded_table, cstr(self.doc.gross_pay), cstr(self.doc.total_deduction), cstr(self.doc.net_pay), cstr(self.doc.total_in_words))
+			sendmail([receiver], sender='automail@erpnext.com', subject=subj, parts=[['text/plain', msg]])
 		else:
 			msgprint("Company Email ID not found.")
diff --git a/patches/patch.py b/patches/patch.py
index dcd019b..616be63 100644
--- a/patches/patch.py
+++ b/patches/patch.py
@@ -1,7 +1,7 @@
 # REMEMBER to update this
 # ========================
 
-last_patch = 344
+last_patch = 346
 #-------------------------------------------
 
 def execute(patch_no):
@@ -257,4 +257,4 @@
 		delete_doc('DocType', 'Profile')
 		reload_doc('core', 'doctype', 'profile')
 		
-		
\ No newline at end of file
+