diff --git a/app/Filament/Exports/ShiftExporter.php b/app/Filament/Exports/ShiftExporter.php index 61f3847..1f4fd8c 100644 --- a/app/Filament/Exports/ShiftExporter.php +++ b/app/Filament/Exports/ShiftExporter.php @@ -25,11 +25,11 @@ class ShiftExporter extends Exporter return ++$rowNumber; }), ExportColumn::make('block.name') - ->label('BLOCK'), - ExportColumn::make('plant.name') - ->label('PLANT'), + ->label('BLOCK NAME'), + ExportColumn::make('plant.code') + ->label('PLANT CODE'), ExportColumn::make('name') - ->label('NAME'), + ->label('SHIFT NAME'), ExportColumn::make('start_time') ->label('START TIME'), ExportColumn::make('duration') @@ -50,10 +50,10 @@ class ShiftExporter extends Exporter public static function getCompletedNotificationBody(Export $export): string { - $body = 'Your shift export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; + $body = 'Your shift export has completed and '.number_format($export->successful_rows).' '.str('row')->plural($export->successful_rows).' exported.'; if ($failedRowsCount = $export->getFailedRowsCount()) { - $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.'; + $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to export.'; } return $body; diff --git a/app/Filament/Imports/ShiftImporter.php b/app/Filament/Imports/ShiftImporter.php index 4b43b0e..f3e3055 100644 --- a/app/Filament/Imports/ShiftImporter.php +++ b/app/Filament/Imports/ShiftImporter.php @@ -18,79 +18,89 @@ class ShiftImporter extends Importer public static function getColumns(): array { return [ - ImportColumn::make('name') - ->requiredMapping() - ->exampleHeader('Shift Name') - ->example('Day') - ->label('Shift 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('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('status') - ->requiredMapping() - ->exampleHeader('Active Status') - ->example('Active') - ->label('Active Status') - ->rules(['required']), + ImportColumn::make('name') + ->requiredMapping() + ->exampleHeader('Shift Name') + ->example('Day') + ->label('Shift 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('block') + ->requiredMapping() + ->exampleHeader('Block Name') + ->example('Block A') + ->label('Block Name') + ->relationship(resolveUsing: 'name') + ->rules(['required']), + ImportColumn::make('plant') + ->requiredMapping() + ->exampleHeader('Plant Code') + ->example('1000') + ->label('Plant Code') + ->relationship(resolveUsing: 'code') + ->rules(['required']), + ImportColumn::make('status') + ->requiredMapping() + ->exampleHeader('Active Status') + ->example('Active') + ->label('Active Status') + ->rules(['required']), ]; } public function resolveRecord(): ?Shift { $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"; + $plantCod = $this->data['plant']; + $plant = null; + $block = null; + if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { + $warnMsg[] = 'Invalid plant code found'; + } else { + $plant = Plant::where('code', $plantCod)->first(); } - if (!empty($warnMsg)) { + if (! $plant) { + $warnMsg[] = 'Plant not found'; + } else { + $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 (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); } @@ -105,8 +115,9 @@ class ShiftImporter extends Importer 'start_time' => $this->data['start_time'], 'duration' => $this->data['duration'], 'end_time' => $this->data['end_time'], - 'status' => $this->data['status'] + 'status' => $this->data['status'], ]); + return $shift; } else { // Safe to create new @@ -117,7 +128,7 @@ class ShiftImporter extends Importer 'start_time' => $this->data['start_time'], 'duration' => $this->data['duration'], 'end_time' => $this->data['end_time'], - 'status' => $this->data['status'] + 'status' => $this->data['status'], ]); } // return Shift::firstOrNew([ @@ -130,10 +141,10 @@ class ShiftImporter extends Importer 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.'; + $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.'; + $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.'; } return $body;