OrderIssueImport.php 6.2 KB

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