PackageController.php 11 KB

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