| 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 / 2 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 84
RejectedImport | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 2 | 462.00 | 0.00% covered (danger) | 0.00% | 0 / 84
collection | 0.00% covered (danger) | 0.00% | 0 / 1 | 210.00 | 0.00% covered (danger) | 0.00% | 0 / 64
submitToApi | 0.00% covered (danger) | 0.00% | 0 / 1 | 56.00 | 0.00% covered (danger) | 0.00% | 0 / 20

1 | <?php
2 |
3 | namespace App\Imports;
4 |
5 | use App\Commodity;
6 | use App\Http\Controllers\api\jianshang\RejectedController;
7 | use App\Http\Controllers\Controller;
8 | use App\Logistic;
9 | use App\Owner;
10 | use App\Rejected;
11 | use App\RejectedBill;
12 | use App\RejectedBillItem;
13 | use Illuminate\Support\Collection;
14 | use Illuminate\Support\Facades\Auth;
15 | use Illuminate\Support\Facades\Session;
16 | use Maatwebsite\Excel\Concerns\ToCollection;
17 | use Maatwebsite\Excel\Concerns\WithHeadingRow;
18 |
19 | class RejectedImport implements ToCollection, WithHeadingRow
20 | {
21 | // protected $isOverride=false;
22 | // public function __construct($isOverride)
23 | // {
24 | // if($isOverride=='1')
25 | // $this->isOverride=true;
26 | // }
27 |
28 | /**
29 | * @param Collection $collections
30 | */
31 | public function collection(Collection $collections)
32 | {
33 | foreach ($collections as $row)
34 | {
35 | $logistic_number_return = trim($row['logistic_number_return']);
36 | $mobile_sender = trim($row['mobile_sender']);
37 | $logistic_name = trim($row['logistic_name']);
38 | $barcode = trim($row['barcode']);
39 | $amount = trim($row['amount']);
40 | $quality_label_name = trim($row['quality_label_name']);
41 | $remark = trim($row['remark']);
42 | $id_owner=Session::get('jianshangIdOwner');
43 |
44 |
45 | $lastReturnNumber = Session::get('jianshangLastImportedBill');
46 | if(!$logistic_number_return)$logistic_number_return=$lastReturnNumber;
47 | $lastMobile = Session::get('jianshangLastImportedMobile');
48 | if(!$mobile_sender)$mobile_sender=$lastMobile;
49 | $lastlogistic_name = Session::get('jianshangLastImportedLogisticName');
50 | if(!$logistic_name)$logistic_name=$lastlogistic_name;
51 |
52 |
53 | if(!$id_owner){
54 | $id_owner=Owner::where('name','like','%笕尚%')->first()['id'];
55 | Session::put('jianshangIdOwner',$id_owner);
56 | }
57 | $id_logistic_return=Logistic::where('name','like',"%{$logistic_name}%")->first()['id'];
58 | if(!$id_logistic_return){
59 | $id_logistic_return=Logistic::where('name','like',"%其它%")->first()['id'];
60 | }
61 | $id_quality_label=1;
62 | if(trim($quality_label_name)!='良品'){
63 | $id_quality_label=2;
64 | }
65 | if(!$amount)$amount=1;
66 |
67 |
68 | $bill=RejectedBill::where('logistic_number_return',trim($logistic_number_return))->first();
69 | if($bill){
70 | $item=RejectedBillItem::where('id_rejected_bill',$bill['id'])
71 | ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
72 | if($item){
73 | $item['amount']+=$amount;
74 | $item->update();
75 | }else{
76 | $item=new RejectedBillItem([
77 | 'id_rejected_bill'=>$bill['id'],
78 | 'barcode_goods'=>trim($barcode),
79 | 'amount'=>trim($amount),
80 | 'id_quality_label'=>$id_quality_label,
81 | 'remark'=>$remark,
82 | ]);
83 | $item->save();
84 | }
85 | }else{
86 | $bill=new RejectedBill([
87 | 'id_owner'=>$id_owner,
88 | 'mobile_sender'=>$mobile_sender,
89 | 'logistic_number_return'=>$logistic_number_return,
90 | 'id_logistic_return'=>$id_logistic_return,
91 | 'is_loaded'=>false,
92 | ]);
93 | $bill->save();
94 | $item=RejectedBillItem::where('id_rejected_bill',$bill['id'])
95 | ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
96 | if($item){
97 | $item['amount']+=$amount;
98 | $item->update();
99 | }else{
100 | $item=new RejectedBillItem([
101 | 'id_rejected_bill'=>$bill['id'],
102 | 'barcode_goods'=>trim($barcode),
103 | 'amount'=>trim($amount),
104 | 'id_quality_label'=>$id_quality_label,
105 | 'remark'=>$remark,
106 | ]);
107 | $item->save();
108 | }
109 | }
110 |
111 | $lastBill = Session::get('jianshangLastImportedBill');
112 | if($lastBill&&$lastBill !=trim($logistic_number_return)){
113 | $this->submitToApi($bill);
114 | }
115 | Session::put('jianshangLastImportedLogisticName',trim($logistic_name));
116 | Session::put('jianshangLastImportedBill',trim($logistic_number_return));
117 | Session::put('jianshangLastImportedMobile',trim($mobile_sender));
118 | }
119 | }
120 | private function submitToApi(RejectedBill $bill){
121 | if(!$bill||$bill->items()->get()->isEmpty())return;
122 | $havingFail=0;
123 | if(!config('api.API_FAKING'))
124 | $bill->items()->get()->each(function(RejectedBillItem $item)use(&$havingFail){
125 | $rejectedBill=RejectedBill::find($item['id_rejected_bill']);
126 | if($rejectedBill['is_loaded']){
127 | return false;
128 | }
129 | $owner = Owner::find($rejectedBill['id_owner']);
130 | if(strstr($owner->name??'','笕尚')){
131 | $rejected=new Rejected();
132 | $rejected->fill($rejectedBill->toArray());
133 | $rejected->fill($item->toArray());
134 | $rejectedJianshang=new RejectedController();
135 | $sended=$rejectedJianshang->sendRejected($rejected);
136 | if(!$sended){
137 | (new Controller())->log(__METHOD__,'error','数据发送给笕尚失败');
138 | $havingFail++;
139 | return false;
140 | }
141 | }
142 | });
143 |
144 | (new Controller())->log(__METHOD__,'error',json_encode(['success'=>'false','failure_info'=>"$havingFail/{$bill->items->count()} 条记录没有发送成功"]));
145 | }
146 | }