Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
85.71% covered (warning)
85.71%
6 / 7
CRAP
97.83% covered (success)
97.83%
45 / 46
WaybillPriceModelsController
0.00% covered (danger)
0.00%
0 / 1
85.71% covered (warning)
85.71%
6 / 7
14
97.83% covered (success)
97.83%
45 / 46
 index
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 create
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 store
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 edit
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 update
0.00% covered (danger)
0.00%
0 / 1
3.02
87.50% covered (warning)
87.50%
7 / 8
 destroy
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 validateWaybillPriceModel
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
13 / 13
<?php
namespace App\Http\Controllers;
use App\WaybillPriceModel;
use App\Carrier;
use App\Province;
use App\Unit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
class WaybillPriceModelsController extends Controller
{
    public function index()
    {
        if(!Gate::allows('计费模型-查询')){ return redirect(url('/'));  }
        $waybillPriceModels= WaybillPriceModel::paginate(50);
        return view('waybill.waybillPriceModel.index',['waybillPriceModels'=>$waybillPriceModels]);
    }
    public function create()
    {
        if(!Gate::allows('计费模型-录入')){ return redirect(url('/'));  }
        $carriers=Carrier::get();
        $provinces=Province::with('cities')->get();
        $units=Unit::get();
        return view('waybill.waybillPriceModel.create',['carriers'=>$carriers,'provinces'=>$provinces,'units'=>$units]);
    }
    public function store(Request $request)
    {
        if(!Gate::allows('计费模型-录入')){ return redirect(url('/'));  }
        $this->validateWaybillPriceModel($request)->validate();
        $waybillPriceModel=$request->input('WaybillPriceModel');
        WaybillPriceModel::create($waybillPriceModel);
        $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
        return redirect('waybillPriceModel')->with('successTip','新计费模型录入成功');
    }
    public function edit($id)
    {
        if(!Gate::allows('计费模型-编辑')){ return redirect(url('/'));  }
        $waybillPriceModel=WaybillPriceModel::find($id);
        $carriers=Carrier::get();
        $provinces=Province::with('cities')->get();
        $units=Unit::get();
        return view('waybill.waybillPriceModel.edit',['waybillPriceModel'=>$waybillPriceModel,'carriers'=>$carriers,'provinces'=>$provinces,'units'=>$units]);
    }
    public function update(Request $request, $id)
    {
        if(!Gate::allows('计费模型-编辑')){ return redirect(url('/'));  }
        $this->validateWaybillPriceModel($request)->validate();
        $waybillPriceModel=WaybillPriceModel::find($id);
        $waybillPriceModel->fill($request->input('WaybillPriceModel'));
        if ($waybillPriceModel->save()){
            $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
            return redirect('waybillPriceModel')->with('successTip','新计费模型修改成功');
        }
    }
    public function destroy($id)
    {
        if(!Gate::allows('计费模型-删除')){ return redirect(url('/'));  }
        $waybillPriceModel=WaybillPriceModel::find($id);
        $this->log(__METHOD__,__FUNCTION__,json_encode($waybillPriceModel),Auth::user()['id']);
        $result=$waybillPriceModel->delete();
        return ['success'=>$result];
    }
    protected function validateWaybillPriceModel(Request $request){
        $min = $request->input('WaybillPriceModel.range_min');
        $validator= Validator::make($request->input(),[
            'WaybillPriceModel.province_id'=>'required|integer',
            'WaybillPriceModel.carrier_id'=>'required|integer',
            'WaybillPriceModel.unit_id'=>'required|integer',
            'WaybillPriceModel.city_id'=>'required|Integer',
            'WaybillPriceModel.range_min'=> 'required|min:0|numeric',
            'WaybillPriceModel.range_max'=> "required|min:{$min}|numeric",
            'WaybillPriceModel.unit_price'=>'required|min:0|numeric',
            'WaybillPriceModel.initial_weight'=>'required|min:0|numeric',
        ],[
            'required'=>':attribute 为必填项',
            'min'     =>':attribute 数值过小',
            'max'      => ':attribute 数值过大',
            'numeric' =>':attribute 应为数字',
            'integer'=> ':attribute 选择错误',
        ],[
            'WaybillPriceModel.province_id'=>'省份',
            'WaybillPriceModel.carrier_id'=>'承运商',
            'WaybillPriceModel.unit_id'=>'计重单位',
            'WaybillPriceModel.city_id'=>'城市',
            'WaybillPriceModel.range_min'=>'价格区间最小值',
            'WaybillPriceModel.range_max'=>'价格区间最大值',
            'WaybillPriceModel.unit_price'=>'单价',
            'WaybillPriceModel.initial_weight'=>'始重',
        ]);
        return $validator;
    }
}