PackageController.php 17 KB

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