| 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 / 15 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 116
RejectedBillItemController | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 15 | 1640.00 | 0.00% covered (danger) | 0.00% | 0 / 116
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
apiStore | 0.00% covered (danger) | 0.00% | 0 / 1 | 30.00 | 0.00% covered (danger) | 0.00% | 0 / 18
apiUpdate | 0.00% covered (danger) | 0.00% | 0 / 1 | 20.00 | 0.00% covered (danger) | 0.00% | 0 / 13
collectionsToPackConfirm | 0.00% covered (danger) | 0.00% | 0 / 1 | 30.00 | 0.00% covered (danger) | 0.00% | 0 / 26
apiPackConfirm | 0.00% covered (danger) | 0.00% | 0 / 1 | 72.00 | 0.00% covered (danger) | 0.00% | 0 / 26
apiPackDestroy | 0.00% covered (danger) | 0.00% | 0 / 1 | 20.00 | 0.00% covered (danger) | 0.00% | 0 / 9
validator | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 4
apiGet | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 6
apiDelete | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 7
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 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
destroy | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1

1 | <?php
2 |
3 | namespace App\Http\Controllers;
4 |
5 | use App\Commodity;
6 | use App\Owner;
7 | use App\Rejected;
8 | use App\RejectedBill;
9 | use App\RejectedBillItem;
10 | use Illuminate\Http\Request;
11 | use Illuminate\Support\Collection;
12 | use Illuminate\Support\Facades\Auth;
13 | use Illuminate\Support\Facades\Gate;
14 | use Illuminate\Support\Facades\Validator;
15 | use Zttp\Zttp;
16 |
17 | class RejectedBillItemController extends Controller
18 | {
19 | /**
20 | * Display a listing of the resource.
21 | *
22 | * @return \Illuminate\Http\Response
23 | */
24 | public function index()
25 | {
26 | //
27 | }
28 |
29 | /**
30 | * Show the form for creating a new resource.
31 | *
32 | * @return \Illuminate\Http\Response
33 | */
34 | public function create()
35 | {
36 | //
37 | }
38 |
39 | /**
40 | * Store a newly created resource in storage.
41 | *
42 | * @param \Illuminate\Http\Request $request
43 | * @return \Illuminate\Http\Response
44 | */
45 | public function store(Request $request)
46 | {
47 | //
48 | }
49 |
50 | public function apiStore(Request $request)
51 | {
52 | if(!Gate::allows('退货管理-录入')){
53 | return ['success'=>'false','failure_info'=>'没有权限'];
54 | }
55 | $errorFields= $this->validator($request->all())->errors()->getMessages();
56 | if(!empty($errorFields)){
57 | return ['success'=>'false','error_fields'=>$errorFields,'failure_info'=>'fileds error'];
58 | }
59 | $rejectedBillItem=RejectedBillItem::where('barcode_goods',$request->input('barcode_goods'))
60 | ->where('id_quality_label',$request->input('id_quality_label'))
61 | ->where('id_rejected_bill',$request->input('id_rejected_bill'))->first();
62 | if($rejectedBillItem){
63 | $rejectedBillItem['amount']+=$request->input('amount');
64 | if($request->input('name_goods')){
65 | $rejectedBillItem['name_goods']=$request->input('name_goods');
66 | }
67 | $rejectedBillItem->update();
68 | }else{
69 | $rejectedBillItem=new RejectedBillItem($request->all());
70 | $rejectedBillItem->save();
71 | }
72 | $rejectedBillItem->injectCommodityName();
73 |
74 |
75 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
76 | return ['success'=>'true','id'=>$rejectedBillItem['id']];
77 | }
78 | public function apiUpdate(Request $request)
79 | {
80 | if(!Gate::allows('退货管理-编辑')){
81 | return ['success'=>'false','failure_info'=>'没有权限'];
82 | }
83 | $errorFields= $this->validator($request->all())->errors()->getMessages();
84 | if(!empty($errorFields)){
85 | return ['success'=>'false','error_fields'=>$errorFields,'failure_info'=>'fileds error'];
86 | }
87 | $rejectedBillItem=RejectedBillItem::where('id',$request->input('id'))->first();
88 | if(!$rejectedBillItem){
89 | return ['success'=>'false','failure_info'=>'数据库没有相应记录，请刷新后重加'];
90 | }else{
91 | $rejectedBillItem->fill($request->all());
92 | $rejectedBillItem->update();
93 | $rejectedBillItem->injectCommodityName();
94 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
95 | return ['success'=>'true','id'=>$rejectedBillItem['id']];
96 | }
97 |
98 |
99 | }
100 |
101 |
102 | public function collectionsToPackConfirm(Collection $rejectedBillItems)
103 | {
104 | $ids=[];
105 | $rejectedBillItems->each(function ($item)use(&$ids){
106 | $ids[]=$item->id;
107 | });
108 | if(!$ids)return ['success'=>'false','fail_info'=>'没有内容'];
109 | $collections=new Collection($ids);
110 | $i=0;
111 | $collections->each(function ($id)use(&$i,$ids){
112 | $rejectedBillItems=RejectedBillItem::whereIn('id', [$ids[$i]]);
113 | $rejectedBillItems->each(function(RejectedBillItem $rejectedBillItem)use(&$havingFail){
114 | $rejectedBill=RejectedBill::find($rejectedBillItem['id_rejected_bill']);
115 | $owner = Owner::find($rejectedBill['id_owner']);
116 | if(strstr($owner->name??'','笕尚')){
117 | $rejected=new Rejected();
118 | $rejected->fill($rejectedBill->toArray());
119 | $rejected->fill($rejectedBillItem->toArray());
120 | $rejectedJianshang=new api\jianshang\RejectedController();
121 | $sended=$rejectedJianshang->sendRejected($rejected);
122 | if(!$sended){
123 | echo '<h1>数据发送给笕尚失败，请通知IT相关负责人</h1>';
124 | $this->log(__METHOD__,'error','数据发送给笕尚失败');
125 | $havingFail++;
126 | return false;
127 | }
128 | }
129 | });
130 | $i++;
131 | if($i%30==0)
132 | sleep(1);
133 | });
134 |
135 | return 'yes'.$i;
136 | }
137 | public function apiPackConfirm(Request $request)
138 | {
139 | if(!Gate::allows('退货管理-录入')){
140 | return ['success'=>'false','failure_info'=>'没有权限'];
141 | }
142 | $ids=$request->input('ids');
143 | if(!$ids)return ['success'=>'false','fail_info'=>'没有内容'];
144 | $rejectedBillItems=RejectedBillItem::whereIn('id', $ids);
145 |
146 | //TODO: 发送给笕尚，应该单独抽出然后做出可配置流程
147 | $havingFail=0;
148 | if(!config('api.API_FAKING'))
149 | $rejectedBillItems->each(function(RejectedBillItem $rejectedBillItem)use(&$havingFail){
150 | $rejectedBill=RejectedBill::find($rejectedBillItem['id_rejected_bill']);
151 | if($rejectedBill['is_loaded']){
152 | return false;
153 | }
154 | $owner = Owner::find($rejectedBill['id_owner']);
155 | if(strstr($owner->name??'','笕尚')){
156 | $rejected=new Rejected();
157 | $rejected->fill($rejectedBill->toArray());
158 | $rejected->fill($rejectedBillItem->toArray());
159 | $rejectedJianshang=new api\jianshang\RejectedController();
160 | $sended=$rejectedJianshang->sendRejected($rejected);
161 | if(!$sended){
162 | echo '<h1>数据发送给笕尚失败，请通知IT相关负责人</h1>';
163 | $this->log(__METHOD__,'error','数据发送给笕尚失败');
164 | $havingFail++;
165 | return false;
166 | }
167 | }
168 | });
169 |
170 | if($havingFail>0)return ['success'=>'false','failure_info'=>"$havingFail/{$rejectedBillItems->count()} 条记录没有发送成功"];
171 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
172 | return ['success'=>'true'];
173 | }
174 | public function apiPackDestroy(Request $request)
175 | {
176 | if(!(Gate::allows('退货管理-录入')||Gate::allows('退货管理-编辑'))){
177 | return ['success'=>'false','failure_info'=>'没有权限'];
178 | }
179 | $ids=$request->input('ids');
180 | if(!$ids)return ['success'=>'false','fail_info'=>'没有内容'];
181 | $rejectedBillItems=RejectedBillItem::whereIn('id', $ids);
182 |
183 | $rejectedBillItems->each(function(RejectedBillItem $rejectedBillItem){
184 | $rejectedBillItem->delete();
185 | });
186 |
187 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
188 | return ['success'=>'true'];
189 | }
190 |
191 | protected function validator(array $data,$isUpdate=false)
192 | {
193 | return Validator::make($data, [
194 | 'id_rejected_bill' => ['required', 'numeric', 'exists:rejected_bills,id'],
195 | 'barcode_goods' => ['required', 'string','max:60'],
196 | 'name_goods' => ['nullable', 'string','max:50'],
197 | 'amount' => ['numeric','nullable', 'max:99999'],
198 | 'id_quality_label' => ['required','numeric', 'max:11'],
199 | 'batch_number' => ['string', 'nullable',],
200 | 'validity_at' => ['nullable','date_format:Y-m-d'],
201 | 'remark' => ['nullable','string','max:191'],
202 | ],[],[
203 | 'id_rejected_bill' =>'退单头',
204 | 'barcode_goods' => '商品条码',
205 | 'name_goods' => '商品名称',
206 | 'amount' => '数量',
207 | 'id_quality_label' => '品质',
208 | 'batch_number' => '批次',
209 | 'validity_at' => '效期',
210 | 'remark' => '备注'
211 | ]);
212 | }
213 | public function apiGet(Request $request)
214 | {
215 | if(!Gate::allows('退货管理-查询')){
216 | return ['success'=>'false','failure_info'=>'没有权限'];}
217 | if(!$request->input('id_rejected_bill')){
218 | return ['success'=>'false','failure_info'=>'表头id不能为空'];
219 | }
220 | $rejectedBillItems = RejectedBillItem::where('id_rejected_bill',$request->input('id_rejected_bill'))->get();
221 | return ['success'=>'true','items'=>$rejectedBillItems->toArray()];
222 | }
223 | public function apiDelete(Request $request)
224 | {
225 | if(!Gate::allows('退货管理-删除')){
226 | return ['success'=>'false','failure_info'=>'没有权限'];}
227 | if(!$request->input('id')){
228 | return ['success'=>'false','failure_info'=>'传入id不能为空'];
229 | }
230 | $rejectedBillItem = RejectedBillItem::find($request->input('id'));
231 | $rejectedBillItem->forceDelete();
232 | return ['success'=>'true','id'=>$request->input('id')];
233 | }
234 |
235 | /**
236 | * Display the specified resource.
237 | *
238 | * @param \App\RejectedBillItem $rejectedBillItem
239 | * @return \Illuminate\Http\Response
240 | */
241 | public function show(RejectedBillItem $rejectedBillItem)
242 | {
243 | //
244 | }
245 |
246 | /**
247 | * Show the form for editing the specified resource.
248 | *
249 | * @param \App\RejectedBillItem $rejectedBillItem
250 | * @return \Illuminate\Http\Response
251 | */
252 | public function edit(RejectedBillItem $rejectedBillItem)
253 | {
254 | //
255 | }
256 |
257 | /**
258 | * Update the specified resource in storage.
259 | *
260 | * @param \Illuminate\Http\Request $request
261 | * @param \App\RejectedBillItem $rejectedBillItem
262 | * @return \Illuminate\Http\Response
263 | */
264 | public function update(Request $request, RejectedBillItem $rejectedBillItem)
265 | {
266 | //
267 | }
268 |
269 | /**
270 | * Remove the specified resource from storage.
271 | *
272 | * @param \App\RejectedBillItem $rejectedBillItem
273 | * @return \Illuminate\Http\Response
274 | */
275 | public function destroy(RejectedBillItem $rejectedBillItem)
276 | {
277 | //
278 | }
279 | }