fix: Dashboard Chart to support Child Table Doctype. (#13995)

* fix: Dashboard Chart with child table doctype

* test:  UI test 

* fix: Made Parent Document Type visible if Document Type is set
This commit is contained in:
Shariq Ansari 2021-11-11 15:09:58 +05:30 committed by GitHub
parent 20e3ed9ff9
commit d27d880402
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 100 additions and 8 deletions

View file

@ -0,0 +1,22 @@
context('Dashboard Chart', () => {
before(() => {
cy.login();
cy.visit('/app/website');
});
it('Check filter populate for child table doctype', () => {
cy.visit('/app/dashboard-chart/new-dashboard-chart-1');
cy.get('[data-fieldname="parent_document_type"]').should('have.css', 'display', 'none');
cy.get_field('document_type', 'Link');
cy.fill_field('document_type', 'Workspace Link', 'Link').focus().blur();
cy.get_field('document_type', 'Link').should('have.value', 'Workspace Link');
cy.fill_field('chart_name', 'Test Chart', 'Data');
cy.get('[data-fieldname="filters_json"]').click().wait(200);
cy.get('.modal-body .filter-action-buttons .add-filter').click();
cy.get('.modal-body .fieldname-select-area').click();
cy.get('.modal-actions .btn-modal-close').click();
});
});

View file

@ -45,6 +45,7 @@ frappe.ui.form.on('Dashboard Chart', {
frm.set_df_property("filters_section", "hidden", 1);
frm.set_df_property("dynamic_filters_section", "hidden", 1);
frm.trigger('set_parent_document_type');
frm.trigger('set_time_series');
frm.set_query('document_type', function() {
return {
@ -110,9 +111,11 @@ frappe.ui.form.on('Dashboard Chart', {
frm.set_value('source', '');
frm.set_value('based_on', '');
frm.set_value('value_based_on', '');
frm.set_value('parent_document_type', '');
frm.set_value('filters_json', '[]');
frm.set_value('dynamic_filters_json', '[]');
frm.trigger('update_options');
frm.trigger('set_parent_document_type');
},
report_name: function(frm) {
@ -125,7 +128,6 @@ frappe.ui.form.on('Dashboard Chart', {
frm.trigger('set_chart_report_filters');
},
set_chart_report_filters: function(frm) {
let report_name = frm.doc.report_name;
@ -148,6 +150,10 @@ frappe.ui.form.on('Dashboard Chart', {
}
},
use_report_chart: function(frm) {
!frm.doc.use_report_chart && frm.trigger('set_chart_field_options');
},
set_chart_field_options: function(frm) {
let filters = frm.doc.filters_json.length > 2 ? JSON.parse(frm.doc.filters_json) : null;
if (frm.doc.dynamic_filters_json && frm.doc.dynamic_filters_json.length > 2) {
@ -179,6 +185,9 @@ frappe.ui.form.on('Dashboard Chart', {
} else {
frappe.msgprint(__('Report has no data, please modify the filters or change the Report Name'));
}
} else {
frm.set_value('use_report_chart', 1);
frm.set_df_property('use_report_chart', 'hidden', false);
}
});
},
@ -365,6 +374,7 @@ frappe.ui.form.on('Dashboard Chart', {
frm.filter_group = new frappe.ui.FilterGroup({
parent: dialog.get_field('filter_area').$wrapper,
doctype: frm.doc.document_type,
parent_doctype: frm.doc.parent_document_type,
on_change: () => {},
});
@ -481,6 +491,36 @@ frappe.ui.form.on('Dashboard Chart', {
frm.dynamic_filter_table.find('tbody').html(filter_rows);
}
},
set_parent_document_type: async function(frm) {
let document_type = frm.doc.document_type;
let doc_is_table = document_type &&
(await frappe.db.get_value('DocType', document_type, 'istable')).message.istable;
frm.set_df_property('parent_document_type', 'hidden', !doc_is_table);
if (document_type && doc_is_table) {
let parent = await frappe.db.get_list('DocField', {
filters: {
'fieldtype': 'Table',
'options': document_type
},
fields: ['parent']
});
parent && frm.set_query('parent_document_type', function() {
return {
filters: {
"name": ['in', parent.map(({ parent }) => parent)]
}
};
});
if (parent.length === 1) {
frm.set_value('parent_document_type', parent[0].parent);
}
}
}
});

View file

@ -17,6 +17,7 @@
"y_axis",
"source",
"document_type",
"parent_document_type",
"based_on",
"value_based_on",
"group_by_type",
@ -268,10 +269,18 @@
"fieldname": "use_report_chart",
"fieldtype": "Check",
"label": "Use Report Chart"
},
{
"depends_on": "eval: doc.chart_type !== 'Custom' && doc.chart_type !== 'Report'",
"description": "The document type selected is a child table, so the parent document type is required.",
"fieldname": "parent_document_type",
"fieldtype": "Link",
"label": "Parent Document Type",
"options": "DocType"
}
],
"links": [],
"modified": "2020-07-23 11:10:33.509497",
"modified": "2021-11-09 17:18:11.456145",
"modified_by": "Administrator",
"module": "Desk",
"name": "Dashboard Chart",

View file

@ -333,7 +333,10 @@ class DashboardChart(Document):
def check_required_field(self):
if not self.document_type:
frappe.throw(_("Document type is required to create a dashboard chart"))
frappe.throw(_("Document type is required to create a dashboard chart"))
if self.document_type and frappe.get_meta(self.document_type).istable and not self.parent_document_type:
frappe.throw(_("Parent document type is required to create a dashboard chart"))
if self.chart_type == 'Group By':
if not self.group_by_based_on:

View file

@ -112,8 +112,11 @@ frappe.ui.FieldSelect = class FieldSelect {
// main table
var main_table_fields = std_filters.concat(frappe.meta.docfield_list[me.doctype]);
$.each(frappe.utils.sort(main_table_fields, "label", "string"), function(i, df) {
let doctype = frappe.get_meta(me.doctype).istable && me.parent_doctype ?
me.parent_doctype : me.doctype;
// show fields where user has read access and if report hide flag is not set
if(frappe.perm.has_perm(me.doctype, df.permlevel, "read"))
if (frappe.perm.has_perm(doctype, df.permlevel, "read"))
me.add_field_option(df);
});
@ -129,8 +132,11 @@ frappe.ui.FieldSelect = class FieldSelect {
}
$.each(frappe.utils.sort(child_table_fields, "label", "string"), function(i, df) {
let doctype = frappe.get_meta(me.doctype).istable && me.parent_doctype ?
me.parent_doctype : me.doctype;
// show fields where user has read access and if report hide flag is not set
if(frappe.perm.has_perm(me.doctype, df.permlevel, "read"))
if (frappe.perm.has_perm(doctype, df.permlevel, "read"))
me.add_field_option(df);
});
}

View file

@ -80,6 +80,7 @@ frappe.ui.Filter = class {
this.fieldselect = new frappe.ui.FieldSelect({
parent: this.filter_edit_area.find('.fieldname-select-area'),
doctype: this.parent_doctype,
parent_doctype: this._parent_doctype,
filter_fields: this.filter_fields,
input_class: 'input-xs',
select: (doctype, fieldname) => {

View file

@ -201,6 +201,7 @@ frappe.ui.FilterGroup = class {
parent: this.wrapper,
parent_doctype: this.doctype,
doctype: doctype,
_parent_doctype: this.parent_doctype,
fieldname: fieldname,
condition: condition,
value: value,

View file

@ -1369,5 +1369,12 @@ Object.assign(frappe.utils, {
sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
},
parse_array(array) {
if (array && array.length !== 0) {
return array;
}
return undefined;
}
});

View file

@ -121,7 +121,7 @@ frappe.report_utils = {
).then(r => {
frappe.dom.eval(r.script || '');
return frappe.after_ajax(() => {
return frappe.query_reports[report_name].filters;
return frappe.query_reports[report_name] && frappe.query_reports[report_name].filters;
})
});
},

View file

@ -455,6 +455,7 @@ export default class ChartWidget extends Widget {
create_filter_group_and_add_filters() {
this.filter_group = new frappe.ui.FilterGroup({
doctype: this.chart_doc.document_type,
parent_doctype: this.chart_doc.parent_document_type,
filter_button: this.filter_button,
on_change: () => {
this.filters = this.filter_group.get_filters();
@ -697,11 +698,13 @@ export default class ChartWidget extends Widget {
.get_filters_for_chart_type(this.chart_doc).then(filters => {
chart_saved_filters = this.update_default_date_filters(filters, chart_saved_filters);
this.filters =
user_saved_filters || this.filters || chart_saved_filters;
frappe.utils.parse_array(user_saved_filters) || frappe.utils.parse_array(this.filters)
|| frappe.utils.parse_array(chart_saved_filters);
});
} else {
this.filters =
user_saved_filters || this.filters || chart_saved_filters;
frappe.utils.parse_array(user_saved_filters) || frappe.utils.parse_array(this.filters)
|| frappe.utils.parse_array(chart_saved_filters);
return Promise.resolve();
}
}