fix: Pass aggregate_on_doctype to properly create the query

This commit is contained in:
Suraj Shetty 2021-04-20 14:21:25 +05:30
parent e637f8d703
commit a19207c2f1
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',