PackageController.php 11 KB

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