销售统计功能
Posted qq849784670
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了销售统计功能相关的知识,希望对你有一定的参考价值。
1 class TongJiView(View): 2 3 def get(self,request): 4 5 date = request.GET.get("date","today") 6 if hasattr(self,date): 7 context = getattr(self,date)() 8 9 return render(request,"customer/tongji.html",context) 10 11 def today(self): 12 today = datetime.datetime.now().date() 13 customer_list = Customer.objects.filter(deal_date=today) 14 15 # 查询每一个销售的名字以及今天对应的成单量 16 ret = UserInfo.objects.filter(depart_id=2,customers__deal_date=today).annotate(c=Count("customers")).values_list("username","c") 17 ret = [[item[0], item[1]] for item in ret] 18 return {"customer_list":customer_list,"ret":ret,"today":today} 19 20 def zuotian(self): 21 today = datetime.datetime.now().date() 22 zuotian = datetime.datetime.now().date() - datetime.timedelta(days=1) 23 customer_list = Customer.objects.filter(deal_date=zuotian) 24 25 # 查询每一个销售的名字以及昨天对应的成单量 26 ret = UserInfo.objects.filter( 27 depart_id=2,customers__deal_date=zuotian).annotate(c=Count("customers")).values_list("username","c") 28 ret = [[item[0], item[1]] for item in ret] 29 return {"customer_list":customer_list,"ret":ret,"today":today} 30 31 def week(self): 32 today = datetime.datetime.now().date() 33 week = datetime.datetime.now().date() - datetime.timedelta(weeks=1) 34 customer_list = Customer.objects.filter(deal_date__gte=week,deal_date__lte=today) 35 36 # 查询每一个销售的名字以及一周对应的成单量 37 ret = UserInfo.objects.filter(depart_id=2,customers__deal_date__gte=week, 38 customers__deal_date__lte=today).annotate(c=Count("customers")).values_list("username", 39 "c") 40 ret = [[item[0], item[1]] for item in ret] 41 return {"customer_list":customer_list,"ret":ret,"today":today} 42 43 def month(self): 44 today = datetime.datetime.now().date() 45 month = datetime.datetime.now().date() - datetime.timedelta(weeks=4) 46 customer_list = Customer.objects.filter(deal_date__gte=month, deal_date__lte=today) 47 48 # 查询每一个销售的名字以及一周对应的成单量 49 ret = UserInfo.objects.filter(depart_id=2, customers__deal_date__gte=month, 50 customers__deal_date__lte=today).annotate(c=Count("customers")).values_list("username", 51 "c") 52 ret = [[item[0],item[1]] for item in ret] 53 return {"customer_list": customer_list,"ret":ret,"today":today}
升级版本
1 class TongJiView(View): 2 3 def get(self,request): 4 5 date = request.GET.get("date","today") 6 7 today = datetime.datetime.now().date() 8 delta1 = datetime.timedelta(days=1) 9 delta2 = datetime.timedelta(weeks=1) 10 delta3 = datetime.timedelta(weeks=4) 11 12 condition = { 13 "today":[{"deal_date":today},{"depart_id":2,"customers__deal_date":today}], 14 "zuotian":[{"deal_date":today-delta1},{"depart_id":2,"customers__deal_date":today-delta1}], 15 "week":[{"deal_date":today-delta2},{"depart_id":2,"customers__deal_date__gte":today-delta2,"customers__deal_date__lte":today}], 16 "month":[{"deal_date":today-delta3},{"depart_id":2,"customers__deal_date__gte":today-delta3,"customers__deal_date__lte":today}], 17 } 18 19 customer_list = Customer.objects.filter(**condition.get(date)[0]) 20 ret = UserInfo.objects.filter(**condition.get(date)[1]).annotate(c=Count("customers")).values_list("username", 21 "c") 22 ret = [[item[0], item[1]] for item in ret] 23 return render(request, "customer/tongji.html", {"customer_list": customer_list,"ret":ret,"today":today})
以上是关于销售统计功能的主要内容,如果未能解决你的问题,请参考以下文章