RejectedImport.php 6.1 KB

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