cqpCow 1 éve
szülő
commit
94c980630e

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

@@ -42,7 +42,6 @@ class BoxService extends Service
      */
     public function boxIn($data)
     {
-
 //        if(!isset($data['params'])) return [false,'not found params'];
 //        $param = $data['params'];
 //        $param = [
@@ -66,8 +65,6 @@ class BoxService extends Service
 
         $ids = [];
         $sale_ids = [];
-//        $key_list = [];
-//        var_dump($data);die;
         $top_order_no = $data['order_no'];
         $box_no = isset($data['box_no']) ? $data['box_no'] : '';
         $transport_no = isset($data['transport_no']) ? $data['transport_no'] : '';
@@ -89,8 +86,7 @@ class BoxService extends Service
             ];
         }
         $insert = [];
-//        var_dump($ids);
-//        var_dump($sale_ids);die;
+
         try{
             DB::beginTransaction();
             $key = $this->lock_key.'_'.$v['id'];
@@ -330,7 +326,7 @@ class BoxService extends Service
         $model = SaleOrdersProduct::where('del_time',0)->wherein('id',$sale_order_ids)
             ->select('id','out_order_no','order_no','customer_no','customer_name','product_no','product_title','product_size','crt_time as production_time','id as sale_orders_product_id','finished_num as dispatch_complete_quantity','box_num','technology_name','wood_name','crt_time','order_quantity','technology_material','technology_name','wood_name','process_mark')
             ->orderBy('id','desc')->get()->toArray();
-        $product_list = ordersProduct::wherein('sale_orders_product_id',$sale_order_ids)->select('id','out_order_no','customer_no','customer_name','product_no','sale_orders_product_id','product_title','product_size','dispatch_complete_quantity','box_num','technology_name','wood_name','crt_time',DB::raw('(production_quantity - scrapp_num) as production_quantity '),DB::raw('finished_num as dispatch_complete_quantity '),'technology_material','technology_name','wood_name','process_mark')->get()->toArray();
+        $product_list = ordersProduct::where('del_time',0)->wherein('sale_orders_product_id',$sale_order_ids)->select('id','out_order_no','customer_no','customer_name','product_no','sale_orders_product_id','product_title','product_size','dispatch_complete_quantity','box_num','technology_name','wood_name','crt_time',DB::raw('(production_quantity - scrapp_num) as production_quantity '),DB::raw('finished_num as dispatch_complete_quantity '),'technology_material','technology_name','wood_name','process_mark')->get()->toArray();
         $model_key_list = [];
         foreach ($model as $v){
             $model_key_list[$v['id']] = $v;
@@ -391,7 +387,6 @@ class BoxService extends Service
                     'sale_num' => $v['order_quantity'],
                 ];
             }else{
-
                 $return[] = [
                     'id' => $v['id'],
                     'technology_material' => $v['technology_material'],
@@ -412,8 +407,8 @@ class BoxService extends Service
                     'sale_num' => $v['order_quantity'],
                 ];
             }
-
         }
+
         foreach ($product_key_list as $v){
             $return[] = $v;
         }

+ 33 - 16
app/Service/DeleteOrderService.php

@@ -10,6 +10,7 @@ use App\Model\OrdersProduct;
 use App\Model\OrdersProductBom;
 use App\Model\OrdersProductMain;
 use App\Model\OrdersProductProcess;
+use App\Model\Process;
 use App\Model\SaleOrdersProduct;
 use Illuminate\Support\Facades\DB;
 
@@ -30,7 +31,8 @@ class DeleteOrderService extends Service
                 list($status,$msg) = $this->delDispatch($data['id']);
                 break;
             case 4:
-                list($status,$msg) = $this->delFinished($data['id']);
+//                list($status,$msg) = $this->delFinished($data['id']);
+                list($status,$msg) = [false,'删除失败!'];
                 break;
             default:
                 list($status,$msg) = [false,'删除失败!'];
