PackageController.php 16 KB

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