| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Imports;
- use App\DischargeTask;
- use App\Owner;
- use App\Services\DischargeTaskService;
- use App\Warehouse;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\WithHeadingRow;
- use Maatwebsite\Excel\Imports\HeadingRowFormatter;
- HeadingRowFormatter::default('none');
- class DischargeTaskImport implements ToCollection, WithHeadingRow
- {
- /**
- * @param Collection $collection
- */
- public function collection(Collection $collection)
- {
- /** @var DischargeTaskService $service */
- $service = app(DischargeTaskService::class);
- $exception = [];
- $numbers = [];
- foreach ($collection as $row => $item) {
- $index = $row + 1;
- $message = '';
- $owner = Owner::query()->where('name', trim($item['货主']))->first();
- $waveHouse = Warehouse::query()->where('name', trim($item['仓库']))->first();
- $type = array_search(trim($item['作业名称']), DischargeTask::types);
- $unit = array_search(trim($item['单位']), DischargeTask::units);
- if (trim($item['单位']) == 'm3'){
- $unit= array_search('m³', DischargeTask::types);
- }
- if (array_search(trim($item['入库单']), $numbers)) $message .= '入库单号重复;';
- if (!$owner) $message .= '对应货主不存在;';
- if (!$waveHouse) $message .= '对应仓库不存在;';
- if (!$item['入库单'] || strlen(trim('入库单')) == 0) $message .= '未输入入库单;';
- if (!$item['数量'] || strlen(trim('数量')) == 0) $message .= '未输入数量;';
- if (!$item['单价'] || strlen(trim('单价')) == 0) $message .= '未输入单价;';
- if (!$item['单位'] || strlen(trim('单位')) == 0) $message .= '未输入单位;';
- if (!isset($type)) $message .= '指定作业类型错误;';
- if (!$item['作业名称']) $message .= '未指定作业类型';
- if (DischargeTask::query()->where('numbers',$item['入库单'])->exists()) $message .= '入库单号已存在;';
- if (strlen($message) > 0) {
- $exception[] = "第{$index}行卸货任务创建失败:" . $message;
- continue;
- }
- $params = [
- 'owner_id' => $owner->id,
- 'warehouse_id' => $waveHouse->id,
- 'type' => $type,
- 'numbers' => trim($item['入库单']),
- 'income_amount' => $item['数量'],
- 'income_unit_price' => $item['单价'],
- 'income_unit' => $unit,
- 'income_total_cost' => $item['数量'] * $item['单价'],
- 'status' => 0,
- 'income_remark' => $item['备注'] ?? '',
- 'income_at' => $item['预约日期'] ?formatExcelDateTime($item['预约日期']): Carbon::now()->format(Carbon::DEFAULT_TO_STRING_FORMAT)
- ];
- try {
- $service->createTask($params, false);
- $numbers[] = trim($item['入库单']);
- } catch (\Exception $e) {
- $exception[] = "第{$index}行创建失败:创建异常";
- }
- }
- Cache::put('exception', $exception, 86400);
- }
- }
|