', {
_style: dataContext._style || "",
value: ((value==null || value==="") ? "" : format_number(value))
});
},
text_formatter: function(row, cell, value, columnDef, dataContext) {
return repl('%(value)s', {
_style: dataContext._style || "",
esc_value: cstr(value).replace(/"/g, '\"'),
value: cstr(value)
});
},
check_formatter: function(row, cell, value, columnDef, dataContext) {
return repl('', {
"id": dataContext.id,
"checked": dataContext.checked ? 'checked="checked"' : ""
})
},
apply_link_formatters: function() {
var me = this;
$.each(this.dataview_columns, function(i, col) {
if(col.link_formatter) {
col.formatter = function(row, cell, value, columnDef, dataContext) {
// added link and open button to links
// link_formatter must have
// filter_input, open_btn (true / false), doctype (will be eval'd)
if(!value) return "";
var me = wn.cur_grid_report;
if(dataContext._show) {
return repl('%(value)s', {
_style: dataContext._style || "",
value: value
});
}
// make link to add a filter
var link_formatter = me.dataview_columns[cell].link_formatter;
if (link_formatter.filter_input) {
var html = repl('\
%(value)s', {
value: value,
col_name: link_formatter.filter_input,
page_name: wn.container.page.page_name
});
} else {
var html = value;
}
// make icon to open form
if(link_formatter.open_btn) {
var doctype = link_formatter.doctype
? eval(link_formatter.doctype)
: dataContext.doctype;
html += me.get_link_open_icon(doctype, value);
}
return html;
}
}
})
},
get_link_open_icon: function(doctype, name) {
return repl(' \
', {
doctype: doctype,
name: encodeURIComponent(name)
});
},
make_date_range_columns: function() {
this.columns = [];
var me = this;
var range = this.filter_inputs.range.val();
this.from_date = dateutil.user_to_str(this.filter_inputs.from_date.val());
this.to_date = dateutil.user_to_str(this.filter_inputs.to_date.val());
var date_diff = dateutil.get_diff(this.to_date, this.from_date);
me.column_map = {};
var add_column = function(date) {
me.columns.push({
id: date,
name: dateutil.str_to_user(date),
field: date,
formatter: me.currency_formatter,
width: 100
});
}
var build_columns = function(condition) {
// add column for each date range
for(var i=0; i <= date_diff; i++) {
var date = dateutil.add_days(me.from_date, i);
if(!condition) condition = function() { return true; }
if(condition(date)) add_column(date);
me.last_date = date;
if(me.columns.length) {
me.column_map[date] = me.columns[me.columns.length-1];
}
}
}
// make columns for all date ranges
if(range=='Daily') {
build_columns();
} else if(range=='Weekly') {
build_columns(function(date) {
if(!me.last_date) return true;
return !(dateutil.get_diff(date, me.from_date) % 7)
});
} else if(range=='Monthly') {
build_columns(function(date) {
if(!me.last_date) return true;
return dateutil.str_to_obj(me.last_date).getMonth() != dateutil.str_to_obj(date).getMonth()
});
} else if(range=='Quarterly') {
build_columns(function(date) {
if(!me.last_date) return true;
return dateutil.str_to_obj(date).getDate()==1 && in_list([0,3,6,9], dateutil.str_to_obj(date).getMonth())
});
} else if(range=='Yearly') {
build_columns(function(date) {
if(!me.last_date) return true;
return $.map(wn.report_dump.data['Fiscal Year'], function(v) {
return date==v.year_start_date ? true : null;
}).length;
});
}
// set label as last date of period
$.each(this.columns, function(i, col) {
col.name = me.columns[i+1]
? dateutil.str_to_user(dateutil.add_days(me.columns[i+1].id, -1))
: dateutil.str_to_user(me.to_date);
});
},
trigger_refresh_on_change: function(filters) {
var me = this;
$.each(filters, function(i, f) {
me.filter_inputs[f] && me.filter_inputs[f].on("change", function() {
me.refresh();
});
});
}
});
wn.views.GridReportWithPlot = wn.views.GridReport.extend({
render_plot: function() {
var plot_data = this.get_plot_data ? this.get_plot_data() : null;
if(!plot_data) {
this.plot_area.toggle(false);
return;
}
wn.require('assets/webnotes/js/lib/flot/jquery.flot.js');
wn.require('assets/webnotes/js/lib/flot/jquery.flot.downsample.js');
this.plot = $.plot(this.plot_area.toggle(true), plot_data,
this.get_plot_options());
this.setup_plot_hover();
},
setup_plot_check: function() {
var me = this;
me.wrapper.bind('make', function() {
me.wrapper.on("click", ".plot-check", function() {
var checked = $(this).prop("checked");
var id = $(this).attr("data-id");
if(me.item_by_name) {
if(me.item_by_name[id]) {
me.item_by_name[id].checked = checked ? true : false;
}
} else {
$.each(me.data, function(i, d) {
if(d.id==id) d.checked = checked;
});
}
me.render_plot();
});
});
},
setup_plot_hover: function() {
var me = this;
this.tooltip_id = wn.dom.set_unique_id();
function showTooltip(x, y, contents) {
$('
' + contents + '
').css( {
position: 'absolute',
display: 'none',
top: y + 5,
left: x + 5,
border: '1px solid #fdd',
padding: '2px',
'background-color': '#fee',
opacity: 0.80
}).appendTo("body").fadeIn(200);
}
this.previousPoint = null;
this.wrapper.find('.plot').bind("plothover", function (event, pos, item) {
if (item) {
if (me.previousPoint != item.dataIndex) {
me.previousPoint = item.dataIndex;
$("#" + me.tooltip_id).remove();
showTooltip(item.pageX, item.pageY,
me.get_tooltip_text(item.series.label, item.datapoint[0], item.datapoint[1]));
}
}
else {
$("#" + me.tooltip_id).remove();
me.previousPoint = null;
}
});
},
get_tooltip_text: function(label, x, y) {
var date = dateutil.obj_to_user(new Date(x));
var value = format_number(y);
return value + " on " + date;
},
get_plot_data: function() {
var data = [];
var me = this;
$.each(this.data, function(i, item) {
if (item.checked) {
data.push({
label: item.name,
data: $.map(me.columns, function(col, idx) {
if(col.formatter==me.currency_formatter && !col.hidden && col.plot!==false) {
return me.get_plot_points(item, col, idx)
}
}),
points: {show: true},
lines: {show: true, fill: true},
});
// prepend opening
data[data.length-1].data = [[dateutil.str_to_obj(me.from_date).getTime(),
item.opening]].concat(data[data.length-1].data);
}
});
return data.length ? data : false;
},
get_plot_options: function() {
return {
grid: { hoverable: true, clickable: true },
xaxis: { mode: "time",
min: dateutil.str_to_obj(this.from_date).getTime(),
max: dateutil.str_to_obj(this.to_date).getTime() },
series: { downsample: { threshold: 1000 } }
}
}
});
wn.views.TreeGridReport = wn.views.GridReportWithPlot.extend({
make_transaction_list: function(parent_doctype, doctype) {
var me = this;
var tmap = {};
$.each(wn.report_dump.data[doctype], function(i, v) {
if(!tmap[v.parent]) tmap[v.parent] = [];
tmap[v.parent].push(v);
});
if (!this.tl) this.tl = {};
if (!this.tl[parent_doctype]) this.tl[parent_doctype] = [];
$.each(wn.report_dump.data[parent_doctype], function(i, parent) {
if(tmap[parent.name]) {
$.each(tmap[parent.name], function(i, d) {
me.tl[parent_doctype].push($.extend(copy_dict(parent), d));
});
}
});
},
add_tree_grid_events: function() {
var me = this;
this.grid.onClick.subscribe(function (e, args) {
if ($(e.target).hasClass("toggle")) {
var item = me.dataView.getItem(args.row);
if (item) {
if (!item._collapsed) {
item._collapsed = true;
} else {
item._collapsed = false;
}
me.dataView.updateItem(item.id, item);
}
e.stopImmediatePropagation();
}
});
},
tree_formatter: function (row, cell, value, columnDef, dataContext) {
var me = wn.cur_grid_report;
value = value.replace(/&/g,"&").replace(//g,">");
var data = me.data;
var spacer = "";
var idx = me.dataView.getIdxById(dataContext.id);
var link = me.tree_grid.formatter(dataContext);
if(dataContext.doctype) {
link += me.get_link_open_icon(dataContext.doctype, dataContext.name);
}
if (data[idx + 1] && data[idx + 1].indent > data[idx].indent) {
if (dataContext._collapsed) {
return spacer + " " + link;
} else {
return spacer + " " + link;
}
} else {
return spacer + " " + link;
}
},
tree_dataview_filter: function(item) {
var me = wn.cur_grid_report;
if(!me.apply_filters(item)) return false;
var parent = item[me.tree_grid.parent_field];
while (parent) {
if (me.item_by_name[parent]._collapsed) {
return false;
}
parent = me.parent_map[parent];
}
return true;
},
prepare_tree: function(item_dt, group_dt) {
var group_data = wn.report_dump.data[group_dt];
var item_data = wn.report_dump.data[item_dt];
// prepare map with child in respective group
var me = this;
var item_group_map = {};
var group_ids = $.map(group_data, function(v) { return v.id; });
$.each(item_data, function(i, item) {
var parent = item[me.tree_grid.parent_field];
if(!item_group_map[parent]) item_group_map[parent] = [];
if(group_ids.indexOf(item.name)==-1) {
item_group_map[parent].push(item);
} else {
msgprint("Ignoring Item "+ item.name.bold() +
", because a group exists with the same name!");
}
});
// arrange items besides their parent item groups
var items = [];
$.each(group_data, function(i, group){
group.is_group = true;
items.push(group);
items = items.concat(item_group_map[group.name] || []);
});
return items;
},
set_indent: function() {
var me = this;
$.each(this.data, function(i, d) {
var indent = 0;
var parent = me.parent_map[d.name];
if(parent) {
while(parent) {
indent++;
parent = me.parent_map[parent];
}
}
d.indent = indent;
});
},
export: function() {
var msgbox = msgprint('
Select To Download:
\
With Groups
\
With Ledgers
\
');
var me = this;
$(msgbox.body).find("button").click(function() {
var with_groups = $(msgbox.body).find("[name='with_groups']").prop("checked");
var with_ledgers = $(msgbox.body).find("[name='with_ledgers']").prop("checked");
var data = wn.slickgrid_tools.get_view_data(me.columns, me.dataView,
function(row, item) {
if(with_groups) {
// add row
for(var i=0; i