MaterialRule($data,false); if(!$status) return [$status,$msg]; DB::beginTransaction(); try{ $time = time(); $model = new ApplyOrder(); $model = $model->where('id',$data['id'])->first(); $model->apply_id = $data['apply_id']; $model->apply_time = $data['apply_time'] ; $model->mark = $data['mark'] ?? ""; $model->storehouse_id = $data['storehouse_id']; $model->storehouse_title = $data['storehouse_title'] ?? ""; $model->type = $data['type']; $model->save(); $id = $model->id; ApplyOrderDetail::where('del_time',0)->where('apply_order_id',$id)->update([ 'del_time' => $time ]); $detail_insert = []; foreach ($data['order_data'] as $v){ $detail_insert[] = [ 'apply_order_id' => $id, 'data_id' => $v['id'], 'quantity' => $v['quantity'] ?? 0, 'product_no' => $v['product_no'] ?? "", 'product_title' => $v['product_title'] ?? "", 'product_size' => $v['product_size'] ?? "", 'product_unit' => $v['product_unit'] ?? "", 'technology_name' => $v['technology_name'] ?? "", //颜色 'top_product_title' => $v['top_product_title'] ?? "", 'top_product_no' => $v['top_product_title'] ?? "", 'type' => $data['type'], 'storehouse_id' => $data['storehouse_id'], 'crt_time' => $time, ]; } ApplyOrderDetail::insert($detail_insert); DB::commit(); }catch (\Exception $e){ DB::rollBack(); return [false,$e->getMessage()]; } return [true,'']; } public function MaterialAdd($data,$user){ list($status,$msg) = $this->MaterialRule($data); if(!$status) return [$status,$msg]; DB::beginTransaction(); try{ $time = time(); $model = new ApplyOrder(); $model->order_number = $data['order_number']; $model->apply_id = $data['apply_id']; $model->apply_time = $data['apply_time'] ; $model->mark = $data['mark'] ?? ""; $model->storehouse_id = $data['storehouse_id']; $model->storehouse_title = $data['storehouse_title'] ?? ""; $model->type = $data['type']; $model->crt_id = $user['id']; $model->save(); $id = $model->id; $detail_insert = []; foreach ($data['order_data'] as $v){ $detail_insert[] = [ 'apply_order_id' => $id, 'data_id' => $v['id'], 'quantity' => $v['quantity'] ?? 0, 'product_no' => $v['product_no'] ?? "", 'product_title' => $v['product_title'] ?? "", 'product_size' => $v['product_size'] ?? "", 'product_unit' => $v['product_unit'] ?? "", 'technology_name' => $v['technology_name'] ?? "", //颜色 'top_product_title' => $v['top_product_title'] ?? "", 'top_product_no' => $v['top_product_title'] ?? "", 'type' => $data['type'], 'storehouse_id' => $data['storehouse_id'], 'crt_time' => $time, ]; } ApplyOrderDetail::insert($detail_insert); DB::commit(); }catch (\Exception $e){ DB::rollBack(); return [false,$e->getMessage()]; } return [true, '']; } public function MaterialDel($data){ if($this->isEmpty($data,'id')) return [false,'ID必须!']; $apply = ApplyOrder::where('id',$data['id'])->first(); if($apply->del_time > 0) return [false,'申请单不存在或已被删除']; if($apply->state != ApplyOrder::state_zero) return [false, '申请单已审核,删除失败']; $time = time(); try { DB::beginTransaction(); $apply->del_time = $time; $apply->save(); ApplyOrderDetail::where('del_time', 0)->where('apply_order_id',$data['id'])->update([ 'del_time'=>time() ]); DB::commit(); }catch (\Throwable $exception){ DB::rollBack(); return [false, $exception->getMessage()]; } return [true,'']; } public function MaterialList($data){ $model = ApplyOrder::where('del_time',0) ->select('*') ->orderBy('id','desc'); if(isset($data['status'])) $model->where('status', $data['status']); if(! empty($data['type'])) $model->where('type', $data['type']); if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%'); if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%'); if(! empty($data['apply_id'])) $model->where('apply_id', $data['apply_id']); if(! empty($data['pc_number'])) { if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){ $dispatch_id = DispatchSub::where('del_time',0) ->where('out_order_no', 'LIKE', '%'.$data['pc_number'].'%') ->select('id') ->get()->toArray(); $dispatch_id = array_column($dispatch_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $dispatch_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); }elseif($data['type'] == ApplyOrder::type_five){ $zj_id = ScrappCount::where('del_time',0) ->where('out_order_no', 'LIKE', '%'.$data['pc_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['pc_number'].'%') ->select('order_no') ->get()->toArray(); $no = array_unique(array_column($no,'order_no')); $box_id = Box::where('del_time',0) ->whereIn('top_order_no', $no) ->select('id') ->get()->toArray(); $box_id = array_column($box_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $box_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); } } if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']); if(! empty($data['apply_time'][0]) && ! empty($data['apply_time'][1])) $model->whereBetween('apply_time',[$data['apply_time'][0],$data['apply_time'][1]]); if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]); if(! empty($data['dispatch_no'])){ $dispatch_id = DispatchSub::where('del_time',0) ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%') ->select('id') ->get()->toArray(); $dispatch_id = array_column($dispatch_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $dispatch_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); } if(! empty($data['box_no'])){ $box_id = Box::where('del_time',0) ->where('order_no', 'LIKE', '%'.$data['box_no'].'%') ->select('id') ->get()->toArray(); $box_id = array_column($box_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $box_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); } if(! empty($data['sale_order_number'])){ if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){ $dispatch_id = DispatchSub::where('del_time',0) ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%') ->select('id') ->get()->toArray(); $dispatch_id = array_column($dispatch_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $dispatch_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); }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'].'%') ->select('order_no') ->get()->toArray(); $no = array_unique(array_column($no,'order_no')); $box_id = Box::where('del_time',0) ->whereIn('top_order_no', $no) ->select('id') ->get()->toArray(); $box_id = array_column($box_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $box_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); } } $list = $this->limit($model,'',$data); $list = $this->fillData($list); return [true,$list]; } public function fillData($data){ if(empty($data['data'])) return $data; $emp = Employee::whereIn('id',array_merge_recursive(array_unique(array_column($data['data'],'crt_id')), array_unique(array_column($data['data'],'apply_id')))) ->pluck('emp_name','id') ->toArray(); $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']; } } $order1 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_one,ApplyOrder::type_two]) ->whereIn('apply_order_id', $array1) ->select('data_id','apply_order_id') ->get()->toArray(); $order1_map = []; foreach ($order1 as $value){ $order1_map[$value['apply_order_id']][] = $value['data_id']; } $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(); $order2_map = []; 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(); $map2 = Box::whereIn('id', array_unique(array_column($order2,'data_id'))) ->pluck('top_order_no','id') ->toArray(); $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2))) ->pluck('out_order_no','order_no') ->toArray(); $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',$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']] ?? ''; if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){ $order_tmp = $order1_map[$value['id']] ?? []; $order_tmp = array_unique($order_tmp); $sale_order_number = []; foreach ($order_tmp as $v){ $str = $map1[$v] ?? ''; if(empty($str)) continue; $sale_order_number[] = $str; } $sale_order_number = array_unique($sale_order_number); $sale_order_number = implode(',',$sale_order_number); $data['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); $sale_order_number = []; foreach ($order_tmp as $v){ $m2_t = $map2[$v] ?? ''; $m2_t = $map2_fin[$m2_t] ?? ""; if(empty($m2_t)) continue; $sale_order_number[] = $m2_t; } $sale_order_number = array_unique($sale_order_number); $sale_order_number = implode(',',$sale_order_number); $data['data'][$key]['sale_order_number'] = $sale_order_number; } } return $data; } public function MaterialDetail($data){ if($this->isEmpty($data,'id')) return [false,'ID不能为空!']; $id = $data['id']; $detail = ApplyOrder::where('del_time',0) ->where('id',$id) ->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) ->get()->toArray(); $apply_id = array_column($apply_d, 'data_id'); $d = []; $out_order_no = ""; if($detail['type'] == ApplyOrder::type_one){ $d = DispatchSub::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time','out_order_no') ->get()->toArray(); $out_order_no = $d[0]['out_order_no'] ?? ""; $args = ""; foreach ($d as $value){ $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR "; } $args = rtrim($args, 'OR '); $d_no_map = []; $d_no = DispatchSub::where('del_time',0) ->whereRaw($args) ->select('dispatch_no as order_no','order_product_id','crt_time') ->get()->toArray(); foreach ($d_no as $value){ if(! isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){ $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no']; } // if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){ // if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no']; // }else{ // $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no']; // } } }elseif ($detail['type'] == ApplyOrder::type_two){ $d = DispatchSub::where('del_time',0) ->whereIn('id', $apply_id) ->select('id','out_order_no', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time') ->get()->toArray(); $out_order_no = $d[0]['out_order_no'] ?? ""; $args = ""; foreach ($d as $value){ $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR "; } $args = rtrim($args, 'OR '); $d_no_map = []; $d_no = DispatchSub::where('del_time',0) ->whereRaw($args) ->select('dispatch_no as order_no','order_product_id','crt_time') ->get()->toArray(); foreach ($d_no as $value){ if(! isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){ $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no']; } // if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){ // if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no']; // }else{ // $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no']; // } } }elseif ($detail['type'] == ApplyOrder::type_three){ $d = Box::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'order_no','top_order_no') ->get()->toArray(); $map2_fin = SaleOrdersProduct::whereIn('order_no',array_column($d,'top_order_no')) ->pluck('out_order_no','order_no') ->toArray(); foreach ($d as $value){ $out_order_no = $map2_fin[$value['top_order_no']] ?? ""; } }elseif ($detail['type'] == ApplyOrder::type_four){ $d = Box::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'order_no','top_order_no') ->get()->toArray(); $map2_fin = SaleOrdersProduct::whereIn('order_no',array_column($d,'top_order_no')) ->pluck('out_order_no','order_no') ->toArray(); foreach ($d as $value){ $out_order_no = $map2_fin[$value['top_order_no']] ?? ""; } }elseif ($detail['type'] == ApplyOrder::type_five){ //质检入库申请 $d = ScrappCount::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'out_order_no','order_number as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time') ->get()->toArray(); $out_order_no = $d[0]['out_order_no'] ?? ""; }elseif ($detail['type'] == ApplyOrder::type_six){ $d = Box::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'order_no','top_order_no') ->get()->toArray(); $map2_fin = SaleOrdersProduct::whereIn('order_no',array_column($d,'top_order_no')) ->pluck('out_order_no','order_no') ->toArray(); foreach ($d as $value){ $out_order_no = $map2_fin[$value['top_order_no']] ?? ""; } } $detail['sale_order_number'] = $out_order_no; $d_tmp = array_column($d,null,'id'); $return = []; foreach ($apply_d as $t){ $tmp = $d_tmp[$t['data_id']] ?? []; $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? ""; $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? ""; $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? ""; $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? ""; if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){ $order_no = implode(',', array_values($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])); }else{ $order_no = $tmp['order_no'] ?? ""; } if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){ $product_unit = "吨"; }else{ $product_unit = $t['product_unit'] ?? ""; } $return[] = [ 'id' => $t['data_id'] ?? 0, 'quantity' => $t['quantity'] ?? 0, 'product_no' => $t['product_no'] ?? "", 'product_title' => $t['product_title'] ?? "", 'product_size' => $t['product_size'] ?? "", 'product_unit' => $product_unit, 'top_product_title' => $top_product_title, 'top_product_no' => $top_product_no, 'technology_name' => $technology_name, //颜色 'wood_name' => $wood_name, 'order_no' => $order_no, ]; } $detail['order_data'] = $return; return [true, $detail]; } public function MaterialRule(&$data,$is_add = true){ if($this->isEmpty($data,'apply_id')) return [false,'申请人不能为空']; if($this->isEmpty($data,'apply_time')) return [false,'申请时间不能为空']; if($this->isEmpty($data,'storehouse_id')) return [false,'仓库不能为空']; if($this->isEmpty($data,'type')) return [false,'申请单类型不能为空']; if(empty($data['order_data'])) return [false, '申请单详细信息不能为空']; foreach ($data['order_data'] as $value){ if(empty($value['id'])) return [false, '申请单详细信息ID不能为空']; if($data['type'] != ApplyOrder::type_three){ if(empty($value['quantity'])) return [false, '申请单详细信息数量不能为空']; } } $id = array_unique(array_column($data['order_data'],'id')); if($data['type'] == ApplyOrder::type_one){ $dispatch = DispatchSub::where('del_time',0) ->whereIn('id', $id) ->select('id', 'dispatch_no', 'status') ->get()->toArray(); if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择']; }elseif ($data['type'] == ApplyOrder::type_two){ $dispatch = DispatchSub::where('del_time',0) ->whereIn('id', $id) ->select('id', 'dispatch_no', 'status') ->get()->toArray(); if(count($dispatch) != count($id)) return [false, '完工单信息错误,请重新选择']; }elseif ($data['type'] == ApplyOrder::type_three){ $box = Box::where('del_time',0) ->whereIn('id', $id) ->select('id', 'order_no', 'status') ->get()->toArray(); if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择']; }elseif ($data['type'] == ApplyOrder::type_four){ $box = Box::where('del_time',0) ->whereIn('id', $id) ->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, '申请单类型错误']; } if($is_add){ $data['order_number'] = $this->setOrderNO($data['type']); if(empty($data['order_number'])) return [false, '申请单唯一标识生成失败']; }else{ if(empty($data['id'])) return [false, '申请单唯一标识不能为空']; if(empty($data['order_number'])) return [false, '申请单唯一标识不能为空']; $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 = "",$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) ->max('order_number'); if(empty($order_number)){ $number = str_pad(1,3,'0',STR_PAD_LEFT); $number = $str . $number; }else{ $tmp = substr($order_number, -3); $tmp = $tmp + 1; //超过999 // if(strlen($tmp) > 3) return ''; $number = str_pad($tmp,3,'0',STR_PAD_LEFT); $number = $str . $number; } return $number; } public function createSQ($data, $user, $type = 0, $status = 1,$time = 0){ if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空']; if($type == ApplyOrder::type_one){ $storehouse_id = "001"; $storehouse_title = "原料仓"; }elseif ($type == ApplyOrder::type_two){ $storehouse_id = "004"; $storehouse_title = "待清洗原材料仓"; }elseif ($type == ApplyOrder::type_three){ $storehouse_id = "002"; $storehouse_title = "产成品仓"; }elseif($type == ApplyOrder::type_four){ $storehouse_id = "003"; $storehouse_title = "包材、辅料仓" ; }elseif ($type == ApplyOrder::type_six){ $storehouse_id = "004"; $storehouse_title = "待清洗原材料仓"; } try{ DB::beginTransaction(); $order_number = $this->setOrderNO($type, $time); // list($status,$msg) = $this->limitingSendRequestBackg("spAdd" . $order_number . $type); // if(! $status) return [false, $msg]; if(empty($time)) $time = time(); $model = new ApplyOrder(); $model->order_number = $order_number; $model->apply_id = $user['id']; $model->apply_time = $time; $model->storehouse_id = $storehouse_id; $model->storehouse_title = $storehouse_title; $model->type = $type; $model->status = $status; //是否审核 $model->save(); $id = $model->id; $detail_insert = []; foreach ($data as $v){ $detail_insert[] = [ 'apply_order_id' => $id, 'data_id' => $v['id'], 'quantity' => $v['quantity'] ?? 0, 'product_no' => $v['product_no'] ?? "", 'product_title' => $v['product_title'] ?? "", 'product_size' => $v['product_size'] ?? "", 'product_unit' => $v['product_unit'] ?? "", 'technology_name' => $v['technology_name'] ?? "", 'wood_name' => $v['wood_name'] ?? "", 'top_product_no' => $v['top_product_no'] ?? "", 'top_product_title' => $v['top_product_title'] ?? "", 'type' => $type, 'storehouse_id' => $storehouse_id, 'crt_time' => $time, ]; } ApplyOrderDetail::insert($detail_insert); DB::commit(); }catch (\Exception $e){ DB::rollBack(); return [false,$e->getMessage()]; } 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){return [true, '']; $record = ApplyOrderDetail::where('apply_order_id', $id) ->where('del_time',0) ->get()->toArray(); if(empty($record)) return [false, '申请单明细数据不存在或已被删除']; try{ DB::beginTransaction(); $time = time(); $insert = []; foreach ($record as $value){ if(empty($value['product_no'])) continue; if($value['type'] == ApplyOrder::type_one){ $number = - $value['quantity']; }else{ $number = $value['quantity']; } $insert[] = [ 'apply_order_id' => $id, 'product_no' => $value['product_no'], 'number' => $number, 'type' => $value['type'], 'storehouse_id' => $value['storehouse_id'], 'crt_time' => $time, ]; } if(! empty($insert)) InOutRecord::insert($insert); DB::commit(); }catch (\Exception $e){ DB::rollBack(); return [false,$e->getMessage()]; } return [true, '']; } //报工 public function reportWorkingEdit($data, $user){ list($status,$msg) = $this->reportWorkingRule($data,$user,false); if(!$status) return [$status,$msg]; DB::beginTransaction(); try{ $time = time(); $model = new ReportWorking(); $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; ReportWorkingDetail::where('del_time',0)->where('report_working_id',$id)->update([ 'del_time' => $time ]); //班组下的人 $team_man = []; $model_t = new EmployeeTeamPermission(); $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray(); foreach ($team_array as $v){ $team_man[$v['team_id']][] = $v['employee_id']; } $detail_insert = []; foreach ($data['order_data'] as $v){ ReportMessage::insert([ 'order_id' => $v['id'], 'quantity' => $v['quantity'] ?? 0, 'opt_case' => ReportMessage::type_one, 'crt_time' => $time, 'user_id' => $user['id'], ]); $t = $team_man[$v['team_id']] ?? []; 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' => 0, 'team_id' => $v['team_id'], 'crt_time' => $time, ]; } } ReportWorkingDetail::insert($detail_insert); DB::commit(); }catch (\Exception $e){ DB::rollBack(); return [false,$e->getMessage()]; } return [true,'']; } public function getReportRate($data, $user){ if(empty($data['dispatch_id'])) return [false, '派工单id不能为空']; $dispatch = DispatchSub::where('id', $data['dispatch_id']) ->select('process_id','dispatch_quantity') ->get()->toArray(); $array = $dispatch[0] ?? []; $process_id = $array['process_id'] ?? 0; $dispatch_quantity = $array['dispatch_quantity'] ?? 0; $process = Process::where('del_time',0) ->where('is_need_remain',1) ->select('id','zj_type','zj_rate') ->get()->toArray(); $process_id_array = array_column($process,'id'); $process_id_map = array_column($process,null,'id'); if(in_array($process_id, $process_id_array)){ $tmp = $process_id_map[$process_id] ?? []; if(! empty($tmp) && $tmp['zj_type'] == 2) { $num1 = ScrappCount::where('del_time',0) ->where('dispatch_sub_id',$data['dispatch_id']) ->count(); $total = bcdiv($dispatch_quantity, 0.75); $num = bcmul($total, bcdiv($tmp['zj_rate'],100,2)); dd($num,$num1,$total); } }else{ return [true , ['type' => 1]]; // 不需要质检 } return [true, ['type' => 0]]; // 错误 或者 全检 } public function getReportInfo($data){ if(empty($data)) return []; $process = Process::where('del_time',0) ->where('is_need_remain',1) ->select('id','zj_type','zj_rate') ->get()->toArray(); $process_id_array = array_column($process,'id'); $process_id_map = array_column($process,null,'id'); $num1_map = ScrappCount::where('del_time',0) ->whereIn('dispatch_sub_id',array_column($data,'id')) ->select(DB::raw('count(*) as number'), 'dispatch_sub_id') ->groupBy('dispatch_sub_id') ->get()->toArray(); $num1_map = array_column($num1_map,'number','dispatch_sub_id'); $return = []; foreach ($data as $value){ $tmp_1 = [ 'zj_type' => 0, 'zj_rate' => 0, 'zj_num' => 0, 'total_num' => 0, ]; if(in_array($value['process_id'], $process_id_array)){ $tmp = $process_id_map[$value['process_id']] ?? []; if(! empty($tmp)) { $num1 = $num1_map[$value['id']] ?? 0; $total = bcdiv($value['dispatch_quantity'], 0.75); $total = $total < 1 ? 1 : $total; $num = bcmul($total, bcdiv($tmp['zj_rate'],100,2)); $return[$value['id']] = [ 'zj_type' => $tmp['zj_type'], 'zj_rate' => $tmp['zj_rate'], 'zj_num' => $num1, 'total_num' => $num, ]; } }else{ $return[$value['id']] = $tmp_1; } } return $return; } public function reportWorkingAdd($data,$user){ list($status,$msg) = $this->reportWorkingRule($data,$user); if(!$status) return [$status,$msg]; DB::beginTransaction(); try{ $time = time(); $model = new ReportWorking(); $model->order_number = $data['order_number']; $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; //班组下的人 $team_man = []; $model_t = new EmployeeTeamPermission(); $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray(); foreach ($team_array as $v){ $team_man[$v['team_id']][] = $v['employee_id']; } $process = Process::where('del_time',0) ->where('is_need_remain',1) ->select('id') ->get()->toArray(); $process_id = array_column($process,'id'); $detail_insert = []; foreach ($data['order_data'] as $v){ if(in_array($v['process_id'], $process_id)){ ReportMessage::insert([ 'order_id' => $v['id'], 'quantity' => $v['quantity'] ?? 0, 'opt_case' => ReportMessage::type_one, 'crt_time' => $time, 'user_id' => $user['id'], ]); } $t = $team_man[$v['team_id']] ?? []; 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' => 0, 'team_id' => $v['team_id'], 'crt_time' => $time, ]; } } ReportWorkingDetail::insert($detail_insert); DB::commit(); }catch (\Exception $e){ DB::rollBack(); return [false,$e->getMessage()]; } return [true, '']; } public function reportWorkingDel($data){ if($this->isEmpty($data,'id')) return [false,'ID必须!']; $apply = ReportWorking::where('id',$data['id'])->first(); if($apply->del_time > 0) return [false,'报工单不存在或已被删除']; if($apply->state != ReportWorking::state_zero) return [false, '报工单已审核,删除失败']; $time = time(); try { DB::beginTransaction(); $apply->del_time = $time; $apply->save(); ReportWorkingDetail::where('del_time', 0)->where('report_working_id',$data['id'])->update([ 'del_time'=>time() ]); DB::commit(); }catch (\Throwable $exception){ DB::rollBack(); return [false, $exception->getMessage()]; } return [true,'']; } public function reportWorkingList($data){ $model = ReportWorking::where('del_time',0) ->select('*') ->orderBy('id','desc'); if(isset($data['status'])) $model->where('status', $data['status']); if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%'); if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']); if(! empty($data['report_time'][0]) && ! empty($data['report_time'][1])) $model->whereBetween('report_time',[$data['report_time'][0],$data['report_time'][1]]); if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]); if(! empty($data['dispatch_no1'])) { $dispatch_id = DispatchSub::where('del_time',0) ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no1'].'%') ->select('id') ->get()->toArray(); $dispatch_id = array_column($dispatch_id,'id'); $detail = ReportWorkingDetail::where('del_time',0) ->whereIn('data_id', $dispatch_id) ->select('report_working_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'report_working_id'))); } if(! empty($data['dispatch_no'])) { $dispatch_id = DispatchSub::where('del_time',0) ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%') ->select('id') ->get()->toArray(); $dispatch_id = array_column($dispatch_id,'id'); $detail = ReportWorkingDetail::where('del_time',0) ->whereIn('data_id', $dispatch_id) ->select('report_working_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'report_working_id'))); } if(! empty($data['sale_order_number'])){ $id = SaleOrdersProduct::where('del_time',0) ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%') ->select('id') ->get()->toArray(); $dispatch_id = DispatchSub::where('del_time',0) ->whereIn('sale_orders_product_id',array_column($id,'id')) ->select('id') ->get()->toArray(); $detail = ReportWorkingDetail::where('del_time',0) ->whereIn('data_id', $dispatch_id) ->select('report_working_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'report_working_id'))); } $list = $this->limit($model,'',$data); $list = $this->reportWorkingfillData($list); return [true,$list]; } public function reportWorkingfillData($data){ if(empty($data['data'])) return $data; $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id'))) ->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',$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; } public function reportWorkingDetail($data){ if(empty($data['id']) && empty($data['order_number'])) return [false,'ID与OrderNumber不能为空!']; $id = $data['id'] ?? 0; $order_number = $data['order_number'] ?? ""; $detail = ReportWorking::where('del_time',0) ->when(! empty($id), function ($query) use ($id){ return $query->where('id', $id); }) ->when(! empty($order_number), function ($query) use ($order_number){ return $query->where('order_number', $order_number); }) ->first(); if(empty($detail)) return [false,'报工单不存在或已被删除']; $detail = $detail->toArray(); if(empty($id)) $id = $detail['id']; $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(); $apply_id = array_column($apply_d, 'data_id'); //派工单 $d = DispatchSub::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','process_id','product_unit','price','product_size','device_id') ->get()->toArray(); $d_tmp = array_column($d,null,'id'); $process_map = Process::whereIn('id',array_unique(array_column($d,'process_id'))) ->pluck('title','id') ->toArray(); $team_map = Team::pluck('title','id')->toArray(); $device_map = Equipment::whereIn('id',array_unique(array_column($d,'device_id'))) ->pluck('title','id') ->toArray(); $return = []; foreach ($apply_d as $t){ $tmp = $d_tmp[$t['data_id']] ?? []; $str = $t['data_id'] . $t['process_id']; if(! isset($return[$str])){ $return[$str] = [ 'id' => $t['data_id'] ?? 0, 'quantity' => $t['quantity'] ?? 0, 'product_no' => $tmp['product_no'] ?? "", 'product_title' => $tmp['product_title'] ?? "", 'product_size' => $tmp['product_size'] ?? "", 'product_unit' => $tmp['product_unit'] ?? "", 'technology_name' => $tmp['technology_name'] ?? "", 'wood_name' => $tmp['wood_name'] ?? "", 'order_no' => $tmp['order_no'] ?? "", 'process_id' => $tmp['process_id'] ?? 0, 'process_name' => $process_map[$tmp['process_id']] ?? "", 'team_id' => $t['team_id'] ?? 0, 'team_title' => $team_map[$t['team_id']] ?? '', 'device_id' => $tmp['device_id'] ?? 0, 'device_title' => $device_map[$tmp['device_id']] ?? '', ]; } } $detail['order_data'] = array_values($return); return [true, $detail]; } 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){ if(empty($value['id'])) return [false, '报工单详细信息ID不能为空']; if(empty($value['quantity'])) return [false, '报工单详细信息数量不能为空']; if(empty($value['process_id'])) return [false, '报工单详细信息工序不能为空']; if(empty($value['team_id'])) return [false, '报工单详细信息班组不能为空']; } $id = array_unique(array_column($data['order_data'],'id')); $dispatch = DispatchSub::where('del_time',0) ->whereIn('id', $id) ->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(); if(empty($data['order_number'])) return [false, '报工单唯一标识生成失败']; }else{ if(empty($data['id'])) return [false, '报工单唯一标识不能为空']; if(empty($data['order_number'])) return [false, '报工单唯一标识不能为空']; $apply = ReportWorking::where('id', $data['id'])->where('del_time', 0)->first(); if(empty($apply)) return [false, '报工单不存在或已被删除']; if($apply->status != 0) return [false, '报工单已审核,编辑失败']; } 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 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, 3); 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 . '%') ->max('order_number'); if(empty($order_number)){ $number = str_pad(1,3,'0',STR_PAD_LEFT); $number = $str . $number; }else{ $tmp = substr($order_number, -3); $tmp = $tmp + 1; //超过999 // if(strlen($tmp) > 3) return ''; $number = str_pad($tmp,3,'0',STR_PAD_LEFT); $number = $str . $number; } return $number; } public function applyOrderReportList($data){ if(empty($data['apply_time'][0]) || empty($data['apply_time'][1])) return [false, '申请日期必须选择!']; if(empty($data['type'])) return [false, 'TYPE不能为空!']; $model = ApplyOrder::where('del_time',0) ->where('apply_time',">=",$data['apply_time'][0]) ->where('apply_time',"<=",$data['apply_time'][1]) ->where('type',$data['type']) ->select('id','order_number','apply_time','mark','status','storehouse_title') ->orderBy('id','desc'); if(isset($data['status'])) $model->where('status', $data['status']); if(! empty($data['pc_number'])) { if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){ $dispatch_id = DispatchSub::where('del_time',0) ->where('out_order_no', 'LIKE', '%'.$data['pc_number'].'%') ->select('id') ->get()->toArray(); $dispatch_id = array_column($dispatch_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $dispatch_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); }elseif($data['type'] == ApplyOrder::type_five){ $zj_id = ScrappCount::where('del_time',0) ->where('out_order_no', 'LIKE', '%'.$data['pc_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['pc_number'].'%') ->select('order_no') ->get()->toArray(); $no = array_unique(array_column($no,'order_no')); $box_id = Box::where('del_time',0) ->whereIn('top_order_no', $no) ->select('id') ->get()->toArray(); $box_id = array_column($box_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $box_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); } } if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])){ $model->where('crt_time',">=",$data['crt_time'][0]); $model->where('crt_time',"<=",$data['crt_time'][1]); } if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%'); if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%'); if(! empty($data['sale_order_number'])){ if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){ $dispatch_id = DispatchSub::where('del_time',0) ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%') ->select('id') ->get()->toArray(); $dispatch_id = array_column($dispatch_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $dispatch_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); }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'].'%') ->select('order_no') ->get()->toArray(); $no = array_unique(array_column($no,'order_no')); $box_id = Box::where('del_time',0) ->whereIn('top_order_no', $no) ->select('id') ->get()->toArray(); $box_id = array_column($box_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $box_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); } } if(! empty($data['dispatch_no'])){ $dispatch_id = DispatchSub::where('del_time',0) ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%') ->select('id') ->get()->toArray(); $dispatch_id = array_column($dispatch_id,'id'); $detail = ApplyOrderDetail::where('del_time',0) ->where('type', $data['type']) ->whereIn('data_id', $dispatch_id) ->select('apply_order_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($detail,'apply_order_id'))); } $list = $model->get()->toArray(); if(empty($list)) return [true, []]; $apply_id = array_column($list, 'id'); //明细数据 $result = ApplyOrderDetail::where('del_time',0) ->whereIn('apply_order_id', $apply_id) ->get()->toArray(); if(empty($result)) return [true, []]; //组织主表需要的数据 $list_map = $this->makeListMap($list, $result, $data['type']); $data_id = array_unique(array_column($result, 'data_id')); list($d, $d_no_map) = $this->getDetailList($data_id, $data['type']); $return = []; $d_tmp = array_column($d,null,'id'); foreach ($result as $t){ $tmp = $d_tmp[$t['data_id']] ?? []; $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? ""; $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? ""; $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? ""; $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? ""; if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){ $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]); }else{ $order_no = $tmp['order_no'] ?? ""; } if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){ $product_unit = "吨"; }elseif($t['type'] == ApplyOrder::type_four){ $product_unit = "只"; }else{ $product_unit = $t['product_unit'] ?? ""; } $tmp = $list_map[$t['apply_order_id']] ?? []; $return[] = [ 'order_number' => $tmp['order_number'], 'sale_order_number' => $tmp['sale_order_number'], 'apply_time' => $tmp['apply_time'], 'mark' => $tmp['mark'], 'status_title' => $tmp['status_title'], 'storehouse_title' => $tmp['storehouse_title'], 'id' => $t['data_id'] ?? 0, // 'main_id' => $t['id'], 'quantity' => $t['quantity'] ?? 0, 'product_no' => $t['product_no'] ?? "", 'product_title' => $t['product_title'] ?? "", 'product_size' => $t['product_size'] ?? "", 'product_unit' => $product_unit, 'top_product_title' => $top_product_title, 'top_product_no' => $top_product_no, 'technology_name' => $technology_name, //颜色 'wood_name' => $wood_name, 'order_no' => $order_no, ]; } return [true, array_values($return)]; } private function getDetailList($apply_id, $type){ $d = $d_no_map = []; if($type == ApplyOrder::type_one){ $d = DispatchSub::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time') ->get()->toArray(); $args = ""; foreach ($d as $value){ $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR "; } $args = rtrim($args, 'OR '); $d_no_map = []; $d_no = DispatchSub::where('del_time',0) ->whereRaw($args) ->select('dispatch_no as order_no','order_product_id','crt_time') ->get()->toArray(); foreach ($d_no as $value){ if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){ if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no']; }else{ $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no']; } } }elseif ($type == ApplyOrder::type_two){ $d = DispatchSub::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time') ->get()->toArray(); $args = ""; foreach ($d as $value){ $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR "; } $args = rtrim($args, 'OR '); $d_no_map = []; $d_no = DispatchSub::where('del_time',0) ->whereRaw($args) ->select('dispatch_no as order_no','order_product_id','crt_time') ->get()->toArray(); foreach ($d_no as $value){ if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){ if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no']; }else{ $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no']; } } }elseif ($type == ApplyOrder::type_three){ $d = Box::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'order_no') ->get()->toArray(); }elseif ($type == ApplyOrder::type_four){ $d = Box::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'order_no') ->get()->toArray(); }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(); }elseif ($type == ApplyOrder::type_six){ $d = Box::where('del_time',0) ->whereIn('id', $apply_id) ->select('id', 'order_no') ->get()->toArray(); } return [$d, $d_no_map]; } private function makeListMap($data, $order, $type){ if(empty($data)) return []; //申请单 所有的数据id $order_map = $data_id = []; foreach ($order as $value){ $order_map[$value['apply_order_id']][] = $value['data_id']; $data_id[] = $value['data_id']; } if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){ $map1 = DispatchSub::whereIn('id', array_unique($data_id)) ->pluck('out_order_no','id') ->toArray(); }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') ->toArray(); $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2))) ->pluck('out_order_no','order_no') ->toArray(); } foreach ($data as $key => $value){ $data[$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : ''; $data[$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? ""; $order_tmp = $order_map[$value['id']] ?? []; $order_tmp = array_unique($order_tmp); $sale_order_number = []; if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){ foreach ($order_tmp as $v){ $str = $map1[$v] ?? ''; if(empty($str)) continue; $sale_order_number[] = $str; } $sale_order_number = array_unique($sale_order_number); $sale_order_number = implode(',',$sale_order_number); $data[$key]['sale_order_number'] = $sale_order_number; }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] ?? ''; $m2_t = $map2_fin[$m2_t] ?? ""; if(empty($m2_t)) continue; $sale_order_number[] = $m2_t; } $sale_order_number = array_unique($sale_order_number); $sale_order_number = implode(',',$sale_order_number); $data[$key]['sale_order_number'] = $sale_order_number; } } return array_column($data,null,'id'); } 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','result','product_no') ->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']; } $sList = ScrappCount::where('del_time',0) ->where('report_id', $data['id']) ->select('dispatch_sub_id as dispatch_id','scrapp_num','quantity') ->get()->toArray(); $sMap = []; foreach ($sList as $value){ $num = bcadd($value['scrapp_num'], $value['quantity'],3); if(isset($sMap[$value['dispatch_id']])){ $t = bcadd($sMap[$value['dispatch_id']], $num,3); $sMap[$value['dispatch_id']] = $t; }else{ $sMap[$value['dispatch_id']] = $num; } } 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, '总数量不能超过报工数量']; $tmp = $sMap[$value['data_id']] ?? 0; if($tmp >= $total) 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']] ?? []; $num = $num['num'] ?? 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($all_data, $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']; }elseif (! empty($value['num'])){ $quantity = $value['num']; } $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, 'wg_status' => 1, ]); } 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 转换为销售订单 $result = $this->changeIdToOrderNew($data); // $sales_id = $this->changeIdToOrder($data); // list($status, $msg) = $this->forSearch($data, $sales_id); return [true, $result]; } private function changeIdToOrder($data) { $id = $data['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('report_working_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'])->select('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', $value['order_no'])->select('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', $value['order_no'])->select('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('report_working_id') ->get()->toArray(); $result = ReportWorking::where('del_time',0) ->whereIn('id', array_unique(array_column($detail,'report_working_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'])->select('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'])->select('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'])->select('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]; } private function changeIdToOrderNew1($data) { $id = $data['data_id']; $search_id = $result = []; if ($data['type'] == 1) { } elseif ($data['type'] == 2) { $result[1] = OrdersProduct::where('del_time', 0) ->where('id', $id) ->pluck('sale_orders_product_id') ->toArray(); } elseif ($data['type'] == 3) { $array = DispatchSub::where('del_time', 0) ->where('id', $data['data_id']) ->select('sale_orders_product_id', 'order_product_id') ->get()->toArray(); foreach ($array as $value){ $result[1][] = $value['sale_orders_product_id']; $result[2][] = $value['order_product_id']; } } elseif ($data['type'] == 4) { $detail = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time', 0) ->select('data_id') ->get()->toArray(); $array = DispatchSub::where('del_time', 0) ->whereIn('id', array_unique(array_column($detail, 'data_id'))) ->select('sale_orders_product_id', 'order_product_id','id') ->get()->toArray(); foreach ($array as $value){ $result[1][] = $value['sale_orders_product_id']; $result[2][] = $value['order_product_id']; $result[3][] = $value['id']; } } elseif ($data['type'] == 5) { $array = DispatchSub::where('del_time', 0) ->where('id', $id) ->where('finished_num', '>', 0) ->select('sale_orders_product_id', 'order_product_id','id') ->get()->toArray(); foreach ($array as $value){ $result[1][] = $value['sale_orders_product_id']; $result[2][] = $value['order_product_id']; $result[3][] = $value['id']; } $detail = ReportWorkingDetail::where('data_id', $id) ->where('del_time', 0) ->select('report_working_id') ->get()->toArray(); foreach ($detail as $value){ $result[4][] = $value['report_working_id']; } } elseif ($data['type'] == 6) { $first = Box::where('del_time', 0) ->where('id', $id) ->select("top_order_no", 'order_no','id') ->first(); if (! empty($first)) { $first = $first->toArray(); $boxArray = BoxWithDispatch::where('del_time',0) ->where('box_id',$first['id']) ->select('dispatch_id','num') ->get()->toArray(); if(! empty($boxArray)){ $dispatch_id = array_column($boxArray,'dispatch_id'); $array = DispatchSub::where('del_time', 0) ->whereIn('id',$dispatch_id) ->select('sale_orders_product_id', 'order_product_id','id') ->get()->toArray(); foreach ($array as $value){ $result[1][] = $value['sale_orders_product_id']; $result[2][] = $value['order_product_id']; $result[3][] = $value['id']; } $detail = ReportWorkingDetail::whereIn('data_id', $dispatch_id) ->where('del_time', 0) ->select('report_working_id') ->get()->toArray(); foreach ($detail as $value){ $result[4][] = $value['report_working_id']; } $result[5] = $dispatch_id; }else{ $array = []; $box_detail = new BoxDetail(['channel' => $first['top_order_no']]); $detail = $box_detail->where('order_no', $first['order_no'])->select('top_id as id')->get()->toArray(); foreach ($detail as $value){ if(! in_array($value['id'], $array)) $array[] = $value['id']; } $result[1] = $array; $result[2] = OrdersProduct::where('del_time',0) ->whereIn('sale_orders_product_id',$array) ->pluck('id')->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', $value['order_no'])->select('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', $value['order_no'])->select('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 changeIdToOrderNew($data) { $id = $data['data_id']; $result = []; $type = $data['type']; $for_type = $data['for_type']; if ($data['type'] == 1) { $result = $this->typeOne($for_type, $id); } elseif ($type == 2) { $result = $this->typeTwo($for_type, $id); } elseif ($type == 3) { $result = $this->typeThree($for_type, $id); } elseif ($type == 4) { $result = $this->typeFour($for_type, $id); } elseif ($type == 5) { $result = $this->typeFive($for_type, $id); } elseif ($type == 6) { $result = $this->typeSix($for_type, $id); } elseif ($type == 7) { $result = $this->typeSeven($for_type, $id); } elseif ($type == 8) { $result = $this->typeEight($for_type, $id); } elseif ($type == 9) { $result = $this->typeNine($for_type, $id); } elseif ($type == 10) { $result = $this->typeTen($for_type, $id); } elseif ($type == 11) { $result = $this->typeEve($for_type, $id); } elseif ($type == 12) { $result = $this->typeTwl($for_type, $id); } elseif ($type == 13) { $result = $this->typeThi($for_type, $id); } elseif ($type == 14) { $result = $this->typeFourTeen($for_type, $id); } return $result; } private function typeOne($for_type, $id){ $result = []; if($for_type == 2){ $result = OrdersProduct::where('del_time',0) ->where('sale_orders_product_id', $id) ->select('id', 'production_no as order_no') ->get()->toArray(); }elseif ($for_type == 3){ $result = DispatchSub::where('del_time',0) ->where('sale_orders_product_id', $id) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 4){ $dispatch = DispatchSub::where('del_time',0) ->where('sale_orders_product_id', $id) ->select('id') ->get()->toArray(); $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id')) ->where('del_time',0) ->select('report_working_id') ->get()->toArray(); $result = ReportWorking::where('del_time',0) ->whereIn('id', array_unique(array_column($detail,'report_working_id'))) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 5){ $result = DispatchSub::where('del_time',0) ->where('sale_orders_product_id', $id) ->where('finished_num','>',0) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 6){ $sale = SaleOrdersProduct::where('id', $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',$id)->select('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 ($for_type == 7){ $process = Process::where('del_time',0) ->where('is_need_remain',1) ->pluck('id') ->toArray(); $result = ScrappCount::where('del_time',0) ->where('sale_orders_product_id', $id) ->whereIn('process_id', $process) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 8){ $result = ScrappCount::where('del_time',0) ->where('sale_orders_product_id', $id) ->where('scrapp_num','>', 0) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 9){ $dispatch_id = DispatchSub::where('del_time',0) ->where('sale_orders_product_id', $id) ->pluck('id') ->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_one) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 10){ $dispatch_id = DispatchSub::where('del_time',0) ->where('sale_orders_product_id', $id) ->pluck('id') ->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_two) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 11){ $sale = SaleOrdersProduct::where('id', $id)->select('order_no','id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_four) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 12){ $sale = SaleOrdersProduct::where('id', $id)->select('order_no','id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_three) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 13){ $s_id = ScrappCount::where('del_time', 0) ->where('sale_orders_product_id', $id) ->pluck('id') ->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $s_id) ->where('type', ApplyOrder::type_five) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 14){ $sale = SaleOrdersProduct::where('id', $id)->select('order_no','id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_six) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); } return $result; } private function typeTwo($for_type, $id){ $result = []; if($for_type == 1){ $sale_orders_product_id = OrdersProduct::where('del_time',0) ->where('id', $id) ->pluck('sale_orders_product_id') ->toArray(); $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id) ->where('del_time',0) ->select('id','out_order_no as order_no') ->get()->toArray(); }elseif ($for_type == 3){ $result = DispatchSub::where('del_time',0) ->where('order_product_id', $id) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 4){ $dispatch = DispatchSub::where('del_time',0) ->where('order_product_id', $id) ->select('id') ->get()->toArray(); $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id')) ->where('del_time',0) ->select('report_working_id') ->get()->toArray(); $result = ReportWorking::where('del_time',0) ->whereIn('id', array_unique(array_column($detail,'report_working_id'))) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 5){ $result = DispatchSub::where('del_time',0) ->where('order_product_id', $id) ->where('finished_num','>',0) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 6){ $sale_orders_product_id = OrdersProduct::where('del_time',0) ->where('id', $id) ->pluck('sale_orders_product_id') ->toArray(); $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray(); foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('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 ($for_type == 7){ $process = Process::where('del_time',0) ->where('is_need_remain',1) ->pluck('id') ->toArray(); $result = ScrappCount::where('del_time',0) ->where('order_product_id', $id) ->whereIn('process_id', $process) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 8){ $result = ScrappCount::where('del_time',0) ->where('order_product_id', $id) ->where('scrapp_num','>', 0) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 9){ $dispatch_id = DispatchSub::where('del_time',0) ->where('order_product_id', $id) ->pluck('id') ->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_one) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 10){ $dispatch_id = DispatchSub::where('del_time',0) ->where('order_product_id', $id) ->pluck('id') ->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_two) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 11){ $sale = OrdersProduct::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_four) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 12){ $sale = OrdersProduct::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_three) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 13){ $s_id = ScrappCount::where('del_time', 0) ->where('order_product_id', $id) ->pluck('id') ->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $s_id) ->where('type', ApplyOrder::type_five) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 14){ $sale = OrdersProduct::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_six) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); } return $result; } private function typeThree($for_type, $id){ $result = []; if($for_type == 1){ $sale_orders_product_id = DispatchSub::where('del_time',0) ->where('id', $id) ->pluck('sale_orders_product_id') ->toArray(); $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id) ->where('del_time',0) ->select('id','out_order_no as order_no') ->get()->toArray(); }elseif ($for_type == 2){ $order_product_id = DispatchSub::where('del_time',0) ->where('id', $id) ->pluck('order_product_id') ->toArray(); $result = OrdersProduct::where('del_time',0) ->whereIn('id', $order_product_id) ->select('id', 'production_no as order_no') ->get()->toArray(); }elseif ($for_type == 4){ $dispatch = DispatchSub::where('del_time',0) ->where('id', $id) ->select('id') ->get()->toArray(); $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id')) ->where('del_time',0) ->select('report_working_id') ->get()->toArray(); $result = ReportWorking::where('del_time',0) ->whereIn('id', array_unique(array_column($detail,'report_working_id'))) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 5){ $result = DispatchSub::where('del_time',0) ->where('id', $id) ->where('finished_num','>',0) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 6){ $sale_orders_product_id = DispatchSub::where('del_time',0) ->where('id', $id) ->pluck('sale_orders_product_id') ->toArray(); $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray(); foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('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 ($for_type == 7){ $process = Process::where('del_time',0) ->where('is_need_remain',1) ->pluck('id') ->toArray(); $result = ScrappCount::where('del_time',0) ->where('dispatch_sub_id', $id) ->whereIn('process_id', $process) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 8){ $result = ScrappCount::where('del_time',0) ->where('dispatch_sub_id', $id) ->where('scrapp_num','>', 0) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 9){ $dispatch = DispatchSub::where('id',$id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_one) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 10){ $dispatch = DispatchSub::where('id',$id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_two) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 11){ $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_four) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 12){ $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_three) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 13){ $dispatch = DispatchSub::where('id',$id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $s_id = ScrappCount::where('del_time', 0) ->whereIn('dispatch_sub_id', $dispatch_id) ->pluck('id') ->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $s_id) ->where('type', ApplyOrder::type_five) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 14){ $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_four) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); } return $result; } private function typeFour($for_type, $id){ $result = []; if($for_type == 1){ $dispatch_id = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time',0) ->pluck('data_id') ->toArray(); $sale_orders_product_id = DispatchSub::where('del_time',0) ->whereIn('id', $dispatch_id) ->pluck('sale_orders_product_id') ->toArray(); $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id) ->where('del_time',0) ->select('id','out_order_no as order_no') ->get()->toArray(); }elseif ($for_type == 2){ $dispatch_id = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time',0) ->pluck('data_id') ->toArray(); $order_product_id = DispatchSub::where('del_time',0) ->whereIn('id', $dispatch_id) ->pluck('order_product_id') ->toArray(); $result = OrdersProduct::where('del_time',0) ->whereIn('id', $order_product_id) ->select('id', 'production_no as order_no') ->get()->toArray(); }elseif ($for_type == 3){ $dispatch_id = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time',0) ->pluck('data_id') ->toArray(); $result = DispatchSub::where('del_time',0) ->whereIn('id', $dispatch_id) ->select('id','dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 5){ $dispatch_id = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time',0) ->pluck('data_id') ->toArray(); $result = DispatchSub::where('del_time',0) ->whereIn('id', $dispatch_id) ->where('finished_num','>',0) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 6){ $dispatch_id = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time',0) ->pluck('data_id') ->toArray(); $sale_orders_product_id = DispatchSub::where('del_time',0) ->whereIn('id', $dispatch_id) ->pluck('sale_orders_product_id') ->toArray(); $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray(); $return = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('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(); $return = array_merge($return, $tmp); } $result[] = $return[0] ?? []; }elseif ($for_type == 7){ $process = Process::where('del_time',0) ->where('is_need_remain',1) ->pluck('id') ->toArray(); $result = ScrappCount::where('del_time',0) ->where('report_id', $id) ->whereIn('process_id',$process) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 8){ $result = ScrappCount::where('del_time',0) ->where('report_id', $id) ->where('scrapp_num','>', 0) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 9){ $dispatch_id = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time',0) ->pluck('data_id') ->toArray(); $dispatch_id = $dispatch_id[0] ?? 0; $dispatch = DispatchSub::where('id',$dispatch_id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_one) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 10){ $dispatch_id = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time',0) ->pluck('data_id') ->toArray(); $dispatch_id = $dispatch_id[0] ?? 0; $dispatch = DispatchSub::where('id',$dispatch_id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_two) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 11){ $dispatch_id = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time',0) ->pluck('data_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_four) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 12){ $dispatch_id = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time',0) ->pluck('data_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no', 'sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_three) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 13){ $s_id = ScrappCount::where('del_time', 0) ->where('report_id', $id) ->pluck('id') ->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $s_id) ->where('type', ApplyOrder::type_five) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 14){ $dispatch_id = ReportWorkingDetail::where('report_working_id', $id) ->where('del_time',0) ->pluck('data_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_six) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; } return $result; } private function typeFive($for_type, $id){ $result = []; if($for_type == 1){ $sale_orders_product_id = DispatchSub::where('del_time',0) ->where('id', $id) ->pluck('sale_orders_product_id') ->toArray(); $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id) ->where('del_time',0) ->select('id','out_order_no as order_no') ->get()->toArray(); }elseif ($for_type == 2){ $order_product_id = DispatchSub::where('del_time',0) ->where('id', $id) ->pluck('order_product_id') ->toArray(); $result = OrdersProduct::where('del_time',0) ->whereIn('id', $order_product_id) ->select('id', 'production_no as order_no') ->get()->toArray(); }elseif ($for_type == 3){ $result = DispatchSub::where('del_time',0) ->where('id', $id) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 4){ $dispatch = DispatchSub::where('del_time',0) ->where('id', $id) ->select('id') ->get()->toArray(); $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id')) ->where('del_time',0) ->select('report_working_id') ->get()->toArray(); $result = ReportWorking::where('del_time',0) ->whereIn('id', array_unique(array_column($detail,'report_working_id'))) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 6){ $box_id = BoxWithDispatch::where('del_time',0) ->where('dispatch_id', $id) ->pluck('box_id') ->toArray(); $box_id = $box_id[0] ?? 0; $result = Box::where('del_time',0) ->where('id', $box_id) ->select("id","order_no") ->get()->toArray(); }elseif ($for_type == 7){ $process = Process::where('del_time',0) ->where('is_need_remain',1) ->pluck('id') ->toArray(); $result = ScrappCount::where('del_time',0) ->where('dispatch_sub_id', $id) ->whereIn('process_id', $process) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 8){ $result = ScrappCount::where('del_time',0) ->where('dispatch_sub_id', $id) ->where('scrapp_num','>', 0) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 9){ $dispatch = DispatchSub::where('id',$id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_one) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 10){ $dispatch = DispatchSub::where('id',$id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_two) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 11){ $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_four) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 12){ $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_three) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 13){ $s_id = ScrappCount::where('del_time', 0) ->where('dispatch_sub_id', $id) ->pluck('id') ->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $s_id) ->where('type', ApplyOrder::type_five) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 14){ $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_six) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; } return $result; } private function typeSix($for_type, $id){ $result = []; $box = Box::where('id', $id)->first(); if(empty($box)) return $result; $top_order_no = $box->top_order_no; $order_no = $box->order_no; if($for_type == 1){ $box_detail = new BoxDetail(['channel'=> $top_order_no]); $sale_orders_product_id = $box_detail->where('del_time',0) ->where('order_no',$order_no) ->pluck('top_id') ->toArray(); $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id) ->where('del_time',0) ->select('id','out_order_no as order_no') ->get()->toArray(); }elseif ($for_type == 2){ $box_detail = new BoxDetail(['channel'=> $top_order_no]); $sale_orders_product_id = $box_detail->where('del_time',0) ->where('order_no',$order_no) ->pluck('top_id') ->toArray(); $result = OrdersProduct::where('del_time',0) ->whereIn('sale_orders_product_id', $sale_orders_product_id) ->select('id', 'production_no as order_no') ->get()->toArray(); }elseif ($for_type == 3){ $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->pluck('dispatch_id') ->toArray(); $result = DispatchSub::where('del_time',0) ->whereIn('id', $dispatch_id) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 4){ $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->pluck('dispatch_id') ->toArray(); $detail = ReportWorkingDetail::whereIn('data_id', $dispatch_id) ->where('del_time',0) ->select('report_working_id') ->get()->toArray(); $return = ReportWorking::where('del_time',0) ->whereIn('id', array_unique(array_column($detail,'report_working_id'))) ->select('id', 'order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 5){ $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->pluck('dispatch_id') ->toArray(); $result = DispatchSub::where('del_time',0) ->whereIn('id', $dispatch_id) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 7){ $process = Process::where('del_time',0) ->where('is_need_remain',1) ->pluck('id') ->toArray(); $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->pluck('dispatch_id') ->toArray(); $report_working_id = ReportWorkingDetail::whereIn('data_id', $dispatch_id) ->where('del_time',0) ->pluck('report_working_id') ->toArray(); $return = ScrappCount::where('del_time',0) ->whereIn('report_id', $report_working_id) ->whereIn('process_id', $process) ->select('id', 'order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 8){ $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->pluck('dispatch_id') ->toArray(); $report_working_id = ReportWorkingDetail::whereIn('data_id', $dispatch_id) ->where('del_time',0) ->pluck('report_working_id') ->toArray(); $return = ScrappCount::where('del_time',0) ->whereIn('report_id', $report_working_id) ->where('scrapp_num','>', 0) ->select('id', 'order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 9){ $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->select('dispatch_id') ->first()->toArray(); $dispatch = DispatchSub::where('id',$dispatch_id['dispatch_id']) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_one) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 10){ $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->select('dispatch_id') ->first()->toArray(); $dispatch = DispatchSub::where('id',$dispatch_id['dispatch_id']) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_two) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 11){ $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->pluck('dispatch_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_four) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 12){ $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->pluck('dispatch_id') ->toArray(); $sale = DispatchSub::where('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_three) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 13){ $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->select('dispatch_id') ->first()->toArray(); $dispatch = DispatchSub::where('id',$dispatch_id['dispatch_id']) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $s_id = ScrappCount::where('del_time', 0) ->whereIn('dispatch_sub_id', $dispatch_id) ->pluck('id') ->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $s_id) ->where('type', ApplyOrder::type_five) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 14){ $dispatch_id = BoxWithDispatch::where('del_time',0) ->where('box_id', $id) ->pluck('dispatch_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_six) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; } return $result; } private function typeSeven($for_type, $id){ $result = []; if($for_type == 1){ $sale_orders_product_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('sale_orders_product_id') ->toArray(); $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id) ->where('del_time',0) ->select('id','out_order_no as order_no') ->get()->toArray(); }elseif ($for_type == 2){ $order_product_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('order_product_id') ->toArray(); $result = OrdersProduct::where('del_time',0) ->whereIn('id', $order_product_id) ->select('id', 'production_no as order_no') ->get()->toArray(); }elseif ($for_type == 3){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $result = DispatchSub::where('del_time',0) ->where('id', $dispatch_id) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 4){ $report_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('report_id') ->toArray(); $result = ReportWorking::where('del_time',0) ->whereIn('id', $report_id) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 5){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $result = DispatchSub::where('del_time',0) ->where('id', $dispatch_id) ->where('finished_num','>',0) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 6){ $sale_orders_product_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('sale_orders_product_id') ->toArray(); $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray(); $return = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('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(); $return = array_merge($return, $tmp); } $result[] = $return[0] ?? []; }elseif ($for_type == 8){ $result = ScrappCount::where('del_time',0) ->where('id', $id) ->where('scrapp_num','>', 0) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 9){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $dispatch_id = $dispatch_id[0] ?? 0; $dispatch = DispatchSub::where('id',$dispatch_id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_one) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 10){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $dispatch_id = $dispatch_id[0] ?? 0; $dispatch = DispatchSub::where('id',$dispatch_id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_two) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 11){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_four) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 12){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_three) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 13){ $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->where('data_id', $id) ->where('type', ApplyOrder::type_five) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 14){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_six) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; } return $result; } private function typeEight($for_type, $id){ $result = []; if($for_type == 1){ $sale_orders_product_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('sale_orders_product_id') ->toArray(); $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id) ->where('del_time',0) ->select('id','out_order_no as order_no') ->get()->toArray(); }elseif ($for_type == 2){ $order_product_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('order_product_id') ->toArray(); $result = OrdersProduct::where('del_time',0) ->whereIn('id', $order_product_id) ->select('id', 'production_no as order_no') ->get()->toArray(); }elseif ($for_type == 3){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $result = DispatchSub::where('del_time',0) ->where('id', $dispatch_id) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 4){ $report_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('report_id') ->toArray(); $result = ReportWorking::where('del_time',0) ->whereIn('id', $report_id) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 5){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $result = DispatchSub::where('del_time',0) ->where('id', $dispatch_id) ->where('finished_num','>',0) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); }elseif ($for_type == 6){ $sale_orders_product_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('sale_orders_product_id') ->toArray(); $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray(); $return = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('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(); $return = array_merge($return, $tmp); } $result[] = $return[0] ?? []; }elseif ($for_type == 7){ $process = Process::where('del_time',0) ->where('is_need_remain',1) ->pluck('id') ->toArray(); $result = ScrappCount::where('del_time',0) ->where('id', $id) ->whereIn('process_id', $process) ->select('id', 'order_number as order_no') ->get()->toArray(); }elseif ($for_type == 9){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $dispatch_id = $dispatch_id[0] ?? 0; $dispatch = DispatchSub::where('id',$dispatch_id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_one) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 10){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $dispatch_id = $dispatch_id[0] ?? 0; $dispatch = DispatchSub::where('id',$dispatch_id) ->select('order_product_id','crt_time','product_no','technology_name') ->first()->toArray(); $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id']) ->where('crt_time',$dispatch['crt_time']) ->where('product_no',$dispatch['product_no']) ->where('technology_name',$dispatch['technology_name']) ->pluck('id')->toArray(); $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $dispatch_id) ->where('type', ApplyOrder::type_two) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 11){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_four) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 12){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_three) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; }elseif ($for_type == 13){ $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->where('data_id', $id) ->where('type', ApplyOrder::type_five) ->pluck('apply_order_id') ->toArray(); $result = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); }elseif ($for_type == 14){ $dispatch_id = ScrappCount::where('del_time',0) ->where('id', $id) ->pluck('dispatch_sub_id') ->toArray(); $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray(); $box_id = []; foreach ($sale as $value){ $box_detail = new BoxDetail(['channel'=> $value['order_no']]); $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray(); $tmp = Box::where('del_time',0) ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no'))) ->pluck("id") ->toArray(); $box_id = array_merge($box_id, $tmp); } $apply_order_id = ApplyOrderDetail::where('del_time', 0) ->whereIn('data_id', $box_id) ->where('type', ApplyOrder::type_six) ->pluck('apply_order_id') ->toArray(); $return = ApplyOrder::where('del_time', 0) ->whereIn('id', $apply_order_id) ->select('id','order_number as order_no') ->get()->toArray(); $result[] = $return[0] ?? []; } return $result; } private function typeNine($for_type, $id){ $data_id = ApplyOrderDetail::where('del_time', 0) ->where('apply_order_id', $id) ->where('type', ApplyOrder::type_one) ->pluck('data_id') ->toArray(); $result = DispatchSub::where('del_time',0) ->whereIn('id', $data_id) ->select('id', 'dispatch_no as order_no') ->get()->toArray(); return $result; } private function typeTen($for_type, $id){ $data_id = ApplyOrderDetail::where('del_time', 0) ->where('apply_order_id', $id) ->where('type', ApplyOrder::type_two) ->pluck('data_id') ->toArray(); $detail = ReportWorkingDetail::whereIn('data_id', $data_id) ->where('del_time',0) ->select('report_working_id') ->get()->toArray(); $return = ReportWorking::where('del_time',0) ->whereIn('id', array_unique(array_column($detail,'report_working_id'))) ->select('id', 'order_number as order_no') ->get()->toArray(); $result[] = $return[0]; return $result; } private function typeEve($for_type, $id){ $detail = ApplyOrderDetail::where('del_time', 0) ->where('apply_order_id', $id) ->where('type', ApplyOrder::type_four) ->select('data_id as box_id') ->get()->toArray(); $result = Box::where('del_time', 0) ->whereIn('id', array_unique(array_column($detail, 'box_id'))) ->select("id", 'order_no') ->get()->toArray(); return $result; } private function typeTwl($for_type, $id){ $detail = ApplyOrderDetail::where('del_time', 0) ->where('apply_order_id', $id) ->where('type', ApplyOrder::type_three) ->select('data_id as box_id') ->get()->toArray(); $result = Box::where('del_time', 0) ->whereIn('id', array_unique(array_column($detail, 'box_id'))) ->select("id", 'order_no') ->get()->toArray(); return $result; } private function typeThi($for_type, $id){ $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('id','order_number as order_no') ->get()->toArray(); return $result; } private function typeFourTeen($for_type, $id){ $detail = ApplyOrderDetail::where('del_time', 0) ->where('apply_order_id', $id) ->where('type', ApplyOrder::type_six) ->select('data_id as box_id') ->get()->toArray(); $result = Box::where('del_time', 0) ->whereIn('id', array_unique(array_column($detail, 'box_id'))) ->select("id", 'order_no') ->get()->toArray(); return $result; } public function reportList($data){ $model = ReportMessage::where('del_time',0) ->select('id','order_id','opt_case','quantity','crt_time','user_id','is_use') ->orderBy('id','desc'); $list = $this->limit($model,'',$data); $list = $this->fillRData($list); $count = ReportMessage::where('del_time',0) ->where('is_use',0) ->count(); return [true, ['list'=> $list, 'count' => $count]]; } public function fillRData($data){ if(empty($data['data'])) return $data; $dispatch = DispatchSub::whereIn('id',array_unique(array_column($data['data'],'order_id'))) ->select('id','dispatch_no','process_id') ->get()->toArray(); $process = Process::where('del_time',0) ->whereIn('id',array_unique(array_column($dispatch,'process_id'))) ->get()->toArray(); $process = array_column($process,null,'id'); $map = []; foreach ($dispatch as $value){ $map[$value['id']] = $value; } $emp_map = Employee::whereIn('id',array_unique(array_column($data['data'],'user_id'))) ->pluck("emp_name",'id') ->toArray(); foreach ($data['data'] as $key => $value){ $time = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : ''; $data['data'][$key]['crt_time'] = $time; $t = $map[$value['order_id']] ?? []; $e = $emp_map[$value['user_id']] ?? ""; $p = $process[$t['process_id']] ?? []; $p_t = $p['title'] ?? ""; if($p['zj_type'] == 0){ $p_t .= ":无需质检"; }elseif($p['zj_type'] == 1){ $p_t .= ":全检"; }else{ $p_t .= ":抽检"; } $data['data'][$key]['message'] = "派工单:" . $t['dispatch_no'] . "于" . $time . "由($e)完成工序($p_t)报工,请及时处理"; } return $data; } public function reportRemainUpdate($data){ if(empty($data['id'])) return [false, 'ID不能为空']; if(empty($data['type'])) return [false, 'type不能为空']; $type = $data['type']; if($type == 1){ ReportMessage::where('id', $data['id']) ->update(['is_use' => 1]); }else{ ReportMessage::where('id', $data['id']) ->update(['del_time' => time()]); } return [true, '']; } public function filldatas(){ $box = Box::where('del_time',0) ->where('crt_time','>=',1750727777) ->select("top_order_no", 'order_no','id') ->get()->toArray(); $box_id = []; foreach ($box as $value) { $box_detail = new BoxDetail(['channel' => $value['top_order_no']]); $t = $box_detail->where('order_no', $value['order_no'])->where('del_time',0)->select('top_id as id')->first()->toArray(); $box_id[$value['id']] = $t['id']; } $result = DispatchSub::where('del_time',0) ->whereIn('sale_orders_product_id', array_values($box_id)) ->where('process_id',14) ->where('finished_num', '>', '0.000') ->select('id','dispatch_quantity','finished_num','order_product_id','sale_orders_product_id','technology_name') ->get()->toArray(); $map = []; $return = []; foreach ($result as $key => $value){ if($value['finished_num'] < 1){ $new_key = $value['order_product_id'] . $value['sale_orders_product_id'] . $value['technology_name']; $tmp = bcsub(0.750,$value['finished_num'],3); $map[$new_key][] = $tmp; $return[] = $value; } }dump(count($return)); $return2= []; foreach ($result as $key => $value){ if($value['finished_num'] >= 1){ $new_key = $value['order_product_id'] . $value['sale_orders_product_id'] . $value['technology_name']; if(isset($map[$new_key])){ foreach ($map[$new_key] as $val){ $return[] = [ 'id' => $value['id'], 'dispatch_quantity' => $val, 'finished_num' => $val, 'order_product_id' => $value['order_product_id'], 'sale_orders_product_id' => $value['sale_orders_product_id'], 'technology_name' => $value['technology_name'], ]; $value['finished_num'] = bcsub($value['finished_num'], $val,3); } } $return2[] = $value; } } dd($return2); } }