prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 1 | import frappe |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 2 | from frappe.utils import cint |
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): |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 57 | if field == "outstanding_amount": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 58 | filters = [["docstatus", "=", "1"], ["company", "=", company]] |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 59 | if date_range: |
| 60 | date_range = frappe.parse_json(date_range) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 61 | filters.append(["posting_date", ">=", "between", [date_range[0], date_range[1]]]) |
| 62 | return frappe.db.get_all( |
| 63 | "Sales Invoice", |
| 64 | fields=["customer as name", "sum(outstanding_amount) as value"], |
| 65 | filters=filters, |
| 66 | group_by="customer", |
| 67 | order_by="value desc", |
| 68 | limit=limit, |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 69 | ) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 70 | else: |
| 71 | if field == "total_sales_amount": |
| 72 | select_field = "sum(so_item.base_net_amount)" |
| 73 | elif field == "total_qty_sold": |
| 74 | select_field = "sum(so_item.stock_qty)" |
| 75 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 76 | date_condition = get_date_condition(date_range, "so.transaction_date") |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 77 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 78 | return frappe.db.sql( |
| 79 | """ |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 80 | select so.customer as name, {0} as value |
| 81 | FROM `tabSales Order` as so JOIN `tabSales Order Item` as so_item |
| 82 | ON so.name = so_item.parent |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 83 | where so.docstatus = 1 {1} and so.company = %s |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 84 | group by so.customer |
| 85 | order by value DESC |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 86 | limit %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 87 | """.format( |
| 88 | select_field, date_condition |
| 89 | ), |
| 90 | (company, cint(limit)), |
| 91 | as_dict=1, |
| 92 | ) |
| 93 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 94 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 95 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 96 | def get_all_items(date_range, company, field, limit=None): |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 97 | if field in ("available_stock_qty", "available_stock_value"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 98 | select_field = "sum(actual_qty)" if field == "available_stock_qty" else "sum(stock_value)" |
| 99 | return frappe.db.get_all( |
| 100 | "Bin", |
| 101 | fields=["item_code as name", "{0} as value".format(select_field)], |
| 102 | group_by="item_code", |
| 103 | order_by="value desc", |
| 104 | limit=limit, |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 105 | ) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 106 | else: |
| 107 | if field == "total_sales_amount": |
| 108 | select_field = "sum(order_item.base_net_amount)" |
| 109 | select_doctype = "Sales Order" |
| 110 | elif field == "total_purchase_amount": |
| 111 | select_field = "sum(order_item.base_net_amount)" |
| 112 | select_doctype = "Purchase Order" |
| 113 | elif field == "total_qty_sold": |
| 114 | select_field = "sum(order_item.stock_qty)" |
| 115 | select_doctype = "Sales Order" |
| 116 | elif field == "total_qty_purchased": |
| 117 | select_field = "sum(order_item.stock_qty)" |
| 118 | select_doctype = "Purchase Order" |
| 119 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 120 | date_condition = get_date_condition(date_range, "sales_order.transaction_date") |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 121 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 122 | return frappe.db.sql( |
| 123 | """ |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 124 | select order_item.item_code as name, {0} as value |
| 125 | from `tab{1}` sales_order join `tab{1} Item` as order_item |
| 126 | on sales_order.name = order_item.parent |
| 127 | where sales_order.docstatus = 1 |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 128 | and sales_order.company = %s {2} |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 129 | group by order_item.item_code |
| 130 | order by value desc |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 131 | limit %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 132 | """.format( |
| 133 | select_field, select_doctype, date_condition |
| 134 | ), |
| 135 | (company, cint(limit)), |
| 136 | as_dict=1, |
| 137 | ) # nosec |
| 138 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 139 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 140 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 141 | def get_all_suppliers(date_range, company, field, limit=None): |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 142 | if field == "outstanding_amount": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 143 | filters = [["docstatus", "=", "1"], ["company", "=", company]] |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 144 | if date_range: |
marination | 2a56000 | 2020-09-03 19:45:04 +0530 | [diff] [blame] | 145 | date_range = frappe.parse_json(date_range) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 146 | filters.append(["posting_date", "between", [date_range[0], date_range[1]]]) |
| 147 | return frappe.db.get_all( |
| 148 | "Purchase Invoice", |
| 149 | fields=["supplier as name", "sum(outstanding_amount) as value"], |
| 150 | filters=filters, |
| 151 | group_by="supplier", |
| 152 | order_by="value desc", |
| 153 | limit=limit, |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 154 | ) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 155 | else: |
| 156 | if field == "total_purchase_amount": |
| 157 | select_field = "sum(purchase_order_item.base_net_amount)" |
| 158 | elif field == "total_qty_purchased": |
| 159 | select_field = "sum(purchase_order_item.stock_qty)" |
| 160 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 161 | date_condition = get_date_condition(date_range, "purchase_order.modified") |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 162 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 163 | return frappe.db.sql( |
| 164 | """ |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 165 | select purchase_order.supplier as name, {0} as value |
| 166 | FROM `tabPurchase Order` as purchase_order LEFT JOIN `tabPurchase Order Item` |
| 167 | as purchase_order_item ON purchase_order.name = purchase_order_item.parent |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 168 | where |
| 169 | purchase_order.docstatus = 1 |
| 170 | {1} |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 171 | and purchase_order.company = %s |
| 172 | group by purchase_order.supplier |
| 173 | order by value DESC |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 174 | limit %s""".format( |
| 175 | select_field, date_condition |
| 176 | ), |
| 177 | (company, cint(limit)), |
| 178 | as_dict=1, |
| 179 | ) # nosec |
| 180 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 181 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 182 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 183 | def get_all_sales_partner(date_range, company, field, limit=None): |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 184 | if field == "total_sales_amount": |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 185 | select_field = "sum(`base_net_total`)" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 186 | elif field == "total_commission": |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 187 | select_field = "sum(`total_commission`)" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 188 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 189 | filters = {"sales_partner": ["!=", ""], "docstatus": 1, "company": company} |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 190 | if date_range: |
| 191 | date_range = frappe.parse_json(date_range) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 192 | filters["transaction_date"] = ["between", [date_range[0], date_range[1]]] |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 193 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 194 | return frappe.get_list( |
| 195 | "Sales Order", |
| 196 | fields=[ |
| 197 | "`sales_partner` as name", |
| 198 | "{} as value".format(select_field), |
| 199 | ], |
| 200 | filters=filters, |
| 201 | group_by="sales_partner", |
| 202 | order_by="value DESC", |
| 203 | limit=limit, |
| 204 | ) |
| 205 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 206 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 207 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 208 | def get_all_sales_person(date_range, company, field=None, limit=0): |
| 209 | date_condition = get_date_condition(date_range, "sales_order.transaction_date") |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 210 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 211 | return frappe.db.sql( |
| 212 | """ |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 213 | select sales_team.sales_person as name, sum(sales_order.base_net_total) as value |
| 214 | from `tabSales Order` as sales_order join `tabSales Team` as sales_team |
| 215 | on sales_order.name = sales_team.parent and sales_team.parenttype = 'Sales Order' |
| 216 | where sales_order.docstatus = 1 |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 217 | and sales_order.company = %s |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 218 | {date_condition} |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 219 | group by sales_team.sales_person |
| 220 | order by value DESC |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 221 | limit %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 222 | """.format( |
| 223 | date_condition=date_condition |
| 224 | ), |
| 225 | (company, cint(limit)), |
| 226 | as_dict=1, |
| 227 | ) |
| 228 | |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 229 | |
| 230 | def get_date_condition(date_range, field): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 231 | date_condition = "" |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 232 | if date_range: |
| 233 | date_range = frappe.parse_json(date_range) |
| 234 | from_date, to_date = date_range |
| 235 | date_condition = "and {0} between {1} and {2}".format( |
| 236 | field, frappe.db.escape(from_date), frappe.db.escape(to_date) |
| 237 | ) |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 238 | return date_condition |