PackageController.php 16 KB

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