OrderIssueImport.php 6.6 KB

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