RejectedImport.php 5.9 KB

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