Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 15s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 15s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 19s
Laravel Pint / pint (pull_request) Successful in 2m41s
Laravel Larastan / larastan (pull_request) Failing after 4m35s
141 lines
5.0 KiB
PHP
141 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\Item;
|
|
use App\Models\Machine;
|
|
use App\Models\Plant;
|
|
use App\Models\StickerMappingMaster;
|
|
use App\Models\StickerStructureDetail;
|
|
use Filament\Actions\Imports\ImportColumn;
|
|
use Filament\Actions\Imports\Importer;
|
|
use Filament\Actions\Imports\Models\Import;
|
|
use Filament\Facades\Filament;
|
|
use Illuminate\Support\Str;
|
|
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
|
|
|
class StickerMappingMasterImporter extends Importer
|
|
{
|
|
protected static ?string $model = StickerMappingMaster::class;
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('plant')
|
|
->requiredMapping()
|
|
->exampleHeader('PLANT CODE')
|
|
->examples(['1000', '1000'])
|
|
->label('PLANT CODE')
|
|
->relationship(resolveUsing: 'code')
|
|
->rules(['required']),
|
|
ImportColumn::make('item')
|
|
->requiredMapping()
|
|
->exampleHeader('ITEM CODE')
|
|
->examples(['123456', '246118'])
|
|
->label('ITEM CODE')
|
|
->rules(['required']),
|
|
ImportColumn::make('sticker_structure1_id')
|
|
->requiredMapping()
|
|
->exampleHeader('STICKER ID')
|
|
->examples(['Test Certificate'])
|
|
->label('STICKER ID')
|
|
->rules(['required']),
|
|
ImportColumn::make('sticker1_machine_id')
|
|
->requiredMapping()
|
|
->exampleHeader('WORK CENTER')
|
|
->examples(['RMI00213'])
|
|
->label('WORK CENTER')
|
|
->rules(['required']),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?StickerMappingMaster
|
|
{
|
|
$warnMsg = [];
|
|
$plantId = null;
|
|
$stickId = null;
|
|
|
|
$plantCod = $this->data['plant'];
|
|
$iCode = $this->data['item'];
|
|
$stickId = trim($this->data['sticker_structure1_id']) ?? null;
|
|
$workCenter = trim($this->data['sticker1_machine_id']) ?? null;
|
|
|
|
$user = Filament::auth()->user();
|
|
|
|
$operatorName = $user->name;
|
|
|
|
if ($plantCod == null || $plantCod == '') {
|
|
$warnMsg[] = "Plant code can't be empty!";
|
|
} elseif ($iCode == null || $iCode == '') {
|
|
$warnMsg[] = "Item Code can't be empty!";
|
|
} elseif ($stickId == null || $stickId == '') {
|
|
$warnMsg[] = "Sticker ID can't be empty!";
|
|
} elseif ($workCenter == null || $workCenter == '') {
|
|
$warnMsg[] = "Work Center can't be empty!";
|
|
}
|
|
|
|
if (Str::length($plantCod) > 0 && (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod))) {
|
|
$warnMsg[] = 'Invalid plant code found!';
|
|
} elseif (Str::length($plantCod) > 0) {
|
|
$plant = Plant::where('code', $plantCod)->first();
|
|
if (! $plant) {
|
|
$warnMsg[] = 'Plant code not found!';
|
|
} else {
|
|
$plantId = $plant->id;
|
|
}
|
|
}
|
|
|
|
if (Str::length($iCode) > 0 && (Str::length($iCode) < 6 || ! ctype_alnum($iCode))) {
|
|
$warnMsg[] = 'Invalid item code found!';
|
|
} elseif ($plantId) {
|
|
$itemCode = Item::where('code', $iCode)->first();
|
|
if (! $itemCode) {
|
|
$warnMsg[] = 'Item code not found in item master!';
|
|
} else {
|
|
$itemCode = Item::where('code', $iCode)->where('plant_id', $plantId)->first();
|
|
if (! $itemCode) {
|
|
$warnMsg[] = 'Item code not found in item master for the given plant!';
|
|
}
|
|
}
|
|
}
|
|
|
|
$stickerId = StickerStructureDetail::where('sticker_id', $stickId)->first();
|
|
|
|
if(!$stickerId){
|
|
$warnMsg[] = "Sticker ID '$stickId' not found in sticker structure detail!";
|
|
}
|
|
|
|
$machineId = Machine::where('plant_id', $plantId)->where('work_center', $workCenter)->first();
|
|
|
|
if(!$machineId){
|
|
$warnMsg[] = "Work center '$workCenter' not found in machines table against plant '$plant->code'!";
|
|
}
|
|
|
|
if (! empty($warnMsg)) {
|
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
|
}
|
|
|
|
StickerMappingMaster::updateOrCreate([
|
|
'plant_id' => $plantId,
|
|
'item_id' => $itemCode->id,
|
|
'sticker_structure1_id' => $stickerId->id,
|
|
'sticker1_machine_id' => $machineId->id ?? null,
|
|
'created_by' => $operatorName,
|
|
'updated_by' => $operatorName,
|
|
]);
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your sticker mapping 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;
|
|
}
|
|
}
|