RejectedImport.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Controllers\api\thirdPart\jianshang\RejectedController;
  4. use App\Http\Controllers\Controller;
  5. use App\Jobs\SyncOrderRejectingStatusJob;
  6. use App\Logistic;
  7. use App\Owner;
  8. use App\Rejected;
  9. use App\RejectedBill;
  10. use App\RejectedBillItem;
  11. use Illuminate\Support\Collection;
  12. use Illuminate\Support\Facades\Session;
  13. use Maatwebsite\Excel\Concerns\ToCollection;
  14. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  15. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  16. HeadingRowFormatter::default('none');
  17. class RejectedImport implements ToCollection, WithHeadingRow
  18. {
  19. /**
  20. * @param Collection $collections
  21. */
  22. public function collection(Collection $collections)
  23. {
  24. foreach ($collections as $row)
  25. {
  26. $logistic_number_return = trim($row['退回单号']??$row['退货单号']??$row['快递单号']??$row['logistic_number_return']??'');
  27. $mobile_sender = trim($row['手机号']??$row['mobile_sender']??'');
  28. $logistic_name = trim($row['快递公司']??$row['logistic_name']??'');
  29. $barcode = trim($row['条码']??$row['barcode']??'');
  30. $amount = trim($row['数量']??$row['amount']??'');
  31. $quality_label_name = trim($row['品质']??$row['quality_label_name']??'');
  32. $remark = trim($row['备注']??$row['remark']??'');
  33. $owner=trim($row['货主']??$row['owner']??'');
  34. $owner=Owner::query()->where('name','like',$owner.'%')
  35. ->whereNull("deleted_at")->first();
  36. $id_logistic_return=Logistic::query()->where('name','like',"%{$logistic_name}%")->first()['id'];
  37. if(!$id_logistic_return){
  38. $id_logistic_return=Logistic::query()->where('name','like',"%其它%")->first()['id'];
  39. }
  40. $id_quality_label=3;
  41. if(trim($quality_label_name)=='正品'){
  42. $id_quality_label=1;
  43. }elseif(trim($quality_label_name)=='残次'){
  44. $id_quality_label=2;
  45. }
  46. if(!$amount)$amount=1;
  47. $bill=RejectedBill::query()->where('logistic_number_return',trim($logistic_number_return))->first();
  48. if($bill){
  49. $item=RejectedBillItem::query()->where('id_rejected_bill',$bill['id'])
  50. ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
  51. if($item){
  52. $item['amount']+=$amount;
  53. $item->update();
  54. }else{
  55. $item=new RejectedBillItem([
  56. 'id_rejected_bill'=>$bill['id'],
  57. 'barcode_goods'=>trim($barcode),
  58. 'amount'=>trim($amount),
  59. 'id_quality_label'=>$id_quality_label,
  60. 'remark'=>$remark,
  61. ]);
  62. $item->save();
  63. }
  64. // $bill->syncOrderIssue();
  65. }else{
  66. $bill=new RejectedBill([
  67. 'id_owner'=>$owner['id'],
  68. 'mobile_sender'=>$mobile_sender,
  69. 'logistic_number_return'=>$logistic_number_return,
  70. 'id_logistic_return'=>$id_logistic_return,
  71. 'is_loaded'=>0,
  72. ]);
  73. $bill->save();
  74. // $bill->joinOrderIssue();
  75. // $bill->syncOrderIssue();
  76. $item=RejectedBillItem::query()->where('id_rejected_bill',$bill['id'])
  77. ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
  78. if($item){
  79. $item['amount']+=$amount;
  80. $item->update();
  81. }else{
  82. $item=new RejectedBillItem([
  83. 'id_rejected_bill'=>$bill['id'],
  84. 'barcode_goods'=>trim($barcode),
  85. 'amount'=>trim($amount),
  86. 'id_quality_label'=>$id_quality_label,
  87. 'remark'=>$remark,
  88. ]);
  89. $item->save();
  90. }
  91. }
  92. SyncOrderRejectingStatusJob::dispatch($bill);
  93. $lastBill = Session::get('jianshangLastImportedBill');
  94. if($owner['name']=='笕尚'&&$lastBill&&$lastBill !=trim($logistic_number_return)){
  95. $this->submitToApi($bill);
  96. }
  97. Session::put('jianshangLastImportedLogisticName',trim($logistic_name));
  98. Session::put('jianshangLastImportedBill',trim($logistic_number_return));
  99. Session::put('jianshangLastImportedMobile',trim($mobile_sender));
  100. }
  101. }
  102. private function submitToApi(RejectedBill $bill){
  103. if(!$bill||$bill->items()->get()->isEmpty())return;
  104. $havingFail=0;
  105. if(!config('api.API_FAKING'))
  106. $bill->items()->get()->each(function(RejectedBillItem $item)use(&$havingFail){
  107. $rejectedBill=RejectedBill::query()->find($item['id_rejected_bill']);
  108. if($rejectedBill['is_loaded']!=0){
  109. return false;
  110. }
  111. $owner = Owner::query()->find($rejectedBill['id_owner']);
  112. if(strstr($owner->name??'','笕尚')){
  113. $rejected=new Rejected();
  114. app('LogService')->log(__METHOD__,'找不到表','124行');
  115. $rejected->fill($rejectedBill->toArray());
  116. $rejected->fill($item->toArray());
  117. $rejectedJianshang=new RejectedController();
  118. $sended=$rejectedJianshang->sendRejected($rejected);
  119. if(!$sended){
  120. (new Controller())->log(__METHOD__,'error','数据发送给笕尚失败');
  121. $havingFail++;
  122. return false;
  123. }
  124. }
  125. });
  126. (new Controller())->log(__METHOD__,'error',json_encode(['success'=>'false','failure_info'=>"$havingFail/{$bill->items->count()} 条记录没有发送成功"]));
  127. }
  128. }