OrderIssueImport.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\OrderIssueType;
  9. use App\OrderPackage;
  10. use App\Services\OrderService;
  11. use App\Services\RejectedService;
  12. use Illuminate\Support\Collection;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\Cache;
  15. use Maatwebsite\Excel\Concerns\ToCollection;
  16. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  17. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  18. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  19. HeadingRowFormatter::default('none');
  20. class OrderIssueImport implements ToCollection, WithHeadingRow, WithMultipleSheets
  21. {
  22. public function Collection(Collection $collection)
  23. {
  24. /** @var OrderService $orderService */
  25. $orderService = app('OrderService');
  26. $endIS = false;
  27. $headerRow = $collection->toArray()[0];
  28. if (!isset($headerRow['原始运单号']) || !isset($headerRow['情况说明']) || !isset($headerRow['问题类别'])) {
  29. Cache::put('error', '请检查您第一行标题是否存在原始运单号,情况说明,问题类别');
  30. } else {
  31. $endIS = true;
  32. }
  33. $exception = [];
  34. $sum = 2;
  35. $rejectedService = new RejectedService();
  36. if ($endIS) {
  37. foreach ($collection as $row) {
  38. $logistic_number = trim($row['原始运单号'], ' ');
  39. 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. } else if (!$row['问题类别']) {
  48. array_push($exception, ['第' . $sum . '行问题类别为空!']);
  49. $sum++;
  50. continue;
  51. }
  52. $order_type = OrderIssueType::query()->where('name', $row['问题类别'])->first();
  53. if (!$order_type) {
  54. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的的问题件类别不存在']);
  55. continue;
  56. }
  57. $count = OracleDOCOrderHeader::query()->where('soreference5', $logistic_number)->count();
  58. $detailCount = OracleActAllocationDetails::query()->where('picktotraceid', $logistic_number)->count();
  59. if (!$count && !$detailCount) {
  60. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的WMS运单不存在']);
  61. $sum++;
  62. continue;
  63. }
  64. $orderPackage = OrderPackage::query()->where('logistic_number', $logistic_number)->first();
  65. if ($orderPackage) {
  66. $order = Order::query()->where('id', $orderPackage['order_id'])->first();
  67. if ($order) {
  68. $orderIssue = OrderIssue::query()->where('order_id', $order['id'])->first();
  69. if ($orderIssue) {
  70. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的问题件已存在']);
  71. continue;
  72. }
  73. }
  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. $orderService->syncOrderInfoByWmsOrderNos($order_nos);
  88. $order = Order::query()->where('code',$order_nos[0])->first();
  89. $orderIssue = OrderIssue::query()->where('order_id', $order['id'])->first();
  90. $rejectedBill = $rejectedService->getRejectedByClientNo($client_no);
  91. if ($orderIssue) {
  92. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的问题件已存在']);
  93. $sum++;
  94. continue;
  95. }
  96. $arr = ['order_id' => $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. $orderIssue = OrderIssue::query()->create($arr);
  101. $orderIssue->同步退单状态();
  102. if ($orderIssue) {
  103. array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建成功!']);
  104. OrderIssueProcessLog::query()->create(['order_issue_id' => $orderIssue['id'], 'user_id' => Auth::user()['id'], 'content' => '', 'type' => '创建']);
  105. } else {
  106. array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建失败']);
  107. }
  108. $sum++;
  109. }
  110. }
  111. Cache::put('exception', $exception, 86400);
  112. }
  113. /**
  114. * 该方法是实现上传文件只选中 第一个表
  115. * ExcelImport 默认是有多少个分表就执行多少次的分表
  116. * @return OrderIssueImport[]|array
  117. */
  118. public function sheets(): array
  119. {
  120. return [0 => new OrderIssueImport()];
  121. }
  122. }