sunhao преди 5 години
родител
ревизия
d7bbb780bc
променени са 3 файла, в които са добавени 250 реда и са изтрити 0 реда
  1. 121 0
      app/Console/Commands/DayGrandSalerTotal.php
  2. 127 0
      app/Console/Commands/DayGrandSalerTotalHistory.php
  3. 2 0
      app/Console/Kernel.php

+ 121 - 0
app/Console/Commands/DayGrandSalerTotal.php

@@ -0,0 +1,121 @@
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
+use App\Order;
9
+class DayGrandSalerTotal extends Command {
10
+
11
+    protected $signature = 'DayGrandSalerTotal';
12
+
13
+    /**
14
+     * The console command description.
15
+     *
16
+     * @var string
17
+     */
18
+    protected $description = '分销售每日汇总投放,成本,毛利等数据';
19
+
20
+
21
+    public function handle()
22
+    {
23
+        $this->DayGrandSalerTotal();
24
+    }
25
+
26
+    
27
+    public function DayGrandSalerTotal(){
28
+        $_start = '2019-09-04';
29
+        $_end = date('Y-m-d');
30
+        
31
+        $idate = date('Y-m-d', strtotime('-1 day'));
32
+        //查看是否已有数据
33
+        $if_e = DB::table('day_grand_saler_total')->where('idate', $idate)->first();
34
+        if(!empty($if_e)){
35
+            echo "\n日期:".$idate." 已存在";
36
+            return false;
37
+        }
38
+        $team_ids = $this->getTeams();
39
+        //获取各个团队销售id
40
+        $salerIds = $this->getTeamSalers($team_ids);
41
+        foreach($salerIds as $team_id=>$saler_ids){
42
+            //团队总投入
43
+            $team_throw_cost = CustTotal::where('is_del',0)->where('team_id',$team_id)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('total_cost');
44
+            //团队销售历史总加粉
45
+            $wx_fan_total = CustDetail::whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('fan_add');
46
+            foreach($saler_ids as $admin_id){
47
+                $data = array();
48
+                $data['idate'] = $idate;
49
+                $data['admin_id'] = $admin_id;
50
+                $data['team_id'] = $team_id;
51
+                //总加粉
52
+                $data['fan_count'] = CustDetail::where('is_del',0)->where('admin_id', $admin_id)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('fan_add');
53
+                //预估总投入                   
54
+                $data['throw_cost'] = round($data['fan_count']/$wx_fan_total * $team_throw_cost, 2); 
55
+
56
+                //订单信息
57
+                $order = Order::select(DB::raw('sum(cost) as goods_cost, sum(freight_cost) as freight_cost, sum(aftersale_fee) as aftersale_cost, count(1) as order_count, sum(receivedAmount) as order_amount, count(distinct(receiverMobile)) as cust_count, sum(refund_price) as refund_fee'))->where('is_del',0)->where('admin_id', $admin_id)->where('createTime','<',$_end)->where('createTime','>=',$_start)->first();
58
+                $data['goods_cost'] = $order->goods_cost;
59
+                $data['freight_cost'] = $order->freight_cost;
60
+                $data['aftersale_cost'] = $order->aftersale_cost;
61
+                $data['refund_fee'] = $order->refund_fee;
62
+                $data['order_count'] = $order->order_count;
63
+                $data['order_amount'] = $order->order_amount;
64
+                $data['cust_count'] = $order->cust_count;
65
+            
66
+                //毛利 = 总销售额-总商品成本-总运费-总售后
67
+                $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
68
+                //总新粉单数            
69
+                $new_order = Order::select(DB::raw('count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('order.admin_id', $admin_id)->where('order.createTime','<',$_end)->where('order.createTime','>=',$_start)->first();
70
+                $data['new_order_count'] = $new_order->order_count;
71
+                //总老粉单数
72
+                $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
73
+                //总复购订单
74
+                $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
75
+
76
+                //7 - 60 roi汇总
77
+                $date = date('Y-m-d', strtotime('-7 day'));
78
+                $data['cost7'] = DB::table('roi_saler_total')->where('type', 1)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('cost');
79
+                $data['order_count7'] = DB::table('roi_saler_total')->where('type', 1)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_count');
80
+                $data['order_amount7'] = DB::table('roi_saler_total')->where('type', 1)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_amount');
81
+
82
+                $date = date('Y-m-d', strtotime('-15 day'));
83
+                $data['cost15'] = DB::table('roi_saler_total')->where('type', 2)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('cost');
84
+                $data['order_count15'] = DB::table('roi_saler_total')->where('type', 2)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_count');
85
+                $data['order_amount15'] = DB::table('roi_saler_total')->where('type', 2)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_amount');
86
+
87
+                $date = date('Y-m-d', strtotime('-30 day'));
88
+                $data['cost30'] = DB::table('roi_saler_total')->where('type', 3)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('cost');
89
+                $data['order_count30'] = DB::table('roi_saler_total')->where('type', 3)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_count');
90
+                $data['order_amount30'] = DB::table('roi_saler_total')->where('type', 3)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_amount');
91
+
92
+                $date = date('Y-m-d', strtotime('-45 day'));
93
+                $data['cost45'] = DB::table('roi_saler_total')->where('type', 4)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('cost');
94
+                $data['order_count45'] = DB::table('roi_saler_total')->where('type', 4)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_count');
95
+                $data['order_amount45'] = DB::table('roi_saler_total')->where('type', 4)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_amount');
96
+
97
+                $date = date('Y-m-d', strtotime('-60 day'));
98
+                $data['cost60'] = DB::table('roi_saler_total')->where('type', 5)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('cost');
99
+                $data['order_count60'] = DB::table('roi_saler_total')->where('type', 5)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_count');
100
+                $data['order_amount60'] = DB::table('roi_saler_total')->where('type', 5)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_amount');
101
+                $res = DB::table('day_grand_saler_total')->insert($data);
102
+                echo "\n日期:".$idate." 团队:".$team_id." 销售:".$admin_id." 插入结果:".$res;
103
+            }
104
+
105
+        }
106
+    }
107
+
108
+    public function getTeamSalers($team_ids){
109
+        $data = array();
110
+        foreach($team_ids as $team_id){
111
+            $data[$team_id] = DB::table('admin')->where('team_id', $team_id)->lists('id');
112
+        }
113
+        return $data;
114
+    }
115
+
116
+    public function getTeams(){
117
+        $data = DB::table('teams')->where('type', 1)->lists('id');
118
+        return $data;
119
+    }
120
+
121
+}

+ 127 - 0
app/Console/Commands/DayGrandSalerTotalHistory.php

@@ -0,0 +1,127 @@
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
+use App\Order;
9
+class DayGrandSalerTotalHistory extends Command {
10
+
11
+    protected $signature = 'DayGrandSalerTotalHistory';
12
+
13
+    /**
14
+     * The console command description.
15
+     *
16
+     * @var string
17
+     */
18
+    protected $description = '分销售每日汇总投放,成本,毛利等数据 历史处理';
19
+
20
+
21
+    public function handle()
22
+    {
23
+        $this->dayGrandSalerTotal();
24
+    }
25
+
26
+    
27
+    public function dayGrandSalerTotal(){
28
+        $_start = '2019-09-04';
29
+        for($i=72;$i>=1;$i--){
30
+            $m = '-'.($i-1).' day';
31
+            $n = '-'.$i.' day';
32
+            $_end = date('Y-m-d', strtotime($m));
33
+            $idate = date('Y-m-d', strtotime($n));
34
+            //查看是否已有数据
35
+            $if_e = DB::table('day_grand_saler_total')->where('idate', $idate)->first();
36
+            if(!empty($if_e)){
37
+                echo "\n日期:".$idate." 已存在";
38
+                return false;
39
+            }
40
+            $team_ids = $this->getTeams();
41
+            //获取各个团队销售id
42
+            $salerIds = $this->getTeamSalers($team_ids);
43
+            foreach($salerIds as $team_id=>$saler_ids){
44
+                //团队总投入
45
+                $team_throw_cost = CustTotal::where('is_del',0)->where('team_id',$team_id)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('total_cost');
46
+                //团队销售历史总加粉
47
+                $wx_fan_total = CustDetail::whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('fan_add');
48
+                foreach($saler_ids as $admin_id){
49
+                    $data = array();
50
+                    $data['idate'] = $idate;
51
+                    $data['admin_id'] = $admin_id;
52
+                    $data['team_id'] = $team_id;
53
+                    //总加粉
54
+                    $data['fan_count'] = CustDetail::where('is_del',0)->where('admin_id', $admin_id)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('fan_add');
55
+                    //预估总投入                   
56
+                    $data['throw_cost'] = round($data['fan_count']/$wx_fan_total * $team_throw_cost, 2); 
57
+                    //订单信息
58
+                    $order = Order::select(DB::raw('sum(cost) as goods_cost, sum(freight_cost) as freight_cost, sum(aftersale_fee) as aftersale_cost, count(1) as order_count, sum(receivedAmount) as order_amount, count(distinct(receiverMobile)) as cust_count, sum(refund_price) as refund_fee'))->where('is_del',0)->where('admin_id', $admin_id)->where('createTime','<',$_end)->where('createTime','>=',$_start)->first();
59
+                    $data['goods_cost'] = $order->goods_cost;
60
+                    $data['freight_cost'] = $order->freight_cost;
61
+                    $data['aftersale_cost'] = $order->aftersale_cost;
62
+                    $data['refund_fee'] = $order->refund_fee;
63
+                    $data['order_count'] = $order->order_count;
64
+                    $data['order_amount'] = $order->order_amount;
65
+                    $data['cust_count'] = $order->cust_count;
66
+                        
67
+                    //毛利 = 总销售额-总商品成本-总运费-总售后
68
+                    $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
69
+                    //总新粉单数            
70
+                    $new_order = Order::select(DB::raw('count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('order.admin_id', $admin_id)->where('order.createTime','<',$_end)->where('order.createTime','>=',$_start)->first();
71
+                    $data['new_order_count'] = $new_order->order_count;
72
+                    //总老粉单数
73
+                    $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
74
+                    //总复购订单
75
+                    $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
76
+
77
+                    //7 - 60 roi汇总
78
+                    $s7 = $idate. ' -7 day';
79
+                    $date = date('Y-m-d', strtotime($s7));
80
+                    $data['cost7'] = DB::table('roi_saler_total')->where('type', 1)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('cost');
81
+                    $data['order_count7'] = DB::table('roi_saler_total')->where('type', 1)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_count');
82
+                    $data['order_amount7'] = DB::table('roi_saler_total')->where('type', 1)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_amount');
83
+
84
+                    $s15 = $idate. ' -15 day';
85
+                    $date = date('Y-m-d', strtotime($s15));
86
+                    $data['cost15'] = DB::table('roi_saler_total')->where('type', 2)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('cost');
87
+                    $data['order_count15'] = DB::table('roi_saler_total')->where('type', 2)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_count');
88
+                    $data['order_amount15'] = DB::table('roi_saler_total')->where('type', 2)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_amount');
89
+
90
+                    $s30 = $idate. ' -30 day';
91
+                    $date = date('Y-m-d', strtotime($s30));
92
+                    $data['cost30'] = DB::table('roi_saler_total')->where('type', 3)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('cost');
93
+                    $data['order_count30'] = DB::table('roi_saler_total')->where('type', 3)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_count');
94
+                    $data['order_amount30'] = DB::table('roi_saler_total')->where('type', 3)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_amount');
95
+
96
+                    $s45 = $idate. ' -45 day';
97
+                    $date = date('Y-m-d', strtotime($s45));
98
+                    $data['cost45'] = DB::table('roi_saler_total')->where('type', 4)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('cost');
99
+                    $data['order_count45'] = DB::table('roi_saler_total')->where('type', 4)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_count');
100
+                    $data['order_amount45'] = DB::table('roi_saler_total')->where('type', 4)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_amount');
101
+
102
+                    $s60 = $idate. ' -60 day';
103
+                    $date = date('Y-m-d', strtotime($s60));
104
+                    $data['cost60'] = DB::table('roi_saler_total')->where('type', 5)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('cost');
105
+                    $data['order_count60'] = DB::table('roi_saler_total')->where('type', 5)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_count');
106
+                    $data['order_amount60'] = DB::table('roi_saler_total')->where('type', 5)->where('admin_id', $admin_id)->where('ad_time', '<', $date)->sum('order_amount');
107
+                    $res = DB::table('day_grand_saler_total')->insert($data);
108
+                    echo "\n日期:".$idate." 团队:".$team_id. " 销售:" .$admin_id." 插入结果:".$res;
109
+                }
110
+            }
111
+        }
112
+    }
113
+
114
+    public function getTeamSalers($team_ids){
115
+        $data = array();
116
+        foreach($team_ids as $team_id){
117
+            $data[$team_id] = DB::table('admin')->where('team_id', $team_id)->lists('id');
118
+        }
119
+        return $data;
120
+    }
121
+
122
+    public function getTeams(){
123
+        $data = DB::table('teams')->where('type', 1)->lists('id');
124
+        return $data;
125
+    }
126
+
127
+}

+ 2 - 0
app/Console/Kernel.php

@@ -44,6 +44,8 @@ class Kernel extends ConsoleKernel {
44 44
         'App\Console\Commands\DayGrandTeamTotalHistory',
45 45
         'App\Console\Commands\RoiSalerTotalHistory',
46 46
         'App\Console\Commands\RoiSalerTotal',
47
+        'App\Console\Commands\DayGrandSalerTotalHistory',
48
+        'App\Console\Commands\DayGrandSalerTotal',
47 49
 
48 50
         
49 51
     ];