prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 1 | |
| 2 | from __future__ import unicode_literals, print_function |
| 3 | import frappe |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 4 | from frappe.utils import cint |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 5 | |
| 6 | def get_leaderboards(): |
| 7 | leaderboards = { |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 8 | "Customer": { |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 9 | "fields": [ |
| 10 | {'fieldname': 'total_sales_amount', 'fieldtype': 'Currency'}, |
| 11 | 'total_qty_sold', |
| 12 | {'fieldname': 'outstanding_amount', 'fieldtype': 'Currency'} |
| 13 | ], |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 14 | "method": "erpnext.startup.leaderboard.get_all_customers", |
prssanna | 12d4be7 | 2020-10-16 11:31:47 +0530 | [diff] [blame] | 15 | "icon": "customer" |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 16 | }, |
| 17 | "Item": { |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 18 | "fields": [ |
| 19 | {'fieldname': 'total_sales_amount', 'fieldtype': 'Currency'}, |
prssanna | 119c976 | 2019-09-27 16:28:08 +0530 | [diff] [blame] | 20 | 'total_qty_sold', |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 21 | {'fieldname': 'total_purchase_amount', 'fieldtype': 'Currency'}, |
| 22 | 'total_qty_purchased', |
| 23 | 'available_stock_qty', |
| 24 | {'fieldname': 'available_stock_value', 'fieldtype': 'Currency'} |
| 25 | ], |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 26 | "method": "erpnext.startup.leaderboard.get_all_items", |
prssanna | 12d4be7 | 2020-10-16 11:31:47 +0530 | [diff] [blame] | 27 | "icon": "stock" |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 28 | }, |
| 29 | "Supplier": { |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 30 | "fields": [ |
| 31 | {'fieldname': 'total_purchase_amount', 'fieldtype': 'Currency'}, |
| 32 | 'total_qty_purchased', |
| 33 | {'fieldname': 'outstanding_amount', 'fieldtype': 'Currency'} |
| 34 | ], |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 35 | "method": "erpnext.startup.leaderboard.get_all_suppliers", |
prssanna | 12d4be7 | 2020-10-16 11:31:47 +0530 | [diff] [blame] | 36 | "icon": "buying" |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 37 | }, |
| 38 | "Sales Partner": { |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 39 | "fields": [ |
| 40 | {'fieldname': 'total_sales_amount', 'fieldtype': 'Currency'}, |
| 41 | {'fieldname': 'total_commission', 'fieldtype': 'Currency'} |
| 42 | ], |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 43 | "method": "erpnext.startup.leaderboard.get_all_sales_partner", |
prssanna | 12d4be7 | 2020-10-16 11:31:47 +0530 | [diff] [blame] | 44 | "icon": "hr" |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 45 | }, |
| 46 | "Sales Person": { |
prssanna | 8f7ed71 | 2019-09-27 15:09:40 +0530 | [diff] [blame] | 47 | "fields": [ |
| 48 | {'fieldname': 'total_sales_amount', 'fieldtype': 'Currency'} |
| 49 | ], |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 50 | "method": "erpnext.startup.leaderboard.get_all_sales_person", |
prssanna | 12d4be7 | 2020-10-16 11:31:47 +0530 | [diff] [blame] | 51 | "icon": "customer" |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 52 | } |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return leaderboards |
| 56 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 57 | @frappe.whitelist() |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 58 | def get_all_customers(date_range, company, field, limit = None): |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 59 | if field == "outstanding_amount": |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 60 | filters = [['docstatus', '=', '1'], ['company', '=', company]] |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 61 | if date_range: |
| 62 | date_range = frappe.parse_json(date_range) |
| 63 | filters.append(['posting_date', '>=', 'between', [date_range[0], date_range[1]]]) |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 64 | return frappe.db.get_all('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 |
| 70 | ) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 71 | else: |
| 72 | if field == "total_sales_amount": |
| 73 | select_field = "sum(so_item.base_net_amount)" |
| 74 | elif field == "total_qty_sold": |
| 75 | select_field = "sum(so_item.stock_qty)" |
| 76 | |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 77 | date_condition = get_date_condition(date_range, 'so.transaction_date') |
| 78 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 79 | return frappe.db.sql(""" |
| 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 |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 87 | """.format(select_field, date_condition), (company, cint(limit)), as_dict=1) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 88 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 89 | @frappe.whitelist() |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 90 | def get_all_items(date_range, company, field, limit = None): |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 91 | if field in ("available_stock_qty", "available_stock_value"): |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 92 | select_field = "sum(actual_qty)" if field=="available_stock_qty" else "sum(stock_value)" |
| 93 | return frappe.db.get_all('Bin', |
| 94 | fields = ['item_code as name', '{0} as value'.format(select_field)], |
| 95 | group_by = 'item_code', |
| 96 | order_by = 'value desc', |
| 97 | limit = limit |
| 98 | ) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 99 | else: |
| 100 | if field == "total_sales_amount": |
| 101 | select_field = "sum(order_item.base_net_amount)" |
| 102 | select_doctype = "Sales Order" |
| 103 | elif field == "total_purchase_amount": |
| 104 | select_field = "sum(order_item.base_net_amount)" |
| 105 | select_doctype = "Purchase Order" |
| 106 | elif field == "total_qty_sold": |
| 107 | select_field = "sum(order_item.stock_qty)" |
| 108 | select_doctype = "Sales Order" |
| 109 | elif field == "total_qty_purchased": |
| 110 | select_field = "sum(order_item.stock_qty)" |
| 111 | select_doctype = "Purchase Order" |
| 112 | |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 113 | date_condition = get_date_condition(date_range, 'sales_order.transaction_date') |
| 114 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 115 | return frappe.db.sql(""" |
| 116 | select order_item.item_code as name, {0} as value |
| 117 | from `tab{1}` sales_order join `tab{1} Item` as order_item |
| 118 | on sales_order.name = order_item.parent |
| 119 | where sales_order.docstatus = 1 |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 120 | and sales_order.company = %s {2} |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 121 | group by order_item.item_code |
| 122 | order by value desc |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 123 | limit %s |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 124 | """.format(select_field, select_doctype, date_condition), (company, cint(limit)), as_dict=1) #nosec |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 125 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 126 | @frappe.whitelist() |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 127 | def get_all_suppliers(date_range, company, field, limit = None): |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 128 | if field == "outstanding_amount": |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 129 | filters = [['docstatus', '=', '1'], ['company', '=', company]] |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 130 | if date_range: |
marination | 2a56000 | 2020-09-03 19:45:04 +0530 | [diff] [blame] | 131 | date_range = frappe.parse_json(date_range) |
| 132 | filters.append(['posting_date', 'between', [date_range[0], date_range[1]]]) |
prssanna | d095acd | 2019-09-26 13:41:24 +0530 | [diff] [blame] | 133 | return frappe.db.get_all('Purchase Invoice', |
| 134 | fields = ['supplier as name', 'sum(outstanding_amount) as value'], |
| 135 | filters = filters, |
| 136 | group_by = 'supplier', |
| 137 | order_by = 'value desc', |
| 138 | limit = limit |
| 139 | ) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 140 | else: |
| 141 | if field == "total_purchase_amount": |
| 142 | select_field = "sum(purchase_order_item.base_net_amount)" |
| 143 | elif field == "total_qty_purchased": |
| 144 | select_field = "sum(purchase_order_item.stock_qty)" |
| 145 | |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 146 | date_condition = get_date_condition(date_range, 'purchase_order.modified') |
| 147 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 148 | return frappe.db.sql(""" |
| 149 | select purchase_order.supplier as name, {0} as value |
| 150 | FROM `tabPurchase Order` as purchase_order LEFT JOIN `tabPurchase Order Item` |
| 151 | as purchase_order_item ON purchase_order.name = purchase_order_item.parent |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 152 | where |
| 153 | purchase_order.docstatus = 1 |
| 154 | {1} |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 155 | and purchase_order.company = %s |
| 156 | group by purchase_order.supplier |
| 157 | order by value DESC |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 158 | limit %s""".format(select_field, date_condition), (company, cint(limit)), as_dict=1) #nosec |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 159 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 160 | @frappe.whitelist() |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 161 | def get_all_sales_partner(date_range, company, field, limit = None): |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 162 | if field == "total_sales_amount": |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 163 | select_field = "sum(`base_net_total`)" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 164 | elif field == "total_commission": |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 165 | select_field = "sum(`total_commission`)" |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 166 | |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 167 | filters = { |
| 168 | 'sales_partner': ['!=', ''], |
| 169 | 'docstatus': 1, |
| 170 | 'company': company |
| 171 | } |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 172 | if date_range: |
| 173 | date_range = frappe.parse_json(date_range) |
| 174 | filters['transaction_date'] = ['between', [date_range[0], date_range[1]]] |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 175 | |
| 176 | return frappe.get_list('Sales Order', fields=[ |
| 177 | '`sales_partner` as name', |
| 178 | '{} as value'.format(select_field), |
| 179 | ], filters=filters, group_by='sales_partner', order_by='value DESC', limit=limit) |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 180 | |
prssanna | b874922 | 2019-09-30 11:12:10 +0530 | [diff] [blame] | 181 | @frappe.whitelist() |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 182 | def get_all_sales_person(date_range, company, field = None, limit = 0): |
| 183 | date_condition = get_date_condition(date_range, 'sales_order.transaction_date') |
| 184 | |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 185 | return frappe.db.sql(""" |
| 186 | select sales_team.sales_person as name, sum(sales_order.base_net_total) as value |
| 187 | from `tabSales Order` as sales_order join `tabSales Team` as sales_team |
| 188 | on sales_order.name = sales_team.parent and sales_team.parenttype = 'Sales Order' |
| 189 | where sales_order.docstatus = 1 |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 190 | and sales_order.company = %s |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 191 | {date_condition} |
prssanna | 3f1444e | 2019-09-24 13:04:53 +0530 | [diff] [blame] | 192 | group by sales_team.sales_person |
| 193 | order by value DESC |
Suraj Shetty | d23c998 | 2019-09-30 13:09:12 +0530 | [diff] [blame] | 194 | limit %s |
Prssanna Desai | fb89906 | 2020-07-22 20:17:50 +0530 | [diff] [blame] | 195 | """.format(date_condition=date_condition), (company, cint(limit)), as_dict=1) |
| 196 | |
| 197 | def get_date_condition(date_range, field): |
| 198 | date_condition = '' |
| 199 | if date_range: |
| 200 | date_range = frappe.parse_json(date_range) |
| 201 | from_date, to_date = date_range |
| 202 | date_condition = "and {0} between {1} and {2}".format( |
| 203 | field, frappe.db.escape(from_date), frappe.db.escape(to_date) |
| 204 | ) |
| 205 | return date_condition |