Added guard_name importer and exporter file
This commit is contained in:
57
app/Filament/Exports/GuardNameExporter.php
Normal file
57
app/Filament/Exports/GuardNameExporter.php
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Exports;
|
||||||
|
|
||||||
|
use App\Models\GuardName;
|
||||||
|
use Filament\Actions\Exports\ExportColumn;
|
||||||
|
use Filament\Actions\Exports\Exporter;
|
||||||
|
use Filament\Actions\Exports\Models\Export;
|
||||||
|
|
||||||
|
class GuardNameExporter extends Exporter
|
||||||
|
{
|
||||||
|
protected static ?string $model = GuardName::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('name')
|
||||||
|
->label('GUARD NAME'),
|
||||||
|
ExportColumn::make('identification1')
|
||||||
|
->label('IDENTIFICATION 1'),
|
||||||
|
ExportColumn::make('identification2')
|
||||||
|
->label('IDENTIFICATION 2'),
|
||||||
|
ExportColumn::make('created_at')
|
||||||
|
->label('CREATED AT'),
|
||||||
|
ExportColumn::make('created_by')
|
||||||
|
->label('CREATED BY'),
|
||||||
|
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 guard name 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
103
app/Filament/Imports/GuardNameImporter.php
Normal file
103
app/Filament/Imports/GuardNameImporter.php
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Imports;
|
||||||
|
|
||||||
|
use App\Models\GuardName;
|
||||||
|
use App\Models\Plant;
|
||||||
|
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||||
|
use Filament\Actions\Imports\ImportColumn;
|
||||||
|
use Filament\Actions\Imports\Importer;
|
||||||
|
use Filament\Actions\Imports\Models\Import;
|
||||||
|
use Filament\Facades\Filament;
|
||||||
|
use Str;
|
||||||
|
|
||||||
|
class GuardNameImporter extends Importer
|
||||||
|
{
|
||||||
|
protected static ?string $model = GuardName::class;
|
||||||
|
|
||||||
|
public static function getColumns(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
ImportColumn::make('plant')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Plant Name')
|
||||||
|
->example('Ransar Industries-I')
|
||||||
|
->label('Plant Name')
|
||||||
|
->relationship(resolveUsing: 'name')
|
||||||
|
->rules(['required']),
|
||||||
|
ImportColumn::make('name')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Guard Name')
|
||||||
|
->example('Shivaram')
|
||||||
|
->label('Guard Name')
|
||||||
|
->rules(['required']),
|
||||||
|
ImportColumn::make('identification1')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Identification 1')
|
||||||
|
->example('1234567890123456')
|
||||||
|
->label('Identification 1')
|
||||||
|
->rules(['required']),
|
||||||
|
ImportColumn::make('identification2')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Identification 2')
|
||||||
|
->example('')
|
||||||
|
->label('Identification 2'),
|
||||||
|
ImportColumn::make('created_by')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Created By')
|
||||||
|
->example(Filament::auth()->user()->name ?? 'Admin')
|
||||||
|
->label('Created By')
|
||||||
|
->rules(['required']),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolveRecord(): ?GuardName
|
||||||
|
{
|
||||||
|
$warnMsg = [];
|
||||||
|
$plant = Plant::where('name', $this->data['plant'])->first();
|
||||||
|
if (!$plant) {
|
||||||
|
$warnMsg[] = "Plant not found"; // '" . $this->data['plant'] . "'
|
||||||
|
}
|
||||||
|
if (Str::length($this->data['name']) < 3) { // || !ctype_alnum($this->data['name'])
|
||||||
|
$warnMsg[] = "Invalid guard name found";
|
||||||
|
}
|
||||||
|
if (Str::length($this->data['identification1']) < 5) {
|
||||||
|
$warnMsg[] = "Invalid identification-1 found";
|
||||||
|
}
|
||||||
|
$createdBy = $this->data['created_by'];
|
||||||
|
if (Str::length($createdBy) < 3) { // || !ctype_alnum($createdBy)
|
||||||
|
$warnMsg[] = "Invalid created by name found";
|
||||||
|
}
|
||||||
|
if (!empty($warnMsg)) {
|
||||||
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
return GuardName::updateOrCreate([
|
||||||
|
'name' => $this->data['name'],
|
||||||
|
'plant_id' => $plant->id
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'identification1' => $this->data['identification1'],
|
||||||
|
'identification2' => $this->data['identification2'],
|
||||||
|
'created_by' => $this->data['created_by']
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// // return GuardName::firstOrNew([
|
||||||
|
// // // Update existing records, matching them by `$this->data['column_name']`
|
||||||
|
// // 'email' => $this->data['email'],
|
||||||
|
// // ]);
|
||||||
|
//return new GuardName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCompletedNotificationBody(Import $import): string
|
||||||
|
{
|
||||||
|
$body = 'Your guard name 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