Added locator importer and exporter file
This commit is contained in:
55
app/Filament/Exports/LocatorExporter.php
Normal file
55
app/Filament/Exports/LocatorExporter.php
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Exports;
|
||||||
|
|
||||||
|
use App\Models\Locator;
|
||||||
|
use Filament\Actions\Exports\ExportColumn;
|
||||||
|
use Filament\Actions\Exports\Exporter;
|
||||||
|
use Filament\Actions\Exports\Models\Export;
|
||||||
|
|
||||||
|
class LocatorExporter extends Exporter
|
||||||
|
{
|
||||||
|
protected static ?string $model = Locator::class;
|
||||||
|
|
||||||
|
public static function getColumns(): array
|
||||||
|
{
|
||||||
|
static $rowNumber = 0;
|
||||||
|
|
||||||
|
return [
|
||||||
|
// ExportColumn::make('id')
|
||||||
|
// ->label('ID'),
|
||||||
|
ExportColumn::make('no')
|
||||||
|
->label('NO')
|
||||||
|
->state(function ($record) use (&$rowNumber) {
|
||||||
|
// Increment and return the row number
|
||||||
|
return ++$rowNumber;
|
||||||
|
}),
|
||||||
|
ExportColumn::make('plant.name')
|
||||||
|
->label('PLANT'),
|
||||||
|
ExportColumn::make('locator_number')
|
||||||
|
->label('LOCATOR NUMBER'),
|
||||||
|
ExportColumn::make('locator_quantity')
|
||||||
|
->label('LOCATOR QUANTITY'),
|
||||||
|
ExportColumn::make('operator_id')
|
||||||
|
->label('OPERATOR ID'),
|
||||||
|
ExportColumn::make('created_at')
|
||||||
|
->label('CREATED AT'),
|
||||||
|
ExportColumn::make('updated_at')
|
||||||
|
->label('UPDATED AT'),
|
||||||
|
ExportColumn::make('deleted_at')
|
||||||
|
->enabledByDefault(false)
|
||||||
|
->label('DELETED AT'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCompletedNotificationBody(Export $export): string
|
||||||
|
{
|
||||||
|
$body = 'Your locator 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.';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
}
|
||||||
62
app/Filament/Imports/LocatorImporter.php
Normal file
62
app/Filament/Imports/LocatorImporter.php
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Imports;
|
||||||
|
|
||||||
|
use App\Models\Locator;
|
||||||
|
use Filament\Actions\Imports\ImportColumn;
|
||||||
|
use Filament\Actions\Imports\Importer;
|
||||||
|
use Filament\Actions\Imports\Models\Import;
|
||||||
|
|
||||||
|
class LocatorImporter extends Importer
|
||||||
|
{
|
||||||
|
protected static ?string $model = Locator::class;
|
||||||
|
|
||||||
|
public static function getColumns(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
ImportColumn::make('locator_number')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Locator Number')
|
||||||
|
->label('Locator Number')
|
||||||
|
->rules(['required']),
|
||||||
|
ImportColumn::make('locator_quantity')
|
||||||
|
->requiredMapping()
|
||||||
|
->numeric()
|
||||||
|
->exampleHeader('Locator Quantity')
|
||||||
|
->label('Locator Quantity')
|
||||||
|
->rules(['required', 'integer']),
|
||||||
|
ImportColumn::make('operator_id')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Operator ID')
|
||||||
|
->label('Operator ID')
|
||||||
|
->rules(['required']),
|
||||||
|
ImportColumn::make('plant_id')
|
||||||
|
->requiredMapping()
|
||||||
|
->relationship('plant', 'name')
|
||||||
|
->exampleHeader('Plant')
|
||||||
|
->label('Plant')
|
||||||
|
->rules(['required']),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolveRecord(): ?Locator
|
||||||
|
{
|
||||||
|
// return Locator::firstOrNew([
|
||||||
|
// // Update existing records, matching them by `$this->data['column_name']`
|
||||||
|
// 'email' => $this->data['email'],
|
||||||
|
// ]);
|
||||||
|
|
||||||
|
return new Locator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCompletedNotificationBody(Import $import): string
|
||||||
|
{
|
||||||
|
$body = 'Your locator 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user