RejectedImport.php 6.4 KB

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