PackageController.php 17 KB

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