All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 13s
250 lines
10 KiB
PHP
250 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\Machine;
|
|
use App\Models\ModelMaster;
|
|
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 Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ModelMasterImporter extends Importer
|
|
{
|
|
protected static ?string $model = ModelMaster::class;
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('plant')
|
|
->label('PLANT CODE')
|
|
->requiredMapping()
|
|
->exampleHeader('PLANT CODE')
|
|
->example('1000')
|
|
->relationship(resolveUsing: 'code')
|
|
->rules(['required']),
|
|
ImportColumn::make('machine')
|
|
->label('WORK CENTER')
|
|
->requiredMapping()
|
|
->exampleHeader('WORK CENTER')
|
|
->example('RMGLAS01')
|
|
->relationship(resolveUsing: 'work_center')
|
|
->rules(['required']),
|
|
ImportColumn::make('heading_name')
|
|
->label('HEADING NAME')
|
|
->exampleHeader('HEADING NAME')
|
|
->example('ZMM_HEADING'),
|
|
ImportColumn::make('heading_value')
|
|
->label('HEADING VALUE')
|
|
->exampleHeader('HEADING VALUE')
|
|
->example('PUMPS'),
|
|
ImportColumn::make('type_name')
|
|
->label('TYPE NAME')
|
|
->exampleHeader('TYPE NAME')
|
|
->example(''),
|
|
ImportColumn::make('type_value')
|
|
->label('TYPE VALUE')
|
|
->exampleHeader('TYPE VALUE')
|
|
->example(''),
|
|
ImportColumn::make('has_motor')
|
|
->label('HAS MOTOR')
|
|
->exampleHeader('HAS MOTOR')
|
|
->example('1'),
|
|
ImportColumn::make('has_m_part')
|
|
->label('HAS MOTOR PART')
|
|
->exampleHeader('HAS MOTOR PART')
|
|
->example('1'),
|
|
ImportColumn::make('has_m_count')
|
|
->label('HAS MOTOR COUNT')
|
|
->exampleHeader('HAS MOTOR COUNT')
|
|
->example('1'),
|
|
ImportColumn::make('has_pump')
|
|
->label('HAS PUMP')
|
|
->exampleHeader('HAS PUMP')
|
|
->example('1'),
|
|
ImportColumn::make('has_p_part')
|
|
->label('HAS PUMP PART')
|
|
->exampleHeader('HAS PUMP PART')
|
|
->example('1'),
|
|
ImportColumn::make('has_p_count')
|
|
->label('HAS PUMP COUNT')
|
|
->exampleHeader('HAS PUMP COUNT')
|
|
->example('1'),
|
|
ImportColumn::make('has_name_plate')
|
|
->label('HAS NAME PLATE')
|
|
->exampleHeader('HAS NAME PLATE')
|
|
->example(''),
|
|
ImportColumn::make('has_np_part')
|
|
->label('HAS NAME PLATE PART')
|
|
->exampleHeader('HAS NAME PLATE PART')
|
|
->example(''),
|
|
ImportColumn::make('has_np_count')
|
|
->label('HAS NAME PLATE COUNT')
|
|
->exampleHeader('HAS NAME PLATE COUNT')
|
|
->example(''),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?ModelMaster
|
|
{
|
|
$warnMsg = [];
|
|
$plantCod = trim($this->data['plant']);
|
|
$plant = null;
|
|
$plantId = null;
|
|
$workCent = trim($this->data['machine']);
|
|
$machine = null;
|
|
$machineId = null;
|
|
$headingName = strtoupper(trim($this->data['heading_name']));
|
|
$headingValue = strtoupper(trim($this->data['heading_value']));
|
|
$typeName = strtoupper(trim($this->data['type_name']));
|
|
$typeValue = (Str::length($typeName) <= 0) ? '' : strtoupper(trim($this->data['type_value']));
|
|
$hasMotor = (trim($this->data['has_motor']) == '1') ? '1' : '0';
|
|
$hasMPart = (trim($this->data['has_m_part']) == '1') ? '1' : '0';
|
|
$hasMCount = ($hasMotor == '1') ? trim($this->data['has_m_count']) : '0';
|
|
$hasPump = (trim($this->data['has_pump']) == '1') ? '1' : '0';
|
|
$hasPPart = (trim($this->data['has_p_part']) == '1') ? '1' : '0';
|
|
$hasPCount = ($hasPump == '1') ? trim($this->data['has_p_count']) : '0';
|
|
$hasNamePlate = (trim($this->data['has_name_plate']) == '1') ? '1' : '0';
|
|
$hasNpPart = (trim($this->data['has_np_part']) == '1') ? '1' : '0';
|
|
$hasNpCount = ($hasNamePlate == '1') ? trim($this->data['has_np_count']) : '0';
|
|
$createdBy = Filament::auth()->user()->name;
|
|
$updatedBy = Filament::auth()->user()->name;
|
|
|
|
if ($plantCod == null || $plantCod == '' || ! $plantCod) {
|
|
$warnMsg[] = "Plant code can't be empty!";
|
|
} elseif (! is_numeric($plantCod)) {
|
|
$warnMsg[] = "Plant code '{$plantCod}' should contain only numeric values!";
|
|
} elseif (Str::length($plantCod) < 4 || Str::length($plantCod) > 7) {
|
|
$warnMsg[] = "Plant code '{$plantCod}' must be between 4 and 7 digits only!";
|
|
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCod)) {
|
|
$warnMsg[] = "Invalid plant code '{$plantCod}' found!";
|
|
}
|
|
|
|
if ($workCent == null || $workCent == '' || ! $workCent) {
|
|
$warnMsg[] = "Work center can't be empty!";
|
|
} elseif (Str::length($workCent) < 6) {
|
|
$warnMsg[] = "Work center '{$workCent}' should contain minimum 6 characters!";
|
|
} elseif (! ctype_alnum($workCent)) {
|
|
$warnMsg[] = "Work center '{$workCent}' should contain only alpha-numeric values!";
|
|
} elseif (! preg_match('/^[a-zA-Z0-9]{6,}$/', $workCent)) {
|
|
$warnMsg[] = "Invalid work center '{$workCent}' found!";
|
|
}
|
|
|
|
$columns = Schema::getColumnListing('class_characteristics');
|
|
|
|
if ($headingName == null || $headingName == '' || ! $headingName) {
|
|
$warnMsg[] = "Heading name can't be empty!";
|
|
} elseif (Str::length($headingName) < 5) {
|
|
$warnMsg[] = "Heading name '{$headingName}' should contain minimum 5 characters!";
|
|
} else {
|
|
if (! in_array($headingName, $columns, true)) {
|
|
$warnMsg[] = 'Unknown heading name found!';
|
|
}
|
|
}
|
|
|
|
if ($typeName != null && $typeName != '' && $typeName) {
|
|
if (Str::length($typeName) < 5) {
|
|
$warnMsg[] = "Type name '{$typeName}' should contain minimum 5 characters!";
|
|
} else {
|
|
if (! in_array($typeName, $columns, true)) {
|
|
$warnMsg[] = 'Unknown type name found!';
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($hasMotor != '1') {
|
|
$hasMPart = '0';
|
|
$hasMCount = '0';
|
|
} else {
|
|
$hasMPart = ($hasMPart != '1') ? '0' : '1';
|
|
$hasMCount = ($hasMCount == '0' || empty($hasMCount) || ! is_numeric($hasMCount) || ! preg_match('/^([1-9]|[1-9][0-9])$/', $hasMCount)) ? '1' : $hasMCount;
|
|
}
|
|
|
|
if ($hasPump != '1') {
|
|
$hasPPart = '0';
|
|
$hasPCount = '0';
|
|
} else {
|
|
$hasPPart = ($hasPPart != '1') ? '0' : '1';
|
|
$hasPCount = ($hasPCount == '0' || empty($hasPCount) || ! is_numeric($hasPCount) || ! preg_match('/^([1-9]|[1-9][0-9])$/', $hasPCount)) ? '1' : $hasPCount;
|
|
}
|
|
|
|
if ($hasNamePlate != '1') {
|
|
$hasNpPart = '0';
|
|
$hasNpCount = '0';
|
|
} else {
|
|
$hasNpPart = ($hasNpPart != '1') ? '0' : '1';
|
|
$hasNpCount = ($hasNpCount == '0' || empty($hasNpCount) || ! is_numeric($hasNpCount) || ! preg_match('/^([1-9]|[1-9][0-9])$/', $hasNpCount)) ? '1' : $hasNpCount;
|
|
}
|
|
|
|
$plant = Plant::where('code', $plantCod)->first();
|
|
if (! $plant) {
|
|
$warnMsg[] = 'Plant code not found!';
|
|
} else {
|
|
$plantId = $plant->id;
|
|
$machine = Machine::where('work_center', $workCent)->first();
|
|
if (! $machine) {
|
|
$warnMsg[] = 'Work center not found!';
|
|
} else {
|
|
$machine = Machine::where('work_center', $workCent)->where('plant_id', $plantId)->first();
|
|
if (! $machine) {
|
|
$warnMsg[] = 'Work center not found for the plant!';
|
|
} else {
|
|
$machineId = $machine->id;
|
|
|
|
if (empty($warnMsg)) {
|
|
$recExist = ModelMaster::where('plant_id', $plantId)->where('machine_id', $machineId)->where('heading_name', $headingName)->where('heading_value', $headingValue)->where('type_name', $typeName)->where('type_value', $typeValue)->first()?->created_by;
|
|
|
|
if ($recExist) {
|
|
$createdBy = $recExist;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! empty($warnMsg)) {
|
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
|
}
|
|
|
|
return ModelMaster::updateOrCreate([
|
|
'plant_id' => $plantId,
|
|
'machine_id' => $machineId,
|
|
'heading_name' => $headingName,
|
|
'heading_value' => $headingValue,
|
|
'type_name' => $typeName,
|
|
'type_value' => $typeValue,
|
|
],
|
|
[
|
|
'has_motor' => $hasMotor,
|
|
'has_m_part' => $hasMPart,
|
|
'has_m_count' => $hasMCount,
|
|
'has_pump' => $hasPump,
|
|
'has_p_part' => $hasPPart,
|
|
'has_p_count' => $hasPCount,
|
|
'has_name_plate' => $hasNamePlate,
|
|
'has_np_part' => $hasNpPart,
|
|
'has_np_count' => $hasNpCount,
|
|
'created_by' => $createdBy,
|
|
'updated_by' => $updatedBy,
|
|
]
|
|
);
|
|
|
|
// return new ModelMaster;
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your model 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;
|
|
}
|
|
}
|