PackageController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Events\WeighedEvent;
  4. use App\Exports\WaybillExport;
  5. use App\Logistic;
  6. use App\Owner;
  7. use App\Package;
  8. use App\PaperBox;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Gate;
  13. use Illuminate\Support\Facades\Validator;
  14. use Maatwebsite\Excel\Facades\Excel;
  15. class PackageController extends Controller
  16. {
  17. public function conditionQuery(Request $request,$packages){
  18. $today=Carbon::now()->subDays(15);
  19. if ($request->input('logistic_number')){
  20. $packages=$packages->where('logistic_number','like','%'.$request->input('logistic_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  21. }
  22. if ($request->input('delivery_number')){
  23. $packages=$packages->where('delivery_number','like','%'.$request->input('delivery_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  24. }
  25. if ($request->input('created_at_start')){
  26. $packages=$packages->where('created_at','>=',$request->input('created_at_start'));
  27. }
  28. if ($request->input('created_at_end')){
  29. $packages=$packages->where('created_at','<=',$request->input('created_at_end'));
  30. }
  31. if ($request->input('owner_id')){
  32. $packages=$packages->where('owner_id',$request->input('owner_id'));
  33. }
  34. if ($request->input('batch_number')){
  35. $packages=$packages->where('batch_number','like','%'.$request->input('batch_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  36. }
  37. return $packages;
  38. }
  39. /**
  40. * Display a listing of the resource.
  41. *
  42. * @return \Illuminate\Http\Response
  43. */
  44. public function index(Request $request)
  45. {
  46. if(!Gate::allows('称重管理-查询')){ return redirect(url('/')); }
  47. if ($request->input()){
  48. $packages=Package::orderBy('id','DESC');
  49. $packages=$this->conditionQuery($request,$packages);
  50. $packages=$packages->paginate($request->input('paginate')?$request->input('paginate'):50);
  51. $owners=Owner::select('id','name')->get();
  52. return view('weight.package.index',['packages'=>$packages,'owners'=>$owners]);
  53. }
  54. $packages=Package::orderBy('id','DESC')->paginate(50);
  55. $owners=Owner::select('id','name')->get();
  56. return view('weight.package.index',['packages'=>$packages,'owners'=>$owners]);
  57. }
  58. /**
  59. * Show the form for creating a new resource.
  60. *
  61. * @return \Illuminate\Http\Response
  62. */
  63. public function create()
  64. {
  65. if(!Gate::allows('称重管理-录入')){ return redirect(url('/')); }
  66. $paperBoxes=PaperBox::select('id','model')->get();
  67. return view('weight.package.create',['paperBoxes'=>$paperBoxes]);
  68. }
  69. /**
  70. * Store a newly created resource in storage.
  71. *
  72. * @param \Illuminate\Http\Request $request
  73. * @return \Illuminate\Http\Response
  74. */
  75. public function store(Request $request)
  76. {
  77. if(!Gate::allows('称重管理-录入')){ return redirect(url('/')); }
  78. $this->validator($request)->validate();
  79. $logistic_number=$request->input('logistic_number');
  80. $weight=$request->input('weight');
  81. $batch_number=$request->input('batch_number');
  82. $paper_box_id=$request->input('paper_box_id');
  83. $package=null;
  84. if($batch_number)$package=Package::where('batch_number',$batch_number)->first();
  85. if($logistic_number)$package=Package::where('logistic_number',$logistic_number)->first();
  86. if (!$package && !$logistic_number)return redirect('package/create')->with('successError','录入失败!系统内没有对应波次的包裹!');
  87. if ($package){
  88. $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController();
  89. if ($package->batch_rule&&strstr($package->batch_rule,'组合')){
  90. $this->syncBatch($package->batch_number,$weight,null,null,null,Carbon::now(),$paper_box_id);
  91. }else{
  92. $package->weight=$weight;
  93. $package->paper_box_id=$paper_box_id;
  94. $package->batch_number=$batch_number;
  95. $result=$accomplishToWMS->accomplishToWMS($package);
  96. if ($result['result']=='success'){
  97. if ($package->status=="记录异常")$package->status="已上传异常";
  98. else $package->status="已上传";
  99. }else{
  100. $package->status="上传异常";
  101. }
  102. }
  103. }else{
  104. $package=new Package([
  105. 'logistic_number'=>$logistic_number,
  106. 'weight'=>$weight,
  107. 'paper_box_id'=>$paper_box_id,
  108. 'batch_number'=>$batch_number
  109. ]);
  110. }
  111. $package->save();
  112. event(new WeighedEvent($package));
  113. return redirect('package/create')->with('successTip','操作成功');
  114. }
  115. public function statistics(Request $request){
  116. $packages=Package::select(DB::raw('owner_id,logistic_id,COUNT(logistic_id) AS count'));
  117. if($request->input('owner_id')){
  118. $owners_id=$id = explode( ',',$request->input('owner_id'));
  119. $packages=$packages->whereIn('owner_id',$owners_id);
  120. }
  121. if($request->input('logistic_id')){
  122. $logistics_id=$id = explode( ',',$request->input('logistic_id'));
  123. $packages=$packages->whereIn('logistic_id',$logistics_id);
  124. }
  125. if($request->input('date_start')){
  126. $packages=$packages->where('created_at','<=',$request->input('date_start'));
  127. }
  128. if($request->input('date_end')){
  129. $packages=$packages->where('created_at','>=',$request->input('date_end'));
  130. }
  131. $packages=$packages->groupBy(['owner_id','logistic_id'])->get();
  132. $owners=Owner::get();
  133. $logistics=Logistic::get();
  134. return view('weight.package.statistics',["packages"=>$packages,'owners'=>$owners,'logistics'=>$logistics]);
  135. }
  136. /**
  137. * Display the specified resource.
  138. *
  139. * @param \App\Package $packages
  140. * @return \Illuminate\Http\Response
  141. */
  142. public function show(Package $packages)
  143. {
  144. //
  145. }
  146. /**
  147. * Show the form for editing the specified resource.
  148. *
  149. * @param \App\Package $packages
  150. * @return \Illuminate\Http\Response
  151. */
  152. public function edit(Package $packages)
  153. {
  154. //
  155. }
  156. /**
  157. * Update the specified resource in storage.
  158. *
  159. * @param \Illuminate\Http\Request $request
  160. * @param \App\Package $packages
  161. * @return \Illuminate\Http\Response
  162. */
  163. public function update(Request $request, Package $packages)
  164. {
  165. //
  166. }
  167. /**
  168. * Remove the specified resource from storage.
  169. *
  170. * @param \App\Package $packages
  171. * @return \Illuminate\Http\Response
  172. */
  173. public function destroy(Package $packages)
  174. {
  175. //
  176. }
  177. public function export($id,Request $request){
  178. if(!Gate::allows('称重管理-查询')){ return '没有权限'; }
  179. if ($id==-1){
  180. $id=[];
  181. ini_set('max_execution_time',2500);
  182. ini_set('memory_limit','1526M');
  183. $packages=Package::select('id');
  184. $packages=$this->conditionQuery($request,$packages);
  185. $packages=$packages->get();
  186. foreach ($packages as $package){
  187. array_push($id,$package->id);
  188. }
  189. }else $id = explode( ',',$id);
  190. if (!$id)return ;
  191. $row=[[
  192. 'id'=>'ID',
  193. 'owner_name'=>'货主',
  194. 'logistic_number'=>'快递单号',
  195. 'delivery_number'=>'发货单号',
  196. 'batch_number'=>'波次号',
  197. 'batch_rule'=>'波次规则',
  198. 'created_at'=>'操作时间',
  199. 'recipient'=>'收件人',
  200. 'recipient_mobile'=>'收件人电话',
  201. 'logistic_name'=>'承运商',
  202. 'measuringMachine_name'=>'设备',
  203. 'weight'=>'重量(KG)',
  204. 'length'=>'长(CM)',
  205. 'width'=>'宽(CM)',
  206. 'height'=>'高(CM)',
  207. 'bulk'=>'体积(CM³)',
  208. 'paperBox_name'=>'纸箱',
  209. 'status'=>'状态',
  210. ]];
  211. $list=[];
  212. for ($i=0; $i<count($id);$i++){
  213. $package=Package::find($id[$i]);
  214. $w=[
  215. 'id'=>isset($package->id)?$package->id:'',
  216. 'owner_name'=>isset($package->owner_name)?$package->owner_name:'',
  217. 'logistic_number'=>isset($package->logistic_number)?$package->logistic_number:'',
  218. 'delivery_number'=>isset($package->delivery_number)?$package->delivery_number:'',
  219. 'batch_number'=>isset($package->batch_number)?$package->batch_number:'',
  220. 'batch_rule'=>isset($package->batch_rule)?$package->batch_rule:'',
  221. 'created_at'=>isset($package->created_at)?$package->created_at:'',
  222. 'recipient'=>isset($package->recipient)?$package->recipient:'',
  223. 'recipient_mobile'=>isset($package->recipient_mobile)?$package->recipient_mobile:'',
  224. 'logistic_name'=>isset($package->logistic_name)?$package->logistic_name:'',
  225. 'measuringMachine_name'=>isset($package->measuringMachine_name)?$package->measuringMachine_name:'',
  226. 'weight'=>isset($package->weight)?$package->weight:'',
  227. 'length'=>isset($package->length)?$package->length:'',
  228. 'width'=>isset($package->width)?$package->width:'',
  229. 'height'=>isset($package->height)?$package->height:'',
  230. 'bulk'=>isset($package->bulk)?$package->bulk:'',
  231. 'paperBox_name'=>isset($package->paperBox_name)?$package->paperBox_name:'',
  232. 'status'=>isset($package->status)?$package->status:''
  233. ];
  234. $list[$i]=$w;
  235. }
  236. return Excel::download(new WaybillExport($row,$list),date('YmdHis', time()).'-称重记录单.xls');
  237. }
  238. protected function validator(Request $request){
  239. $validator=Validator::make($request->input(),[
  240. 'logistic_number'=>['nullable','max:50'],
  241. 'weight'=>'required|min:0|max:999999|numeric',
  242. 'paper_box_id'=>'nullable|integer',
  243. 'batch_number'=>'required_without:logistic_number'
  244. ],[
  245. 'required'=>':attribute 为必填项',
  246. 'max'=>':attribute 字符过多或输入值过大',
  247. 'integer'=>':attribute 选择有误',
  248. 'min'=>':attribute 不得为负',
  249. 'numeric'=>':attribute 应为数字',
  250. 'unique'=>':attribute 已存在',
  251. 'required_without'=>':attribute 与快递单号至少填一项'
  252. ],[
  253. 'logistic_number'=>'快递单号',
  254. 'weight'=>'重量',
  255. 'paper_box_id'=>'纸箱',
  256. 'batch_number'=>'波次号',
  257. ]);
  258. return $validator;
  259. }
  260. public function syncBatch($batch_number,$weight,$max,$centre,$min,$date,$paperBox_id){
  261. $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController();
  262. $packagesBatch=Package::where('batch_number',$batch_number)->get();
  263. foreach ($packagesBatch as $packageBatch){
  264. $packageBatch->weight=$weight;
  265. if($max)$packageBatch->length=$max;
  266. if($centre)$packageBatch->width=$centre;
  267. if($min)$packageBatch->height=$min;
  268. $packageBatch->bulk=$max*$centre*$min;
  269. $packageBatch->weighed_at=$date;
  270. if ($paperBox_id)$packageBatch->paper_box_id=$paperBox_id;
  271. $packageBatch->status="未上传";
  272. $result=$accomplishToWMS->accomplishToWMS($packageBatch);
  273. if ($result['result']=='success'){
  274. if ($packageBatch->status=="记录异常")$packageBatch->status="已上传异常";
  275. else $packageBatch->status="已上传";
  276. }else{
  277. $packageBatch->status="上传异常";
  278. }
  279. $packageBatch->save();
  280. }
  281. }
  282. }