sunhao 5 years ago
parent
commit
1c072b7349

+ 17 - 17
app/Http/Controllers/Admin/TemplateController.php

735
             $metime = date('Y-m-1', strtotime($etime.' +1 month'));
735
             $metime = date('Y-m-1', strtotime($etime.' +1 month'));
736
         }
736
         }
737
         $_start = '2019-11-01';
737
         $_start = '2019-11-01';
738
-        $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){
738
+        $count = TemplatesLog::select(DB::raw('mdate,admin_id'))->where('create_time', '>', $_start)->where(function($query) use($admin_id, $mstime, $metime, $sale_ids){
739
             if($admin_id>0) $query->where('admin_id', $admin_id);
739
             if($admin_id>0) $query->where('admin_id', $admin_id);
740
             if($mstime) $query->where('create_time', '>=', $mstime);
740
             if($mstime) $query->where('create_time', '>=', $mstime);
741
             if($metime) $query->where('create_time', '<', $metime);
741
             if($metime) $query->where('create_time', '<', $metime);
742
             if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
742
             if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
743
-        })->groupBy('month')->groupBy('admin_id')->get();
743
+        })->groupBy('mdate')->groupBy('admin_id')->get();
744
         $count = count($count);
744
         $count = count($count);
745
         if ($count > 1) {
745
         if ($count > 1) {
746
             // 总页数
746
             // 总页数
750
             $pages = 1;
750
             $pages = 1;
751
         }
751
         }
752
 
752
 
753
-        $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){
753
+        $result = TemplatesLog::select(DB::raw('mdate,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){
754
             if($admin_id>0) $query->where('admin_id', $admin_id);
754
             if($admin_id>0) $query->where('admin_id', $admin_id);
755
             if($mstime) $query->where('create_time', '>=', $mstime);
755
             if($mstime) $query->where('create_time', '>=', $mstime);
756
             if($metime) $query->where('create_time', '<', $metime);
756
             if($metime) $query->where('create_time', '<', $metime);
757
             if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
757
             if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
758
-        })->groupBy('month')->groupBy('admin_id')->orderBy('month', 'desc')->get();
758
+        })->groupBy('mdate')->groupBy('admin_id')->orderBy('mdate', 'desc')->get();
759
         $result = json_decode(json_encode($result), true);
759
         $result = json_decode(json_encode($result), true);
