RejectedImport.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 App\Services\LogisticService;
  11. use App\Services\OwnerService;
  12. use Illuminate\Support\Collection;
  13. use Illuminate\Support\Facades\Session;
  14. use Maatwebsite\Excel\Concerns\ToCollection;
  15. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  16. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  17. HeadingRowFormatter::default('none');
  18. class RejectedImport implements ToCollection, WithHeadingRow
  19. {
  20. // protected $isOverride=false;
  21. // public function __construct($isOverride)
  22. // {
  23. // if($isOverride=='1')
  24. // $this->isOverride=true;
  25. // }
  26. /**
  27. * @param Collection $collections
  28. */
  29. public function collection(Collection $collections)
  30. {
  31. foreach ($collections as $row)
  32. {
  33. $logistic_number_return = trim($row['退回单号']??$row['退货单号']??$row['快递单号']??$row['logistic_number_return']??'');
  34. $mobile_sender = trim($row['手机号']??$row['mobile_sender']??'');
  35. $logistic_name = trim($row['快递公司']??$row['logistic_name']??'');
  36. $barcode = trim($row['条码']??$row['barcode']??'');
  37. $amount = trim($row['数量']??$row['amount']??'');
  38. $quality_label_name = trim($row['品质']??$row['quality_label_name']??'');
  39. $remark = trim($row['备注']??$row['remark']??'');
  40. $owner=trim($row['货主']??$row['owner']??'');
  41. // $lastReturnNumber = Session::get('jianshangLastImportedBill');
  42. // if(!$logistic_number_return)$logistic_number_return=$lastReturnNumber;
  43. // $lastMobile = Session::get('jianshangLastImportedMobile');
  44. // if(!$mobile_sender)$mobile_sender=$lastMobile;
  45. // $lastlogistic_name = Session::get('jianshangLastImportedLogisticName');
  46. // if(!$logistic_name)$logistic_name=$lastlogistic_name;
  47. $owner=Owner::query()->where('name','like',$owner.'%')->first();
  48. $id_logistic_return=Logistic::query()->where('name','like',"%{$logistic_name}%")->first()['id'];
  49. if(!$id_logistic_return){
  50. $id_logistic_return=Logistic::query()->where('name','like',"%其它%")->first()['id'];
  51. }
  52. $id_quality_label=3;
  53. if(trim($quality_label_name)=='正品'){
  54. $id_quality_label=1;
  55. }elseif(trim($quality_label_name)=='残次'){
  56. $id_quality_label=2;
  57. }
  58. if(!$amount)$amount=1;
  59. $bill=RejectedBill::query()->where('logistic_number_return',trim($logistic_number_return))->first();
  60. if($bill){
  61. $item=RejectedBillItem::query()->where('id_rejected_bill',$bill['id'])
  62. ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
  63. if($item){
  64. $item['amount']+=$amount;
  65. $item->update();
  66. }else{
  67. $item=new RejectedBillItem([
  68. 'id_rejected_bill'=>$bill['id'],
  69. 'barcode_goods'=>trim($barcode),
  70. 'amount'=>trim($amount),
  71. 'id_quality_label'=>$id_quality_label,
  72. 'remark'=>$remark,
  73. ]);
  74. $item->save();
  75. }
  76. $bill->syncOrderIssue();
  77. }else{
  78. $bill=new RejectedBill([
  79. 'id_owner'=>$owner['id'],
  80. 'mobile_sender'=>$mobile_sender,
  81. 'logistic_number_return'=>$logistic_number_return,
  82. 'id_logistic_return'=>$id_logistic_return,
  83. 'is_loaded'=>0,
  84. ]);
  85. $bill->save();
  86. $bill->joinOrderIssue();
  87. $bill->syncOrderIssue();
  88. $item=RejectedBillItem::query()->where('id_rejected_bill',$bill['id'])
  89. ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
  90. if($item){
  91. $item['amount']+=$amount;
  92. $item->update();
  93. }else{
  94. $item=new RejectedBillItem([
  95. 'id_rejected_bill'=>$bill['id'],
  96. 'barcode_goods'=>trim($barcode),
  97. 'amount'=>trim($amount),
  98. 'id_quality_label'=>$id_quality_label,
  99. 'remark'=>$remark,
  100. ]);
  101. $item->save();
  102. }
  103. }
  104. $lastBill = Session::get('jianshangLastImportedBill');
  105. if($owner['name']=='笕尚'&&$lastBill&&$lastBill !=trim($logistic_number_return)){
  106. $this->submitToApi($bill);
  107. }
  108. Session::put('jianshangLastImportedLogisticName',trim($logistic_name));
  109. Session::put('jianshangLastImportedBill',trim($logistic_number_return));
  110. Session::put('jianshangLastImportedMobile',trim($mobile_sender));
  111. }
  112. }
  113. // public function collection(Collection $collections)
  114. // {
  115. // $rejectedBill_index=[];
  116. // $errers=[];
  117. // foreach ($collections as $i=>$row) {
  118. // $logistic_number_return = trim($row['退回单号'] ?? $row['退货单号'] ?? $row['快递单号'] ?? $row['logistic_number_return'] ?? '');
  119. // $mobile_sender = trim($row['手机号'] ?? $row['mobile_sender'] ?? '');
  120. // $logistic_name = trim($row['快递公司'] ?? $row['logistic_name'] ?? '');
  121. // $barcode = trim($row['条码'] ?? $row['barcode'] ?? '');
  122. // $amount = trim($row['数量'] ?? $row['amount'] ?? '');
  123. // $quality_label_name = trim($row['品质'] ?? $row['quality_label_name'] ?? '正品');
  124. // $remark = trim($row['备注'] ?? $row['remark'] ?? '');
  125. // $ownerName = trim($row['货主'] ?? $row['owner'] ?? '');
  126. // if(!$logistic_number_return){$errers[$i][]='第'.($i+1).'行退回单号不能为空,该行没有录入,请手动编辑';continue;}
  127. // if(!$logistic_name){$errers[$i][]='第'.($i+1).'行快递公司不能为空,该行没有录入,请手动编辑';continue;}
  128. // if(!$barcode){$errers[$i][]='第'.($i+1).'行条码不能为空,该行没有录入,请手动编辑';continue;}
  129. // if(!$amount){$errers[$i][]='第'.($i+1).'行数量不能为空,该行没有录入,请手动编辑';continue;}
  130. // if(!$ownerName){$errers[$i][]='第'.($i+1).'行货主不能为空,该行没有录入,请手动编辑';continue;}
  131. // /** @var OwnerService $ownerService */
  132. // $ownerService=app('OwnerService');
  133. // /** @var LogisticService $logisticService */
  134. // $logisticService=app('LogisticService');
  135. // if(!isset($rejectedBill_index[$logistic_number_return])){
  136. // $rejectedBill=RejectedBill::query()->where('logistic_number_return',$logistic_number_return)->get();
  137. // if($rejectedBill){$errers[$i][]='第'.($i+1).'行的退货单在导入前已有记录,需要先删除掉再导入或手动添加,该行没有录入';continue;}
  138. // $owner_id = $ownerService->get(['name' => $ownerName])->first()['id'];
  139. // if($rejectedBill){$errers[$i][]='第'.($i+1).'行的货主不存在,请检查是否输错货主名或未录入系统,该行没有录入,请手动编辑';continue;}
  140. // $logistic_id = $logisticService->get(['name' => $logistic_name])->first()['id'];
  141. // if($rejectedBill){$errers[$i][]='第'.($i+1).'行的快递不存在,请检查是否输错快递名或未录入系统,该行没有录入,请手动编辑';continue;}
  142. // $rejectedBill=new RejectedBill([
  143. // 'id_owner'=> $owner_id,
  144. // 'mobile_sender'=>$mobile_sender,
  145. // 'logistic_number_return'=>$logistic_number_return,
  146. // 'id_logistic_return'=>$logistic_id,
  147. // 'is_loaded'=>0,
  148. // ]);
  149. // $rejectedBill_index[$logistic_number_return]=$rejectedBill;
  150. // }
  151. // }
  152. //
  153. // foreach ($collections as $row)
  154. // {
  155. // $logistic_number_return = trim($row['退回单号']??$row['退货单号']??$row['快递单号']??$row['logistic_number_return']??'');
  156. // $mobile_sender = trim($row['手机号']??$row['mobile_sender']??'');
  157. // $logistic_name = trim($row['快递公司']??$row['logistic_name']??'');
  158. // $barcode = trim($row['条码']??$row['barcode']??'');
  159. // $amount = trim($row['数量']??$row['amount']??'');
  160. // $quality_label_name = trim($row['品质']??$row['quality_label_name']??'');
  161. // $remark = trim($row['备注']??$row['remark']??'');
  162. // $owner=trim($row['货主']??$row['owner']??'');
  163. //
  164. //// $lastReturnNumber = Session::get('jianshangLastImportedBill');
  165. //// if(!$logistic_number_return)$logistic_number_return=$lastReturnNumber;
  166. //// $lastMobile = Session::get('jianshangLastImportedMobile');
  167. //// if(!$mobile_sender)$mobile_sender=$lastMobile;
  168. //// $lastlogistic_name = Session::get('jianshangLastImportedLogisticName');
  169. //// if(!$logistic_name)$logistic_name=$lastlogistic_name;
  170. //
  171. //
  172. // $owner=Owner::query()->where('name','like',$owner.'%')->first();
  173. // $id_logistic_return=Logistic::query()->where('name','like',"%{$logistic_name}%")->first()['id'];
  174. // if(!$id_logistic_return){
  175. // $id_logistic_return=Logistic::query()->where('name','like',"%其它%")->first()['id'];
  176. // }
  177. //
  178. // $id_quality_label=3;
  179. // if(trim($quality_label_name)=='正品'){
  180. // $id_quality_label=1;
  181. // }elseif(trim($quality_label_name)=='残次'){
  182. // $id_quality_label=2;
  183. // }
  184. // if(!$amount)$amount=1;
  185. //
  186. //
  187. // $bill=RejectedBill::query()->where('logistic_number_return',trim($logistic_number_return))->first();
  188. // if($bill){
  189. // $item=RejectedBillItem::query()->where('id_rejected_bill',$bill['id'])
  190. // ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
  191. // if($item){
  192. // $item['amount']+=$amount;
  193. // $item->update();
  194. // }else{
  195. // $item=new RejectedBillItem([
  196. // 'id_rejected_bill'=>$bill['id'],
  197. // 'barcode_goods'=>trim($barcode),
  198. // 'amount'=>trim($amount),
  199. // 'id_quality_label'=>$id_quality_label,
  200. // 'remark'=>$remark,
  201. // ]);
  202. // $item->save();
  203. // }
  204. // $bill->syncOrderIssue();
  205. // }else{
  206. // $bill=new RejectedBill([
  207. // 'id_owner'=>$owner['id'],
  208. // 'mobile_sender'=>$mobile_sender,
  209. // 'logistic_number_return'=>$logistic_number_return,
  210. // 'id_logistic_return'=>$id_logistic_return,
  211. // 'is_loaded'=>0,
  212. // ]);
  213. // $bill->save();
  214. // $bill->joinOrderIssue();
  215. // $bill->syncOrderIssue();
  216. // $item=RejectedBillItem::query()->where('id_rejected_bill',$bill['id'])
  217. // ->where('barcode_goods',trim($barcode))->where('id_quality_label',$id_quality_label)->first();
  218. // if($item){
  219. // $item['amount']+=$amount;
  220. // $item->update();
  221. // }else{
  222. // $item=new RejectedBillItem([
  223. // 'id_rejected_bill'=>$bill['id'],
  224. // 'barcode_goods'=>trim($barcode),
  225. // 'amount'=>trim($amount),
  226. // 'id_quality_label'=>$id_quality_label,
  227. // 'remark'=>$remark,
  228. // ]);
  229. // $item->save();
  230. // }
  231. // }
  232. //
  233. // $lastBill = Session::get('jianshangLastImportedBill');
  234. // if($owner['name']=='笕尚'&&$lastBill&&$lastBill !=trim($logistic_number_return)){
  235. // $this->submitToApi($bill);
  236. // }
  237. // Session::put('jianshangLastImportedLogisticName',trim($logistic_name));
  238. // Session::put('jianshangLastImportedBill',trim($logistic_number_return));
  239. // Session::put('jianshangLastImportedMobile',trim($mobile_sender));
  240. // }
  241. // }
  242. private function submitToApi(RejectedBill $bill){
  243. if(!$bill||$bill->items()->get()->isEmpty())return;
  244. $havingFail=0;
  245. if(!config('api.API_FAKING'))
  246. $bill->items()->get()->each(function(RejectedBillItem $item)use(&$havingFail){
  247. $rejectedBill=RejectedBill::query()->find($item['id_rejected_bill']);
  248. if($rejectedBill['is_loaded']!=0){
  249. return false;
  250. }
  251. $owner = Owner::query()->find($rejectedBill['id_owner']);
  252. if(strstr($owner->name??'','笕尚')){
  253. $rejected=new Rejected();
  254. app('LogService')->log(__METHOD__,'找不到表','132行');
  255. $rejected->fill($rejectedBill->toArray());
  256. $rejected->fill($item->toArray());
  257. $rejectedJianshang=new RejectedController();
  258. $sended=$rejectedJianshang->sendRejected($rejected);
  259. if(!$sended){
  260. (new Controller())->log(__METHOD__,'error','数据发送给笕尚失败');
  261. $havingFail++;
  262. return false;
  263. }
  264. }
  265. });
  266. (new Controller())->log(__METHOD__,'error',json_encode(['success'=>'false','failure_info'=>"$havingFail/{$bill->items->count()} 条记录没有发送成功"]));
  267. }
  268. }