OrderIssueImport.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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;
  74. if ($count) {
  75. $orderHeader = OracleDOCOrderHeader::query()->where('soreference5', $logistic_number)->first();
  76. $client_no = $orderHeader['soreference1'];
  77. } else if ($detailCount) {
  78. $detail = OracleActAllocationDetails::query()->where('picktotraceid', $logistic_number)->first();
  79. $orderHeader = OracleDOCOrderHeader::query()->where('orderno', $detail['orderno'])->first();
  80. $client_no = $orderHeader['soreference1'];
  81. }
  82. $order = $orderService->findOrCreateByClientCode($client_no);
  83. $orderIssue = OrderIssue::query()->where('order_id', $order['id'])->first();
  84. $rejectedBill = $rejectedService->getRejectedByClientNo($client_no);
  85. if ($orderIssue) {
  86. array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的问题件已存在']);
  87. $sum++;
  88. continue;
  89. }
  90. $arr = ['order_id' => $order['id'], 'order_issue_type_id' => $order_type['id'],'result_explain'=>$row['情况说明'],'imported_status'=>'导入未处理'];
  91. if ($rejectedBill) {
  92. $arr['rejected_bill_id'] = $rejectedBill['id'];
  93. }
  94. $orderIssue = OrderIssue::query()->create($arr);
  95. $orderIssue->同步退单状态();
  96. if ($orderIssue) {
  97. array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建成功!']);
  98. OrderIssueProcessLog::query()->create(['order_issue_id' => $orderIssue['id'], 'user_id' => Auth::user()['id'], 'content' => '', 'type' => '创建']);
  99. } else {
  100. array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建失败']);
  101. }
  102. $sum++;
  103. }
  104. }
  105. Cache::put('exception', $exception, 86400);
  106. }
  107. /**
  108. * 该方法是实现上传文件只选中 第一个表
  109. * ExcelImport 默认是有多少个分表就执行多少次的分表
  110. * @return OrderIssueImport[]|array
  111. */
  112. public function sheets(): array
  113. {
  114. return [0 => new OrderIssueImport()];
  115. }
  116. }