| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace App\Imports;
- use App\Http\Controllers\api\thirdPart\jianshang\RejectedController;
- use App\Http\Controllers\Controller;
- use App\Jobs\SyncOrderRejectingStatusJob;
- use App\Logistic;
- use App\Owner;
- use App\Rejected;
- use App\RejectedBill;
- use App\RejectedBillItem;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Session;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\WithHeadingRow;
- use Maatwebsite\Excel\Imports\HeadingRowFormatter;
- HeadingRowFormatter::default('none');
- class RejectedImport implements ToCollection, WithHeadingRow
- {
- /**
- * @param Collection $collections
- */
- public function collection(Collection $collections)
- {
- foreach ($collections as $row)
- {
- $logistic_number_return = trim($row['退回单号']??$row['退货单号']??$row['快递单号']??$row['logistic_number_return']??'');
- $mobile_sender = trim($row['手机号']??$row['mobile_sender']??'');
- $logistic_name = trim($row['快递公司']??$row['logistic_name']??'');
- $barcode = trim($row['条码']??$row['barcode']??'');
- $amount = trim($row['数量']??$row['amount']??'');
- $quality_label_name = trim($row['品质']??$row['quality_label_name']??'');
- $remark = trim($row['备注']??$row['remark']??'');
- $owner=trim($row['货主']??$row['owner']??'');
- $owner=Owner::query()->where('name','like',$owner.'%')
- ->whereNull("deleted_at")->first();
- $id_logistic_return=Logistic::query()->where('name','like',"%{$logistic_name}%")->first()['id'];
- if(!$id_logistic_return){
- $id_logistic_return=Logistic::query()->where('name','like',"%其它%")->first()['id'];
- }
- $id_quality_label=3;
- if(trim($quality_label_name)=='正品'){
- $id_quality_label=1;
- }elseif(trim($quality_label_name)=='残次'){
- $id_quality_label=2;
- }
- if(!$amount)$amount=1;
- $bill=RejectedBill::query()->where('logistic_number_return',trim($logistic_number_return))->first();
- if($bill){
- $item=RejectedBillItem::query()->where('id_rejected_bill',$bill['id'])
- ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
- if($item){
- $item['amount']+=$amount;
- $item->update();
- }else{
- $item=new RejectedBillItem([
- 'id_rejected_bill'=>$bill['id'],
- 'barcode_goods'=>trim($barcode),
- 'amount'=>trim($amount),
- 'id_quality_label'=>$id_quality_label,
- 'remark'=>$remark,
- ]);
- $item->save();
- }
- // $bill->syncOrderIssue();
- }else{
- $bill=new RejectedBill([
- 'id_owner'=>$owner['id'],
- 'mobile_sender'=>$mobile_sender,
- 'logistic_number_return'=>$logistic_number_return,
- 'id_logistic_return'=>$id_logistic_return,
- 'is_loaded'=>0,
- ]);
- $bill->save();
- // $bill->joinOrderIssue();
- // $bill->syncOrderIssue();
- $item=RejectedBillItem::query()->where('id_rejected_bill',$bill['id'])
- ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
- if($item){
- $item['amount']+=$amount;
- $item->update();
- }else{
- $item=new RejectedBillItem([
- 'id_rejected_bill'=>$bill['id'],
- 'barcode_goods'=>trim($barcode),
- 'amount'=>trim($amount),
- 'id_quality_label'=>$id_quality_label,
- 'remark'=>$remark,
- ]);
- $item->save();
- }
- }
- SyncOrderRejectingStatusJob::dispatch($bill);
- $lastBill = Session::get('jianshangLastImportedBill');
- if($owner['name']=='笕尚'&&$lastBill&&$lastBill !=trim($logistic_number_return)){
- $this->submitToApi($bill);
- }
- Session::put('jianshangLastImportedLogisticName',trim($logistic_name));
- Session::put('jianshangLastImportedBill',trim($logistic_number_return));
- Session::put('jianshangLastImportedMobile',trim($mobile_sender));
- }
- }
- private function submitToApi(RejectedBill $bill){
- if(!$bill||$bill->items()->get()->isEmpty())return;
- $havingFail=0;
- if(!config('api.API_FAKING'))
- $bill->items()->get()->each(function(RejectedBillItem $item)use(&$havingFail){
- $rejectedBill=RejectedBill::query()->find($item['id_rejected_bill']);
- if($rejectedBill['is_loaded']!=0){
- return false;
- }
- $owner = Owner::query()->find($rejectedBill['id_owner']);
- if(strstr($owner->name??'','笕尚')){
- $rejected=new Rejected();
- app('LogService')->log(__METHOD__,'找不到表','124行');
- $rejected->fill($rejectedBill->toArray());
- $rejected->fill($item->toArray());
- $rejectedJianshang=new RejectedController();
- $sended=$rejectedJianshang->sendRejected($rejected);
- if(!$sended){
- (new Controller())->log(__METHOD__,'error','数据发送给笕尚失败');
- $havingFail++;
- return false;
- }
- }
- });
- (new Controller())->log(__METHOD__,'error',json_encode(['success'=>'false','failure_info'=>"$havingFail/{$bill->items->count()} 条记录没有发送成功"]));
- }
- }
|