OrderIssueImport.php 5.9 KB

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