PackageController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\WaybillExport;
  4. use App\Owner;
  5. use App\Package;
  6. use App\PaperBox;
  7. use Carbon\Carbon;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Gate;
  10. use Illuminate\Support\Facades\Validator;
  11. use Maatwebsite\Excel\Facades\Excel;
  12. class PackageController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @return \Illuminate\Http\Response
  18. */
  19. public function index(Request $request)
  20. {
  21. if(!Gate::allows('包裹信息-查询')){ return redirect(url('/')); }
  22. if ($request->input()){
  23. $packages=Package::orderBy('id','DESC');
  24. $today=Carbon::now()->subDays(15);
  25. if ($request->input('logistic_number')){
  26. $packages=$packages->where('logistic_number','like','%'.$request->input('logistic_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  27. }
  28. if ($request->input('delivery_number')){
  29. $packages=$packages->where('delivery_number','like','%'.$request->input('delivery_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  30. }
  31. if ($request->input('created_at_start')){
  32. $packages=$packages->where('created_at','>=',$request->input('created_at_start'));
  33. }
  34. if ($request->input('created_at_end')){
  35. $packages=$packages->where('created_at','<=',$request->input('created_at_end'));
  36. }
  37. if ($request->input('owner_id')){
  38. $packages=$packages->where('owner_id',$request->input('owner_id'));
  39. }
  40. $packages=$packages->paginate($request->input('paginate')?$request->input('paginate'):50);
  41. $owners=Owner::select('id','name')->get();
  42. return view('weight.package.index',['packages'=>$packages,'owners'=>$owners]);
  43. }
  44. $packages=Package::orderBy('id','DESC')->paginate(50);
  45. $owners=Owner::select('id','name')->get();
  46. return view('weight.package.index',['packages'=>$packages,'owners'=>$owners]);
  47. }
  48. /**
  49. * Show the form for creating a new resource.
  50. *
  51. * @return \Illuminate\Http\Response
  52. */
  53. public function create()
  54. {
  55. if(!Gate::allows('包裹信息-录入')){ return redirect(url('/')); }
  56. $paperBoxes=PaperBox::select('id','model')->get();
  57. return view('weight.package.create',['paperBoxes'=>$paperBoxes]);
  58. }
  59. /**
  60. * Store a newly created resource in storage.
  61. *
  62. * @param \Illuminate\Http\Request $request
  63. * @return \Illuminate\Http\Response
  64. */
  65. public function store(Request $request)
  66. {
  67. if(!Gate::allows('包裹信息-录入')){ return redirect(url('/')); }
  68. $this->validator($request)->validate();
  69. $logistic_number=$request->input('logistic_number');
  70. $weight=$request->input('weight');
  71. $paper_box_id=$request->input('paper_box_id');
  72. $package=new Package([
  73. 'logistic_number'=>$logistic_number,
  74. 'weight'=>$weight,
  75. 'paper_box_id'=>$paper_box_id
  76. ]);
  77. $package->save();
  78. return redirect('weight/package/create')->with('successTip','新记录“'.$logistic_number.'”录入成功');
  79. }
  80. /**
  81. * Display the specified resource.
  82. *
  83. * @param \App\Package $packages
  84. * @return \Illuminate\Http\Response
  85. */
  86. public function show(Package $packages)
  87. {
  88. //
  89. }
  90. /**
  91. * Show the form for editing the specified resource.
  92. *
  93. * @param \App\Package $packages
  94. * @return \Illuminate\Http\Response
  95. */
  96. public function edit(Package $packages)
  97. {
  98. //
  99. }
  100. /**
  101. * Update the specified resource in storage.
  102. *
  103. * @param \Illuminate\Http\Request $request
  104. * @param \App\Package $packages
  105. * @return \Illuminate\Http\Response
  106. */
  107. public function update(Request $request, Package $packages)
  108. {
  109. //
  110. }
  111. /**
  112. * Remove the specified resource from storage.
  113. *
  114. * @param \App\Package $packages
  115. * @return \Illuminate\Http\Response
  116. */
  117. public function destroy(Package $packages)
  118. {
  119. //
  120. }
  121. public function export($id){
  122. $id = explode( ',',$id);
  123. $row=[[
  124. 'id'=>'ID',
  125. 'owner_name'=>'货主',
  126. 'logistic_number'=>'快递单号',
  127. 'delivery_number'=>'发货单号',
  128. 'batch_number'=>'波次号',
  129. 'batch_rule'=>'波次规则',
  130. 'created_at'=>'操作时间',
  131. 'recipient'=>'收件人',
  132. 'recipient_mobile'=>'收件人电话',
  133. 'logistic_name'=>'承运商',
  134. 'measuringMachine_name'=>'设备',
  135. 'weight'=>'重量(KG)',
  136. 'length'=>'长(CM)',
  137. 'width'=>'宽(CM)',
  138. 'height'=>'高(CM)',
  139. 'bulk'=>'体积(CM³)',
  140. 'paperBox_name'=>'纸箱',
  141. 'status'=>'状态',
  142. ]];
  143. $list=[];
  144. for ($i=0; $i<count($id);$i++){
  145. $package=Package::find($id[$i]);
  146. $w=[
  147. 'id'=>isset($package->id)?$package->id:'',
  148. 'owner_name'=>isset($package->owner_name)?$package->owner_name:'',
  149. 'logistic_number'=>isset($package->logistic_number)?$package->logistic_number:'',
  150. 'delivery_number'=>isset($package->delivery_number)?$package->delivery_number:'',
  151. 'batch_number'=>isset($package->batch_number)?$package->batch_number:'',
  152. 'batch_rule'=>isset($package->batch_rule)?$package->batch_rule:'',
  153. 'created_at'=>isset($package->created_at)?$package->created_at:'',
  154. 'recipient'=>isset($package->recipient)?$package->recipient:'',
  155. 'recipient_mobile'=>isset($package->recipient_mobile)?$package->recipient_mobile:'',
  156. 'logistic_name'=>isset($package->logistic_name)?$package->logistic_name:'',
  157. 'measuringMachine_name'=>isset($package->measuringMachine_name)?$package->measuringMachine_name:'',
  158. 'weight'=>isset($package->weight)?$package->weight:'',
  159. 'length'=>isset($package->length)?$package->length:'',
  160. 'width'=>isset($package->width)?$package->width:'',
  161. 'height'=>isset($package->height)?$package->height:'',
  162. 'bulk'=>isset($package->bulk)?$package->bulk:'',
  163. 'paperBox_name'=>isset($package->paperBox_name)?$package->paperBox_name:'',
  164. 'status'=>isset($package->status)?$package->status:''
  165. ];
  166. $list[$i]=$w;
  167. }
  168. return Excel::download(new WaybillExport($row,$list),date('YmdHis', time()).'-称重记录单.xls');
  169. }
  170. protected function validator(Request $request){
  171. $validator=Validator::make($request->input(),[
  172. 'logistic_number'=>['required','max:50','unique:packages,logistic_number'],
  173. 'weight'=>'required|min:0|max:999999|numeric',
  174. 'paper_box_id'=>'required|integer',
  175. ],[
  176. 'required'=>':attribute 为必填项',
  177. 'max'=>':attribute 字符过多或输入值过大',
  178. 'integer'=>':attribute 选择有误',
  179. 'min'=>':attribute 不得为负',
  180. 'numeric'=>':attribute 应为数字',
  181. 'unique'=>':attribute 已存在',
  182. ],[
  183. 'logistic_number'=>'快递单号',
  184. 'weight'=>'重量',
  185. 'paper_box_id'=>'纸箱',
  186. ]);
  187. return $validator;
  188. }
  189. }