| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 20 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 206
RejectedController | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 20 | 4556.00 | 0.00% covered (danger) | 0.00% | 0 / 206
index | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 13
recycle | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 10
apiRecoverSelected | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 6
packFilterParams | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 17
buildFilteredQuery | 0.00% covered (danger) | 0.00% | 0 / 1 | 506.00 | 0.00% covered (danger) | 0.00% | 0 / 61
create | 0.00% covered (danger) | 0.00% | 0 / 1 | 20.00 | 0.00% covered (danger) | 0.00% | 0 / 9
store | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 6
ajaxCheck | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 9
ajaxCheckAll | 0.00% covered (danger) | 0.00% | 0 / 1 | 30.00 | 0.00% covered (danger) | 0.00% | 0 / 15
ajaxFinishAll | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 10
validatorCreate | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 4
validatorUpdate | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
show | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
edit | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 6
destroy | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
exportExcel | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 9
exportExcelOnFilterParams | 0.00% covered (danger) | 0.00% | 0 / 1 | 20.00 | 0.00% covered (danger) | 0.00% | 0 / 14
import | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
importExcel | 0.00% covered (danger) | 0.00% | 0 / 1 | 20.00 | 0.00% covered (danger) | 0.00% | 0 / 11

1 | <?php
2 |
3 | namespace App\Http\Controllers;
4 |
5 | use App\Exports\RejectedExport;
6 | use App\Imports\CommodityImport;
7 | use App\Imports\RejectedImport;
8 | use App\Logistic;
9 | use App\Owner;
10 | use App\QualityLabel;
11 | use App\Rejected;
12 | use App\RejectedBill;
13 | use App\RejectedBillItem;
14 | use Carbon\Carbon;
15 | use Exception;
16 | use Illuminate\Contracts\View\Factory;
17 | use Illuminate\Http\RedirectResponse;
18 | use Illuminate\Http\Request;
19 | use Illuminate\Http\Response;
20 | use Illuminate\Support\Facades\Auth;
21 | use Illuminate\Support\Facades\Gate;
22 | use Illuminate\Support\Facades\Session;
23 | use Illuminate\Support\Facades\Validator;
24 | use Illuminate\View\View;
25 | use Maatwebsite\Excel\Facades\Excel;
26 | use function foo\func;
27 |
28 | class RejectedController extends Controller
29 | {
30 | /**
31 | * Display a listing of the resource.
32 | *
33 | * @return Factory|View|RedirectResponse
34 | */
35 | public function index(Request $request)
36 | {
37 | if(!Gate::allows('退货管理-查询')){ return redirect(url('/')); }
38 | $user=Auth::user();
39 | $paginate = $request->input('paginate')??50;
40 | $paginateParams = $this->packFilterParams($request);
41 | $page =$paginateParams['page'] ?? $request->input('page') ?? 1;
42 | $paginateParams['paginate']=$paginate;
43 | $rejectedQuery=$this->buildFilteredQuery($paginateParams)->orderBy('id','desc')->whereIn('id_owner',
44 | $user?$user->getPermittingOwnerIdsAttribute():[]);
45 | $rejectedBills=$rejectedQuery->paginate($paginate, ['*'], 'page', $page);
46 | $owners = Owner::filterAuthorities()->get();
47 | $qualityLabels = QualityLabel::all();
48 | return view('rejected.index',compact('rejectedBills','owners',
49 | 'paginateParams','qualityLabels'));
50 | }
51 | public function recycle(Request $request)
52 | {
53 | if(!Gate::allows('退货管理-删除')){ return redirect(url('/')); }
54 | $user=Auth::user();
55 | $paginate = $request->input('paginate')??50;
56 | $paginateParams = $this->packFilterParams($request);
57 | $paginateParams['paginate']=$paginate;
58 | $rejectedQuery=$this->buildFilteredQuery($paginateParams)->orderBy('id','desc')->whereIn('id_owner',
59 | $user?$user->getPermittingOwnerIdsAttribute():[])->withTrashed()->whereNotNull('deleted_at');
60 | $total = $rejectedQuery->count();
61 | $rejectedBills=$rejectedQuery->paginate($paginate);
62 | return view('rejected.recycle',compact('rejectedBills','total','paginateParams'));
63 | }
64 | public function apiRecoverSelected(Request $request)
65 | {
66 | if(!Gate::allows('退货管理-删除')){ return ['success'=>'false','fail_info'=>"没有权限"]; }
67 | $ids=$request->input('ids')??'';
68 | $rejecteds=RejectedBill::withTrashed()->whereIn('id',$ids)->get();
69 | $rejecteds->each(function(RejectedBill $rejected){
70 | $rejected->restore();
71 | });
72 | return ['success'=>'true','rejecteds'=>$rejecteds];
73 | }
74 | private function packFilterParams(Request $request){
75 | $paginateParams=[
76 | 'page'=> $request->input('page'),
77 | 'created_at_start'=> $request->input('created_at_start'),
78 | 'created_at_end'=> $request->input('created_at_end'),
79 | 'owner_id'=> $request->input('owner_id'),
80 | 'id_quality_label'=> $request->input('id_quality_label'),
81 | 'is_checked'=> $request->input('is_checked'),
82 | 'is_loaded'=> $request->input('is_loaded'),
83 | 'mobile_sender'=> $request->input('mobile_sender'),
84 | 'checked_numbers'=> $request->input('checked_numbers'),
85 | 'order_number'=> $request->input('order_number'),
86 | 'barcode_goods'=> $request->input('barcode_goods'),
87 | 'logistic_number_return'=> $request->input('logistic_number_return'),
88 | ];
89 |
90 | if(Session::get('rejected.index.isFromEditPage')){
91 | $paginateParams=json_decode(Session::get('rejected.index.filterParams'),true);
92 | Session::forget('rejected.index.isFromEditPage');
93 | Session::forget('rejected.index.filterParams');
94 | }
95 | return $paginateParams;
96 | }
97 | private function buildFilteredQuery(Array $params){
98 | $created_at_start = $params['created_at_start'];
99 | $created_at_end = $params['created_at_end'];
100 | $owner_id = $params['owner_id'];
101 | $id_quality_label = $params['id_quality_label'];
102 | $is_checked = $params['is_checked'];
103 | $is_loaded = $params['is_loaded'];
104 | $mobile_sender = $params['mobile_sender'];
105 | $checked_numbers = $params['checked_numbers'];
106 | $order_number = $params['order_number'];
107 | $barcode_goods = $params['barcode_goods'];
108 | $logistic_number_return = $params['logistic_number_return'];
109 | $rejectedBillsQuery=RejectedBill::with(['owner','logistic','items',]);
110 | if($created_at_start&&!$created_at_end){
111 | $created_at_end = $created_at_start;
112 | }
113 | if(!$created_at_start&&$created_at_end){
114 | $created_at_start = $created_at_end;
115 | }
116 | if($created_at_end){
117 | $created_at_end.=' 23:59:59';
118 | }
119 | if($created_at_start&&$created_at_end){
120 | if($created_at_start==$created_at_end){
121 | $rejectedBillsQuery=$rejectedBillsQuery->where('created_at','like',$created_at_start.'%');
122 | }else
123 | $rejectedBillsQuery=$rejectedBillsQuery->whereBetween('created_at',[$created_at_start,$created_at_end]);
124 | }
125 | if($owner_id){
126 | $rejectedBillsQuery=$rejectedBillsQuery->where('id_owner',$owner_id);
127 | }
128 | if($id_quality_label!=''){
129 | if($id_quality_label==1){
130 | $rejectedBillsQuery->whereDoesntHave('items',function ($query){
131 | $query->where('id_quality_label',2);
132 | });
133 | }else{
134 | $rejectedBillsQuery->whereHas('items',function ($query){
135 | $query->where('id_quality_label',2);
136 | });
137 | }
138 | }
139 | if($is_checked!=''){
140 | $orderSign = false;
141 | if($is_checked){
142 | $orderSign = true;
143 | }
144 | $rejectedBillsQuery=$rejectedBillsQuery->where('is_checked', $orderSign);
145 | }
146 | if($is_loaded!=''){
147 | $rejectedBillsQuery=$rejectedBillsQuery->where('is_loaded', $is_loaded);
148 | }
149 | if($mobile_sender!=''){
150 | $rejectedBillsQuery=$rejectedBillsQuery->where('mobile_sender', trim($mobile_sender));
151 | }
152 | if($checked_numbers!=''){
153 | $rejectedBillsQuery=$rejectedBillsQuery->where('checked_numbers','like', trim($checked_numbers).'%');
154 | }
155 | if($barcode_goods!=''){
156 | $rejectedBillsQuery->whereHas('items',function ($query)use($barcode_goods){
157 | $query->where('barcode_goods','like',trim($barcode_goods).'%');
158 | });
159 | }
160 | if($order_number){
161 | $today = Carbon::now()->subDays(15);
162 | $rejectedBillsQueryTem=clone $rejectedBillsQuery;
163 | $rejectedBillsQueryTem=$rejectedBillsQueryTem->where('created_at','>',$today->format('Y-m-d'));
164 | $rejectedBillsQueryTem=$rejectedBillsQueryTem->where('order_number','like','%'.$order_number.'%');
165 | if($rejectedBillsQueryTem->count()==0){
166 | $rejectedBillsQuery=$rejectedBillsQuery->where('order_number','like','%'.$order_number.'%');
167 | }else{
168 | $rejectedBillsQuery=$rejectedBillsQueryTem;
169 | }
170 | }
171 | if($logistic_number_return){
172 | $today = Carbon::now()->subDays(15);
173 | $rejectedBillsQueryTem=clone $rejectedBillsQuery;
174 | $rejectedBillsQueryTem=$rejectedBillsQueryTem->where('created_at','>',$today->format('Y-m-d'));
175 | $rejectedBillsQueryTem=$rejectedBillsQueryTem->where('logistic_number_return','like','%'.$logistic_number_return.'%');
176 | if($rejectedBillsQueryTem->count()==0){
177 | $rejectedBillsQuery=$rejectedBillsQuery->where('logistic_number_return','like','%'.$logistic_number_return.'%');
178 | }else{
179 | $rejectedBillsQuery=$rejectedBillsQueryTem;
180 | }
181 | }
182 | return $rejectedBillsQuery;
183 | }
184 |
185 | /**
186 | * Show the form for creating a new resource.
187 | *
188 | * @return Response
189 | */
190 | public function create()
191 | {
192 | if(!Gate::allows('退货管理-录入')){ return redirect(url('/'));}
193 | $user=Auth::user();
194 | $owners=Owner::all()->whereIn('id',
195 | $user?$user->getPermittingOwnerIdsAttribute():[])->sortBy('name');
196 | $logistics=Logistic::all()->sortBy('name');
197 | $qualityLabels=QualityLabel::all()->sortBy('name');
198 | $rejectedBills = RejectedBill::query()->orderByDesc('id')->whereIn('id_owner',
199 | $user?$user->getPermittingOwnerIdsAttribute():[])->limit(20)->get();
200 | return view('rejected.create',compact('owners','logistics','qualityLabels','rejectedBills'));
201 | }
202 |
203 | /**
204 | * Store a newly created resource in storage.
205 | *
206 | * @param Request $request
207 | * @return Response
208 | */
209 | public function store(Request $request)
210 | {
211 | if(!Gate::allows('退货管理-录入')){ return redirect(url('/')); }
212 | $this->validatorCreate($request->all())->validate();
213 | $rejected=new Rejected($request->all());
214 | $rejected->save();
215 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
216 | return redirect('rejected/create')->with('successTip',"成功录入退货信息（退单：{$request->input('logistic_number_return')}）");
217 | }
218 | public function ajaxCheck(Request $request)
219 | {
220 | if(!Gate::allows('退货管理-审核')){ return ['success'=>'false','fail_info'=>"没有权限"]; }
221 | $rejected=RejectedBill::find($request->input('id')??'');
222 | $rejected['is_checked']=true;
223 | $rejected->makeCheckedNumbers();
224 | $re=$rejected->update();
225 | if ($re){
226 | return ['success'=>'true','checked_numbers'=>$rejected['checked_numbers']];
227 | }
228 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
229 | return ['success'=>'false','fail_info'=>'数据更新失败'];
230 | }
231 | public function ajaxCheckAll(Request $request)
232 | {
233 | if(!Gate::allows('退货管理-审核')){ return ['success'=>'false','fail_info'=>"没有权限"]; }
234 | $ids=$request->input('ids')??'';
235 | $rejecteds=RejectedBill::whereIn('id',$ids)->get();
236 | $checkNumbersArr=[];
237 | $rejecteds->each(function($rejected)use(&$checkNumbersArr){
238 | if($rejected['is_checked'])return true;
239 | $rejected['is_checked']=true;
240 | if(isset($checkNumbersArr[$rejected->owner->id])){
241 | $rejected['checked_numbers']=$checkNumbersArr[$rejected->owner->id];
242 | }else{
243 | $checkNumbersArr[$rejected->owner->id]=$rejected->makeCheckedNumbers();
244 | }
245 | $re=$rejected->update();
246 | if (!$re){
247 | return ['success'=>'false','fail_info'=>"数据 {$rejected['id']} 更新失败"];
248 | }
249 | });
250 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
251 | return ['success'=>'true','rejecteds'=>$rejecteds];
252 | }
253 | public function ajaxFinishAll(Request $request)
254 | {
255 | if(!Gate::allows('退货管理-编辑')){return ['success'=>'false','fail_info'=>"没有权限"]; }
256 | $ids=$request->input('ids')??'';
257 | $rejecteds=RejectedBill::whereIn('id',$ids)->get();
258 | $rejecteds->each(function($rejected){
259 | $rejected['is_finished']=true;
260 | $re=$rejected->update();
261 | if (!$re){
262 | return ['success'=>'false','fail_info'=>"数据 {$rejected['id']} 更新失败"];
263 | }
264 | });
265 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
266 | return ['success'=>'true'];
267 | }
268 | protected function validatorCreate(array $data)
269 | {
270 | return Validator::make($data, [
271 | 'id_owner' => ['required', 'numeric', 'exists:owners,id'],
272 | 'order_number' => ['nullable', 'string','max:60'],
273 | 'sender' => ['nullable', 'max:50'],
274 | 'mobile_sender' => ['nullable', 'max:20'],
275 | 'logistic_number' => ['nullable', 'max:60'],
276 | 'logistic_number_return' => ['required','string', 'max:60'],
277 | 'id_logistic_return' => ['required', 'numeric', 'exists:logistics,id'],
278 | 'fee_collected' => ['nullable','numeric'],
279 | 'is_loaded' => ['required','boolean'],
280 | 'barcode_goods' => ['nullable','string', 'max:60'],
281 | 'name_goods' => ['nullable','string', 'max:50'],
282 | 'amount' => ['required','numeric'],
283 | 'id_quality_label' => ['required', 'numeric', 'exists:quality_labels,id'],
284 | 'batch_number' => ['nullable', 'string', 'max:60'],
285 | 'validity_at' => ['nullable', 'date'],
286 | 'remark' => ['nullable', 'string', 'max:60'],
287 | ],[],[
288 |
289 | ]);
290 | }
291 | protected function validatorUpdate(array $data)
292 | {
293 | return Validator::make($data, [
294 | // 'name' => ['required', 'string', 'max:50'],
295 | ]);
296 | }
297 |
298 | /**
299 | * Display the specified resource.
300 | *
301 | * @param Rejected $rejected
302 | * @return void
303 | */
304 | public function show(Rejected $rejected)
305 | {
306 | //
307 | }
308 |
309 | /**
310 | * Show the form for editing the specified resource.
311 | *
312 | * @param Rejected $rejected
313 | * @return Response
314 | */
315 | public function edit(RejectedBill $rejectedBill)
316 | {
317 | }
318 |
319 | /**
320 | * Update the specified resource in storage.
321 | *
322 | * @param Request $request
323 | * @param Rejected $rejected
324 | * @return Response
325 | */
326 | public function update(Request $request, Rejected $rejected)
327 | {
328 | if(!Gate::allows('退货管理-编辑')){ return redirect(url('/')); }
329 | $this->validatorUpdate($request->all())->validate();
330 | $rejected->fill($request->all());
331 | $rejected->update();
332 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
333 | return redirect('rejected/')->with('successTip',"成功修改退货信息“{$rejected['name']}”！");
334 | }
335 |
336 | /**
337 | * Remove the specified resource from storage.
338 | *
339 | * @param Rejected $rejected
340 | * @return array|Response
341 | * @throws Exception
342 | */
343 | public function destroy(Rejected $rejected)
344 | {
345 | // if(!Gate::allows('退货管理-删除')){ return redirect(url('/'));}
346 | // $this->log(__METHOD__,__FUNCTION__,$rejectedBill->toJson(),Auth::user()['id']);
347 | // $items=$rejectedBill->items();
348 | // $items->delete();
349 | // $this->log(__METHOD__,__FUNCTION__,$items->toJson(),Auth::user()['id']);
350 | // $re=$rejectedBill->delete();
351 | // return ['success'=>$re];
352 | }
353 | public function exportExcel(Request $request)
354 | {
355 | if(!Gate::allows('退货管理-查询')){ return '没有权限'; }
356 | ini_set('max_execution_time',2100);
357 | ini_set('memory_limit','512M');
358 | $ids=$request->input('ids')??'';
359 | $ids=json_decode($ids,true);
360 | $export=new RejectedExport();
361 | $export->setIds($ids);
362 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
363 | return Excel::download($export,'退货表'.Carbon::now().'.xlsx');
364 | }
365 | public function exportExcelOnFilterParams(Request $request)
366 | {
367 | if(!Gate::allows('退货管理-查询')){ return '没有权限'; }
368 | ini_set('max_execution_time',2100);
369 | ini_set('memory_limit','512M');
370 | $filterParams=json_decode($request->input('filterParams'),true);
371 | $user=Auth::user();
372 | $rejectedBills=$this->buildFilteredQuery($filterParams)->orderBy('id','desc')->whereIn('id_owner',
373 | $user?$user->getPermittingOwnerIdsAttribute():[]);
374 | if($rejectedBills->count()>RejectedExport::$max){
375 | $rejectedBills->where('is_finished',false);
376 | }
377 | $rejectedBills->get();
378 | $export=new RejectedExport();
379 | $export->setRejectedBills($rejectedBills);
380 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
381 | return Excel::download($export,'退货表'.Carbon::now().'.xlsx');
382 |
383 | }
384 | public function import(Request $request)
385 | {
386 | return view('rejected.import');
387 | }
388 | public function importExcel(Request $request)
389 | {
390 | $isOverride = $request->input('isOverride');
391 | try{
392 | ini_set('max_execution_time',2100);
393 | ini_set('memory_limit','512M');
394 | $extension=$request->file()['file']->getClientOriginalExtension();
395 | $extension[0] = strtoupper($extension[0]);
396 | Excel::import(new RejectedImport(), $request->file()['file']->path(),null,$extension);
397 | return '<h1 class="text-success">导入成功</h1>';
398 | }catch (Exception $e){
399 | if(strstr($e->getMessage(),'No ReaderType')){return '<h1 class="text-danger">没有上传写权限，请修改php.ini 对应的upload_tmp_dir 目录或其权限</h1>'.$e->getMessage();}
400 | if(strstr($e->getMessage(),'SQLSTATE')){return '<h1 class="text-danger">数据库插入错误，数据不支持，可能有重复或异常字符</h1>'.$e->getMessage();}
401 | return '<h1 class="text-danger">失败</h1>'.$e->getMessage();
402 | }
403 | }
404 | }