feat:Custom fields in reports

(cherry picked from commit ff6cf8c3e3)
This commit is contained in:
deepeshgarg007 2019-04-04 12:15:51 +05:30
parent 9579dea26a
commit 3be3d66fb0
2 changed files with 131 additions and 6 deletions

View file

@ -21,6 +21,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "report_name",
"fieldtype": "Data",
"hidden": 0,
@ -52,6 +53,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "ref_doctype",
"fieldtype": "Link",
"hidden": 0,
@ -84,6 +86,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "is_standard",
"fieldtype": "Select",
"hidden": 0,
@ -116,6 +119,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "module",
"fieldtype": "Link",
"hidden": 0,
@ -148,6 +152,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "add_total_row",
"fieldtype": "Check",
"hidden": 0,
@ -179,6 +184,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "column_break_4",
"fieldtype": "Column Break",
"hidden": 0,
@ -209,6 +215,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "report_type",
"fieldtype": "Select",
"hidden": 0,
@ -241,6 +248,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "disabled",
"fieldtype": "Check",
"hidden": 0,
@ -272,6 +280,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "icon",
"fieldtype": "Data",
"hidden": 0,
@ -304,6 +313,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "color",
"fieldtype": "Data",
"hidden": 0,
@ -337,6 +347,7 @@
"collapsible": 0,
"columns": 0,
"depends_on": "eval: doc.is_standard == \"No\"",
"fetch_if_empty": 0,
"fieldname": "letter_head",
"fieldtype": "Link",
"hidden": 0,
@ -370,6 +381,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "section_break_6",
"fieldtype": "Section Break",
"hidden": 0,
@ -401,6 +413,7 @@
"collapsible": 0,
"columns": 0,
"depends_on": "eval:doc.report_type==\"Query Report\"",
"fetch_if_empty": 0,
"fieldname": "query",
"fieldtype": "Code",
"hidden": 0,
@ -434,6 +447,7 @@
"columns": 0,
"depends_on": "",
"description": "JavaScript Format: frappe.query_reports['REPORTNAME'] = {}",
"fetch_if_empty": 0,
"fieldname": "javascript",
"fieldtype": "Code",
"hidden": 1,
@ -466,6 +480,7 @@
"collapsible": 0,
"columns": 0,
"depends_on": "eval:doc.report_type==\"Report Builder\"",
"fetch_if_empty": 0,
"fieldname": "json",
"fieldtype": "Code",
"hidden": 0,
@ -497,6 +512,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "permission_rules",
"fieldtype": "Section Break",
"hidden": 0,
@ -530,6 +546,7 @@
"collapsible": 0,
"columns": 0,
"depends_on": "eval:doc.is_standard == 'Yes'",
"fetch_if_empty": 0,
"fieldname": "roles",
"fieldtype": "Table",
"hidden": 0,
@ -563,6 +580,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "disable_prepared_report",
"fieldtype": "Check",
"hidden": 0,
@ -595,6 +613,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "prepared_report",
"fieldtype": "Check",
"hidden": 1,
@ -620,6 +639,72 @@
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_in_quick_entry": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "customize_section",
"fieldtype": "Section Break",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Customize",
"length": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 0,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_in_quick_entry": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fetch_if_empty": 0,
"fieldname": "add_custom_fields_in_report",
"fieldtype": "Check",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Add Custom Fields In Report",
"length": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 0,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
}
],
"has_web_view": 0,
@ -633,7 +718,7 @@
"issingle": 0,
"istable": 0,
"max_attachments": 0,
"modified": "2019-01-25 12:04:50.833264",
"modified": "2019-04-03 20:39:41.786686",
"modified_by": "Administrator",
"module": "Core",
"name": "Report",

View file

@ -82,10 +82,10 @@ def generate_report_result(report, filters=None, user=None):
data_to_be_printed = res[4]
if result:
result = get_filtered_data(report.ref_doctype, columns, result, user)
result, columns = get_filtered_data(report.ref_doctype, columns, result, user, report.add_custom_fields_in_report)
if cint(report.add_total_row) and result:
result = add_total_row(result, columns)
# if cint(report.add_total_row) and result:
# result = add_total_row(result, columns)
return {
"result": result,
@ -354,7 +354,7 @@ def add_total_row(result, columns, meta = None):
return result
def get_filtered_data(ref_doctype, columns, data, user):
def get_filtered_data(ref_doctype, columns, data, user, add_custom_fields):
result = []
linked_doctypes = get_linked_doctypes(columns, data)
match_filters_per_doctype = get_user_match_filters(linked_doctypes, user=user)
@ -364,6 +364,46 @@ def get_filtered_data(ref_doctype, columns, data, user):
role_permissions = get_role_permissions(frappe.get_meta(ref_doctype), user)
if_owner = role_permissions.get("if_owner", {}).get("report")
doc_fields_map = {}
custom_field_value_map = {}
if add_custom_fields:
fields = frappe.db.sql(""" select dt, fieldname, fieldtype from `tabCustom Field`
where fieldtype not in ('Section Break', 'Column Break') and
dt in (%s)""" % ', '.join(['%s']* len(linked_doctypes)), tuple([doctype for doctype in linked_doctypes.keys()]), as_dict=1)
for d in fields:
doc_fields_map.setdefault(d.dt, [])
doc_fields_map.get(d.dt).append(d.fieldname)
columns.append({
"label": frappe.unscrub(d.fieldname),
"fieldname": d.filedname,
"fieldtype": d.fieldtype,
"width": 100
})
for doctype in linked_doctypes.keys():
if doc_fields_map.get(doctype):
values = frappe.db.sql("select name, {fields} from `tab{doctype}` "
.format(fields = ", ".join(doc_fields_map.get(doctype)), doctype=doctype), as_dict=1)
for value in values:
custom_field_value_map.setdefault(value.name, value)
for row in data:
for index, column in enumerate(columns):
print(index,column)
if isinstance(row, dict) and column.get("fieldtype") == "Link":
fieldname = column.get("fieldname")
row.update({ "test_field": custom_field_value_map.get(row.get(fieldname),{}).get("test_field")})
else:
print("$$$$$$$$$$")
custom_field_value_map.get(row[index],{})
row.append(custom_field_value_map.get(row[index],{}).get("test_field"))
if match_filters_per_doctype:
for row in data:
# Why linked_doctypes.get(ref_doctype)? because if column is empty, linked_doctypes[ref_doctype] is removed
@ -375,7 +415,7 @@ def get_filtered_data(ref_doctype, columns, data, user):
else:
result = list(data)
return result
return result, columns
def has_match(row, linked_doctypes, doctype_match_filters, ref_doctype, if_owner, columns_dict, user):