Browse Source

1.用户列表添加角色名称列
2.新粉每月销售额梯形图脚本以及页面方法

shensong 4 years ago
parent
commit
e27b7e4e74

+ 111 - 0
app/Console/Commands/GroupFanAmountByMonth.php

@@ -0,0 +1,111 @@
1
+<?php namespace App\Console\Commands;
2
+
3
+use Illuminate\Console\Command;
4
+use Illuminate\Support\Facades\DB;
5
+use App\Admin;
6
+use App\Distributors;
7
+
8
+class GroupFanAmountByMonth extends Command {
9
+
10
+	/**
11
+	 * The console command name.
12
+	 *
13
+	 * @var string
14
+	 */
15
+	protected $name = 'GroupFanAmountByMonth';
16
+	protected $date;
17
+
18
+	/**
19
+	 * The console command description.
20
+	 *
21
+	 * @var string
22
+	 */
23
+	protected $description = '新粉月销售额梯形图';
24
+
25
+    public function handle()
26
+    {
27
+        try{
28
+            $this->date = date('Y-m-01',strtotime('-1 months'));
29
+            $this->TeamFanAmountByMonth();
30
+        } catch (\Exception $e) {
31
+            echo 'line:'.$e->getLine().' message:'.$e->getMessage();
32
+        }
33
+
34
+    }
35
+
36
+    public function TeamFanAmountByMonth() {
37
+        $team_ids = Admin::getTeams();
38
+        foreach ($team_ids as $team_id) {
39
+            $date = $this->date;
40
+            $date_first = $this->date;
41
+            $date_end = $this->getthemonth($date);
42
+            #团队成员:
43
+            $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
44
+            //第一步: 统计上月数据
45
+            #上月加粉
46
+            $phones = DB::table('customers')->whereIn('admin_id',$saler_ids)->where('fanTime', '>=', $date_first)->where('fanTime', '<=', $date_end)->lists('phone');
47
+            $amount = DB::table('order')->whereIn('receiverMobile', $phones)->where('createTime','>=',$date)->where('createTime', '<=', $date_end. '23:59:59')->where('is_del', 0)->where('team_id', $team_id)->sum('receivedAmount');
48
+            //存入数据表
49
+            $result = $this->updateData($date, $amount, $date, $team_id);
50
+            echo "\nDate:".$date." team_id:".$team_id." amount:".$amount." 处理结果:".$result;
51
+            //第二布:统计往期数据
52
+            #查日期列表
53
+            $dates = DB::table('cust_month_remain')->select('idate')->where('team_id', $team_id)->where('idate','<', $date)->groupBy('idate')->orderBy('idate', 'desc')->get();
54
+            if( !empty($dates) ) {
55
+                $dates = json_decode(json_encode($dates),true);
56
+                foreach ($dates as $k=>&$v) {
57
+                    $idate_first = date('Y-m-01',strtotime($v['idate']));
58
+                    $idate_end = $this->getthemonth($v['idate']);
59
+                    $phones = DB::table('customers')->whereIn('admin_id',$saler_ids)->where('fanTime', '>=', $idate_first)->where('fanTime', '<=', $idate_end)->lists('phone');
60
+                    $amount = DB::table('order')->whereIn('receiverMobile', $phones)->where('createTime','>=', $date_first)->where('createTime', '<', $date_end. '23:59:59')->where('is_del', 0)->where('team_id', $team_id)->sum('receivedAmount');
61
+                    //存入数据表
62
+                    $result = $this->updateData($v['idate'], $amount, $date, $team_id);
63
+                    echo "\nDate:".$v['idate']." team_id:".$team_id." amount:".$amount." 处理结果:".$result;
64
+                }
65
+            }
66
+        }
67
+    }
68
+
69
+    public function updateData($idate, $amount, $date, $team_id){
70
+        #当前年份
71
+        $month = date('Y-01', strtotime($date));
72
+        $data = array(); 
73
+        #如果是上个月的数据直接插入
74
+        if($idate == $date ){
75
+            $data['team_id'] = $team_id;
76
+            $data['idate'] = $idate;
77
+            $data['month_time'] = $month;
78
+            $data['amount_data'] = json_encode([0=>['date'=>$date, 'amount'=>$amount]]);
79
+            $data['create_time'] = date('Y-m-d H:i:s',time());
80
+            $res = DB::table('cust_month_remain')->insert($data);
81
+        }else{
82
+            if(12 < $this->getMonthNum( $idate, $date)){
83
+                return 0;
84
+            }
85
+
86
+            //往期成单
87
+            $odata = DB::table('cust_month_remain')->where('team_id', $team_id)->where('idate', $idate)->first();
88
+            $amount_data = json_decode($odata->amount_data, true);
89
+            $amount_data[] = ['date'=>$date, 'amount'=>$amount];
90
+            $data['amount_data'] = json_encode($amount_data);
91
+            $res = DB::table('cust_month_remain')->where('team_id', $team_id)->where('idate', $idate)->update($data);
92
+        }
93
+
94
+        return $res;
95
+    }
96
+
97
+    public function getthemonth($date)
98
+    {
99
+        $firstday = date('Y-m-01', strtotime($date));
100
+        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
101
+        return $lastday;
102
+    }
103
+
104
+    public function getMonthNum($date1,$date2){
105
+        $date1_stamp=strtotime($date1);
106
+        $date2_stamp=strtotime($date2);
107
+        list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp));
108
+        list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp));
109
+        return abs($date_1['y']-$date_2['y'])*12 +$date_2['m']-$date_1['m'];
110
+    }
111
+}

