PackageController.php 7.7 KB

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