Merge pull request 'Added sticker mapping master importer logic' (#1172) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
Reviewed-on: #1172
This commit was merged in pull request #1172.
This commit is contained in:
@@ -2,10 +2,17 @@
|
||||
|
||||
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
|
||||
{
|
||||
@@ -14,32 +21,110 @@ class StickerMappingMasterImporter extends Importer
|
||||
public static function getColumns(): array
|
||||
{
|
||||
return [
|
||||
ImportColumn::make('plant_id')
|
||||
ImportColumn::make('plant')
|
||||
->requiredMapping()
|
||||
->numeric()
|
||||
->rules(['required', 'integer']),
|
||||
->exampleHeader('PLANT CODE')
|
||||
->examples(['1000', '1000'])
|
||||
->label('PLANT CODE')
|
||||
->relationship(resolveUsing: 'code')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('item')
|
||||
->requiredMapping()
|
||||
->relationship()
|
||||
->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']),
|
||||
ImportColumn::make('sticker1'),
|
||||
ImportColumn::make('sticker2'),
|
||||
ImportColumn::make('sticker3'),
|
||||
ImportColumn::make('sticker4'),
|
||||
ImportColumn::make('sticker5'),
|
||||
ImportColumn::make('created_by'),
|
||||
ImportColumn::make('updated_by'),
|
||||
];
|
||||
}
|
||||
|
||||
public function resolveRecord(): ?StickerMappingMaster
|
||||
{
|
||||
// return StickerMappingMaster::firstOrNew([
|
||||
// // Update existing records, matching them by `$this->data['column_name']`
|
||||
// 'email' => $this->data['email'],
|
||||
// ]);
|
||||
$warnMsg = [];
|
||||
$plantId = null;
|
||||
$stickId = null;
|
||||
|
||||
return new StickerMappingMaster();
|
||||
$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
|
||||
|
||||
Reference in New Issue
Block a user