PackageController.php 14 KB

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