isEmpty($data,'id')) return [false,'数据必须选择!']; if($this->isEmpty($data,'type')) return [false,'单据类型不能为空!']; switch ($data['type']){ case 1: list($status,$msg) = $this->delSaleOrdersProduct($data['id']); break; case 2: list($status,$msg) = $this->delOrdersProduct($data['id']); break; case 3: list($status,$msg) = $this->delDispatch($data['id']); break; case 4: list($status,$msg) = $this->delFinished($data['id']); break; default: list($status,$msg) = [false,'删除失败!']; } return [$status,$msg]; } //销售订单删除 public function delSaleOrdersProduct($id){ $bool = OrdersProduct::where('del_time',0) ->whereIn('sale_orders_product_id',$id) ->exists(); if($bool) return [false,'销售订单已生成生产订单,删除失败!']; try { DB::beginTransaction(); //销售订单 SaleOrdersProduct::whereIn('id',$id)->update([ 'del_time' => time() ]); //内部订单号与销售订单关联表 $subquery = DB::table('sale_orders_product') ->select('order_no') ->whereIn('id',$id); $order_no = DB::table('sale_orders_product') ->select('sale_orders_product.order_no','sale_orders_product.del_time') ->joinSub($subquery, 'sub', function ($join) { $join->on('sale_orders_product.order_no', '=', 'sub.order_no'); })->get()->toArray(); $update_order = []; if(! empty($order_no)){ $tmp = []; foreach ($order_no as $value){ $value = (array)$value; if($value['del_time'] == 0){ $tmp[] = $value['order_no']; }else{ $update_order[] = $value['order_no']; } } $tmp = array_unique($tmp); $update_order = array_unique($update_order); $update_order = array_diff($update_order, $tmp); } if(! empty($update_order)) Orders::whereIn('order_no',$update_order)->update(['del_time' => time()]); DB::commit(); }catch (\Throwable $e){ DB::rollBack(); return [false,$e->getMessage()]; } return [true,'']; } //生产订单删除 public function delOrdersProduct($id){ $bool = DispatchSub::where('del_time',0) ->whereIn('order_product_id',$id) ->exists(); if($bool) return [false,'生产订单已生成派工单,删除失败!']; try { DB::beginTransaction(); //生产订单 OrdersProduct::whereIn('id',$id)->update([ 'del_time' => time() ]); //生产订单主表 $subquery = DB::table('orders_product') ->select('production_no') ->whereIn('id',$id); $order_no = DB::table('orders_product') ->select('orders_product.production_no','orders_product.del_time') ->joinSub($subquery, 'sub', function ($join) { $join->on('orders_product.production_no', '=', 'sub.production_no'); })->get()->toArray(); $update_order = []; if(! empty($order_no)){ $tmp = []; foreach ($order_no as $value){ $value = (array)$value; if($value['del_time'] == 0){ $tmp[] = $value['production_no']; }else{ $update_order[] = $value['production_no']; } } $tmp = array_unique($tmp); $update_order = array_unique($update_order); $update_order = array_diff($update_order, $tmp); } if(! empty($update_order)) OrdersProductMain::whereIn('production_no',$update_order)->update(['del_time' => time()]); //生产订单子表 $message = OrdersProduct::whereIn('id',$id)->select('out_order_no_time','sale_orders_product_id')->get()->toArray(); $time = array_unique(array_column($message,'out_order_no_time')); $arr_time = []; if(! empty($time)){ foreach ($time as $value){ $time_tmp = date("Ymd", $value); if(! in_array($time_tmp,$arr_time)) $arr_time[] = $time_tmp; } } if(! empty($arr_time)){ foreach ($arr_time as $value){ $modelBom = new OrdersProductBom(['channel'=> $value]); $modelBom->where('order_product_id',$id)->update(['del_time' => time()]); $modelProcess = new OrdersProductProcess(['channel' => $value]); $modelProcess->where('order_product_id',$id)->update(['del_time' => time()]); } } //销售订单里的已生产数量 (new ProductionOrderService())->writeProductionQuantityDEL(array_column($message,'sale_orders_product_id')); DB::commit(); }catch (\Throwable $e){ DB::rollBack(); return [false,$e->getMessage()]; } return [true,'']; } //派工单删除 public function delDispatch($id){ $bool = DispatchSub::where('del_time',0) ->whereIn('id',$id) ->where('finished_num','>',0) ->exists(); if($bool) return [false,'工序派工单已有完工操作,删除失败!']; try { DB::beginTransaction(); //工序派工单 DispatchSub::whereIn('id',$id)->update([ 'del_time' => time() ]); //工序派工单主表 $subquery = DB::table('dispatch_sub') ->select('dispatch_no') ->whereIn('id',$id); $order_no = DB::table('dispatch_sub') ->select('dispatch_sub.dispatch_no','dispatch_sub.del_time') ->joinSub($subquery, 'sub', function ($join) { $join->on('dispatch_sub.dispatch_no', '=', 'sub.dispatch_no'); })->get()->toArray(); $update_order = []; if(! empty($order_no)){ $tmp = []; foreach ($order_no as $value){ $value = (array)$value; if($value['del_time'] == 0){ $tmp[] = $value['dispatch_no']; }else{ $update_order[] = $value['dispatch_no']; } } $tmp = array_unique($tmp); $update_order = array_unique($update_order); $update_order = array_diff($update_order, $tmp); } 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(); if(! empty($message)){ foreach ($message as $value){ $tmp_time = date('Ymd',$value['out_order_no_time']); $modelProcess = new OrdersProductProcess(['channel' => $tmp_time]); $modelProcess->where('order_product_id',$value['order_product_id']) ->where('process_id',$value['process_id']) ->where('dispatch_no',$value['dispatch_no']) ->take($value['dispatch_quantity']) ->update([ 'dispatch_no' => '', 'status' => 0 ]); DispatchEmpSub::where('order_product_id',$value['order_product_id']) ->where('dispatch_no',$value['dispatch_no']) ->update([ 'del_time' => time() ]); } } //已派工数量 (new DispatchService())->writeDispatchQuantity(array_column($message,'order_product_id')); DB::commit(); }catch (\Throwable $e){ DB::rollBack(); return [false,$e->getMessage()]; } return [true,'']; } //完工单删除 public function delFinished($id){ $result = DispatchSub::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') ->orderBy('id','desc') ->get()->toArray(); try { DB::beginTransaction(); foreach ($result as $key => $value){ SaleOrdersProduct::where('id',$value['sale_orders_product_id'])->decrement('finished_num', $value['finished_num']); $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['finished_num']) ->update([ 'finished_time' => 0, 'status' => 1, 'finished_id' => 0, 'team_id' => 0, 'equipment_id' => 0 ]); $process_model->where('order_product_id',$value['order_product_id']) ->where('process_id',$value['process_id']) ->where('dispatch_no',$value['dispatch_no']) ->where('status',4) ->update([ 'del_time' => time() ]); $result[$key]['quantity'] = $value['finished_num']; } DispatchSub::whereIn('id',$id)->update([ 'finished_num' => 0, 'waste_num' => 0 ]); DB::commit(); }catch (\Throwable $e){ DB::rollBack(); return [false,$e->getMessage()]; } return [true,'']; } }