2016-03-08 12:36:21 +00:00
|
|
|
# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
2013-06-07 08:04:16 +00:00
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2014-04-14 13:50:45 +00:00
|
|
|
from frappe import _
|
2014-03-03 06:36:50 +00:00
|
|
|
from frappe.utils import cint
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2013-06-05 12:47:28 +00:00
|
|
|
|
|
|
|
def execute(filters=None):
|
2013-06-07 08:04:16 +00:00
|
|
|
if not filters:
|
|
|
|
filters = {}
|
|
|
|
|
|
|
|
days_since_last_order = filters.get("days_since_last_order")
|
2015-06-11 13:06:35 +00:00
|
|
|
doctype = filters.get("doctype")
|
2016-03-08 12:36:21 +00:00
|
|
|
|
2013-06-07 09:58:43 +00:00
|
|
|
if cint(days_since_last_order) <= 0:
|
2014-04-14 13:50:45 +00:00
|
|
|
frappe.throw(_("'Days Since Last Order' must be greater than or equal to zero"))
|
2013-06-07 08:04:16 +00:00
|
|
|
|
2014-04-14 13:50:45 +00:00
|
|
|
columns = get_columns()
|
2015-06-11 13:06:35 +00:00
|
|
|
customers = get_sales_details(doctype)
|
2013-06-05 12:47:28 +00:00
|
|
|
|
|
|
|
data = []
|
|
|
|
for cust in customers:
|
2014-03-03 06:36:50 +00:00
|
|
|
if cint(cust[8]) >= cint(days_since_last_order):
|
2015-06-11 13:06:35 +00:00
|
|
|
cust.insert(7, get_last_sales_amt(cust[0], doctype))
|
2013-06-05 12:47:28 +00:00
|
|
|
data.append(cust)
|
2014-04-14 13:50:45 +00:00
|
|
|
return columns, data
|
2013-06-05 12:47:28 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-06-11 13:06:35 +00:00
|
|
|
def get_sales_details(doctype):
|
|
|
|
cond = """sum(so.base_net_total) as 'total_order_considered',
|
|
|
|
max(so.posting_date) as 'last_order_date',
|
|
|
|
DATEDIFF(CURDATE(), max(so.posting_date)) as 'days_since_last_order' """
|
|
|
|
if doctype == "Sales Order":
|
|
|
|
cond = """sum(if(so.status = "Stopped",
|
|
|
|
so.base_net_total * so.per_delivered/100,
|
|
|
|
so.base_net_total)) as 'total_order_considered',
|
|
|
|
max(so.transaction_date) as 'last_order_date',
|
|
|
|
DATEDIFF(CURDATE(), max(so.transaction_date)) as 'days_since_last_order'"""
|
2016-03-08 12:36:21 +00:00
|
|
|
|
2014-04-14 13:50:45 +00:00
|
|
|
return frappe.db.sql(
|
|
|
|
"""select
|
|
|
|
cust.name,
|
|
|
|
cust.customer_name,
|
|
|
|
cust.territory,
|
|
|
|
cust.customer_group,
|
|
|
|
count(distinct(so.name)) as 'num_of_order',
|
2015-06-11 13:06:35 +00:00
|
|
|
sum(base_net_total) as 'total_order_value', {0}
|
|
|
|
from `tabCustomer` cust, `tab{1}` so
|
2013-06-05 12:47:28 +00:00
|
|
|
where cust.name = so.customer and so.docstatus = 1
|
|
|
|
group by cust.name
|
2015-06-11 13:06:35 +00:00
|
|
|
order by 'days_since_last_order' desc """.format(
|
|
|
|
cond, doctype
|
|
|
|
),
|
|
|
|
as_list=1,
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-06-05 12:47:28 +00:00
|
|
|
|
2015-06-11 13:06:35 +00:00
|
|
|
def get_last_sales_amt(customer, doctype):
|
|
|
|
cond = "posting_date"
|
|
|
|
if doctype == "Sales Order":
|
|
|
|
cond = "transaction_date"
|
|
|
|
res = frappe.db.sql(
|
|
|
|
"""select base_net_total from `tab{0}`
|
|
|
|
where customer = %s and docstatus = 1 order by {1} desc
|
|
|
|
limit 1""".format(
|
|
|
|
doctype, cond
|
|
|
|
),
|
|
|
|
customer,
|
|
|
|
)
|
2013-06-06 14:01:14 +00:00
|
|
|
|
|
|
|
return res and res[0][0] or 0
|
2013-06-05 12:47:28 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-06-05 12:47:28 +00:00
|
|
|
def get_columns():
|
|
|
|
return [
|
2014-09-11 08:15:27 +00:00
|
|
|
_("Customer") + ":Link/Customer:120",
|
|
|
|
_("Customer Name") + ":Data:120",
|
|
|
|
_("Territory") + "::120",
|
|
|
|
_("Customer Group") + "::120",
|
|
|
|
_("Number of Order") + "::120",
|
|
|
|
_("Total Order Value") + ":Currency:120",
|
|
|
|
_("Total Order Considered") + ":Currency:160",
|
|
|
|
_("Last Order Amount") + ":Currency:160",
|
2015-06-11 13:06:35 +00:00
|
|
|
_("Last Order Date") + ":Date:160",
|
2014-09-11 08:15:27 +00:00
|
|
|
_("Days Since Last Order") + "::160",
|
2014-04-14 13:50:45 +00:00
|
|
|
]
|