OrderIssueImport.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Imports;
  3. use App\Events\AddOrUpdateOrderIssues;
  4. use App\Jobs\SyncOrderIssueOrWorkOrderBySWMS;
  5. use App\Jobs\SyncRejectedBillRejectingStatusJob;
  6. use App\OracleActAllocationDetails;
  7. use App\OracleDOCOrderHeader;
  8. use App\Order;
  9. use App\OrderIssue;
  10. use App\OrderIssueProcessLog;
  11. use App\OrderIssueType;
  12. use App\OrderPackage;
  13. use App\Services\OrderService;
  14. use App\Services\RejectedService;
  15. use Illuminate\Support\Collection;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\Cache;
  18. use Maatwebsite\Excel\Concerns\ToCollection;
  19. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  20. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  21. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  22. HeadingRowFormatter::default('none');
  23. class OrderIssueImport implements ToCollection, WithHeadingRow, WithMultipleSheets
  24. {
  25. /** @var OrderService $orderService*/
  26. private $orderService;
  27. /** @var RejectedService $rejectedService */
  28. private $rejectedService;
  29. public function Collection(Collection $collection)
  30. {
  31. $this->orderService = app(OrderService::class);
  32. $this->rejectedService = app(RejectedService::class);
  33. $endIS = false;
  34. $headerRow = $collection->toArray()[0];
  35. if (!isset($headerRow['原始运单号']) || !isset($headerRow['情况说明']) || !isset($headerRow['问题类别'])) {
  36. Cache::put('error', '请检查您第一行标题是否存在原始运单号,情况说明,问题类别');
  37. } else {
  38. $endIS = true;
  39. }
  40. $exception = [];
  41. $sum = 2;
  42. if ($endIS) {
  43. foreach ($collection as $row) {
  44. $logistic_number = trim($row['原始运单号'], ' ');
  45. if (!$row['原始运单号']) {
  46. array_push($exception, ['第' . $sum . '行数据运单号为空!']);
  47. $sum++;
  48. continue;
  49. } else if (!$row['情况说明']) {
  50. array_push($exception, ['第' . $sum . '行问题说明为空!']);
  51. $sum++;
  52. continue;
  53. } else if (!$row['问题类别']) {
  54. array_push($exception, ['第' . $sum . '行问题类别为空!']);
  55. $sum++;
  56. continue;
  57. }
  58. $order_type = OrderIssueType::query()->where('name', $row['问题类别'])->first();
  59. if (!$order_type) {
  60. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的的问题件类别不存在']);
  61. continue;
  62. }
  63. $count = OracleDOCOrderHeader::query()->where('soreference5', $logistic_number)->count();
  64. $detailCount = OracleActAllocationDetails::query()->where('picktotraceid', $logistic_number)->count();
  65. if (!$count && !$detailCount) {
  66. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的WMS运单不存在']);
  67. $sum++;
  68. continue;
  69. }
  70. $query = OrderPackage::query()->select('order_id')->where('logistic_number', $logistic_number);
  71. $order_issue_exists = OrderIssue::query()->whereIn('order_id', $query)->exists();
  72. if ($order_issue_exists) {
  73. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的问题件已存在']);
  74. continue;
  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. $this->orderService->syncOrderInfoByWmsOrderNos($order_nos);
  89. $query = Order::query()->where('code', $order_nos[0]);
  90. $orderIssue = OrderIssue::query()->where('order_id', $query)->first();
  91. $rejectedBill = $this->rejectedService->getRejectedByClientNo($client_no);
  92. if ($orderIssue) {
  93. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的问题件已存在']);
  94. $sum++;
  95. continue;
  96. }
  97. $arr = ['order_id' => $orderIssue->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([$orderIssue->order_id]));
  104. SyncRejectedBillRejectingStatusJob::dispatch($orderIssue->order);
  105. if ($orderIssue) {
  106. array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建成功!']);
  107. SyncOrderIssueOrWorkOrderBySWMS::dispatch($orderIssue,SyncOrderIssueOrWorkOrderBySWMS::$ORDER_ISSUE_TYPE);
  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. }