|
@@ -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
|
+
|