PackageController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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->first()[$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($batch_number){
  114. $package=Package::where('batch_number',$batch_number)->first();
  115. }elseif($logistic_number){
  116. $package=Package::where('logistic_number',$logistic_number)->first();
  117. }
  118. if ($order_code)$package=Package::where('order_code',$order_code)->first();
  119. $accomplishToWMS=new Api\thirdPart\flux\PackageController();
  120. $result=$accomplishToWMS->markWMSOnBatch($batch_number,$weight);
  121. if ($result['result']){
  122. $newValues['status']='已上传';
  123. Controller::logS(__METHOD__,'SUCCESS_'.__FUNCTION__,'批量更改波次上传成功'.$batch_number);
  124. }else{
  125. $newValues['status']='上传异常';
  126. Controller::logS(__METHOD__,'error_'.__FUNCTION__,'批量更改波次上传异常:'.$result['message'].$batch_number);
  127. }
  128. if (!$result['result']){
  129. return redirect('package/create')->with('successError','录入失败!'.$result['message']);
  130. }
  131. // if (!$package && !$logistic_number)return redirect('package/create')->with('successError','录入失败!系统内没有对应波次的包裹!');
  132. $successTip = '操作成功';
  133. if ($package){
  134. $accomplishToWMS=new Api\thirdPart\flux\PackageController();
  135. if ($isSamePackBatch||($package->batch_rule&&strstr($package->batch_rule,'组合'))){
  136. $this->log(__METHOD__,'活动波次开始同步_'.__FUNCTION__,json_encode($package),Auth::user()['name']);
  137. $this->syncBatch($package->batch_number,$weight,null,null,null,Carbon::now(),null);
  138. }else{
  139. if($batch_number){
  140. return redirect('package/create')->with('successError','录入失败!该波次不是组合提总!');
  141. }
  142. $package->weight=$weight;
  143. $package->paper_box_id=$paper_box_id;
  144. $package->batch_number=$batch_number;
  145. $package->order_code=$order_code;
  146. $result=$accomplishToWMS->accomplishToWMS($package);
  147. if ($result['result']){
  148. if ($package->status=="记录异常")$package->status="已上传异常";
  149. else $package->status="已上传";
  150. }else{
  151. $package->status="上传异常";
  152. }
  153. }
  154. $package->save();
  155. $this->log(__METHOD__,'create_'.__FUNCTION__,json_encode($package),Auth::user()['name']);
  156. event(new WeighedEvent($package));
  157. }else{
  158. $this->syncBatch($batch_number,$weight,null,null,null,Carbon::now(),null);
  159. $successTip = "批量录入波次成功!波次号:$batch_number";
  160. }
  161. return redirect('package/create')->with('successTip', $successTip);
  162. }
  163. public function statistics(Request $request){
  164. $packages=Package::select(DB::raw('owner_id,logistic_id,COUNT(logistic_id) AS count'))->whereNotNull('owner_id');
  165. if($request->input('owner_id')){
  166. $owners_id=$id = explode( ',',$request->input('owner_id'));
  167. $packages=$packages->whereIn('owner_id',$owners_id);
  168. }
  169. if($request->input('logistic_id')){
  170. $logistics_id=$id = explode( ',',$request->input('logistic_id'));
  171. $packages=$packages->whereIn('logistic_id',$logistics_id);
  172. }
  173. if($request->input('date_start')){
  174. $packages=$packages->where('created_at','>=',$request->input('date_start'));
  175. }
  176. if($request->input('date_end')){
  177. $packages=$packages->where('created_at','<=',$request->input('date_end'));
  178. }
  179. $packages=$packages->groupBy(['owner_id','logistic_id'])->get();
  180. $owners=Owner::get();
  181. $logistics=Logistic::get();
  182. if ($request->input('checkSign')){
  183. $logistics=$this->checkLogistic($packages);
  184. if ($request->input('checkSign')=="-1"){
  185. $excel=$this->statisticExport($packages,$owners,$logistics);
  186. return $excel;
  187. }
  188. $id=$id = explode( ',',$request->input('checkSign'));
  189. $packages=Package::select(DB::raw('owner_id,logistic_id,COUNT(logistic_id) AS count'))
  190. ->whereIn('owner_id',$id)->groupBy(['owner_id','logistic_id'])->get();
  191. $logistics=$this->checkLogistic($packages);
  192. $excel=$this->statisticExport($packages,$owners,$logistics);
  193. return $excel;
  194. }
  195. return view('weight.package.statistics',["packages"=>$packages,'owners'=>$owners,'logistics'=>$logistics]);
  196. }
  197. /*
  198. * 根据packages统计记录删选物流公司
  199. */
  200. public function checkLogistic($packages){
  201. $logisticIds=[];
  202. foreach ($packages as $package){
  203. $isLogistic=true;
  204. for ($i=0;$i<count($logisticIds);$i++){
  205. if ($package->logistic_id==$logisticIds[$i]){
  206. $isLogistic=false;
  207. break;
  208. }
  209. }
  210. if ($isLogistic){
  211. array_push($logisticIds,$package->logistic_id);
  212. }
  213. }
  214. $logistics=Logistic::whereIn('id',$logisticIds)->get();
  215. return $logistics;
  216. }
  217. /**
  218. * Display the specified resource.
  219. *
  220. * @param Package $packages
  221. * @return Response
  222. */
  223. public function show(Package $packages)
  224. {
  225. //
  226. }
  227. /**
  228. * Show the form for editing the specified resource.
  229. *
  230. * @param Package $packages
  231. * @return Response
  232. */
  233. public function edit(Package $packages)
  234. {
  235. //
  236. }
  237. /**
  238. * Update the specified resource in storage.
  239. *
  240. * @param Request $request
  241. * @param Package $packages
  242. * @return Response
  243. */
  244. public function update(Request $request, Package $packages)
  245. {
  246. //
  247. }
  248. /**
  249. * Remove the specified resource from storage.
  250. *
  251. * @param Package $packages
  252. * @return Response
  253. */
  254. public function destroy(Package $packages)
  255. {
  256. //
  257. }
  258. public function export($id,Request $request){
  259. if(!Gate::allows('称重管理-查询')){ return '没有权限'; }
  260. ini_set('max_execution_time',5500);
  261. ini_set('memory_limit','5526M');
  262. if ($id==-1){
  263. $id=[];
  264. $packages=Package::select('id');
  265. $packages=$this->conditionQuery($request,$packages);
  266. $packages=$packages->get();
  267. foreach ($packages as $package){
  268. array_push($id,$package->id);
  269. }
  270. }else $id = explode( ',',$id);
  271. if (!$id)return ;
  272. $row=[[
  273. 'id'=>'ID',
  274. 'owner_name'=>'货主',
  275. 'logistic_number'=>'快递单号',
  276. 'delivery_number'=>'发货单号',
  277. 'batch_number'=>'波次号',
  278. 'batch_rule'=>'波次规则',
  279. 'created_at'=>'操作时间',
  280. 'recipient'=>'收件人',
  281. 'recipient_mobile'=>'收件人电话',
  282. 'logistic_name'=>'承运商',
  283. 'measuringMachine_name'=>'设备',
  284. 'weight'=>'重量(KG)',
  285. 'length'=>'长(CM)',
  286. 'width'=>'宽(CM)',
  287. 'height'=>'高(CM)',
  288. 'bulk'=>'体积(CM³)',
  289. 'paperBox_name'=>'纸箱',
  290. 'status'=>'状态',
  291. ]];
  292. $list=[];
  293. for ($i=0; $i<count($id);$i++){
  294. $package=Package::find($id[$i]);
  295. $w=[
  296. 'id'=>isset($package->id)?$package->id:'',
  297. 'owner_name'=>isset($package->owner_name)?$package->owner_name:'',
  298. 'logistic_number'=>isset($package->logistic_number)?$package->logistic_number:'',
  299. 'delivery_number'=>isset($package->delivery_number)?$package->delivery_number:'',
  300. 'batch_number'=>isset($package->batch_number)?$package->batch_number:'',
  301. 'batch_rule'=>isset($package->batch_rule)?$package->batch_rule:'',
  302. 'created_at'=>isset($package->created_at)?$package->created_at:'',
  303. 'recipient'=>isset($package->recipient)?$package->recipient:'',
  304. 'recipient_mobile'=>isset($package->recipient_mobile)?$package->recipient_mobile:'',
  305. 'logistic_name'=>isset($package->logistic_name)?$package->logistic_name:'',
  306. 'measuringMachine_name'=>isset($package->measuringMachine_name)?$package->measuringMachine_name:'',
  307. 'weight'=>isset($package->weight)?$package->weight:'',
  308. 'length'=>isset($package->length)?$package->length:'',
  309. 'width'=>isset($package->width)?$package->width:'',
  310. 'height'=>isset($package->height)?$package->height:'',
  311. 'bulk'=>isset($package->bulk)?$package->bulk:'',
  312. 'paperBox_name'=>isset($package->paperBox_name)?$package->paperBox_name:'',
  313. 'status'=>isset($package->status)?$package->status:''
  314. ];
  315. $list[$i]=$w;
  316. }
  317. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-称重记录单.xlsx');
  318. }
  319. protected function validator(Request $request){
  320. $validator=Validator::make($request->input(),[
  321. 'logistic_number'=>['required_without_all:batch_number,order_code'],
  322. 'order_code'=>['required_without_all:logistic_number,batch_number'],
  323. 'weight'=>'required|min:0|max:999999|numeric',
  324. 'paper_box_id'=>'nullable|integer',
  325. 'batch_number'=>['required_without_all:logistic_number,order_code']
  326. ],[
  327. 'required'=>':attribute 为必填项',
  328. 'max'=>':attribute 字符过多或输入值过大',
  329. 'integer'=>':attribute 选择有误',
  330. 'min'=>':attribute 不得为负',
  331. 'numeric'=>':attribute 应为数字',
  332. 'unique'=>':attribute 已存在',
  333. 'required_without_all'=>':attribute 与快递单号或订单号至少选一项'
  334. ],[
  335. 'logistic_number'=>'快递单号',
  336. 'weight'=>'重量',
  337. 'paper_box_id'=>'纸箱',
  338. 'batch_number'=>'波次号',
  339. 'order_code'=>'订单号',
  340. ]);
  341. return $validator;
  342. }
  343. public function syncBatch($batch_number,$weight,$max,$centre,$min,$date,$paperBox_id){
  344. // $accomplishToWMS=new Api\thirdPart\flux\PackageController();
  345. // $packageBatch=Package::where('batch_number',$batch_number)->first();
  346. $newValues = ['weight' => $weight];
  347. $newValues['batch_number']=$batch_number;
  348. if($max)$newValues['length']=$max;
  349. if($centre)$newValues['width']=$centre;
  350. if($min)$newValues['height']=$min;
  351. if($date)$newValues['weighed_at']=$date;
  352. if($paperBox_id)$newValues['paper_box_id']=$paperBox_id;
  353. if($max&&$centre&&$min){
  354. $newValues['bulk']=$max*$centre*$min;
  355. }
  356. // $weightChanged=$packageBatch['weight']!=$weight;
  357. // Package::where('batch_number',$batch_number)->update($newValues);
  358. // $packageBatch['forceUpload']=$weightChanged;
  359. Controller::logS(__METHOD__,__FUNCTION__,"批量更新时批次号传入:{$batch_number}");
  360. // $result=$accomplishToWMS->markWMSOnBatch($packageBatch['batch_number']);
  361. // if ($result['result']){
  362. // $newValues['status']='已上传';
  363. // Controller::logS(__METHOD__,'SUCCESS_'.__FUNCTION__,'批量更改波次上传成功'.json_encode($packageBatch));
  364. // }else{
  365. // $newValues['status']='上传异常';
  366. // Controller::logS(__METHOD__,'error_'.__FUNCTION__,'批量更改波次上传异常:'.$result['message'].json_encode($packageBatch));
  367. // }picktotraceid
  368. $packagesInOracle=OracleDOCWaveDetails::where('doc_wave_details.waveno',$batch_number)
  369. ->leftJoin('act_allocation_details','act_allocation_details.orderno','doc_wave_details.orderno')
  370. ->get();
  371. foreach ($packagesInOracle as $packageInOracle) {
  372. $newValues['logistic_number']=$packageInOracle['picktotraceid'];
  373. $package = Package::where('logistic_number', $packageInOracle['picktotraceid'])->first();
  374. if(!$package){
  375. Package::create($newValues);
  376. }else{
  377. $package->fill($newValues);
  378. $package->update();
  379. }
  380. }
  381. // Package::where('batch_number',$batch_number)->update($newValues);
  382. }
  383. public function statisticExport($packages,$owners,$logistics){
  384. if (!$packages||!$owners||!$logistics) return;
  385. ini_set('max_execution_time',3500);
  386. ini_set('memory_limit','2726M');
  387. $row=[[]];
  388. $row[0]['owner']='货主';
  389. $row[0]['sum']='总计';
  390. foreach ($logistics as $logistic){
  391. $row[0][$logistic->id]=$logistic->name;
  392. }
  393. $ownerArr=[];
  394. foreach ($owners as $owner){
  395. foreach ($packages as $package){
  396. if ($owner->id==$package->owner_id){
  397. $ownerArr[$owner->name][$package->logistic_id]=$package->count;
  398. }
  399. }
  400. }
  401. $list=[];
  402. $i=0;
  403. foreach ($owners as $owner){
  404. if (isset($ownerArr[$owner->name])){
  405. $w['owner']=$owner->name;
  406. $sum=0;
  407. foreach ($logistics as $logistic){
  408. if (isset($ownerArr[$owner->name][$logistic->id])){
  409. $w[$logistic->id]=$ownerArr[$owner->name][$logistic->id];
  410. $sum=$sum+$ownerArr[$owner->name][$logistic->id];
  411. }
  412. if (!isset($ownerArr[$owner->name][$logistic->id])){
  413. $w[$logistic->id]=0;
  414. }
  415. }
  416. $w['sum']=$sum;
  417. $list[$i]=$w;
  418. $i++;
  419. }
  420. }
  421. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-称重统计记录单.xlsx');
  422. }
  423. }