Skip to content

Commit

Permalink
refactor: filters for export on prepared report (backport #26106) (#2…
Browse files Browse the repository at this point in the history
…6116)

* fix: filters on prepared report export

(cherry picked from commit 71a9fa4)

# Conflicts:
#	frappe/public/js/frappe/views/reports/query_report.js

* refactor: Store prepared_report_name state on report obj

(cherry picked from commit b5b6a52)

# Conflicts:
#	frappe/core/doctype/prepared_report/prepared_report.js
#	frappe/public/js/frappe/views/reports/query_report.js

---------

Co-authored-by: Rutwik Hiwalkar <rutwikhdev@gmail.com>
Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
3 people committed Apr 24, 2024
1 parent cc3b654 commit 5a73fd8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
9 changes: 2 additions & 7 deletions frappe/core/doctype/prepared_report/prepared_report.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ frappe.ui.form.on("Prepared Report", {

if (frm.doc.status == "Completed") {
frm.page.set_primary_action(__("Show Report"), () => {
frappe.set_route(
"query-report",
frm.doc.report_name,
frappe.utils.make_query_string({
prepared_report_name: frm.doc.name,
})
);
frappe.route_options = { prepared_report_name: frm.doc.name };
frappe.set_route("query-report", frm.doc.report_name);
});
}
},
Expand Down
36 changes: 17 additions & 19 deletions frappe/public/js/frappe/views/reports/query_report.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,22 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
}

get_url_with_filters() {
const query_params = Object.entries(this.get_filter_values())
.map(([field, value], _idx) => {
let query_params = new URLSearchParams();
if (this.prepared_report_name) {
query_params.append("prepared_report_name", this.prepared_report_name);
} else {
Object.entries(this.get_filter_values()).map(([field, value], _idx) => {
// multiselects
if (Array.isArray(value)) {
if (!value.length) return "";
value = JSON.stringify(value);
}
return `${field}=${encodeURIComponent(value)}`;
})
.filter(Boolean)
.join("&");

query_params.append(field, value);
});
}
let full_url = window.location.href.replace(window.location.search, "");
if (query_params) {
full_url += "?" + query_params;
if (query_params.toString()) {
full_url += "?" + query_params.toString();
}
return full_url;
}
Expand Down Expand Up @@ -377,6 +378,7 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
}

refresh_report(route_options) {
this.prepared_report_name = null; // this should be set only if prepared report is EXPLICITLY requested
this.toggle_message(true);
this.toggle_report(false);

Expand Down Expand Up @@ -579,6 +581,7 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
const fields = Object.keys(route_options);

const filters_to_set = this.filters.filter((f) => fields.includes(f.df.fieldname));
this.prepared_report_name = route_options.prepared_report_name;

const promises = filters_to_set.map((f) => {
return () => {
Expand Down Expand Up @@ -630,10 +633,8 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
this.last_ajax.abort();
}

const query_params = this.get_query_params();

if (query_params.prepared_report_name) {
filters.prepared_report_name = query_params.prepared_report_name;
if (this.prepared_report_name) {
filters.prepared_report_name = this.prepared_report_name;
}

return new Promise((resolve) => {
Expand Down Expand Up @@ -669,7 +670,7 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
this.prepared_report_document = data.doc;
// If query_string contains prepared_report_name then set filters
// to match the mentioned prepared report doc and disable editing
if (query_params.prepared_report_name) {
if (this.prepared_report_name) {
this.prepared_report_action = "Edit";
const filters_from_report = JSON.parse(data.doc.filters);
Object.values(this.filters).forEach(function (field) {
Expand Down Expand Up @@ -1485,11 +1486,8 @@ frappe.views.QueryReport = class QueryReport extends frappe.views.BaseList {
this.make_access_log("Export", file_format);

let filters = this.get_filter_values(true);
if (frappe.urllib.get_dict("prepared_report_name")) {
filters = Object.assign(
frappe.urllib.get_dict("prepared_report_name"),
filters
);
if (this.prepared_report_name) {
filters.prepared_report_name = this.prepared_report_name;
}

const visible_idx = this.datatable?.bodyRenderer.visibleRowIndices || [];
Expand Down

0 comments on commit 5a73fd8

Please sign in to comment.