PackageController.php 7.5 KB

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