PackageController.php 18 KB

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