2018-03-26 12:18:07 +00:00
|
|
|
const Page = require('frappejs/client/view/page');
|
2018-03-27 13:55:26 +00:00
|
|
|
const Form = require('frappejs/client/view/form');
|
2018-03-26 12:18:07 +00:00
|
|
|
const DataTable = require('frappe-datatable');
|
|
|
|
const frappe = require('frappejs');
|
|
|
|
const utils = require('frappejs/client/ui/utils');
|
2018-03-27 13:55:26 +00:00
|
|
|
const Observable = require('frappejs/utils/observable');
|
2018-03-26 12:18:07 +00:00
|
|
|
|
|
|
|
// baseclass for report
|
|
|
|
// `url` url for report
|
|
|
|
// `getColumns` return columns
|
|
|
|
|
|
|
|
module.exports = class ReportPage extends Page {
|
2018-03-27 13:55:26 +00:00
|
|
|
constructor({title, filterFields}) {
|
2018-03-26 12:18:07 +00:00
|
|
|
super({title: title, hasRoute: true});
|
|
|
|
|
|
|
|
this.fullPage = true;
|
2018-03-27 13:55:26 +00:00
|
|
|
this.filterFields = filterFields;
|
2018-03-26 12:18:07 +00:00
|
|
|
|
2018-03-27 13:55:26 +00:00
|
|
|
this.filterWrapper = frappe.ui.add('div', 'filter-toolbar', this.body);
|
2018-03-26 12:18:07 +00:00
|
|
|
this.tableWrapper = frappe.ui.add('div', 'table-page-wrapper', this.body);
|
|
|
|
|
|
|
|
this.btnNew = this.addButton(frappe._('Refresh'), 'btn-primary', async () => {
|
|
|
|
await this.run();
|
|
|
|
});
|
|
|
|
|
2018-03-27 13:55:26 +00:00
|
|
|
this.makeFilters();
|
2018-03-26 12:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getColumns() {
|
|
|
|
// overrride
|
|
|
|
}
|
|
|
|
|
2018-03-27 13:55:26 +00:00
|
|
|
makeFilters() {
|
|
|
|
this.form = new Form({
|
|
|
|
parent: this.filterWrapper,
|
|
|
|
meta: { fields: this.filterFields },
|
|
|
|
doc: new Observable(),
|
|
|
|
inline: true,
|
|
|
|
container: this
|
|
|
|
});
|
2018-03-26 12:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getFilterValues() {
|
|
|
|
const values = {};
|
2018-03-27 13:55:26 +00:00
|
|
|
for (let control of this.form.controlList) {
|
|
|
|
values[control.fieldname] = control.getInputValue();
|
|
|
|
if (control.required && !values[control.fieldname]) {
|
2018-03-26 12:18:07 +00:00
|
|
|
frappe.ui.showAlert({message: frappe._('{0} is mandatory', control.label), color: 'red'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
async show(params) {
|
|
|
|
super.show();
|
|
|
|
await this.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
async run() {
|
2018-03-27 13:55:26 +00:00
|
|
|
if (frappe.params && frappe.params.filters) {
|
|
|
|
for (let key in frappe.params.filters) {
|
|
|
|
if (this.form.controls[key]) {
|
|
|
|
this.form.controls[key].setInputValue(frappe.params.filters[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
frappe.params = null;
|
|
|
|
|
|
|
|
|
2018-03-26 12:18:07 +00:00
|
|
|
if (!this.datatable) {
|
|
|
|
this.makeDataTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
const filterValues = this.getFilterValues();
|
|
|
|
if (filterValues === false) return;
|
|
|
|
|
2018-03-26 13:02:35 +00:00
|
|
|
let data = await frappe.call(this.method, filterValues);
|
2018-03-26 12:18:07 +00:00
|
|
|
this.datatable.refresh(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
makeDataTable() {
|
|
|
|
this.datatable = new DataTable(this.tableWrapper, {
|
|
|
|
columns: utils.convertFieldsToDatatableColumns(this.getColumns(), this.layout),
|
|
|
|
data: [],
|
|
|
|
layout: this.layout || 'fluid',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|