@@ -164,24 +166,47 @@ class DeleteOrderService extends Service
 
     //派工单删除
     public function delDispatch($id){
-        $bool = DispatchSub::where('del_time',0)
+        //校验工序派工单是否完工
+        $dispatch = DispatchSub::where('del_time',0)
             ->whereIn('id',$id)
-            ->where('finished_num','>',0)
-            ->exists();
-        if($bool) return [false,'工序派工单已有完工操作,删除失败!'];
+            ->select('id','dispatch_no','process_id','order_product_id','crt_time','finished_num')
+            ->get()
+            ->toArray();
+        $process_map = Process::whereIn('id',array_unique(array_column($dispatch,'process_id')))->pluck('title','id')->toArray();
+        $search = "";
+        foreach ($dispatch as $value){
+            $tmp_process = $process_map[$value['process_id']] ?? "";
+            if($value['finished_num'] > 0) return [false,'派工单号:' . $value['dispatch_no']. '的工序:' . $tmp_process .'已存在完工操作,删除失败!'];
+            $search .= "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
+        }
 
+        //校验同一批工序派工单是否完工
+        $search = rtrim($search,'OR ');
+        $search = "($search)";
+        $dispatch = DispatchSub::where('del_time',0)
+            ->whereRaw($search)
+            ->select('id','dispatch_no','process_id','order_product_id','crt_time','finished_num','out_order_no_time','sale_orders_product_id','dispatch_no','dispatch_quantity','order_product_id')
+            ->get()
+            ->toArray();
+        $process_map = Process::whereIn('id',array_unique(array_column($dispatch,'process_id')))->pluck('title','id')->toArray();
+        foreach ($dispatch as $value){
+            $tmp_process = $process_map[$value['process_id']] ?? "";
+            if($value['finished_num'] > 0) return [false,'派工单号:' . $value['dispatch_no']. '的工序:' . $tmp_process .'已存在完工操作,删除失败!'];
+        }
+
+        $new_del_id = array_column($dispatch,'id');
         try {
             DB::beginTransaction();
 
             //工序派工单
-            DispatchSub::whereIn('id',$id)->update([
+            DispatchSub::whereIn('id',$new_del_id)->update([
                 'del_time' => time()
             ]);
 
             //工序派工单主表
             $subquery = DB::table('dispatch_sub')
                 ->select('dispatch_no')
-                ->whereIn('id',$id);
+                ->whereIn('id',$new_del_id);
             $order_no = DB::table('dispatch_sub')
                 ->select('dispatch_sub.dispatch_no','dispatch_sub.del_time')
                 ->joinSub($subquery, 'sub', function ($join) {
@@ -205,10 +230,8 @@ class DeleteOrderService extends Service
             if(! empty($update_order)) Dispatch::whereIn('dispatch_no',$update_order)->update(['del_time' => time()]);
 
             //工序表
-            $message = DispatchSub::whereIn('id',$id)->select('out_order_no_time','sale_orders_product_id','dispatch_no','dispatch_quantity','order_product_id','process_id')->get()->toArray();
+            $message = $dispatch;
             if(! empty($message)){
-
-
                 foreach ($message as $value){
                     $tmp_time = date('Ymd',$value['out_order_no_time']);
                     $modelProcess = new OrdersProductProcess(['channel' => $tmp_time]);
@@ -220,12 +243,6 @@ class DeleteOrderService extends Service
                             'dispatch_no' => '',
                             'status' => 0
                         ]);
-
-                    DispatchEmpSub::where('order_product_id',$value['order_product_id'])
-                        ->where('dispatch_no',$value['dispatch_no'])
-                        ->update([
-                            'del_time' => time()
-                        ]);
                 }
             }
 

+ 202 - 134
app/Service/DispatchService.php

@@ -51,13 +51,14 @@ class DispatchService extends Service
         try{
             DB::beginTransaction();
 
+            $time = time();
             if($data['is_split']){
                 foreach ($msg as $value){
-                    list($s,$m) = $this->insertDispatch([$value],$data,$user);
+                    list($s,$m) = $this->insertDispatch([$value],$data,$user,$time);
                     if(! $s) return [false,$m];
                 }
             }else{
-                list($s,$m) = $this->insertDispatch($msg,$data,$user);
+                list($s,$m) = $this->insertDispatch($msg,$data,$user,$time);
                 if(! $s) return [false, $m];
             }
 
@@ -67,7 +68,7 @@ class DispatchService extends Service
             DB::commit();
         }catch (\Exception $e){
             DB::rollBack();
-            return [false,$e->getLine().':'.$e->getMessage()];
+            return [false,$e->getFile() . ':' .$e->getLine().':'.$e->getMessage()];
         }
 
         return [true,''];
@@ -147,10 +148,9 @@ class DispatchService extends Service
         return $arr;
     }
 
-    public function insertDispatch($msg, $data, $user){
+    public function insertDispatch($msg, $data, $user, $time){
         //生产数据的源数据
         $result = $msg;
-        $time = time();
 
         $dispatch_no = $this->setOrderNO();
         if(! $dispatch_no) return [false,'单号生成失败!'];
@@ -161,39 +161,23 @@ class DispatchService extends Service
         $time_tmp = date("Ymd", $data['out_order_no_time'][0]);
         $process_model = new OrdersProductProcess(['channel' => $time_tmp]);
 
-        $equipment_id = is_array($data['equipment_id']) ? $data['equipment_id'] : [$data['equipment_id']];
-        $equipment_id = array_filter($equipment_id);
-        $team_id = is_array($data['team_id']) ? $data['team_id'] : [$data['team_id']];
-        $team_id = array_filter($team_id);
-
-        $insert_emp_sub = [];
         foreach ($result as $key => $value){
             $result[$key]['dispatch_no'] = $dispatch_no;
-            $result[$key]['process_id'] = $data['process_id'];
-            $result[$key]['dispatch_time_start'] = $data['dispatch_time'][0];
-            $result[$key]['dispatch_time_end'] = $data['dispatch_time'][1];
-            $result[$key]['dispatch_quantity'] = $value['quantity'];
             $result[$key]['crt_time'] = $time;
             $result[$key]['crt_id'] = $user['id'];
 
-            $tmp = $this->makeData($equipment_id,$team_id,$data['employee_id'],$result[$key]);
-            $insert_emp_sub = array_merge_recursive($insert_emp_sub,$tmp);
-
             $process_model->where('order_product_id',$value['order_product_id'])
-                ->where('process_id',$data['process_id'])
+                ->where('process_id',$value['process_id'])
                 ->where('dispatch_no','')
-                ->take($value['quantity'])
+                ->take($value['dispatch_quantity'])
                 ->update([
                     'dispatch_no' => $dispatch_no,
                     'status' => 1
                 ]);
-            unset($result[$key]['quantity']);
         }
 
         DispatchSub::insert($result);
 
-        if(! empty($insert_emp_sub)) DispatchEmpSub::insert($insert_emp_sub);
-
         return [true,''];
     }
 
@@ -227,7 +211,7 @@ class DispatchService extends Service
         if(! $status) return [false, $msg];
 
         $model = OrdersProduct::where('del_time',0)
-            ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','production_time','production_no','status','crt_id','dispatch_complete_quantity','pre_shipment_time')
+            ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','production_time','production_no','status','crt_id','dispatch_complete_quantity','pre_shipment_time','process_id')
             ->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]])
             ->whereIn('id',$msg)
             ->orderBy('id','desc');
@@ -285,43 +269,75 @@ class DispatchService extends Service
     }
 
     public function orderRule($data){
-        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
-        if($this->isEmpty($data,'quantity')) return [false,'请填写数量!'];
-        if(in_array(false, $data['quantity'], true) || in_array(0, $data['quantity'], true) || in_array('', $data['quantity'], true))return [false,'数量不能为空!'];
-        if(empty($data['dispatch_time'][0]) || empty($data['dispatch_time'][1])) return [false,'计划生产时间不能为空!'];
         if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单时间不能为空!'];
-//        if($this->isEmpty($data,'process_id')) return [false,'工序不能为空!'];
         if(! isset($data['is_split'])) return [false,'是否拆分标识不能为空!'];
+        if(empty($data['detail'])) return [false, '派工明细数据不能为空!'];
+        $id = [];
+        $detail_map = $detail_2 = [];
+        foreach ($data['detail'] as $value){
+            if(empty($value['id'])) return [false,'请选择派工数据!'];
+            if(empty($value['process_id'])) return [false,'工序不能为空!'];
+            if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'数量不能小于0!'];
+            if(empty($value['dispatch_time'][0]) || empty($value['dispatch_time'][1])) return [false,'计划生产时间不能为空!'];
+            if(! in_array($value['id'], $id)) $id[] = $value['id'];
+            $key = $value['id'] . $value['process_id'];
+            if(isset($detail_map[$key])){
+                $detail_map[$key] += $value['quantity'];
+            }else{
+                $detail_map[$key] = $value['quantity'];
+            }
+            if(isset($detail_2[$value['id']][$value['process_id']])){
+                $detail_2[$value['id']][$value['process_id']] += $value['quantity'];
+            }else{
+                $detail_2[$value['id']][$value['process_id']] = $value['quantity'];
+            }
+        }
 
