PackageController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\Export;
  4. use App\Logistic;
  5. use App\Owner;
  6. use App\Package;
  7. use App\PaperBox;
  8. use App\Services\OwnerService;
  9. use App\Services\PackageService;
  10. use Carbon\Carbon;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Gate;
  15. use Illuminate\Support\Facades\Validator;
  16. use Maatwebsite\Excel\Facades\Excel;
  17. class PackageController extends Controller
  18. {
  19. public function __construct()
  20. {
  21. app()->singleton('packageService',PackageService::class);
  22. }
  23. /**
  24. * Display a listing of the resource.
  25. * @param Request $request
  26. * @param OwnerService $ownerService
  27. * @return void
  28. */
  29. public function index(Request $request,OwnerService $ownerService)
  30. {
  31. if(!Gate::allows('称重管理-查询')){ return redirect(url('/')); }
  32. $request = $request->input();
  33. $packages = app('packageService')->paginate($request);
  34. return view('weight.package.index',['packages'=>$packages,'owners'=>$ownerService->getSelection(),'request'=>$request]);
  35. }
  36. /**
  37. * Show the form for creating a new resource.
  38. *
  39. * @return void
  40. */
  41. public function create()
  42. {
  43. if(!Gate::allows('称重管理-录入')){ return redirect(url('/')); }
  44. $paperBoxes=PaperBox::select('id','model')->get();
  45. return view('weight.package.create',['paperBoxes'=>$paperBoxes]);
  46. }
  47. /**
  48. * Store a newly created resource in storage.
  49. *
  50. * @param Request $request
  51. * @return void
  52. */
  53. public function store(Request $request)
  54. {
  55. if(!Gate::allows('称重管理-录入')){ return redirect(url('/')); }
  56. $this->validator($request)->validate();
  57. $logistic_number=$request->input('logistic_number');
  58. $weight=$request->input('weight');
  59. $batch_number=strtoupper($request->input('batch_number'));
  60. $order_code=$request->input('order_code');
  61. $paper_box_id=$request->input('paper_box_id');
  62. // $isSamePackBatch=$request->input('is_same_pack_batch');
  63. if($logistic_number&&$batch_number)return redirect('package/create')->with('successError','录入失败!波次号和快递单号只能填一项!');
  64. $package=null;
  65. $successTip = '操作成功';
  66. $accomplishToWMS=new Api\thirdPart\flux\PackageController();
  67. if($batch_number){
  68. $this->log(__METHOD__,'活动波次开始同步_'.__FUNCTION__,json_encode($package),Auth::user()['name']);
  69. // $this->syncBatch($package->batch_number,$weight,null,null,null,Carbon::now(),null);
  70. $result=$accomplishToWMS->markWMSOnBatch($batch_number,$weight);
  71. Package::createPackagesFromBatchCode($batch_number,$weight);
  72. if($package&&$package->isActivityBatch())
  73. $package->unifyThisMeasureUnderSameBatch();
  74. if ($result['result']){
  75. Controller::logS(__METHOD__,'SUCCESS_'.__FUNCTION__,'批量更改波次上传成功'.$batch_number);
  76. $successTip = "批量录入波次成功!波次号:$batch_number";
  77. return redirect('package/create')->with('successTip', $successTip);
  78. }
  79. Controller::logS(__METHOD__,'error_'.__FUNCTION__,'批量更改波次上传异常:'.$result['message'].$batch_number);
  80. return redirect('package/create')->with('successError','录入失败!'.$result['message']);
  81. }
  82. if ($order_code){
  83. $package=Package::where('order_code',$order_code)->first();
  84. }else{
  85. if($batch_number){
  86. $package=Package::where('batch_number',$batch_number)->first();
  87. }elseif($logistic_number){
  88. $package=Package::where('logistic_number',$logistic_number)->first();
  89. }
  90. }
  91. if (!$package && !$logistic_number)return redirect('package/create')->with('successError','录入失败!系统内没有对应波次的包裹!');
  92. if ($package){
  93. if(!$package->batch_number)$package->batch_number=$batch_number;
  94. if(!$package->order_code)$package->order_code=$order_code;
  95. $package->fetchAllFromOracle();
  96. $package->weight=$weight;
  97. if(!$package->paper_box_id)$package->paper_box_id=$paper_box_id;
  98. $result=$accomplishToWMS->accomplishToWMS($package);
  99. if ($result['result']){
  100. $package->status="已上传";
  101. }else{
  102. $package->status="上传异常";
  103. }
  104. $package->save();
  105. $this->log(__METHOD__,'create_'.__FUNCTION__,json_encode($package),Auth::user()['name']);
  106. // event(new WeighedEvent($package));
  107. }else{
  108. $package=new Package([
  109. 'logistic_number'=>$logistic_number??'',
  110. 'delivery_number'=>$logistic_number??'',
  111. 'weight'=>$weight,
  112. 'weighed_at'=>Carbon::now(),
  113. 'status'=>"已上传",
  114. ]);
  115. $package->fetchAllFromOracle();
  116. $result=$accomplishToWMS->accomplishToWMS($package);
  117. $str = "录入包裹成功!";
  118. $str .= $package['logistic_number']?"快递单号:{$package['logistic_number']}":'';
  119. $str .= $package['order_code']?", 订单号:{$package['order_code']}":'';
  120. if(!$result||$result['result']=='false'){
  121. $package['status'] = '上传失败';
  122. $str = "录入包裹失败!";
  123. $str .= $package['logistic_number']?"快递单号:{$package['logistic_number']}":'';
  124. $str .= $package['order_code']?", 订单号:{$package['order_code']}":'';
  125. $this->log(__METHOD__,__FUNCTION__,json_encode($package).json_encode($result).$str,Auth::user()['name']);
  126. }
  127. $package->save();
  128. $successTip = $str;
  129. }
  130. return redirect('package/create')->with('successTip', $successTip);
  131. }
  132. public function statistics(Request $request){
  133. $packages=Package::select(DB::raw('owner_id,logistic_id,COUNT(logistic_id) AS count'))->whereNotNull('owner_id');
  134. if($request->input('owner_id')){
  135. $owners_id=$id = explode( ',',$request->input('owner_id'));
  136. $packages=$packages->whereIn('owner_id',$owners_id);
  137. }
  138. if($request->input('logistic_id')){
  139. $logistics_id=$id = explode( ',',$request->input('logistic_id'));
  140. $packages=$packages->whereIn('logistic_id',$logistics_id);
  141. }
  142. if($request->input('date_start')){
  143. $packages=$packages->where('created_at','>=',$request->input('date_start'));
  144. }
  145. if($request->input('date_end')){
  146. $packages=$packages->where('created_at','<=',$request->input('date_end'));
  147. }
  148. $packages=$packages->groupBy(['owner_id','logistic_id'])->get();
  149. $owners=Owner::get();
  150. $logistics=Logistic::get();
  151. if ($request->input('checkSign')){
  152. $logistics=$this->checkLogistic($packages);
  153. if ($request->input('checkSign')=="-1"){
  154. $excel=$this->statisticExport($packages,$owners,$logistics);
  155. return $excel;
  156. }
  157. $id=$id = explode( ',',$request->input('checkSign'));
  158. $packages=Package::select(DB::raw('owner_id,logistic_id,COUNT(logistic_id) AS count'))
  159. ->whereIn('owner_id',$id)->groupBy(['owner_id','logistic_id'])->get();
  160. $logistics=$this->checkLogistic($packages);
  161. $excel=$this->statisticExport($packages,$owners,$logistics);
  162. return $excel;
  163. }
  164. return view('weight.package.statistics',["packages"=>$packages,'owners'=>$owners,'logistics'=>$logistics]);
  165. }
  166. /*
  167. * 根据packages统计记录删选物流公司
  168. */
  169. public function checkLogistic($packages){
  170. $logisticIds=[];
  171. foreach ($packages as $package){
  172. $isLogistic=true;
  173. for ($i=0;$i<count($logisticIds);$i++){
  174. if ($package->logistic_id==$logisticIds[$i]){
  175. $isLogistic=false;
  176. break;
  177. }
  178. }
  179. if ($isLogistic){
  180. array_push($logisticIds,$package->logistic_id);
  181. }
  182. }
  183. $logistics=Logistic::whereIn('id',$logisticIds)->get();
  184. return $logistics;
  185. }
  186. public function export(Request $request){
  187. if(!Gate::allows('称重管理-查询')){ return '没有权限'; }
  188. ini_set('max_execution_time',3500);
  189. ini_set('memory_limit','3526M');
  190. if ($request->checkAllSign){
  191. $request->offsetUnset('checkAllSign');
  192. $packages = app('packageService')->get($request);
  193. }else{
  194. $packages=app('packageService')->some($request);
  195. }
  196. $row=[[
  197. 'id'=>'ID',
  198. 'owner_name'=>'货主',
  199. 'logistic_number'=>'快递单号',
  200. 'delivery_number'=>'发货单号',
  201. 'batch_number'=>'波次号',
  202. 'batch_rule'=>'波次规则',
  203. 'created_at'=>'操作时间',
  204. 'recipient'=>'收件人',
  205. 'recipient_mobile'=>'收件人电话',
  206. 'logistic_name'=>'承运商',
  207. 'measuringMachine_name'=>'设备',
  208. 'weight'=>'重量(KG)',
  209. 'length'=>'长(CM)',
  210. 'width'=>'宽(CM)',
  211. 'height'=>'高(CM)',
  212. 'bulk'=>'体积(DM³)',
  213. 'paperBox_name'=>'纸箱',
  214. 'status'=>'状态',
  215. ]];
  216. $list=[];
  217. for ($i=0; $i<count($packages);$i++){
  218. $package=$packages[$i];
  219. $w=[
  220. 'id'=>isset($package->id)?$package->id:'',
  221. 'owner_name'=>isset($package->owner_name)?$package->owner_name:'',
  222. 'logistic_number'=>isset($package->logistic_number)?$package->logistic_number:'',
  223. 'delivery_number'=>isset($package->delivery_number)?$package->delivery_number:'',
  224. 'batch_number'=>isset($package->batch_number)?$package->batch_number:'',
  225. 'batch_rule'=>isset($package->batch_rule)?$package->batch_rule:'',
  226. 'created_at'=>isset($package->created_at)?$package->created_at:'',
  227. 'recipient'=>isset($package->recipient)?$package->recipient:'',
  228. 'recipient_mobile'=>isset($package->recipient_mobile)?$package->recipient_mobile:'',
  229. 'logistic_name'=>isset($package->logistic_name)?$package->logistic_name:'',
  230. 'measuringMachine_name'=>isset($package->measuringMachine_name)?$package->measuringMachine_name:'',
  231. 'weight'=>isset($package->weight)?$package->weight:'',
  232. 'length'=>isset($package->length)?$package->length/10:'',
  233. 'width'=>isset($package->width)?$package->width/10:'',
  234. 'height'=>isset($package->height)?$package->height/10:'',
  235. 'bulk'=>isset($package->bulk)?round($package->bulk/10000)/100:'',//立方毫米到立方分米带双小数
  236. 'paperBox_name'=>isset($package->paperBox_name)?$package->paperBox_name:'',
  237. 'status'=>isset($package->status)?$package->status:''
  238. ];
  239. $list[$i]=$w;
  240. }
  241. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-称重记录单.xlsx');
  242. }
  243. protected function validator(Request $request){
  244. $validator=Validator::make($request->input(),[
  245. 'logistic_number'=>['required_without_all:batch_number,order_code'],
  246. 'order_code'=>['required_without_all:logistic_number,batch_number'],
  247. 'weight'=>'required|min:0|max:999999|numeric',
  248. 'paper_box_id'=>'nullable|integer',
  249. 'batch_number'=>['required_without_all:logistic_number,order_code']
  250. ],[
  251. 'required'=>':attribute 为必填项',
  252. 'max'=>':attribute 字符过多或输入值过大',
  253. 'integer'=>':attribute 选择有误',
  254. 'min'=>':attribute 不得为负',
  255. 'numeric'=>':attribute 应为数字',
  256. 'unique'=>':attribute 已存在',
  257. 'required_without_all'=>':attribute 与快递单号或订单号至少选一项'
  258. ],[
  259. 'logistic_number'=>'快递单号',
  260. 'weight'=>'重量',
  261. 'paper_box_id'=>'纸箱',
  262. 'batch_number'=>'波次号',
  263. 'order_code'=>'订单号',
  264. ]);
  265. return $validator;
  266. }
  267. // public function syncBatch($batch_number,$weight,$max,$centre,$min,$date,$paperBox_id){
  268. //// $accomplishToWMS=new Api\thirdPart\flux\PackageController();
  269. //// $packageBatch=Package::where('batch_number',$batch_number)->first();
  270. // $newValues = ['weight' => $weight];
  271. // $newValues['batch_number']=$batch_number;
  272. // if($max)$newValues['length']=$max;
  273. // if($centre)$newValues['width']=$centre;
  274. // if($min)$newValues['height']=$min;
  275. // if($date)$newValues['weighed_at']=$date;
  276. // if($paperBox_id)$newValues['paper_box_id']=$paperBox_id;
  277. // if($max&&$centre&&$min){
  278. // $newValues['bulk']=$max*$centre*$min;
  279. // }
  280. //// $weightChanged=$packageBatch['weight']!=$weight;
  281. //// Package::where('batch_number',$batch_number)->update($newValues);
  282. //// $packageBatch['forceUpload']=$weightChanged;
  283. // Controller::logS(__METHOD__,__FUNCTION__,"批量更新时批次号传入:{$batch_number}");
  284. //// $result=$accomplishToWMS->markWMSOnBatch($packageBatch['batch_number']);
  285. //// if ($result['result']){
  286. //// $newValues['status']='已上传';
  287. //// Controller::logS(__METHOD__,'SUCCESS_'.__FUNCTION__,'批量更改波次上传成功'.json_encode($packageBatch));
  288. //// }else{
  289. //// $newValues['status']='上传异常';
  290. //// Controller::logS(__METHOD__,'error_'.__FUNCTION__,'批量更改波次上传异常:'.$result['message'].json_encode($packageBatch));
  291. //// }picktotraceid
  292. // $packagesInOracle=OracleDOCWaveDetails::where('doc_wave_details.waveno',$batch_number)
  293. // ->leftJoin('act_allocation_details','act_allocation_details.orderno','doc_wave_details.orderno')
  294. // ->get();
  295. // foreach ($packagesInOracle as $packageInOracle) {
  296. // $newValues['logistic_number']=$packageInOracle['picktotraceid'];
  297. // $package = Package::where('logistic_number', $packageInOracle['picktotraceid'])->first();
  298. // if(!$package){
  299. // Package::create($newValues);
  300. // }else{
  301. // $package->fill($newValues);
  302. // $package->update();
  303. // }
  304. // }
  305. //// Package::where('batch_number',$batch_number)->update($newValues);
  306. //
  307. // }
  308. public function statisticExport($packages,$owners,$logistics){
  309. if (!$packages||!$owners||!$logistics) return;
  310. ini_set('max_execution_time',3500);
  311. ini_set('memory_limit','2726M');
  312. $row=[[]];
  313. $row[0]['owner']='货主';
  314. $row[0]['sum']='总计';
  315. foreach ($logistics as $logistic){
  316. $row[0][$logistic->id]=$logistic->name;
  317. }
  318. $ownerArr=[];
  319. foreach ($owners as $owner){
  320. foreach ($packages as $package){
  321. if ($owner->id==$package->owner_id){
  322. $ownerArr[$owner->name][$package->logistic_id]=$package->count;
  323. }
  324. }
  325. }
  326. $list=[];
  327. $i=0;
  328. foreach ($owners as $owner){
  329. if (isset($ownerArr[$owner->name])){
  330. $w['owner']=$owner->name;
  331. $sum=0;
  332. foreach ($logistics as $logistic){
  333. if (isset($ownerArr[$owner->name][$logistic->id])){
  334. $w[$logistic->id]=$ownerArr[$owner->name][$logistic->id];
  335. $sum=$sum+$ownerArr[$owner->name][$logistic->id];
  336. }
  337. if (!isset($ownerArr[$owner->name][$logistic->id])){
  338. $w[$logistic->id]=0;
  339. }
  340. }
  341. $w['sum']=$sum;
  342. $list[$i]=$w;
  343. $i++;
  344. }
  345. }
  346. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-称重统计记录单.xlsx');
  347. }
  348. }