Added importer and exporter file for work group masters
This commit is contained in:
54
app/Filament/Exports/WorkGroupMasterExporter.php
Normal file
54
app/Filament/Exports/WorkGroupMasterExporter.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Exports;
|
||||
|
||||
use App\Models\WorkGroupMaster;
|
||||
use Filament\Actions\Exports\ExportColumn;
|
||||
use Filament\Actions\Exports\Exporter;
|
||||
use Filament\Actions\Exports\Models\Export;
|
||||
|
||||
class WorkGroupMasterExporter extends Exporter
|
||||
{
|
||||
protected static ?string $model = WorkGroupMaster::class;
|
||||
|
||||
public static function getColumns(): array
|
||||
{
|
||||
static $rowNumber = 0;
|
||||
return [
|
||||
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('NAME'),
|
||||
ExportColumn::make('description')
|
||||
->label('DESCRIPTION'),
|
||||
ExportColumn::make('operation_number')
|
||||
->label('OPERATION NUMBER'),
|
||||
ExportColumn::make('created_by')
|
||||
->label('CREATED BY'),
|
||||
ExportColumn::make('created_at')
|
||||
->label('CREATED AT'),
|
||||
ExportColumn::make('updated_at')
|
||||
->label('UPDATED AT'),
|
||||
ExportColumn::make('deleted_at')
|
||||
->label('DELETED AT')
|
||||
->enabledByDefault(false),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getCompletedNotificationBody(Export $export): string
|
||||
{
|
||||
$body = 'Your work group master 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;
|
||||
}
|
||||
}
|
||||
154
app/Filament/Imports/WorkGroupMasterImporter.php
Normal file
154
app/Filament/Imports/WorkGroupMasterImporter.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Imports;
|
||||
|
||||
use App\Models\Plant;
|
||||
use App\Models\User;
|
||||
use App\Models\WorkGroupMaster;
|
||||
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 WorkGroupMasterImporter extends Importer
|
||||
{
|
||||
protected static ?string $model = WorkGroupMaster::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('Name')
|
||||
->example('RMGCEABC')
|
||||
->label('Name')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('description')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Description')
|
||||
->example('Testing Model 1')
|
||||
->label('Description')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('operation_number')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Operation Number')
|
||||
->example('0020')
|
||||
->label('Operation Number')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('created_by')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Created By')
|
||||
->example('Admin')
|
||||
->label('Created By')
|
||||
->rules(['required']),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function resolveRecord(): ?WorkGroupMaster
|
||||
{
|
||||
// return WorkGroupMaster::firstOrNew([
|
||||
// // Update existing records, matching them by `$this->data['column_name']`
|
||||
// 'email' => $this->data['email'],
|
||||
// ]);
|
||||
$warnMsg = [];
|
||||
$plant = Plant::where('name', $this->data['plant'])->first();
|
||||
if (!$plant) {
|
||||
$warnMsg[] = "Plant not found";
|
||||
}
|
||||
|
||||
if (Str::length($this->data['name']) < 0) { //|| !ctype_alnum($this->data['description'])
|
||||
$warnMsg[] = "Invalid name found";
|
||||
}
|
||||
|
||||
if (Str::length(trim($this->data['description'])) <= 0) {
|
||||
$warnMsg[] = "Invalid description found";
|
||||
}
|
||||
|
||||
$desc = trim($this->data['description']);
|
||||
|
||||
if (Str::length($desc) > 44) {
|
||||
$warnMsg[] = "Description should be less than 44 digits.";
|
||||
}
|
||||
|
||||
if (Str::length($this->data['operation_number']) < 0) {
|
||||
$warnMsg[] = "Invalid operation number found";
|
||||
}
|
||||
|
||||
if(!is_numeric($this->data['operation_number']))
|
||||
{
|
||||
$warnMsg[] = "Invalid operation number found must be numeric";
|
||||
}
|
||||
|
||||
$user = User::where('name', $this->data['created_by'])->first();
|
||||
if (!$user) {
|
||||
$warnMsg[] = "Operator ID not found";
|
||||
}
|
||||
|
||||
if (!empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
else
|
||||
{
|
||||
//Check (plant_id, name)
|
||||
$existingByName = WorkGroupMaster::where('plant_id', $plant->id)
|
||||
->where('name', $this->data['name'])
|
||||
->first();
|
||||
|
||||
if ($existingByName) {
|
||||
throw new RowImportFailedException("Work group name already exists for this plant!");
|
||||
}
|
||||
|
||||
//Check (plant_id, operation_number)
|
||||
$existingByOpNum = WorkGroupMaster::where('plant_id', $plant->id)
|
||||
->where('operation_number', $this->data['operation_number'])
|
||||
->where('name', $this->data['name'])
|
||||
->first();
|
||||
|
||||
if ($existingByOpNum) {
|
||||
throw new RowImportFailedException("Operation number already exists for this plant!");
|
||||
}
|
||||
|
||||
//Check (plant_id)
|
||||
$existingByOperator = WorkGroupMaster::where('plant_id', $plant->id)
|
||||
->where('name', $this->data['name'])
|
||||
->first();
|
||||
|
||||
if ($existingByOperator) {
|
||||
throw new RowImportFailedException("Already work group name assigned to another plant!");
|
||||
}
|
||||
}
|
||||
|
||||
WorkGroupMaster::updateOrCreate([
|
||||
'plant_id' => $plant->id,
|
||||
'name' => $this->data['name'],
|
||||
'description' => $this->data['description'],
|
||||
'operation_number' => $this->data['operation_number'],
|
||||
'created_by' => $this->data['created_by'],
|
||||
]);
|
||||
|
||||
return null;
|
||||
|
||||
//return new WorkGroupMaster();
|
||||
}
|
||||
|
||||
public static function getCompletedNotificationBody(Import $import): string
|
||||
{
|
||||
$body = 'Your work group master 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