-        $result = OrdersProduct::whereIn('id',$data['id'])
-            ->select('id as order_product_id','sale_orders_product_id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','production_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','sale_orders_product_id','out_order_no_time','price','customer_name','out_order_no','customer_no','pre_shipment_time')
+        $result = OrdersProduct::whereIn('id',$id)
+            ->select('id as order_product_id','sale_orders_product_id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','production_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','sale_orders_product_id','out_order_no_time','price','customer_name','out_order_no','customer_no','pre_shipment_time','process_id','production_no')
             ->orderBy('id','desc')
             ->get()->toArray();
         $result_map = array_column($result,null,'order_product_id');
 
+        //每个生产订单所有的工序
+        $process_map = $this->getProcess($result);
         //已派工数据
-        $map = $this->getDispatchQuantity($data['id']);
-
-        $total_map = $return = [];
-        foreach ($data['id'] as $key => $value){
-            if(isset($total_map[$value])){
-                $total_map[$value] += $data['quantity'][$key];
-            }else{
-                $total_map[$value] = $data['quantity'][$key];
+        $map = $this->getDispatchQuantity($id);
+        //校验
+        foreach ($result as $value){
+            //总数量校验
+            $detail2 = $detail_2[$value['order_product_id']] ?? [];
+            $uniqueValuesCount = count(array_unique($detail2));
+            if($uniqueValuesCount != 1) return [false , "生产订单号:" . $value['production_no'] . "所有工序派工数量必须相等"];
+
+            //本次提交的工序
+//            $submit_process = array_keys($detail2);
+
+            //工序
+            $tmp = $process_map[$value['order_product_id']] ?? [];
+            foreach ($tmp as $v) {
+//                if(! in_array($v['process_id'], $submit_process)) continue;
+                //键值 生成订单id + 工序id
+                $key = $value['order_product_id'] . $v['process_id'];
+                //本次提交数量
+                $quantity_tmp = $detail_map[$key] ?? 0;
+                //工序已派工
+                $q = $map[$key] ?? 0;
+                if($q + $quantity_tmp > $value['production_quantity']) return [false, "生产订单号:" . $value['production_no'] . "的工序:" . $v['process_title'] . "的派单数量不能超过生产订单数量"];
             }
-            $tmp = $result_map[$value];
-            $tmp['quantity'] = $data['quantity'][$key];
-            $return[] = $tmp;
         }
 
-        foreach ($result as $key => $value){
-            //数据校验
-            $quantity_tmp = $total_map[$value['order_product_id']];
-            if(isset($map[$value['order_product_id']])){
-                if($map[$value['order_product_id']] + $quantity_tmp > $value['production_quantity']) return [false,'派单数量不能大于生产订单数量'];
-            }else{
-                if($quantity_tmp > $value['production_quantity']) return [false,'派单数量不能大于生产订单数量'];
-            }
+        $return = [];
+        foreach ($data['detail'] as $value){
+            $tmp = $result_map[$value['id']] ?? [];
+            unset($tmp['process_id']);unset($tmp['production_no']);
+            $tmp['process_id'] = $value['process_id'];
+            $tmp['team_id'] = $value['team_id'];
+            $tmp['device_id'] = $value['device_id'];
+            $tmp['dispatch_time_start'] = $value['dispatch_time'][0];
+            $tmp['dispatch_time_end'] = $value['dispatch_time'][1];
+            $tmp['dispatch_quantity'] = $value['quantity'];
+            $return[] = $tmp;
         }
 
         return [true, $return];
@@ -335,13 +351,24 @@ class DispatchService extends Service
             ->pluck('emp_name','id')
             ->toArray();
 
+        $process_map = $this->getProcess($data['data']);
+
         foreach ($data['data'] as $key => $value){
+            $tmp = $process_map[$value['id']] ?? [];
+            foreach ($tmp as $t => $v) {
+                $q = $map[$value['id'] . $v['process_id']] ?? 0;
+                $tmp[$t]['dispatch_quantity'] = $q;
+                $tmp[$t]['not_dispatch_quantity'] = $value['production_quantity'] - $q;
+            }
+            $data['data'][$key]['process'] = $tmp;
+            $last_process = end($tmp);
+            $dispatch_quantity = $last_process['dispatch_quantity'] ?? 0;
+            $data['data'][$key]['dispatch_quantity'] = $dispatch_quantity;
+            $data['data'][$key]['not_dispatch_quantity'] = $value['production_quantity'] - $dispatch_quantity;
             $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
             $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
             $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
             $data['data'][$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
-            $data['data'][$key]['dispatch_quantity'] = $map[$value['id']] ?? 0;
-            $data['data'][$key]['not_dispatch_quantity'] = $value['production_quantity'] - ($map[$value['id']] ?? 0);
             $data['data'][$key]['order_product_man'] = $emp_map[$value['crt_id']] ?? '';
             if($value['dispatch_complete_quantity'] >= $value['production_quantity']){
                 $data['data'][$key]['is_create'] = 1;
@@ -362,21 +389,69 @@ class DispatchService extends Service
 
         $result = DispatchSub::where('del_time',0)
             ->whereIn("order_product_id",$order_product_id)
-            ->select(DB::raw("sum(dispatch_quantity) as dispatch_quantity"),'order_product_id')
-            ->groupby('order_product_id')
-            ->pluck('dispatch_quantity','order_product_id')
+            ->select('dispatch_quantity','order_product_id','process_id')
+            ->get()
             ->toArray();
 
-        return $result;
+        $return = [];
+        foreach ($result as $value){
+            $key = $value['order_product_id'] . $value['process_id'];
+            if(isset($return[$key])){
+                $return[$key] += $value['dispatch_quantity'];
+            }else{
+                $return[$key] = $value['dispatch_quantity'];
+            }
+        }
+
+        return $return;
+    }
+
+    public function getProcess($data){
+        if(empty($data)) return [];
+
+        $process_id_array = [];
+        $process_id = array_column($data,'process_id');
+        foreach ($process_id as $value){
+            $tmp = explode(',',$value);
+            foreach ($tmp as $v){
+                if(! in_array($v,$process_id_array)) $process_id_array[] = $v;
+            }
+        }
+        $process_array = Process::whereIn('id', $process_id_array)->get()->toArray();
+        $process_map = array_column($process_array,null,'id');
+
+        $return = [];
+        foreach ($data as $value){
+            $tmp = explode(',',$value['process_id']);
+            foreach ($tmp as $v){
+               $process_tmp = $process_map[$v] ?? [];
+               if(isset($value['id'])){
+                   $return[$value['id']][] = [
+                       'process_id' => $v,
+                       'process_title' => $process_tmp['title'] ?? "",
+                       'team_id' => $process_tmp['team_id'] ?? 0,
+                       'device_id' => $process_tmp['device_id'] ?? 0,
+                   ];
+               }else{
+                   $return[$value['order_product_id']][] = [
+                       'process_id' => $v,
+                       'process_title' => $process_tmp['title'] ?? "",
+                       'team_id' => $process_tmp['team_id'] ?? 0,
+                       'device_id' => $process_tmp['device_id'] ?? 0,
+                   ];
+               }
+            }
+        }
+
+        return $return;
     }
 
     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','status','crt_id','process_id','dispatch_time_start','dispatch_time_end','crt_time','finished_num','waste_num','customer_name','order_product_id','out_order_no')
+            ->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','status','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')
             ->orderBy('id','desc');
 
         if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
-
         if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
         if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
         if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
@@ -386,29 +461,14 @@ class DispatchService extends Service
             $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);
             $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);
         }
-        if(! empty($data['team_id'])) {
-            $res = DispatchEmpSub::where('del_time',0)
-                ->where('team_id',$data['team_id'])
-                ->distinct()
-                ->select('dispatch_no')
-                ->get()->toArray();
-            $model->whereIn('dispatch_no',array_column($res,'dispatch_no'));
-        }
-        if(! empty($data['equipment_id'])) {
-            $res = DispatchEmpSub::where('del_time',0)
-                ->where('equipment_id',$data['equipment_id'])
-                ->distinct()
-                ->select('dispatch_no')
-                ->get()->toArray();
-            $model->whereIn('dispatch_no',array_column($res,'dispatch_no'));
-        }
-        if(! empty($data['employee_id'])) {
-            $res = DispatchEmpSub::where('del_time',0)
-                ->where('employee_id',$data['employee_id'])
-                ->distinct()
-                ->select('dispatch_no')
-                ->get()->toArray();
-            $model->whereIn('dispatch_no',array_column($res,'dispatch_no'));
+        if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);
+        if(! empty($data['device_id'])) $model->whereIn('device_id',$data['device_id']);
+        if(isset($data['is_finished'])) {
+            if($data['is_finished']){
+                $model->wherecolumn('dispatch_quantity','=','finished_num');
+            }else{
+                $model->wherecolumn('dispatch_quantity','>','finished_num');
+            }
         }
 
         $list = $this->limit($model,'',$data);
@@ -420,36 +480,12 @@ class DispatchService extends Service
     public function fillDispatchOrderListData($data){
         if(empty($data['data'])) return $data;
 
-        $emp_sub = DispatchEmpSub::where('del_time',0)
-            ->whereIn('dispatch_no',array_column($data['data'],'dispatch_no'))
-            ->select('dispatch_no','equipment_id','team_id','employee_id')
-            ->get()->toArray();
-        if(! empty($emp_sub)){
-            $team_map = Team::whereIn('id',array_unique(array_column($emp_sub,'team_id')))
-                ->pluck('title','id')
-                ->toArray();
-            $equipment_map = Equipment::whereIn('id',array_unique(array_column($emp_sub,'equipment_id')))
-                ->pluck('title','id')
-                ->toArray();
-            $emp_map = Employee::whereIn('id',array_unique(array_column($emp_sub,'employee_id')))
-                ->pluck('emp_name','id')
-                ->toArray();
-
-            $equipment_map1 = $team_map1 = $employee_map1 = $employee_map2 = $equipment_map2 = $team_map2 =  [];
-            foreach ($emp_sub as $value){
-                $equipment_name = $equipment_map[$value['equipment_id']] ?? '';
-                $team_name = $team_map[$value['team_id']] ?? '';
-                $employee_name = $emp_map[$value['employee_id']] ?? '';
-
-                $equipment_map1[$value['dispatch_no']][] = $equipment_name;
-                $equipment_map2[$value['dispatch_no']][] = $value['equipment_id'];
-                $team_map1[$value['dispatch_no']][] = $team_name;
-                $team_map2[$value['dispatch_no']][] = $value['team_id'];
-                $employee_map1[$value['dispatch_no']][] = $employee_name;
-                $employee_map2[$value['dispatch_no']][] = $value['employee_id'];
-            }
-        }
-
+        $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))
+            ->pluck('title','id')
+            ->toArray();
+        $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))
+            ->pluck('title','id')
+            ->toArray();
         $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
             ->pluck('title','id')
             ->toArray();
