sunhao 5 years ago
parent
commit
e360f5fb73
1 changed files with 88 additions and 28 deletions
  1. 88 28
      app/Http/Controllers/Admin/StatisticsController.php

+ 88 - 28
app/Http/Controllers/Admin/StatisticsController.php

@@ -1749,14 +1749,9 @@ class StatisticsController extends Controller
1749 1749
         }
1750 1750
         $offset = ($page-1) * $pageSize;
1751 1751
 
1752
+        $_data = $this->teamCtimePage($stime, $etime, $team_id, $offset, $pageSize);
1752 1753
 
1753
-        $countQuery = Order::select(DB::raw('left(createTime, 10) as ctime, team_id'));        
1754
-        $count = $countQuery->where(function($query) use($team_id, $stime, $etime){
1755
-            if($team_id) $query->where('team_id', $team_id);
1756
-            if($stime ) $query->where('createTime', '>=', $stime);
1757
-            if($etime ) $query->where('createTime', '<=', $etime. ' 23:59:59');
1758
-        })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->get();
1759
-        $count = count($count);
1754
+        $count = $_data['count'];
1760 1755
 
1761 1756
         if ($count > 1) {
1762 1757
             // 总页数
@@ -1768,17 +1763,20 @@ class StatisticsController extends Controller
1768 1763
 
1769 1764
         $teams = DB::table('teams')->lists('name', 'id');
1770 1765
 
1771
-        $result = Order::select(DB::raw('left(createTime, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime){
1772
-            if($team_id) $query->where('team_id', $team_id);
1773
-            if($stime) $query->where('createTime', '>=', $stime);
1774
-            if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
1775
-        })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('createTime','desc')->offset($offset)->limit($pageSize)->get();
1776
-        
1777
-        $result = json_decode(json_encode($result), true);
1778
-        foreach($result as $k=>&$v){
1766
+        foreach($_data['data'] as $k=>&$v){
1767
+            $order_result = Order::select(DB::raw('team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime){
1768
+                if($team_id) $query->where('team_id', $team_id);
1769
+                if($stime) $query->where('createTime', '>=', $stime);
1770
+                if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
1771
+            })->where('is_del', 0)->whereRaw('left(createTime, 10)='.$v['date'])->where('team_id', $v['team_id'])->first();
1772
+
1773
+            $v['order_count'] = isset($order_result->order_count) ? $order_result->order_count : '';
1774
+            $v['receivedAmount'] = isset($order_result->receivedAmount) ? $order_result->receivedAmount : '';
1775
+            $v['wx_money'] = isset($order_result->wx_money) ? $order_result->wx_money : '';
1776
+          
1779 1777
             $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
1780 1778
             #当天售后和退补
1781
-            $ctime = $v['ctime'];
1779
+            $ctime = $v['ctime'] = $v['date'];
1782 1780
             $team = $v['team_id'];
1783 1781
             $aftersale_info = Order::select(DB::raw('count(1) as aftersale_count, sum(aftersale_fee) as aftersale_fee'))->where(function($query) use($team, $ctime){
1784 1782
                 $query->where('team_id', $team);
@@ -1824,12 +1822,13 @@ class StatisticsController extends Controller
1824 1822
 
1825 1823
             #毛利
1826 1824
             $v['profit'] = $v['wx_true_amount'] + $v['zfm_true_amount'] - $v['cost'] - $v['freight_cost'];
1825
+
1827 1826
         }
1828 1827
 
1829 1828
         $teamList = DB::table('teams')->select('id', 'name')->get();
1830 1829
         $teamList = json_decode(json_encode($teamList), true);
1831 1830
         return view('/statistics/teamFinanceTotal',[
1832
-            'result'   =>   $result,
1831
+            'result'   =>   $_data['data'],
1833 1832
             'team_id'   =>   $team_id,
1834 1833
             'stime'   =>   $stime,
1835 1834
             'etime'   =>   $etime,
@@ -1880,26 +1879,31 @@ class StatisticsController extends Controller
1880 1879
         $etime = $request->input('etime');
1881 1880
        
1882 1881
         $teams = DB::table('teams')->lists('name', 'id');
1883
-        $result = Order::select(DB::raw('left(createTime, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(cost) as cost, sum(freight_cost) as freight_cost, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime){
1884
-            if($team_id) $query->where('team_id', $team_id);
1885
-            if($stime) $query->where('createTime', '>=', $stime);
1886
-            if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
1887
-        })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('createTime','desc')->get();
1888
-        
1889
-        $result = json_decode(json_encode($result), true);
1882
+        $result = $this->teamCtimePage_export($stime, $etime, $team_id);
1883
+
1890 1884
         foreach($result as $k=>&$v){
1885
+            $order_result = Order::select(DB::raw('team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime){
1886
+                if($team_id) $query->where('team_id', $team_id);
1887
+                if($stime) $query->where('createTime', '>=', $stime);
1888
+                if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
1889
+            })->where('is_del', 0)->whereRaw('left(createTime, 10)='.$v['date'])->where('team_id', $v['team_id'])->first();
1890
+
1891
+            $v['order_count'] = isset($order_result->order_count) ? $order_result->order_count : '';
1892
+            $v['receivedAmount'] = isset($order_result->receivedAmount) ? $order_result->receivedAmount : '';
1893
+            $v['wx_money'] = isset($order_result->wx_money) ? $order_result->wx_money : '';
1894
+          
1891 1895
             $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
1892 1896
             #当天售后和退补
1893
-            $ctime = $v['ctime'];
1897
+            $ctime = $v['ctime'] = $v['date'];
1894 1898
             $team = $v['team_id'];
1895 1899
             $aftersale_info = Order::select(DB::raw('count(1) as aftersale_count, sum(aftersale_fee) as aftersale_fee'))->where(function($query) use($team, $ctime){
1896 1900
                 $query->where('team_id', $team);
1897 1901
                 $query->where('aftersale_time', '>=', $ctime);
1898 1902
                 $query->where('aftersale_time', '<=', $ctime.' 23:59:59');
1899
-            })->where('is_del', 0)->where('aftersale_fee','>',0)->first();
1903
+            })->where('is_del', 0)->where('aftersale_fee', '>', 0)->first();
1900 1904
             $v['aftersale_count'] = $aftersale_info->aftersale_count;
1901 1905
             $v['aftersale_fee'] = $aftersale_info->aftersale_fee;
1902
-
1906
+            #退补
1903 1907
             $refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where(function($query) use($team, $ctime){
1904 1908
                 $query->where('team_id', $team);
1905 1909
                 $query->where('refund_time', '=', $ctime);
@@ -1936,8 +1940,8 @@ class StatisticsController extends Controller
1936 1940
 
1937 1941
             #毛利
1938 1942
             $v['profit'] = $v['wx_true_amount'] + $v['zfm_true_amount'] - $v['cost'] - $v['freight_cost'];
1939
-        }
1940 1943
 
1944
+        }
1941 1945
 
1942 1946
         $indexKey = ['team_name','ctime','order_count','receivedAmount','wx_money','zfm_money','aftersale_count','aftersale_fee','wx_refund_count','wx_refund_price','wx_true_amount','zfm_refund_count','zfm_refund_price','zfm_true_amount', 'send_count', 'cost', 'freight_cost', 'profit'];
1943 1947
         $title = ['团队', '时间', '总单数', '成交金额', '微信支付金额','付款码支付金额', '售后单数', '售后金额', '微信退补单数', '微信退补差价', '微信实际金额', '付款码退补单数', '付款码退补差价', '付款码实际金额', '发货单数', '货品成本', '物流成本', '毛利'];
@@ -2994,6 +2998,62 @@ class StatisticsController extends Controller
2994 2998
         $filename = 'xiaoshouyj_'.date('Y-m-d_H').'.xlsx';
2995 2999
         return Order::export_excel($result, $filename, $indexKey, $title);
2996 3000
     }
3001
+
3002
+
3003
+    public function teamCtimePage($stime, $etime, $team_id, $offset, $pageSize){
3004
+        $result = array();
3005
+        $sdate = '2019-09-04';
3006
+        $edate = date('Y-m-d');
3007
+        if($stime && $stime>$sdate){
3008
+            $sdate = $stime;
3009
+        }
3010
+        if($etime && $etime<$edate){
3011
+            $edate = $etime;
3012
+        }
3013
+        $teams = [1,2,3,5];
3014
+        if($team_id>0){
3015
+            $teams = [$team_id];
3016
+        }
3017
+        $d = (int)(strtotime($edate) - strtotime($sdate)) / 86400 + 1;
3018
+        for($i=$d-1; $i>=0; $i--){
3019
+            $dtime = $sdate. '+'.$i.' day';
3020
+            $date = date('Y-m-d', strtotime($dtime));
3021
+            foreach($teams as $team_id){
3022
+                $result[] = ['date'=>$date, 'team_id'=>$team_id];
3023
+            }
3024
+        }
3025
+
3026
+        $count = count($result);
3027
+        $ret['data'] = array_slice($result, $offset, $pageSize);
3028
+        $ret['count'] = $count;
3029
+        return $ret;
3030
+    }
3031
+
3032
+    public function teamCtimePage_export($stime, $etime, $team_id){
3033
+        $result = array();
3034
+        $sdate = '2019-09-04';
3035
+        $edate = date('Y-m-d');
3036
+        if($stime && $stime>$sdate){
3037
+            $sdate = $stime;
3038
+        }
3039
+        if($etime && $etime<$edate){
3040
+            $edate = $etime;
3041
+        }
3042
+        $teams = [1,2,3,5];
3043
+        if($team_id>0){
3044
+            $teams = [$team_id];
3045
+        }
3046
+        $d = (int)(strtotime($edate) - strtotime($sdate)) / 86400 + 1;
3047
+        for($i=$d-1; $i>=0; $i--){
3048
+            $dtime = $sdate. '+'.$i.' day';
3049
+            $date = date('Y-m-d', strtotime($dtime));
3050
+            foreach($teams as $team_id){
3051
+                $result[] = ['date'=>$date, 'team_id'=>$team_id];
3052
+            }
3053
+        }
3054
+
3055
+        return $result;
3056
+    }
2997 3057
 }
2998 3058
   
2999 3059