// Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
//
// MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
wn.provide("wn.report_dump");
$.extend(wn.report_dump, {
data: {},
with_data: function(doctypes, callback, progress_bar) {
var missing = [];
$.each(doctypes, function(i, v) {
if(!wn.report_dump.data[v]) missing.push(v);
})
if(missing.length) {
wn.call({
method: "webnotes.widgets.report_dump.get_data",
args: {
doctypes: doctypes,
missing: missing
},
callback: function(r) {
// creating map of data from a list
$.each(r.message, function(doctype, doctype_data) {
var data = [];
$.each(doctype_data.data, function(i, d) {
var row = {};
$.each(doctype_data.columns, function(idx, col) {
row[col] = d[idx];
});
row.id = row.name || doctype + "-" + i
data.push(row);
});
wn.report_dump.data[doctype] = data;
});
// reverse map names
$.each(r.message, function(doctype, doctype_data) {
if(doctype_data.links) {
$.each(wn.report_dump.data[doctype], function(row_idx, row) {
$.each(doctype_data.links, function(link_key, link) {
if(wn.report_dump.data[link[0]][row[link_key]]) {
row[link_key] = wn.report_dump.data[link[0]][row[link_key]][link[1]];
} else {
row[link_key] = null;
}
})
})
}
});
callback();
},
progress_bar: progress_bar
})
} else {
callback();
}
}
});
wn.provide("wn.views");
wn.views.GridReport = Class.extend({
init: function(opts) {
this.filter_inputs = {};
this.preset_checks = [];
this.tree_grid = {show: false};
$.extend(this, opts);
this.wrapper = $('
').appendTo(this.parent);
if(this.filters) {
this.make_filters();
}
this.make_waiting();
this.import_slickgrid();
var me = this;
this.get_data();
},
bind_show: function() {
// bind show event to reset cur_report_grid
// and refresh filters from url
// this must be called after init
// because "wn.container.page" will only be set
// once "load" event is over.
var me = this;
$(this.page).bind('show', function() {
// reapply filters on show
wn.cur_grid_report = me;
me.apply_filters_from_route();
me.refresh();
});
},
get_data: function() {
var me = this;
wn.report_dump.with_data(this.doctypes, function() {
// setup filters
me.setup_filters();
me.init_filter_values();
me.refresh();
}, this.wrapper.find(".progress .bar"));
},
setup_filters: function() {
var me = this;
$.each(me.filter_inputs, function(i, v) {
var opts = v.get(0).opts;
if (opts.fieldtype == "Select" && inList(me.doctypes, opts.link)) {
$(v).add_options($.map(wn.report_dump.data[opts.link], function(d) {
return d.name;
}));
}
});
// refresh
this.filter_inputs.refresh && this.filter_inputs.refresh.click(function() {
me.set_route();
});
// reset filters
this.filter_inputs.reset_filters && this.filter_inputs.reset_filters.click(function() {
me.init_filter_values();
me.set_route();
});
},
init_filter_values: function() {
var me = this;
$.each(this.filter_inputs, function(key, filter) {
var opts = filter.get(0).opts;
if(sys_defaults[key]) {
filter.val(sys_defaults[key]);
} else if(opts.fieldtype=='Select') {
filter.get(0).selectedIndex = 0;
} else if(opts.fieldtype=='Data') {
filter.val("");
}
})
if(this.filter_inputs.from_date)
this.filter_inputs.from_date.val(dateutil.str_to_user(sys_defaults.year_start_date));
if(this.filter_inputs.to_date)
this.filter_inputs.to_date.val(dateutil.str_to_user(sys_defaults.year_end_date));
},
make_filters: function() {
var me = this;
$.each(this.filters, function(i, v) {
v.fieldname = v.fieldname || v.label.replace(/ /g, '_').toLowerCase();
var input = null;
if(v.fieldtype=='Select') {
input = me.appframe.add_select(v.label, v.options || [v.default_value]);
} else if(v.fieldtype=='Button') {
input = me.appframe.add_button(v.label);
if(v.icon) {
$('').prependTo(input);
}
} else if(v.fieldtype=='Date') {
input = me.appframe.add_date(v.label);
} else if(v.fieldtype=='Label') {
input = me.appframe.add_label(v.label);
} else if(v.fieldtype=='Data') {
input = me.appframe.add_data(v.label);
}
if(input) {
input && (input.get(0).opts = v);
if(v.cssClass) {
input.addClass(v.cssClass);
}
input.keypress(function(e) {
if(e.which==13) {
me.set_route();
}
})
}
me.filter_inputs[v.fieldname] = input;
});
},
make_waiting: function() {
this.waiting = $('