PackageController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Events\WeighedEvent;
  4. use App\Exports\Export;
  5. use App\Logistic;
  6. use App\Owner;
  7. use App\Package;
  8. use App\PaperBox;
  9. use Box\Spout\Common\Helper\Escaper\XLSX;
  10. use Box\Spout\Common\Type;
  11. use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
  12. use Box\Spout\Writer\Common\Creator\WriterFactory;
  13. use Carbon\Carbon;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Facades\Auth;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Gate;
  18. use Illuminate\Support\Facades\Validator;
  19. use Maatwebsite\Excel\Facades\Excel;
  20. use Box\Spout\Common\Entity\Row;
  21. class PackageController extends Controller
  22. {
  23. //超15天精确查询抽离 cloumn前提:数据库字段名必须与request内字段名一致
  24. public function preciseQuery(string $column,Request $request,$packages){
  25. $today=Carbon::now()->subDays(15);
  26. $packagesTem=clone $packages;
  27. $packagesTem=$packagesTem->where($column,'like','%'.$request->input($column).'%')->where('created_at','>',$today->format('Y-m-d'));
  28. if($packagesTem->count()==0
  29. ||$packagesTem->first()[$column]==$request->input($column)){
  30. $packages=$packages->where($column,$request->input($column));
  31. }else{
  32. $packages=$packagesTem;
  33. }
  34. return $packages;
  35. }
  36. public function conditionQuery(Request $request,$packages){
  37. if ($request->input('logistic_number')){
  38. $packages=$this->preciseQuery('logistic_number',$request,$packages);
  39. }
  40. /* if ($request->input('delivery_number')){
  41. $packages=$this->preciseQuery('delivery_number',$request,$packages);
  42. }*/
  43. if ($request->input('created_at_start')){
  44. $created_at_start=$request->input('created_at_start')." 00:00:00";
  45. $packages=$packages->where('created_at','>=',$created_at_start);
  46. }
  47. if ($request->input('created_at_end')){
  48. $created_at_end=$request->input('created_at_end')." 23:59:59";
  49. $packages=$packages->where('created_at','<=',$created_at_end);
  50. }
  51. if ($request->input('owner_id')){
  52. $packages=$packages->where('owner_id',$request->input('owner_id'));
  53. }
  54. if ($request->input('batch_number')){
  55. $packages=$this->preciseQuery('batch_number',$request,$packages);
  56. }
  57. return $packages;
  58. }
  59. /**
  60. * Display a listing of the resource.
  61. *
  62. * @return \Illuminate\Http\Response
  63. */
  64. public function index(Request $request)
  65. {
  66. if(!Gate::allows('称重管理-查询')){ return redirect(url('/')); }
  67. if ($request->input()){
  68. $packages=Package::orderBy('id','DESC');
  69. $packages=$this->conditionQuery($request,$packages);
  70. $packages=$packages->paginate($request->input('paginate')?$request->input('paginate'):50);
  71. $owners=Owner::select('id','name')->get();
  72. return view('weight.package.index',['packages'=>$packages,'owners'=>$owners,'request'=>$request->input()]);
  73. }
  74. $packages=Package::orderBy('id','DESC')->paginate(50);
  75. $owners=Owner::select('id','name')->get();
  76. return view('weight.package.index',['packages'=>$packages,'owners'=>$owners]);
  77. }
  78. /**
  79. * Show the form for creating a new resource.
  80. *
  81. * @return \Illuminate\Http\Response
  82. */
  83. public function create()
  84. {
  85. if(!Gate::allows('称重管理-录入')){ return redirect(url('/')); }
  86. $paperBoxes=PaperBox::select('id','model')->get();
  87. return view('weight.package.create',['paperBoxes'=>$paperBoxes]);
  88. }
  89. /**
  90. * Store a newly created resource in storage.
  91. *
  92. * @param \Illuminate\Http\Request $request
  93. * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  94. */
  95. public function store(Request $request)
  96. {
  97. if(!Gate::allows('称重管理-录入')){ return redirect(url('/')); }
  98. $this->validator($request)->validate();
  99. $logistic_number=$request->input('logistic_number');
  100. $weight=$request->input('weight');
  101. $batch_number=$request->input('batch_number');
  102. $order_code=$request->input('order_code');
  103. $paper_box_id=$request->input('paper_box_id');
  104. $isSamePackBatch=$request->input('is_same_pack_batch');
  105. if($logistic_number&&$batch_number)return redirect('package/create')->with('successError','录入失败!波次号和快递单号只能填一项!');
  106. $package=null;
  107. if($batch_number){
  108. $package=Package::where('batch_number',$batch_number)->first();
  109. }elseif($logistic_number){
  110. $package=Package::where('logistic_number',$logistic_number)->first();
  111. }
  112. if ($order_code)$package=Package::where('order_code',$order_code)->first();
  113. if (!$package && !$logistic_number)return redirect('package/create')->with('successError','录入失败!系统内没有对应波次的包裹!');
  114. $successTip = '操作成功';
  115. if ($package){
  116. $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController();
  117. if ($isSamePackBatch||($package->batch_rule&&strstr($package->batch_rule,'组合'))){
  118. $this->log(__METHOD__,'活动波次开始同步_'.__FUNCTION__,json_encode($package),Auth::user()['name']);
  119. $this->syncBatch($package->batch_number,$weight,null,null,null,Carbon::now(),$paper_box_id);
  120. }else{
  121. if($batch_number){
  122. return redirect('package/create')->with('successError','录入失败!该波次不是组合提总!');
  123. }
  124. $package->weight=$weight;
  125. $package->paper_box_id=$paper_box_id;
  126. $package->batch_number=$batch_number;
  127. $package->order_code=$order_code;
  128. $result=$accomplishToWMS->accomplishToWMS($package);
  129. if ($result['result']=='success'){
  130. if ($package->status=="记录异常")$package->status="已上传异常";
  131. else $package->status="已上传";
  132. }else{
  133. $package->status="上传异常";
  134. }
  135. }
  136. }else{
  137. $package=new Package([
  138. 'logistic_number'=>$logistic_number,
  139. 'weight'=>$weight,
  140. 'paper_box_id'=>$paper_box_id,
  141. 'batch_number'=>$batch_number,
  142. 'order_code'=>$order_code,
  143. ]);
  144. $successTip = "新建称重记录成功!单号:$logistic_number";
  145. }
  146. $package->save();
  147. $this->log(__METHOD__,'create_'.__FUNCTION__,json_encode($package),Auth::user()['name']);
  148. event(new WeighedEvent($package));
  149. return redirect('package/create')->with('successTip', $successTip);
  150. }
  151. public function statistics(Request $request){
  152. $packages=Package::select(DB::raw('owner_id,logistic_id,COUNT(logistic_id) AS count'))->whereNotNull('owner_id');
  153. if($request->input('owner_id')){
  154. $owners_id=$id = explode( ',',$request->input('owner_id'));
  155. $packages=$packages->whereIn('owner_id',$owners_id);
  156. }
  157. if($request->input('logistic_id')){
  158. $logistics_id=$id = explode( ',',$request->input('logistic_id'));
  159. $packages=$packages->whereIn('logistic_id',$logistics_id);
  160. }
  161. if($request->input('date_start')){
  162. $packages=$packages->where('created_at','>=',$request->input('date_start'));
  163. }
  164. if($request->input('date_end')){
  165. $packages=$packages->where('created_at','<=',$request->input('date_end'));
  166. }
  167. $packages=$packages->groupBy(['owner_id','logistic_id'])->get();
  168. $owners=Owner::get();
  169. $logistics=Logistic::get();
  170. if ($request->input('checkSign')){
  171. $logistics=$this->checkLogistic($packages);
  172. if ($request->input('checkSign')=="-1"){
  173. $excel=$this->statisticExport($packages,$owners,$logistics);
  174. return $excel;
  175. }
  176. $id=$id = explode( ',',$request->input('checkSign'));
  177. $packages=Package::select(DB::raw('owner_id,logistic_id,COUNT(logistic_id) AS count'))
  178. ->whereIn('owner_id',$id)->groupBy(['owner_id','logistic_id'])->get();
  179. $logistics=$this->checkLogistic($packages);
  180. $excel=$this->statisticExport($packages,$owners,$logistics);
  181. return $excel;
  182. }
  183. return view('weight.package.statistics',["packages"=>$packages,'owners'=>$owners,'logistics'=>$logistics]);
  184. }
  185. /*
  186. * 根据packages统计记录删选物流公司
  187. */
  188. public function checkLogistic($packages){
  189. $logisticIds=[];
  190. foreach ($packages as $package){
  191. $isLogistic=true;
  192. for ($i=0;$i<count($logisticIds);$i++){
  193. if ($package->logistic_id==$logisticIds[$i]){
  194. $isLogistic=false;
  195. break;
  196. }
  197. }
  198. if ($isLogistic){
  199. array_push($logisticIds,$package->logistic_id);
  200. }
  201. }
  202. $logistics=Logistic::whereIn('id',$logisticIds)->get();
  203. return $logistics;
  204. }
  205. /**
  206. * Display the specified resource.
  207. *
  208. * @param \App\Package $packages
  209. * @return \Illuminate\Http\Response
  210. */
  211. public function show(Package $packages)
  212. {
  213. //
  214. }
  215. /**
  216. * Show the form for editing the specified resource.
  217. *
  218. * @param \App\Package $packages
  219. * @return \Illuminate\Http\Response
  220. */
  221. public function edit(Package $packages)
  222. {
  223. //
  224. }
  225. /**
  226. * Update the specified resource in storage.
  227. *
  228. * @param \Illuminate\Http\Request $request
  229. * @param \App\Package $packages
  230. * @return \Illuminate\Http\Response
  231. */
  232. public function update(Request $request, Package $packages)
  233. {
  234. //
  235. }
  236. /**
  237. * Remove the specified resource from storage.
  238. *
  239. * @param \App\Package $packages
  240. * @return \Illuminate\Http\Response
  241. */
  242. public function destroy(Package $packages)
  243. {
  244. //
  245. }
  246. public function export($id,Request $request){
  247. if(!Gate::allows('称重管理-查询')){ return '没有权限'; }
  248. ini_set('max_execution_time',3500);
  249. ini_set('memory_limit','3526M');
  250. if ($id==-1){
  251. $id=[];
  252. $packages=Package::select('id');
  253. $packages=$this->conditionQuery($request,$packages);
  254. $packages=$packages->get();
  255. foreach ($packages as $package){
  256. array_push($id,$package->id);
  257. }
  258. }else $id = explode( ',',$id);
  259. if (!$id)return ;
  260. $row=[[
  261. 'id'=>'ID',
  262. 'owner_name'=>'货主',
  263. 'logistic_number'=>'快递单号',
  264. 'delivery_number'=>'发货单号',
  265. 'batch_number'=>'波次号',
  266. 'batch_rule'=>'波次规则',
  267. 'created_at'=>'操作时间',
  268. 'recipient'=>'收件人',
  269. 'recipient_mobile'=>'收件人电话',
  270. 'logistic_name'=>'承运商',
  271. 'measuringMachine_name'=>'设备',
  272. 'weight'=>'重量(KG)',
  273. 'length'=>'长(CM)',
  274. 'width'=>'宽(CM)',
  275. 'height'=>'高(CM)',
  276. 'bulk'=>'体积(CM³)',
  277. 'paperBox_name'=>'纸箱',
  278. 'status'=>'状态',
  279. ]];
  280. $list=[];
  281. for ($i=0; $i<count($id);$i++){
  282. $package=Package::find($id[$i]);
  283. $w=[
  284. 'id'=>isset($package->id)?$package->id:'',
  285. 'owner_name'=>isset($package->owner_name)?$package->owner_name:'',
  286. 'logistic_number'=>isset($package->logistic_number)?$package->logistic_number:'',
  287. 'delivery_number'=>isset($package->delivery_number)?$package->delivery_number:'',
  288. 'batch_number'=>isset($package->batch_number)?$package->batch_number:'',
  289. 'batch_rule'=>isset($package->batch_rule)?$package->batch_rule:'',
  290. 'created_at'=>isset($package->created_at)?$package->created_at:'',
  291. 'recipient'=>isset($package->recipient)?$package->recipient:'',
  292. 'recipient_mobile'=>isset($package->recipient_mobile)?$package->recipient_mobile:'',
  293. 'logistic_name'=>isset($package->logistic_name)?$package->logistic_name:'',
  294. 'measuringMachine_name'=>isset($package->measuringMachine_name)?$package->measuringMachine_name:'',
  295. 'weight'=>isset($package->weight)?$package->weight:'',
  296. 'length'=>isset($package->length)?$package->length:'',
  297. 'width'=>isset($package->width)?$package->width:'',
  298. 'height'=>isset($package->height)?$package->height:'',
  299. 'bulk'=>isset($package->bulk)?$package->bulk:'',
  300. 'paperBox_name'=>isset($package->paperBox_name)?$package->paperBox_name:'',
  301. 'status'=>isset($package->status)?$package->status:''
  302. ];
  303. $list[$i]=$w;
  304. }
  305. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-称重记录单.xlsx');
  306. }
  307. protected function validator(Request $request){
  308. $validator=Validator::make($request->input(),[
  309. 'logistic_number'=>['required_without_all:batch_number,order_code'],
  310. 'order_code'=>['required_without_all:logistic_number,batch_number'],
  311. 'weight'=>'required|min:0|max:999999|numeric',
  312. 'paper_box_id'=>'nullable|integer',
  313. 'batch_number'=>['required_without_all:logistic_number,order_code']
  314. ],[
  315. 'required'=>':attribute 为必填项',
  316. 'max'=>':attribute 字符过多或输入值过大',
  317. 'integer'=>':attribute 选择有误',
  318. 'min'=>':attribute 不得为负',
  319. 'numeric'=>':attribute 应为数字',
  320. 'unique'=>':attribute 已存在',
  321. 'required_without_all'=>':attribute 与快递单号或订单号至少选一项'
  322. ],[
  323. 'logistic_number'=>'快递单号',
  324. 'weight'=>'重量',
  325. 'paper_box_id'=>'纸箱',
  326. 'batch_number'=>'波次号',
  327. 'order_code'=>'订单号',
  328. ]);
  329. return $validator;
  330. }
  331. public function syncBatch($batch_number,$weight,$max,$centre,$min,$date,$paperBox_id){
  332. $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController();
  333. $packageBatch=Package::where('batch_number',$batch_number)->first();
  334. if(!$packageBatch)return;
  335. $newValues = ['weight' => $weight];
  336. if($max)$newValues['length']=$max;
  337. if($centre)$newValues['width']=$centre;
  338. if($min)$newValues['height']=$min;
  339. if($date)$newValues['weighed_at']=$date;
  340. if($paperBox_id)$newValues['paper_box_id']=$paperBox_id;
  341. if($max&&$centre&&$min){
  342. $newValues['bulk']=$max*$centre*$min;
  343. }
  344. $weightChanged=$packageBatch['weight']!=$weight;
  345. Package::where('batch_number',$batch_number)->update($newValues);
  346. $packageBatch['forceUpload']=$weightChanged;
  347. $result=$accomplishToWMS->accomplishToWMS($packageBatch);
  348. if ($result['result']=='success'){
  349. $newValues['status']='已上传';
  350. Controller::logS(__METHOD__,'SUCCESS_'.__FUNCTION__,'批量更改波次上传成功'.json_encode($packageBatch));
  351. }else{
  352. $newValues['status']='上传异常';
  353. Controller::logS(__METHOD__,'error_'.__FUNCTION__,'批量更改波次上传异常:'.json_encode($packageBatch));
  354. }
  355. Package::where('batch_number',$batch_number)->update($newValues);
  356. }
  357. public function statisticExport($packages,$owners,$logistics){
  358. if (!$packages||!$owners||!$logistics) return;
  359. $row=[[]];
  360. $row[0]['owner']='货主';
  361. $row[0]['sum']='总计';
  362. foreach ($logistics as $logistic){
  363. $row[0][$logistic->id]=$logistic->name;
  364. }
  365. $ownerArr=[];
  366. foreach ($owners as $owner){
  367. foreach ($packages as $package){
  368. if ($owner->id==$package->owner_id){
  369. $ownerArr[$owner->name][$package->logistic_id]=$package->count;
  370. }
  371. }
  372. }
  373. $list=[];
  374. $i=0;
  375. foreach ($owners as $owner){
  376. if (isset($ownerArr[$owner->name])){
  377. $w['owner']=$owner->name;
  378. $sum=0;
  379. foreach ($logistics as $logistic){
  380. if (isset($ownerArr[$owner->name][$logistic->id])){
  381. $w[$logistic->id]=$ownerArr[$owner->name][$logistic->id];
  382. $sum=$sum+$ownerArr[$owner->name][$logistic->id];
  383. }
  384. if (!isset($ownerArr[$owner->name][$logistic->id])){
  385. $w[$logistic->id]=0;
  386. }
  387. }
  388. $w['sum']=$sum;
  389. $list[$i]=$w;
  390. $i++;
  391. }
  392. }
  393. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-称重统计记录单.xlsx');
  394. }
  395. }