cqp 3 сар өмнө
parent
commit
696f61fb79

+ 61 - 25
app/Http/Controllers/Api/ScreenController.php

@@ -297,18 +297,53 @@ class ScreenController extends BaseController
             ->where('type',ApplyOrder::type_two)
             ->where('crt_time','>=',$time)
             ->select('quantity','crt_time','data_id')
+            ->orderBy('crt_time','asc')
             ->get()->toArray();
-        $dispatch = DispatchSub::whereIn('id',array_column($data,'data_id'))
-            ->pluck('sale_orders_product_id','id')
-            ->toArray();
-        $sale_order_map = [];
-        $sale_order = SaleOrdersProduct::whereIn('id', array_unique(array_values($dispatch)))
-            ->select('order_quantity','id','crt_time')
-            ->get()
-            ->toArray();
-        foreach ($sale_order as $value){
-            $sale_order_map[$value['id']] = $value;
+        $f = [];
+        foreach ($data as $value){
+            $start_time = strtotime(date("Y-m-d 00:00:00",$value['crt_time']));
+            $end_time = strtotime(date("Y-m-d 23:59:59",$value['crt_time']));
+            $str = $start_time . "|" . $end_time;
+            if(! in_array($str, $f)) $f[] = $str;
+        }
+
+        $data2 = ApplyOrderDetail::where('del_time',0)
+            ->where('type',ApplyOrder::type_one)
+            ->where('crt_time','>=',$time)
+            ->when(! empty($f), function ($query) use ($f) {
+                return $query->where(function ($q) use ($f) {
+                    foreach ($f as $value) {
+                        list($start_time, $end_time) = explode("|", $value);
+                        $q->orWhere(function ($subQ) use ($start_time, $end_time) {
+                            $subQ->where('crt_time', '>=', $start_time)
+                                ->where('crt_time', '<=', $end_time);
+                        });
+                    }
+                });
+            })
+            ->select('quantity','crt_time','data_id')
+            ->get()->toArray();
+        $plan_3 = [];
+        foreach ($data2 as $value) {
+            $date = date("Y-m-d", $value['crt_time']);
+            if (isset($plan_3[$date])) {
+                $quantity = bcadd($plan_3[$date], $value['quantity'], 3);
+                $plan_3[$date] = $quantity;
+            } else {
+                $plan_3[$date] = $value['quantity'];
+            }
         }
+//        $dispatch = DispatchSub::whereIn('id',array_column($data,'data_id'))
+//            ->pluck('sale_orders_product_id','id')
+//            ->toArray();
+//        $sale_order_map = [];
+//        $sale_order = SaleOrdersProduct::whereIn('id', array_unique(array_values($dispatch)))
+//            ->select('order_quantity','id','crt_time')
+//            ->get()
+//            ->toArray();
+//        foreach ($sale_order as $value){
+//            $sale_order_map[$value['id']] = $value;
+//        }
 
         $plan = $plan_2 = [];
         foreach ($data as $value) {
@@ -320,20 +355,21 @@ class ScreenController extends BaseController
                 $return[$date] = $value['quantity'];
             }
 
-            $sale_id = $dispatch[$value['data_id']] ?? 0;
-            $n = $sale_order_map[$sale_id] ?? [];
-            if(! empty($n)){
-                $date_2 = date("Y-m-d", $n['crt_time']);
-                if(isset($plan_2[$date_2]) && in_array($sale_id, $plan_2[$date_2])) continue;
-                if (isset($plan[$date_2])) {
-                    $quantity = bcadd($plan[$date_2], $n['order_quantity'], 3);
-                    $plan[$date_2] = $quantity;
-                } else {
-                    $plan[$date_2] = $n['order_quantity'];
-                }
-                $plan_2[$date_2][] = $sale_id;
-            }
-        }unset($plan_2);
+//            $sale_id = $dispatch[$value['data_id']] ?? 0;
+//            $n = $sale_order_map[$sale_id] ?? [];
+//            if(! empty($n)){
+//                $date_2 = date("Y-m-d", $n['crt_time']);
+//                if(isset($plan_2[$date_2]) && in_array($sale_id, $plan_2[$date_2])) continue;
+//                if (isset($plan[$date_2])) {
+//                    $quantity = bcadd($plan[$date_2], $n['order_quantity'], 3);
+//                    $plan[$date_2] = $quantity;
+//                } else {
+//                    $plan[$date_2] = $n['order_quantity'];
+//                }
+//                $plan_2[$date_2][] = $sale_id;
+//            }
+        }
+        //unset($plan_2);
 
         ksort($return);
 
@@ -342,7 +378,7 @@ class ScreenController extends BaseController
             $tmp = [
                 'time' => $key,
                 'output' => $value,
-                'plan' => $plan[$key] ?? $value,
+                'plan' => $plan_3[$key] ?? $value,
             ];
             $result[] = $tmp;
         }