From 6ac3c664ddb31772967997031d81a96f3937d6e7 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 1 Dec 2025 14:42:01 +0530 Subject: [PATCH] Added sticker prinitng import and export file --- .../Exports/StickerPrintingExporter.php | 54 +++++++++ .../Imports/StickerPrintingImporter.php | 106 ++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 app/Filament/Exports/StickerPrintingExporter.php create mode 100644 app/Filament/Imports/StickerPrintingImporter.php diff --git a/app/Filament/Exports/StickerPrintingExporter.php b/app/Filament/Exports/StickerPrintingExporter.php new file mode 100644 index 0000000..5f9150e --- /dev/null +++ b/app/Filament/Exports/StickerPrintingExporter.php @@ -0,0 +1,54 @@ +label('NO') + ->state(function ($record) use (&$rowNumber) { + // Increment and return the row number + return ++$rowNumber; + }), + ExportColumn::make('plant.code') + ->label('PLANT CODE'), + ExportColumn::make('reference_number') + ->label('REFERENCE NUMBER'), + ExportColumn::make('serial_number') + ->label('SERIAL NUMBER'), + ExportColumn::make('created_at') + ->label('CREATED AT'), + ExportColumn::make('updated_at') + ->label('UPDATED AT'), + ExportColumn::make('created_by') + ->label('CREATED BY'), + ExportColumn::make('updated_by') + ->label('UPDATED BY'), + ExportColumn::make('deleted_at') + ->label('DELETED AT') + ->enabledByDefault(false), + ]; + } + + public static function getCompletedNotificationBody(Export $export): string + { + $body = 'Your sticker printing export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; + + if ($failedRowsCount = $export->getFailedRowsCount()) { + $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.'; + } + + return $body; + } +} diff --git a/app/Filament/Imports/StickerPrintingImporter.php b/app/Filament/Imports/StickerPrintingImporter.php new file mode 100644 index 0000000..17bf171 --- /dev/null +++ b/app/Filament/Imports/StickerPrintingImporter.php @@ -0,0 +1,106 @@ +requiredMapping() + ->exampleHeader('PLANT CODE') + ->example('1000') + ->label('PLANT CODE') + ->relationship(resolveUsing: 'code') + ->rules(['required']), + ImportColumn::make('reference_number') + ->exampleHeader('REFERENCE NUMBER') + ->example('REF123456') + ->label('REFERENCE NUMBER'), + ImportColumn::make('serial_number') + ->exampleHeader('SERIAL NUMBER') + ->example('135245325212') + ->label('SERIAL NUMBER'), + // ImportColumn::make('created_by') + // ->exampleHeader('CREATED BY') + // ->example('RAW01234') + // ->label('CREATED BY'), + ]; + } + + public function resolveRecord(): ?StickerPrinting + { + // return StickerPrinting::firstOrNew([ + // // Update existing records, matching them by `$this->data['column_name']` + // 'email' => $this->data['email'], + // ]); + + $warnMsg = []; + $plant = Plant::where('code', $this->data['plant'])->first(); + + if (Str::length($this->data['serial_number']) < 9 || !ctype_alnum($this->data['serial_number'])) { + $warnMsg[] = "Invalid serial number found"; + } + + $existing = StickerPrinting::where('plant_id', $plant->id) + ->where('serial_number', $this->data['serial_number']) + ->first(); + + if ($existing) { + $warnMsg[] = "Serial number already exists for this plant!";//throw new RowImportFailedException("Serial number already exists for this plant!"); + } + + $serial = $this->data['serial_number']; + + // --- Check duplicate in DB --- + $existsInDB = StickerPrinting::where('plant_id', $plant->id) + ->where('serial_number', $serial) + ->first(); + + if ($existsInDB) { + //throw new RowImportFailedException("Serial number '{$serial}' already exists in DB for this plant!"); + $warnMsg[] = "Serial number '{$serial}' already exists in DB for this plant!"; + } + + if (!empty($warnMsg)) { + throw new RowImportFailedException(implode(', ', $warnMsg)); + } + + StickerPrinting::Create([ + 'plant_id' => $plant->id, + 'reference_number' => $this->data['reference_number'], + 'serial_number' => $this->data['serial_number'], + 'created_at' => now(), + 'updated_at' =>now(), + 'created_by' => Filament::auth()->user()?->name, + ]); + + return null; + + //return new StickerPrinting(); + } + + public static function getCompletedNotificationBody(Import $import): string + { + $body = 'Your sticker printing 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; + } +}