added dashboard

This commit is contained in:
Rushabh Mehta 2011-08-25 19:17:44 +05:30
parent 9e6e7fa0ef
commit 29a75c4d7a
6 changed files with 424 additions and 0 deletions

View File

View File

@ -0,0 +1,12 @@
div.dashboard_table td {
width: 50%;
}
div.dashboard-title {
font-weight: bold;
padding: '3px 0px';
}
div.dashboard-graph {
height: 180px;
}

View File

@ -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>

View File

@ -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();
}
})
}

View File

@ -0,0 +1,216 @@
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):
"""
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
""", (acc=='Income' and 'Credit' or 'Debit', start, end))[0]
return acc=='Income' and (ret[1]-ret[0]) or (ret[0]-ret[1])
def value(self, opts, start, end):
"""
Value of the series on a particular date
"""
import webnotes
if opts['type']=='account':
bal = self.get_account_amt(opts['account'], start, end)
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 type=='Credit':
return sum([self.get_account_balance(acc, start)[1] for acc in self.cash_accounts]) or 0
if type=='Debit':
return sum([self.get_account_balance(acc, start)[0] 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"
}""")

View File

@ -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'
}
]