RejectedImport.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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['logistic_number_return']);
  30. $mobile_sender = trim($row['mobile_sender']);
  31. $logistic_name = trim($row['logistic_name']);
  32. $barcode = trim($row['barcode']);
  33. $amount = trim($row['amount']);
  34. $quality_label_name = trim($row['quality_label_name']);
  35. $remark = trim($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=1;
  52. if(trim($quality_label_name)!='良品'){
  53. $id_quality_label=2;
  54. }
  55. if(!$amount)$amount=1;
  56. $bill=RejectedBill::where('logistic_number_return',trim($logistic_number_return))->first();
  57. if($bill){
  58. $item=RejectedBillItem::where('id_rejected_bill',$bill['id'])
  59. ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
  60. if($item){
  61. $item['amount']+=$amount;
  62. $item->update();
  63. }else{
  64. $item=new RejectedBillItem([
  65. 'id_rejected_bill'=>$bill['id'],
  66. 'barcode_goods'=>trim($barcode),
  67. 'amount'=>trim($amount),
  68. 'id_quality_label'=>$id_quality_label,
  69. 'remark'=>$remark,
  70. ]);
  71. $item->save();
  72. }
  73. }else{
  74. $bill=new RejectedBill([
  75. 'id_owner'=>$id_owner,
  76. 'mobile_sender'=>$mobile_sender,
  77. 'logistic_number_return'=>$logistic_number_return,
  78. 'id_logistic_return'=>$id_logistic_return,
  79. 'is_loaded'=>0,
  80. ]);
  81. $bill->save();
  82. $item=RejectedBillItem::where('id_rejected_bill',$bill['id'])
  83. ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
  84. if($item){
  85. $item['amount']+=$amount;
  86. $item->update();
  87. }else{
  88. $item=new RejectedBillItem([
  89. 'id_rejected_bill'=>$bill['id'],
  90. 'barcode_goods'=>trim($barcode),
  91. 'amount'=>trim($amount),
  92. 'id_quality_label'=>$id_quality_label,
  93. 'remark'=>$remark,
  94. ]);
  95. $item->save();
  96. }
  97. }
  98. $lastBill = Session::get('jianshangLastImportedBill');
  99. if($lastBill&&$lastBill !=trim($logistic_number_return)){
  100. $this->submitToApi($bill);
  101. }
  102. Session::put('jianshangLastImportedLogisticName',trim($logistic_name));
  103. Session::put('jianshangLastImportedBill',trim($logistic_number_return));
  104. Session::put('jianshangLastImportedMobile',trim($mobile_sender));
  105. }
  106. }
  107. private function submitToApi(RejectedBill $bill){
  108. if(!$bill||$bill->items()->get()->isEmpty())return;
  109. $havingFail=0;
  110. if(!config('api.API_FAKING'))
  111. $bill->items()->get()->each(function(RejectedBillItem $item)use(&$havingFail){
  112. $rejectedBill=RejectedBill::find($item['id_rejected_bill']);
  113. if($rejectedBill['is_loaded']!=0){
  114. return false;
  115. }
  116. $owner = Owner::find($rejectedBill['id_owner']);
  117. if(strstr($owner->name??'','笕尚')){
  118. $rejected=new Rejected();
  119. $this->log(__METHOD__,'找不到表','132行');
  120. $rejected->fill($rejectedBill->toArray());
  121. $rejected->fill($item->toArray());
  122. $rejectedJianshang=new RejectedController();
  123. $sended=$rejectedJianshang->sendRejected($rejected);
  124. if(!$sended){
  125. (new Controller())->log(__METHOD__,'error','数据发送给笕尚失败');
  126. $havingFail++;
  127. return false;
  128. }
  129. }
  130. });
  131. (new Controller())->log(__METHOD__,'error',json_encode(['success'=>'false','failure_info'=>"$havingFail/{$bill->items->count()} 条记录没有发送成功"]));
  132. }
  133. }