Added update or create record on import

This commit is contained in:
dhanabalan
2025-04-25 12:55:28 +05:30
parent 999228e272
commit 58a58f3d9b
6 changed files with 78 additions and 11 deletions

View File

@@ -6,6 +6,7 @@ use App\Models\Block;
use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import; use Filament\Actions\Imports\Models\Import;
use Notification;
class BlockImporter extends Importer class BlockImporter extends Importer
{ {
@@ -32,12 +33,18 @@ class BlockImporter extends Importer
public function resolveRecord(): ?Block public function resolveRecord(): ?Block
{ {
$plant = \App\Models\Plant::where('name', $this->data['plant'])->first();
return Block::updateOrCreate([
'name' => $this->data['name'],
'plant_id' => $plant->id
]
);
// return Block::firstOrNew([ // return Block::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']` // // Update existing records, matching them by `$this->data['column_name']`
// 'email' => $this->data['email'], // 'email' => $this->data['email'],
// ]); // ]);
return new Block(); // return new Block();
} }
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string

View File

@@ -25,12 +25,16 @@ class CompanyImporter extends Importer
public function resolveRecord(): ?Company public function resolveRecord(): ?Company
{ {
return Company::updateOrCreate([
'name' => $this->data['name']
]
);
// return Company::firstOrNew([ // return Company::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']` // // Update existing records, matching them by `$this->data['column_name']`
// 'email' => $this->data['email'], // 'email' => $this->data['email'],
// ]); // ]);
return new Company(); // return new Company();
} }
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string

View File

@@ -45,12 +45,17 @@ class ItemImporter extends Importer
public function resolveRecord(): ?Item public function resolveRecord(): ?Item
{ {
// return Item::firstOrNew([ $plant = \App\Models\Plant::where('name', $this->data['plant'])->first();
// // Update existing records, matching them by `$this->data['column_name']` return Item::updateOrCreate([
// 'email' => $this->data['email'], 'code' => $this->data['code'],
// ]); 'plant_id' => $plant->id
],
return new Item; [
'description' => $this->data['description'],
'hourly_quantity' => $this->data['hourly_quantity']
]
);
// return new Item;
} }
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string

View File

@@ -38,12 +38,21 @@ class LineImporter extends Importer
public function resolveRecord(): ?Line public function resolveRecord(): ?Line
{ {
$plant = \App\Models\Plant::where('name', $this->data['plant'])->first();
return Line::updateOrCreate([
'name' => $this->data['name'],
'plant_id' => $plant->id
],
[
'type' => $this->data['type']
]
);
// return Line::firstOrNew([ // return Line::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']` // // Update existing records, matching them by `$this->data['column_name']`
// 'email' => $this->data['email'], // 'email' => $this->data['email'],
// ]); // ]);
return new Line(); // return new Line();
} }
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string

View File

@@ -31,12 +31,19 @@ class LineStopImporter extends Importer
public function resolveRecord(): ?LineStop public function resolveRecord(): ?LineStop
{ {
return LineStop::updateOrCreate([
'code' => $this->data['code']
],
[
'reason' => $this->data['reason']
]
);
// return LineStop::firstOrNew([ // return LineStop::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']` // // Update existing records, matching them by `$this->data['column_name']`
// 'email' => $this->data['email'], // 'email' => $this->data['email'],
// ]); // ]);
return new LineStop(); // return new LineStop();
} }
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string

View File

@@ -26,7 +26,7 @@ class StickerMasterImporter extends Importer
->requiredMapping() ->requiredMapping()
->exampleHeader('Plant Name') ->exampleHeader('Plant Name')
->example('Ransar Industries-I') ->example('Ransar Industries-I')
->label('PLANT') ->label('PLANT NAME')
->relationship(resolveUsing: 'name') ->relationship(resolveUsing: 'name')
->rules(['required']), ->rules(['required']),
@@ -178,6 +178,41 @@ class StickerMasterImporter extends Importer
public function resolveRecord(): ?StickerMaster public function resolveRecord(): ?StickerMaster
{ {
$item = \App\Models\Item::where('code', $this->data['item'])->first();
$plant = \App\Models\Plant::where('name', $this->data['plant'])->first();
if (!$plant || !$item) {
// Optionally handle missing plant/block
return null;
}
return StickerMaster::updateOrCreate([
'item_id' => $item->id,
'plant_id' => $plant->id
],
[
'serial_number_motor' => $this->data['serial_number_motor'],
'serial_number_pump' => $this->data['serial_number_pump'],
'serial_number_pumpset' => $this->data['serial_number_pumpset'],
'pack_slip_motor' => $this->data['pack_slip_motor'],
'pack_slip_pump' => $this->data['pack_slip_pump'],
'pack_slip_pumpset' => $this->data['pack_slip_pumpset'],
'name_plate_motor' => $this->data['name_plate_motor'],
'name_plate_pump' => $this->data['name_plate_pump'],
'name_plate_pumpset' => $this->data['name_plate_pumpset'],
'tube_sticker_motor' => $this->data['tube_sticker_motor'],
'tube_sticker_pump' => $this->data['tube_sticker_pump'],
'tube_sticker_pumpset' => $this->data['tube_sticker_pumpset'],
'warranty_card' => $this->data['warranty_card'],
'part_validation1' => $this->data['part_validation1'],
'part_validation2' => $this->data['part_validation2'],
'part_validation3' => $this->data['part_validation3'],
'part_validation4' => $this->data['part_validation4'],
'part_validation5' => $this->data['part_validation5'],
'panel_box_code' => $this->data['panel_box_code'],
'load_rate' => $this->data['load_rate'],
'bundle_quantity' => $this->data['bundle_quantity'],
'material_type' => $this->data['material_type']
]
);
// return StickerMaster::firstOrNew([ // return StickerMaster::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']` // // Update existing records, matching them by `$this->data['column_name']`
// 'email' => $this->data['email'], // 'email' => $this->data['email'],