fix: Pass aggregate_on_doctype to properly create the query (backport #12917) (#12922)

(cherry picked from commit a19207c2f1)

Co-authored-by: Suraj Shetty <surajshetty3416@gmail.com>
This commit is contained in:
mergify[bot] 2021-04-20 15:01:28 +05:30 committed by GitHub
parent c5c5da51a2
commit 98f0aca0bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 7 deletions

View file

@ -126,13 +126,14 @@ def setup_group_by(data):
if data.group_by:
if data.aggregate_function.lower() not in ('count', 'sum', 'avg'):
frappe.throw(_('Invalid aggregate function'))
if '`' in data.aggregate_on:
raise_invalid_field(data.aggregate_on)
data.fields.append('{aggregate_function}(`tab{doctype}`.`{aggregate_on}`) AS _aggregate_column'.format(**data))
if data.aggregate_on:
data.fields.append(data.aggregate_on)
data.pop('aggregate_on')
if frappe.db.has_column(data.aggregate_on_doctype, data.aggregate_on_field):
data.fields.append('{aggregate_function}(`tab{aggregate_on_doctype}`.`{aggregate_on_field}`) AS _aggregate_column'.format(**data))
else:
raise_invalid_field(data.aggregate_on_field)
data.pop('aggregate_on_doctype')
data.pop('aggregate_on_field')
data.pop('aggregate_function')
def raise_invalid_field(fieldname):

View file

@ -313,7 +313,8 @@ frappe.ui.GroupBy = class {
Object.assign(args, {
with_comment_count: false,
aggregate_on: this.aggregate_on || 'name',
aggregate_on_field: this.aggregate_on_field || 'name',
aggregate_on_doctype: this.aggregate_on_doctype || this.doctype,
aggregate_function: this.aggregate_function || 'count',
group_by: this.report_view.group_by || null,
order_by: '_aggregate_column desc',