@@ -461,19 +497,10 @@ class DispatchService extends Service
             $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
             $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;
             $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
-            $tmp = $employee_map1[$value['dispatch_no']] ?? [];
-            $data['data'][$key]['team_man'] = implode(',',array_unique($tmp));
-            $tmp = $employee_map2[$value['dispatch_no']] ?? [];
-            $data['data'][$key]['team_man_id'] = array_unique($tmp);
-            $tmp = $team_map1[$value['dispatch_no']] ?? [];
-            $data['data'][$key]['team_name'] = implode(',',array_unique($tmp));
-            $tmp = $team_map2[$value['dispatch_no']] ?? [];
-            $data['data'][$key]['team_id'] = implode(',',array_unique($tmp));
-            $tmp = $equipment_map1[$value['dispatch_no']] ?? [];
-            $data['data'][$key]['equipment_name'] = implode(',',array_unique($tmp));
-            $tmp = $equipment_map2[$value['dispatch_no']] ?? [];
-            $data['data'][$key]['equipment_id'] = implode(',',array_unique($tmp));
-            $data['data'][$key]['un_finished_quantity'] = $value['dispatch_quantity'] - $value['finished_num'] - $value['waste_num'];
+            $data['data'][$key]['team_name'] = $team_map[$value['team_id']] ?? "";
+            $data['data'][$key]['equipment_name'] = $equipment_map[$value['device_id']] ?? "";
+            $data['data'][$key]['equipment_id'] = $value['device_id'];
+            $data['data'][$key]['un_finished_quantity'] = $value['dispatch_quantity'] - $value['finished_num'];
         }
         $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
 
