| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 53.85% covered (warning) | 53.85% | 7 / 13 | CRAP | 65.09% covered (warning) | 65.09% | 151 / 232
WaybillController | 0.00% covered (danger) | 0.00% | 0 / 1 | 53.85% covered (warning) | 53.85% | 7 / 13 | 344.61 | 65.09% covered (warning) | 65.09% | 151 / 232
index | 0.00% covered (danger) | 0.00% | 0 / 1 | 70.81 | 25.81% covered (danger) | 25.81% | 8 / 31
create | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 3 / 3
store | 0.00% covered (danger) | 0.00% | 0 / 1 | 5.04 | 88.46% covered (warning) | 88.46% | 23 / 26
edit | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 7 / 7
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 9 | 95.35% covered (success) | 95.35% | 41 / 43
waybillUpdate | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 9
waybillAudit | 100.00% covered (success) | 100.00% | 1 / 1 | 3 | 100.00% covered (success) | 100.00% | 16 / 16
waybillEdit | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 4 / 4
waybillRetreatAudit | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 8 / 8
waybillEndAudit | 100.00% covered (success) | 100.00% | 1 / 1 | 3 | 100.00% covered (success) | 100.00% | 24 / 24
waybillExport | 0.00% covered (danger) | 0.00% | 0 / 1 | 1056.00 | 0.00% covered (danger) | 0.00% | 0 / 43
validatorWaybill | 100.00% covered (success) | 100.00% | 1 / 1 | 1 | 100.00% covered (success) | 100.00% | 5 / 5
validatorWaybillDispatch | 0.00% covered (danger) | 0.00% | 0 / 1 | 3.00 | 92.31% covered (success) | 92.31% | 12 / 13

