fix: use orm for queries
This commit is contained in:
parent
3f1444e410
commit
d095acdad5
@ -4,25 +4,43 @@ import frappe
|
|||||||
|
|
||||||
def get_leaderboards():
|
def get_leaderboards():
|
||||||
leaderboards = {
|
leaderboards = {
|
||||||
'Customer': 'erpnext.startup.leaderboard.get_all_customers',
|
"Customer": {
|
||||||
'Item': 'erpnext.startup.leaderboard.get_all_items',
|
"fields": ['total_sales_amount', 'total_qty_sold', 'outstanding_amount'],
|
||||||
'Supplier': 'erpnext.startup.leaderboard.get_all_suppliers',
|
"method": "erpnext.startup.leaderboard.get_all_customers",
|
||||||
'Sales Partner': 'erpnext.startup.leaderboard.get_all_sales_partner',
|
},
|
||||||
'Sales Person': 'erpnext.startup.leaderboard.get_all_sales_person',
|
"Item": {
|
||||||
|
"fields": ["total_sales_amount", "total_qty_sold", "total_purchase_amount",
|
||||||
|
"total_qty_purchased", "available_stock_qty", "available_stock_value"],
|
||||||
|
"method": "erpnext.startup.leaderboard.get_all_items",
|
||||||
|
},
|
||||||
|
"Supplier": {
|
||||||
|
"fields": ["total_purchase_amount", "total_qty_purchased", "outstanding_amount"],
|
||||||
|
"method": "erpnext.startup.leaderboard.get_all_suppliers",
|
||||||
|
},
|
||||||
|
"Sales Partner": {
|
||||||
|
"fields": ["total_sales_amount", "total_commission"],
|
||||||
|
"method": "erpnext.startup.leaderboard.get_all_sales_partner",
|
||||||
|
},
|
||||||
|
"Sales Person": {
|
||||||
|
"fields": ["total_sales_amount"],
|
||||||
|
"method": "erpnext.startup.leaderboard.get_all_sales_person",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return leaderboards
|
return leaderboards
|
||||||
|
|
||||||
def get_all_customers(from_date, company, field):
|
def get_all_customers(from_date, company, field, limit = None):
|
||||||
if field == "outstanding_amount":
|
if field == "outstanding_amount":
|
||||||
return frappe.db.sql("""
|
filters = [['docstatus', '=', '1'], ['company', '=', company]]
|
||||||
select customer as name, sum(outstanding_amount) as value
|
if from_date:
|
||||||
FROM `tabSales Invoice`
|
filters.append(['posting_date', '>=', from_date])
|
||||||
where docstatus = 1 and posting_date >= %s and company = %s
|
return frappe.db.get_all('Sales Invoice',
|
||||||
group by customer
|
fields = ['customer as name', 'sum(outstanding_amount) as value'],
|
||||||
order by value DESC
|
filters = filters,
|
||||||
limit 20
|
group_by = 'customer',
|
||||||
""", (from_date, company), as_dict=1)
|
order_by = 'value desc',
|
||||||
|
limit = limit
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
if field == "total_sales_amount":
|
if field == "total_sales_amount":
|
||||||
select_field = "sum(so_item.base_net_amount)"
|
select_field = "sum(so_item.base_net_amount)"
|
||||||
@ -36,18 +54,18 @@ def get_all_customers(from_date, company, field):
|
|||||||
where so.docstatus = 1 and so.transaction_date >= %s and so.company = %s
|
where so.docstatus = 1 and so.transaction_date >= %s and so.company = %s
|
||||||
group by so.customer
|
group by so.customer
|
||||||
order by value DESC
|
order by value DESC
|
||||||
limit 20
|
limit %s
|
||||||
""".format(select_field), (from_date, company), as_dict=1)
|
""".format(select_field), (from_date, company, limit), as_dict=1)
|
||||||
|
|
||||||
def get_all_items(from_date, company, field):
|
def get_all_items(from_date, company, field, limit = None):
|
||||||
if field in ("available_stock_qty", "available_stock_value"):
|
if field in ("available_stock_qty", "available_stock_value"):
|
||||||
return frappe.db.sql("""
|
select_field = "sum(actual_qty)" if field=="available_stock_qty" else "sum(stock_value)"
|
||||||
select item_code as name, {0} as value
|
return frappe.db.get_all('Bin',
|
||||||
from tabBin
|
fields = ['item_code as name', '{0} as value'.format(select_field)],
|
||||||
group by item_code
|
group_by = 'item_code',
|
||||||
order by value desc
|
order_by = 'value desc',
|
||||||
limit 20
|
limit = limit
|
||||||
""".format("sum(actual_qty)" if field=="available_stock_qty" else "sum(stock_value)"), as_dict=1)
|
)
|
||||||
else:
|
else:
|
||||||
if field == "total_sales_amount":
|
if field == "total_sales_amount":
|
||||||
select_field = "sum(order_item.base_net_amount)"
|
select_field = "sum(order_item.base_net_amount)"
|
||||||
@ -70,18 +88,21 @@ def get_all_items(from_date, company, field):
|
|||||||
and sales_order.company = %s and sales_order.transaction_date >= %s
|
and sales_order.company = %s and sales_order.transaction_date >= %s
|
||||||
group by order_item.item_code
|
group by order_item.item_code
|
||||||
order by value desc
|
order by value desc
|
||||||
limit 20
|
limit %s
|
||||||
""".format(select_field, select_doctype), (company, from_date), as_dict=1)
|
""".format(select_field, select_doctype), (company, from_date, limit), as_dict=1)
|
||||||
|
|
||||||
def get_all_suppliers(from_date, company, field):
|
def get_all_suppliers(from_date, company, field, limit = None):
|
||||||
if field == "outstanding_amount":
|
if field == "outstanding_amount":
|
||||||
return frappe.db.sql("""
|
filters = [['docstatus', '=', '1'], ['company', '=', company]]
|
||||||
select supplier as name, sum(outstanding_amount) as value
|
if from_date:
|
||||||
FROM `tabPurchase Invoice`
|
filters.append(['posting_date', '>=', from_date])
|
||||||
where docstatus = 1 and posting_date >= %s and company = %s
|
return frappe.db.get_all('Purchase Invoice',
|
||||||
group by supplier
|
fields = ['supplier as name', 'sum(outstanding_amount) as value'],
|
||||||
order by value DESC
|
filters = filters,
|
||||||
limit 20""", (from_date, company), as_dict=1)
|
group_by = 'supplier',
|
||||||
|
order_by = 'value desc',
|
||||||
|
limit = limit
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
if field == "total_purchase_amount":
|
if field == "total_purchase_amount":
|
||||||
select_field = "sum(purchase_order_item.base_net_amount)"
|
select_field = "sum(purchase_order_item.base_net_amount)"
|
||||||
@ -96,9 +117,9 @@ def get_all_suppliers(from_date, company, field):
|
|||||||
and purchase_order.company = %s
|
and purchase_order.company = %s
|
||||||
group by purchase_order.supplier
|
group by purchase_order.supplier
|
||||||
order by value DESC
|
order by value DESC
|
||||||
limit 20""".format(select_field), (from_date, company), as_dict=1)
|
limit %s""".format(select_field), (from_date, company, limit), as_dict=1)
|
||||||
|
|
||||||
def get_all_sales_partner(from_date, company, field):
|
def get_all_sales_partner(from_date, company, field, limit = None):
|
||||||
if field == "total_sales_amount":
|
if field == "total_sales_amount":
|
||||||
select_field = "sum(base_net_total)"
|
select_field = "sum(base_net_total)"
|
||||||
elif field == "total_commission":
|
elif field == "total_commission":
|
||||||
@ -111,10 +132,10 @@ def get_all_sales_partner(from_date, company, field):
|
|||||||
and transaction_date >= %s and company = %s
|
and transaction_date >= %s and company = %s
|
||||||
group by sales_partner
|
group by sales_partner
|
||||||
order by value DESC
|
order by value DESC
|
||||||
limit 20
|
limit %s
|
||||||
""".format(select_field), (from_date, company), as_dict=1)
|
""".format(select_field), (from_date, company, limit), as_dict=1)
|
||||||
|
|
||||||
def get_all_sales_person(from_date, company, field = None):
|
def get_all_sales_person(from_date, company, field = None, limit = None):
|
||||||
return frappe.db.sql("""
|
return frappe.db.sql("""
|
||||||
select sales_team.sales_person as name, sum(sales_order.base_net_total) as value
|
select sales_team.sales_person as name, sum(sales_order.base_net_total) as value
|
||||||
from `tabSales Order` as sales_order join `tabSales Team` as sales_team
|
from `tabSales Order` as sales_order join `tabSales Team` as sales_team
|
||||||
@ -124,5 +145,5 @@ def get_all_sales_person(from_date, company, field = None):
|
|||||||
and sales_order.company = %s
|
and sales_order.company = %s
|
||||||
group by sales_team.sales_person
|
group by sales_team.sales_person
|
||||||
order by value DESC
|
order by value DESC
|
||||||
limit 20
|
limit %s
|
||||||
""", (from_date, company), as_dict=1)
|
""", (from_date, company, limit), as_dict=1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user