| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace App\Imports;
- use App\OracleDOCOrderHeader;
- use App\Order;
- use App\OrderIssue;
- use App\OrderIssueProcessLog;
- use App\OrderIssueType;
- use App\Services\OrderService;
- use App\Services\RejectedService;
- use Doctrine\DBAL\Connection;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\WithHeadingRow;
- use Maatwebsite\Excel\Concerns\WithMultipleSheets;
- use Maatwebsite\Excel\Imports\HeadingRowFormatter;
- HeadingRowFormatter::default('none');
- class OrderIssueImport implements ToCollection, WithHeadingRow, WithMultipleSheets
- {
- public function Collection(Collection $collection)
- {
- $endIS = false;
- if (!isset($collection->toArray()[0]['原始运单号']) || !isset($collection->toArray()[0]['情况说明']) || !isset($collection->toArray()[0]['问题类别'])) {
- Cache::put('error', '请检查您第一行标题是否存在原始运单号,情况说明,问题类别');
- } else {
- $endIS = true;
- }
- $exception = [];
- $sum = 2;
- $orderService = app('orderService');
- $rejectedService = new RejectedService();
- if ($endIS) {
- foreach ($collection as $row) {
- $client_no = trim($row['原始运单号'], ' ');
- if (!$row['原始运单号']) {
- array_push($exception, ['第' . $sum . '行数据运单号为空!']);
- $sum++;
- continue;
- } else if (!$row['情况说明']) {
- array_push($exception, ['第' . $sum . '行问题说明为空!']);
- $sum++;
- continue;
- } else if (!$row['问题类别']) {
- array_push($exception, ['第' . $sum . '行问题类别为空!']);
- $sum++;
- continue;
- }
- $count = OracleDOCOrderHeader::where('SOReference1', $client_no)->count();
- if (!$count) {
- array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的WMS运单不存在']);
- $sum++;
- continue;
- }
- $order = $orderService->findOrCreateByClientCode($client_no);
- $orderIssue = OrderIssue::where('order_id', $order['id'])->first();
- $rejectedBill = $rejectedService->getRejectedByClientNo($client_no);
- if ($orderIssue) {
- array_push($exception, ['原始运单号' . $row['原始运单号'] . '对应的问题件已存在']);
- $sum++;
- continue;
- }
- $arr = ['order_id' => $order['id'],];
- // $arr['result_explain'] = $row['情况说明'];
- if ($rejectedBill) {
- $arr['rejected_bill_id'] = $rejectedBill['id'];
- }
- $orderIssue = OrderIssue::create($arr);
- if ($orderIssue) {
- array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建成功!']);
- OrderIssueProcessLog::create(['order_issue_id' => $orderIssue['id'], 'user_id' => Auth::user()['id'], 'content' => $row['情况说明'], 'type' => '创建']);
- } else {
- array_push($exception, ['订单' . $row['原始运单号'] . '问题件创建失败']);
- }
- $sum++;
- }
- }
- Cache::put('exception', $exception, 86400);
- }
- /**
- * 该方法是实现上传文件只选中 第一个表
- * ExcelImprot 默认是有多少个分表就执行多少次的分表
- * @return OrderIssueImport[]|array
- */
- public function sheets(): array
- {
- return [0 => new OrderIssueImport()];
- }
- }
|