760
         foreach($result as $k=>&$v){
760
         foreach($result as $k=>&$v){
761
             //起止时间
761
             //起止时间
762
-            $s_time = date('Y-m-1', strtotime($v['month']));
763
-            $e_time = date('Y-m-1', strtotime($v['month'].' +1 month'));
762
+            $s_time = date('Y-m-1', strtotime($v['mdate']));
763
+            $e_time = date('Y-m-1', strtotime($v['mdate'].' +1 month'));
764
             //获取销售当月加粉情况
764
             //获取销售当月加粉情况
765
-            $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();
765
+            $custDetail = CustDetail::select(DB::raw('sum(fan_add) as fan_add'))->whereRaw('left(dtime,7)="'.$v['mdate'].'"')->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
766
             if(!empty($custDetail)){
766
             if(!empty($custDetail)){
767
                 $v['fan_add'] = $custDetail->fan_add;
767
                 $v['fan_add'] = $custDetail->fan_add;
768
             }else{
768
             }else{
776
             $v['change_rate'] = $v['pv_count']>0 && $v['fan_add']>0 ? round( $v['fan_add'] / $v['pv_count'], 4 ) * 100 . '%' : '';
776
             $v['change_rate'] = $v['pv_count']>0 && $v['fan_add']>0 ? round( $v['fan_add'] / $v['pv_count'], 4 ) * 100 . '%' : '';
777
 
777
 
778
             //1.获取当月新粉
778
             //1.获取当月新粉
779
-            $phones = DB::table('customers')->whereRaw('left(fanTime,7)="'.$v['month'].'"')->lists('phone');
780
-            $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(createTime,7)="'.$v['month'].'"')->where('is_del', 0)->first();
779
+            $phones = DB::table('customers')->whereRaw('left(fanTime,7)="'.$v['mdate'].'"')->lists('phone');
780
+            $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(createTime,7)="'.$v['mdate'].'"')->where('is_del', 0)->first();
781
             //当月新粉下单单数
781
             //当月新粉下单单数
782
             $v['order_count'] = $order->order_count;
782
             $v['order_count'] = $order->order_count;
783
             //当月新粉下单人数
783
             //当月新粉下单人数
790
             $v['new_order_rate'] = $v['fan_add']>0 ? round($v['cust_count'] / $v['fan_add'], 4) * 100 . '%' : '';
790
             $v['new_order_rate'] = $v['fan_add']>0 ? round($v['cust_count'] / $v['fan_add'], 4) * 100 . '%' : '';
791
             //新粉复购率
791
             //新粉复购率
792
             //复购用户数 = 下单超过一单的用户数
792
             //复购用户数 = 下单超过一单的用户数
793
-            $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(createTime,7)="'.$v['month'].'"')->groupBy('receiverMobile')->having('counts', '>', 1)->get();
793
+            $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(createTime,7)="'.$v['mdate'].'"')->groupBy('receiverMobile')->having('counts', '>', 1)->get();
794
             $customer_fugou_count = count($order_cust_fugou);
794
             $customer_fugou_count = count($order_cust_fugou);
795
             $v['cust_fugou_rate'] = $v['cust_count']>0 ? round($customer_fugou_count / $v['cust_count'], 4) * 100 . '%' : '';
795
             $v['cust_fugou_rate'] = $v['cust_count']>0 ? round($customer_fugou_count / $v['cust_count'], 4) * 100 . '%' : '';
796
             //新粉订单复购率
796
             //新粉订单复购率
862
             $metime = date('Y-m-1', strtotime($etime.' +1 month'));
862
             $metime = date('Y-m-1', strtotime($etime.' +1 month'));
863
         }
863
         }
864
         $_start = '2019-11-01';
864
         $_start = '2019-11-01';
865
-        $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){
865
+        $result = TemplatesLog::select(DB::raw('mdate,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){
866
             if($admin_id>0) $query->where('admin_id', $admin_id);
866
             if($admin_id>0) $query->where('admin_id', $admin_id);
867
             if($mstime) $query->where('create_time', '>=', $mstime);
867
             if($mstime) $query->where('create_time', '>=', $mstime);
868
             if($metime) $query->where('create_time', '<', $metime);
868
             if($metime) $query->where('create_time', '<', $metime);
869
             if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
869
             if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
870
-        })->groupBy('month')->groupBy('admin_id')->orderBy('month', 'desc')->get();
870
+        })->groupBy('mdate')->groupBy('admin_id')->orderBy('mdate', 'desc')->get();
871
         $result = json_decode(json_encode($result), true);
871
         $result = json_decode(json_encode($result), true);
