Added Customers Not Buying Since Long Time against Sales Invoice
This commit is contained in:
parent
1f048b2426
commit
587bd144d5
@ -8,6 +8,13 @@ frappe.query_reports["Customers Not Buying Since Long Time"] = {
|
|||||||
"label": __("Days Since Last Order"),
|
"label": __("Days Since Last Order"),
|
||||||
"fieldtype": "Int",
|
"fieldtype": "Int",
|
||||||
"default": 60
|
"default": 60
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldname":"doctype",
|
||||||
|
"label": __("Doctype"),
|
||||||
|
"fieldtype": "Select",
|
||||||
|
"default": "Sales Order",
|
||||||
|
"options": "Sales Order\nSales Invoice"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -10,41 +10,51 @@ def execute(filters=None):
|
|||||||
if not filters: filters ={}
|
if not filters: filters ={}
|
||||||
|
|
||||||
days_since_last_order = filters.get("days_since_last_order")
|
days_since_last_order = filters.get("days_since_last_order")
|
||||||
|
doctype = filters.get("doctype")
|
||||||
|
|
||||||
if cint(days_since_last_order) <= 0:
|
if cint(days_since_last_order) <= 0:
|
||||||
frappe.throw(_("'Days Since Last Order' must be greater than or equal to zero"))
|
frappe.throw(_("'Days Since Last Order' must be greater than or equal to zero"))
|
||||||
|
|
||||||
columns = get_columns()
|
columns = get_columns()
|
||||||
customers = get_so_details()
|
customers = get_sales_details(doctype)
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
for cust in customers:
|
for cust in customers:
|
||||||
if cint(cust[8]) >= cint(days_since_last_order):
|
if cint(cust[8]) >= cint(days_since_last_order):
|
||||||
cust.insert(7,get_last_so_amt(cust[0]))
|
cust.insert(7,get_last_sales_amt(cust[0], doctype))
|
||||||
data.append(cust)
|
data.append(cust)
|
||||||
return columns, data
|
return columns, data
|
||||||
|
|
||||||
def get_so_details():
|
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'"""
|
||||||
|
|
||||||
return frappe.db.sql("""select
|
return frappe.db.sql("""select
|
||||||
cust.name,
|
cust.name,
|
||||||
cust.customer_name,
|
cust.customer_name,
|
||||||
cust.territory,
|
cust.territory,
|
||||||
cust.customer_group,
|
cust.customer_group,
|
||||||
count(distinct(so.name)) as 'num_of_order',
|
count(distinct(so.name)) as 'num_of_order',
|
||||||
sum(base_net_total) as 'total_order_value',
|
sum(base_net_total) as 'total_order_value', {0}
|
||||||
sum(if(so.status = "Stopped",
|
from `tabCustomer` cust, `tab{1}` so
|
||||||
so.base_net_total * so.per_delivered/100,
|
|
||||||
so.base_net_total)) as 'total_order_considered',
|
|
||||||
max(so.transaction_date) as 'last_sales_order_date',
|
|
||||||
DATEDIFF(CURDATE(), max(so.transaction_date)) as 'days_since_last_order'
|
|
||||||
from `tabCustomer` cust, `tabSales Order` so
|
|
||||||
where cust.name = so.customer and so.docstatus = 1
|
where cust.name = so.customer and so.docstatus = 1
|
||||||
group by cust.name
|
group by cust.name
|
||||||
order by 'days_since_last_order' desc """,as_list=1)
|
order by 'days_since_last_order' desc """.format(cond, doctype), as_list=1)
|
||||||
|
|
||||||
def get_last_so_amt(customer):
|
def get_last_sales_amt(customer, doctype):
|
||||||
res = frappe.db.sql("""select base_net_total from `tabSales Order`
|
cond = "posting_date"
|
||||||
where customer = %s and docstatus = 1 order by transaction_date desc
|
if doctype =="Sales Order":
|
||||||
limit 1""", customer)
|
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)
|
||||||
|
|
||||||
return res and res[0][0] or 0
|
return res and res[0][0] or 0
|
||||||
|
|
||||||
@ -58,6 +68,6 @@ def get_columns():
|
|||||||
_("Total Order Value") + ":Currency:120",
|
_("Total Order Value") + ":Currency:120",
|
||||||
_("Total Order Considered") + ":Currency:160",
|
_("Total Order Considered") + ":Currency:160",
|
||||||
_("Last Order Amount") + ":Currency:160",
|
_("Last Order Amount") + ":Currency:160",
|
||||||
_("Last Sales Order Date") + ":Date:160",
|
_("Last Order Date") + ":Date:160",
|
||||||
_("Days Since Last Order") + "::160"
|
_("Days Since Last Order") + "::160"
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user