All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 16s
233 lines
8.6 KiB
PHP
233 lines
8.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\Plant;
|
|
use App\Models\TestingLineVisualization;
|
|
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 Illuminate\Support\Str;
|
|
|
|
class TestingLineVisualizationImporter extends Importer
|
|
{
|
|
protected static ?string $model = TestingLineVisualization::class;
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('plant')
|
|
->requiredMapping()
|
|
->label('PLANT CODE')
|
|
->exampleHeader('PLANT CODE')
|
|
->example(['1000'])
|
|
->relationship(resolveUsing: 'code')
|
|
->rules(['required']),
|
|
ImportColumn::make('test_bed_group')
|
|
->requiredMapping()
|
|
->label('TEST BED GROUP')
|
|
->exampleHeader('TEST BED GROUP')
|
|
->example(['Test Bed 1'])
|
|
->rules(['required']),
|
|
ImportColumn::make('line_no')
|
|
->requiredMapping()
|
|
->label('LINE NO')
|
|
->exampleHeader('LINE NO')
|
|
->example(['1'])
|
|
->rules(['required']),
|
|
ImportColumn::make('pump_model')
|
|
->requiredMapping()
|
|
->label('PUMP MODEL')
|
|
->exampleHeader('PUMP MODEL')
|
|
->example(['CRP-001'])
|
|
->rules(['required']),
|
|
ImportColumn::make('pump_type')
|
|
->requiredMapping()
|
|
->label('PUMP TYPE')
|
|
->exampleHeader('PUMP TYPE')
|
|
->example(['Vertical Turbine (VT)'])
|
|
->rules(['required']),
|
|
ImportColumn::make('motor_details')
|
|
->requiredMapping()
|
|
->label('MOTOR DETAILS')
|
|
->exampleHeader('MOTOR DETAILS')
|
|
->example(['200 kW / 2950 RPM'])
|
|
->rules(['required']),
|
|
ImportColumn::make('test_type')
|
|
->requiredMapping()
|
|
->label('TEST TYPE')
|
|
->exampleHeader('TEST TYPE')
|
|
->example(['Performance'])
|
|
->rules(['required']),
|
|
ImportColumn::make('flow')
|
|
->requiredMapping()
|
|
->label('FLOW')
|
|
->exampleHeader('FLOW')
|
|
->example(['220'])
|
|
->rules(['required']),
|
|
ImportColumn::make('head')
|
|
->requiredMapping()
|
|
->label('HEAD')
|
|
->exampleHeader('HEAD')
|
|
->example(['150'])
|
|
->rules(['required']),
|
|
ImportColumn::make('run_time')
|
|
->requiredMapping()
|
|
->label('RUN TIME')
|
|
->exampleHeader('RUN TIME')
|
|
->example(['2.5'])
|
|
->rules(['required']),
|
|
ImportColumn::make('live_status')
|
|
->requiredMapping()
|
|
->label('STATUS')
|
|
->exampleHeader('STATUS')
|
|
->example(['Running'])
|
|
->rules(['required']),
|
|
ImportColumn::make('observation_remarks')
|
|
->requiredMapping()
|
|
->label('OBSERVATION / REMARKS')
|
|
->exampleHeader('OBSERVATION / REMARKS')
|
|
->example(['Normal']),
|
|
// ImportColumn::make('created_by')
|
|
// ->requiredMapping()
|
|
// ->label('CREATED BY')
|
|
// ->exampleHeader('CREATED BY')
|
|
// ->example('RAW01234'),
|
|
// ImportColumn::make('updated_by')
|
|
// ->requiredMapping()
|
|
// ->label('UPDATED BY')
|
|
// ->exampleHeader('UPDATED BY')
|
|
// ->example('RAW01234'),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?TestingLineVisualization
|
|
{
|
|
$warnMsg = [];
|
|
$plantCod = trim($this->data['plant']);
|
|
$plant = null;
|
|
$plantId = null;
|
|
$testBedGroup = trim($this->data['test_bed_group']);
|
|
$lineNo = trim($this->data['line_no']);
|
|
$pumpModel = trim($this->data['pump_model']);
|
|
$pumpType = trim($this->data['pump_type']);
|
|
$motorDetails = trim($this->data['motor_details']);
|
|
$testType = trim($this->data['test_type']);
|
|
$flow = trim($this->data['flow']);
|
|
$head = trim($this->data['head']);
|
|
$runTime = trim($this->data['run_time']);
|
|
$status = trim($this->data['live_status']);
|
|
$remarks = trim($this->data['observation_remarks']);
|
|
$createdBy = Filament::auth()->user()->name;
|
|
$updatedBy = Filament::auth()->user()->name;
|
|
|
|
if ($plantCod == null || $plantCod == '') {
|
|
$warnMsg[] = "Plant code can't be empty!";
|
|
} elseif (! is_numeric($plantCod)) {
|
|
$warnMsg[] = 'Plant code should contain only numeric values!';
|
|
} elseif (Str::length($plantCod) < 4 || Str::length($plantCod) > 7) {
|
|
$warnMsg[] = 'Plant code must be between 4 and 7 digits only!';
|
|
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCod)) {
|
|
$warnMsg[] = 'Invalid plant code found!';
|
|
} else {
|
|
$plant = Plant::where('code', $plantCod)->first();
|
|
}
|
|
|
|
if (! $plant) {
|
|
$warnMsg[] = 'Plant not found!';
|
|
} else {
|
|
$plantId = $plant->id;
|
|
if ($testBedGroup == null || $testBedGroup == '') {
|
|
$warnMsg[] = "Test bed group can't be empty!";
|
|
}
|
|
|
|
if ($lineNo == null || $lineNo == '') {
|
|
$warnMsg[] = "Line no can't be empty!";
|
|
}
|
|
|
|
if ($pumpModel == null || $pumpModel == '') {
|
|
$warnMsg[] = "Pump model can't be empty!";
|
|
}
|
|
|
|
if ($pumpType == null || $pumpType == '') {
|
|
$warnMsg[] = "Pump type can't be empty!";
|
|
}
|
|
|
|
if ($motorDetails == null || $motorDetails == '') {
|
|
$warnMsg[] = "Motor details can't be empty!";
|
|
}
|
|
|
|
if ($testType == null || $testType == '') {
|
|
$warnMsg[] = "Test type can't be empty!";
|
|
}
|
|
|
|
if ($flow == null || $flow == '') {
|
|
$flow = '0';
|
|
} elseif (! is_numeric($flow)) {
|
|
$warnMsg[] = 'Flow must be a numeric value!';
|
|
}
|
|
|
|
if ($head == null || $head == '') {
|
|
$head = '0';
|
|
} elseif (! is_numeric($head)) {
|
|
$warnMsg[] = 'Head must be a numeric value!';
|
|
}
|
|
|
|
if ($runTime == null || $runTime == '') {
|
|
$runTime = '0.0';
|
|
} elseif (! is_numeric($runTime)) {
|
|
$warnMsg[] = 'Run time must be a numeric value!';
|
|
}
|
|
|
|
if ($status == null || $status == '') {
|
|
$warnMsg[] = "Status can't be empty!";
|
|
} elseif ($status != 'Running' && $status != 'OFF' && $status != 'Aligning' && $status != 'MTN') {
|
|
$warnMsg[] = 'Invalid status value!';
|
|
}
|
|
// Check for duplicate record based on plant_id, test_bed_group, and line_no
|
|
}
|
|
|
|
if (! empty($warnMsg)) {
|
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
|
}
|
|
|
|
TestingLineVisualization::updateOrCreate([
|
|
'plant_id' => $plantId,
|
|
'test_bed_group' => $testBedGroup,
|
|
'line_no' => $lineNo,
|
|
'pump_model' => $pumpModel,
|
|
'pump_type' => $pumpType,
|
|
'motor_details' => $motorDetails,
|
|
'test_type' => $testType,
|
|
'flow' => $flow,
|
|
'head' => $head,
|
|
'run_time' => $runTime,
|
|
'live_status' => $status,
|
|
'observation_remarks' => $remarks,
|
|
'created_by' => $createdBy,
|
|
'updated_by' => $updatedBy,
|
|
]);
|
|
|
|
return null;
|
|
// return TestingLineVisualization::firstOrNew([
|
|
// // Update existing records, matching them by `$this->data['column_name']`
|
|
// 'email' => $this->data['email'],
|
|
// ]);
|
|
|
|
// // return new TestingLineVisualization;
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your testing line visualization 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;
|
|
}
|
|
}
|