Updated validation func. on import
This commit is contained in:
@@ -2,10 +2,14 @@
|
||||
|
||||
namespace App\Filament\Imports;
|
||||
|
||||
use App\Models\Block;
|
||||
use App\Models\Plant;
|
||||
use App\Models\Shift;
|
||||
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||
use Filament\Actions\Imports\ImportColumn;
|
||||
use Filament\Actions\Imports\Importer;
|
||||
use Filament\Actions\Imports\Models\Import;
|
||||
use Str;
|
||||
|
||||
class ShiftImporter extends Importer
|
||||
{
|
||||
@@ -64,12 +68,30 @@ class ShiftImporter extends Importer
|
||||
|
||||
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();
|
||||
$warnMsg = [];
|
||||
$plant = Plant::where('name', $this->data['plant'])->first();
|
||||
if (!$plant) {
|
||||
$warnMsg[] = "Plant not found";
|
||||
}
|
||||
$block = Block::where('name', $this->data['block'])->where('plant_id', $plant->id)->first();
|
||||
if (!$block) {
|
||||
$warnMsg[] = "Block not found";
|
||||
}
|
||||
if (Str::length($this->data['duration']) < 0 || !is_numeric($this->data['duration'])) {
|
||||
$warnMsg[] = "Invalid duration found";
|
||||
}
|
||||
if (Str::length($this->data['start_time']) < 0) {
|
||||
$warnMsg[] = "Invalid start time found";
|
||||
}
|
||||
if (Str::length($this->data['end_time']) < 0) {
|
||||
$warnMsg[] = "Invalid end time found";
|
||||
}
|
||||
if (Str::length($this->data['status']) < 0 || $this->data['status'] != 'Active') {
|
||||
$warnMsg[] = "Invalid shift status found";
|
||||
}
|
||||
|
||||
if (!$plant || !$block) {
|
||||
// Optionally handle missing plant/block
|
||||
return null;
|
||||
if (!empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
|
||||
$shift = Shift::where('name', $this->data['name'])
|
||||
|
||||
Reference in New Issue
Block a user