cqp 3 ヶ月 前
コミット
f5e91ac0cb

+ 13 - 0
app/Http/Controllers/Api/ApplyOrderController.php

@@ -7,6 +7,19 @@ use Illuminate\Http\Request;
 
 
 class ApplyOrderController extends BaseController
 class ApplyOrderController extends BaseController
 {
 {
+    public function applyOrderReportList(Request $request)
+    {
+        $service = new ApplyOrderService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->applyOrderReportList($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
     public function materialList(Request $request)
     public function materialList(Request $request)
     {
     {
         $service = new ApplyOrderService();
         $service = new ApplyOrderService();

+ 242 - 0
app/Service/ApplyOrderService.php

@@ -884,4 +884,246 @@ class ApplyOrderService extends Service
 
 
         return $number;
         return $number;
     }
     }
+
+    public function applyOrderReportList($data){
+        if(empty($data['apply_time'][0]) || empty($data['apply_time'][1])) return [false, '申请日期必须选择!'];
+        if(empty($data['type'])) return [false, 'TYPE不能为空!'];
+
+        $model = ApplyOrder::where('del_time',0)
+            ->where('apply_time',">=",$data['apply_time'][0])
+            ->where('apply_time',"<=",$data['apply_time'][1])
+            ->where('type',$data['type'])
+            ->select('id','order_number','apply_time','mark','status')
+            ->orderBy('id','desc');
+        if(isset($data['status'])) $model->where('status', $data['status']);
+        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])){
+            $model->where('crt_time',">=",$data['crt_time'][0]);
+            $model->where('crt_time',"<=",$data['crt_time'][1]);
+        }
+        if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
+        if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
+        if(! empty($data['sale_order_number'])){
+            if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
+                $dispatch_id = DispatchSub::where('del_time',0)
+                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
+                    ->select('id')
+                    ->get()->toArray();
+                $dispatch_id = array_column($dispatch_id,'id');
+                $detail = ApplyOrderDetail::where('del_time',0)
+                    ->where('type', $data['type'])
+                    ->whereIn('data_id', $dispatch_id)
+                    ->select('apply_order_id')
+                    ->get()->toArray();
+                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
+            }else{
+                $no = SaleOrdersProduct::where('del_time',0)
+                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
+                    ->select('order_no')
+                    ->get()->toArray();
+                $no = array_unique(array_column($no,'order_no'));
+                $box_id = Box::where('del_time',0)
+                    ->whereIn('top_order_no', $no)
+                    ->select('id')
+                    ->get()->toArray();
+                $box_id = array_column($box_id,'id');
+                $detail = ApplyOrderDetail::where('del_time',0)
+                    ->where('type', $data['type'])
+                    ->whereIn('data_id', $box_id)
+                    ->select('apply_order_id')
+                    ->get()->toArray();
+                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
+            }
+        }
+        if(! empty($data['dispatch_no'])){
+            $dispatch_id = DispatchSub::where('del_time',0)
+                ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
+                ->select('id')
+                ->get()->toArray();
+            $dispatch_id = array_column($dispatch_id,'id');
+            $detail = ApplyOrderDetail::where('del_time',0)
+                ->where('type', $data['type'])
+                ->whereIn('data_id', $dispatch_id)
+                ->select('apply_order_id')
+                ->get()->toArray();
+            $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
+        }
+        $list = $model->get()->toArray();
+        if(empty($list)) return [true, []];
+        $apply_id = array_column($list, 'id');
+        //明细数据
+        $result = ApplyOrderDetail::where('del_time',0)
+            ->whereIn('apply_order_id', $apply_id)
+            ->get()->toArray();
+        if(empty($result)) return [true, []];
+        //组织主表需要的数据
+        $list_map = $this->makeListMap($list, $result, $data['type']);
+
+        $data_id = array_unique(array_column($result, 'data_id'));
+        list($d, $d_no_map) = $this->getDetailList($data_id, $data['type']);
+
+        $return = [];
+        $d_tmp = array_column($d,null,'id');
+        foreach ($result as $t){
+            $tmp = $d_tmp[$t['data_id']] ?? [];
+            $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
+            $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
+            $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
+            $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
+
+            if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
+                $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);
+            }else{
+                $order_no = $tmp['order_no'] ?? "";
+            }
+
+            if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
+                $product_unit = "吨";
+            }elseif($t['type'] == ApplyOrder::type_four){
+                $product_unit = "只";
+            }else{
+                $product_unit = $t['product_unit'] ?? "";
+            }
+
+            $tmp = $list_map[$t['apply_order_id']] ?? [];
+
+            $return[] = [
+                'order_number' => $tmp['order_number'],
+                'sale_order_number' => $tmp['sale_order_number'],
+                'apply_time' => $tmp['apply_time'],
+                'mark' => $tmp['mark'],
+                'status_title' => $tmp['status_title'],
+                'id' => $t['data_id'] ?? 0,
+//                'main_id' => $t['id'],
+                'quantity' => $t['quantity'] ?? 0,
+                'product_no' => $t['product_no'] ?? "",
+                'product_title' => $t['product_title'] ?? "",
+                'product_size' => $t['product_size'] ?? "",
+                'product_unit' => $product_unit,
+                'top_product_title' => $top_product_title,
+                'top_product_no' => $top_product_no,
+                'technology_name' => $technology_name, //颜色
+                'wood_name' => $wood_name,
+                'order_no' => $order_no,
+            ];
+        }
+
+        return [true, array_values($return)];
+    }
+
+    private function getDetailList($apply_id, $type){
+        $d = $d_no_map = [];
+        if($type == ApplyOrder::type_one){
+            $d = DispatchSub::where('del_time',0)
+                ->whereIn('id', $apply_id)
+                ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
+                ->get()->toArray();
+            $args = "";
+            foreach ($d as $value){
+                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
+            }
+            $args = rtrim($args, 'OR ');
+
+            $d_no_map = [];
+            $d_no = DispatchSub::where('del_time',0)
+                ->whereRaw($args)
+                ->select('dispatch_no as order_no','order_product_id','crt_time')
+                ->get()->toArray();
+            foreach ($d_no as $value){
+                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
+                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
+                }else{
+                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
+                }
+            }
+        }elseif ($type == ApplyOrder::type_two){
+            $d = DispatchSub::where('del_time',0)
+                ->whereIn('id', $apply_id)
+                ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
+                ->get()->toArray();
+            $args = "";
+            foreach ($d as $value){
+                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
+            }
+            $args = rtrim($args, 'OR ');
+
+            $d_no_map = [];
+            $d_no = DispatchSub::where('del_time',0)
+                ->whereRaw($args)
+                ->select('dispatch_no as order_no','order_product_id','crt_time')
+                ->get()->toArray();
+            foreach ($d_no as $value){
+                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
+                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
+                }else{
+                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
+                }
+            }
+        }elseif ($type == ApplyOrder::type_three){
+            $d = Box::where('del_time',0)
+                ->whereIn('id', $apply_id)
+                ->select('id', 'order_no')
+                ->get()->toArray();
+        }elseif ($type == ApplyOrder::type_four){
+            $d = Box::where('del_time',0)
+                ->whereIn('id', $apply_id)
+                ->select('id', 'order_no')
+                ->get()->toArray();
+        }
+
+        return [$d, $d_no_map];
+    }
+
+    private function makeListMap($data, $order, $type){
+        if(empty($data)) return [];
+
+        //申请单 所有的数据id
+        $order_map = $data_id = [];
+        foreach ($order as $value){
+            $order_map[$value['apply_order_id']][] = $value['data_id'];
+            $data_id[] = $value['data_id'];
+        }
+        if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
+            $map1 = DispatchSub::whereIn('id', array_unique($data_id))
+                ->pluck('out_order_no','id')
+                ->toArray();
+        }else{
+            $map2 = Box::whereIn('id', array_unique($data_id))
+                ->pluck('top_order_no','id')
+                ->toArray();
+            $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
+                ->pluck('out_order_no','order_no')
+                ->toArray();
+        }
+
+        foreach ($data as $key => $value){
+            $data[$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';
+            $data[$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
+
+            $order_tmp = $order_map[$value['id']] ?? [];
+            $order_tmp = array_unique($order_tmp);
+            $sale_order_number = [];
+            if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
+                foreach ($order_tmp as $v){
+                    $str = $map1[$v] ?? '';
+                    if(empty($str)) continue;
+                    $sale_order_number[] = $str;
+                }
+                $sale_order_number = array_unique($sale_order_number);
+                $sale_order_number = implode(',',$sale_order_number);
+                $data[$key]['sale_order_number'] = $sale_order_number;
+            }else{
+                foreach ($order_tmp as $v){
+                    $m2_t = $map2[$v] ?? '';
+                    $m2_t = $map2_fin[$m2_t] ?? "";
+                    if(empty($m2_t)) continue;
+                    $sale_order_number[] = $m2_t;
+                }
+                $sale_order_number = array_unique($sale_order_number);
+                $sale_order_number = implode(',',$sale_order_number);
+                $data[$key]['sale_order_number'] = $sale_order_number;
+            }
+        }
+
+        return array_column($data,null,'id');
+    }
 }
 }

+ 1 - 0
routes/api.php

@@ -284,6 +284,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('applyOrderEdit', 'Api\ApplyOrderController@materialEdit');
     $route->any('applyOrderEdit', 'Api\ApplyOrderController@materialEdit');
     $route->any('applyOrderAdd', 'Api\ApplyOrderController@materialAdd');
     $route->any('applyOrderAdd', 'Api\ApplyOrderController@materialAdd');
     $route->any('applyOrderDel', 'Api\ApplyOrderController@materialDel');
     $route->any('applyOrderDel', 'Api\ApplyOrderController@materialDel');
+    $route->any('applyOrderReportList', 'Api\ApplyOrderController@applyOrderReportList');
 
 
     //仓库
     //仓库
     $route->any('storeHouseList', 'Api\FyyOrderController@getStorehouseDataFromSqlServer');
     $route->any('storeHouseList', 'Api\FyyOrderController@getStorehouseDataFromSqlServer');