Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 1 | // ERPNext - web based ERP (http://erpnext.com) |
| 2 | // Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 17 | pscript.onload_dashboard = function() { |
| 18 | // load jqplot |
Rushabh Mehta | eec8e92 | 2012-02-14 17:25:31 +0530 | [diff] [blame] | 19 | wn.require('lib/css/jqplot.css'); |
Rushabh Mehta | 5caedff | 2011-09-08 15:45:37 +0530 | [diff] [blame] | 20 | wn.require('lib/js/legacy/jquery/jquery.jqplot.min.js'); |
| 21 | wn.require('lib/js/legacy/jquery/jqplot-plugins/jqplot.barRenderer.js'); |
| 22 | wn.require('lib/js/legacy/jquery/jqplot-plugins/jqplot.canvasAxisTickRenderer.min.js'); |
| 23 | wn.require('lib/js/legacy/jquery/jqplot-plugins/jqplot.canvasTextRenderer.min.js'); |
| 24 | wn.require('lib/js/legacy/jquery/jqplot-plugins/jqplot.categoryAxisRenderer.min.js'); |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 25 | |
| 26 | |
| 27 | pscript.dashboard_settings = { |
| 28 | company: sys_defaults.company, |
Anand Doshi | 130ae98 | 2011-12-12 20:24:06 +0530 | [diff] [blame] | 29 | start: (function() { |
| 30 | var start_date = dateutil.add_days(new Date(), -180); |
| 31 | var year_start_date = dateutil.str_to_obj(sys_defaults.year_start_date); |
| 32 | if (start_date < year_start_date) { start_date = year_start_date; } |
| 33 | console.log(start_date); |
| 34 | return dateutil.obj_to_str(start_date); |
| 35 | })(), |
| 36 | end: (function() { |
| 37 | var end_date = new Date(); |
| 38 | var year_end_date = dateutil.str_to_obj(sys_defaults.year_end_date); |
| 39 | if (end_date > year_end_date) { end_date = year_end_date; } |
| 40 | console.log(end_date); |
| 41 | return dateutil.obj_to_str(end_date); |
| 42 | })(), |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 43 | interval: 30 |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 44 | } |
| 45 | |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 46 | var ph = new PageHeader($('.dashboard .header').get(0), 'Dashboard'); |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 47 | var db = new Dashboard(); |
| 48 | |
| 49 | ph.add_button('Settings', db.show_settings); |
| 50 | |
| 51 | db.refresh(); |
| 52 | |
| 53 | } |
| 54 | |
| 55 | Dashboard = function() { |
| 56 | var me = this; |
| 57 | $.extend(me, { |
| 58 | refresh: function() { |
| 59 | $('.dashboard .help_box').css('display', 'block'); |
| 60 | $c_page('home', 'dashboard', 'load_dashboard', JSON.stringify(pscript.dashboard_settings), function(r,rt) { |
| 61 | $('.dashboard .help_box').css('display', 'none'); |
| 62 | me.render(r.message); |
| 63 | }) |
| 64 | }, |
| 65 | |
| 66 | render: function(data) { |
| 67 | $('.dashboard_table').html(''); |
| 68 | var t = make_table($('.dashboard_table').get(0), 4, 2, '100%', ['50%', '50%'], {padding: '5px'}); |
| 69 | var ridx=0; var cidx=0; |
| 70 | for(var i=0; i< data.length; i++) { |
| 71 | // switch columns and rows |
| 72 | if(cidx==2) { cidx=0; ridx++} |
| 73 | |
| 74 | // give an id! |
| 75 | var cell = $td(t,ridx,cidx); |
| 76 | var title = $a(cell, 'div', 'dashboard-title', '', data[i][0].title); |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 77 | var parent = $a(cell, 'div', 'dashboard-graph'); |
| 78 | if(data[i][0].comment); |
| 79 | var comment = $a(cell, 'div', 'comment', '', data[i][0].comment) |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 80 | |
| 81 | parent.id = '_dashboard' + ridx + '-' + cidx; |
| 82 | |
| 83 | // render graph |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 84 | me.render_graph(parent.id, data[i][1], data[i][0].fillColor); |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 85 | cidx++; |
| 86 | } |
| 87 | }, |
| 88 | |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 89 | render_graph: function(parent, values, fillColor) { |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 90 | var vl = []; |
| 91 | $.each(values, function(i,v) { |
| 92 | vl.push([dateutil.str_to_user(v[0]), v[1]]); |
| 93 | }); |
| 94 | $.jqplot(parent, [vl], { |
| 95 | seriesDefaults:{ |
| 96 | renderer:$.jqplot.BarRenderer, |
| 97 | rendererOptions: {fillToZero: true}, |
| 98 | }, |
| 99 | axes: { |
| 100 | // Use a category axis on the x axis and use our custom ticks. |
| 101 | xaxis: { |
| 102 | min: 0, |
| 103 | renderer: $.jqplot.CategoryAxisRenderer, |
| 104 | tickRenderer: $.jqplot.CanvasAxisTickRenderer, |
| 105 | tickOptions: { |
| 106 | angle: -30, |
| 107 | fontSize: '8pt' |
| 108 | } |
| 109 | }, |
| 110 | // Pad the y axis just a little so bars can get close to, but |
| 111 | // not touch, the grid boundaries. 1.2 is the default padding. |
| 112 | yaxis: { |
| 113 | min: 0, |
| 114 | pad: 1.05, |
| 115 | tickOptions: {formatString: '%d'} |
| 116 | } |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 117 | }, |
| 118 | seriesColors: [fillColor] |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 119 | }); |
| 120 | }, |
| 121 | |
| 122 | show_settings: function() { |
| 123 | var d = new wn.widgets.Dialog({ |
| 124 | title: 'Set Company Settings', |
| 125 | width: 500, |
| 126 | fields: [ |
| 127 | { |
| 128 | label:'Company', |
| 129 | reqd: 1, |
| 130 | fieldname:'company', |
| 131 | fieldtype:'Link', |
| 132 | options: 'Company' |
| 133 | }, |
| 134 | { |
| 135 | label:'Start Date', |
| 136 | reqd: 1, |
| 137 | fieldname:'start', |
| 138 | fieldtype:'Date', |
| 139 | }, |
| 140 | { |
| 141 | label:'End Date', |
| 142 | reqd: 1, |
| 143 | fieldname:'end', |
| 144 | fieldtype:'Date', |
| 145 | }, |
| 146 | { |
| 147 | label:'Interval', |
| 148 | reqd: 1, |
| 149 | fieldname:'interval', |
| 150 | fieldtype:'Int' |
| 151 | }, |
| 152 | { |
| 153 | label:'Regenerate', |
| 154 | fieldname:'refresh', |
| 155 | fieldtype:'Button' |
| 156 | } |
| 157 | ] |
| 158 | }); |
| 159 | d.onshow = function() { |
| 160 | d.set_values(pscript.dashboard_settings); |
| 161 | } |
| 162 | d.fields_dict.refresh.input.onclick = function() { |
| 163 | pscript.dashboard_settings = d.get_values(); |
| 164 | me.refresh(); |
| 165 | d.hide(); |
| 166 | } |
| 167 | d.show(); |
| 168 | } |
| 169 | }) |
| 170 | } |