872
         foreach($result as $k=>&$v){
872
         foreach($result as $k=>&$v){
873
             //获取销售当月加粉情况
873
             //获取销售当月加粉情况
874
-            $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();
874
+            $custDetail = CustDetail::select(DB::raw('sum(fan_add) as fan_add'))->whereRaw('left(dtime,7)="'.$v['mdate'].'"')->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
875
             if(!empty($custDetail)){
875
             if(!empty($custDetail)){
876
                 $v['fan_add'] = $custDetail->fan_add;
876
                 $v['fan_add'] = $custDetail->fan_add;
877
             }else{
877
             }else{
885
             $v['change_rate'] = $v['pv_count']>0 && $v['fan_add']>0 ? round( $v['fan_add'] / $v['pv_count'], 4 ) * 100 . '%' : '';
885
             $v['change_rate'] = $v['pv_count']>0 && $v['fan_add']>0 ? round( $v['fan_add'] / $v['pv_count'], 4 ) * 100 . '%' : '';
886
 
886
 
887
             //1.获取当月新粉
887
             //1.获取当月新粉
888
-            $phones = DB::table('customers')->whereRaw('left(fanTime,7)="'.$v['month'].'"')->lists('phone');
889
-            $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(createTime,7)="'.$v['month'].'"')->where('is_del', 0)->first();
888
+            $phones = DB::table('customers')->whereRaw('left(fanTime,7)="'.$v['mdate'].'"')->lists('phone');
889
+            $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(createTime,7)="'.$v['mdate'].'"')->where('is_del', 0)->first();
890
             //当月新粉下单单数
890
             //当月新粉下单单数
891
             $v['order_count'] = $order->order_count;
891
             $v['order_count'] = $order->order_count;
892
             //当月新粉下单人数
892
             //当月新粉下单人数
899
             $v['new_order_rate'] = $v['fan_add']>0 ? round($v['cust_count'] / $v['fan_add'], 4) * 100 . '%' : '';
899
             $v['new_order_rate'] = $v['fan_add']>0 ? round($v['cust_count'] / $v['fan_add'], 4) * 100 . '%' : '';
900
             //新粉复购率
900
             //新粉复购率
901
             //复购用户数 = 下单超过一单的用户数
901
             //复购用户数 = 下单超过一单的用户数
902
-            $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(createTime,7)="'.$v['month'].'"')->groupBy('receiverMobile')->having('counts', '>', 1)->get();
902
+            $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(createTime,7)="'.$v['mdate'].'"')->groupBy('receiverMobile')->having('counts', '>', 1)->get();
903
             $customer_fugou_count = count($order_cust_fugou);
903
             $customer_fugou_count = count($order_cust_fugou);
904
             $v['cust_fugou_rate'] = $v['cust_count']>0 ? round($customer_fugou_count / $v['cust_count'], 4) * 100 . '%' : '';
904
             $v['cust_fugou_rate'] = $v['cust_count']>0 ? round($customer_fugou_count / $v['cust_count'], 4) * 100 . '%' : '';
905
             //新粉订单复购率
905
             //新粉订单复购率
912
         $sort_c = array_column($result, 'avg_amount');
912
         $sort_c = array_column($result, 'avg_amount');
913
         array_multisort($sort_c,SORT_DESC,$result);
913
         array_multisort($sort_c,SORT_DESC,$result);
914
 
914
 
915
-        $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'];
915
+        $indexKey = ['mdate','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'];
916
         $title = ['月份', '销售名', '当月PV量', '当月长按次数', '当月上报加粉数', '当月新粉下单人数', '新粉下单单数', '当月粉丝复购单数', '当月粉丝总销售额', '粉丝转化率', '新粉下单率', '新粉复购率', '新粉订单复购率', '当月粉丝人均销售额'];
916
         $title = ['月份', '销售名', '当月PV量', '当月长按次数', '当月上报加粉数', '当月新粉下单人数', '新粉下单单数', '当月粉丝复购单数', '当月粉丝总销售额', '粉丝转化率', '新粉下单率', '新粉复购率', '新粉订单复购率', '当月粉丝人均销售额'];
917
         $filename = 'daofenyuebao_'.date('Y-m-d_H').'.xlsx';
917
         $filename = 'daofenyuebao_'.date('Y-m-d_H').'.xlsx';
918
         return Order::export_excel($result, $filename, $indexKey, $title);  
918
         return Order::export_excel($result, $filename, $indexKey, $title);  

+ 1 - 1
resources/views/template/templateLogMonthReport.blade.php

57
                 @if($result)
57
                 @if($result)
58
                     @foreach($result as $a)
58
                     @foreach($result as $a)
59
                         <tr class="text-c" style=" text-align:center;">                           
59
                         <tr class="text-c" style=" text-align:center;">                           
60
-                            <td>{{$a['month']}}</td>                                                                                   
60
+                            <td>{{$a['mdate']}}</td>                                                                                   
61
                             <td>{{$a['admin_name']}}</td>                                                                                   
61
                             <td>{{$a['admin_name']}}</td>                                                                                   
62
                             <td>{{$a['pv_count']}}</td>                                                                                   
62
                             <td>{{$a['pv_count']}}</td>                                                                                   
63
                             <td>{{$a['long_count']}}</td>                                                                                   
63
                             <td>{{$a['long_count']}}</td>