RejectedImport.php 6.3 KB

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