| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace App\Http\Controllers;
- use App\Exports\WaybillExport;
- use App\Owner;
- use App\Package;
- use App\PaperBox;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Gate;
- use Illuminate\Support\Facades\Validator;
- use Maatwebsite\Excel\Facades\Excel;
- class PackageController extends Controller
- {
- /**
- * 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');
- $today=Carbon::now()->subDays(15);
- if ($request->input('logistic_number')){
- $packages=$packages->where('logistic_number','like','%'.$request->input('logistic_number').'%')->where('created_at','>',$today->format('Y-m-d'));
- }
- if ($request->input('delivery_number')){
- $packages=$packages->where('delivery_number','like','%'.$request->input('delivery_number').'%')->where('created_at','>',$today->format('Y-m-d'));
- }
- 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'));
- }
- $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]);
- }
- $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');
- $paper_box_id=$request->input('paper_box_id');
- $package=new Package([
- 'logistic_number'=>$logistic_number,
- 'weight'=>$weight,
- 'paper_box_id'=>$paper_box_id
- ]);
- $package->save();
- return redirect('weight/package/create')->with('successTip','新记录“'.$logistic_number.'”录入成功');
- }
- /**
- * 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){
- $id = explode( ',',$id);
- $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; $i<count($id);$i++){
- $package=Package::find($id[$i]);
- $w=[
- 'id'=>isset($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()).'-称重记录单.xls');
- }
- protected function validator(Request $request){
- $validator=Validator::make($request->input(),[
- 'logistic_number'=>['required','max:50','unique:packages,logistic_number'],
- 'weight'=>'required|min:0|max:999999|numeric',
- 'paper_box_id'=>'required|integer',
- ],[
- 'required'=>':attribute 为必填项',
- 'max'=>':attribute 字符过多或输入值过大',
- 'integer'=>':attribute 选择有误',
- 'min'=>':attribute 不得为负',
- 'numeric'=>':attribute 应为数字',
- 'unique'=>':attribute 已存在',
- ],[
- 'logistic_number'=>'快递单号',
- 'weight'=>'重量',
- 'paper_box_id'=>'纸箱',
- ]);
- return $validator;
- }
- }
|