Browse Source

销售roi

sunhao 5 years ago
parent
commit
25d7dff0a5
2 changed files with 108 additions and 0 deletions
  1. 107 0
      app/Console/Commands/RoiSalerTotal.php
  2. 1 0
      app/Console/Kernel.php

+ 107 - 0
app/Console/Commands/RoiSalerTotal.php

@@ -0,0 +1,107 @@
1
+<?php 
2
+namespace App\Console\Commands;
3
+
4
+use Illuminate\Console\Command;
5
+use DB;
6
+use App\CustTotal;
7
+use App\CustDetail;
8
+class RoiSalerTotal extends Command {
9
+
10
+    protected $signature = 'RoiSalerTotal';
11
+
12
+    /**
13
+     * The console command description.
14
+     *
15
+     * @var string
16
+     */
17
+    protected $description = '销售汇总roi';
18
+
19
+
20
+    public function handle()
21
+    {
22
+        $this->RoiSalerTotal();
23
+    }
24
+    public function RoiSalerTotal(){
25
+        $timeArr = array(
26
+            1 => '-7 day',
27
+            2 => '-15 day',
28
+            3 => '-30 day',
29
+            4 => '-45 day',
30
+            5 => '-60 day',
31
+        );
32
+        //获取团队
33
+        $team_ids = $this->getTeams();
34
+        //获取各个团队销售id
35
+        $salerIds = $this->getTeamSalers($team_ids);
36
+        for($i=1;$i<6;$i++){
37
+            $stime = date('Y-m-d', strtotime($timeArr[$i]));   
38
+            $etime = $date = date('Y-m-d');
39
+
40
+            //计算date i天内的roi    
41
+            $custTotal = CustTotal::select('total_cost', 'total_fan_add', 'team_id')->whereIn('team_id', $team_ids)->where('is_del',0)->where('dtime', $stime)->get();
42
+            $custTotal = json_decode(json_encode($custTotal), true);  
43
+
44
+            foreach($custTotal as $k=>$v){
45
+                $saler_ids = $salerIds[$v['team_id']];
46
+                //团队当日销售总加粉
47
+                $wx_fan_total = CustDetail::whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime', $stime)->sum('fan_add');
48
+                //团队销售上报加粉
49
+                $result = CustDetail::select('fan_add', 'admin_id')->whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime', $stime)->get();
50
+                $result = json_decode(json_encode($result), true);
51
+                foreach($result as $key=>$detail){  
52
+
53
+                    $data = array();
54
+                    $data['cost'] = round($detail['fan_add']/$wx_fan_total * $v['total_cost'], 2);
55
+                    $data['gzh_count'] = round($detail['fan_add']/$wx_fan_total * $v['total_fan_add']);
56
+
57
+                    $data['ad_time'] = $stime;  
58
+                    $data['type'] = $i;            
59
+                    //新粉收入
60
+                    //当日加粉
61
+                    $phones = DB::table('customers')->where('fanTime', $stime)->lists('phone');
62
+                    #当日加粉15日订单总计:
63
+                    $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, count(distinct(receiverMobile)) as cust_count'))->whereIn('receiverMobile', $phones)->where('createTime', '>=', $stime)->where('createTime', '<', $etime)->where('is_del', 0)->where('admin_id', $detail['admin_id'])->first();
64
+                    #当日新粉成单:            
65
+                    $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $stime)->where('createTime','<=', $stime.' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where('admin_id', $detail['admin_id'])->first();
66
+                   
67
+                    // 1.当日新粉成单数
68
+                    $data['new_fan_order_count'] = $new_order->order_count;
69
+                    // 2.当日新粉成交额 
70
+                    $data['new_fan_order_amount'] = $new_order->order_amount;
71
+                    // 3.当日粉丝总成交额
72
+                    $data['order_amount'] = $order->order_amount;
73
+                    // 15日总单数
74
+                    $data['order_count'] = $order->order_count;
75
+                    $data['cust_count'] = $order->cust_count;
76
+
77
+                    $data['fan_count'] = $detail['fan_add'];
78
+                    $data['admin_id'] = $detail['admin_id'];
79
+                    //插入数据
80
+                    $res = $this->insertData($data);
81
+                }
82
+            }
83
+        }
84
+    }
85
+
86
+    /**
87
+     * @param idate:统计日期 rate:成单率 date:昨日
88
+     *
89
+     */
90
+    public function insertData($data){        
91
+        return DB::table('roi_saler_total')->insert($data);
92
+    }
93
+
94
+    public function getTeamSalers($team_ids){
95
+        $data = array();
96
+        foreach($team_ids as $team_id){
97
+            $data[$team_id] = DB::table('admin')->where('team_id', $team_id)->lists('id');
98
+        }
99
+        return $data;
100
+    }
101
+
102
+    public function getTeams(){
103
+        $data = DB::table('teams')->where('type', 1)->lists('id');
104
+        return $data;
105
+    }
106
+
107
+}

+ 1 - 0
app/Console/Kernel.php

@@ -43,6 +43,7 @@ class Kernel extends ConsoleKernel {
43 43
         'App\Console\Commands\DayGrandTeamTotal',
44 44
         'App\Console\Commands\DayGrandTeamTotalHistory',
45 45
         'App\Console\Commands\RoiSalerTotalHistory',
46
+        'App\Console\Commands\RoiSalerTotal',
46 47
 
47 48
         
48 49
     ];