prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 1 | import frappe |
barredterra | 956c3c50 | 2023-12-11 19:46:37 +0100 | [diff] [blame] | 2 | from frappe.utils.deprecations import deprecated |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 3 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 5 | def get_leaderboards(): |
| 6 | leaderboards = { |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 7 | "Customer": { |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 8 | "fields": [ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 9 | {"fieldname": "total_sales_amount", "fieldtype": "Currency"}, |
| 10 | "total_qty_sold", |
| 11 | {"fieldname": "outstanding_amount", "fieldtype": "Currency"}, |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 12 | ], |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 13 | "method": "erpnext.startup.leaderboard.get_all_customers", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 14 | "icon": "customer", |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 15 | }, |
| 16 | "Item": { |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 17 | "fields": [ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 18 | {"fieldname": "total_sales_amount", "fieldtype": "Currency"}, |
| 19 | "total_qty_sold", |
| 20 | {"fieldname": "total_purchase_amount", "fieldtype": "Currency"}, |
| 21 | "total_qty_purchased", |
| 22 | "available_stock_qty", |
| 23 | {"fieldname": "available_stock_value", "fieldtype": "Currency"}, |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 24 | ], |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 25 | "method": "erpnext.startup.leaderboard.get_all_items", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 26 | "icon": "stock", |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 27 | }, |
| 28 | "Supplier": { |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 29 | "fields": [ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 30 | {"fieldname": "total_purchase_amount", "fieldtype": "Currency"}, |
| 31 | "total_qty_purchased", |
| 32 | {"fieldname": "outstanding_amount", "fieldtype": "Currency"}, |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 33 | ], |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 34 | "method": "erpnext.startup.leaderboard.get_all_suppliers", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 35 | "icon": "buying", |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 36 | }, |
| 37 | "Sales Partner": { |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 38 | "fields": [ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 39 | {"fieldname": "total_sales_amount", "fieldtype": "Currency"}, |
| 40 | {"fieldname": "total_commission", "fieldtype": "Currency"}, |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 41 | ], |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 42 | "method": "erpnext.startup.leaderboard.get_all_sales_partner", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 43 | "icon": "hr", |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 44 | }, |
| 45 | "Sales Person": { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 46 | "fields": [{"fieldname": "total_sales_amount", "fieldtype": "Currency"}], |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 47 | "method": "erpnext.startup.leaderboard.get_all_sales_person", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 48 | "icon": "customer", |
| 49 | }, |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return leaderboards |
| 53 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 54 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 55 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 56 | def get_all_customers(date_range, company, field, limit=None): |
barredterra | 137b5a6 | 2023-12-11 18:32:49 +0100 | [diff] [blame] | 57 | filters = [["docstatus", "=", "1"], ["company", "=", company]] |
| 58 | from_date, to_date = parse_date_range(date_range) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 59 | if field == "outstanding_amount": |
barredterra | 137b5a6 | 2023-12-11 18:32:49 +0100 | [diff] [blame] | 60 | if from_date and to_date: |
| 61 | filters.append(["posting_date", "between", [from_date, to_date]]) |
| 62 | |
| 63 | return frappe.get_list( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 64 | "Sales Invoice", |
| 65 | fields=["customer as name", "sum(outstanding_amount) as value"], |
| 66 | filters=filters, |
| 67 | group_by="customer", |
| 68 | order_by="value desc", |
| 69 | limit=limit, |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 70 | ) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 71 | else: |
| 72 | if field == "total_sales_amount": |
barredterra | 137b5a6 | 2023-12-11 18:32:49 +0100 | [diff] [blame] | 73 | select_field = "base_net_total" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 74 | elif field == "total_qty_sold": |
barredterra | 137b5a6 | 2023-12-11 18:32:49 +0100 | [diff] [blame] | 75 | select_field = "total_qty" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 76 | |
barredterra | 137b5a6 | 2023-12-11 18:32:49 +0100 | [diff] [blame] | 77 | if from_date and to_date: |
| 78 | filters.append(["transaction_date", "between", [from_date, to_date]]) |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 79 | |
barredterra | 137b5a6 | 2023-12-11 18:32:49 +0100 | [diff] [blame] | 80 | return frappe.get_list( |
| 81 | "Sales Order", |
| 82 | fields=["customer as name", f"sum({select_field}) as value"], |
| 83 | filters=filters, |
| 84 | group_by="customer", |
| 85 | order_by="value desc", |
| 86 | limit=limit, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 87 | ) |
| 88 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 89 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 90 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 91 | def get_all_items(date_range, company, field, limit=None): |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 92 | if field in ("available_stock_qty", "available_stock_value"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 93 | select_field = "sum(actual_qty)" if field == "available_stock_qty" else "sum(stock_value)" |
barredterra | 2721ee3a8 | 2023-12-11 19:34:24 +0100 | [diff] [blame] | 94 | results = frappe.db.get_all( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 95 | "Bin", |
| 96 | fields=["item_code as name", "{0} as value".format(select_field)], |
| 97 | group_by="item_code", |
| 98 | order_by="value desc", |
| 99 | limit=limit, |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 100 | ) |
barredterra | 2721ee3a8 | 2023-12-11 19:34:24 +0100 | [diff] [blame] | 101 | readable_active_items = set(frappe.get_list("Item", filters={"disabled": 0}, pluck="name")) |
| 102 | return [item for item in results if item["name"] in readable_active_items] |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 103 | else: |
| 104 | if field == "total_sales_amount": |
barredterra | 2721ee3a8 | 2023-12-11 19:34:24 +0100 | [diff] [blame] | 105 | select_field = "base_net_amount" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 106 | select_doctype = "Sales Order" |
| 107 | elif field == "total_purchase_amount": |
barredterra | 2721ee3a8 | 2023-12-11 19:34:24 +0100 | [diff] [blame] | 108 | select_field = "base_net_amount" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 109 | select_doctype = "Purchase Order" |
| 110 | elif field == "total_qty_sold": |
barredterra | 2721ee3a8 | 2023-12-11 19:34:24 +0100 | [diff] [blame] | 111 | select_field = "stock_qty" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 112 | select_doctype = "Sales Order" |
| 113 | elif field == "total_qty_purchased": |
barredterra | 2721ee3a8 | 2023-12-11 19:34:24 +0100 | [diff] [blame] | 114 | select_field = "stock_qty" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 115 | select_doctype = "Purchase Order" |
| 116 | |
barredterra | 2721ee3a8 | 2023-12-11 19:34:24 +0100 | [diff] [blame] | 117 | filters = [["docstatus", "=", "1"], ["company", "=", company]] |
| 118 | from_date, to_date = parse_date_range(date_range) |
| 119 | if from_date and to_date: |
| 120 | filters.append(["transaction_date", "between", [from_date, to_date]]) |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 121 | |
barredterra | 2721ee3a8 | 2023-12-11 19:34:24 +0100 | [diff] [blame] | 122 | child_doctype = f"{select_doctype} Item" |
| 123 | return frappe.get_list( |
| 124 | select_doctype, |
| 125 | fields=[ |
| 126 | f"`tab{child_doctype}`.item_code as name", |
| 127 | f"sum(`tab{child_doctype}`.{select_field}) as value", |
| 128 | ], |
| 129 | filters=filters, |
| 130 | order_by="value desc", |
| 131 | group_by=f"`tab{child_doctype}`.item_code", |
| 132 | limit=limit, |
| 133 | ) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 134 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 135 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 136 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 137 | def get_all_suppliers(date_range, company, field, limit=None): |
barredterra | 65df4b6 | 2023-12-11 19:34:54 +0100 | [diff] [blame] | 138 | filters = [["docstatus", "=", "1"], ["company", "=", company]] |
| 139 | from_date, to_date = parse_date_range(date_range) |
| 140 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 141 | if field == "outstanding_amount": |
barredterra | 65df4b6 | 2023-12-11 19:34:54 +0100 | [diff] [blame] | 142 | if from_date and to_date: |
| 143 | filters.append(["posting_date", "between", [from_date, to_date]]) |
| 144 | |
| 145 | return frappe.get_list( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 146 | "Purchase Invoice", |
| 147 | fields=["supplier as name", "sum(outstanding_amount) as value"], |
| 148 | filters=filters, |
| 149 | group_by="supplier", |
| 150 | order_by="value desc", |
| 151 | limit=limit, |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 152 | ) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 153 | else: |
| 154 | if field == "total_purchase_amount": |
barredterra | 65df4b6 | 2023-12-11 19:34:54 +0100 | [diff] [blame] | 155 | select_field = "base_net_total" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 156 | elif field == "total_qty_purchased": |
barredterra | 65df4b6 | 2023-12-11 19:34:54 +0100 | [diff] [blame] | 157 | select_field = "total_qty" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 158 | |
barredterra | 65df4b6 | 2023-12-11 19:34:54 +0100 | [diff] [blame] | 159 | if from_date and to_date: |
| 160 | filters.append(["transaction_date", "between", [from_date, to_date]]) |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 161 | |
barredterra | 65df4b6 | 2023-12-11 19:34:54 +0100 | [diff] [blame] | 162 | return frappe.get_list( |
| 163 | "Purchase Order", |
| 164 | fields=["supplier as name", f"sum({select_field}) as value"], |
| 165 | filters=filters, |
| 166 | group_by="supplier", |
| 167 | order_by="value desc", |
| 168 | limit=limit, |
| 169 | ) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 170 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 171 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 172 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 173 | def get_all_sales_partner(date_range, company, field, limit=None): |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 174 | if field == "total_sales_amount": |
barredterra | 40c1acc | 2023-12-11 19:45:32 +0100 | [diff] [blame] | 175 | select_field = "base_net_total" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 176 | elif field == "total_commission": |
barredterra | 40c1acc | 2023-12-11 19:45:32 +0100 | [diff] [blame] | 177 | select_field = "total_commission" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 178 | |
barredterra | 40c1acc | 2023-12-11 19:45:32 +0100 | [diff] [blame] | 179 | filters = [["docstatus", "=", "1"], ["company", "=", company], ["sales_partner", "is", "set"]] |
| 180 | from_date, to_date = parse_date_range(date_range) |
| 181 | if from_date and to_date: |
| 182 | filters.append(["transaction_date", "between", [from_date, to_date]]) |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 183 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 184 | return frappe.get_list( |
| 185 | "Sales Order", |
| 186 | fields=[ |
barredterra | 40c1acc | 2023-12-11 19:45:32 +0100 | [diff] [blame] | 187 | "sales_partner as name", |
| 188 | f"sum({select_field}) as value", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 189 | ], |
| 190 | filters=filters, |
| 191 | group_by="sales_partner", |
| 192 | order_by="value DESC", |
| 193 | limit=limit, |
| 194 | ) |
| 195 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 196 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 197 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 198 | def get_all_sales_person(date_range, company, field=None, limit=0): |
barredterra | 7babfd4 | 2023-12-11 19:44:24 +0100 | [diff] [blame] | 199 | filters = [ |
| 200 | ["docstatus", "=", "1"], |
| 201 | ["company", "=", company], |
| 202 | ["Sales Team", "sales_person", "is", "set"], |
| 203 | ] |
| 204 | from_date, to_date = parse_date_range(date_range) |
| 205 | if from_date and to_date: |
| 206 | filters.append(["transaction_date", "between", [from_date, to_date]]) |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 207 | |
barredterra | 7babfd4 | 2023-12-11 19:44:24 +0100 | [diff] [blame] | 208 | return frappe.get_list( |
| 209 | "Sales Order", |
| 210 | fields=[ |
| 211 | "`tabSales Team`.sales_person as name", |
| 212 | "sum(`tabSales Team`.allocated_amount) as value", |
| 213 | ], |
| 214 | filters=filters, |
| 215 | group_by="`tabSales Team`.sales_person", |
| 216 | order_by="value desc", |
| 217 | limit=limit, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 218 | ) |
| 219 | |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 220 | |
barredterra | 956c3c50 | 2023-12-11 19:46:37 +0100 | [diff] [blame] | 221 | @deprecated |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 222 | def get_date_condition(date_range, field): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 223 | date_condition = "" |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 224 | if date_range: |
| 225 | date_range = frappe.parse_json(date_range) |
| 226 | from_date, to_date = date_range |
| 227 | date_condition = "and {0} between {1} and {2}".format( |
| 228 | field, frappe.db.escape(from_date), frappe.db.escape(to_date) |
| 229 | ) |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 230 | return date_condition |
barredterra | 137b5a6 | 2023-12-11 18:32:49 +0100 | [diff] [blame] | 231 | |
| 232 | |
| 233 | def parse_date_range(date_range): |
| 234 | if date_range: |
| 235 | date_range = frappe.parse_json(date_range) |
| 236 | return date_range[0], date_range[1] |
| 237 | |
| 238 | return None, None |