PackageController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. $batch_number=$request->input('batch_number');
  80. $paper_box_id=$request->input('paper_box_id');
  81. $package=Package::where('logistic_number',$logistic_number)->first();
  82. if (!$package && !$logistic_number)return redirect('package/create')->with('successError','录入失败!没有填写快递单号!');
  83. if ($package){
  84. $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController();
  85. $packageController=new \App\Http\Controllers\PackageController();
  86. if ($package->batch_rule&&strstr($package->batch_rule,'组合')){
  87. $packageController->syncBatch($package->batch_number,$weight,null,null,null,Carbon::now(),$paper_box_id);
  88. }else{
  89. $package->weight=$weight;
  90. $package->paper_box_id=$paper_box_id;
  91. $package->batch_number=$batch_number;
  92. $result=$accomplishToWMS->accomplishToWMS($package);
  93. if ($result['result']=='success'){
  94. if ($package->status=="记录异常")$package->status="已上传异常";
  95. else $package->status="已上传";
  96. }else{
  97. $package->status="上传异常";
  98. }
  99. }
  100. }else{
  101. $package=new Package([
  102. 'logistic_number'=>$logistic_number,
  103. 'weight'=>$weight,
  104. 'paper_box_id'=>$paper_box_id,
  105. 'batch_number'=>$batch_number
  106. ]);
  107. }
  108. $package->save();
  109. event(new WeighedEvent($package));
  110. return redirect('package/create')->with('successTip','新记录“'.$logistic_number.'”录入成功');
  111. }
  112. /**
  113. * Display the specified resource.
  114. *
  115. * @param \App\Package $packages
  116. * @return \Illuminate\Http\Response
  117. */
  118. public function show(Package $packages)
  119. {
  120. //
  121. }
  122. /**
  123. * Show the form for editing the specified resource.
  124. *
  125. * @param \App\Package $packages
  126. * @return \Illuminate\Http\Response
  127. */
  128. public function edit(Package $packages)
  129. {
  130. //
  131. }
  132. /**
  133. * Update the specified resource in storage.
  134. *
  135. * @param \Illuminate\Http\Request $request
  136. * @param \App\Package $packages
  137. * @return \Illuminate\Http\Response
  138. */
  139. public function update(Request $request, Package $packages)
  140. {
  141. //
  142. }
  143. /**
  144. * Remove the specified resource from storage.
  145. *
  146. * @param \App\Package $packages
  147. * @return \Illuminate\Http\Response
  148. */
  149. public function destroy(Package $packages)
  150. {
  151. //
  152. }
  153. public function export($id,Request $request){
  154. if(!Gate::allows('称重管理-查询')){ return '没有权限'; }
  155. if ($id==-1){
  156. $id=[];
  157. ini_set('max_execution_time',2500);
  158. ini_set('memory_limit','1526M');
  159. $packages=Package::select('id');
  160. $packages=$this->conditionQuery($request,$packages);
  161. $packages=$packages->get();
  162. foreach ($packages as $package){
  163. array_push($id,$package->id);
  164. }
  165. }else $id = explode( ',',$id);
  166. if (!$id)return ;
  167. $row=[[
  168. 'id'=>'ID',
  169. 'owner_name'=>'货主',
  170. 'logistic_number'=>'快递单号',
  171. 'delivery_number'=>'发货单号',
  172. 'batch_number'=>'波次号',
  173. 'batch_rule'=>'波次规则',
  174. 'created_at'=>'操作时间',
  175. 'recipient'=>'收件人',
  176. 'recipient_mobile'=>'收件人电话',
  177. 'logistic_name'=>'承运商',
  178. 'measuringMachine_name'=>'设备',
  179. 'weight'=>'重量(KG)',
  180. 'length'=>'长(CM)',
  181. 'width'=>'宽(CM)',
  182. 'height'=>'高(CM)',
  183. 'bulk'=>'体积(CM³)',
  184. 'paperBox_name'=>'纸箱',
  185. 'status'=>'状态',
  186. ]];
  187. $list=[];
  188. for ($i=0; $i<count($id);$i++){
  189. $package=Package::find($id[$i]);
  190. $w=[
  191. 'id'=>isset($package->id)?$package->id:'',
  192. 'owner_name'=>isset($package->owner_name)?$package->owner_name:'',
  193. 'logistic_number'=>isset($package->logistic_number)?$package->logistic_number:'',
  194. 'delivery_number'=>isset($package->delivery_number)?$package->delivery_number:'',
  195. 'batch_number'=>isset($package->batch_number)?$package->batch_number:'',
  196. 'batch_rule'=>isset($package->batch_rule)?$package->batch_rule:'',
  197. 'created_at'=>isset($package->created_at)?$package->created_at:'',
  198. 'recipient'=>isset($package->recipient)?$package->recipient:'',
  199. 'recipient_mobile'=>isset($package->recipient_mobile)?$package->recipient_mobile:'',
  200. 'logistic_name'=>isset($package->logistic_name)?$package->logistic_name:'',
  201. 'measuringMachine_name'=>isset($package->measuringMachine_name)?$package->measuringMachine_name:'',
  202. 'weight'=>isset($package->weight)?$package->weight:'',
  203. 'length'=>isset($package->length)?$package->length:'',
  204. 'width'=>isset($package->width)?$package->width:'',
  205. 'height'=>isset($package->height)?$package->height:'',
  206. 'bulk'=>isset($package->bulk)?$package->bulk:'',
  207. 'paperBox_name'=>isset($package->paperBox_name)?$package->paperBox_name:'',
  208. 'status'=>isset($package->status)?$package->status:''
  209. ];
  210. $list[$i]=$w;
  211. }
  212. return Excel::download(new WaybillExport($row,$list),date('YmdHis', time()).'-称重记录单.xls');
  213. }
  214. protected function validator(Request $request){
  215. $validator=Validator::make($request->input(),[
  216. 'logistic_number'=>['nullable','max:50'],
  217. 'weight'=>'required|min:0|max:999999|numeric',
  218. 'paper_box_id'=>'nullable|integer',
  219. 'batch_number'=>'required_without:logistic_number'
  220. ],[
  221. 'required'=>':attribute 为必填项',
  222. 'max'=>':attribute 字符过多或输入值过大',
  223. 'integer'=>':attribute 选择有误',
  224. 'min'=>':attribute 不得为负',
  225. 'numeric'=>':attribute 应为数字',
  226. 'unique'=>':attribute 已存在',
  227. 'required_without'=>':attribute 与快递单号至少填一项'
  228. ],[
  229. 'logistic_number'=>'快递单号',
  230. 'weight'=>'重量',
  231. 'paper_box_id'=>'纸箱',
  232. 'batch_number'=>'波次号',
  233. ]);
  234. return $validator;
  235. }
  236. public function syncBatch($batch_number,$weight,$max,$centre,$min,$date,$paperBox_id){
  237. $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController();
  238. $packagesBatch=Package::where('batch_number',$batch_number)->get();
  239. foreach ($packagesBatch as $packageBatch){
  240. $packageBatch->weight=$weight;
  241. if($max)$packageBatch->length=$max;
  242. if($centre)$packageBatch->width=$centre;
  243. if($min)$packageBatch->height=$min;
  244. $packageBatch->bulk=$max*$centre*$min;
  245. $packageBatch->weighed_at=$date;
  246. if ($paperBox_id)$packageBatch->paper_box_id=$paperBox_id;
  247. $packageBatch->status="未上传";
  248. $result=$accomplishToWMS->accomplishToWMS($packageBatch);
  249. if ($result['result']=='success'){
  250. if ($packageBatch->status=="记录异常")$packageBatch->status="已上传异常";
  251. else $packageBatch->status="已上传";
  252. }else{
  253. $packageBatch->status="上传异常";
  254. }
  255. $packageBatch->save();
  256. }
  257. }
  258. }