+ 4 - 0
app/Http/Controllers/Admin/AdminController.php

@@ -36,6 +36,10 @@ class AdminController extends Controller
36 36
             #获取团队
37 37
             $team = DB::table('teams')->select('name')->where('id', $item->team_id)->first();
38 38
             $item->team_name = isset($team->name) ? $team->name : '';
39
+            #获取角色名称
40
+            $adminRoleId = DB::table('admin_role')->where('user_id',$item->id)->pluck('role_id');
41
+            $roleName = DB::table('roles')->where('id',$adminRoleId)->pluck('name');
42
+            $item->role_name = $roleName;
39 43
         }
40 44
         return view('admin/adminall', ['admin' => $admin]);
41 45
     }

+ 87 - 3
app/Http/Controllers/Admin/StatisticsController.php

@@ -4741,11 +4741,11 @@ class StatisticsController extends Controller
4741 4741
             $data['order_amount'] = $order->order_amount;
4742 4742
             $data['cust_count'] = $order->cust_count;
4743 4743
             //加粉
4744
-            $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add');    
4744
+            $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add');
4745 4745
             //毛利 = 总销售额-总商品成本-总运费-总售后
4746 4746
             $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
4747 4747
             $data['roi'] = $data['throw_cost']>0?  round($order->order_amount / $data['throw_cost'], 4) * 100 .'%' : '';
4748
-            //总新粉单数            
4748
+            //总新粉单数
4749 4749
             $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.createTime','>=',$_start)->first();
4750 4750
             $data['new_order_count'] = $new_order->order_count;
4751 4751
             //总老粉单数
@@ -4812,7 +4812,7 @@ class StatisticsController extends Controller
4812 4812
         return view('statistics/dayGrandTotal', ['result' =>$result,
4813 4813
             'page'              =>$page,
4814 4814
             'count'             =>$count,
4815
-            'pages'             =>$pages,                                                                                                   
4815
+            'pages'             =>$pages,
4816 4816
             ]);
4817 4817
     }
4818 4818
 
@@ -5689,5 +5689,89 @@ class StatisticsController extends Controller
5689 5689
         $ret['count'] = $count;
5690 5690
         return $ret;
5691 5691
     }