@@ -484,18 +511,38 @@ class DispatchService extends Service
     public function writeDispatchQuantity($order_product_id){
         if(empty($order_product_id)) return;
 
+        //最后一道工序
+        $last_process = [];
+        $process_id = OrdersProduct::whereIn('id',$order_product_id)
+            ->select('id','process_id')
+            ->get()->toArray();
+        foreach ($process_id as $value){
+            $tmp_process = explode(',', $value['process_id']);
+            $last_process[$value['id']] = end($tmp_process);
+        }
         $result = DispatchSub::where('del_time',0)
             ->whereIn('order_product_id',$order_product_id)
-            ->select(DB::raw("sum(dispatch_quantity) as dispatch_quantity"),'order_product_id')
-            ->groupby('order_product_id')
-            ->pluck('dispatch_quantity','order_product_id')
+            ->select('dispatch_quantity','order_product_id','process_id')
+            ->get()
             ->toArray();
 
         if(empty($result)) return;
+        $new_result = [];
+        foreach ($result as $value){
+           //统计最后一道工序的派工数量
+           $tmp_last_process = $last_process[$value['order_product_id']];
+           if($tmp_last_process == $value['process_id']){
+               if(isset($new_result[$value['order_product_id']])){
+                   $new_result[$value['order_product_id']] += $value['dispatch_quantity'];
+               }else{
+                   $new_result[$value['order_product_id']] = $value['dispatch_quantity'];
+               }
+           }
+        }
 
-        foreach ($result as $key => $value){
-            OrdersProduct::where('id',$key)->update([
-                'dispatch_complete_quantity' => $value
+        foreach ($new_result as $order_product_id => $num){
+            OrdersProduct::where('id',$order_product_id)->update([
+                'dispatch_complete_quantity' => $num
             ]);
         }
     }
@@ -504,15 +551,36 @@ class DispatchService extends Service
     public function writeDispatchQuantityDEL($order_product_id){
         if(empty($order_product_id)) return;
 
+        //最后一道工序
+        $last_process = [];
+        $process_id = OrdersProduct::whereIn('id',$order_product_id)
+            ->select('id','process_id')
+            ->get()->toArray();
+        foreach ($process_id as $value){
+            $tmp_process = explode(',', $value['process_id']);
+            $last_process[$value['id']] = end($tmp_process);
+        }
         $result = DispatchSub::where('del_time',0)
             ->whereIn('order_product_id',$order_product_id)
-            ->select(DB::raw("sum(dispatch_quantity) as dispatch_quantity"),'order_product_id')
-            ->groupby('order_product_id')
-            ->pluck('dispatch_quantity','order_product_id')
+            ->select('dispatch_quantity','order_product_id','process_id')
+            ->get()
             ->toArray();
 
+        $new_result = [];
+        foreach ($result as $value){
+            //统计最后一道工序的派工数量
+            $tmp_last_process = $last_process[$value['order_product_id']];
+            if($tmp_last_process == $value['process_id']){
+                if(isset($new_result[$value['order_product_id']])){
+                    $new_result[$value['order_product_id']] += $value['dispatch_quantity'];
+                }else{
+                    $new_result[$value['order_product_id']] = $value['dispatch_quantity'];
+                }
+            }
+        }
+
         foreach ($order_product_id as $value){
-            $quantity = $result[$value] ?? 0;
+            $quantity = $new_result[$value] ?? 0;
             OrdersProduct::where('id',$value)->update([
                 'dispatch_complete_quantity' => $quantity
             ]);

+ 233 - 70
app/Service/FinishedOrderService.php

@@ -52,30 +52,106 @@ class FinishedOrderService extends Service
         list($status,$msg) = $this->orderRule($data);
         if(!$status) return [$status,$msg];
 
-        $user = [
-            'id' => $user['id'],
-            'operate_time' => time(),
-            'zt' => Request()->header('zt'),
-        ];
-        $redis = [
-            'result' => $msg,
-            'data' => $data,
-        ];
+        //数据源
+        $result = $msg;
+        $time = time();
+        try{
+            DB::beginTransaction();
+            foreach ($result as $value){
+                $finished_num = $value['quantity'] + $value['finished_num'];
+                $waste_num = $value['waste_quantity'] + $value['waste_num'];
 
-        $job = ProcessDataJob::dispatch($redis,$user,1)->onQueue('finished_operation');
-        if(! $job){
-            return [false,'任务没有进入队列!'];
-        }
+                DispatchSub::where('id',$value['id'])->update([
+                    'finished_num' => $finished_num,
+                    'waste_num' => $waste_num,
+                ]);
 
-        //错误计数
-        Redis::hSet('order_failures', md5(json_encode($redis)), 0);
+                $insert_waste = $scrapp = [];
+                if(! empty($value['waste_array'])){
+                    foreach ($value['waste_array'] as $v){
+                        for($i = 0 ;$i < $v['num']; $i++){
+                            $insert_waste[] = [
+                                'order_product_id' => $value['order_product_id'],
+                                'order_no' => $value['order_no'],
+                                'product_no' => $value['product_no'],
+                                'product_title' => $value['product_title'],
+                                'process_id' => $value['process_id'],
+                                'crt_time' => $time,
+                                'dispatch_no' => $value['dispatch_no'],
+                                'status' => 4,//不良品
+                                'team_id' => $value['team_id'],
+                                'finished_id' => $value['finish_id'],
+                                'equipment_id' => $value['device_id'],
+                                'scrapp_id' => $v['scrapp_id']
+                            ];
+                        }
+                        $scrapp[] = [
+                            'sale_orders_product_id' => $value['sale_orders_product_id'],
+                            'order_product_id' => $value['order_product_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_time' => $time,
+                            'scrapp_num' => $v['num'],
+                            'scrapp_id' => $v['scrapp_id']
+                        ];
+                    }
+                }
 
-        //标记进入队列的数据
-        DispatchSub::whereIn('id',$data['id'])->update([
-            'job_status' => 1,
-        ]);
+                //工序表
+                $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
+                $process_model->where('order_product_id',$value['order_product_id'])
+                    ->where('process_id',$value['process_id'])
+                    ->where('dispatch_no',$value['dispatch_no'])
+                    ->take($value['quantity'])
+                    ->update([
+                        'finished_time' => $time,
+                        'status' => 2,
+                        'finished_id' => $value['finish_id'],
+                        'team_id' => $value['team_id'],
+                        'equipment_id' => $value['device_id'],
+                    ]);
 
-        return [true,'任务已进入队列!'];
+                if(! empty($insert_waste)) $process_model->insert($insert_waste);
+                if(! empty($scrapp)) {
+                    $scrapp_model = new ScrappCount();
+                    $scrapp_model->insert($scrapp);
+                }
+
+                if(! empty($value['waste_quantity'])){
+                    $num = $value['waste_quantity'];
+                    OrdersProduct::where('id',$value['order_product_id'])->update([
+                        'scrapp_num' => DB::raw("scrapp_num + {$num}"),
+                    ]);
+                }
+            }
+
+            //反写数量
+            //生产订单
+            $id = array_column($result,'order_product_id');
+            $this->writeFinishedQuantityByOrdersProductId($id);
+            //销售订单
+            $this->writeFinishedQuantity($id);
+
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
+        }
+
+        return [true, ''];
     }
 
     public function addInJob($result,$data,$user){
@@ -235,8 +311,8 @@ class FinishedOrderService extends Service
             }
 
             //反写数量
-            $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
-            $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
+//            $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
+//            $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
             DB::commit();
         }catch (\Exception $e){
             DB::rollBack();
@@ -292,29 +368,93 @@ class FinishedOrderService extends Service
     }
 
     public function orderRule($data){
-        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
-        if($this->isEmpty($data,'quantity')) return [false,'请填写完工数量!'];
-        foreach ($data['quantity'] as $value){
-            if(! is_numeric($value) || $value <= 0) return [false,'请填写正确的完工数量!'];
-        }
-        if($this->isEmpty($data,'finish_id') && $this->isEmpty($data,'team_id')) return [false,'人员和班组不能都为空!'];
+        if(! isset($data['is_finish_all'])) return [false, '完工类型不能为空!'];
+        if(empty($data['detail'])) return [false, '完工明细数据不能为空!'];
+        $detail_map = $id = $detail_map2 = $waste_map = [];
+        foreach ($data['detail'] as $value){
+            if(empty($value['id'])) return [false,'请选择完工数据!'];
+            $id[] = $value['id'];
+            if(empty($value['device_id'])) return [false, '请选择设备!'];
+            if(empty($value['team_id'])) return [false,'请选择班组!'];
+            if(empty($value['finish_id'])) return [false,'请选择人员!'];
+            if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'完工数量不能小于0!'];
+            if(! empty($value['waste'])){
+                foreach ($value['waste'] as $waste){
+                    if(empty($waste['scrapp_id'])) return [false,'请选择损耗原因!'];
+                    if(! is_numeric($value['num']) || $value['num'] < 0) return [false,'损耗数量不能小于0!'];
+                    if(isset($waste_map[$value['id']])){
+                        $waste_map[$value['id']] += $value['num'];
+                    }else{
+                        $waste_map[$value['id']] = $value['num'];
+                    }
+                }
+            }
 
-        $bool = DispatchSub::whereIn('id',$data['id'])->where('job_status',1)->exists();
-        if($bool) return [false,'正在队列中,请不要重复操作!'];
+            //完工数量
+            if(isset($detail_map[$value['id']])){
+                $detail_map[$value['id']] += $value['quantity'];
+            }else{
+                $detail_map[$value['id']] = $value['quantity'];
+            }
 
-        $result = DispatchSub::whereIn('id',$data['id'])
-            ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id')
-            ->orderBy('id','desc')
+            if(! isset($detail_map2[$value['id']])){
+                $detail_map2[$value['id']] = $value;
+            }
+        }
+
+        //校验
+        $result = DispatchSub::where('del_time',0)
+            ->whereIn('id',$id)
+            ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id','crt_time')
             ->get()->toArray();
+        if(empty($result)) return [false, '派工单不存在或已被删除!'];
+        $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))->pluck('title','id')->toArray();
+        $search = "";$args = [];
+        foreach ($result as $key => $value){
+            $detail2 = $detail_map2[$value['id']] ?? [];
+            $result[$key]['device_id'] = $detail2['device_id'] ?? 0;
+            $result[$key]['team_id'] = $detail2['team_id'] ?? 0;
+            $result[$key]['finish_id'] = $detail2['finish_id'] ?? 0;
+            $result[$key]['waste_quantity'] = $waste_map[$value['id']] ?? 0;
+            $result[$key]['waste_array'] = $detail2['waste'] ?? [];
+            $q = $detail_map[$value['id']] ?? 0;
+            $quantity_tmp = $q + $value['finished_num'];
+            if($quantity_tmp > $value['dispatch_quantity']) {
+                $process_tmp = $process_map[$value['process_id']] ?? "";
+                return [false,'派工单:' . $value['dispatch_no']. '的工序' . $process_tmp .'完工数量不能大于派工数量'];
+            }
+            $result[$key]['quantity'] = $q;
 
-        $waste = [];
-        foreach ($data['waste'] as $key => $value){
-            $waste[$key] = array_sum(array_column($value,'num'));
+            $search_key = $value['order_product_id'] . $value['crt_time'];
+            if($data['is_finish_all'] && ! in_array($search_key , $args)){
+                $search .= "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
+                $args[] = $search_key;
+            }
         }
-        foreach ($result as $key => $value){
-            $tmp = $waste[$key] ?? 0;
-            $quantity_tmp = $data['quantity'][$key] + $value['finished_num'] + $value['waste_num'] + $tmp;
-            if($quantity_tmp > $value['dispatch_quantity']) return [false,'完工数量加上损耗数量不能大于派工数量'];
+        $search = rtrim($search,'OR ');
+        $search = "($search)";
+
+        if($data['is_finish_all']){
+            //系统帮助完工的数据
+            $result2 = DispatchSub::where('del_time',0)
+                ->whereNotIn('id',$id)
+                ->whereColumn('dispatch_quantity', '>', 'finished_num')
+                ->whereRaw($search)
+                ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id','crt_time')
+                ->get()->toArray();
+            if(! empty($result2)){
+                foreach ($result2 as $key => $value){
+                    $not_finished_num = $value['dispatch_quantity'] - $value['finished_num'];
+                    $result2[$key]['quantity'] = $not_finished_num;
+                    $result2[$key]['waste_array'] = [];
+                    $result2[$key]['waste_quantity'] = 0;
+                    $result2[$key]['device_id'] = 0;
+                    $result2[$key]['team_id'] = 0;
+                    $result2[$key]['finish_id'] = 0;
+                }
+            }
+
+            $result = array_merge_recursive($result, $result2);
         }
 
         return [true, $result];
@@ -426,50 +566,73 @@ class FinishedOrderService extends Service
         return $data;
     }
 
-    //反写写完工数量(根据销售订单id)
-    public function writeFinishedQuantity($sale_orders_product_id,$database){
-        if(empty($sale_orders_product_id)) return;
+    //反写销售订单完工数量
+    public function writeFinishedQuantity($orders_product_id){
+        if(empty($orders_product_id)) return;
 
-        $models = new DispatchSub();
-        $models->setConnection($database);
-        $result = $models->where('del_time',0)
-            ->whereIn('sale_orders_product_id',$sale_orders_product_id)
+        $sale_orders_product_id = OrdersProduct::where('del_time',0)
+            ->whereIn('id', $orders_product_id)
+            ->select('sale_orders_product_id')
+            ->get()->toArray();
+        $sale_orders_product_id = array_unique(array_column($sale_orders_product_id,'sale_orders_product_id'));
+
+        $production_order = OrdersProduct::where('del_time',0)
+            ->whereIn('sale_orders_product_id', $sale_orders_product_id)
             ->select(DB::raw("sum(finished_num) as finished_num"),'sale_orders_product_id')
             ->groupby('sale_orders_product_id')
             ->pluck('finished_num','sale_orders_product_id')
             ->toArray();
 
-        if(empty($result)) return;
-
-        $models = new SaleOrdersProduct();
-        $models->setConnection($database);
-        foreach ($result as $key => $value){
-            $models->where('id',$key)->update([
-                'finished_num' => $value
+        foreach ($production_order as $sale_orders_product_id => $finished_num){
+            SaleOrdersProduct::where('id',$sale_orders_product_id)->update([
+                'finished_num' => $finished_num,
             ]);
         }
     }
 
-    //反写写完工数量(根据生产订单id)
-    public function writeFinishedQuantityByOrdersProductId($order_product_id,$database){
+    //反写生产订单完工数量
+    public function writeFinishedQuantityByOrdersProductId($order_product_id){
         if(empty($order_product_id)) return;
 
-        $models = new DispatchSub();
-        $models->setConnection($database);
-        $result = $models->where('del_time',0)
+        $result = DispatchSub::where('del_time',0)
             ->whereIn('order_product_id',$order_product_id)
-            ->select(DB::raw("sum(finished_num) as finished_num"),'order_product_id')
-            ->groupby('order_product_id')
-            ->pluck('finished_num','order_product_id')
+            ->select('finished_num','order_product_id','crt_time','process_id')
+            ->get()
             ->toArray();
-
         if(empty($result)) return;
 
-        $model = new OrdersProduct();
-        $model->setConnection($database);
-        foreach ($result as $key => $value){
-            $model->where('id',$key)->update([
-                'finished_num' => $value
+        //生产订单
+        $production_order = OrdersProduct::where('del_time',0)
+            ->whereIn('id',array_unique(array_column($result,'order_product_id')))
+            ->select('process_id','id','production_quantity')
+            ->get()
+            ->toArray();
+
+        $judge = [];
+        foreach ($result as $value){
+            if(isset($judge[$value['order_product_id']][$value['process_id']])){
+                $judge[$value['order_product_id']][$value['process_id']] += $value['finished_num'];
+            }else{
+                $judge[$value['order_product_id']][$value['process_id']] = $value['finished_num'];
+            }
+        }
+
+        foreach ($production_order as $value){
+            //生产订单下的所有工序派工单
+            $tmp = $judge[$value['id']] ?? [];
+            if(empty($tmp)) continue;
+            //生产订单下的工序种类
+            $ori_process = explode(',',$value['process_id']);
+            $ori_process_count = count($ori_process);
+            //生产订单下的派工单的工序种类
+            $process = array_keys($tmp);
+            $process_count = count($process);
+            if($ori_process_count != $process_count) continue;
+            //获取工序里最小的数值
+            $min_finished_num = min($tmp);
+            //更新完工数量
+            OrdersProduct::where('id',$value['id'])->update([
+                'finished_num' => $min_finished_num
             ]);
         }
     }
@@ -671,8 +834,8 @@ class FinishedOrderService extends Service
             }
 
             //反写数量
-            $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
-            $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
+//            $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
+//            $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
             DB::commit();
             //本地数据写入结束-----------
         }catch (\Exception $e){

+ 3 - 1
app/Service/ProcessService.php

@@ -102,6 +102,7 @@ class ProcessService extends Service
         $model->wages_unit = $data['wages_unit'] ?? 0;
         $model->wages = $data['wages'] ?? '';
         $model->team_id = $data['team_id'] ?? 0;
+        $model->device_id = $data['device_id'] ?? 0;
         $model->save();
 
         return [true,'保存成功!'];
@@ -121,7 +122,8 @@ class ProcessService extends Service
         $model->capacity = $data['capacity'] ?? '';
         $model->wages_unit = $data['wages_unit'] ?? 0;
         $model->wages = $data['wages'] ?? '';
-        $model->team_id = $data['team_id'] ?? '';
+        $model->team_id = $data['team_id'] ?? 0;
+        $model->device_id = $data['device_id'] ?? 0;
         $model->save();
 
         return [true,'保存成功!'];

+ 14 - 10
app/Service/ProductionOrderService.php

@@ -7,6 +7,7 @@ use App\Model\OrdersProduct;
 use App\Model\OrdersProductBom;
 use App\Model\OrdersProductMain;
 use App\Model\OrdersProductProcess;
+use App\Model\Process;
 use App\Model\SaleOrdersProduct;
 use App\Model\Technology;
 use Illuminate\Support\Facades\DB;
@@ -52,7 +53,7 @@ class ProductionOrderService extends Service
         if(empty($production_no)) return [false,'单据号生成失败!'];
         
         //工序
-        $process_arr = explode(',',$data['technology']['process_id']);
+        $process_arr = $data['process_id'];
 
         try{
             DB::beginTransaction();
@@ -60,7 +61,7 @@ class ProductionOrderService extends Service
             $time = time();
 
             //主表数据写入
-            OrdersProductMain::insert(['production_no' => $production_no,'crt_time' => $time,'crt_id' => $user['id']]);
+            OrdersProductMain::insert(['production_no' => $production_no,'crt_time' => $time,'crt_id' => $user['id'], 'process_id' => implode(',',$process_arr)]);
 
             //生产数据的源数据
             $result = $msg[0];
@@ -69,6 +70,7 @@ class ProductionOrderService extends Service
             $boom = $process = [];
 
             foreach ($result as $key => $value){
+                $result[$key]['process_id'] = implode(',', $process_arr);
                 $result[$key]['production_no'] = $production_no;
                 $result[$key]['production_quantity'] = $quantity_map[$value['sale_orders_product_id']];
                 $result[$key]['production_time'] = $time;
@@ -186,14 +188,16 @@ class ProductionOrderService extends Service
     }
 
     public function orderRule(&$data){
-        if($this->isEmpty($data,'technology_id')) return [false, '工艺路线不能为空!'];
-        //工艺 -> 工序
-        $technology = Technology::where('del_time',0)
-            ->where('id',$data['technology_id'])
-            ->first();
-        if(empty($technology)) return [false,'工艺路线不存在或已被删除!'];
-        $technology = $technology->toArray();
-        $data['technology'] = $technology;
+        if($this->isEmpty($data,'process_id')) return [false, '工序不能为空!'];
+        //工序
+        $process = Process::whereIn('id', $data['process_id'])->get()->toArray();
+        if(empty($process)) return [false,'工序不存在或已被删除!'];
+        $process_id = [];
+        foreach ($process as $value){
+            if($value['del_time'] > 0) return [false, '工序:' . $value['title'] . '不存在或已被删除!'];
+            $process_id[] = $value['id'];
+        }
+        $data['process_id'] = $process_id;
         if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
         if($this->isEmpty($data,'quantity')) return [false,'数量不能为空!'];
         if(in_array(false, $data['quantity'], true) || in_array(0, $data['quantity'], true) || in_array('', $data['quantity'], true)) return [false,'数量不能为空!'];