diff --git a/aumms/aumms/report/aumms_items_summary_report/__init__.py b/aumms/aumms/report/aumms_items_summary_report/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/aumms/aumms/report/aumms_items_summary_report/aumms_items_summary_report.js b/aumms/aumms/report/aumms_items_summary_report/aumms_items_summary_report.js new file mode 100644 index 00000000..6a3605f9 --- /dev/null +++ b/aumms/aumms/report/aumms_items_summary_report/aumms_items_summary_report.js @@ -0,0 +1,20 @@ +// Copyright (c) 2024, efeone and contributors +// For license information, please see license.txt + +frappe.query_reports["Aumms Items Summary report"] = { + "filters": [ + + { + "label":__("Item Group"), + "fieldname":"item_group", + "fieldtype":"Link", + "options":"AuMMS Item Group" + }, + { + "label":__("Item Code"), + "fieldname":"item_code", + "fieldtype":"Data", + }, + + ] +}; diff --git a/aumms/aumms/report/aumms_items_summary_report/aumms_items_summary_report.json b/aumms/aumms/report/aumms_items_summary_report/aumms_items_summary_report.json new file mode 100644 index 00000000..1f04b1f6 --- /dev/null +++ b/aumms/aumms/report/aumms_items_summary_report/aumms_items_summary_report.json @@ -0,0 +1,33 @@ +{ + "add_total_row": 0, + "columns": [], + "creation": "2024-02-07 10:18:53.941930", + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "filters": [], + "idx": 0, + "is_standard": "Yes", + "letter_head": "Sawa_gold", + "letterhead": null, + "modified": "2024-02-07 10:30:29.645973", + "modified_by": "Administrator", + "module": "AuMMS", + "name": "Aumms Items Summary report", + "owner": "Administrator", + "prepared_report": 0, + "ref_doctype": "AuMMS Item", + "report_name": "Aumms Items Summary report", + "report_type": "Script Report", + "roles": [ + { + "role": "System Manager" + }, + { + "role": "Sales Manager" + }, + { + "role": "AuMMS Manager" + } + ] +} \ No newline at end of file diff --git a/aumms/aumms/report/aumms_items_summary_report/aumms_items_summary_report.py b/aumms/aumms/report/aumms_items_summary_report/aumms_items_summary_report.py new file mode 100644 index 00000000..a5311365 --- /dev/null +++ b/aumms/aumms/report/aumms_items_summary_report/aumms_items_summary_report.py @@ -0,0 +1,107 @@ +# Copyright (c) 2024, efeone and contributors +# For license information, please see license.txt + +import frappe +from frappe import _ + +def execute(filters=None): + if not filters: + filters = {} + + if "item_code" not in filters: + filters["item_code"] = None + + columns = get_columns(filters) + data = get_data(filters) + return columns, data + +def get_columns(filters): + columns = [ + + { + "label": _("Item Group"), + "fieldname": "item_group", + "fieldtype": "Link", + "options": "AuMMS Item Group", + "width": 180 + }, + { + "label": _("Item Code"), + "fieldname": "item_code", + "fieldtype": "Data", + "width": 250 + }, + { + "label": _("Item Count"), + "fieldname": "item_count", + "fieldtype": "Int", + "width": 120 + }, + { + "label": _("Item Group Count"), + "fieldname": "item_group_count", + "fieldtype": "Int", + "width": 150 + }, + ] + return columns + +def get_data(filters): + # query = """ + # SELECT + # ai.item_group, + # ai.item_code, + # item_counts.item_count + # FROM + # `tabAuMMS Item` ai + # JOIN + # (SELECT item_group, COUNT(*) AS item_count + # FROM `tabAuMMS Item` + # GROUP BY item_group) AS item_counts + # ON ai.item_group = item_counts.item_group + # WHERE 1=1 + # """ + query = """ + SELECT + CASE WHEN ROW_NUMBER() OVER (PARTITION BY grouped_items.item_group ORDER BY ai.item_code) = 1 + THEN grouped_items.item_group + ELSE '' + END AS item_group, + CASE WHEN ROW_NUMBER() OVER (PARTITION BY grouped_items.item_group ORDER BY ai.item_code) = 1 + THEN grouped_items.item_group_count + ELSE NULL + END AS item_group_count, + ai.item_code, + COUNT(*) AS item_count + FROM + (SELECT + item_group, + COUNT(*) AS item_group_count + FROM + `tabAuMMS Item` + GROUP BY + item_group) AS grouped_items + JOIN + `tabAuMMS Item` AS ai + ON + grouped_items.item_group = ai.item_group + """ + + combine_where = 0 + if filters.get("item_code"): + query += " WHERE ai.item_code like '%{0}%'".format(filters.get("item_code")) + combine_where = 1 + + if filters.get("item_group"): + if combine_where: + query += " AND ai.item_group = '{0}'".format(filters.get("item_group")) + else: + query += " WHERE ai.item_group = '{0}'".format(filters.get("item_group")) + + query += " GROUP BY ai.item_code" + # query += " ORDER BY ai.item_group, ai.item_code, item_counts.item_count;" + + # Execute the query and fetch data + data = frappe.db.sql(query, as_dict=True) + + return data