OrderIssueImport.php 6.1 KB

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