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