requiredMapping() ->exampleHeader('Shift Name') ->example('Day') ->label('Shift Name') ->rules(['required']), ImportColumn::make('block') ->requiredMapping() ->exampleHeader('Block Name') ->example('Block A') ->label('Block Name') ->relationship(resolveUsing:'name') ->rules(['required']), ImportColumn::make('plant') ->requiredMapping() ->exampleHeader('Plant Name') ->example('Ransar Industries-I') ->label('Plant Name') ->relationship(resolveUsing:'name') ->rules(['required']), ImportColumn::make('start_time') ->requiredMapping() ->exampleHeader('Start Time') ->example('08:00:00') ->label('Start Time') ->rules(['required']), ImportColumn::make('duration') ->requiredMapping() ->numeric() ->exampleHeader('Shift Duration') ->example('11.30') ->label('Shift Duration') ->rules(['required']), ImportColumn::make('end_time') ->requiredMapping() ->exampleHeader('End Time') ->example('19:30:00') ->label('End Time') ->rules(['required']), ImportColumn::make('status') ->requiredMapping() ->exampleHeader('Active Status') ->example('Active') ->label('Active Status') ->rules(['required']), ]; } public function resolveRecord(): ?Shift { $plant = \App\Models\Plant::where('name', $this->data['plant'])->first(); $block = \App\Models\Block::where('name', $this->data['block'])->where('plant_id', $plant->id)->first(); if (!$plant || !$block) { // Optionally handle missing plant/block return null; } $shift = Shift::where('name', $this->data['name']) ->where('plant_id', $plant->id) ->where('block_id', $block->id) ->first(); if ($shift) { // Update only if necessary to avoid duplicate key error $shift->update([ 'start_time' => $this->data['start_time'], 'duration' => $this->data['duration'], 'end_time' => $this->data['end_time'], 'status' => $this->data['status'] ]); return $shift; } else { // Safe to create new return Shift::create([ 'name' => $this->data['name'], 'plant_id' => $plant->id, 'block_id' => $block->id, 'start_time' => $this->data['start_time'], 'duration' => $this->data['duration'], 'end_time' => $this->data['end_time'], 'status' => $this->data['status'] ]); } // return Shift::firstOrNew([ // // Update existing records, matching them by `$this->data['column_name']` // 'email' => $this->data['email'], // ]); // return new Shift(); } public static function getCompletedNotificationBody(Import $import): string { $body = 'Your shift import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; if ($failedRowsCount = $import->getFailedRowsCount()) { $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.'; } return $body; } }