PackageController.php 7.5 KB

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