sunhao 5 years ago
parent
commit
47fe0504be

+ 85 - 0
app/Http/Controllers/Admin/StatisticsController.php

@@ -12,6 +12,7 @@ use App\Logs;
12 12
 use App\CustTotal;
13 13
 use App\CustDetail;
14 14
 use App\AdCost;
15
+use App\Order;
15 16
 use Illuminate\Http\Request;
16 17
 use Illuminate\Support\Facades\DB;
17 18
 
@@ -1568,4 +1569,88 @@ class StatisticsController extends Controller
1568 1569
             'total_data'             =>$total_data,          
1569 1570
             ]);
1570 1571
     }
1572
+
1573
+    /**
1574
+     * 导出地域roi报表
1575
+     */
1576
+    public function districtRoi_export(Request $request){
1577
+        $stime = $request->input('stime');
1578
+        $etime = $request->input('etime');
1579
+        $city = $request->input('city');
1580
+
1581
+        if($city !== null){
1582
+            $city = str_replace('市', '', $city);
1583
+        }       
1584
+
1585
+        $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city, ad_time'))->where(function($query) use($stime, $etime, $city){    
1586
+            if($stime) $query->where('ad_time', '>=', $stime);
1587
+            if($etime) $query->where('ad_time', '<=', $etime);
1588
+            if($city) $query->where('city', 'like', '%'.$city.'%');
1589
+        })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->get();
1590
+
1591
+        $result = json_decode(json_encode($result), true);
1592
+
1593
+        foreach($result as $k=>&$v){
1594
+            //新粉收入
1595
+            //当日加粉
1596
+            $city_name = str_replace('市', '', $v['city']);
1597
+            $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
1598
+            #当日加粉订单总计:
1599
+            $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
1600
+            #当日新粉成单:            
1601
+            $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['ad_time'])->where('createTime','<=', $v['ad_time'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->first();
1602
+            // 1.当日新粉成单数
1603
+            $v['new_order_count'] = $new_order->order_count;
1604
+            // 2.当日新粉成交额 
1605
+            $v['new_order_amount'] = $new_order->order_amount;
1606
+            // 3.当日粉丝总成交额
1607
+            $v['order_amount'] = $order->order_amount;
1608
+            // 总单数
1609
+            $v['order_count'] = $order->order_count;
1610
+            //新粉roi
1611
+            $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
1612
+            //累计roi
1613
+            $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
1614
+            $v['new_fan_count'] = count($phones);
1615
+
1616
+        }
1617
+
1618
+        //总计
1619
+        $total_data = [
1620
+            'cost' => 0,
1621
+            'fan_count' => 0,
1622
+            'order_count' => 0,
1623
+            'order_amount' => 0,
1624
+            'roi' => 0,
1625
+            'conversion_times' => 0,
1626
+        ];
1627
+
1628
+        //投入
1629
+        $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){    
1630
+            if($stime) $query->where('ad_time', '>=', $stime);
1631
+            if($etime) $query->where('ad_time', '<=', $etime);
1632
+            if($city) $query->where('city', 'like', '%'.$city.'%');
1633
+        })->first();
1634
+        $total_data['cost'] = $adcost->total_cost;
1635
+        $total_data['conversion_times'] = $adcost->conversion_times;
1636
+        //总新粉
1637
+        $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){    
1638
+            if($stime) $query->where('fanTime', '>=', $stime);
1639
+            if($etime) $query->where('fanTime', '<=', $etime);
1640
+            if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
1641
+        })->lists('phone');
1642
+        $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
1643
+        //总收入
1644
+        $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
1645
+        $total_data['order_count'] = $order->order_count;
1646
+        $total_data['order_amount'] = $order->order_amount;
1647
+        $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
1648
+
1649
+        $indexKey = ['ad_time','city','total_cost','conversion_times','new_fan_count','new_order_count','new_order_amount','new_roi','order_count','order_amount','total_roi'];
1650
+        $title = ['日期', '城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
1651
+        $filename = 'district_roi_'.date('Y-m-d_H').'.xlsx';
1652
+        return Order::export_excel($result, $filename, $indexKey, $title);
1653
+    }
1571 1654
 }
1655
+
1656
+

+ 2 - 0
app/Http/routes.php

@@ -123,6 +123,8 @@ Route::group(['prefix' => 'admin'], function(){
123 123
         Route::get('/statistics/orderDistrict', 'Admin\StatisticsController@orderDistrict');
124 124
         //地域roi
125 125
         Route::get('/statistics/districtRoi', 'Admin\StatisticsController@districtRoi');
126
+        //地域roi导出
127
+        Route::get('/statistics/districtRoi_export', 'Admin\StatisticsController@districtRoi_export');
126 128
 
127 129
     });
128 130
     

+ 42 - 0
app/Order.php

@@ -8,6 +8,8 @@
8 8
 
9 9
 namespace App;
10 10
 use Illuminate\Database\Eloquent\Model;
11
+use PHPExcel;
12
+use PHPExcel_Writer_Excel2007;
11 13
 
12 14
 class Order extends Model
13 15
 {
@@ -72,4 +74,44 @@ class Order extends Model
72 74
 		$order_sn = rand(100, 9999). substr(time(), 5, 5). rand(10, 9999);
73 75
 		return $order_sn;
74 76
 	} 
77
+
78
+	/**
79
+     * 导出excel
80
+     * @param $data
81
+     * @param string 
82
+     */
83
+    public static function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
84
+
85
+        if( !is_array($indexKey)) return false;            
86
+        $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');  
87
+        //初始化PHPExcel()  
88
+        $objPHPExcel = new PHPExcel();            
89
+        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);    
90
+            
91
+        //接下来就是写数据到表格里面去  
92
+        $objActSheet = $objPHPExcel->getActiveSheet();  
93
+        foreach($title as $k=>$item){
94
+            $objActSheet->setCellValue($header_arr[$k].'1',$item);
95
+        }
96
+        $startRow = 2;  
97
+        foreach ($data as $row) {  
98
+            foreach ($indexKey as $key => $value){  
99
+                //这里是设置单元格的内容  
100
+                $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);  
101
+            }  
102
+            $startRow++;  
103
+        } 
104
+
105
+        header("Pragma: public");  
106
+        header("Expires: 0");  
107
+        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  
108
+        header("Content-Type:application/force-download");  
109
+        header("Content-Type:application/vnd.ms-execl");  
110
+        header("Content-Type:application/octet-stream");  
111
+        header("Content-Type:application/download");;  
112
+        header('Content-Disposition:attachment;filename='.$filename.'');  
113
+        header("Content-Transfer-Encoding:binary");  
114
+        $objWriter->save('php://output');  
115
+        exit();
116
+    }
75 117
 }

+ 1 - 1
resources/views/statistics/districtRoi.blade.php

@@ -13,7 +13,7 @@
13 13
                
14 14
                 
15 15
                 <a class="btn btn-primary radius"  style="margin-left: 5px" onclick="user_search()" href="javascript:;">搜索</a>
16
-                <!--a class="btn btn-primary radius" onclick="statistics_export()" href="javascript:;"><i class="Hui-iconfont">&#xe600;</i> 导出数据</a-->
16
+                <a class="btn btn-primary radius" onclick="statistics_export()" href="javascript:;"><i class="Hui-iconfont"></i> 导出数据</a>
17 17
                 
18 18
             </div>
19 19
         </div>