subDays(15); $packagesTem=clone $packages; $packagesTem=$packagesTem->where($column,'like','%'.$request->input($column).'%')->where('created_at','>',$today->format('Y-m-d')); if($packagesTem->count()==0 ||$packagesTem->first()[$column]==$request->input($column)){ $packages=$packages->where($column,$request->input($column)); }else{ $packages=$packagesTem; } return $packages; } public function conditionQuery(Request $request,$packages){ if ($request->input('logistic_number')){ $packages=$this->preciseQuery('logistic_number',$request,$packages); } if ($request->input('delivery_number')){ $packages=$this->preciseQuery('logistic_number',$request,$packages); } if ($request->input('created_at_start')){ $packages=$packages->where('created_at','>=',$request->input('created_at_start')); } if ($request->input('created_at_end')){ $packages=$packages->where('created_at','<=',$request->input('created_at_end')); } if ($request->input('owner_id')){ $packages=$packages->where('owner_id',$request->input('owner_id')); } if ($request->input('batch_number')){ $packages=$this->preciseQuery('logistic_number',$request,$packages); } return $packages; } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { if(!Gate::allows('称重管理-查询')){ return redirect(url('/')); } if ($request->input()){ $packages=Package::orderBy('id','DESC'); $packages=$this->conditionQuery($request,$packages); $packages=$packages->paginate($request->input('paginate')?$request->input('paginate'):50); $owners=Owner::select('id','name')->get(); return view('weight.package.index',['packages'=>$packages,'owners'=>$owners,'request'=>$request->input()]); } $packages=Package::orderBy('id','DESC')->paginate(50); $owners=Owner::select('id','name')->get(); return view('weight.package.index',['packages'=>$packages,'owners'=>$owners]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { if(!Gate::allows('称重管理-录入')){ return redirect(url('/')); } $paperBoxes=PaperBox::select('id','model')->get(); return view('weight.package.create',['paperBoxes'=>$paperBoxes]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { if(!Gate::allows('称重管理-录入')){ return redirect(url('/')); } $this->validator($request)->validate(); $logistic_number=$request->input('logistic_number'); $weight=$request->input('weight'); $batch_number=$request->input('batch_number'); $paper_box_id=$request->input('paper_box_id'); $package=null; if($batch_number)$package=Package::where('batch_number',$batch_number)->first(); if($logistic_number)$package=Package::where('logistic_number',$logistic_number)->first(); if (!$package && !$logistic_number)return redirect('package/create')->with('successError','录入失败!系统内没有对应波次的包裹!'); if ($package){ $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController(); if ($package->batch_rule&&strstr($package->batch_rule,'组合')){ $this->log(__METHOD__,'活动波次开始同步_'.__FUNCTION__,json_encode($package),Auth::user()['name']); $this->syncBatch($package->batch_number,$weight,null,null,null,Carbon::now(),$paper_box_id); }else{ $package->weight=$weight; $package->paper_box_id=$paper_box_id; $package->batch_number=$batch_number; $result=$accomplishToWMS->accomplishToWMS($package); if ($result['result']=='success'){ if ($package->status=="记录异常")$package->status="已上传异常"; else $package->status="已上传"; }else{ $package->status="上传异常"; } } }else{ $package=new Package([ 'logistic_number'=>$logistic_number, 'weight'=>$weight, 'paper_box_id'=>$paper_box_id, 'batch_number'=>$batch_number ]); } $package->save(); $this->log(__METHOD__,'create_'.__FUNCTION__,json_encode($package),Auth::user()['name']); event(new WeighedEvent($package)); return redirect('package/create')->with('successTip','操作成功'); } public function statistics(Request $request){ $packages=Package::select(DB::raw('owner_id,logistic_id,COUNT(logistic_id) AS count'))->whereNotNull('owner_id'); if($request->input('owner_id')){ $owners_id=$id = explode( ',',$request->input('owner_id')); $packages=$packages->whereIn('owner_id',$owners_id); } if($request->input('logistic_id')){ $logistics_id=$id = explode( ',',$request->input('logistic_id')); $packages=$packages->whereIn('logistic_id',$logistics_id); } if($request->input('date_start')){ $packages=$packages->where('created_at','>=',$request->input('date_start')); } if($request->input('date_end')){ $packages=$packages->where('created_at','<=',$request->input('date_end')); } $packages=$packages->groupBy(['owner_id','logistic_id'])->get(); $owners=Owner::get(); $logistics=Logistic::get(); if ($request->input('checkSign')){ $logistics=$this->checkLogistic($packages); if ($request->input('checkSign')=="-1"){ $excel=$this->statisticExport($packages,$owners,$logistics); return $excel; } $id=$id = explode( ',',$request->input('checkSign')); $packages=Package::select(DB::raw('owner_id,logistic_id,COUNT(logistic_id) AS count')) ->whereIn('owner_id',$id)->groupBy(['owner_id','logistic_id'])->get(); $logistics=$this->checkLogistic($packages); $excel=$this->statisticExport($packages,$owners,$logistics); return $excel; } return view('weight.package.statistics',["packages"=>$packages,'owners'=>$owners,'logistics'=>$logistics]); } /* * 根据packages统计记录删选物流公司 */ public function checkLogistic($packages){ $logisticIds=[]; foreach ($packages as $package){ $isLogistic=true; for ($i=0;$ilogistic_id==$logisticIds[$i]){ $isLogistic=false; break; } } if ($isLogistic){ array_push($logisticIds,$package->logistic_id); } } $logistics=Logistic::whereIn('id',$logisticIds)->get(); return $logistics; } /** * Display the specified resource. * * @param \App\Package $packages * @return \Illuminate\Http\Response */ public function show(Package $packages) { // } /** * Show the form for editing the specified resource. * * @param \App\Package $packages * @return \Illuminate\Http\Response */ public function edit(Package $packages) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Package $packages * @return \Illuminate\Http\Response */ public function update(Request $request, Package $packages) { // } /** * Remove the specified resource from storage. * * @param \App\Package $packages * @return \Illuminate\Http\Response */ public function destroy(Package $packages) { // } public function export($id,Request $request){ if(!Gate::allows('称重管理-查询')){ return '没有权限'; } ini_set('max_execution_time',3500); ini_set('memory_limit','3526M'); if ($id==-1){ $id=[]; $packages=Package::select('id'); $packages=$this->conditionQuery($request,$packages); $packages=$packages->get(); foreach ($packages as $package){ array_push($id,$package->id); } }else $id = explode( ',',$id); if (!$id)return ; $row=[[ 'id'=>'ID', 'owner_name'=>'货主', 'logistic_number'=>'快递单号', 'delivery_number'=>'发货单号', 'batch_number'=>'波次号', 'batch_rule'=>'波次规则', 'created_at'=>'操作时间', 'recipient'=>'收件人', 'recipient_mobile'=>'收件人电话', 'logistic_name'=>'承运商', 'measuringMachine_name'=>'设备', 'weight'=>'重量(KG)', 'length'=>'长(CM)', 'width'=>'宽(CM)', 'height'=>'高(CM)', 'bulk'=>'体积(CM³)', 'paperBox_name'=>'纸箱', 'status'=>'状态', ]]; $list=[]; for ($i=0; $iisset($package->id)?$package->id:'', 'owner_name'=>isset($package->owner_name)?$package->owner_name:'', 'logistic_number'=>isset($package->logistic_number)?$package->logistic_number:'', 'delivery_number'=>isset($package->delivery_number)?$package->delivery_number:'', 'batch_number'=>isset($package->batch_number)?$package->batch_number:'', 'batch_rule'=>isset($package->batch_rule)?$package->batch_rule:'', 'created_at'=>isset($package->created_at)?$package->created_at:'', 'recipient'=>isset($package->recipient)?$package->recipient:'', 'recipient_mobile'=>isset($package->recipient_mobile)?$package->recipient_mobile:'', 'logistic_name'=>isset($package->logistic_name)?$package->logistic_name:'', 'measuringMachine_name'=>isset($package->measuringMachine_name)?$package->measuringMachine_name:'', 'weight'=>isset($package->weight)?$package->weight:'', 'length'=>isset($package->length)?$package->length:'', 'width'=>isset($package->width)?$package->width:'', 'height'=>isset($package->height)?$package->height:'', 'bulk'=>isset($package->bulk)?$package->bulk:'', 'paperBox_name'=>isset($package->paperBox_name)?$package->paperBox_name:'', 'status'=>isset($package->status)?$package->status:'' ]; $list[$i]=$w; } return Excel::download(new WaybillExport($row,$list),date('YmdHis', time()).'-称重记录单.xlsx'); } protected function validator(Request $request){ $validator=Validator::make($request->input(),[ 'logistic_number'=>['nullable','max:50'], 'weight'=>'required|min:0|max:999999|numeric', 'paper_box_id'=>'nullable|integer', 'batch_number'=>'required_without:logistic_number' ],[ 'required'=>':attribute 为必填项', 'max'=>':attribute 字符过多或输入值过大', 'integer'=>':attribute 选择有误', 'min'=>':attribute 不得为负', 'numeric'=>':attribute 应为数字', 'unique'=>':attribute 已存在', 'required_without'=>':attribute 与快递单号至少填一项' ],[ 'logistic_number'=>'快递单号', 'weight'=>'重量', 'paper_box_id'=>'纸箱', 'batch_number'=>'波次号', ]); return $validator; } public function syncBatch($batch_number,$weight,$max,$centre,$min,$date,$paperBox_id){ $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController(); $packagesBatch=Package::where('batch_number',$batch_number)->get(); foreach ($packagesBatch as $packageBatch){ $packageBatch->weight=$weight; if($max)$packageBatch->length=$max; if($centre)$packageBatch->width=$centre; if($min)$packageBatch->height=$min; $packageBatch->bulk=$max*$centre*$min; $packageBatch->weighed_at=$date; if ($paperBox_id)$packageBatch->paper_box_id=$paperBox_id; $packageBatch->status="未上传"; $result=$accomplishToWMS->accomplishToWMS($packageBatch); if ($result['result']=='success'){ if ($packageBatch->status=="记录异常")$packageBatch->status="已上传异常"; else $packageBatch->status="已上传"; }else{ $packageBatch->status="上传异常"; } $packageBatch->save(); $this->log(__METHOD__,'SUCCESS_'.__FUNCTION__,json_encode($packageBatch)); } } public function statisticExport($packages,$owners,$logistics){ if (!$packages||!$owners||!$logistics) return; $row=[[]]; $row[0]['owner']='货主'; $row[0]['sum']='总计'; foreach ($logistics as $logistic){ $row[0][$logistic->id]=$logistic->name; } $ownerArr=[]; foreach ($owners as $owner){ foreach ($packages as $package){ if ($owner->id==$package->owner_id){ $ownerArr[$owner->name][$package->logistic_id]=$package->count; } } } $list=[]; $i=0; foreach ($owners as $owner){ if (isset($ownerArr[$owner->name])){ $w['owner']=$owner->name; $sum=0; foreach ($logistics as $logistic){ if (isset($ownerArr[$owner->name][$logistic->id])){ $w[$logistic->id]=$ownerArr[$owner->name][$logistic->id]; $sum=$sum+$ownerArr[$owner->name][$logistic->id]; } if (!isset($ownerArr[$owner->name][$logistic->id])){ $w[$logistic->id]=0; } } $w['sum']=$sum; $list[$i]=$w; $i++; } } return Excel::download(new WaybillExport($row,$list),date('YmdHis', time()).'-称重统计记录单.xlsx'); } }