1 | <?php
2 |
3 | namespace App\Http\Controllers;
4 |
5 | use App\WaybillAuditLog;
6 | use App\WaybillPriceModel;
7 | use App\Carrier;
8 | use App\CarType;
9 | use App\City;
10 | use App\Exports\WaybillExport;
11 | use App\Owner;
12 | use App\Unit;
13 | use App\Waybill;
14 | use App\WaybillPayoff;
15 | use App\WaybillFinancialSnapshot;
16 | use Carbon\Carbon;
17 | use Illuminate\Http\Request;
18 | use Illuminate\Support\Facades\Auth;
19 | use Illuminate\Support\Facades\Gate;
20 | use Illuminate\Support\Facades\Validator;
21 | use Maatwebsite\Excel\Facades\Excel;
22 | use Ramsey\Uuid\Uuid;
23 |
24 | class WaybillController extends Controller
25 | {
26 |
27 | public function index(Request $request)
28 | {
29 | if(!Gate::allows('运输管理-运单-查询')){ return redirect(url('/')); }
30 | $data=$request->input();
31 | if ($data != null ) {
32 | $today=Carbon::now()->subDays(15);
33 | $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
34 | return $query->with('user');
35 | }]);
36 | if ($request->input('waybill_number')){
37 | $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
38 | }
39 | if ($request->input('carrier_bill')){
40 | $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d'));
41 | }
42 | if ($request->input('carrier_id')){
43 | $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
44 | }
45 | if ($request->input('owner_id')){
46 | $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
47 | }
48 | if ($request->input('wms_bill_number')){
49 | $waybills=$waybills->where('wms_bill_number','like','$'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
50 | }
51 | if ($request->input('created_at_start')){
52 | $waybills=$waybills->where('created_at','>',$request->input('created_at_start'));
53 | }
54 | if ($request->input('created_at_end')){
55 | $waybills=$waybills->where('created_at','<',$request->input('created_at_end'));
56 | }
57 | if ($request->input('type')){
58 | $waybills=$waybills->where('type','=',$request->input('type'));
59 | }
60 | $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50);
61 | $carries = Carrier::get();
62 | $owners = Owner::get();
63 | return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data]);
64 | } else {
65 | $waybills = Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
66 | return $query->with('user');
67 | }])->paginate(50);
68 | $carries = Carrier::get();
69 | $owners = Owner::get();
70 | return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data]);
71 |
72 | }
73 | }
74 | public function create()
75 | {
76 | if(!Gate::allows('运输管理-运单-录入')){ return redirect(url('/')); }
77 | $owners=Owner::get();
78 | return view('waybill.create',['owners'=>$owners]);
79 | }
80 |
81 | public function store(Request $request)
82 | {
83 | if(!Gate::allows('运输管理-运单-录入')){ return redirect(url('/')); }
84 | $this->validatorWaybill($request)->validate();
85 | $data=$request->input();
86 |
87 | $waybill=new Waybill([
88 | 'type'=>$data['type'],
89 | 'state'=>'未审核',
90 | 'waybill_number'=>Uuid::uuid1(),
91 | 'owner_id'=>$data['owner_id'],
92 | 'wms_bill_number'=>$data['wms_bill_number'],
93 | 'origination'=>$data['origination'],
94 | 'destination'=>$data['destination'],
95 | 'recipient'=>$data['recipient'],
96 | 'recipient_mobile'=>$data['recipient_mobile'],
97 | 'charge'=>$data['charge'],
98 | 'ordering_remark'=>$data['ordering_remark']
99 | ]);
100 | $waybill->save();
101 | $number_id=$waybill->id;
102 | if ($data['type']=='直发车'){
103 | $waybill_number='BSZF'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
104 | $waybill->waybill_number=$waybill_number;
105 | $waybill->update();
106 | }else{
107 | $waybill_number='BSZX'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
108 | $waybill->waybill_number=$waybill_number;
109 | $waybill->update();
110 | }
111 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
112 | return redirect('waybill')->with('successTip','新运单“'.$waybill_number.'”录入成功');
113 | }
114 |
115 | public function edit($id)
116 | {
117 | if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
118 | $waybill=Waybill::find($id);
119 | $carriers=Carrier::get();
120 | $cities=City::get();
121 | $units=Unit::get();
122 | $carTypes=CarType::get();
123 | return view('waybill/edit',['waybill'=>$waybill,'carriers'=>$carriers,'cities'=>$cities,'units'=>$units,'carTypes'=>$carTypes]);
124 | }
125 |
126 | public function update(Request $request, $id)
127 | {
128 | if(!Gate::allows('运输管理-运单-调度')){ return redirect(url('/')); }
129 | $this->validatorWaybillDispatch($request)->validate();
130 | $data=$request->input();
131 | $waybill=Waybill::find($id);
132 |
133 | if ($waybill->type=="专线"){
134 | $carrier_weight=$request->input('carrier_weight');
135 | $isWaybillPriceModel=WaybillPriceModel::whereRaw('carrier_id = ? and city_id = ? and unit_id = ? and range_min < ? and range_max >= ?',[
136 | $request->input('carrier_id'),$request->input('destination_city_id'),
137 | $request->input('warehouse_weight_unit_id'),$carrier_weight,
138 | $carrier_weight
139 | ])->first();
140 | if ($isWaybillPriceModel==null){
141 | return redirect()->back()->with('notWaybillPriceModel','计费模型未定义')->withInput();
142 | }
143 | }
144 | $waybill->fill($data);
145 | if ($waybill->save()){
146 | if ($waybill->type=="直发车"){
147 | $total_receivable=($waybill->charge);
148 | $total_expense=($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee);
149 | }else if ($waybill->type=="专线"){
150 | if ($carrier_weight<$isWaybillPriceModel->initial_weight){
151 | $fee=($isWaybillPriceModel->unit_price)*($isWaybillPriceModel->initial_weight);
152 | }else{
153 | $fee=($isWaybillPriceModel->unit_price)*$carrier_weight;
154 | }
155 | $waybill->fee=$fee;
156 | $waybill->save();
157 | $total_receivable=($waybill->charge);
158 | $total_expense=($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee);
159 | }
160 | $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
161 | if ($waybillPayoff){
162 | $waybillPayoff->waybill_id=$id;
163 | $waybillPayoff->total_expense=$total_expense;
164 | $waybillPayoff->total_receivable=$total_receivable;
165 | $waybillPayoff->gross_margin=$total_receivable-$total_expense;
166 | $waybillPayoff->gross_profit_rate=(($total_receivable-$total_expense)/$total_receivable);
167 | $waybillPayoff->save();
168 | }else{
169 | WaybillPayoff::create([
170 | 'waybill_id'=>$id,
171 | 'total_expense'=>$total_expense,
172 | 'total_receivable'=>$total_receivable,
173 | 'gross_margin'=>$total_receivable-$total_expense,
174 | 'gross_profit_rate'=>(($total_receivable-$total_expense)/$total_receivable),
175 | ]);
176 | };
177 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
178 | return redirect('waybill')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功');
179 | }
180 | }
181 |
182 | public function waybillUpdate(Request $request, $id){
183 | if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
184 | $this->validatorWaybill($request)->validate();
185 | $data=$request->input();
186 | $waybill=Waybill::find($id);
187 | $waybill->fill($data);
188 | if ($waybill->save()){
189 | $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
190 | return redirect('waybill')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功');
191 | }
192 | }
193 |
194 | public function waybillAudit(Request $request){
195 | if(!Gate::allows('运输管理-运单-运单审核')){ return redirect(url('/')); }
196 | $id=$request->input('id');
197 | $waybill=Waybill::find($id);
198 | $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first();
199 | if (empty($isAudit)){
200 | $waybillAuditLog=new WaybillAuditLog([
201 | 'waybill_id'=>$id,
202 | 'audit_stage'=>'运单阶段',
203 | 'user_id'=>Auth::id(),
204 | ]);
205 | $waybillAuditLog->save();
206 | $waybillAuditLog['user']=Auth::user();
207 | $waybill->state='已审核';
208 | $result=$waybill->save();
209 | $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
210 | return ['success'=>$result,'state'=>$waybill->state,'waybillAuditLog'=>$waybillAuditLog];
211 | }
212 | return ['exception'=>'请勿重复审核！'];
213 | }
214 |
215 | public function waybillEdit($id){
216 | if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
217 | $waybill=Waybill::find($id);
218 | $owners=Owner::get();
219 | return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]);
220 | }
221 | public function waybillRetreatAudit(Request $request){
222 | if(!Gate::allows('运输管理-运单-调度')){ return redirect(url('/')); }
223 | $id=$request->input('id');
224 | $waybill=Waybill::find($id);
225 | WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
226 | $waybill->state='待重审';
227 | $result=$waybill->save();
228 | $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
229 | return ['success'=>$result,'state'=>$waybill->state];
230 | }
231 | public function waybillEndAudit(Request $request){
232 | if(!Gate::allows('运输管理-运单-调度审核')){ return redirect(url('/')); }
233 | $id=$request->input('id');
234 | $waybill=Waybill::find($id);
235 | $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
236 | if (empty($isAudit)){
237 | $waybillAuditLog=new WaybillAuditLog([
238 | 'waybill_id'=>$id,
239 | 'audit_stage'=>'调度阶段',
240 | 'user_id'=>Auth::id(),
241 | ]);
242 | $waybillAuditLog->save();
243 | $waybillAuditLog['user']=Auth::user();
244 | $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
245 | $waybillPayoff->load(["waybill"]);
246 | $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","warehouse_weight_unit","carrier_weight_unit","carType","waybillAuditLogs"]);
247 | $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
248 | $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
249 | WaybillFinancialSnapshot::create([
250 | 'waybill_id'=>$id,
251 | 'json_content'=>$waybillPayoffJson,
252 | ]);
253 | $waybill->state='完结';
254 | $result=$waybill->save();
255 | $this->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::user()['id']);
256 | return ['success'=>$result,'state'=>$waybill->state,'waybillAuditLog'=>$waybillAuditLog];
257 | }
258 | return ['exception'=>'请勿重复审核！'];
259 | }
260 |
261 | public function waybillExport($id){
262 | $id = explode( ',',$id);
263 | $row=[[
264 | 'type'=>'运单类型',
265 | 'waybill_number'=>'运单号',
266 | 'owner'=>'货主',
267 | 'wms_bill_number'=>'WMS单号',
268 | 'origination'=>'始发地',
269 | 'destination'=>'目的地',
270 | 'recipient'=>'收件人',
271 | 'recipient_mobile'=>'收件人电话',
272 | 'charge'=>'收费(元)',
273 | 'ordering_remark'=>'下单备注',
274 | 'carrier'=>'承运商',
275 | 'carrier_bill'=>'承运商单号',
276 | 'origination_city'=>'始发市',
277 | 'destination_city'=>'目的市',
278 | 'warehouse_weight'=>'仓库计重(抛)',
279 | 'warehouse_weight_unit'=>'仓库计重单位 ',
280 | 'carrier_weight'=>'承运商计重(抛)',
281 | 'carrier_weight_unit'=>'承运商计重单位',
282 | 'carType'=>'车型',
283 | 'fee'=>'运费(元)',
284 | 'pick_up_fee'=>'提货费(元)',
285 | 'other_fee'=>'其他费用(元)',
286 | 'collect_fee'=>'到付金额(元)',
287 | 'dispatch_remark'=>'调度备注',
288 | 'waybillAuditor'=>'运单审核人',
289 | 'dispatchAuditor'=>'调度审核人',
290 | 'created_at'=>'创建时间'
291 | ]];
292 | $list=[];
293 | for ($i=0; $i<count($id);$i++){
294 | $waybill=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
295 | return $query->with('user');
296 | }])->find($id[$i]);
297 | foreach ($waybill->waybillAuditLogs as $waybillAuditLog){
298 | if ($waybillAuditLog->audit_stage=="运单阶段"){
299 | $waybillAuditor=$waybillAuditLog->user->name;
300 | }else{
301 | $waybillAuditor='';
302 | }
303 | if ($waybillAuditLog->audit_stage=="调度阶段"){
304 | $dispatchAuditor=$waybillAuditLog->user->name;
305 | }else{
306 | $dispatchAuditor='';
307 | }
308 | };
309 | $w=[
310 | 'type'=>isset($waybill->type)?$waybill->type:'',
311 | 'waybill_number'=>isset($waybill->waybill_number)?$waybill->waybill_number:'',
312 | 'owner'=>isset($waybill->owner->name)?$waybill->owner->name:'',
313 | 'wms_bill_number'=>isset($waybill->wms_bill_number)?$waybill->wms_bill_number:'',
314 | 'origination'=>isset($waybill->origination)?$waybill->origination:'',
315 | 'destination'=>isset($waybill->destination)?$waybill->destination:'',
316 | 'recipient'=>isset($waybill->recipient)?$waybill->recipient:'',
317 | 'recipient_mobile'=>isset($waybill->recipient_mobile)?$waybill->recipient_mobile:'',
318 | 'charge'=>isset($waybill->charge)?$waybill->charge:'',
319 | 'ordering_remark'=>isset($waybill->ordering_remark)?$waybill->ordering_remark:'',
320 | 'carrier'=>isset($waybill->carrier_name)?$waybill->carrier_name:'',
321 | 'carrier_bill'=>isset($waybill->carrier_bill)?$waybill->carrier_bill:'',
322 | 'origination_city'=>isset($waybill->origination_city_name)?$waybill->origination_city_name:'',
323 | 'destination_city'=>isset($waybill->destination_city_name)?$waybill->destination_city_name:'',
324 | 'warehouse_weight'=>isset($waybill->warehouse_weight)?$waybill->warehouse_weight:'',
325 | 'warehouse_weight_unit'=>isset($waybill->warehouse_weight_unit_name)?$waybill->warehouse_weight_unit_name:'',
326 | 'carrier_weight'=>isset($waybill->carrier_weight)?$waybill->carrier_weight:'',
327 | 'carrier_weight_unit'=>isset($waybill->carrier_weight_unit_name)?$waybill->carrier_weight_unit_name:'',
328 | 'carType'=>isset($waybill->carType->name)?$waybill->carType->name.($waybill->carType->length.'米'):'',
329 | 'fee'=>isset($waybill->fee)?$waybill->fee:'',
330 | 'pick_up_fee'=>isset($waybill->pick_up_fee)?$waybill->pick_up_fee:'',
331 | 'other_fee'=>isset($waybill->other_fee)?$waybill->other_fee:'',
332 | 'collect_fee'=>isset($waybill->collect_fee)?$waybill->collect_fee:'',
333 | 'dispatch_remark'=>isset($waybill->dispatch_remark)?$waybill->dispatch_remark:'',
334 | 'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
335 | 'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
336 | 'created_at'=>isset($waybill->created_at)?$waybill->created_at:''
337 | ];
338 | $list[$i]=$w;
339 | }
340 | $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
341 | return Excel::download(new WaybillExport($row,$list), date('Y:m:d ') . '运单列表.xls');
342 | }
343 |
344 | protected function validatorWaybill(Request $request){
345 | $validator=Validator::make($request->input(),[
346 | 'wms_bill_number'=>'nullable|alpha_num|max:50',
347 | 'origination'=>'required|max:255',
348 | 'destination'=>'required|max:255',
349 | 'recipient'=>'required|max:50',
350 | 'recipient_mobile'=>'required|digits:11|integer',
351 | 'charge'=>'required|min:0|numeric',
352 | ],[
353 | 'required'=>':attribute 为必填项',
354 | 'alpha_num'=>':attribute 应为字母或数字',
355 | 'max'=>':attribute 字符过多',
356 | 'digits'=>':attribute 应为11位',
357 | 'integer'=>':attribute 应为整数',
358 | 'min'=>':attribute 不得为负',
359 | 'numeric'=>':attribute 应为数字',
360 | ],[
361 | 'wms_bill_number'=>'WMS单号',
362 | 'origination'=>'始发地',
363 | 'destination'=>'目的地',
364 | 'recipient'=>'收件人',
365 | 'recipient_mobile'=>'收件人电话',
366 | 'charge'=>'收费',
367 | ]);
368 | return $validator;
369 | }
370 | protected function validatorWaybillDispatch(Request $request){
371 | if ($request->input('type')=='直发车'){
372 | $validator=Validator::make($request->input(),[
373 | 'carrier_bill'=>'required|alpha_num|max:50',
374 | 'fee'=>'required|min:0|numeric',
375 | 'other_fee'=>'nullable|min:0|numeric',
376 | 'collect_fee'=>'nullable|min:0|numeric',
377 | ],[
378 | 'required'=>':attribute 为必填项',
379 | 'alpha_num'=>':attribute 应为字母或数字',
380 | 'max'=>':attribute 字符过多',
381 | 'min'=>':attribute 不得为负',
382 | 'numeric'=>':attribute 应为数字',
383 | ],[
384 | 'carrier_bill'=>'承运商单号',
385 | 'fee'=>'运费',
386 | 'other_fee'=>'其他费用',
387 | 'collect_fee'=>'到付金额',
388 | ]);
389 | return $validator;
390 | }else if ($request->input('type')=='专线'){
391 | $validator=Validator::make($request->input(),[
392 | 'carrier_bill'=>'required|alpha_num|max:50',
393 | 'warehouse_weight'=>'required|min:0|numeric',
394 | 'carrier_weight'=>'required|min:0|numeric',
395 | 'pick_up_fee'=>'required|min:0|numeric',
396 | 'other_fee'=>'nullable|min:0|numeric',
397 | ],[
398 | 'required'=>':attribute 为必填项',
399 | 'alpha_num'=>':attribute 应为字母或数字',
400 | 'max'=>':attribute 字符过多',
401 | 'min'=>':attribute 不得为负',
402 | 'numeric'=>':attribute 应为数字',
403 | ],[
404 | 'carrier_bill'=>'承运商单号',
405 | 'warehouse_weight'=>'仓库计重(抛)',
406 | 'carrier_weight'=>'承运商计重(抛)',
407 | 'pick_up_fee'=>'提货费',
408 | 'other_fee'=>'其他费用',
409 | ]);
410 | return $validator;
411 | }else{
412 | return false;
413 | }
414 | }
415 | }