فهرست منبع

版本大修改

cqp 4 ماه پیش
والد
کامیت
70df99a05c

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

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

+ 2 - 0
app/Model/ApplyOrder.php

@@ -20,12 +20,14 @@ class ApplyOrder extends Model
     const type_two = 2;
     const type_three = 3;
     const type_four = 4;
+    const type_five = 5;
 
     public static $type_name = [
         self::type_one => '领料申请单',
         self::type_two => '完工入库申请单',
         self::type_three => '包装入库申请单',
         self::type_four => '包装领料申请单',
+        self::type_five => '质检入库申请单',
     ];
 
     const state_zero = 0;

+ 3 - 0
app/Model/ReportWorking.php

@@ -18,4 +18,7 @@ class ReportWorking extends Model
         self::state_zero => '未审核',
         self::state_one => '已审核',
     ];
+
+    const type_one = 1; // 重新入生产流程
+    const type_two = 2; // 重新入仓库
 }

+ 1063 - 24
app/Service/ApplyOrderService.php

@@ -6,15 +6,19 @@ use App\Model\ApplyOrder;
 use App\Model\ApplyOrderDetail;
 use App\Model\Box;
 use App\Model\BoxDetail;
+use App\Model\Dispatch;
 use App\Model\DispatchSub;
 use App\Model\Employee;
 use App\Model\EmployeeTeamPermission;
 use App\Model\InOutRecord;
+use App\Model\OrdersProduct;
 use App\Model\Process;
 use App\Model\ReportWorking;
 use App\Model\ReportWorkingDetail;
 use App\Model\SaleOrdersProduct;
+use App\Model\ScrappCount;
 use App\Model\Team;
+use App\Service\Box\BoxService;
 use Illuminate\Support\Facades\DB;
 
 class ApplyOrderService extends Service
@@ -196,6 +200,18 @@ class ApplyOrderService extends Service
                     ->select('apply_order_id')
                     ->get()->toArray();
                 $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
+            }elseif($data['type'] == ApplyOrder::type_five){
+                $zj_id = ScrappCount::where('del_time',0)
+                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
+                    ->select('id')
+                    ->get()->toArray();
+                $zj_id = array_column($zj_id,'id');
+                $detail = ApplyOrderDetail::where('del_time',0)
+                    ->where('type', $data['type'])
+                    ->whereIn('data_id', $zj_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'].'%')
@@ -229,10 +245,12 @@ class ApplyOrderService extends Service
             ->pluck('emp_name','id')
             ->toArray();
 
-        $array1 = $array2 = [];
+        $array1 = $array2 = $array3 = [];
         foreach ($data['data'] as $value){
             if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
                 $array1[] = $value['id'];
+            }elseif ($value['type'] == ApplyOrder::type_five){
+                $array3[] = $value['id'];
             }else{
                 $array2[] = $value['id'];
             }
@@ -245,7 +263,7 @@ class ApplyOrderService extends Service
         foreach ($order1 as $value){
             $order1_map[$value['apply_order_id']][] = $value['data_id'];
         }
-        $order2 = ApplyOrderDetail::whereNotIn('type',[ApplyOrder::type_one,ApplyOrder::type_two])
+        $order2 = ApplyOrderDetail::whereNotIn('type',[ApplyOrder::type_one,ApplyOrder::type_two,ApplyOrder::type_five])
             ->whereIn('apply_order_id', $array2)
             ->select('data_id','apply_order_id')
             ->get()->toArray();
@@ -253,6 +271,14 @@ class ApplyOrderService extends Service
         foreach ($order2 as $value){
             $order2_map[$value['apply_order_id']][] = $value['data_id'];
         }
+        $order3 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_five])
+            ->whereIn('apply_order_id', $array3)
+            ->select('data_id','apply_order_id')
+            ->get()->toArray();
+        $order3_map = [];
+        foreach ($order3 as $value){
+            $order3_map[$value['apply_order_id']][] = $value['data_id'];
+        }
         $map1 = DispatchSub::whereIn('id', array_unique(array_column($order1,'data_id')))
             ->pluck('out_order_no','id')
             ->toArray();
@@ -262,8 +288,12 @@ class ApplyOrderService extends Service
         $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
             ->pluck('out_order_no','order_no')
             ->toArray();
