PackageController.php 12 KB

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