5692
+
5693
+    public function newFanMonthAmountList(Request $request) {
5694
+
5695
+        $stime = $request->input('stime');
5696
+        $etime = $request->input('etime');
5697
+        $team_id = (int)$request->input('team_id',0);
5698
+
5699
+        $role_id = session('role_id');
5700
+
5701
+        #销售和审核人员只能看自己团队的
5702
+        if($role_id == '3' || $role_id == '4'){
5703
+            $self_id = session('admin_id');
5704
+            $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
5705
+        }
5706
+
5707
+        if($stime == '' && $etime == ''){
5708
+            $stime = date("Y-m", strtotime('-13 month'));
5709
+            $etime = date("Y-m", strtotime('-1 month'));
5710
+        }
5711
+
5712
+        $result = DB::table('cust_month_remain')
5713
+            ->where(function($query) use($stime, $etime, $team_id){
5714
+                if($stime) $query->where('idate','>=', $stime.'-01');
5715
+                if($etime) $query->where('idate','<=', $etime.'-01');
5716
+                if($team_id >0) $query->where('team_id',$team_id);
5717
+            })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
5718
+
5719
+        #获取列
5720
+        $columns = array();
5721
+        $i_max = 13;
5722
+        for($i=0; $i<$i_max; $i++){
5723
+            $columns[] = $i;
5724
+        }
5725
+
5726
+        #数据整合
5727
+        $data = array();
5728
+        foreach($result as $k=>$v){
5729
+            $key = $v->idate;
5730
+            $amount_data = json_decode($v->amount_data, true);
5731
+            $data[$key] = isset($data[$key]) ? array_merge($data[$key], $amount_data) : $amount_data;
5732
+        }
5733
+
5734
+        $saler_ids = null;
5735
+        if( $team_id>0 ){
5736
+            $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
5737
+        }
5738
+
5739
+        #数据格式化
5740
+        $new_data = array();
5741
+        foreach($data as $k=>$v){
5742
+            //当月加粉数
5743
+            $k =date('Y-m',strtotime($k));
5744
+            $end = $this->getthemonth($k);
5745
+            $fan_add = DB::table('cust_day_detail')->where('dtime', '>=', $k)->where('dtime', '<=', $end)->where('is_del',0)->where(function($query) use ($saler_ids) {
5746
+                if( $saler_ids ) $query->whereIn('admin_id',$saler_ids);
5747
+            })->sum('fan_add');
5748
+            $new_data[$k]['fan_add'] = $fan_add;
5749
+            foreach($columns as $val){
5750
+                $n = $val;
5751
+                $new_data[$k]['amount'][] = isset($v[$n]['amount']) ? $v[$n]['amount'] : '';
5752
+            }
5753
+        }
5754
+
5755
+        $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($role_id, $team_id){
5756
+            if($team_id>0 && ($role_id==3 || $role_id==4)) $query->where('id', $team_id);
5757
+        })->where('type', 1)->get();
5758
+        $teamList = json_decode(json_encode($teamList), true);
5759
+
5760
+        return view('statistics/newFanMonthAmountList', [
5761
+            'result' =>$new_data,
5762
+            'columns'=>$columns,
5763
+            'stime'=>$stime,
5764
+            'etime'=>$etime,
5765
+            'team_id'=>$team_id,
5766
+            'teamlist'=>$teamList,
5767
+        ]);
5768
+    }
5769
+
5770
+    public function getthemonth($date)
5771
+    {
5772
+        $firstday = date('Y-m-01', strtotime($date));
5773
+        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
5774
+        return $lastday;
5775
+    }
5692 5776
                                                                                 
5693 5777
 }

+ 2 - 0
resources/views/admin/adminall.blade.php

@@ -18,6 +18,7 @@
18 18
                     <th width="10%">真实姓名</th>
19 19
                     <th width="15%">手机号</th>
20 20
                     <th width="10%">所属团队</th>
21
+                    <th width="10%">角色名称</th>
21 22
                     <th width="10%">微信号</th>
22 23
                     <th width="15%">微信二维码图</th>
23 24
                     <th width="10%">状态</th>
@@ -34,6 +35,7 @@
34 35
                         <td class="text-c">{{$a['realname']}}</td>
35 36
                         <td class="text-c">{{$a['phone']}}</td>
36 37
                         <td class="text-c">{{$a['team_name']}}</td>
38
+                        <td class="text-c">{{$a['role_name']}}</td>
37 39
                         <td class="text-c">{{$a['wx_id']}}</td>
38 40
                         <td class="text-c"><img width=100 src="{{$a['qrcode']}}" /></td>
39 41
                         <td class="text-c">@if($a['is_use'] == 1) 启用 @else 禁用@endif</td>

