PackageController.php 8.3 KB

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