OrderIssueImport.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Imports;
  3. use App\OracleActAllocationDetails;
  4. use App\OracleDOCOrderHeader;
  5. use App\Order;
  6. use App\OrderIssue;
  7. use App\OrderIssueProcessLog;
  8. use App\OrderPackage;
  9. use App\Services\RejectedService;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Cache;
  13. use Maatwebsite\Excel\Concerns\ToCollection;
  14. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  15. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  16. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  17. HeadingRowFormatter::default('none');
  18. class OrderIssueImport implements ToCollection, WithHeadingRow, WithMultipleSheets
  19. {
  20. public function Collection(Collection $collection)
  21. {
  22. $endIS = false;
  23. if (!isset($collection->toArray()[0]['原始运单号']) || !isset($collection->toArray()[0]['情况说明']) || !isset($collection->toArray()[0]['问题类别'])) {
  24. Cache::put('error', '请检查您第一行标题是否存在原始运单号,情况说明,问题类别');
  25. } else {
  26. $endIS = true;
  27. }
  28. $exception = [];
  29. $sum = 2;
  30. $orderService = app('orderService');
  31. $rejectedService = new RejectedService();
  32. if ($endIS) {
  33. foreach ($collection as $row) {
  34. $logistic_number = trim($row['原始运单号'], ' ');
  35. if (!$row['原始运单号']) {
  36. array_push($exception, ['第' . $sum . '行数据运单号为空!']);
  37. $sum++;
  38. continue;
  39. } else if (!$row['情况说明']) {
  40. array_push($exception, ['第' . $sum . '行问题说明为空!']);
  41. $sum++;
  42. continue;
  43. } else if (!$row['问题类别']) {
  44. array_push($exception, ['第' . $sum . '行问题类别为空!']);
  45. $sum++;
  46. continue;
  47. }
  48. $count = OracleDOCOrderHeader::where('soreference5', $logistic_number)->count();
  49. $detailCount = OracleActAllocationDetails::where('picktotraceid',$logistic_number)->count();
  50. if (!$count && !$detailCount) {
  51. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的WMS运单不存在']);
  52. $sum++;
  53. continue;
  54. }
  55. $orderPackage = OrderPackage::where('logistic_number',$logistic_number)->first();
  56. if($orderPackage){
  57. $order = Order::where('id',$orderPackage['order_id'])->first();
  58. if($order ){
  59. $orderIssue = OrderIssue::where('order_id',$order['id'])->first();
  60. if($orderIssue){
  61. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的问题件已存在']);
  62. continue;
  63. }
  64. }
  65. }
  66. $client_no = null;
  67. if($count){
  68. $orderHeader = OracleDOCOrderHeader::where('soreference5',$logistic_number)->first();
  69. $client_no =$orderHeader['soreference1'];
  70. }else if($detailCount){
  71. $detail = OracleActAllocationDetails::where('picktotraceid',$logistic_number)->first();
  72. $orderHeader = OracleDOCOrderHeader::where('orderno',$detail['orderno'])->first();
  73. $client_no =$orderHeader['soreference1'];
  74. }
  75. $order = $orderService->findOrCreateByClientCode($client_no);
  76. $orderIssue = OrderIssue::where('order_id', $order['id'])->first();
  77. $rejectedBill = $rejectedService->getRejectedByClientNo($client_no);
  78. if ($orderIssue) {
  79. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的问题件已存在']);
  80. $sum++;
  81. continue;
  82. }
  83. $arr = ['order_id' => $order['id'],];
  84. if ($rejectedBill) {
  85. $arr['rejected_bill_id'] = $rejectedBill['id'];
  86. }
  87. $orderIssue = OrderIssue::create($arr);
  88. if ($orderIssue) {
  89. array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建成功!']);
  90. OrderIssueProcessLog::create(['order_issue_id' => $orderIssue['id'], 'user_id' => Auth::user()['id'], 'content' => $row['情况说明'], 'type' => '创建']);
  91. } else {
  92. array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建失败']);
  93. }
  94. $sum++;
  95. }
  96. }
  97. Cache::put('exception', $exception, 86400);
  98. }
  99. /**
  100. * 该方法是实现上传文件只选中 第一个表
  101. * ExcelImprot 默认是有多少个分表就执行多少次的分表
  102. * @return OrderIssueImport[]|array
  103. */
  104. public function sheets(): array
  105. {
  106. return [0 => new OrderIssueImport()];
  107. }
  108. }