+ 74 - 0
resources/views/statistics/newFanMonthAmountList.blade.php

@@ -0,0 +1,74 @@
1
+@extends('admin/master')
2
+@section('content')
3
+    <body>
4
+    <div class="page-container">
5
+        <div>
6
+            <div>
7
+                <input class="input-text" style="width:6%;text-align:center" type="text" value="所属团队"/>
8
+                <select style="width:10%;text-align:center" id='team_id' name="team_id">
9
+                    <option value="0" @if($team_id=='') selected @endif>-- 选择团队 --</option>
10
+                    @foreach($teamlist as $v)
11
+                        <option value="{{$v['id']}}" @if($team_id==$v['id']) selected @endif>{{$v['name']}}</option>
12
+                    @endforeach
13
+                </select>
14
+                <input class="input-text" style="width:6%;text-align:center" type="text" value="开始时间"/>
15
+                <input id="stime" type="text" onfocus="WdatePicker({ dateFmt:'yyyy-MM' })" class="input-text Wdate" style="width:12%;text-align:center;margin-left: -5px" name="stime" value="{{$stime?$stime:''}}">
16
+                <input class="input-text" style="width:6%;text-align:center" type="text" value="结束时间"/>
17
+                <input id="etime"type="text" onfocus="WdatePicker({ dateFmt:'yyyy-MM' })" class="input-text Wdate" style="width:12%;text-align:center;margin-left: -5px" name="etime" value="{{$etime?$etime:''}}">
18
+                <a class="btn btn-primary radius"  style="margin-left: 5px" onclick="user_search()" href="javascript:;">搜索</a>
19
+                <!--a class="btn btn-primary radius" onclick="statistics_export()" href="javascript:;"><i class="Hui-iconfont">&#xe600;</i> 导出数据</a-->
20
+
21
+            </div>
22
+        </div>
23
+
24
+        <div class="mt-20">
25
+            <table class="table table-border table-bordered table-bg table-hover table-sort">
26
+                <thead>
27
+                <tr class="text-c">
28
+                    <th>时间</th>
29
+                    <th>加粉数</th>
30
+                    @foreach($columns as $v)
31
+                        <th>@if($v==0) 当月销售额 @else {{$v}}月后销售额@endif</th>
32
+                    @endforeach
33
+                </tr>
34
+                </thead>
35
+                <tbody>
36
+                @if($result)
37
+                    @foreach($result as $k=>$a)
38
+                        <tr class="text-c" style=" text-align:center;">
39
+                            <td>{{$k}}</td>
40
+                            <td>{{$a['fan_add']}}</td>
41
+                            @foreach($a['amount'] as $val)
42
+                                <td>{{$val}}</td>
43
+                            @endforeach
44
+                        </tr>
45
+                    @endforeach
46
+                @endif
47
+                </tbody>
48
+            </table>
49
+        </div>
50
+        <div id="page" class="page_div"></div>
51
+    </div>
52
+
53
+    <!--_footer 作为公共模版分离出去-->
54
+    <script type="text/javascript" src="/admin/lib/jquery/1.9.1/jquery.min.js"></script>
55
+    <script type="text/javascript" src="/admin/lib/layer/2.4/layer.js"></script>
56
+    <script type="text/javascript" src="/admin/static/h-ui/js/H-ui.min.js"></script>
57
+    <script type="text/javascript" src="/admin/static/h-ui.admin/js/H-ui.admin.js"></script>
58
+    <script type="text/javascript" src="/admin/lib/page/paging.js"></script>
59
+    <script type="text/javascript" src="/admin/lib/My97DatePicker/4.8/WdatePicker.js"></script>
60
+    <!--/_footer 作为公共模版分离出去-->
61
+    <!--/_footer 作为公共模版分离出去-->
62
+    <script type="text/javascript">
63
+
64
+        function user_search(){
65
+            var stime = $('#stime').val();
66
+            var etime = $('#etime').val();
67
+            var team_id = $('#team_id').val();
68
+            location.href = 'newFanMonthAmountList?stime='+stime+'&etime='+etime+'&team_id='+team_id;
69
+        }
70
+    </script>
71
+
72
+    </body>
73
+
74
+@endsection