| 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 / 12 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 80
RejectedBillController | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 12 | 1332.00 | 0.00% covered (danger) | 0.00% | 0 / 80
index | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
create | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
store | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
apiGetRecent | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 6
apiStore | 0.00% covered (danger) | 0.00% | 0 / 1 | 42.00 | 0.00% covered (danger) | 0.00% | 0 / 14
apiUpdate | 0.00% covered (danger) | 0.00% | 0 / 1 | 30.00 | 0.00% covered (danger) | 0.00% | 0 / 13
validator | 0.00% covered (danger) | 0.00% | 0 / 1 | 42.00 | 0.00% covered (danger) | 0.00% | 0 / 16
apiSetIsLoadedAll | 0.00% covered (danger) | 0.00% | 0 / 1 | 42.00 | 0.00% covered (danger) | 0.00% | 0 / 13
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 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 9
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
destroy | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 4

1 | <?php
2 |
3 | namespace App\Http\Controllers;
4 |
5 | use App\Logistic;
6 | use App\Owner;
7 | use App\QualityLabel;
8 | use App\Rejected;
9 | use App\RejectedBill;
10 | use Illuminate\Contracts\View\Factory;
11 | use Illuminate\Http\RedirectResponse;
12 | use Illuminate\Http\Request;
13 | use Illuminate\Http\Response;
14 | use Illuminate\Routing\Redirector;
15 | use Illuminate\Support\Facades\Auth;
16 | use Illuminate\Support\Facades\Gate;
17 | use Illuminate\Support\Facades\Session;
18 | use Illuminate\Support\Facades\Validator;
19 | use Illuminate\View\View;
20 | use Ramsey\Uuid\Uuid;
21 |
22 | class RejectedBillController extends Controller
23 | {
24 | /**
25 | * Display a listing of the resource.
26 | *
27 | * @return Response
28 | */
29 | public function index()
30 | {
31 | //
32 | }
33 |
34 | /**
35 | * Show the form for creating a new resource.
36 | *
37 | * @return Response
38 | */
39 | public function create()
40 | {
41 | //
42 | }
43 |
44 | /**
45 | * Store a newly created resource in storage.
46 | *
47 | * @param Request $request
48 | * @return Response
49 | */
50 | public function store(Request $request)
51 | {
52 | //
53 | }
54 | public function apiGetRecent()
55 | {
56 | if(!Gate::allows('退货管理-查询')){
57 | return ['success'=>'false','fail_info'=>'没有权限'];}
58 | $user=Auth::user();
59 | $rejectedBills = RejectedBill::query()->orderByDesc('id')->whereIn('id_owner',
60 | $user?$user->getPermittingOwnerIdsAttribute():[])->limit(20)->get();
61 | return ['success'=>'true','rejectedBills'=>$rejectedBills->toArray()];
62 | }
63 |
64 |
65 | public function apiStore(Request $request)
66 | {
67 | if(!Gate::allows('退货管理-录入')){
68 | return ['success'=>'false','fail_info'=>'没有权限'];
69 | }
70 | $softDeleted=RejectedBill::withTrashed()->where('logistic_number_return',$request->input('logistic_number_return'))->first();
71 | if($softDeleted&&$softDeleted->trashed()){
72 | $softDeleted->forceDelete();
73 | }
74 | if($request['is_loaded']=='null')$request['is_loaded']=null;
75 | $errorFields= $this->validator($request->all())->errors()->getMessages();
76 | if(!empty($errorFields)){
77 | return ['success'=>'false','error_fields'=>$errorFields,'fail_info'=>'fileds error'];
78 | }
79 | $rejectedBill=new RejectedBill($request->all());
80 | $rejectedBill['id_operator'] = Auth::user()['id'];
81 | $rejectedBill->save();
82 |
83 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
84 | return ['success'=>'true','id'=>$rejectedBill['id']];
85 | }
86 |
87 | public function apiUpdate(Request $request)
88 | {
89 | if(!Gate::allows('退货管理-编辑')){
90 | return ['success'=>'false','fail_info'=>'没有权限'];
91 | }
92 | if($request['is_loaded']=='null')$request['is_loaded']=null;
93 | $errorFields= $this->validator($request->all(),true)->errors()->getMessages();
94 | if(!empty($errorFields)){
95 | return ['success'=>'false','error_fields'=>$errorFields,'fail_info'=>'fileds error'];
96 | }
97 | $rejectedBill=RejectedBill::find($request->input('id'));
98 | $rejectedBill->fill($request->all());
99 | $rejectedBill->save();
100 |
101 | if($request->input('is_at_edit_page')==true){
102 | Session::put('rejected.index.isFromEditPage',true);
103 | }
104 |
105 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
106 | return ['success'=>'true'];
107 | }
108 |
109 |
110 | protected function validator(array $data,$isUpdate=false)
111 | {
112 | $logistic_number_return_unique='unique:rejected_bills,logistic_number_return';
113 | if($isUpdate)
114 | $logistic_number_return_unique='';
115 | $mobileShouldBeRequired = 'required';
116 | $logistic=Logistic::find($data['id_logistic_return']);
117 | if($logistic){if(strstr($logistic['name'],'顺丰')){
118 | $mobileShouldBeRequired = 'nullable';
119 | }}
120 | $mobileDigits = '';
121 | if($data['mobile_sender']&&$data['mobile_sender'][0]=='1'){
122 | $mobileDigits = 'digits:11';
123 | }
124 | return Validator::make($data, [
125 | 'id_owner' => ['required', 'numeric', 'exists:owners,id'],
126 | 'order_number' => ['nullable', 'string','max:60'],
127 | 'sender' => ['nullable', 'max:50'],
128 | 'mobile_sender' => [$mobileShouldBeRequired, 'max:20',$mobileDigits],
129 | 'logistic_number' => ['nullable', 'max:60'],
130 | 'logistic_number_return' => ['required','string', 'max:60', $logistic_number_return_unique],
131 | 'id_logistic_return' => ['required', 'numeric', 'exists:logistics,id'],
132 | 'fee_collected' => ['nullable','between:0,99999','numeric'],
133 | 'is_loaded' => ['nullable','boolean'],
134 | ],[
135 | 'mobile_sender.digits'=>'如果是手机则必须为11位',
136 | ],[
137 | 'id_owner'=>'客户名称',
138 | 'order_number'=>'订单号',
139 | 'sender'=>'寄件人',
140 | 'mobile_sender'=>'寄件人手机',
141 | 'logistic_number'=>'原单号',
142 | 'logistic_number_return'=>'退回单号',
143 | 'id_logistic_return'=>'物流公司',
144 | 'fee_collected'=>'到付费用',
145 | 'is_loaded'=>'是否入库',
146 | ]);
147 | }
148 |
149 | public function apiSetIsLoadedAll(Request $request)
150 | {
151 | if(!Gate::allows('退货管理-编辑')){ return redirect(url('/')); }
152 | $ids=$request->input('ids')??'';
153 | $rejecteds=RejectedBill::whereIn('id',$ids)->get();
154 | $is_loaded = $request->input('is_loaded');
155 | switch ($is_loaded){
156 | case '0':$is_loaded=false;break;
157 | case '1':$is_loaded=true;break;
158 | case 'null':$is_loaded=null;break;
159 | }
160 | $rejecteds->each(function($rejected)use($is_loaded){
161 | $rejected['is_loaded']=$is_loaded;
162 | $re=$rejected->update();
163 | if (!$re){
164 | return ['success'=>'false','fail_info'=>"数据 {$rejected['id']} 更新失败"];
165 | }
166 | });
167 | return ['success'=>'true'];
168 | }
169 |
170 | /**
171 | * Display the specified resource.
172 | *
173 | * @param RejectedBill $rejectedBill
174 | * @return Response
175 | */
176 | public function show(RejectedBill $rejectedBill)
177 | {
178 | //
179 | }
180 |
181 | /**
182 | * Show the form for editing the specified resource.
183 | *
184 | * @param RejectedBill $rejectedBill
185 | * @param Request $request
186 | * @return Factory|RedirectResponse|Response|Redirector|View
187 | */
188 | public function edit(RejectedBill $rejectedBill,Request $request)
189 | {
190 | if(!Gate::allows('退货管理-编辑')){ return redirect(url('/')); }
191 | $filterParams=$request->input('filterParams');
192 | Session::put('rejected.index.filterParams',$filterParams);
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 | return view('rejected.edit',compact('owners','logistics','qualityLabels','rejectedBill'));
199 | }
200 |
201 | /**
202 | * Update the specified resource in storage.
203 | *
204 | * @param Request $request
205 | * @param RejectedBill $rejectedBill
206 | * @return Response
207 | */
208 | public function update(Request $request, RejectedBill $rejectedBill)
209 | {
210 | //
211 | }
212 |
213 | public function destroy(RejectedBill $rejectedBill)
214 | {
215 | if(!Gate::allows('退货管理-删除')){ return redirect(url('/'));}
216 | $re=$rejectedBill->delete();
217 | $this->log(__METHOD__,__FUNCTION__,$rejectedBill->toJson(),Auth::user()['id']);
218 | return ['success'=>$re];
219 | }
220 | }