pluck('value', 'key')->toArray(); self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray(); self::$box_hook = BoxHookService::getInstance(); } /** * 包装 * @param $data * @return array */ public function boxIn($data) { // if(!isset($data['params'])) return [false,'not found params']; // $param = $data['params']; // $param = [ // [ // 'id' => 716, // 'order_no' => 716, // 'params' => [ // '1', // '1', // // ], // ], [ // 'id' => 723, // 'order_no' => 716, // 'params' => [ // '40' // ], // ], // ]; $ids = []; $sale_ids = []; // $key_list = []; // var_dump($data);die; $top_order_no = $data['order_no']; $box_no = isset($data['box_no']) ? $data['box_no'] : ''; $transport_no = isset($data['transport_no']) ? $data['transport_no'] : ''; $data = $data['param']; $key_list = []; foreach ($data as $v) { if($v['id'] < 0) $sale_ids[] = -$v['id']; else $ids[] = $v['id']; if($v['id'] < 0) $v['id'] = -$v['id']; $total = 0; foreach ($v['params'] as $vv) { $total += $vv; } $key_list[$v['id']] = [ 'detail' => $v['params'], 'total' => $total, 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0, ]; } $insert = []; // var_dump($ids); // var_dump($sale_ids);die; try{ DB::beginTransaction(); $key = $this->lock_key.'_'.$v['id']; $lock_status = $this->isLock($key); if(!$lock_status) return [false,'操作过于频繁']; $product_list = OrdersProduct::wherein('id', $ids)->get()->toArray(); $sale_product_list = SaleOrdersProduct::wherein('id', $sale_ids)->get()->toArray(); $box_insert = []; foreach ($product_list as $v) { $num_list = $key_list[$v['id']]; $total = $num_list['total']; $detail = $num_list['detail']; $team_id = $num_list['team_id']; $un_box_num = $v['dispatch_complete_quantity'] - $v['box_num']; if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足']; $ext_1 = $v['product_no'];//产品编号 $ext_2 = $v['technology_material']; //工艺材质 $ext_3 = $v['technology_name'];//工艺名称 $ext_4 = $v['wood_name'];//木皮 $ext_5 = $v['process_mark'];//工艺备注 $out_order_no = $insert['out_order_no'] = $v['out_order_no']; $top_id = $v['sale_orders_product_id']; foreach ($detail as $vv){ $box_insert[] = [ 'out_order_no' => $out_order_no, 'top_id' => $top_id, 'ext_1' => $ext_1, 'ext_2' => $ext_2, 'ext_3' => $ext_3, 'ext_4' => $ext_4, 'ext_5' => $ext_5, 'num' => $vv, 'box_type' => 1, 'team_id' => $team_id, 'shipment_order_no' => $transport_no, ]; } SaleOrdersProduct::where('id',$v['sale_orders_product_id'])->update([ 'box_num' => DB::raw('box_num + '.$total), ]); OrdersProduct::where('id',$v['id'])->update([ 'box_num' => DB::raw('box_num + '.$total), ]); } foreach ($sale_product_list as $v) { $box_type = 0; if($v['id'] < 0) { $v['id'] = -$v['id']; $box_type = 1; } $num_list = $key_list[$v['id']]; $total = $num_list['total']; $detail = $num_list['detail']; $team_id = $num_list['team_id']; $box_detail = new BoxDetail(['channel'=>$top_order_no]); $un_box_num = $v['order_quantity'] - $v['box_num'] - $box_detail->where('top_id',$v['id'])->where('del_time',0)->where('box_type',1)->sum('num'); if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足']; $ext_1 = $v['product_no'];//产品编号 $ext_2 = $v['technology_material']; //工艺材质 $ext_3 = $v['technology_name'];//工艺名称 $ext_4 = $v['wood_name'];//木皮 $ext_5 = $v['process_mark'];//工艺备注 $out_order_no = $insert['out_order_no'] = $v['out_order_no']; $top_id = $v['id']; foreach ($detail as $vv){ $box_insert[] = [ 'out_order_no' => $out_order_no, 'top_id' => $top_id, 'ext_1' => $ext_1, 'ext_2' => $ext_2, 'ext_3' => $ext_3, 'ext_4' => $ext_4, 'ext_5' => $ext_5, 'num' => $vv, 'box_type' => $box_type, 'team_id' => $team_id, 'shipment_order_no' => $transport_no, ]; } SaleOrdersProduct::where('id',$v['id'])->update([ 'box_num' => DB::raw('box_num + '.$total), ]); } $insert['detail'] = $box_insert; $insert['top_order_no'] = $top_order_no; $insert['order_no'] = $box_no; $insert['shipment_order_no'] = $transport_no; list($status,$msg) = self::$box_hook->boxInsert($insert); if(!$status) { $this->delLock($key); DB::rollBack(); return [false,$msg]; } $this->delLock($key); DB::commit(); return [true,'']; }catch (\Exception $e){ DB::rollBack(); return [false,$e->getLine().':'.$e->getMessage()]; } } /** * 根据发货单包装 * @param $data * @return array */ public function transportBoxIn($data) { // $param = [ // [ // 'order_no' => 'XD202309000155', // 'product_no' => '0301011816', // 'technology_material' => '多层板,单贴,新木纹钢板', // 'technology_name' => '', // 'wood_name' => '', // 'process_mark' => '', // 'num' => '5', // 'team_id' => '1', // ],[ // 'order_no' => 'XD202309000155', // 'product_no' => '0104010753', // 'technology_material' => '多层线条', // 'technology_name' => '', // 'wood_name' => '', // 'process_mark' => '', // 'num' => '4', // 'team_id' => '1', // ],[ // 'order_no' => 'XD202309000156', // 'product_no' => '0301011712', // 'technology_material' => '多层板,双贴,雨丝钢板', // 'technology_name' => '', // 'wood_name' => '', // 'process_mark' => '', // 'num' => '3', // ], // ]; // dd(json_encode($param)); // $params = [ // 'params' => $param, // 'transport_no' => '123443212', // ]; $param = $data['params']; $shipment_order_no = $data['transport_no']; $sale_order_nos = []; $key_list = []; foreach ($param as $v){ if(!in_array($v['order_no'],$sale_order_nos)) $sale_order_nos[] = $v['order_no']; $key = $this->getKey($v); $key_list[$key] = [ 'num' => $v['num'], 'order_no' => $v['order_no'], 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0, ]; } $sale_order_product_list = SaleOrdersProduct::wherein('out_order_no',$sale_order_nos)->get()->toArray(); $sale_order_product_key_list = []; foreach ($sale_order_product_list as $v){ $key = $this->getKey($v); $sale_order_product_key_list[$key] = $v['id']; } $param = []; // var_dump($key_list); // var_dump($sale_order_product_key_list);die; foreach ($key_list as $k=>$v){ if(!isset($sale_order_product_key_list[$k])) return [false,$k.' 不存在!']; $param[$v['order_no']][] = [ 'id' => -(intval($sale_order_product_key_list[$k])), 'team_id' => $v['team_id'], 'params' => [$v['num']] ]; } $box_no = self::$box_hook->setOrderNo(); foreach ($param as $k=>$v){ $top_order_no = SaleOrdersProduct::where('out_order_no',$k)->value('order_no'); $postParam = [ 'order_no' => $top_order_no, 'param' => $v, 'box_no' => $box_no, 'transport_no' => $shipment_order_no, ]; $this->boxIn($postParam); // var_dump($postParam);die; } return [true,'保存成功']; } protected function getKey($data){ return $data['product_no'].'_'.$data['technology_material'].'_'.$data['technology_name'].'_'.$data['wood_name'].'_'.$data['process_mark']; } /** * 包装详情1用于包装前 * @param $data * @return array */ public function boxDetail($data) { list($status, $data) = self::$box_hook->boxDetail($data); if (!$status) return [false, $data]; $sale_orders_product_ids = []; foreach ($data as $v){ $sale_orders_product_ids[] = $v['top_id']; } $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray(); $key_list = []; foreach ($list as $v){ $key_list[$v['id']] = $v; } foreach ($data as &$v){ $detail = $key_list[$v['top_id']]; $v['customer_no'] = $detail['customer_no']; $v['customer_name'] = $detail['customer_name']; } return [true, $data]; } public function boxProductList($data){ if(!isset($data['id'])) return [false,'id not found']; $sale_order_ids = $data['id']; $model = SaleOrdersProduct::where('del_time',0)->wherein('id',$sale_order_ids) ->select('id','out_order_no','order_no','customer_no','customer_name','product_no','product_title','product_size','crt_time as production_time','id as sale_orders_product_id','finished_num as dispatch_complete_quantity','box_num','technology_name','wood_name','crt_time','order_quantity','technology_material','technology_name','wood_name','process_mark') ->orderBy('id','desc')->get()->toArray(); $product_list = ordersProduct::wherein('sale_orders_product_id',$sale_order_ids)->select('id','out_order_no','customer_no','customer_name','product_no','sale_orders_product_id','product_title','product_size','dispatch_complete_quantity','box_num','technology_name','wood_name','crt_time',DB::raw('(production_quantity - scrapp_num) as production_quantity '),DB::raw('finished_num as dispatch_complete_quantity '),'technology_material','technology_name','wood_name','process_mark')->get()->toArray(); $model_key_list = []; foreach ($model as $v){ $model_key_list[$v['id']] = $v; } $product_key_num_list = []; foreach ($product_list as $v){ if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']]))$product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [ 'product_num' => 0 , 'box_num' => 0 , ]; $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity']; $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num']; $detail = $model_key_list[$v['sale_orders_product_id']]; $detail['box_type'] = 1; $detail['id'] = $v['id']; $detail['box_num'] = $v['box_num']; $detail['un_box_num'] = $v['dispatch_complete_quantity'] - $v['box_num']; $detail['type'] = 1; $model[] = $detail; } $return = []; $product_key_list = []; foreach ($model as $v){ if(!isset($v['box_type'])) { $product_num = 0; $box_num = 0; if(isset($product_key_num_list[$v['id']])){ foreach ($product_key_num_list[$v['id']] as $vv){ $product_num += $vv['product_num']; $box_num += $vv['box_num']; } } // $product_num = isset($product_key_num_list[$v['id']]) ? $product_key_num_list[$v['id']]['product_num'] : 0; // $box_num = isset($product_key_num_list[$v['id']]) ? $product_key_num_list[$v['id']]['box_num'] : 0; if(($v['order_quantity'] - $product_num) === 0) continue; // var_dump($v['order_quantity']); // var_dump($v['order_quantity']); $product_key_list[$v['sale_orders_product_id']] = [ 'out_order_no' => $v['out_order_no'], 'order_no' => $v['order_no'], 'production_time' => '未下生产', 'customer_no' => $v['customer_no'], 'customer_name' => $v['customer_name'], 'product_no' => $v['product_no'], 'product_title' => $v['product_title'], 'product_size' => $v['product_size'], 'id' => -$v['sale_orders_product_id'], 'technology_material' => $v['technology_material'], 'technology_name' => $v['technology_name'], 'wood_name' => $v['wood_name'], 'process_mark' => $v['process_mark'], 'type' => '2', 'is_box_num' => $v['box_num'] - $box_num, 'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num, 'sale_num' => $v['order_quantity'], ]; }else{ $return[] = [ 'id' => $v['id'], 'technology_material' => $v['technology_material'], 'technology_name' => $v['technology_name'], 'wood_name' => $v['wood_name'], 'order_no' => $v['order_no'], 'process_mark' => $v['process_mark'], 'out_order_no' => $v['out_order_no'], 'production_time' => date('Y-m-d',$v['crt_time']), 'customer_no' => $v['customer_no'], 'customer_name' => $v['customer_name'], 'product_no' => $v['product_no'], 'product_title' => $v['product_title'], 'product_size' => $v['product_size'], 'type' => '1', 'is_box_num' => $v['box_num'], 'un_box_num' => $v['un_box_num'], 'sale_num' => $v['order_quantity'], ]; } } foreach ($product_key_list as $v){ $return[] = $v; } // $data = [ // [ // 'id' => 1, // 'out_order_no' => '销售订单号', // 'production_time' => '下生产时间', // 'customer_no' => '客户编码', // 'customer_name' => '客户名称', // 'product_no' => '产品编码', // 'product_title' => '产品名称', // 'product_size' => '产品规格', // 'type' => '1生产包装2备用包装', // 'is_box_num' => '已包装数量', // 'un_box_num' => '未包装', // 'sale_num' => '销售数量', // ] // ]; return [true,$return]; } /** * 包装详情2用于包装后 * @param $data * @return array */ public function boxOrderDetail($data) { list($status, $data) = self::$box_hook->boxDetail($data); // var_dump($data);die; $sale_orders_product_ids = []; foreach ($data as $v){ $sale_orders_product_ids[] = $v['top_id']; } $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray(); $key_list = []; foreach ($list as $v){ $key_list[$v['id']] = $v; } foreach ($data as &$v){ $v['ext_3'] = $key_list[$v['top_id']]['product_no']; $v['wood_name'] = $key_list[$v['top_id']]['wood_name']; $v['technology_name'] = $key_list[$v['top_id']]['technology_name']; $v['process_mark'] = $key_list[$v['top_id']]['process_mark']; $v['technology_material'] = $key_list[$v['top_id']]['technology_material']; } if (!$status) return [false, $data]; return [true, $data]; } /** * 通过销售单号获取包装单 * @param $data * @return array */ public function boxOrderDetailByTop($data){ $list = new Box(); if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']); if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']); // if(isset($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']); // if(isset($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']); $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->groupBy('order_no')->get()->toArray(); return [true,$list]; } /** * 通过包装单获取包装详情 * @param $data * @return array */ public function boxOrderDetailByOrderNo($data){ $order_nos = $data['order_nos']; $order_nos = array_unique($order_nos); $list = Box::where('del_time',0)->wherein('order_no',$order_nos)->select('order_no','out_order_no','top_order_no','shipment_order_no as transport_no')->get()->toArray(); $detail_list = []; foreach ($list as $v){ $model = new BoxDetail(['channel'=>$v['top_order_no']]); $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray()); } $list = $detail_list; $ids = []; foreach ($list as $v){ $ids[] = $v['top_id']; } $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray(); $sale_orders_key_product = []; foreach ($sale_orders_product as $v){ $sale_orders_key_product[$v['id']] = $v; } $team_key_list = Team::pluck('title','id')->toArray(); foreach ($list as &$v){ $detail = $sale_orders_key_product[$v['top_id']]; // var_dump($detail);die; $v['customer_name'] = $detail['customer_name']; $v['table_body_mark'] = $detail['table_body_mark']; $v['ext_4'] = $detail['product_title']; $v['ext_5'] = $detail['product_size']; $v['customer_name'] = $detail['customer_name']; $v['technology_material'] = $detail['technology_material']; $v['transport_no'] = $v['shipment_order_no']; $v['wood_name'] = $detail['wood_name']; $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ; } return [true,$list]; } public function boxOrderGroup($data){ $list = new Box(); if(!isset($data['top_order_no'])&&!isset($data['transport_no'])) return [false,'销售单或发货单必选一个']; if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']); if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']); $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->get()->toArray(); return [true,$list]; } public function transportNo(){ $list = Box::where('del_time',0)->where('shipment_order_no','<>','')->groupBy('shipment_order_no')->pluck('shipment_order_no')->toArray(); return [true,$list]; } public function delBoxDetail($data){ $order_nos = $data['order_nos']; foreach ($order_nos as $v){ $list = self::$box_hook->delBox($v); foreach ($list as $vv){ SaleOrdersProduct::where('id',$vv['top_id'])->update([ 'box_num' => DB::raw('box_num - '.$vv['num']) ]); if($vv['box_type'] == 1){ // $ext_1 = $v['product_no'];//产品编号 // $ext_2 = $v['technology_material']; //工艺材质 // $ext_3 = $v['technology_name'];//工艺名称 // $ext_4 = $v['wood_name'];//木皮 // $ext_5 = $v['process_mark'];//工艺备注 OrdersProduct::where('product_no',$vv['ext_1'])->where('technology_material',$vv['ext_2'])->where('technology_name',$vv['ext_3'])->where('wood_name',$vv['ext_4'])->where('process_mark',$vv['ext_5'])->where('sale_orders_product_id',$vv['top_id']) ->update([ 'box_num' => DB::raw('box_num - '.$vv['num']) ]); } } } return [true,'删除成功']; } public function boxAdd($data) { list($status,$msg) = $this->boxAddRule($data); if(! $status) return [false,$msg]; try{ DB::beginTransaction(); $box = new Box(); $box->order_no = $msg['order_no']; $box->save(); DB::table('box_detail')->insert($msg['detail']); DB::commit(); return [true,'']; }catch (\Exception $e){ DB::rollBack(); return [false,$e->getLine().':'.$e->getMessage()]; } } public function boxAddRule($data) { if(empty($data['param'])) return [false,'请选择包装数据']; $order_no = (new BoxHookService())->setOrderNo(); $detail = []; foreach ($data['param'] as $value) { $key = $this->lock_key.'_'.$value['idlsid']; list($status,$msg) = $this->limitingSendRequestBackg($key); if(! $status) return [false,$msg]; if(empty($value['submit_quantity']) || ! is_numeric($value['submit_quantity']) || $value['submit_quantity'] > $value['quantity']) return [false,'包装数量错误']; if(empty($value['team_id'])) return [false,'班组必须选择']; $detail[] = [ 'order_no' => $order_no, 'out_order_no' => $value['csocode'], 'crt_time' => time(), 'upd_time' => time(), 'type' => 1, 'ext_1' => $value['cinvcode'], 'ext_2' => $value['technology_material'], 'ext_3' => $value['cfree1'] ?? "", 'ext_4' => $value['cfree2'] ?? "", 'ext_5' => $value['process_mark'], 'ext_6' => $value['customer_name'] ?? "", 'ext_7' => $value['cuscode'] ?? "", 'ext_8' => $value['product_title'], 'ext_9' => $value['product_size'], 'top_id' => $value['idlsid'], 'state' => 0, 'num' => $value['submit_quantity'], 'box_type' => 2, 'team_id' => $value['team_id'], 'shipment_order_no' => $value['cdlcode'], ]; } return [true,['order_no' => $order_no, 'detail' => $detail]]; } public function boxFhDetail($data){ if(empty($data['idlsid'])) return [false,'请选择数据']; $result = DB::table('box_detail')->where('del_time',0) ->where('top_id',$data['idlsid']) ->select('order_no','ext_6','ext_7','ext_1','ext_8','ext_9','num','ext_2','ext_3','ext_4','ext_5') ->get()->toArray(); if(! empty($result)){ foreach ($result as $key => $value){ $result[$key] = (array)$value; } } return [true,$result]; } public function boxFhDel($data){ if(empty($data['order_nos'])) return [false,'包装单号不能为空']; $box = Box::whereIn('order_no',$data['order_nos'])->update(['del_time' => time()]); DB::table('box_detail')->whereIn('order_no',$data['order_nos'])->where('del_time',0)->update([ 'del_time' => time() ]); return [true,'']; } public function transportDetail($data){ if(empty($data['order_nos'])) return [false,'包装单号不能为空']; $order_nos = $data['order_nos']; $list = BoxDetail::wherein('order_no',$order_nos)->select('ext_1','ext_2','ext_3','ext_4','ext_5','ext_6','ext_7','ext_8','ext_9','num','order_no','shipment_order_no')->get()->toArray(); foreach ($list as &$v){ $v['product_no'] = $v['ext_1']; $v['technology_material'] = $v['ext_2']; $v['technology_name'] = $v['ext_3']; $v['wood_name'] = $v['ext_4']; $v['process_mark'] = $v['ext_5']; $v['customer_no'] = $v['ext_6']; $v['customer_name'] = $v['ext_7']; $v['product_title'] = $v['ext_8']; $v['product_size'] = $v['ext_9']; } return [200,$list]; } public function boxFhBzDetail($data){ if(empty($data['order_no'])) return [false,'请选择包装单数据']; $result = DB::table('box_detail')->where('del_time',0) ->whereIn('order_no',$data['order_no']) ->select('order_no','ext_6','ext_7','ext_1','ext_8','ext_9','num','ext_2','ext_3','ext_4','ext_5') ->get()->toArray(); if(! empty($result)){ foreach ($result as $key => $value){ $result[$key] = (array)$value; } } return [true,$result]; } }