+        $map3 = ScrappCount::whereIn('id', array_unique(array_column($order3,'data_id')))
+            ->pluck('out_order_no','id')
+            ->toArray();
         foreach ($data['data'] as $key => $value){
-            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
+            $data['data'][$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';
             $data['data'][$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
             $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
             $data['data'][$key]['apply_name'] = $emp[$value['apply_id']] ?? '';
@@ -279,6 +309,18 @@ class ApplyOrderService extends Service
                 $sale_order_number = array_unique($sale_order_number);
                 $sale_order_number = implode(',',$sale_order_number);
                 $data['data'][$key]['sale_order_number'] = $sale_order_number;
+            }elseif ($value['type'] == ApplyOrder::type_five){
+                $order_tmp = $order3_map[$value['id']] ?? [];
+                $order_tmp = array_unique($order_tmp);
+                $sale_order_number = [];
+                foreach ($order_tmp as $v){
+                    $str = $map3[$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['data'][$key]['sale_order_number'] = $sale_order_number;
             }else{
                 $order_tmp = $order2_map[$value['id']] ?? [];
                 $order_tmp = array_unique($order_tmp);
@@ -306,6 +348,7 @@ class ApplyOrderService extends Service
             ->first();
         if(empty($detail)) return [false,'申请单不存在或已被删除'];
         $detail = $detail->toArray();
+        $detail['apply_time'] = $detail['apply_time'] ? date('Y-m-d',$detail['apply_time']) : '';
         $detail['apply_title'] = Employee::where('id', $detail['apply_id'])->value('emp_name');
         $apply_d = ApplyOrderDetail::where('del_time', 0)
             ->where('apply_order_id', $id)
@@ -369,6 +412,11 @@ class ApplyOrderService extends Service
                 ->whereIn('id', $apply_id)
                 ->select('id', 'order_no')
                 ->get()->toArray();
+        }elseif ($detail['type'] == ApplyOrder::type_five){ //质检入库申请
+            $d = ScrappCount::where('del_time',0)
+                ->whereIn('id', $apply_id)
+                ->select('id', 'order_number as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
+                ->get()->toArray();
         }
         $d_tmp = array_column($d,null,'id');
 
@@ -450,6 +498,12 @@ class ApplyOrderService extends Service
                 ->select('id', 'order_no', 'status')
                 ->get()->toArray();
             if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
+        }elseif ($data['type'] == ApplyOrder::type_five){
+            $scrapp = ScrappCount::where('del_time',0)
+                ->whereIn('id', $id)
+                ->select('id', 'order_no', 'status')
+                ->get()->toArray();
+            if(count($scrapp) != count($id)) return [false, '质检单信息错误,请重新选择'];
         }else{
             return [false, '申请单类型错误'];
         }
@@ -463,14 +517,17 @@ class ApplyOrderService extends Service
             $apply = ApplyOrder::where('id', $data['id'])->where('del_time', 0)->first();
             if(empty($apply)) return [false, '申请单不存在或已被删除'];
             if($apply->status != 0) return [false, '申请单已审核,编辑失败'];
-
         }
 
         return [true,''];
     }
 
-    public function setOrderNO($type = ""){
-        $str = date('Ymd',time());
+    public function setOrderNO($type = "",$time = 0){
+        if(empty($time)){
+            $str = date('Ymd',time());
+        }else{
+            $str = date('Ymd',$time);
+        }
 
         $order_number = ApplyOrder::where('order_number','Like','%'. $str . '%')
             ->where('type', $type)
@@ -493,7 +550,7 @@ class ApplyOrderService extends Service
         return $number;
     }
 
-    public function createSQ($data, $user, $type = 0, $status = 1){
+    public function createSQ($data, $user, $type = 0, $status = 1,$time = 0){
         if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];
 
         if($type == ApplyOrder::type_one){
@@ -505,7 +562,7 @@ class ApplyOrderService extends Service
         }elseif ($type == ApplyOrder::type_three){
             $storehouse_id = "002";
             $storehouse_title = "产成品仓";
-        }else{
+        }elseif($type == ApplyOrder::type_four){
             $storehouse_id = "003";
             $storehouse_title = "包材、辅料仓" ;
         }
@@ -513,15 +570,16 @@ class ApplyOrderService extends Service
         try{
             DB::beginTransaction();
 
-            $order_number = $this->setOrderNO($type);
+            $order_number = $this->setOrderNO($type, $time);
 //            list($status,$msg) = $this->limitingSendRequestBackg("spAdd" . $order_number . $type);
 //            if(! $status) return [false, $msg];
 
-            $time = time();
+            if(empty($time)) $time = time();
+
             $model = new ApplyOrder();
             $model->order_number = $order_number;
             $model->apply_id = $user['id'];
-            $model->apply_time = time();
+            $model->apply_time = $time;
             $model->storehouse_id = $storehouse_id;
             $model->storehouse_title = $storehouse_title;
             $model->type = $type;
@@ -559,8 +617,59 @@ class ApplyOrderService extends Service
         return [true, $id];
     }
 
+    public function createSqFive($data, $user, $type = 0, $status = 1, $time = 0){
+        if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];
+
+        try{
+            DB::beginTransaction();
+
+            if(empty($time)) $time = time();
+
+            $first = $data[0] ?? [];
+            $order_number = $this->setOrderNO($type, $time);
+            $model = new ApplyOrder();
+            $model->order_number = $order_number;
+            $model->apply_id = $user['id'];
+            $model->apply_time = $time;
+            $model->storehouse_id = $first['storehouse_id'];
+            $model->storehouse_title = $first['storehouse_title'];
+            $model->type = $type;
+            $model->status = $status; //是否审核
+            $model->save();
+            $id = $model->id;
+
+            $insert = [];
+            foreach ($data as $value){
+                $insert[] = [
+                    'apply_order_id' => $id,
+                    'data_id' => $value['id'],
+                    'quantity' => $value['quantity'] ?? 0,
+                    'product_no' => $value['product_no'] ?? "",
+                    'product_title' => $value['product_title'] ?? "",
+                    'product_size' => $value['product_size'] ?? "",
+                    'product_unit' => $value['product_unit'] ?? "",
+                    'technology_name' => $value['technology_name'] ?? "",
+                    'wood_name' => $value['wood_name'] ?? "",
+                    'top_product_no' => $value['top_product_no'] ?? "",
+                    'top_product_title' => $value['top_product_title'] ?? "",
+                    'type' => $type,
+                    'storehouse_id' => $value['storehouse_id'],
+                    'crt_time' => $time,
+                ];
+            }
+            ApplyOrderDetail::insert($insert);
+
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            return [false,$e->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
     //流水
-    public function createRecord($id){
+    public function createRecord($id){return [true, ''];
         $record = ApplyOrderDetail::where('apply_order_id', $id)
             ->where('del_time',0)
             ->get()->toArray();
@@ -601,7 +710,7 @@ class ApplyOrderService extends Service
 
     //报工
     public function reportWorkingEdit($data, $user){
-        list($status,$msg) = $this->reportWorkingRule($data,false);
+        list($status,$msg) = $this->reportWorkingRule($data,$user,false);
         if(!$status) return [$status,$msg];
 
         DB::beginTransaction();
@@ -611,6 +720,7 @@ class ApplyOrderService extends Service
             $model = $model->where('id',$data['id'])->first();
             $model->report_time = $data['report_time'];
             $model->mark = $data['mark']?? "";
+            $model->status = $data['auto'] ?? 0;
             $model->save();
             $id = $model->id;
 
@@ -629,13 +739,25 @@ class ApplyOrderService extends Service
             $detail_insert = [];
             foreach ($data['order_data'] as $v){
                 $t = $team_man[$v['team_id']] ?? [];
-                foreach ($t as $t_v){
+                if(! empty($t)){
+                    foreach ($t as $t_v){
+                        $detail_insert[] = [
+                            'report_working_id' => $id,
+                            'data_id' => $v['id'],
+                            'quantity' => $v['quantity'] ?? 0,
+                            'process_id' => $v['process_id'] ?? 0,
+                            'finished_id' => $t_v,
+                            'team_id' => $v['team_id'],
+                            'crt_time' => $time,
+                        ];
+                    }
+                }else{
                     $detail_insert[] = [
                         'report_working_id' => $id,
                         'data_id' => $v['id'],
                         'quantity' => $v['quantity'] ?? 0,
                         'process_id' => $v['process_id'] ?? 0,
-                        'finished_id' => $t_v,
+                        'finished_id' => 0,
                         'team_id' => $v['team_id'],
                         'crt_time' => $time,
                     ];
@@ -653,7 +775,7 @@ class ApplyOrderService extends Service
     }
 
     public function reportWorkingAdd($data,$user){
-        list($status,$msg) = $this->reportWorkingRule($data);
+        list($status,$msg) = $this->reportWorkingRule($data,$user);
         if(!$status) return [$status,$msg];
 
         DB::beginTransaction();
@@ -664,6 +786,7 @@ class ApplyOrderService extends Service
             $model->report_time = $data['report_time'];
             $model->mark = $data['mark']?? "";
             $model->crt_id = $user['id'];
+            $model->status = $data['auto'] ?? 0;
             $model->save();
             $id = $model->id;
 
@@ -678,13 +801,25 @@ class ApplyOrderService extends Service
             $detail_insert = [];
             foreach ($data['order_data'] as $v){
                 $t = $team_man[$v['team_id']] ?? [];
-                foreach ($t as $t_v){
+                if(! empty($t)){
+                    foreach ($t as $t_v){
+                        $detail_insert[] = [
+                            'report_working_id' => $id,
+                            'data_id' => $v['id'],
+                            'quantity' => $v['quantity'] ?? 0,
+                            'process_id' => $v['process_id'] ?? 0,
+                            'finished_id' => $t_v,
+                            'team_id' => $v['team_id'],
+                            'crt_time' => $time,
+                        ];
+                    }
+                }else{
                     $detail_insert[] = [
                         'report_working_id' => $id,
                         'data_id' => $v['id'],
                         'quantity' => $v['quantity'] ?? 0,
                         'process_id' => $v['process_id'] ?? 0,
-                        'finished_id' => $t_v,
+                        'finished_id' => 0,
                         'team_id' => $v['team_id'],
                         'crt_time' => $time,
                     ];
@@ -792,11 +927,31 @@ class ApplyOrderService extends Service
             ->pluck('emp_name','id')
             ->toArray();
 
+        $detail = ReportWorkingDetail::where('del_time',0)
+            ->whereIn('report_working_id', array_column($data['data'],'id'))
+            ->select('data_id','report_working_id')
+            ->get()->toArray();
+        $map = [];
+        foreach ($detail as $value){
+            $map[$value['data_id']] = $value['report_working_id'];
+        }
+        $dispatch = DispatchSub::whereIn('id',array_column($detail,'data_id'))
+            ->get()->toArray();
+        $map1 = [];
+        $team_map = Team::where("del_time",0)->pluck('title','id')->toArray();
+        $process_map = Process::where("del_time",0)->pluck('title','id')->toArray();
+        foreach ($dispatch as $value){
+            $report_working_id = $map[$value['id']] ?? 0;
+            $value['team_name'] = $team_map[$value['team_id']] ?? "";
+            $value['process_name'] = $process_map[$value['process_id']] ?? "";
+            $map1[$report_working_id][] = $value;
+        }
         foreach ($data['data'] as $key => $value){
-            $data['data'][$key]['report_time'] = $value['report_time'] ? date('Y-m-d H:i:s',$value['report_time']) : '';
-            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
+            $data['data'][$key]['report_time'] = $value['report_time'] ? date('Y-m-d',$value['report_time']) : '';
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
             $data['data'][$key]['status_title'] = ReportWorking::$state_name[$value['status']] ?? "";
             $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
+            $data['data'][$key]['dispatch'] = $map1[$value['id']] ?? [];
         }
 
         return $data;
@@ -810,6 +965,8 @@ class ApplyOrderService extends Service
             ->first();
         if(empty($detail)) return [false,'报工单不存在或已被删除'];
         $detail = $detail->toArray();
+        $detail['report_time'] = $detail['report_time'] ? date('Y-m-d',$detail['report_time']) : '';
+
         $apply_d = ReportWorkingDetail::where('del_time', 0)
             ->where('report_working_id', $id)
             ->get()->toArray();
@@ -856,7 +1013,7 @@ class ApplyOrderService extends Service
         return [true, $detail];
     }
 
-    public function reportWorkingRule(&$data,$is_add = true){
+    public function reportWorkingRule(&$data,$user, $is_add = true){
         if($this->isEmpty($data,'report_time')) return [false,'报工时间不能为空'];
         if(empty($data['order_data'])) return [false, '报工单详细信息不能为空'];
         foreach ($data['order_data'] as $value){
@@ -869,9 +1026,11 @@ class ApplyOrderService extends Service
         $id = array_unique(array_column($data['order_data'],'id'));
         $dispatch = DispatchSub::where('del_time',0)
             ->whereIn('id', $id)
-            ->select('id', 'dispatch_no', 'status')
+            ->select('id', 'dispatch_no', 'status', 'dispatch_quantity','order_product_id')
             ->get()->toArray();
         if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
+        $count = array_unique(array_column($dispatch,'order_product_id'));
+        if(count($count) > 1) return [false, '批量报工时请选择同一生产订单下的派工单'];
 
         if($is_add){
             $data['order_number'] = $this->setOrderNO2();
@@ -885,11 +1044,55 @@ class ApplyOrderService extends Service
 
         }
 
+        list($status, $msg) = $this->compareQuantity($dispatch, $data);
+        if(! $status) return [false, $msg];
+
+        $em = new EmployeeService();
+        $data['auto'] = $em->is_auto($user, "wgbg_auto");
+
         return [true,''];
     }
 
-    public function setOrderNO2(){
-        $str = date('Ymd',time());
+    public function compareQuantity($dispatch, $data){
+        $id = $data['id'] ?? 0;
+
+        $result = ReportWorkingDetail::where('del_time',0)
+            ->when(! empty($id), function ($query) use ($id){
+                return $query->where('report_working_id', '<>', $id);
+            })
+            ->where('data_id', array_column($dispatch, 'id'))
+            ->select('data_id as dispatch_id','quantity')
+            ->get()
+            ->toArray();
+        $map1 = [];
+        foreach ($result as $value){
+            if(isset($map1[$value['dispatch_id']])){
+                $tmp = bcadd($map1[$value['dispatch_id']], $value['quantity'], 3);
+                $map1[$value['dispatch_id']] = $tmp;
+            }else{
+                $map1[$value['dispatch_id']] = $value['quantity'];
+            }
+        }
+        $map2 = array_column($dispatch,null,'id');
+
+        foreach ($data['order_data'] as $value){
+            $tmp = $map2[$value['id']] ?? [];
+            $total = $tmp['dispatch_quantity'] ?? 0;
+            $dispatch_no = $tmp['dispatch_no'] ?? "";
+            $save = $map1[$value['id']] ?? 0;
+            $save_total = bcadd($value['quantity'], $save, 2);
+            if($save_total > $total) return [false, "派工单号:" . $dispatch_no . "的报工总数量不能超过" . $total . "吨,目前累计报工" . $save . "吨"];
+        }
+
+        return [true, ''];
+    }
+
+    public function setOrderNO2($time = 0){
+        if(empty($time)) {
+            $str = date('Ymd',time());
+        }else{
+            $str = date('Ymd', $time);
+        }
 
         $model = new ReportWorking();
         $order_number = $model->where('order_number','Like','%'. $str . '%')
@@ -942,6 +1145,18 @@ class ApplyOrderService extends Service
                     ->select('apply_order_id')
                     ->get()->toArray();
                 $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
+            }elseif($data['type'] == ApplyOrder::type_five){
+                $zj_id = ScrappCount::where('del_time',0)
+                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
+                    ->select('id')
+                    ->get()->toArray();
+                $zj_id = array_column($zj_id,'id');
+                $detail = ApplyOrderDetail::where('del_time',0)
+                    ->where('type', $data['type'])
+                    ->whereIn('data_id', $zj_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'].'%')
@@ -1096,6 +1311,11 @@ class ApplyOrderService extends Service
                 ->whereIn('id', $apply_id)
                 ->select('id', 'order_no')
                 ->get()->toArray();
+        }elseif($type == ApplyOrder::type_five){
+            $d = ScrappCount::where('del_time',0)
+                ->whereIn('id', $apply_id)
+                ->select('id', 'order_number as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
+                ->get()->toArray();
         }
 
         return [$d, $d_no_map];
@@ -1114,6 +1334,10 @@ class ApplyOrderService extends Service
             $map1 = DispatchSub::whereIn('id', array_unique($data_id))
                 ->pluck('out_order_no','id')
                 ->toArray();
+        }elseif($type == ApplyOrder::type_five){
+            $map3 = ScrappCount::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')
@@ -1139,6 +1363,15 @@ class ApplyOrderService extends Service
                 $sale_order_number = array_unique($sale_order_number);
                 $sale_order_number = implode(',',$sale_order_number);
                 $data[$key]['sale_order_number'] = $sale_order_number;
+            }elseif ($type == ApplyOrder::type_five){
+                foreach ($order_tmp as $v){
+                    $str = $map3[$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] ?? '';
@@ -1154,4 +1387,810 @@ class ApplyOrderService extends Service
 
         return array_column($data,null,'id');
     }
+
+    public function editZj($data, $user){
+       list($status, $msg) = $this->editZjRule($data);
+        if(! $status) return [false, $msg];
+
+        $order_map = array_column($msg,null,'data_id');
+        try{
+            DB::beginTransaction();
+            $time = time();
+
+            $insert = [];
+            $max_id = $this->getDailyId();
+
+            list($status,$msg) = $this->limitingSendRequestBackg("quantity_id_" . $max_id);
+            if(! $status) return [false,$msg];
+
+            $get_order_number = $insert_wg = [];
+            foreach ($data['data'] as $value){
+                $order_t = $order_map[$value['data_id']] ?? [];
+                $order = $order_t['dispatch'] ?? [];
+                if(empty($value['waste'])){
+                    $max_id += 1;
+                    $order_number = date("Ymd") . str_pad($max_id,3,'0',STR_PAD_LEFT);
+                    $get_order_number[] = $order_number;
+                    $insert[] = [
+                        'sale_orders_product_id' => $order['sale_orders_product_id'],
+                        'order_product_id' => $order['order_product_id'],
+                        'dispatch_sub_id' => $order['id'],
+                        'out_order_no' => $order['out_order_no'],
+                        'order_no' => $order['order_no'],
+                        'customer_no' => $order['customer_no'],
+                        'customer_name' => $order['customer_name'],
+                        'product_no' => $order['product_no'],
+                        'product_title' => $order['product_title'],
+                        'product_size' => $order['product_size'],
+                        'product_unit' => $order['product_unit'],
+                        'technology_material' => $order['technology_material'],
+                        'technology_name' => $order['technology_name'],
+                        'wood_name' => $order['wood_name'],
+                        'price' => $order['price'],
+                        'process_mark' => $order['process_mark'],
+                        'table_body_mark' => $order['table_body_mark'],
+                        'table_header_mark' => $order['table_header_mark'],
+                        'crt_id' => $user['id'],
+                        'crt_time' => $time,
+                        'scrapp_num' => 0,
+                        'scrapp_id' => 0,
+                        'team_id' => $data['team_id'],
+                        'finished_id' => $user['id'],
+                        'equipment_id' => $data['equipment_id'],
+                        'order_number' => $order_number,
+                        'process_id' => $order['process_id'],
+                        'quantity' => $value['num'],
+                        'report_id' => $data['id'],
+                        'result' => 0,
+                        'warehouse_no' => "",
+                        'warehouse_title' => "",
+                        'daily_id' => $max_id,
+                    ];
+                }else{
+                    foreach ($value['waste'] as $v){
+                        $max_id += 1;
+                        $order_number = date("Ymd") . str_pad($max_id,3,'0',STR_PAD_LEFT);
+                        $get_order_number[] = $order_number;
+                        $insert[] = [
+                            'sale_orders_product_id' => $order['sale_orders_product_id'],
+                            'order_product_id' => $order['order_product_id'],
+                            'dispatch_sub_id' => $order['id'],
+                            'out_order_no' => $order['out_order_no'],
+                            'order_no' => $order['order_no'],
+                            'customer_no' => $order['customer_no'],
+                            'customer_name' => $order['customer_name'],
+                            'product_no' => $order['product_no'],
+                            'product_title' => $order['product_title'],
+                            'product_size' => $order['product_size'],
+                            'product_unit' => $order['product_unit'],
+                            'technology_material' => $order['technology_material'],
+                            'technology_name' => $order['technology_name'],
+                            'wood_name' => $order['wood_name'],
+                            'price' => $order['price'],
+                            'process_mark' => $order['process_mark'],
+                            'table_body_mark' => $order['table_body_mark'],
+                            'table_header_mark' => $order['table_header_mark'],
+                            'crt_id' => $user['id'],
+                            'crt_time' => $time,
+                            'scrapp_num' => $v['num'],
+                            'scrapp_id' => $v['scrapp_id'],
+                            'team_id' => $data['team_id'],
+                            'finished_id' => $user['id'],
+                            'equipment_id' => $data['equipment_id'],
+                            'order_number' => $order_number,
+                            'process_id' => $order['process_id'],
+                            'quantity' => $value['num'],
+                            'report_id' => $data['id'],
+                            'result' => $v['result'],
+                            'warehouse_no' => $v['warehouse_no'],
+                            'warehouse_title' => $v['warehouse_title'],
+                            'daily_id' => $max_id,
+                        ];
+                    }
+                }
+
+                if($value['num'] > 0) $insert_wg[] = [
+                    'dispatch_id' => $value['data_id'],
+                    'num' => $value['num']
+                ];
+            }
+
+            //质检单 不良品单 会记录报工单的id
+            if(! empty($insert)) ScrappCount::insert($insert);
+            //获取新增的单据
+            $result = ScrappCount::whereIn('order_number',$get_order_number)
+                ->select('scrapp_num as num','id','dispatch_sub_id','warehouse_no','warehouse_title')
+                ->get()->toArray();
+
+            //完工入库申请单 半成品
+            list($status, $msg) = $this->insertWg($insert_wg, $user);
+            if(! $status) return [false, $msg];
+
+            //获取写入的数据 写其它的业务逻辑
+            list($status, $msg) = $this->insertOther($result, $user, $time);
+            if(! $status) return [false, $msg];
+
+            //反写 根据质检单上的派工单id 反写上游单据
+            $this->writeDispatchQuantity(array_column($result,'dispatch_sub_id'));
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getMessage() . '|' . $exception->getLine()];
+        }
+
+        return [true, ''];
+    }
+
+    public function editZjRule($data){
+        if(empty($data['id'])) return [false, '报工单ID不能为空'];
+        $order = ReportWorkingDetail::where('del_time',0)
+            ->where('report_working_id', $data['id'])
+            ->get()->toArray();
+        if(empty($order)) return [false, "报工单信息不存在或已被删除"];
+        $map = [];
+        foreach ($order as $value){
+            if(! isset($map[$value['data_id']])) $map[$value['data_id']] = $value['quantity'];
+        }
+
+        if(empty($data['team_id'])) return [false, '质检班组不能为空'];
+        if(empty($data['equipment_id'])) return [false, '设备不能为空'];
+        if(empty($data['data'])) return [false, '质检信息不能为空'];
+
+        $data_id = [];
+        foreach ($data['data'] as $value){
+            if(empty($value['data_id'])) return [false, 'dataID不能为空'];
+            if(! isset($value['num'])) return [false, 'num不能为空'];
+            $num = 0;
+            if(! empty($value['waste'])){
+                foreach ($value['waste'] as $v){
+                    if(empty($v['num'])) return [false, "质检数量不能为空"];
+                    if(empty($v['scrapp_id'])) return [false, "请选择质检原因"];
+                    if(empty($v['result'])) return [false, '请选择质检产品处理结果'];
+                    if($v['result'] == ReportWorking::type_two && (empty($v['warehouse_no']) || empty($v['warehouse_title']))) return [false, '请选择产品去向仓库'];
+                    $num = bcadd($v['num'], $num, 3);
+                }
+            }
+            $total = bcadd($num, $value['num'],3);
+            $total2 = $map[$value['data_id']] ?? 0;
+            if($total > $total2) return [false, '总数量不能超过报工数量'];
+
+            $data_id[] = $value['data_id'];
+        }
+
+        $dispatch = DispatchSub::whereIn('id',$data_id)->get()->toArray();
+        $dispatch_map = array_column($dispatch,null,'id');
+        foreach ($order as $key => $value){
+            if(! in_array($value['data_id'], $data_id)){
+                unset($order[$key]);
+                continue;
+            }
+            $order[$key]['dispatch'] = $dispatch_map[$value['data_id']] ?? [];
+        }
+
+        return [true, array_values($order)];
+    }
+
+    public function getDailyId($time = 0)
+    {
+        if(empty($time)) $time = time();
+
+        $today_start = strtotime(date("Y-m-d 00:00:00",$time));
+        $today_end = strtotime(date("Y-m-d 23:59:59",$time));
+
+        // 获取今天的最大daily_id
+        $maxDailyId = ScrappCount::where('crt_time', '>=', $today_start)
+            ->where('crt_time', '<=', $today_end)
+            ->max(DB::raw('daily_id + 0'));
+
+        return $maxDailyId ?? 0;
+    }
+
+    public function insertWg($all_data, $user){
+        if(empty($all_data)) return [true, ''];
+        $dispatchList = DispatchSub::whereIn('id', array_column($all_data,'dispatch_id'))->get()->toArray();
+
+        //是否最后一道工序
+        $orders = OrdersProduct::whereIn('id', array_unique(array_column($dispatchList, 'order_product_id')))
+            ->select('id','process_id')
+            ->get()->toArray();
+        $map = [];
+        foreach ($orders as $value){
+            $p = explode(",", $value['process_id']);
+            $lastElement = end($p);
+            $map[$value['id']] = $lastElement;
+        }
+
+        $all_data_map = array_column($all_data,null,'dispatch_id');
+        $need_insert = [];
+        foreach ($dispatchList as $value){
+            $num = $all_data_map[$value['id']] ?? 0;
+            if(! $num) continue;
+            if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id']){
+                $tmp = [
+                    'id' => $value['id'],
+                    'quantity' => $num,
+                    'product_no' => $value['product_no'] ?? "",
+                    'product_title' => $value['product_title'] ?? "",
+                    'product_size' => $value['product_size'] ?? "",
+                    'product_unit' => $value['product_unit'] ?? "",
+                ];
+                $need_insert[] = $tmp;
+            }
+        }
+
+        //生成完工入库申请单
+        $service = new BoxService();
+        list($status, $msg) = $service->createWGSQ($need_insert, $user);
+        if(! $status) return [false, $msg];
+
+        return [true, ''];
+    }
+
+    public function insertOther($all_data, $user, $time){
+        try {
+            $result = [];
+            foreach ($all_data as $value){
+                if($value['result'] > 0) $result[] = $value;
+            }
+            if(empty($result)) return [true, ''];
+            $dispatchList = DispatchSub::whereIn('id', array_column($result,'dispatch_sub_id'))->get()->toArray();
+            $dispatch_map = array_column($dispatchList,null,'id');
+
+            //是否自动审核
+            $em = new EmployeeService();
+            $auto = $em->is_auto($user, "pg_auto");
+
+            $dispatch_no = (new DispatchService())->setOrderNO();
+            if(! $dispatch_no) return [false,'返工派工单号生成失败!'];
+            $prefix = substr($dispatch_no, 0, -3);// "20250611"
+            $startNum = (int)substr($dispatch_no, -3); // 7
+            $is_first = true;
+
+            $dispatch_main = $dispatch = $is_need_ll = $is_need_warehouse = [];
+            foreach ($result as $value){
+                //派工单
+                $t = $dispatch_map[$value['dispatch_sub_id']] ?? [];
+
+                if(! $is_first){
+                    $startNum += 1;
+                    $number = str_pad($startNum,3,'0',STR_PAD_LEFT);
+                    $number = $prefix . $number;
+                    $dispatch_no_t = $number;
+                }else{
+                    $dispatch_no_t = $dispatch_no;
+                    $is_first = false;
+                }
+
+                if($value['result'] == ReportWorking::type_one){
+                    //重新入生产流程 需要生成返工派工单
+                }else{
+                    //入仓库 那么需要生成质检入库申请单 生成领料申请单 生成返工派工单
+                    //'id','dispatch_quantity as quantity',"product_no","order_product_id","product_no",'technology_name','crt_time'
+                    $is_need_ll[] = $dispatch_no_t;
+                    $is_need_warehouse[] = [
+                        'warehouse_no' => $value['warehouse_no'],
+                        'warehouse_title' => $value['warehouse_title'],
+                    ];
+                }
+
+                $dispatch_main[] = ['dispatch_no' => $dispatch_no_t,'crt_time' => $time];
+                $dispatch[] = [
+                    'order_product_id' => $t['order_product_id'],
+                    'order_no' => $t['order_no'],
+                    'product_no' => $t['product_no'],
+                    'product_title' => $t['product_title'],
+                    'product_size' => $t['product_size'],
+                    'product_unit' => $t['product_unit'],
+                    'production_quantity' => $t['production_quantity'],
+                    'dispatch_quantity' => $value['num'],
+                    'technology_material' => $t['technology_material'],
+                    'technology_name' => $t['technology_name'],
+                    'wood_name' => $t['wood_name'],
+                    'process_mark' => $t['process_mark'],
+                    'table_body_mark' => $t['table_body_mark'],
+                    'table_header_mark' => $t['table_header_mark'],
+                    'crt_time' => $time,
+                    'status' => $auto,
+                    'dispatch_no' => $dispatch_no_t,
+                    'process_id' => $t['process_id'],
+                    'dispatch_time_start' => $t['dispatch_time_start'],
+                    'dispatch_time_end' => $t['dispatch_time_end'],
+                    'crt_id' => $user['id'],
+                    'sale_orders_product_id' => $t['sale_orders_product_id'],
+                    'out_order_no' => $t['out_order_no'],
+                    'out_order_no_time' => $t['out_order_no_time'],
+                    'price' => $t['price'],
+                    'customer_no' => $t['customer_no'],
+                    'customer_name' => $t['customer_name'],
+                    'pre_shipment_time' => $t['pre_shipment_time'],
+                    'team_id' => $t['team_id'],
+                    'device_id' => $t['device_id'],
+                    'type' => 1,
+                ];
+            }
+
+            Dispatch::insert($dispatch_main);
+            DispatchSub::insert($dispatch);
+
+            if(! empty($is_need_ll)){
+                //获取上一次插入订单的所有数据
+                $insert = DispatchSub::whereIn('dispatch_no', $is_need_ll)
+                    ->where('crt_time',$time)
+                    ->where('crt_id',$user['id'])
+                    ->select('id','dispatch_quantity as quantity',"product_no")
+                    ->get()->toArray();
+
+                //领料申请单
+                list($status, $msg) = (new DispatchService())->createSQ($insert, $user);
+                if(! $status) return [false, $msg];
+
+                //质检入库申请单 原材料
+                list($status, $msg) = $this->createZJSQ($insert, $user, $is_need_warehouse);
+                if(! $status) return [false, $msg];
+            }
+
+            return [true, ''];
+        }catch (\Throwable $exception){
+            return [false, $exception->getMessage()];
+        }
+    }
+
+    public function createZJSQ($insert_data, $user, $warehouse, $time = 0){
+        try {
+            DB::beginTransaction();
+
+            //是否自动审核质检入库申请单
+            $em = new EmployeeService();
+            $auto = $em->is_auto($user, "zjsq_auto");
+
+            $return = [];
+            $product_no = array_unique(array_column($insert_data,'product_no'));
+            //获取原料
+            $service = new FyyOrderService();
+            list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);
+            if($status) $return = $msg;
+
+            //组织原材料写入数据
+            $insert = [];
+            foreach ($insert_data as $key => $value){
+                $quantity = 0;
+                if(! empty($value['quantity'])) {
+                    $quantity = $value['quantity'];
+                }elseif (! empty($value['dispatch_quantity'])){
+                    $quantity = $value['dispatch_quantity'];
+                }
+                $t = $return[$value['product_no']] ?? [];
+                $tmp = [
+                    'id' => $value['id'],
+                    'quantity' => $quantity,
+                    'product_no' => "",
+                    'product_title' => "",
+                    'product_size' => "",
+                    'product_unit' => "",
+                    'storehouse_id' => $warehouse[$key]['warehouse_no'] ?? "",
+                    'storehouse_title' => $warehouse[$key]['warehouse_title'] ?? "",
+                ];
+                if(! empty($t)){
+                    foreach ($t as $v){
+                        $tmp['product_no'] = $v['product_no'];
+                        $tmp['product_title'] = $v['product_title'];
+                        $tmp['product_size'] = $v['product_size'];
+                        $tmp['product_unit'] = $v['product_unit'];
+                        $insert[] = $tmp;
+                    }
+                }else{
+                    $tmp['quantity'] = 0;
+                    $insert[] = $tmp;
+                }
+            }
+
+            //生成质检入库申请单
+            $service = new ApplyOrderService();
+            list($status, $msg) = $service->createSqFive($insert, $user, ApplyOrder::type_five, $auto, $time);
+            if(! $status) {
+                DB::rollBack();
+                return [false, $msg];
+            }
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
+        }
+
+        return [true, ''];
+    }
+
+    //反写
+    public function writeDispatchQuantity($dispatch_id){
+        $result = ScrappCount::where('del_time',0)
+            ->whereIn('dispatch_sub_id', $dispatch_id)
+            ->select('dispatch_sub_id','quantity','scrapp_num','crt_time','process_id','order_product_id')
+            ->get()->toArray();
+
+        //是否最后一道工序
+        $orders = OrdersProduct::whereIn('id', array_unique(array_column($result, 'order_product_id')))
+            ->select('id','process_id')
+            ->get()->toArray();
+        $map = [];
+        foreach ($orders as $value){
+            $p = explode(",", $value['process_id']);
+            $lastElement = end($p);
+            $map[$value['id']] = $lastElement;
+        }
+
+        //汇总这个派工单一共质检良品数量 但是同一时间下的 只取一次
+        $update = $flag = $need_update = [];
+        foreach ($result as $value){
+            if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id'] && ! in_array($value['order_product_id'], $need_update)){
+                $need_update[] = [
+                    'order_product_id' => $value['order_product_id'],
+                    'process_id' => $value['process_id'],
+                ];
+            }
+            $key = $value['dispatch_sub_id'] . $value['crt_time'];
+            if(isset($flag[$key])) continue;
+
+            if(isset($update[$value['dispatch_sub_id']])){
+                $tmp = bcadd($value['quantity'], $update[$value['dispatch_sub_id']], 3);
+                $update[$value['dispatch_sub_id']] = $tmp;
+            }else{
+                $update[$value['dispatch_sub_id']] = $value['quantity'];
+            }
+            $flag[$key] = 1;
+        }
+        unset($flag);
+
+        //更新派工单完工数量----------------------------------------------------
+        foreach ($update as $id => $num){
+            DispatchSub::where('id', $id)->update([
+                'finished_num' => $num
+            ]);
+        }
+
+        if(! empty($need_update)){
+            //更新生产订单的完工数量--------------------------------------------------
+            $list = DispatchSub::where("del_time",0)
+                ->when(! empty($need_update), function ($query) use ($need_update) {
+                    return $query->where(function ($q) use ($need_update) {
+                        foreach ($need_update as $value) {
+                            $order_product_id = $value['order_product_id'];
+                            $process_id = $value['process_id'];
+                            $q->orWhere(function ($subQ) use ($order_product_id, $process_id) {
+                                $subQ->where('order_product_id', $order_product_id)
+                                    ->where('process_id', $process_id);
+                            });
+                        }
+                    });
+                })
+                ->select('order_product_id', 'finished_num', 'sale_orders_product_id')
+                ->get()->toArray();
+            $map2 = [];
+            foreach ($list as $value){
+                if(isset($map2[$value['order_product_id']])){
+                    $tmp = bcadd($map2[$value['order_product_id']], $value['finished_num'], 3);
+                    $map2[$value['order_product_id']] = $tmp;
+                }else{
+                    $map2[$value['order_product_id']] = $value['finished_num'];
+                }
+            }
+            foreach ($map2 as $id => $num){
+                OrdersProduct::where('id', $id)->update([
+                    'finished_num' => $num
+                ]);
+            }
+
+            //更新销售订单完工数量-------------------------------------------------------
+            $list2 = OrdersProduct::where('del_time',0)
+                ->whereIn('sale_orders_product_id', array_unique(array_column($list, 'sale_orders_product_id')))
+                ->select('finished_num','sale_orders_product_id')
+                ->get()->toArray();
+            $map3 = [];
+            foreach ($list2 as $value){
+                if(isset($map3[$value['sale_orders_product_id']])){
+                    $tmp = bcadd($map3[$value['sale_orders_product_id']], $value['finished_num'], 3);
+                    $map3[$value['sale_orders_product_id']] = $tmp;
+                }else{
+                    $map3[$value['sale_orders_product_id']] = $value['finished_num'];
+                }
+            }
+            foreach ($map3 as $id => $num){
+                SaleOrdersProduct::where('id', $id)->update([
+                    'finished_num' => $num
+                ]);
+            }
+        }
+    }
+
+    public function orderSearchList($data){
+        if(empty($data['type'])) return [false, '单据联查类型不能为空'];
+        if(empty($data['data_id'])) return [false, '依据单据ID不能为空'];
+        //不管进来的是什么单据id 转换为销售订单
+        $sales_id = $this->changeIdToOrder($data);
+        list($status, $msg) = $this->forSearch($data, $sales_id);
+
+        return [$status, $msg];
+    }
+
+    private function changeIdToOrder($data)
+    {
+        $id = $data['id'];
+        $search_id = $result = [];
+        if ($data['type'] == 1) {
+            $result[] = [
+                'id' => $id
+            ];
+        } elseif ($data['type'] == 2) {
+            $result = OrdersProduct::where('del_time', 0)
+                ->where('id', $id)
+                ->select('sale_orders_product_id as id')
+                ->get()->toArray();
+        } elseif ($data['type'] == 3) {
+            $result = DispatchSub::where('del_time', 0)
+                ->where('id', $data['data_id'])
+                ->select('sale_orders_product_id as id')
+                ->get()->toArray();
+        } elseif ($data['type'] == 4) {
+            $detail = ReportWorkingDetail::where('data_id', $id)
+                ->where('del_time', 0)
+                ->select('data_id')
+                ->get()->toArray();
+            $result = DispatchSub::where('del_time', 0)
+                ->whereIn('id', array_unique(array_column($detail, 'data_id')))
+                ->select('sale_orders_product_id as id')
+                ->get()->toArray();
+        } elseif ($data['type'] == 5) {
+            $result = DispatchSub::where('del_time', 0)
+                ->where('id', $id)
+                ->where('finished_num', '>', 0)
+                ->select('sale_orders_product_id as id')
+                ->get()->toArray();
+        } elseif ($data['type'] == 6) {
+            $first = Box::where('del_time', 0)
+                ->where('id', $id)
+                ->select("top_order_no", 'order_no')
+                ->first();
+            if (!empty($first)) {
+                $first = $first->toArray();
+                $box_detail = new BoxDetail(['channel' => $first['top_order_no']]);
+                $result = $box_detail->where('order_no', $first['order_no'])->selct('top_id as id')->get()->toArray();
+            }
+        } elseif ($data['type'] == 7) {
+            $result = ScrappCount::where('del_time', 0)
+                ->where('id', $id)
+                ->select('sale_orders_product_id as id')
+                ->get()->toArray();
+        } elseif ($data['type'] == 8) {
+            $result = ScrappCount::where('del_time', 0)
+                ->where('id', $id)
+                ->where('scrapp_num', '>', 0)
+                ->select('sale_orders_product_id as id')
+                ->get()->toArray();
+        } elseif ($data['type'] == 9) {
+            $detail = ApplyOrderDetail::where('del_time', 0)
+                ->where('apply_order_id', $id)
+                ->where('type', ApplyOrder::type_one)
+                ->select('data_id as dispatch_id')
+                ->get()->toArray();
+            $result = DispatchSub::where('del_time', 0)
+                ->where('id', array_unique(array_column($detail, 'dispatch_id')))
+                ->select('sale_orders_product_id as id')
+                ->get()->toArray();
+        } elseif ($data['type'] == 10) {
+            $detail = ApplyOrderDetail::where('del_time', 0)
+                ->where('apply_order_id', $id)
+                ->where('type', ApplyOrder::type_two)
+                ->select('data_id as dispatch_id')
+                ->get()->toArray();
+            $result = DispatchSub::where('del_time', 0)
+                ->where('id', array_unique(array_column($detail, 'dispatch_id')))
+                ->select('sale_orders_product_id as id')
+                ->get()->toArray();
+        } elseif ($data['type'] == 11) {
+            $detail = ApplyOrderDetail::where('del_time', 0)
+                ->where('apply_order_id', $id)
+                ->where('type', ApplyOrder::type_four)
+                ->select('data_id as box_id')
+                ->get()->toArray();
+            $first = Box::where('del_time', 0)
+                ->whereIn('id', array_unique(array_column($detail, 'box_id')))
+                ->select("top_order_no", 'order_no')
+                ->get()->toArray();
+            if (!empty($first)) {
+                foreach ($first as $value) {
+                    $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);
+                    $t = $box_detail->where('order_no', $first['order_no'])->selct('top_id as id')->get()->toArray();
+                    $result = array_merge($result, $t);
+                }
+            }
+        } elseif ($data['type'] == 12) {
+            $detail = ApplyOrderDetail::where('del_time', 0)
+                ->where('apply_order_id', $id)
+                ->where('type', ApplyOrder::type_three)
+                ->select('data_id as box_id')
+                ->get()->toArray();
+            $first = Box::where('del_time', 0)
+                ->whereIn('id', array_unique(array_column($detail, 'box_id')))
+                ->select("top_order_no", 'order_no')
+                ->get()->toArray();
+            if (!empty($first)) {
+                foreach ($first as $value) {
+                    $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);
+                    $t = $box_detail->where('order_no', $first['order_no'])->selct('top_id as id')->get()->toArray();
+                    $result = array_merge($result, $t);
+                }
+            }
+        } elseif ($data['type'] == 13) {
+            $detail = ApplyOrderDetail::where('del_time', 0)
+                ->where('apply_order_id', $id)
+                ->where('type', ApplyOrder::type_five)
+                ->select('data_id as zj_id')
+                ->get()->toArray();
+            $result = ScrappCount::where('del_time', 0)
+                ->whereIn('id', array_unique(array_column($detail, 'zj_id')))
+                ->select('sale_orders_product_id as id')
+                ->get()->toArray();
+        }
+        foreach ($result as $value) {
+            if (!in_array($value['id'], $search_id)) {
+                $search_id[] = $value['id'];
+            }
+        }
+
+        return $search_id;
+
+    }
+
+    private function forSearch($data, $sales_id){
+        $result = [];
+        //通过销售订单查找所有业务单据
+        if($data['for_type'] == 1){
+            $result = SaleOrdersProduct::where('del_time',0)
+                ->whereIn('id', $sales_id)
+                ->select('id', 'out_order_no as order_no')
+                ->get()->toArray();
+        }elseif($data['for_type'] == 2){
+            $result = OrdersProduct::where('del_time',0)
+                ->whereIn('sale_orders_product_id', $sales_id)
+                ->select('id', 'production_no as order_no')
+                ->get()->toArray();
+        }elseif($data['for_type'] == 3){
+            $result = DispatchSub::where('del_time',0)
+                ->whereIn('sale_orders_product_id', $sales_id)
+                ->select('id', 'dispatch_no as order_no')
+                ->get()->toArray();
+        }elseif($data['for_type'] == 4){
+            $dispatch = DispatchSub::where('del_time',0)
+                ->whereIn('sale_orders_product_id', $sales_id)
+                ->select('id')
+                ->get()->toArray();
+            $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
+                ->where('del_time',0)
+                ->select('data_id')
+                ->get()->toArray();
+            $result = ReportWorking::where('del_time',0)
+                ->whereIn('id', array_unique(array_column($detail,'data_id')))
+                ->select('id', 'order_number as order_no')
+                ->get()->toArray();
+        }elseif($data['for_type'] == 5){
+            $result = DispatchSub::where('del_time',0)
+                ->whereIn('sale_orders_product_id', $sales_id)
+                ->where('finished_num','>',0)
+                ->select('id', 'dispatch_no as order_no')
+                ->get()->toArray();
+        }elseif($data['for_type'] == 6){
+            $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();
+
+            foreach ($sale as $value){
+                $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
+                $box_detail_order = $box_detail->where('top_id',$data['data_id'])->selct('order_no')->get()->toArray();
+                $tmp = Box::where('del_time',0)
+                    ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
+                    ->select("id","order_no")
+                    ->get()->toArray();
+                $result = array_merge($result, $tmp);
+            }
+        }elseif ($data['for_type'] == 7){
+            $result = ScrappCount::where('del_time',0)
+                ->whereIn('sale_orders_product_id', $sales_id)
+                ->select('id', 'order_number as order_no')
+                ->get()->toArray();
+        }elseif ($data['for_type'] == 8){
+            $result = ScrappCount::where('del_time',0)
+                ->whereIn('sale_orders_product_id', $sales_id)
+                ->where('scrapp_num','>', 0)
+                ->select('id', 'order_number as order_no')
+                ->get()->toArray();
+        }elseif ($data['for_type'] == 9){
+            $dispatch = DispatchSub::where('del_time',0)
+                ->whereIn('sale_orders_product_id', $sales_id)
+                ->select('id')
+                ->get()->toArray();
+            $detail = ApplyOrderDetail::where('del_time',0)
+                ->whereIn('data_id', array_column($dispatch,'id'))
+                ->where('type',ApplyOrder::type_one)
+                ->select('apply_order_id')
+                ->get()->toArray();
+            $result = ApplyOrder::where('del_time',0)
+                ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
+                ->select('id','order_number as order_no')
+                ->get()->toArray();
+        }elseif ($data['for_type'] == 10){
+            $dispatch = DispatchSub::where('del_time',0)
+                ->whereIn('sale_orders_product_id', $sales_id)
+                ->select('id')
+                ->get()->toArray();
+            $detail = ApplyOrderDetail::where('del_time',0)
+                ->whereIn('data_id', array_column($dispatch,'id'))
+                ->where('type',ApplyOrder::type_two)
+                ->select('apply_order_id')
+                ->get()->toArray();
+            $result = ApplyOrder::where('del_time',0)
+                ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
+                ->select('id','order_number as order_no')
+                ->get()->toArray();
+        }elseif($data['for_type'] == 11){
+            $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();
+            $ids = [];
+            foreach ($sale as $value){
+                $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
+                $box_detail_order = $box_detail->where('top_id',$data['data_id'])->selct('order_no')->get()->toArray();
+                $tmp = Box::where('del_time',0)
+                    ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
+                    ->select("id")
+                    ->get()->toArray();
+                $ids = array_merge($ids, $tmp);
+            }
+
+            $detail = ApplyOrderDetail::where('del_time',0)
+                ->whereIn('data_id', array_column($ids,'id'))
+                ->where('type',ApplyOrder::type_four)
+                ->select('apply_order_id')
+                ->get()->toArray();
+            $result = ApplyOrder::where('del_time',0)
+                ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
+                ->select('id','order_number as order_no')
+                ->get()->toArray();
+        }elseif($data['for_type'] == 12){
+            $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();
+            $ids = [];
+            foreach ($sale as $value){
+                $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
+                $box_detail_order = $box_detail->where('top_id',$data['data_id'])->selct('order_no')->get()->toArray();
+                $tmp = Box::where('del_time',0)
+                    ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
+                    ->select("id")
+                    ->get()->toArray();
+                $ids = array_merge($ids, $tmp);
+            }
+
+            $detail = ApplyOrderDetail::where('del_time',0)
+                ->whereIn('data_id', array_column($ids,'id'))
+                ->where('type',ApplyOrder::type_three)
+                ->select('apply_order_id')
+                ->get()->toArray();
+            $result = ApplyOrder::where('del_time',0)
+                ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
+                ->select('id','order_number as order_no')
+                ->get()->toArray();
+        }elseif($data['for_type'] == 13){
+            $zj_id = ScrappCount::where('del_time',0)
+                ->where('scrapp_num','>',0)
+                ->whereIn('sale_orders_product_id', $sales_id)
+                ->select('id')
+                ->get()->toArray();
+            $detail = ApplyOrderDetail::where('del_time',0)
+                ->whereIn('data_id', array_column($zj_id,'id'))
+                ->where('type',ApplyOrder::type_five)
+                ->select('apply_order_id')
+                ->get()->toArray();
+            $result = ApplyOrder::where('del_time',0)
+                ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
+                ->select('id','order_number as order_no')
+                ->get()->toArray();
+        }
+
+        return [true, $result];
+    }
 }

+ 411 - 8
app/Service/Box/BoxService.php

@@ -12,7 +12,10 @@ use App\Model\EmployeeTeamPermission;
 use App\Model\Header_ext;
 use App\Model\OrdersProduct;
 use App\Model\OrdersProductProcess;
+use App\Model\ReportWorking;
+use App\Model\ReportWorkingDetail;
 use App\Model\SaleOrdersProduct;
+use App\Model\ScrappCount;
 use App\Model\Setting;
 use App\Model\Team;
 use App\Service\ApplyOrderService;
@@ -49,6 +52,8 @@ class BoxService extends Service
         list($status, $msg) = $this->boxInRule($data, $user);
         if(! $status) return [false, $msg];
 
+        $data['zt'] = Request()->header('zt');
+//list($status, $msg) = $this->boxInSettle($data,$user);dd($status,$msg);
         //限制频率
         $limit_key = $this->lock_key;
         list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
@@ -110,6 +115,10 @@ class BoxService extends Service
     }
 
     public function boxInSettle($data, $user){
+        //设置数据库连接
+        $this->setConnect($data);
+        $zt = $data['zt'] ?? "";
+
         $ids = [];
         $sale_ids = [];
         $top_order_no = $data['order_no'];
@@ -140,8 +149,8 @@ class BoxService extends Service
             DB::beginTransaction();
 
             //数据获取
-            $product_list = OrdersProduct::wherein('id', $ids)->get()->toArray();
-            $sale_product_list = SaleOrdersProduct::wherein('id', $sale_ids)->get()->toArray();
+            $product_list = OrdersProduct::whereIn('id', $ids)->get()->toArray();
+            $sale_product_list = SaleOrdersProduct::whereIn('id', $sale_ids)->get()->toArray();
 
             //生产订单包装----------------
             $box_insert = [];
@@ -204,11 +213,11 @@ class BoxService extends Service
                 return [false, $msg];
             }
 
-            //自动完工 更新完工数量
+            //反写数量
             $em = new EmployeeService();
             $auto = $em->is_auto($user, "bzwg_auto");
             if($auto){
-                list($status, $msg1) = $this->updateFinish($product_list,$key_list,$user);
+                list($status, $msg1) = $this->updateFinishNew($product_list,$key_list,$user);
                 if(! $status) {
                     $this->dellimitingSendRequestBackgNeed($limit_key);
                     DB::rollBack();
@@ -241,9 +250,9 @@ class BoxService extends Service
                 return [false, $msg];
             }
 
-            //是否自动审核  包装入库申请单
+            //是否自动审核  包装入库申请单 如果是测试的不写用友
             $auto = $em->is_auto($user,"bzsq_auto");
-            if(! empty($package_data) && $auto) {
+            if(! empty($package_data) && $auto && $zt != 'cs') {
                 //用友 ------产成品入库
                 $service = new FinishedOrderService();
                 list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
@@ -255,6 +264,7 @@ class BoxService extends Service
             }
 
             DB::commit();
+
             $this->dellimitingSendRequestBackgNeed($limit_key);
 
             return [true, ['package_data' => $package_data]];
@@ -268,6 +278,24 @@ class BoxService extends Service
         }
     }
 
+    private function setConnect($data){
+        $storeCode = $data['zt'] ?? '';
+        $connectionName = "mysql";
+        if($storeCode == 'cs') $connectionName = "mysql_" . $storeCode;
+
+        if (in_array($connectionName, array_keys(config('database.connections')))) {
+            // 切换默认数据库连接
+            config(['database.default' => $connectionName]);
+
+            // 重新解析 DB Facade 使用新的连接
+            app()->forgetInstance('db');
+            $db = app('db');
+
+            // 如果用到了模型,还要重置模型连接
+            \Illuminate\Database\Eloquent\Model::setConnectionResolver($db);
+        }
+    }
+
     public function createllSQ($package_data, $insert_id, $user){
         //是否自动审核  包装领料申请单
         $em = new EmployeeService();
@@ -390,6 +418,381 @@ class BoxService extends Service
         $this->dellimitingSendRequestBackgNeed($this->lock_key);
     }
 
+    //新的完工
+    public function updateFinishNew($production_list = [], $map, $user){
+        if(empty($production_list)) return [true, ''];
+
+        $order_product_id = array_column($production_list,'id');
+        $order = OrdersProduct::where('del_time',0)
+            ->where('id', $order_product_id)
+            ->select('id', 'process_id')
+            ->get()->toArray();
+        $order_map = [];
+        //生产订单 =》 工序
+        foreach ($order as $value){
+            $order_map[$value['id']] = explode(',', $value['process_id']);
+        }
+
+        //组织派工数据
+        $dispatch = DispatchSub::where('del_time',0)
+            ->whereIn('order_product_id', array_column($production_list,'id'))
+            ->get()->toArray();
+        if(empty($dispatch)) return [true,''];
+        $sale_order_id = array_unique(array_column($dispatch,'sale_orders_product_id'));
+        $dispatch_map = [];
+        foreach ($dispatch as $value){
+            $key = $value['order_product_id'] . '|' .  $value['crt_time']; //同一次下的派工单
+            $dispatch_map[$key][] = $value;
+        }
+        $result = []; $tmp = [];
+        foreach ($dispatch_map as $key => $value){
+            $dispatch_quantity = array_sum(array_column($value, 'dispatch_quantity'));
+            $finished_num = array_sum(array_column($value, 'finished_num'));
+            //同一批派工 且 全部完工的 跳过
+            if($dispatch_quantity == $finished_num) continue;
+            $order_id = explode('|', $key)[0];
+            // 生产订单id存在 跳过(原因 同一个生产订单可能下多次派工,那么这一次包装数量只需要一次派工的数据)
+            if(in_array($order_id, $tmp)) continue;
+            foreach ($value as $val){
+                $num = bcadd($val['finished_num'], $val['waste_num'], 3);
+                if($num >= $val['dispatch_quantity']) continue; //某一个工序完工
+                //包装数量
+                $tmp_num = $map[$val['order_product_id']]['total'] ?? 0;
+                if($tmp_num <= 0) continue;
+                $tmp_num2 = bcsub($val['dispatch_quantity'] , $val['finished_num'],3);
+                if($tmp_num2 >= $tmp_num){
+                    $val['quantity'] = $tmp_num;
+                }else{
+                    $val['quantity'] = $tmp_num2;
+                }
+                $result[] = $val;
+                $tmp[] = $order_id;
+            }
+        }
+        if(empty($result)) return [true,''];
+
+        try {
+            DB::beginTransaction();
+
+            //班组下的人
+            $team_man = [];
+            $team_array = EmployeeTeamPermission::select('employee_id','team_id')->get()->toArray();
+            foreach ($team_array as $value){
+                //拿班组里的一个人
+                if(! isset($team_man[$value['team_id']]))  $team_man[$value['team_id']] = $value['employee_id'];
+            }
+            //工序 班组
+            $process_map = [
+                12 => 65,
+                11 => 66,
+                15 => 69,
+                14 => 67,
+                13 => 68,
+            ];
+
+            $service = new FinishedOrderService();
+            $service1 = new ApplyOrderService();
+            $dispatch_time = $c_time = $result[0]['crt_time']; //派工单创建时间
+            $date = date("Ymd", $c_time);
+//            //派工和包装不在同一天 质检单日期加一天
+//            if($date != date("Ymd")){
+//                $c_time = strtotime("+1 day", strtotime($date));
+//                $date = date("Ymd", $c_time);
+//            }
+            $max_id = $service1->getDailyId($c_time);
+
+            //是否需要质检
+            list($status, $return) = $service->isNeedZJ($sale_order_id);
+            $flag = $return;
+            //根据派工数据组织
+            $last_update = $last_update2 = $str_tmp = $insert = $get_order_number = [];
+            foreach ($result as $value){
+                $need_zj_process = $flag[$value['sale_orders_product_id']] ?? 0;
+                $bad_quantity = $scrapp_id = $for_r = 0;
+                $warehouse_no = $warehouse_title = "";
+                if(! empty($need_zj_process) && $value['process_id'] == $need_zj_process){
+                    $numbers = [0.08, 0.09, 0.1, 0.11, 0.12];
+                    $bad_quantity = $numbers[array_rand($numbers)];
+                    $flag[$value['sale_orders_product_id']] = 0;
+                    if($need_zj_process == 12){
+                        $scrapp_id = 19;
+                        $for_r = ReportWorking::type_two;
+                        $warehouse_no = "005";
+                        $warehouse_title = "报废仓";
+                    }else{
+                        $scrapp_id = mt_rand(10, 18);
+                        $for_r = ReportWorking::type_one;
+                    }
+                }
+                $quantity = bcsub($value['quantity'], $bad_quantity, 3);
+
+                //质检
+                $max_id += 1;
+                $order_number = $date . str_pad($max_id,3,'0',STR_PAD_LEFT);
+                $get_order_number[] = $order_number;
+                $insert[] = [
+                    'sale_orders_product_id' => $value['sale_orders_product_id'],
+                    'order_product_id' => $value['order_product_id'],
+                    'dispatch_sub_id' => $value['id'],
+                    'out_order_no' => $value['out_order_no'],
+                    'order_no' => $value['order_no'],
+                    'customer_no' => $value['customer_no'],
+                    'customer_name' => $value['customer_name'],
+                    'product_no' => $value['product_no'],
+                    'product_title' => $value['product_title'],
+                    'product_size' => $value['product_size'],
+                    'product_unit' => $value['product_unit'],
+                    'technology_material' => $value['technology_material'],
+                    'technology_name' => $value['technology_name'],
+                    'wood_name' => $value['wood_name'],
+                    'price' => $value['price'],
+                    'process_mark' => $value['process_mark'],
+                    'table_body_mark' => $value['table_body_mark'],
+                    'table_header_mark' => $value['table_header_mark'],
+                    'crt_id' => $user['id'],
+                    'crt_time' => $c_time,
+                    'scrapp_num' => $bad_quantity,
+                    'scrapp_id' => $scrapp_id,
+                    'team_id' => 73,
+                    'finished_id' => $user['id'],
+                    'equipment_id' => $value['device_id'],
+                    'order_number' => $order_number,
+                    'process_id' => $value['process_id'],
+                    'quantity' => $quantity,
+                    'report_id' => 0,
+                    'result' => $for_r,
+                    'warehouse_no' => $warehouse_no,
+                    'warehouse_title' => $warehouse_title,
+                    'daily_id' => $max_id,
+                ];
+
+                $tmp = [
+                    'id' => $value['id'],
+                    'quantity' => $value['quantity'],
+                    'product_no' => $value['product_no'] ?? "",
+                    'product_title' => $value['product_title'] ?? "",
+                    'product_size' => $value['product_size'] ?? "",
+                    'product_unit' => $value['product_unit'] ?? "",
+                ];
+                $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
+                if(! in_array($str, $str_tmp)){
+                    $last_update[] = $tmp;
+                    $str_tmp[] = $str;
+                }
+
+                $tmp['process_id'] = $value['process_id'];
+                $tmp['quantity'] = $quantity;
+                $team_tmp = $process_map[$value['process_id']] ?? 0; // 班组
+                $t_m = $team_man[$team_tmp] ?? 0; //班组下的人
+                $tmp['finished_id'] = $t_m;
+                $tmp['team_id'] = $team_tmp;
+                $last_update2[] = $tmp;
+            }
+
+            //生成报工单
+            list($status, $msg) = $this->createbg($last_update2, $user,$dispatch_time);
+            if(! $status) return [false, $msg];
+            //报工单id数组
+            list($report_id, $report_order_number) = $msg;
+            if(empty($report_id)) return [false, '报工单返回信息异常'];
+            //根据报工单创建质检单
+            list($status,$msg) = $service->createZJFirst($report_id, $insert);
+            if(! $status) return [false, $msg];
+            //生成因为有不良品需要的返工的业务单据
+            list($status, $msg) = $service->createZJSecond($get_order_number, $user, $dispatch_time, $c_time,$max_id,$team_man,$report_order_number);
+            if(! $status) return [false, $msg];
+            $get_order_number_result = $msg;
+            //生成完工入库申请单
+            list($status, $msg) = $this->createWGSQ($last_update, $user, $c_time);
+            if(! $status) return [false, $msg];
+
+            //反写 根据质检单上的派工单id 反写上游单据
+            $this->writeDispatchQuantity($get_order_number_result);
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
+        }
+
+        return [true, ''];
+    }
+
+    public function createbg($insert, $user,$dispatch_time = 0, $order_number = ""){
+        //报工单
+        $em = new EmployeeService();
+        $auto = $em->is_auto($user, "wgbg_auto");
+
+        if(empty($order_number)){
+            $order_number = (new ApplyOrderService())->setOrderNO2($dispatch_time);
+            if(empty($order_number)) return [false, '报工单编号生成失败'];
+            $is_first = true;
+        }else{
+            $is_first = false;
+        }
+
+        try {
+            DB::beginTransaction();
+
+            if(empty($dispatch_time)){
+                $time = time();
+            }else{
+                $time = $dispatch_time;
+            }
+
+            $prefix = substr($order_number, 0, -3);// "20250611"
+            $startNum = (int)substr($order_number, -3); // 7
+            $return = $get_order_number = [];
+            foreach ($insert as $v){
+                if(! $is_first){
+                    $startNum += 1;
+                    $number = str_pad($startNum,3,'0',STR_PAD_LEFT);
+                    $number = $prefix . $number;
+                    $dispatch_no_t = $number;
+                }else{
+                    $dispatch_no_t = $order_number;
+                    $is_first = false;
+                }
+                $get_order_number[] = $dispatch_no_t;
+                $model = new ReportWorking();
+                $model->order_number = $dispatch_no_t;
+                $model->report_time = $time;
+                $model->crt_id = $user['id'];
+                $model->status = $auto;
+                $model->save();
+                $id = $model->id;
+
+                ReportWorkingDetail::insert([
+                    'report_working_id' => $id,
+                    'data_id' => $v['id'],
+                    'quantity' => $v['quantity'] ?? 0,
+                    'finished_id' => $v['finished_id'] ?? 0,
+                    'process_id' => $v['process_id'] ?? 0,
+                    'team_id' => $v['team_id'] ?? 0,
+                    'crt_time' => $time,
+                ]);
+
+                $return[$v['id']] = $id;
+            }
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
+        }
+
+        return [true, [$return, $get_order_number]];
+    }
+
+    //反写
+    public function writeDispatchQuantity($get_order_number_result){
+        $result = ScrappCount::whereIn('order_number',$get_order_number_result)
+            ->select('dispatch_sub_id','quantity','scrapp_num as num','crt_time','process_id','order_product_id')
+            ->get()->toArray();
+
+        //是否最后一道工序
+        $orders = OrdersProduct::whereIn('id', array_unique(array_column($result, 'order_product_id')))
+            ->select('id','process_id')
+            ->get()->toArray();
+        $map = [];
+        foreach ($orders as $value){
+            $p = explode(",", $value['process_id']);
+            $lastElement = end($p);
+            $map[$value['id']] = $lastElement;
+        }
+
+        //汇总这个派工单一共质检良品数量 但是同一时间下的 只取一次
+        $update = $flag = $need_update = [];
+        foreach ($result as $value){
+            if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id'] && ! in_array($value['order_product_id'], $need_update)){
+                $need_update[] = [
+                    'order_product_id' => $value['order_product_id'],
+                    'process_id' => $value['process_id'],
+                ];
+            }
+            $key = $value['dispatch_sub_id'] . $value['crt_time'];
+            if(isset($flag[$key])) continue;
+
+            if(isset($update[$value['dispatch_sub_id']])){
+                $tmp = bcadd($value['quantity'], $update[$value['dispatch_sub_id']]['finished_num'], 3);
+                $tmp1 = bcadd($value['num'], $update[$value['dispatch_sub_id']]['waste_num'], 3);
+                $update[$value['dispatch_sub_id']] = [
+                    'finished_num' => $tmp,
+                    'waste_num' => $tmp1,
+                ];
+            }else{
+                $update[$value['dispatch_sub_id']] = [
+                    'finished_num' => $value['quantity'],
+                    'waste_num' => $value['num'],
+                ];
+            }
+            $flag[$key] = 1;
+        }
+        unset($flag);
+
+        //更新派工单完工数量----------------------------------------------------
+        foreach ($update as $id => $value){
+            DispatchSub::where('id', $id)->update([
+                'finished_num' => DB::raw('finished_num + '.$value['finished_num']),
+                'waste_num' => DB::raw('waste_num + '.$value['waste_num']),
+            ]);
+        }
+
+        if(! empty($need_update)){
+            //更新生产订单的完工数量--------------------------------------------------
+            $list = DispatchSub::where("del_time",0)
+                ->when(! empty($need_update), function ($query) use ($need_update) {
+                    return $query->where(function ($q) use ($need_update) {
+                        foreach ($need_update as $value) {
+                            $order_product_id = $value['order_product_id'];
+                            $process_id = $value['process_id'];
+                            $q->orWhere(function ($subQ) use ($order_product_id, $process_id) {
+                                $subQ->where('order_product_id', $order_product_id)
+                                    ->where('process_id', $process_id);
+                            });
+                        }
+                    });
+                })
+                ->select('order_product_id', 'finished_num', 'sale_orders_product_id')
+                ->get()->toArray();
+            $map2 = [];
+            foreach ($list as $value){
+                if(isset($map2[$value['order_product_id']])){
+                    $tmp = bcadd($map2[$value['order_product_id']], $value['finished_num'], 3);
+                    $map2[$value['order_product_id']] = $tmp;
+                }else{
+                    $map2[$value['order_product_id']] = $value['finished_num'];
+                }
+            }
+            foreach ($map2 as $id => $num){
+                OrdersProduct::where('id', $id)->update([
+                    'finished_num' => $num
+                ]);
+            }
+
+            //更新销售订单完工数量-------------------------------------------------------
+            $list2 = OrdersProduct::where('del_time',0)
+                ->whereIn('sale_orders_product_id', array_unique(array_column($list, 'sale_orders_product_id')))
+                ->select('finished_num','sale_orders_product_id')
+                ->get()->toArray();
+            $map3 = [];
+            foreach ($list2 as $value){
+                if(isset($map3[$value['sale_orders_product_id']])){
+                    $tmp = bcadd($map3[$value['sale_orders_product_id']], $value['finished_num'], 3);
+                    $map3[$value['sale_orders_product_id']] = $tmp;
+                }else{
+                    $map3[$value['sale_orders_product_id']] = $value['finished_num'];
+                }
+            }
+            foreach ($map3 as $id => $num){
+                SaleOrdersProduct::where('id', $id)->update([
+                    'finished_num' => $num
+                ]);
+            }
+        }
+    }
+
+    //旧的完工
     public function updateFinish($production_list = [], $map,$user){
         if(empty($production_list)) return [true,''];
 
@@ -544,7 +947,7 @@ class BoxService extends Service
         return [true, ''];
     }
 
-    public function createWGSQ($insert_id, $user){
+    public function createWGSQ($insert_id, $user, $time = 0){
         //完工入库申请单自动审核并同步用友
         $em = new EmployeeService();
         $auto = $em->is_auto($user, "wgsq_auto");
@@ -554,7 +957,7 @@ class BoxService extends Service
 
             //生成申请单
             $service = new ApplyOrderService();
-            list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_two, $auto);
+            list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_two, $auto, $time);
             if(! $status) {
                 DB::rollBack();
                 return [false, $msg];

+ 23 - 7
app/Service/DispatchService.php

@@ -20,8 +20,13 @@ class DispatchService extends Service
 {
     public function edit($data){}
 
-    public function setOrderNO(){
-        $str = date('Ymd',time());
+    public function setOrderNO($time = 0){
+        if(empty($time)) {
+            $str = date('Ymd',time());
+        }else{
+            $str = date('Ymd',$time);
+        }
+
 
         $order_number = Dispatch::where('dispatch_no','Like','%'. $str . '%')
             ->max('dispatch_no');
@@ -99,7 +104,7 @@ class DispatchService extends Service
         return [true,''];
     }
 
-    public function createSQ($insert_data, $user){
+    public function createSQ($insert_data, $user,$dispatch_time = 0){
         try {
             DB::beginTransaction();
 
@@ -117,10 +122,16 @@ class DispatchService extends Service
             //组织原材料写入数据
             $insert = [];
             foreach ($insert_data as $value){
+                $quantity = 0;
+                if(! empty($value['quantity'])) {
+                    $quantity = $value['quantity'];
+                }elseif (! empty($value['dispatch_quantity'])){
+                    $quantity = $value['dispatch_quantity'];
+                }
                 $t = $return[$value['product_no']] ?? [];
                 $tmp = [
                     'id' => $value['id'],
-                    'quantity' => $value['quantity'] ?? 0,
+                    'quantity' => $quantity,
                     'product_no' => "",
                     'product_title' => "",
                     'product_size' => "",
@@ -142,7 +153,7 @@ class DispatchService extends Service
 
             //生成申请单
             $service = new ApplyOrderService();
-            list($status, $msg) = $service->createSQ($insert, $user, ApplyOrder::type_one, $auto);
+            list($status, $msg) = $service->createSQ($insert, $user, ApplyOrder::type_one, $auto,$dispatch_time);
             if(! $status) {
                 DB::rollBack();
                 return [false, $msg];
@@ -584,9 +595,10 @@ class DispatchService extends Service
 
     public function dispatchOrderList($data){
         $model = DispatchSub::where('del_time',0)
-            ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','production_quantity','dispatch_no','crt_id','process_id','dispatch_time_start','dispatch_time_end','crt_time','finished_num','waste_num','customer_name','order_product_id','out_order_no','team_id','device_id','wg_status','status')
+            ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','production_quantity','dispatch_no','crt_id','process_id','dispatch_time_start','dispatch_time_end','crt_time','finished_num','waste_num','customer_name','order_product_id','out_order_no','team_id','device_id','wg_status','status','type')
             ->orderBy('id','desc');
 
+        if(isset($data['type'])) $model->where('type', $data['type']);
         if(isset($data['status'])) $model->where('status', $data['status']);
         if(isset($data['wg_status'])) $model->where('wg_status', $data['wg_status']);
         if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
@@ -617,12 +629,16 @@ class DispatchService extends Service
                 $model->wherecolumn('dispatch_quantity','>','finished_num');
             }
         }
-        if(! empty($data['employee_id'])) {//todo
+        if(! empty($data['employee_id'])) {
             $team = EmployeeTeamPermission::where('employee_id',$data['employee_id'])
                 ->select('team_id')
                 ->get()->toArray();
             $model->whereIn('team_id',array_unique(array_column($team,'team_id')));
         }
+        if(! empty($data['dispatch_id'])){
+            $id = explode(',',$data['dispatch_id']);
+            $model->whereIn('id', $id);
+        }
 
         $list = $this->limit($model,'',$data);
         $list = $this->fillDispatchOrderListData($list);

+ 244 - 5
app/Service/FinishedOrderService.php

@@ -5,6 +5,7 @@ namespace App\Service;
 use App\Jobs\ProcessDataJob;
 use App\Model\Box;
 use App\Model\BoxDetail;
+use App\Model\Dispatch;
 use App\Model\DispatchSub;
 use App\Model\Employee;
 use App\Model\EmployeeTeamPermission;
@@ -174,7 +175,7 @@ class FinishedOrderService extends Service
         return $number;
     }
 
-    public function add($data,$user){
+    public function add($data,$user){return [false, '接口功能已关闭'];
         //数据校验以及填充
         list($status,$msg) = $this->orderRule($data);
         if(!$status) return [$status,$msg];
@@ -347,18 +348,23 @@ class FinishedOrderService extends Service
         return [true, ''];
     }
 
-    public function createbg($insert, $user){
+    public function createbg($insert, $user,$dispatch_time = 0){
         //报工单
         $em = new EmployeeService();
         $auto = $em->is_auto($user, "wgbg_auto");
 
-        $order_number = (new ApplyOrderService())->setOrderNO2();
+        $order_number = (new ApplyOrderService())->setOrderNO2($dispatch_time);
         if(empty($order_number)) return [false, '报工单编号生成失败'];
 
         try {
             DB::beginTransaction();
 
-            $time = time();
+            if(empty($dispatch_time)){
+                $time = time();
+            }else{
+                $time = $dispatch_time;
+            }
+
             $model = new ReportWorking();
             $model->order_number = $order_number;
             $model->report_time = $time;
@@ -389,9 +395,242 @@ class FinishedOrderService extends Service
             return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
         }
 
+        return [true, $id];
+    }
+
+    public function createZJFirst($report_id, $insert){
+        foreach ($insert as $key => $value) {
+            $t_report_id = $report_id[$value['dispatch_sub_id']] ?? 0;
+            $insert[$key]['report_id'] = $t_report_id;
+        }
+        ScrappCount::insert($insert);
+
         return [true, ''];
     }
 
+    public function createZJSecond($get_order_number, $user, $dispatch_time, $c_time,$max_id,$team_man,$report_order_number){
+        try {
+            $time = time();
+            //获取新增的单据
+            $result = ScrappCount::whereIn('order_number',$get_order_number)
+                ->where('result','>',0)
+                ->select('scrapp_num as num','id','dispatch_sub_id','warehouse_no','warehouse_title','result','product_no')
+                ->get()->toArray();
+            if(empty($result)) return [true, $get_order_number];
+            $dispatchList = DispatchSub::whereIn('id', array_column($result,'dispatch_sub_id'))->get()->toArray();
+            $dispatch_map = array_column($dispatchList,null,'id');
+
+            //是否自动审核
+            $em = new EmployeeService();
+            $auto = $em->is_auto($user, "pg_auto");
+
+            $dispatch_no = (new DispatchService())->setOrderNO($dispatch_time);
+            if(! $dispatch_no) return [false,'返工派工单号生成失败!'];
+            $prefix = substr($dispatch_no, 0, -3);
+            $startNum = (int)substr($dispatch_no, -3);
+            $is_first = true;
+
+            $dispatch_main = $dispatch = $is_need_ll = $is_need_warehouse = $zj = [];
+            foreach ($result as $value){
+                //派工单
+                $t = $dispatch_map[$value['dispatch_sub_id']] ?? [];
+                if(! $is_first){
+                    $startNum += 1;
+                    $number = str_pad($startNum,3,'0',STR_PAD_LEFT);
+                    $number = $prefix . $number;
+                    $dispatch_no_t = $number;
+                }else{
+                    $dispatch_no_t = $dispatch_no;
+                    $is_first = false;
+                }
+
+                if($value['result'] == ReportWorking::type_one){
+                    //重新入生产流程 需要生成返工派工单 报工单 质检单
+                }else{
+                    //入仓库 那么需要生成生成返工派工单 报工单 质检单 质检入库申请单 生成领料申请单
+                    $is_need_warehouse[] = [
+                        'warehouse_no' => $value['warehouse_no'],
+                        'warehouse_title' => $value['warehouse_title'],
+                    ];
+                    $zj[] = [
+                        'id' => $value['id'],
+                        'quantity' => $value['num'],
+                        'product_no' => $value['product_no'],
+                    ];
+                }
+                $is_need_ll[] = $dispatch_no_t;
+                $dispatch_main[] = ['dispatch_no' => $dispatch_no_t,'crt_time' => $time];
+                $dispatch[] = [
+                    'order_product_id' => $t['order_product_id'],
+                    'order_no' => $t['order_no'],
+                    'product_no' => $t['product_no'],
+                    'product_title' => $t['product_title'],
+                    'product_size' => $t['product_size'],
+                    'product_unit' => $t['product_unit'],
+                    'production_quantity' => $t['production_quantity'],
+                    'dispatch_quantity' => $value['num'],
+//                    'finished_num' => $value['num'],
+                    'wg_status' => 1,
+                    'technology_material' => $t['technology_material'],
+                    'technology_name' => $t['technology_name'],
+                    'wood_name' => $t['wood_name'],
+                    'process_mark' => $t['process_mark'],
+                    'table_body_mark' => $t['table_body_mark'],
+                    'table_header_mark' => $t['table_header_mark'],
+                    'crt_time' => $dispatch_time,
+                    'status' => $auto,
+                    'dispatch_no' => $dispatch_no_t,
+                    'process_id' => $t['process_id'],
+                    'dispatch_time_start' => $t['dispatch_time_start'],
+                    'dispatch_time_end' => $t['dispatch_time_end'],
+                    'crt_id' => $user['id'],
+                    'sale_orders_product_id' => $t['sale_orders_product_id'],
+                    'out_order_no' => $t['out_order_no'],
+                    'out_order_no_time' => $t['out_order_no_time'],
+                    'price' => $t['price'],
+                    'customer_no' => $t['customer_no'],
+                    'customer_name' => $t['customer_name'],
+                    'pre_shipment_time' => $t['pre_shipment_time'],
+                    'team_id' => $t['team_id'],
+                    'device_id' => $t['device_id'],
+                    'type' => 1,
+                ];
+            }
+
+            Dispatch::insert($dispatch_main);
+            DispatchSub::insert($dispatch);
+
+            $get_order_number1 = [];
+            //获取上一次插入订单的所有数据
+            $insert = DispatchSub::whereIn('dispatch_no', $is_need_ll)
+                ->where('crt_time',$dispatch_time)
+                ->where('crt_id',$user['id'])
+                ->get()->toArray();
+            $insert_bg = [];
+            foreach ($insert as $value){
+                $t = $team_man[$value['team_id']] ?? 0;
+                $insert_bg[] = [
+                    'id' => $value['id'],
+                    'quantity' => $value['dispatch_quantity'],
+                    'product_no' => $value['product_no'] ?? "",
+                    'product_title' => $value['product_title'] ?? "",
+                    'product_size' => $value['product_size'] ?? "",
+                    'process_id' => $value['process_id'] ?? "",
+                    'team_id' => $value['team_id'] ?? "",
+                    'finished_id' => $t,
+                ];
+            }
+
+            //生成报工单
+            $service_box = new BoxService();
+            $first_order_number = end($report_order_number);
+            list($status, $msg) = $service_box->createbg($insert_bg, $user, $dispatch_time, $first_order_number);
+            if(! $status) return [false, $msg];
+            list($report_id, $report_order_number1) = $msg;
+
+            //生成质检单
+            $service1 = new ApplyOrderService();
+            $date = date("Ymd", $c_time);
+            $result = [];
+            foreach ($insert as $value){
+                //质检
+                $max_id += 1;
+                $order_number = $date . str_pad($max_id,3,'0',STR_PAD_LEFT);
+                $get_order_number1[] = $order_number;
+                $t_report_id = $report_id[$value['id']] ?? 0;
+                $result[] = [
+                    'sale_orders_product_id' => $value['sale_orders_product_id'],
+                    'order_product_id' => $value['order_product_id'],
+                    'dispatch_sub_id' => $value['id'],
+                    'out_order_no' => $value['out_order_no'],
+                    'order_no' => $value['order_no'],
+                    'customer_no' => $value['customer_no'],
+                    'customer_name' => $value['customer_name'],
+                    'product_no' => $value['product_no'],
+                    'product_title' => $value['product_title'],
+                    'product_size' => $value['product_size'],
+                    'product_unit' => $value['product_unit'],
+                    'technology_material' => $value['technology_material'],
+                    'technology_name' => $value['technology_name'],
+                    'wood_name' => $value['wood_name'],
+                    'price' => $value['price'],
+                    'process_mark' => $value['process_mark'],
+                    'table_body_mark' => $value['table_body_mark'],
+                    'table_header_mark' => $value['table_header_mark'],
+                    'crt_id' => $user['id'],
+                    'crt_time' => $c_time,
+                    'team_id' => 73,
+                    'finished_id' => $user['id'],
+                    'equipment_id' => $value['device_id'],
+                    'order_number' => $order_number,
+                    'process_id' => $value['process_id'],
+                    'quantity' => $value['dispatch_quantity'],
+                    'report_id' => $t_report_id,
+                    'daily_id' => $max_id,
+                ];
+            }
+            ScrappCount::insert($result);
+
+            if(! empty($is_need_warehouse)){
+                //领料申请单
+                list($status, $msg) = (new DispatchService())->createSQ($insert, $user, $dispatch_time);
+                if(! $status) return [false, $msg];
+
+                //质检入库申请单 原材料
+                list($status, $msg) = $service1->createZJSQ($zj, $user, $is_need_warehouse, $c_time);
+                if(! $status) return [false, $msg];
+            }
+
+            return [true, array_merge($get_order_number, $get_order_number1)];
+        }catch (\Throwable $exception){
+            return[false, $exception->getMessage() . $exception->getLine()];
+        }
+    }
+
+    public function isNeedZJ($sale_orders_product_id = []){
+        $sale_order = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
+            ->where('order_quantity','>=', 20)
+            ->select('id', 'order_quantity')
+            ->get()->toArray();
+        if(empty($sale_order)) return [false, ''];
+        $need = [];
+        $sale_order_id = array_column($sale_order,'id');
+        //每个销售订单有4次的不良品创建需要
+        foreach ($sale_order_id as $value){
+            $need[$value] = 4;
+        }
+        $scrapp = ScrappCount::where("del_time",0)
+            ->whereIn("sale_orders_product_id",$sale_order_id)
+            ->where('scrapp_num','>',0)
+            ->select('sale_orders_product_id','process_id')
+            ->orderBy('process_id','asc')
+            ->get()->toArray();
+        $orderBy = [];
+        foreach ($scrapp as $value){
+            if(isset($need[$value['sale_orders_product_id']])) $need[$value['sale_orders_product_id']] -= 1;
+            $orderBy[$value['sale_orders_product_id']][] = $value['process_id'];
+        }
+        $count = [];
+        foreach ($need as $key => $value){
+            if($value > 0) $count[$key] = $value;
+        }
+        //是否需要创建
+        if(empty($count)) return [false, ''];
+        $return = [];
+        $process_id = [12,12,14,14];
+        foreach ($count as $key => $value){
+            $p_t = $orderBy[$key] ?? [];
+            $p_t_k = count($p_t) ?? 0;
+
+            if(isset($process_id[$p_t_k])){
+                $t = $process_id[$p_t_k];
+                $return[$key] = $t;
+            }
+        }
+
+        return [true, $return];
+    }
+
     //产成品入库
     public function U8Rdrecord10Save($package_data, $user){
         if(empty($package_data)) return [false, '产成品入库为空,请确认!'];
@@ -964,7 +1203,7 @@ class FinishedOrderService extends Service
         }
     }
 
-    public function mobileAdd($data,$user){
+    public function mobileAdd($data,$user){return [false, '接口功能已关闭'];
         //数据校验以及填充
         list($status,$msg,$count_arr) = $this->orderMobileRule($data);
         if(!$status) return [$status,$msg];

+ 26 - 22
app/Service/ScrappService.php

@@ -7,6 +7,7 @@ use App\Model\Employee;
 use App\Model\EmployeeTeamPermission;
 use App\Model\Equipment;
 use App\Model\Process;
+use App\Model\ReportWorking;
 use App\Model\SaleOrdersProduct;
 use App\Model\Scrapp;
 use App\Model\ScrappCount;
@@ -94,12 +95,11 @@ class ScrappService extends Service
     //质检单
     public function zjList($data, $user){
         $model = ScrappCount::where('del_time',0)
-            ->select('id','dispatch_sub_id','crt_time','team_id','finished_id','equipment_id','order_number','process_id','sale_orders_product_id')
-            ->orderBy('dispatch_sub_id','desc')
-            ->groupBy('order_number');
+            ->select('*')
+            ->orderBy('order_number','desc');
 
+        if(! empty($data['sq'])) $model->where('result',ReportWorking::type_two);
         if(! empty($data['order_number'])) {
-            $order_number = str_replace("ZJ","",$data['order_number']);
             if(! empty($order_number)) $model->where('order_number', 'LIKE', '%'.$order_number.'%');
         }
         if(! empty($data['dispatch_no'])) {
@@ -130,12 +130,12 @@ class ScrappService extends Service
         if(! empty($data['process_id'])) $model->where('process_id', $data['process_id']);
 
         $list = $this->limit($model,'',$data);
-        $list = $this->fillZjList($list);
+        $list = $this->fillZjList($list, $data, $user);
 
         return [true,$list];
     }
 
-    public function fillZjList($data){
+    public function fillZjList($data, $erg, $user){
         if(empty($data['data'])) return $data;
         $team_id = array_unique(array_column($data['data'],'team_id'));
         $team_maps = Team::whereIn('id',$team_id)
@@ -169,18 +169,25 @@ class ScrappService extends Service
         $dispatch_no = DispatchSub::whereIn('id',array_column($data['data'], 'dispatch_sub_id'))
             ->pluck('dispatch_no','id')
             ->toArray();
+        $return = [];
+        if(! empty($erg['material'])){
+            $product_no = array_unique(array_column($data['data'],'product_no'));
+            $service = new FyyOrderService();
+            list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);
+            if($status) $return = $msg;
+        }
         foreach ($data['data'] as $key => $value){
-//            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : '';
+            $data['data'][$key]['material'] = $return[$value['product_no']] ?? [];
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d", $value['crt_time']) : '';
             $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
             $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
             $data['data'][$key]['team_man'] = $team_man_maps[$value['team_id']] ?? '';
             $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
             $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
-            $data['data'][$key]['order_number'] = "ZJ" . $value['order_number'];
             $data['data'][$key]['sale_order_number'] = $sales_number[$value['sale_orders_product_id']] ?? "";
             $dispatch_t = $dispatch_no[$value['dispatch_sub_id']] ?? "";
             $data['data'][$key]['dispatch_no'] = $dispatch_t;
-            $data['data'][$key]['crt_time'] = date("Y-m-d",strtotime(substr($dispatch_t,0,8)));
+//            $data['data'][$key]['crt_time'] = date("Y-m-d",strtotime(substr($dispatch_t,0,8)));
         }
 
         return $data;
@@ -188,14 +195,13 @@ class ScrappService extends Service
 
     public function zjDetail($data, $user){
         if(empty($data['order_number'])) return [false, '请选择质检单数据'];
-        $order_number = str_replace("ZJ","",$data['order_number']);
         $result = ScrappCount::where('del_time',0)
-            ->where('order_number', $order_number)
+            ->where('order_number', $data['order_number'])
             ->get()->toArray();
         if(empty($result)) return [false, '质检单不存在或已被删除'];
 
         $first = $result[0] ?? [];
-        $order['order_number'] = "ZJ" . $first['order_number'];
+        $order['order_number'] = $first['order_number'];
         $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
         $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
         $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
@@ -203,7 +209,7 @@ class ScrappService extends Service
         $dispatch = DispatchSub::where('id',$first['dispatch_sub_id'])->first();
         $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
         $order['dispatch_no'] = $dispatch['dispatch_no'] ?? "";
-        $order['crt_time'] = date("Y-m-d",strtotime(substr($order['dispatch_no'],0,8)));
+        $order['crt_time'] = date("Y-m-d", $first['crt_time']);
 
         $team_man = EmployeeTeamPermission::where('team_id',$first['team_id'])
             ->select('team_id','employee_id')
@@ -246,12 +252,12 @@ class ScrappService extends Service
     //不良品
     public function blpList($data, $user){
         $model = ScrappCount::where('del_time',0)
+            ->where('scrapp_num','>',0)
             ->select('id','dispatch_sub_id','crt_time','team_id','finished_id','equipment_id','order_number','process_id','sale_orders_product_id')
             ->orderBy('dispatch_sub_id','desc')
             ->groupBy('order_number');
 
         if(! empty($data['order_number'])) {
-            $order_number = str_replace("BLP","",$data['order_number']);
             if(! empty($order_number)) $model->where('order_number', 'LIKE', '%'.$order_number.'%');
         }
         if(! empty($data['dispatch_no'])) {
@@ -323,17 +329,16 @@ class ScrappService extends Service
             ->pluck('dispatch_no','id')
             ->toArray();
         foreach ($data['data'] as $key => $value){
-//            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : '';
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d", $value['crt_time']) : '';
             $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
             $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
             $data['data'][$key]['team_man'] = $team_man_maps[$value['team_id']] ?? '';
             $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
             $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
-            $data['data'][$key]['order_number'] = "BLP" . $value['order_number'];
             $data['data'][$key]['sale_order_number'] = $sales_number[$value['sale_orders_product_id']] ?? "";
             $dispatch_t = $dispatch_no[$value['dispatch_sub_id']] ?? "";
             $data['data'][$key]['dispatch_no'] = $dispatch_t;
-            $data['data'][$key]['crt_time'] = date("Y-m-d",strtotime(substr($dispatch_t,0,8)));
+//            $data['data'][$key]['crt_time'] = date("Y-m-d",strtotime(substr($dispatch_t,0,8)));
         }
 
         return $data;
@@ -341,14 +346,13 @@ class ScrappService extends Service
 
     public function blpDetail($data, $user){
         if(empty($data['order_number'])) return [false, '请选择质检单数据'];
-        $order_number = str_replace("BLP","",$data['order_number']);
         $result = ScrappCount::where('del_time',0)
-            ->where('order_number', $order_number)
+            ->where('order_number', $data['order_number'])
             ->get()->toArray();
         if(empty($result)) return [false, '不良品单不存在或已被删除'];
 
         $first = $result[0] ?? [];
-        $order['order_number'] = "BLP" . $first['order_number'];
+        $order['order_number'] = $first['order_number'];
         $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
         $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
         $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
@@ -356,7 +360,7 @@ class ScrappService extends Service
         $dispatch = DispatchSub::where('id',$first['dispatch_sub_id'])->first();
         $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
         $order['dispatch_no'] = $dispatch['dispatch_no'] ?? "";
-        $order['crt_time'] = date("Y-m-d",strtotime(substr($order['dispatch_no'],0,8)));
+        $order['crt_time'] = date("Y-m-d",$first['crt_time']);
 
         $team_man = EmployeeTeamPermission::where('team_id',$first['team_id'])
             ->select('team_id','employee_id')
@@ -388,7 +392,7 @@ class ScrappService extends Service
                 'product_unit' => $first['product_unit'],
                 'technology_name' => $first['technology_name'],
                 'scrapp_quantity' => $value['scrapp_num'], // 不良品数量
-                'scrapp_title' => $scrapp[$value['scrapp_id']], // 不良品原因
+                'scrapp_title' => $scrapp[$value['scrapp_id']] ?? "", // 不良品原因
             ];
             $detail[] = $tmp;
         }

+ 9 - 10
config/database.php

@@ -10,9 +10,8 @@ if (app()->runningInConsole()) {//命令行请求
 //    }
 }else{// HTTP请求
     //账套
-//    $zt = $_SERVER['HTTP_ZT'];
-//    if($zt === '001') $mysql = "mysql_001";
-//    if($zt === '002') $mysql = "mysql_002";
+    $zt = $_SERVER['HTTP_ZT'];
+    if($zt === 'cs') $mysql = "mysql_cs";
 }
 
 return [
@@ -28,8 +27,8 @@ return [
     |
     */
 
-    'default' => env('DB_CONNECTION', 'mysql'),
-//    'default' => $mysql,
+//    'default' => env('DB_CONNECTION', 'mysql'),
+    'default' => $mysql,
 
     /*
     |--------------------------------------------------------------------------
@@ -74,14 +73,14 @@ return [
                 PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
             ]) : [],
         ],
-        'mysql_001' => [
+        'mysql_cs' => [
             'driver' => 'mysql',
             'url' => env('DATABASE_URL'),
-            'host' => 'rm-bp11ssr8mdn1xl5pl.mysql.rds.aliyuncs.com',
+            'host' => env('DB_HOST', '127.0.0.1'),
             'port' => env('DB_PORT', '3306'),
-            'database' => 'sys',
-            'username' => 'qingyaokeji23',
-            'password' => '@9X2p+Nipz2gh_G@',
+            'database' => env('DB_DATABASE_cs', 'forge'),
+            'username' => env('DB_USERNAME', 'forge'),
+            'password' => env('DB_PASSWORD', ''),
             'unix_socket' => env('DB_SOCKET', ''),
             'charset' => 'utf8mb4',
             'collation' => 'utf8mb4_unicode_ci',

+ 3 - 0
routes/api.php

@@ -299,6 +299,9 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('reportWorkingEdit', 'Api\ApplyOrderController@reportWorkingEdit');
     $route->any('reportWorkingAdd', 'Api\ApplyOrderController@reportWorkingAdd');
     $route->any('reportWorkingDel', 'Api\ApplyOrderController@reportWorkingDel');
+    //报工单质检
+    $route->any('qualityTesting', 'Api\ApplyOrderController@qualityTesting');
+    $route->any('orderSearchList', 'Api\ApplyOrderController@orderSearchList');
 
     $route->any('systemDataList','Api\ScreenController@systemDataList');
 });