|
@@ -594,6 +594,197 @@ class TemplateController extends Controller
|
594
|
594
|
exit(json_encode($result));
|
595
|
595
|
}
|
596
|
596
|
|
|
597
|
+ /**
|
|
598
|
+ * 销售引流模板分配月报
|
|
599
|
+ */
|
|
600
|
+ public function templateLogMonthReport(Request $request){
|
|
601
|
+ $admin_id = (int)$request->input('admin_id');
|
|
602
|
+ $team_id = (int)$request->input('team_id');
|
|
603
|
+ $stime = $request->input('stime');
|
|
604
|
+ $etime = $request->input('etime');
|
|
605
|
+
|
|
606
|
+ //假如有团队筛选,检索销售队员
|
|
607
|
+ $sale_ids = null;
|
|
608
|
+ if($team_id>0){
|
|
609
|
+ $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
|
|
610
|
+ }
|
|
611
|
+
|
|
612
|
+ $page = (int)$request->input('page');
|
|
613
|
+ $pageSize = 20;
|
|
614
|
+ if($page<=0){
|
|
615
|
+ $page = 1;
|
|
616
|
+ }
|
|
617
|
+
|
|
618
|
+ $offset = ($page-1) * $pageSize;
|
|
619
|
+
|
|
620
|
+ $mstime = '';
|
|
621
|
+ $metime = '';
|
|
622
|
+ if($stime){
|
|
623
|
+ $mstime = date('Y-m-1', strtotime($stime));
|
|
624
|
+ }
|
|
625
|
+ if($etime){
|
|
626
|
+ $metime = date('Y-m-1', strtotime($etime.' +1 month'));
|
|
627
|
+ }
|
|
628
|
+ $_start = '2019-11-01';
|
|
629
|
+ $count = TemplatesLog::select(DB::raw('left(create_time,7) as month,admin_id'))->where('create_time', '>', $_start)->where(function($query) use($admin_id, $mstime, $metime, $sale_ids){
|
|
630
|
+ if($admin_id>0) $query->where('admin_id', $admin_id);
|
|
631
|
+ if($mstime) $query->where('create_time', '>=', $mstime);
|
|
632
|
+ if($metime) $query->where('create_time', '<', $metime);
|
|
633
|
+ if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
|
|
634
|
+ })->groupBy('month')->groupBy('admin_id')->get();
|
|
635
|
+ $count = count($count);
|
|
636
|
+ if ($count > 1) {
|
|
637
|
+ // 总页数
|
|
638
|
+ $pages = ceil($count/$pageSize);
|
|
639
|
+ }else{
|
|
640
|
+ // 总页数
|
|
641
|
+ $pages = 1;
|
|
642
|
+ }
|
|
643
|
+
|
|
644
|
+ $result = TemplatesLog::select(DB::raw('left(create_time,7) as month,admin_id,count(1) as tcount, count(if(type=1,true,null)) as pv_count'))->where('create_time', '>', $_start)->where(function($query) use($admin_id, $mstime, $metime, $sale_ids){
|
|
645
|
+ if($admin_id>0) $query->where('admin_id', $admin_id);
|
|
646
|
+ if($mstime) $query->where('create_time', '>=', $mstime);
|
|
647
|
+ if($metime) $query->where('create_time', '<', $metime);
|
|
648
|
+ if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
|
|
649
|
+ })->groupBy('month')->groupBy('admin_id')->orderBy('month', 'desc')->offset($offset)->limit($pageSize)->get();
|
|
650
|
+ $result = json_decode(json_encode($result), true);
|
|
651
|
+ foreach($result as $k=>&$v){
|
|
652
|
+ //起止时间
|
|
653
|
+ $s_time = date('Y-m-1', strtotime($v['month']));
|
|
654
|
+ $e_time = date('Y-m-1', strtotime($v['month'].' +1 month'));
|
|
655
|
+ //获取销售当月加粉情况
|
|
656
|
+ $custDetail = CustDetail::select(DB::raw('sum(fan_add) as fan_add'))->whereRaw('left(dtime,7)="'.$v['month'].'"')->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
|
|
657
|
+ if(!empty($custDetail)){
|
|
658
|
+ $v['fan_add'] = $custDetail->fan_add;
|
|
659
|
+ }else{
|
|
660
|
+ $v['fan_add'] = '';
|
|
661
|
+ }
|
|
662
|
+ $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('realname');
|
|
663
|
+ $v['long_count'] = $v['tcount'] - $v['pv_count'];
|
|
664
|
+
|
|
665
|
+ //点击率
|
|
666
|
+ //$v['click_rate'] = $v['pv_count']>0 && $v['long_count']>0 ? round( $v['long_count'] / $v['pv_count'], 4 ) * 100 . '%' : '';
|
|
667
|
+ $v['change_rate'] = $v['long_count']>0 && $v['fan_add']>0 ? round( $v['fan_add'] / $v['long_count'], 4 ) * 100 . '%' : '';
|
|
668
|
+
|
|
669
|
+ //1.获取当月新粉
|
|
670
|
+ $phones = DB::table('customers')->whereRaw('left(fanTime,7)="'.$v['month'].'"')->lists('phone');
|
|
671
|
+ $order = Order::select(DB::raw('count(1) as order_count, count(distinct(receiverMobile)) as cust_count, sum(receivedAmount) as order_amount'))->where('admin_id', $v['admin_id'])->whereIn('receiverMobile', $phones)->whereRaw('left(creatTime,7)="'.$v['month'].'"')->where('is_del', 0)->first();
|
|
672
|
+ //当月新粉下单单数
|
|
673
|
+ $v['order_count'] = $order->order_count;
|
|
674
|
+ //当月新粉下单人数
|
|
675
|
+ $v['cust_count'] = $order->cust_count;
|
|
676
|
+ //当月粉丝复购单数
|
|
677
|
+ $v['fugou_order_count'] = $order->order_count - $order->cust_count;
|
|
678
|
+ //当月粉丝总销售额
|
|
679
|
+ $v['order_amount'] = $order->order_amount;
|
|
680
|
+ //新粉下单率
|
|
681
|
+ $v['new_order_rate'] = $v['fan_add']>0 ? round($v['cust_count'] / $v['fan_add'], 4) * 100 . '%' : '';
|
|
682
|
+ //新粉复购率
|
|
683
|
+ //复购用户数 = 下单超过一单的用户数
|
|
684
|
+ $order_cust_fugou = Order::select(DB::raw('count(1) as counts,receiverMobile'))->where('admin_id', $v['admin_id'])->whereIn('receiverMobile', $phones)->where('is_del', 0)->whereRaw('left(creatTime,7)="'.$v['month'].'"')->groupBy('receiverMobile')->having('counts', '>', 1)->get();
|
|
685
|
+ $customer_fugou_count = count($order_cust_fugou);
|
|
686
|
+ $v['cust_fugou_rate'] = $v['cust_count']>0 ? round($customer_fugou_count / $v['cust_count'], 4) * 100 . '%' : '';
|
|
687
|
+ //新粉订单复购率
|
|
688
|
+ $v['order_fugou_rate'] = $order->cust_count>0 ? round($v['fugou_order_count'] / $order->cust_count, 4) * 100 . '%' : '';
|
|
689
|
+ //当月粉丝人均销售额
|
|
690
|
+ $v['avg_amount'] = $v['fan_add']>0 ? round($order->order_amount / $v['fan_add'], 2) : '';
|
|
691
|
+
|
|
692
|
+ }
|
|
693
|
+
|
|
694
|
+ $teamList = DB::table('teams')->select('id', 'name')->get();
|
|
695
|
+ $teamList = json_decode(json_encode($teamList), true);
|
|
696
|
+ $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->get();
|
|
697
|
+ $adminList = json_decode(json_encode($adminList), true);
|
|
698
|
+
|
|
699
|
+ return view('template/templateLogMonthReport', ['result'=>$result,
|
|
700
|
+ 'page' =>$page,
|
|
701
|
+ 'count' =>$count,
|
|
702
|
+ 'pages' =>$pages,
|
|
703
|
+ 'teamlist' =>$teamList,
|
|
704
|
+ 'adminlist' =>$adminList,
|
|
705
|
+ 'team_id' =>$team_id,
|
|
706
|
+ 'admin_id' =>$admin_id,
|
|
707
|
+ 'stime' =>$stime,
|
|
708
|
+ 'etime' =>$etime,
|
|
709
|
+ ]);
|
|
710
|
+ }
|
|
711
|
+
|
|
712
|
+ /**
|
|
713
|
+ * 销售引流模板分配月报
|
|
714
|
+ */
|
|
715
|
+ public function templateLogMonthReport_export(Request $request){
|
|
716
|
+ $admin_id = (int)$request->input('admin_id');
|
|
717
|
+ $team_id = (int)$request->input('team_id');
|
|
718
|
+ $stime = $request->input('stime');
|
|
719
|
+ $etime = $request->input('etime');
|
|
720
|
+
|
|
721
|
+ //假如有团队筛选,检索销售队员
|
|
722
|
+ $sale_ids = null;
|
|
723
|
+ if($team_id>0){
|
|
724
|
+ $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
|
|
725
|
+ }
|
|
726
|
+
|
|
727
|
+ $mstime = '';
|
|
728
|
+ $metime = '';
|
|
729
|
+ if($stime){
|
|
730
|
+ $mstime = date('Y-m-1', strtotime($stime));
|
|
731
|
+ }
|
|
732
|
+ if($etime){
|
|
733
|
+ $metime = date('Y-m-1', strtotime($etime.' +1 month'));
|
|
734
|
+ }
|
|
735
|
+ $_start = '2019-11-01';
|
|
736
|
+ $result = TemplatesLog::select(DB::raw('left(create_time,7) as month,admin_id,count(1) as tcount, count(if(type=1,true,null)) as pv_count'))->where('create_time', '>', $_start)->where(function($query) use($admin_id, $mstime, $metime, $sale_ids){
|
|
737
|
+ if($admin_id>0) $query->where('admin_id', $admin_id);
|
|
738
|
+ if($mstime) $query->where('create_time', '>=', $mstime);
|
|
739
|
+ if($metime) $query->where('create_time', '<', $metime);
|
|
740
|
+ if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
|
|
741
|
+ })->groupBy('month')->groupBy('admin_id')->orderBy('month', 'desc')->get();
|
|
742
|
+ $result = json_decode(json_encode($result), true);
|
|
743
|
+ foreach($result as $k=>&$v){
|
|
744
|
+ //获取销售当月加粉情况
|
|
745
|
+ $custDetail = CustDetail::select(DB::raw('sum(fan_add) as fan_add'))->whereRaw('left(dtime,7)="'.$v['month'].'"')->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
|
|
746
|
+ if(!empty($custDetail)){
|
|
747
|
+ $v['fan_add'] = $custDetail->fan_add;
|
|
748
|
+ }else{
|
|
749
|
+ $v['fan_add'] = '';
|
|
750
|
+ }
|
|
751
|
+ $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('realname');
|
|
752
|
+ $v['long_count'] = $v['tcount'] - $v['pv_count'];
|
|
753
|
+
|
|
754
|
+ //点击率
|
|
755
|
+ //$v['click_rate'] = $v['pv_count']>0 && $v['long_count']>0 ? round( $v['long_count'] / $v['pv_count'], 4 ) * 100 . '%' : '';
|
|
756
|
+ $v['change_rate'] = $v['long_count']>0 && $v['fan_add']>0 ? round( $v['fan_add'] / $v['long_count'], 4 ) * 100 . '%' : '';
|
|
757
|
+
|
|
758
|
+ //1.获取当月新粉
|
|
759
|
+ $phones = DB::table('customers')->whereRaw('left(fanTime,7)="'.$v['month'].'"')->lists('phone');
|
|
760
|
+ $order = Order::select(DB::raw('count(1) as order_count, count(distinct(receiverMobile)) as cust_count, sum(receivedAmount) as order_amount'))->where('admin_id', $v['admin_id'])->whereIn('receiverMobile', $phones)->whereRaw('left(creatTime,7)="'.$v['month'].'"')->where('is_del', 0)->first();
|
|
761
|
+ //当月新粉下单单数
|
|
762
|
+ $v['order_count'] = $order->order_count;
|
|
763
|
+ //当月新粉下单人数
|
|
764
|
+ $v['cust_count'] = $order->cust_count;
|
|
765
|
+ //当月粉丝复购单数
|
|
766
|
+ $v['fugou_order_count'] = $order->order_count - $order->cust_count;
|
|
767
|
+ //当月粉丝总销售额
|
|
768
|
+ $v['order_amount'] = $order->order_amount;
|
|
769
|
+ //新粉下单率
|
|
770
|
+ $v['new_order_rate'] = $v['fan_add']>0 ? round($v['cust_count'] / $v['fan_add'], 4) * 100 . '%' : '';
|
|
771
|
+ //新粉复购率
|
|
772
|
+ //复购用户数 = 下单超过一单的用户数
|
|
773
|
+ $order_cust_fugou = Order::select(DB::raw('count(1) as counts,receiverMobile'))->where('admin_id', $v['admin_id'])->whereIn('receiverMobile', $phones)->where('is_del', 0)->whereRaw('left(creatTime,7)="'.$v['month'].'"')->groupBy('receiverMobile')->having('counts', '>', 1)->get();
|
|
774
|
+ $customer_fugou_count = count($order_cust_fugou);
|
|
775
|
+ $v['cust_fugou_rate'] = $v['cust_count']>0 ? round($customer_fugou_count / $v['cust_count'], 4) * 100 . '%' : '';
|
|
776
|
+ //新粉订单复购率
|
|
777
|
+ $v['order_fugou_rate'] = $order->cust_count>0 ? round($v['fugou_order_count'] / $order->cust_count, 4) * 100 . '%' : '';
|
|
778
|
+ //当月粉丝人均销售额
|
|
779
|
+ $v['avg_amount'] = $v['fan_add']>0 ? round($order->order_amount / $v['fan_add'], 2) : '';
|
|
780
|
+
|
|
781
|
+ }
|
|
782
|
+
|
|
783
|
+ $indexKey = ['month','admin_name','pv_count','long_count','fan_add','cust_count','order_count','fugou_order_count','order_amount','change_rate','new_order_rate','cust_fugou_rate','order_fugou_rate','avg_amount'];
|
|
784
|
+ $title = ['月份', '销售名', '当月PV量', '当月长按次数', '当月上报加粉数', '当月新粉下单人数', '新粉下单单数', '当月粉丝复购单数', '当月粉丝总销售额', '粉丝转化率', '新粉下单率', '新粉复购率', '新粉订单复购率', '当月粉丝人均销售额'];
|
|
785
|
+ $filename = 'daofenyuebao_'.date('Y-m-d_H').'.xlsx';
|
|
786
|
+ return Order::export_excel($result, $filename, $indexKey, $title);
|
|
787
|
+ }
|
|
788
|
+
|
597
|
789
|
|
598
|
|
-}
|
599
|
|
-
|
|
790
|
+}
|