ChenWuJie 4 years ago
parent
commit
90625d3032
1 changed files with 88 additions and 40 deletions
  1. 88 40
      app/Http/Controllers/Api/mini/DataController.php

+ 88 - 40
app/Http/Controllers/Api/mini/DataController.php

xqd xqd
@@ -6,6 +6,7 @@ use App\Models\Device;
 use App\Models\DeviceName;
 use App\Models\Order;
 use App\Models\OrderDevice;
+use App\Models\OrderOverviewModel;
 use App\Models\Project;
 use App\Models\RentType;
 use App\Models\Spec;
@@ -254,57 +255,104 @@ class DataController extends BaseController
 
     public function getDetailData(Request $request)
     {
-        $order_ids = $this->getOrderIds($request);
-        if(!$order_ids) return $this->error(['msg' => '']);
-
-        $order_devices = OrderDevice::whereIn('order_id', $order_ids)->orderBy('created_at')->get();
+//        $order_ids = $this->getOrderIds($request);
+//        if(!$order_ids) return $this->error(['msg' => '']);
+//        $order_devices = OrderDevice::whereIn('order_id', $order_ids)->orderBy('created_at')->get();
+//        $devices = Device::all();
+//
+//        $dates = [];
+//        // year|month
+//        $type = $request->input('type') ? $request->input('type') : 'year';
+//        $columns = [];
+//        $date = '';
+//        $values = [
+//            ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')]
+//        ];
+//        foreach($order_devices as $item) {
+//            if($this->inDate($dates, $item->created_at, $type)) {
+//                foreach($devices as $key => $device) {
+//                    $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
+//                    $columns[$key] = $columns[$key] + $money;
+//                }
+//            } else {
+//                $date = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
+//                array_push($dates, $date);
+//                if(count($columns) > 0) {
+//                    array_push($values, [
+//                        'date' => $date,
+//                        'total' => collect($columns)->sum() / 100,
+//                        'data' => $columns
+//                    ]);
+//                }
+//                $columns = [];
+//                foreach($devices as $device) {
+//                    $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
+//                    array_push($columns, $money);
+//                }
+//            }
+//        }
+//        if(count($columns) > 0) {
+//            array_push($values, [
+//                'date' => $date,
+//                'total' => collect($columns)->sum(),
+//                'data' => $columns
+//            ]);
+//        }
+//        return $this->success(['data' => $values]);
+
+
+        $date = $this->getStartAt($request->input('date'));
+        $year = date('Y',strtotime($date));
+        $project_id = $request->input('project_ids');
+        $project_id = $project_id[0];
         $devices = Device::all();
-
-        $dates = [];
-        // year|month
-        $type = $request->input('type') ? $request->input('type') : 'year';
-        $columns = [];
-        $date = '';
-        $values = [
-            ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')]
+        $data = [
+            ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')->toArray()]
         ];
-        foreach($order_devices as $item) {
-            if($this->inDate($dates, $item->created_at, $type)) {
-                foreach($devices as $key => $device) {
-                    $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
-                    $columns[$key] = $columns[$key] + $money;
-                }
-            } else {
-                $date = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
-                array_push($dates, $date);
-                if(count($columns) > 0) {
-                    array_push($values, [
-                        'date' => $date,
-                        'total' => collect($columns)->sum() / 100,
-                        'data' => $columns
-                    ]);
+        $items = OrderOverviewModel::whereYear('date',$year)->where('project_id',$project_id)->get()->toArray();
+        foreach ($items as &$item)
+        {
+            $arr = [
+                'data'=>[],
+            ];
+            $arr_data = $devices->pluck('id')->toArray();
+            $arr['total'] = $item['total_price']/100;
+            $arr['date'] = date('Y-m',strtotime($item['date']));
+            //获取到月份的开始时间和结束时间
+            $start_at = $item['date'];
+            $end_at = $this->getEndAt(date('Y-m',strtotime($item['date'])));
+            //获取到订单数组
+            $order_arr = Order::where('project_id',$project_id)
+                ->where('status',3)
+                ->where('type',1)
+                ->whereBetween('updated_at',[$start_at,$end_at])
+                ->pluck('id')->toArray();
+            $order_details = OrderDevice::whereIn('order_id',$order_arr)->get(['quantity','price','device_id'])->groupBy('device_id')->toArray();
+            foreach ($order_details as  $key=>&$detail)
+            {
+                $total_nums = 0;
+                foreach ($detail as $detail_value)
+                {
+                    $total_nums += ($detail_value['price']*$detail_value['quantity'])/100;
                 }
-                $columns = [];
-                foreach($devices as $device) {
-                    $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
-                    array_push($columns, $money);
+                $device_data = $arr_data;
+                //重置键值数组
+                foreach ($device_data as $k=>&$device_datum)
+                {
+                    $device_datum = 0;
                 }
+                $device_data[$key-1] = $total_nums;
+                $arr['data']=$device_data;
             }
+            array_push($data,$arr);
         }
-        if(count($columns) > 0) {
-            array_push($values, [
-                'date' => $date,
-                'total' => collect($columns)->sum(),
-                'data' => $columns
-            ]);
-        }
-        return $this->success(['data' => $values]);
+        return $this->success(['data' => $data]);
     }
 
     public function getTotalInfo(Request $request)
     {
         $start_at = $this->getStartAt($request->input('date'));
-        $end_at = $this->getStartAt($request->input('date'));
+        $end_at = $this->getEndAt($request->input('date'));
         $total_money = $this->order->whereIn('project_id', $request->input('project_ids'))->sum('money') / 100;
         $month_money = $this->order->whereIn('project_id', $request->input('project_ids'))->where([
             ['created_at', '>=', $start_at],