Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
53.85% |
7 / 13 |
CRAP | |
65.09% |
151 / 232 |
| WaybillsController | |
0.00% |
0 / 1 |
|
53.85% |
7 / 13 |
344.61 | |
65.09% |
151 / 232 |
| index | |
0.00% |
0 / 1 |
70.81 | |
25.81% |
8 / 31 |
|||
| create | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| store | |
0.00% |
0 / 1 |
5.04 | |
88.46% |
23 / 26 |
|||
| edit | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
| update | |
0.00% |
0 / 1 |
9 | |
95.35% |
41 / 43 |
|||
| waybillUpdate | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 9 |
|||
| waybillAudit | |
100.00% |
1 / 1 |
3 | |
100.00% |
16 / 16 |
|||
| waybillEdit | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| waybillRetreatAudit | |
100.00% |
1 / 1 |
2 | |
100.00% |
8 / 8 |
|||
| waybillEndAudit | |
100.00% |
1 / 1 |
3 | |
100.00% |
24 / 24 |
|||
| waybillExport | |
0.00% |
0 / 1 |
1056.00 | |
0.00% |
0 / 43 |
|||
| validatorWaybill | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| validatorWaybillDispatch | |
0.00% |
0 / 1 |
3.00 | |
92.31% |
12 / 13 |
|||
| <?php | |
| namespace App\Http\Controllers; | |
| use App\WaybillAuditLog; | |
| use App\WaybillPriceModel; | |
| use App\Carrier; | |
| use App\CarType; | |
| use App\City; | |
| use App\Exports\WaybillExport; | |
| use App\Owner; | |
| use App\Unit; | |
| use App\Waybill; | |
| use App\WaybillPayoff; | |
| use App\WaybillFinancialSnapshot; | |
| use Carbon\Carbon; | |
| use Illuminate\Http\Request; | |
| use Illuminate\Support\Facades\Auth; | |
| use Illuminate\Support\Facades\Gate; | |
| use Illuminate\Support\Facades\Validator; | |
| use Maatwebsite\Excel\Facades\Excel; | |
| use Ramsey\Uuid\Uuid; | |
| class WaybillsController extends Controller | |
| { | |
| public function index(Request $request) | |
| { | |
| if(!Gate::allows('运输管理-查询')){ return redirect(url('/')); } | |
| $data=$request->input(); | |
| if ($data != null ) { | |
| $today=Carbon::now()->subDays(15); | |
| $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) { | |
| return $query->with('user'); | |
| }]); | |
| if ($request->input('waybill_number')){ | |
| $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d')); | |
| } | |
| if ($request->input('carrier_bill')){ | |
| $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d')); | |
| } | |
| if ($request->input('carrier_id')){ | |
| $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id')); | |
| } | |
| if ($request->input('owner_id')){ | |
| $waybills=$waybills->where('owner_id','=',$request->input('owner_id')); | |
| } | |
| if ($request->input('wms_bill_number')){ | |
| $waybills=$waybills->where('wms_bill_number','like','$'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d')); | |
| } | |
| if ($request->input('created_at_start')){ | |
| $waybills=$waybills->where('created_at','>',$request->input('created_at_start')); | |
| } | |
| if ($request->input('created_at_end')){ | |
| $waybills=$waybills->where('created_at','<',$request->input('created_at_end')); | |
| } | |
| if ($request->input('type')){ | |
| $waybills=$waybills->where('type','=',$request->input('type')); | |
| } | |
| $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50); | |
| $carries = Carrier::get(); | |
| $owners = Owner::get(); | |
| return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data]); | |
| } else { | |
| $waybills = Waybill::with(['owner', 'waybillAuditLogs' => function ($query) { | |
| return $query->with('user'); | |
| }])->paginate(50); | |
| $carries = Carrier::get(); | |
| $owners = Owner::get(); | |
| return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data]); | |
| } | |
| } | |
| public function create() | |
| { | |
| if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); } | |
| $owners=Owner::get(); | |
| return view('waybill.create',['owners'=>$owners]); | |
| } | |
| public function store(Request $request) | |
| { | |
| if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); } | |
| $this->validatorWaybill($request)->validate(); | |
| $data=$request->input(); | |
| $waybill=new Waybill([ | |
| 'type'=>$data['type'], | |
| 'state'=>'未审核', | |
| 'waybill_number'=>Uuid::uuid1(), | |
| 'owner_id'=>$data['owner_id'], | |
| 'wms_bill_number'=>$data['wms_bill_number'], | |
| 'origination'=>$data['origination'], | |
| 'destination'=>$data['destination'], | |
| 'recipient'=>$data['recipient'], | |
| 'recipient_mobile'=>$data['recipient_mobile'], | |
| 'charge'=>$data['charge'], | |
| 'ordering_remark'=>$data['ordering_remark'] | |
| ]); | |
| $waybill->save(); | |
| $number_id=$waybill->id; | |
| if ($data['type']=='直发车'){ | |
| $waybill_number='BSZF'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT); | |
| $waybill->waybill_number=$waybill_number; | |
| $waybill->update(); | |
| }else{ | |
| $waybill_number='BSZX'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT); | |
| $waybill->waybill_number=$waybill_number; | |
| $waybill->update(); | |
| } | |
| $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); | |
| return redirect('waybill')->with('successTip','新运单“'.$waybill_number.'”录入成功'); | |
| } | |
| public function edit($id) | |
| { | |
| if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); } | |
| $waybill=Waybill::find($id); | |
| $carriers=Carrier::get(); | |
| $cities=City::get(); | |
| $units=Unit::get(); | |
| $carTypes=CarType::get(); | |
| return view('waybill/edit',['waybill'=>$waybill,'carriers'=>$carriers,'cities'=>$cities,'units'=>$units,'carTypes'=>$carTypes]); | |
| } | |
| public function update(Request $request, $id) | |
| { | |
| if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); } | |
| $this->validatorWaybillDispatch($request)->validate(); | |
| $data=$request->input(); | |
| $waybill=Waybill::find($id); | |
| if ($waybill->type=="专线"){ | |
| $carrier_weight=$request->input('carrier_weight'); | |
| $isWaybillPriceModel=WaybillPriceModel::whereRaw('carrier_id = ? and city_id = ? and unit_id = ? and range_min < ? and range_max >= ?',[ | |
| $request->input('carrier_id'),$request->input('destination_city_id'), | |
| $request->input('warehouse_weight_unit_id'),$carrier_weight, | |
| $carrier_weight | |
| ])->first(); | |
| if ($isWaybillPriceModel==null){ | |
| return redirect()->back()->with('notWaybillPriceModel','计费模型未定义')->withInput(); | |
| } | |
| } | |
| $waybill->fill($data); | |
| if ($waybill->save()){ | |
| if ($waybill->type=="直发车"){ | |
| $total_receivable=($waybill->charge); | |
| $total_expense=($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee); | |
| }else if ($waybill->type=="专线"){ | |
| if ($carrier_weight<$isWaybillPriceModel->initial_weight){ | |
| $fee=($isWaybillPriceModel->unit_price)*($isWaybillPriceModel->initial_weight); | |
| }else{ | |
| $fee=($isWaybillPriceModel->unit_price)*$carrier_weight; | |
| } | |
| $waybill->fee=$fee; | |
| $waybill->save(); | |
| $total_receivable=($waybill->charge); | |
| $total_expense=($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee); | |
| } | |
| $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first(); | |
| if ($waybillPayoff){ | |
| $waybillPayoff->waybill_id=$id; | |
| $waybillPayoff->total_expense=$total_expense; | |
| $waybillPayoff->total_receivable=$total_receivable; | |
| $waybillPayoff->gross_margin=$total_receivable-$total_expense; | |
| $waybillPayoff->gross_profit_rate=(($total_receivable-$total_expense)/$total_receivable); | |
| $waybillPayoff->save(); | |
| }else{ | |
| WaybillPayoff::create([ | |
| 'waybill_id'=>$id, | |
| 'total_expense'=>$total_expense, | |
| 'total_receivable'=>$total_receivable, | |
| 'gross_margin'=>$total_receivable-$total_expense, | |
| 'gross_profit_rate'=>(($total_receivable-$total_expense)/$total_receivable), | |
| ]); | |
| }; | |
| $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); | |
| return redirect('waybill')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功'); | |
| } | |
| } | |
| public function waybillUpdate(Request $request, $id){ | |
| if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); } | |
| $this->validatorWaybill($request)->validate(); | |
| $data=$request->input(); | |
| $waybill=Waybill::find($id); | |
| $waybill->fill($data); | |
| if ($waybill->save()){ | |
| $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']); | |
| return redirect('waybill')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功'); | |
| } | |
| } | |
| public function waybillAudit(Request $request){ | |
| if(!Gate::allows('运输管理-运单审核')){ return redirect(url('/')); } | |
| $id=$request->input('id'); | |
| $waybill=Waybill::find($id); | |
| $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first(); | |
| if (empty($isAudit)){ | |
| $waybillAuditLog=new WaybillAuditLog([ | |
| 'waybill_id'=>$id, | |
| 'audit_stage'=>'运单阶段', | |
| 'user_id'=>Auth::id(), | |
| ]); | |
| $waybillAuditLog->save(); | |
| $waybillAuditLog['user']=Auth::user(); | |
| $waybill->state='已审核'; | |
| $result=$waybill->save(); | |
| $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']); | |
| return ['success'=>$result,'state'=>$waybill->state,'waybillAuditLog'=>$waybillAuditLog]; | |
| } | |
| return ['exception'=>'请勿重复审核!']; | |
| } | |
| public function waybillEdit($id){ | |
| if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); } | |
| $waybill=Waybill::find($id); | |
| $owners=Owner::get(); | |
| return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]); | |
| } | |
| public function waybillRetreatAudit(Request $request){ | |
| if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); } | |
| $id=$request->input('id'); | |
| $waybill=Waybill::find($id); | |
| WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete(); | |
| $waybill->state='待重审'; | |
| $result=$waybill->save(); | |
| $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']); | |
| return ['success'=>$result,'state'=>$waybill->state]; | |
| } | |
| public function waybillEndAudit(Request $request){ | |
| if(!Gate::allows('运输管理-调度审核')){ return redirect(url('/')); } | |
| $id=$request->input('id'); | |
| $waybill=Waybill::find($id); | |
| $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first(); | |
| if (empty($isAudit)){ | |
| $waybillAuditLog=new WaybillAuditLog([ | |
| 'waybill_id'=>$id, | |
| 'audit_stage'=>'调度阶段', | |
| 'user_id'=>Auth::id(), | |
| ]); | |
| $waybillAuditLog->save(); | |
| $waybillAuditLog['user']=Auth::user(); | |
| $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first(); | |
| $waybillPayoff->load(["waybill"]); | |
| $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","warehouse_weight_unit","carrier_weight_unit","carType","waybillAuditLogs"]); | |
| $waybillPayoff->waybill->waybillAuditLogs->load(["user"]); | |
| $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE); | |
| WaybillFinancialSnapshot::create([ | |
| 'waybill_id'=>$id, | |
| 'json_content'=>$waybillPayoffJson, | |
| ]); | |
| $waybill->state='完结'; | |
| $result=$waybill->save(); | |
| $this->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::user()['id']); | |
| return ['success'=>$result,'state'=>$waybill->state,'waybillAuditLog'=>$waybillAuditLog]; | |
| } | |
| return ['exception'=>'请勿重复审核!']; | |
| } | |
| public function waybillExport($id){ | |
| $id = explode( ',',$id); | |
| $row=[[ | |
| 'type'=>'运单类型', | |
| 'waybill_number'=>'运单号', | |
| 'owner'=>'货主', | |
| 'wms_bill_number'=>'WMS单号', | |
| 'origination'=>'始发地', | |
| 'destination'=>'目的地', | |
| 'recipient'=>'收件人', | |
| 'recipient_mobile'=>'收件人电话', | |
| 'charge'=>'收费(元)', | |
| 'ordering_remark'=>'下单备注', | |
| 'carrier'=>'承运商', | |
| 'carrier_bill'=>'承运商单号', | |
| 'origination_city'=>'始发市', | |
| 'destination_city'=>'目的市', | |
| 'warehouse_weight'=>'仓库计重(抛)', | |
| 'warehouse_weight_unit'=>'仓库计重单位 ', | |
| 'carrier_weight'=>'承运商计重(抛)', | |
| 'carrier_weight_unit'=>'承运商计重单位', | |
| 'carType'=>'车型', | |
| 'fee'=>'运费(元)', | |
| 'pick_up_fee'=>'提货费(元)', | |
| 'other_fee'=>'其他费用(元)', | |
| 'collect_fee'=>'到付金额(元)', | |
| 'dispatch_remark'=>'调度备注', | |
| 'waybillAuditor'=>'运单审核人', | |
| 'dispatchAuditor'=>'调度审核人', | |
| 'created_at'=>'创建时间' | |
| ]]; | |
| $list=[]; | |
| for ($i=0; $i<count($id);$i++){ | |
| $waybill=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) { | |
| return $query->with('user'); | |
| }])->find($id[$i]); | |
| foreach ($waybill->waybillAuditLogs as $waybillAuditLog){ | |
| if ($waybillAuditLog->audit_stage=="运单阶段"){ | |
| $waybillAuditor=$waybillAuditLog->user->name; | |
| }else{ | |
| $waybillAuditor=''; | |
| } | |
| if ($waybillAuditLog->audit_stage=="调度阶段"){ | |
| $dispatchAuditor=$waybillAuditLog->user->name; | |
| }else{ | |
| $dispatchAuditor=''; | |
| } | |
| }; | |
| $w=[ | |
| 'type'=>isset($waybill->type)?$waybill->type:'', | |
| 'waybill_number'=>isset($waybill->waybill_number)?$waybill->waybill_number:'', | |
| 'owner'=>isset($waybill->owner->name)?$waybill->owner->name:'', | |
| 'wms_bill_number'=>isset($waybill->wms_bill_number)?$waybill->wms_bill_number:'', | |
| 'origination'=>isset($waybill->origination)?$waybill->origination:'', | |
| 'destination'=>isset($waybill->destination)?$waybill->destination:'', | |
| 'recipient'=>isset($waybill->recipient)?$waybill->recipient:'', | |
| 'recipient_mobile'=>isset($waybill->recipient_mobile)?$waybill->recipient_mobile:'', | |
| 'charge'=>isset($waybill->charge)?$waybill->charge:'', | |
| 'ordering_remark'=>isset($waybill->ordering_remark)?$waybill->ordering_remark:'', | |
| 'carrier'=>isset($waybill->carrier_name)?$waybill->carrier_name:'', | |
| 'carrier_bill'=>isset($waybill->carrier_bill)?$waybill->carrier_bill:'', | |
| 'origination_city'=>isset($waybill->origination_city_name)?$waybill->origination_city_name:'', | |
| 'destination_city'=>isset($waybill->destination_city_name)?$waybill->destination_city_name:'', | |
| 'warehouse_weight'=>isset($waybill->warehouse_weight)?$waybill->warehouse_weight:'', | |
| 'warehouse_weight_unit'=>isset($waybill->warehouse_weight_unit_name)?$waybill->warehouse_weight_unit_name:'', | |
| 'carrier_weight'=>isset($waybill->carrier_weight)?$waybill->carrier_weight:'', | |
| 'carrier_weight_unit'=>isset($waybill->carrier_weight_unit_name)?$waybill->carrier_weight_unit_name:'', | |
| 'carType'=>isset($waybill->carType->name)?$waybill->carType->name.($waybill->carType->length.'米'):'', | |
| 'fee'=>isset($waybill->fee)?$waybill->fee:'', | |
| 'pick_up_fee'=>isset($waybill->pick_up_fee)?$waybill->pick_up_fee:'', | |
| 'other_fee'=>isset($waybill->other_fee)?$waybill->other_fee:'', | |
| 'collect_fee'=>isset($waybill->collect_fee)?$waybill->collect_fee:'', | |
| 'dispatch_remark'=>isset($waybill->dispatch_remark)?$waybill->dispatch_remark:'', | |
| 'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'', | |
| 'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'', | |
| 'created_at'=>isset($waybill->created_at)?$waybill->created_at:'' | |
| ]; | |
| $list[$i]=$w; | |
| } | |
| $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']); | |
| return Excel::download(new WaybillExport($row,$list), date('Y:m:d ') . '运单列表.xls'); | |
| } | |
| protected function validatorWaybill(Request $request){ | |
| $validator=Validator::make($request->input(),[ | |
| 'wms_bill_number'=>'nullable|alpha_num|max:50', | |
| 'origination'=>'required|max:255', | |
| 'destination'=>'required|max:255', | |
| 'recipient'=>'required|max:50', | |
| 'recipient_mobile'=>'required|digits:11|integer', | |
| 'charge'=>'required|min:0|numeric', | |
| ],[ | |
| 'required'=>':attribute 为必填项', | |
| 'alpha_num'=>':attribute 应为字母或数字', | |
| 'max'=>':attribute 字符过多', | |
| 'digits'=>':attribute 应为11位', | |
| 'integer'=>':attribute 应为整数', | |
| 'min'=>':attribute 不得为负', | |
| 'numeric'=>':attribute 应为数字', | |
| ],[ | |
| 'wms_bill_number'=>'WMS单号', | |
| 'origination'=>'始发地', | |
| 'destination'=>'目的地', | |
| 'recipient'=>'收件人', | |
| 'recipient_mobile'=>'收件人电话', | |
| 'charge'=>'收费', | |
| ]); | |
| return $validator; | |
| } | |
| protected function validatorWaybillDispatch(Request $request){ | |
| if ($request->input('type')=='直发车'){ | |
| $validator=Validator::make($request->input(),[ | |
| 'carrier_bill'=>'required|alpha_num|max:50', | |
| 'fee'=>'required|min:0|numeric', | |
| 'other_fee'=>'nullable|min:0|numeric', | |
| 'collect_fee'=>'nullable|min:0|numeric', | |
| ],[ | |
| 'required'=>':attribute 为必填项', | |
| 'alpha_num'=>':attribute 应为字母或数字', | |
| 'max'=>':attribute 字符过多', | |
| 'min'=>':attribute 不得为负', | |
| 'numeric'=>':attribute 应为数字', | |
| ],[ | |
| 'carrier_bill'=>'承运商单号', | |
| 'fee'=>'运费', | |
| 'other_fee'=>'其他费用', | |
| 'collect_fee'=>'到付金额', | |
| ]); | |
| return $validator; | |
| }else if ($request->input('type')=='专线'){ | |
| $validator=Validator::make($request->input(),[ | |
| 'carrier_bill'=>'required|alpha_num|max:50', | |
| 'warehouse_weight'=>'required|min:0|numeric', | |
| 'carrier_weight'=>'required|min:0|numeric', | |
| 'pick_up_fee'=>'required|min:0|numeric', | |
| 'other_fee'=>'nullable|min:0|numeric', | |
| ],[ | |
| 'required'=>':attribute 为必填项', | |
| 'alpha_num'=>':attribute 应为字母或数字', | |
| 'max'=>':attribute 字符过多', | |
| 'min'=>':attribute 不得为负', | |
| 'numeric'=>':attribute 应为数字', | |
| ],[ | |
| 'carrier_bill'=>'承运商单号', | |
| 'warehouse_weight'=>'仓库计重(抛)', | |
| 'carrier_weight'=>'承运商计重(抛)', | |
| 'pick_up_fee'=>'提货费', | |
| 'other_fee'=>'其他费用', | |
| ]); | |
| return $validator; | |
| }else{ | |
| return false; | |
| } | |
| } | |
| } |