Hello…
I am creating a custom script report for a custom doctype “Inspection”. Docs of this doctype are basically inspection checklists for different cemeteries which can have different status (active, inactive, etc…) Ultimately, I want a report that would show the “Cemetery”, it’s “Status” and “Total Inspections” done on this cemetery. This report can be filtered by date range, cemetery status or for a certain cemetery.
JS Code:
frappe.query_reports["Cemetery Inspections"] = {
"filters": [
{
"fieldname": "cemetery_status",
"label": __("Cemetery Status"),
"fieldtype": "Select",
"options": ["فعالة", "غير فعالة", "عشوائية"],
"default": "فعالة"
},
{
"fieldname": "cemetery",
"label": __("Cemetery"),
"fieldtype": "Link",
"options": "Cemetery"
},
{
"fieldname": "from_date",
"label": __("From Date"),
"fieldtype": "Date",
"default": frappe.datetime.add_months(frappe.datetime.nowdate(), -1)
},
{
"fieldname": "to_date",
"label": __("To Date"),
"fieldtype": "Date",
"default": frappe.datetime.nowdate()
}
]
}
Python Code
def get_columns():
return [
{
'fieldname': 'cemetery',
'label': _('Cemetery'),
'fieldtype': 'Link',
'options': 'Cemetery'
},
{
'fieldname': 'cemetery_status',
'label': _('Cemetery Status'),
'fieldtype': 'Select',
'options': 'فعالة\nغير فعالة\nعشوائية'
},
{
'fieldname': 'total_inspections',
'label': _('Total Inspections'),
'fieldtype': 'Int'
}
]
def get_data(filters):
data = []
inspections = frappe.get_all('Inspection', filters={
'cemetery_status': filters.get('cemetery_status'),
'date': ('between', [filters.get('from_date'), filters.get('to_date')])
})
cemetery_count = {}
for inspection in inspections:
cemetery = inspection.get('cemetery')
if cemetery in cemetery_count:
cemetery_count[cemetery] = cemetery_count.get(cemetery, 0) + 1
else:
cemetery_count[cemetery] = 1
for cemetery, count in cemetery_count.items():
data.append({
'cemetery': cemetery,
'cemetery_status': frappe.get_value('Cemetery', cemetery, 'status'),
'total_inspections': count
})
return data
def execute(filters=None):
return get_columns(), get_data(filters)
UI:
Report is not standard.
Using ERPNext V14 hosted on Frappe Cloud.
Any help is appreciated…
Thanks in advance!