where('dispatch_sub_id', $data['id']) ->where('process_id', $data['process_id']) ->select('scrapp_id','scrapp_num as num','process_id') ->get()->toArray(); $return = []; foreach ($old as $value){ $key = $value['scrapp_id'] . $value['process_id']; if(isset($return[$key])){ $return[$key]['num'] += $value['num']; }else{ $return[$key] = [ 'scrapp_id' => $value['scrapp_id'], 'num' => $value['num'], 'process_id' => $value['process_id'], ]; } } return [true, ['save' => array_values($return)]]; } public function editZj($data, $user){ list($status, $msg) = $this->editZjRule($data); if(! $status) return [false, $msg]; $order = $msg; try{ DB::beginTransaction(); $time = time(); //更新次品数量 $num = 0; if(! empty($data['waste'])){ foreach ($data['waste'] as $value){ $num += $value['num']; } } DispatchSub::where('id',$order['id'])->update([ 'waste_num' => $num, ]); $insert = []; foreach ($data['waste'] as $value){ $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' => $value['num'], 'scrapp_id' => $value['scrapp_id'], 'team_id' => $data['team_id'], 'finished_id' => 0, 'equipment_id' => $data['equipment_id'], 'order_number' => $order['id'] . $time . $order['process_id'], 'process_id' => $order['process_id'], 'quantity' => $order['finished_num'], ]; } //质检单 不良品单 ScrappCount::where('del_time',0) ->where('dispatch_sub_id', $order['id']) ->where('process_id',$order['process_id']) ->update(['del_time' => $time]); if(! empty($insert)) ScrappCount::insert($insert); DB::commit(); }catch (\Throwable $exception){ DB::rollBack(); return [false, $exception->getMessage()]; } return [true, '']; } public function editZjRule($data){ if(empty($data['id'])) return [false, '完工ID不能为空']; $order = DispatchSub::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($order)) return [false, '完工单不存在或已被删除']; $order = $order->toArray(); if(empty($data['team_id'])) return [false, '质检班组不能为空']; // if(empty($value['finished_id'])) return [false, '质检人员不能为空']; if(empty($data['equipment_id'])) return [false, '设备不能为空']; // if(empty($data['waste'])) return [false, '质检信息不能为空']; // if(! empty($data['waste'])){ // $num = 0; // foreach ($data['waste'] as $value){ // if(empty($value['num'])) return [false, '质检损耗数量不能为空']; // if(empty($value['scrapp_id'])) return [false, '质检损耗原因不能为空']; // $num += $value['num']; // } // // if($num > $order['finished_num']) return [false, '质检总数不能超过完工数量']; // } return [true, $order]; } public function setOrderNO(){ $str = date('Ymd',time()); $order_number = FinishedOrder::where('finished_no','Like','%'. $str . '%') ->max('finished_no'); 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; //超过99999 if(strlen($tmp) > 3) return ''; $number = str_pad($tmp,3,'0',STR_PAD_LEFT); $number = $str . $number; } return $number; } public function add($data,$user){ //数据校验以及填充 list($status,$msg) = $this->orderRule($data); if(!$status) return [$status,$msg]; //数据源 $result = $msg; $time = time(); try{ DB::beginTransaction(); $em = new EmployeeService(); $auto = $em->is_auto($user, "wg_auto"); //班组下的人 $team_man = []; $team_array = EmployeeTeamPermission::select('employee_id', 'team_id')->get()->toArray(); foreach ($team_array as $v){ $team_man[$v['team_id']][] = $v['employee_id']; } $last_update = $last_update2 = $str_tmp = []; foreach ($result as $value){ $finished_num = $value['quantity'] + $value['finished_num']; $waste_num = $value['waste_quantity'] + $value['waste_num']; DispatchSub::where('id',$value['id'])->update([ 'finished_num' => $finished_num, 'waste_num' => $waste_num, 'wg_status' => $auto, ]); $tmp = [ 'id' => $value['id'], 'quantity' => $value['quantity'], 'product_no' => $value['product_no'] ?? "", 'product_title' => $value['product_title'] ?? "", 'product_size' => $value['product_size'] ?? "", 'product_unit' => $value['product_unit'] ?? "", ]; $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name']; if(! in_array($str, $str_tmp)){ $last_update[] = $tmp; $str_tmp[] = $str; } //班组下的人 $man = $team_man[$value['team_id']] ?? []; $f_m = 0; if(! in_array($value['finish_id'], $man) && $value['finish_id'] > 0) { $man[] = $value['finish_id']; $f_m = 1; } $tmp['process_id'] = $value['process_id']; foreach ($man as $m_v){ if($f_m && $m_v == $value['finish_id']){ $tmp['team_id'] = 0; }else{ $tmp['team_id'] = $value['team_id']; } $tmp['finished_id'] = $m_v; $last_update2[] = $tmp; } $insert_waste = $scrapp = []; if(! empty($value['waste_array'])){ foreach ($value['waste_array'] as $k => $v){ // $num = $v['num'] * 1000; // for($i = 0 ;$i < $num; $i++){ // $insert_waste[] = [ // 'order_product_id' => $value['order_product_id'], // 'order_no' => $value['order_no'], // 'product_no' => $value['product_no'], // 'product_title' => $value['product_title'], // 'process_id' => $value['process_id'], // 'crt_time' => $time, // 'dispatch_no' => $value['dispatch_no'], // 'status' => 4,//不良品 // 'team_id' => $value['team_id'], // 'finished_id' => $value['finish_id'], // 'equipment_id' => $value['device_id'], // 'scrapp_id' => $v['scrapp_id'] // ]; // } $scrapp[] = [ 'sale_orders_product_id' => $value['sale_orders_product_id'], 'order_product_id' => $value['order_product_id'], 'dispatch_sub_id' => $value['id'], 'out_order_no' => $value['out_order_no'], 'order_no' => $value['order_no'], 'customer_no' => $value['customer_no'], 'customer_name' => $value['customer_name'], 'product_no' => $value['product_no'], 'product_title' => $value['product_title'], 'product_size' => $value['product_size'], 'product_unit' => $value['product_unit'], 'technology_material' => $value['technology_material'], 'technology_name' => $value['technology_name'], 'wood_name' => $value['wood_name'], 'price' => $value['price'], 'process_mark' => $value['process_mark'], 'table_body_mark' => $value['table_body_mark'], 'table_header_mark' => $value['table_header_mark'], 'crt_id' => 1, 'crt_time' => $time, 'scrapp_num' => $v, 'scrapp_id' => $k, 'team_id' => $value['team_id'] ?? 0, 'finished_id' => $value['finish_id'] ?? 0, 'equipment_id' => $value['device_id'] ?? 0, 'order_number' => $value['id'] . $time . $value['process_id'], 'process_id' => $value['process_id'] ?? 0, 'quantity' => $value['quantity'], ]; } } $quantity = $value['quantity'] * 1000; //工序表 $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]); $process_model->where('order_product_id',$value['order_product_id']) ->where('process_id',$value['process_id']) ->where('dispatch_no',$value['dispatch_no']) ->where('status', 1) ->take($quantity) ->update([ 'finished_time' => $time, 'status' => 2, 'finished_id' => $value['finish_id'] ?? 0, 'team_id' => $value['team_id'] ?? 0, 'equipment_id' => $value['device_id'] ?? 0, ]); if(! empty($insert_waste)) $process_model->insert($insert_waste); if(! empty($scrapp)) { $scrapp_model = new ScrappCount(); $scrapp_model->insert($scrapp); } // // if(! empty($value['waste_quantity'])){ // $num = $value['waste_quantity']; // OrdersProduct::where('id',$value['order_product_id'])->update([ // 'scrapp_num' => DB::raw("scrapp_num + {$num}"), // ]); // } } //反写数量 //生产订单 $id = array_column($result,'order_product_id'); $this->writeFinishedQuantityByOrdersProductId($id); //销售订单 $this->writeFinishedQuantity($id); //生成完工入库申请单 $service = new BoxService(); list($status, $msg) = $service->createWGSQ($last_update, $user); if(! $status) return [false, $msg]; //生成报工单 list($status, $msg) = $this->createbg($last_update2, $user); if(! $status) return [false, $msg]; DB::commit(); }catch (\Exception $e){ DB::rollBack(); return [false,$e->getFile() . $e->getLine(). $e->getMessage()]; } return [true, '']; } public function createbg($insert, $user){ //报工单 $em = new EmployeeService(); $auto = $em->is_auto($user, "wgbg_auto"); $order_number = (new ApplyOrderService())->setOrderNO2(); if(empty($order_number)) return [false, '报工单编号生成失败']; try { DB::beginTransaction(); $time = time(); $model = new ReportWorking(); $model->order_number = $order_number; $model->report_time = $time; $model->mark = $data['mark']?? ""; $model->crt_id = $user['id']; $model->status = $auto; $model->save(); $id = $model->id; $detail_insert = []; foreach ($insert as $v){ $detail_insert[] = [ 'report_working_id' => $id, 'data_id' => $v['id'], 'quantity' => $v['quantity'] ?? 0, 'finished_id' => $v['finished_id'] ?? 0, 'process_id' => $v['process_id'] ?? 0, 'team_id' => $v['team_id'] ?? 0, 'crt_time' => $time, ]; } ReportWorkingDetail::insert($detail_insert); DB::commit(); }catch (\Throwable $exception){ DB::rollBack(); return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()]; } return [true, '']; } //产成品入库 public function U8Rdrecord10Save($package_data, $user){ if(empty($package_data)) return [false, '产成品入库为空,请确认!']; try{ //获取包装单信息 $box = $package_data; $boxDetail = new BoxDetail(['channel'=>$box['top_order_no']]); $boxDetail = $boxDetail->where('del_time',0) ->where('order_no',$box['order_no']) ->select('id','top_id','num','ext_1','ext_2','ext_3','ext_4','ext_5','out_order_no','box_type','price') ->get()->toArray(); //用友数据插入------------ if(! empty($boxDetail)){ $sqlServerModel = new FyySqlServerService($user); if($sqlServerModel->error) return [false, $sqlServerModel->error]; $username = $sqlServerModel->getYongyouName(); $box['create_name'] = $username; list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($box,$boxDetail); if(! $status) return [false, $msg]; } //用友数据插入------------ }catch (\Exception $e){ return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()]; } return [true,'']; } //销售发货出库 public function U8Rdrecord32Save($send_data, $user){ if(empty($send_data)) return [false, '销售发货出库为空,请确认!']; try{ //用友数据插入------------ $sqlServerModel = new FyySqlServerService($user); if($sqlServerModel->error) return [false, $sqlServerModel->error]; $create_name = $sqlServerModel->getYongyouName(); list($status,$msg) = $sqlServerModel->U8Rdrecord32Save($send_data,$create_name); if(! $status) return [false, $msg]; //用友数据插入------------ }catch (\Exception $e){ return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()]; } return [true,'']; } public function addInJob($result,$data,$user){ try{ //获取数据库连接 $database = $this->getConnectionName($user['zt']); //用友数据插入------------ $insert_sql_server = []; foreach ($result as $key => $value){ $quantity_tmp = $data['quantity'][$key]; $result[$key]['quantity'] = $quantity_tmp; //工序表 $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]); $process_model->setConnection($database); $process_id = $process_model->select('process_id') ->where('sort',$process_model->where('del_time',0) ->where('order_product_id',$value['order_product_id']) ->max('sort')) ->first(); if(empty($process_id)) return [false,"未找到最后一道工序"]; $process_id = $process_id->process_id; if($process_id == $value['process_id']){ $insert_sql_server[] = $result[$key]; } } if(! empty($insert_sql_server)){ $sqlServerModel = new FyySqlServerService($user); if($sqlServerModel->error) return [false,$sqlServerModel->error]; foreach ($insert_sql_server as $value){ // list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value); // if(! $status) return [false,$msg]; } } //用友数据插入结束---------- //本地数据更新 DB::beginTransaction(); $waste = []; foreach ($data['waste'] as $key => $value){ $waste[$key] = array_sum(array_column($value,'num')); } $time = time(); foreach ($result as $key => $value){ $finished_id_tmp = $data['finish_id'][$key]; $team_tmp = $data['team_id'][$key]; $equipment_id_tmp = $data['equipment_id'][$key]; $finished_num = $value['quantity'] + $value['finished_num']; $model = new DispatchSub(); $model->setConnection($database); $model->where('id',$value['id'])->update([ 'finished_num' => $finished_num, 'waste_num' => $waste[$key], 'job_status' => 0, 'dispatch_quantity' => DB::raw("dispatch_quantity + {$waste[$key]}"),//派工数量增加 派工单可以继续派送 ]); $insert_waste = $insert_dispatch = $scrapp = []; if(! empty($data['waste'][$key])){ foreach ($data['waste'][$key] as $v){ for($i = 0 ;$i < $v['num']; $i++){ $insert_waste[] = [ 'order_product_id' => $value['order_product_id'], 'order_no' => $value['order_no'], 'product_no' => $value['product_no'], 'product_title' => $value['product_title'], 'process_id' => $value['process_id'], 'crt_time' => $time, 'dispatch_no' => $value['dispatch_no'], 'status' => 4,//不良品 'team_id' => $team_tmp, 'finished_id' => $finished_id_tmp, 'equipment_id' => $equipment_id_tmp, 'scrapp_id' => $v['scrapp_id'] ]; $insert_dispatch[] = [ 'order_product_id' => $value['order_product_id'], 'out_order_no' => $value['out_order_no'], 'order_no' => $value['order_no'], 'product_no' => $value['product_no'], 'product_title' => $value['product_title'], 'process_id' => $value['process_id'], 'crt_time' => $time, 'dispatch_no' => $value['dispatch_no'], 'status' => 1, //已派工 'team_id' => 0, 'finished_id' => 0, 'equipment_id' => 0, 'scrapp_id' => 0 ]; } $scrapp[] = [ 'sale_orders_product_id' => $value['sale_orders_product_id'], 'order_product_id' => $value['order_product_id'], 'out_order_no' => $value['out_order_no'], 'order_no' => $value['order_no'], 'customer_no' => $value['customer_no'], 'customer_name' => $value['customer_name'], 'product_no' => $value['product_no'], 'product_title' => $value['product_title'], 'product_size' => $value['product_size'], 'product_unit' => $value['product_unit'], 'technology_material' => $value['technology_material'], 'technology_name' => $value['technology_name'], 'wood_name' => $value['wood_name'], 'price' => $value['price'], 'process_mark' => $value['process_mark'], 'table_body_mark' => $value['table_body_mark'], 'table_header_mark' => $value['table_header_mark'], 'crt_time' => $time, 'scrapp_num' => $v['num'], 'scrapp_id' => $v['scrapp_id'] ]; } } //工序表 $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]); $process_model->setConnection($database); $process_model->where('order_product_id',$value['order_product_id']) ->where('process_id',$value['process_id']) ->where('dispatch_no',$value['dispatch_no']) ->take($value['quantity']) ->update([ 'finished_time' => $time, 'status' => 2, 'finished_id' => $finished_id_tmp, 'team_id' => $team_tmp, 'equipment_id' => $equipment_id_tmp ]); if(! empty($insert_waste)) $process_model->insert($insert_waste); if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch); if(! empty($scrapp)) { $scrapp_model = new ScrappCount(); $scrapp_model->setConnection($database); $scrapp_model->insert($scrapp); } //生产订单数量 if(! empty($waste[$key])){ $num = $waste[$key]; $model2 = new OrdersProduct(); $model2->setConnection($database); $model2->where('id',$value['order_product_id'])->update([ 'production_quantity' => DB::raw("production_quantity + {$num}"), 'scrapp_num' => DB::raw("scrapp_num + {$num}"), 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加 ]); } } //反写数量 // $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database); // $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database); DB::commit(); }catch (\Exception $e){ DB::rollBack(); return [false,$e->getFile() . $e->getLine(). $e->getMessage()]; } return [true,'']; } public function del($data){ if($this->isEmpty($data,'id')) return [false,'ID不能为空!']; return [true,'删除成功']; } public function orderDetail($data){ if($this->isEmpty($data,'id')) return [false,'ID不能为空!']; $first = DispatchSub::where('id',$data['id'])->first(); $result = []; $scrapp = ScrappCount::where('del_time',0) ->where('order_product_id',$first->order_product_id) ->select('order_product_id','scrapp_num as num', 'scrapp_id') ->get()->toArray(); foreach ($scrapp as $value){ if(isset($result[$value['order_product_id']])){ $tmp = bcadd($result[$value['scrapp_id']],$value['num'],3); $result[$value['scrapp_id']]['num'] = $tmp; }else{ $result[$value['scrapp_id']] = [ 'scrapp_id' => $value['scrapp_id'], 'num' => $value['num'] ]; } } $result = array_values($result); // $first = DispatchSub::where('id',$data['id'])->first(); // $process_model = new OrdersProductProcess(['channel' => date("Ymd",$first->out_order_no_time)]); // // $result = $process_model->where('del_time',0) // ->where('dispatch_no',$first->dispatch_no) // ->where('order_product_id',$first->order_product_id) // ->where('status',4) // ->select(DB::raw('count(id) as num'),'scrapp_id') // ->groupBy('scrapp_id') // ->get()->toArray(); $map = Scrapp::whereIn('id',array_column($result,'scrapp_id')) ->pluck('title','id') ->toArray(); foreach ($result as $key => $value){ $result[$key]['scrapp_name'] = $map[$value['scrapp_id']] ?? ''; } return [true,$result]; } public function is_same_month($timestamp1,$timestamp2){ // 格式化时间戳为年份和月份 $year1 = date('Y', $timestamp1); $month1 = date('m', $timestamp1); $year2 = date('Y', $timestamp2); $month2 = date('m', $timestamp2); if ($year1 === $year2 && $month1 === $month2) { return true; } else { return false; } } public function orderRule($data){ if(! isset($data['is_finish_all'])) return [false, '完工类型不能为空!']; if(empty($data['detail'])) return [false, '完工明细数据不能为空!']; $detail_map = $id = $detail_map2 = $waste_map = []; //班组下的人 $team_man = []; $team_array = EmployeeTeamPermission::select('employee_id', 'team_id')->get()->toArray(); foreach ($team_array as $v){ $team_man[$v['team_id']][] = $v['employee_id']; } foreach ($data['detail'] as $value){ if(empty($value['id'])) return [false,'请选择完工数据!']; $id[] = $value['id']; if(empty($value['device_id'])) return [false, '请选择设备!']; if(empty($value['team_id'])) return [false, '班组必须选择']; if(empty($team_man[$value['team_id']])) return [false, '班组下的人不能为空,请设置']; // if(empty($value['team_id']) && empty($value['finish_id'])) return [false,'班组和人员必须选择一个!']; if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'完工数量不能小于0!']; if(! empty($value['waste'])){ foreach ($value['waste'] as $waste){ if(empty($waste['scrapp_id'])) return [false,'请选择损耗原因!']; if(! is_numeric($waste['num']) || $waste['num'] < 0) return [false,'损耗数量不能小于0!']; if(isset($waste_map[$value['id']])){ $waste_map[$value['id']] += $waste['num']; }else{ $waste_map[$value['id']] = $waste['num']; } } } //完工数量 if(isset($detail_map[$value['id']])){ $detail_map[$value['id']] += $value['quantity']; }else{ $detail_map[$value['id']] = $value['quantity']; } if(! isset($detail_map2[$value['id']])){ $detail_map2[$value['id']] = $value; } } //校验 $result = DispatchSub::where('del_time',0) ->whereIn('id',$id) ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id','crt_time') ->get()->toArray(); if(empty($result)) return [false, '派工单不存在或已被删除!']; $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))->pluck('title','id')->toArray(); $search = "";$args = []; foreach ($result as $key => $value){ $detail2 = $detail_map2[$value['id']] ?? []; $result[$key]['device_id'] = $detail2['device_id'] ?? 0; $result[$key]['team_id'] = $detail2['team_id'] ?? 0; $result[$key]['finish_id'] = $detail2['finish_id'] ?? 0; $result[$key]['waste_quantity'] = $waste_map[$value['id']] ?? 0; $tmp = []; if(! empty($detail2['waste'])){ foreach ($detail2['waste'] as $v){ if(isset($tmp[$v['id']])){ $n = bcadd($v['num'],$tmp[$v['scrapp_id']],3); $tmp[$v['scrapp_id']] = $n; }else{ $tmp[$v['scrapp_id']] = $v['num']; } } } $result[$key]['waste_array'] = $tmp; $q = $detail_map[$value['id']] ?? 0; $quantity_tmp = bcadd($q,$value['finished_num'],3); if($quantity_tmp > $value['dispatch_quantity']) { $process_tmp = $process_map[$value['process_id']] ?? ""; return [false,'派工单:' . $value['dispatch_no']. '的工序' . $process_tmp .'完工数量不能大于派工数量']; } $result[$key]['quantity'] = $q; $search_key = $value['order_product_id'] . $value['crt_time']; if($data['is_finish_all'] && ! in_array($search_key , $args)){ $search .= "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR "; $args[] = $search_key; } } $search = rtrim($search,'OR '); $search = "($search)"; if($data['is_finish_all']){ //系统帮助完工的数据 $result2 = DispatchSub::where('del_time',0) ->whereNotIn('id',$id) ->whereColumn('dispatch_quantity', '>', 'finished_num') ->whereRaw($search) ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id','crt_time') ->get()->toArray(); if(! empty($result2)){ foreach ($result2 as $key => $value){ $not_finished_num = $value['dispatch_quantity'] - $value['finished_num']; $result2[$key]['quantity'] = $not_finished_num; $result2[$key]['waste_array'] = []; $result2[$key]['waste_quantity'] = 0; $result2[$key]['device_id'] = 0; $result2[$key]['team_id'] = 0; $result2[$key]['finish_id'] = 0; } } $result = array_merge_recursive($result, $result2); } return [true, $result]; } public function orderList($data){ $model = FinishedOrderSub::where('del_time',0) ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','dispatch_quantity','finished_num','status','crt_id','process_id','equipment_id','team_id','dispatch_time_start','dispatch_time_end','dispatch_time','crt_time','dispatch_no') ->orderBy('upd_time','desc') ->orderBy('id','desc'); if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%'); if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%'); if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%'); if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%'); if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%'); if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%'); if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%'); if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%'); if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%'); if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])) $model->whereBetween('dispatch_time',[$data['dispatch_time'][0],$data['dispatch_time'][1]]); if(! empty($data['employee_id'])) { $team_id = Employee::from('employee as a') ->leftJoin('employee_team_permission as b','b.employee_id','a.id') ->where('a.id', $data['employee_id']) ->select('b.team_id') ->get()->toArray(); $team_id = array_column($team_id,'team_id'); $model->where('team_id',$team_id ?? []); } if(isset($data['status'])) $model->where('status',$data['status']); $list = $this->limit($model,'',$data); $list = $this->fillData($list); return [true,$list]; } public function fillData($data){ if(empty($data['data'])) return $data; $waste_map = $waste_map_2 = []; $waste = FinishedOrderScrapp::where('del_time',0) ->whereIn('finished_order_id',array_column($data['data'],'id')) ->select('finished_order_id','num','scrapp_id') ->get()->toArray(); if(! empty($waste)){ foreach ($waste as $value){ $waste_map[$value['finished_order_id']][] = [ 'num' => $value['num'], 'scrapp_id' => $value['scrapp_id'] ]; if(isset($waste_map_2[$value['finished_order_id']])){ $waste_map_2[$value['finished_order_id']] += $value['num']; }else{ $waste_map_2[$value['finished_order_id']] = $value['num']; } } } $team = EmployeeTeamPermission::from('employee_team_permission as a') ->leftJoin('employee as b','b.id','a.employee_id') ->whereIn('a.team_id',array_column($data['data'],'team_id')) ->select('b.emp_name','a.team_id') ->get() ->toArray(); $team_map = []; if(! empty($team)){ foreach ($team as $value){ if(isset($team_map[$value['team_id']])){ $team_map[$value['team_id']] .= ','. $value['emp_name']; }else{ $team_map[$value['team_id']] = $value['emp_name']; } } } $process_map = Process::whereIn('id',array_column($data['data'],'process_id')) ->pluck('title','id') ->toArray(); $team_maps = Team::whereIn('id',array_column($data['data'],'team_id')) ->pluck('title','id') ->toArray(); $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id')) ->pluck('title','id') ->toArray(); foreach ($data['data'] as $key => $value){ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : ''; $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : ''; $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : ''; $data['data'][$key]['dispatch_plan_time'] = $time1 . ' ' . $time2; $data['data'][$key]['dispatch_time'] = $value['dispatch_time'] ? date('Y-m-d',$value['dispatch_time']) : ''; $data['data'][$key]['team_man'] = $team_map[$value['team_id']] ?? ''; $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? ''; $data['data'][$key]['team_name'] = $team_maps[$value['team_id']] ?? ''; $data['data'][$key]['equipment_name'] = $equipment_map[$value['equipment_id']] ?? ''; $data['data'][$key]['waste'] = $waste_map[$value['id']] ?? []; $data['data'][$key]['waste_quantity'] = $waste_map_2[$value['id']] ?? 0; $data['data'][$key]['not_finished_num'] = $value['dispatch_quantity'] - $value['finished_num']; } $data['finished_num'] = $this->getTotal($data['data'], 'finished_num'); $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity'); $data['waste_quantity'] = $this->getTotal($data['data'], 'waste_quantity'); $data['not_finished_num'] = $this->getTotal($data['data'], 'not_finished_num'); return $data; } //反写销售订单完工数量 public function writeFinishedQuantity($orders_product_id){ if(empty($orders_product_id)) return; $sale_orders_product_id = OrdersProduct::where('del_time',0) ->whereIn('id', $orders_product_id) ->select('sale_orders_product_id') ->get()->toArray(); $sale_orders_product_id = array_unique(array_column($sale_orders_product_id,'sale_orders_product_id')); $production_order = OrdersProduct::where('del_time',0) ->whereIn('sale_orders_product_id', $sale_orders_product_id) ->select(DB::raw("sum(finished_num) as finished_num"),'sale_orders_product_id') ->groupby('sale_orders_product_id') ->pluck('finished_num','sale_orders_product_id') ->toArray(); foreach ($production_order as $sale_orders_product_id => $finished_num){ SaleOrdersProduct::where('id',$sale_orders_product_id)->update([ 'finished_num' => $finished_num, ]); } } //反写生产订单完工数量 public function writeFinishedQuantityByOrdersProductId($order_product_id){ if(empty($order_product_id)) return; $result = DispatchSub::where('del_time',0) ->whereIn('order_product_id',$order_product_id) ->select('finished_num','order_product_id','crt_time','process_id') ->get() ->toArray(); if(empty($result)) return; //生产订单 $production_order = OrdersProduct::where('del_time',0) ->whereIn('id',array_unique(array_column($result,'order_product_id'))) ->select('process_id','id','production_quantity') ->get() ->toArray(); $judge = []; foreach ($result as $value){ if(isset($judge[$value['order_product_id']][$value['process_id']])){ $judge[$value['order_product_id']][$value['process_id']] += $value['finished_num']; }else{ $judge[$value['order_product_id']][$value['process_id']] = $value['finished_num']; } } foreach ($production_order as $value){ //生产订单下的所有工序派工单 $tmp = $judge[$value['id']] ?? []; if(empty($tmp)) continue; //生产订单下的工序种类 $ori_process = explode(',',$value['process_id']); $ori_process_count = count($ori_process); //生产订单下的派工单的工序种类 $process = array_keys($tmp); $process_count = count($process); if($ori_process_count != $process_count) continue; //获取工序里最小的数值 $min_finished_num = min($tmp); //更新完工数量 OrdersProduct::where('id',$value['id'])->update([ 'finished_num' => $min_finished_num ]); } } public function mobileAdd($data,$user){ //数据校验以及填充 list($status,$msg,$count_arr) = $this->orderMobileRule($data); if(!$status) return [$status,$msg]; $user = [ 'id' => $user['id'], 'operate_time' => time(), 'zt' => Request()->header('zt'), ]; $redis = [ 'result' => $msg, 'data' => $data, 'quantity_count' => $count_arr, ]; $job = ProcessDataJob::dispatch($redis,$user,3)->onQueue(ProcessDataJob::job_one); if(! $job) return [false,'任务没有进入队列!']; //错误计数 Redis::hSet('order_failures', md5(json_encode($redis)), 0); //标记进入队列的数据 DispatchSub::whereIn('id',array_column($msg,'id'))->update([ 'job_status' => 1, ]); return [true,'任务已进入队列!']; } public function addMobileInJob($result, $data, $count_arr,$user){ try{ //获取数据库连接 $database = $this->getConnectionName($user['zt']); //用友数据写入------------ $insert_sql_server = []; foreach ($result as $key => $value){ $quantity_tmp = $count_arr[$value['id']]; $result[$key]['quantity'] = $quantity_tmp; $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]); $process_model->setConnection($database); $process_id = $process_model->select('process_id') ->where('sort',$process_model->where('del_time',0) ->where('order_product_id',$value['order_product_id']) ->max('sort')) ->first(); if(empty($process_id)) return [false,"未找到最后一道工序"]; $process_id = $process_id->process_id; if($process_id == $value['process_id']){ $insert_sql_server[] = $result[$key]; } } if(! empty($insert_sql_server)){ $sqlServerModel = new FyySqlServerService($user); if($sqlServerModel->error) return [false,$sqlServerModel->error]; foreach ($insert_sql_server as $value){ // list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value); // if(! $status) return [false,$msg]; } } //用友数据写入结束------------ //本地数据写入----------- DB::beginTransaction(); $waste = $waste2 = []; foreach ($data as $value){ if(! empty($value['waste'])){ foreach ($value['waste'] as $v){ if(isset($waste[$value['id']])){ $waste[$value['id']] += $v['num']; $waste2[$value['order_product_id']] += $v['num']; }else{ $waste[$value['id']] = $v['num']; $waste2[$value['order_product_id']] = $v['num']; } } } } $time = time(); foreach ($result as $value){ $finished_num = $value['quantity'] + $value['finished_num']; $watste_num = $waste[$value['id']] ?? 0; $model = new DispatchSub(); $model->setConnection($database); $model->where('id',$value['id'])->update([ 'finished_num' => $finished_num, 'waste_num' => $watste_num, 'job_status' => 0, 'dispatch_quantity' => DB::raw("dispatch_quantity + {$watste_num}"),//派工数量增加 派工单可以继续派送 ]); //生产订单数量 if(! empty($waste2[$value['order_product_id']])){ $num = $waste2[$value['order_product_id']]; $model2 = new OrdersProduct(); $model2->setConnection($database); $model2->where('id',$value['order_product_id'])->update([ 'production_quantity' => DB::raw("production_quantity + {$num}"), 'scrapp_num' => DB::raw("scrapp_num + {$num}"), 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加 ]); } //损耗和派工 损耗统计 $insert_waste = $insert_dispatch = $scrapp = []; //工序 $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]); $process_model->setConnection($database); foreach ($data as $d){ if($d['id'] == $value['id']){ $process_model->where('order_product_id',$value['order_product_id']) ->where('process_id',$value['process_id']) ->where('dispatch_no',$value['dispatch_no']) ->take($d['quantity']) ->update([ 'finished_time' => $time, 'status' => 2, 'finished_id' => $d['finished_id'] ?? 0, 'team_id' => $d['team_id'] ?? 0, 'equipment_id' => $d['equipment_id'] ?? 0 ]); if(! empty($d['waste'])){ foreach ($d['waste'] as $v){ for($i = 0 ;$i < $v['num']; $i++){ $insert_waste[] = [ 'order_product_id' => $value['order_product_id'], 'out_order_no' => $value['out_order_no'], 'order_no' => $value['order_no'], 'product_no' => $value['product_no'], 'product_title' => $value['product_title'], 'process_id' => $value['process_id'], 'crt_time' => $time, 'dispatch_no' => $value['dispatch_no'], 'status' => 4, 'team_id' => $d['team_id'], 'finished_id' => $d['finished_id'] ?? 0, 'equipment_id' => $d['equipment_id'] ?? 0, 'scrapp_id' => $v['scrapp_id'] ?? 0 ]; $insert_dispatch[] = [ 'order_product_id' => $value['order_product_id'], 'out_order_no' => $value['out_order_no'], 'order_no' => $value['order_no'], 'product_no' => $value['product_no'], 'product_title' => $value['product_title'], 'process_id' => $value['process_id'], 'crt_time' => $time, 'dispatch_no' => $value['dispatch_no'], 'status' => 1, //已派工 'team_id' => 0, 'finished_id' => 0, 'equipment_id' => 0, 'scrapp_id' => 0 ]; } $scrapp[] = [ 'sale_orders_product_id' => $value['sale_orders_product_id'], 'order_product_id' => $value['order_product_id'], 'out_order_no' => $value['out_order_no'], 'order_no' => $value['order_no'], 'customer_no' => $value['customer_no'], 'customer_name' => $value['customer_name'], 'product_no' => $value['product_no'], 'product_title' => $value['product_title'], 'product_size' => $value['product_size'], 'product_unit' => $value['product_unit'], 'technology_material' => $value['technology_material'], 'technology_name' => $value['technology_name'], 'wood_name' => $value['wood_name'], 'price' => $value['price'], 'process_mark' => $value['process_mark'], 'table_body_mark' => $value['table_body_mark'], 'table_header_mark' => $value['table_header_mark'], 'crt_time' => $time, 'scrapp_num' => $v['num'], 'scrapp_id' => $v['scrapp_id'] ]; } } } } if(! empty($insert_waste)) $process_model->insert($insert_waste); if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch); if(! empty($scrapp)) { $scrapp_model = new ScrappCount(); $scrapp_model->setConnection($database); $scrapp_model->insert($scrapp); } } //反写数量 // $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database); // $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database); DB::commit(); //本地数据写入结束----------- }catch (\Exception $e){ DB::rollBack(); return [false,$e->getFile() . $e->getLine() . $e->getMessage()]; } return [true,'']; } public function orderMobileRule($data){ if(empty($data)) return [false,'数据不能为空!','']; $dispatch_id = array_unique(array_column($data,'id')); $bool = DispatchSub::whereIn('id',$dispatch_id)->where('job_status',1)->exists(); if($bool) return [false,'正在队列中,请不要重复操作!','']; $post = $waste = []; foreach ($data as $value){ if(! is_numeric($value['quantity']) || $value['quantity'] <= 0) return [false,'请填写正确的完工数量!','']; if(empty($value['finished_id']) && empty($value['team_id'])) return [false,'人员和班组必须填写一项!','']; if(isset($post[$value['id']])){ $post[$value['id']] += $value['quantity']; }else{ $post[$value['id']] = $value['quantity']; } if(! empty($value['waste'])){ foreach ($value['waste'] as $v){ if(isset($waste[$value['id']])){ $waste[$value['id']] += $v['num']; }else{ $waste[$value['id']] = $v['num']; } } } } $result = DispatchSub::whereIn('id',$dispatch_id) ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id') ->orderBy('id','desc') ->get()->toArray(); foreach ($result as $key => $value){ $tmp = $waste[$value['id']] ?? 0; $quantity_tmp = $post[$value['id']] + $value['finished_num'] + $value['waste_num'] + $tmp; if($quantity_tmp > $value['dispatch_quantity']) return [false,"完工数量加上损耗数量不能大于派工数量",'']; } return [true, $result, $post]; } public function getConnectionName($name) { $mysql = "mysql"; if ($name === '001') { $mysql = "mysql_001"; } elseif ($name === '002') { $mysql = "mysql_002"; } return $mysql; } public function insertYy($order, $user){ //包装单 $package_data = json_decode($order['post'], true); if(! empty($package_data)) { //用友 ------产成品入库 $service = new FinishedOrderService(); list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user); if(! $status) return [false, $msg]; } return [true, '']; } }