From 09b69b9aa3f9d32938805f454161ce7afb3864fc Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 23 Jun 2025 18:28:19 +0530 Subject: [PATCH] Added check_point_time importer and exporter file --- .../Exports/CheckPointTimeExporter.php | 61 +++++++ .../Imports/CheckPointTimeImporter.php | 153 ++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 app/Filament/Exports/CheckPointTimeExporter.php create mode 100644 app/Filament/Imports/CheckPointTimeImporter.php diff --git a/app/Filament/Exports/CheckPointTimeExporter.php b/app/Filament/Exports/CheckPointTimeExporter.php new file mode 100644 index 000000000..42193f161 --- /dev/null +++ b/app/Filament/Exports/CheckPointTimeExporter.php @@ -0,0 +1,61 @@ +label('ID'), + ExportColumn::make('no') + ->label('NO') + ->state(function ($record) use (&$rowNumber) { + // Increment and return the row number + return ++$rowNumber; + }), + ExportColumn::make('plant.name') + ->label('PLANT'), + ExportColumn::make('checkPointNames1.name') + ->label('CHECK POINT 1'), + ExportColumn::make('checkPointNames2.name') + ->label('CHECK POINT 2'), + ExportColumn::make('sequence_number') + ->label('SEQUENCE NUMBER'), + ExportColumn::make('time_lapse') + ->label('TIME LAPSE'), + ExportColumn::make('time_lapse_cushioning') + ->label('TIME LAPSE CUSHIONING'), + ExportColumn::make('created_at') + ->label('CREATED AT'), + ExportColumn::make('created_by') + ->label('CREATED BY'), + ExportColumn::make('updated_at') + ->label('UPDATED AT'), + ExportColumn::make('deleted_at') + ->enabledByDefault(false) + ->label('DELETED AT'), + ]; + } + + public static function getCompletedNotificationBody(Export $export): string + { + $body = 'Your check point time 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/CheckPointTimeImporter.php b/app/Filament/Imports/CheckPointTimeImporter.php new file mode 100644 index 000000000..f531c2308 --- /dev/null +++ b/app/Filament/Imports/CheckPointTimeImporter.php @@ -0,0 +1,153 @@ +requiredMapping() + ->exampleHeader('Plant Name') + ->example('Ransar Industries-I') + ->label('Plant Name') + ->relationship(resolveUsing: 'name') + ->rules(['required']), + ImportColumn::make('checkPointNames1') + ->requiredMapping() + ->exampleHeader('Check Point 1') + ->example('STP BACKSIDE') + ->label('Check Point 1') + ->relationship(resolveUsing: 'name') + ->rules(['required']), + ImportColumn::make('checkPointNames2') + ->requiredMapping() + ->exampleHeader('Check Point 2') + ->example('CANTEEN') + ->label('Check Point 2') + ->relationship(resolveUsing: 'name') + ->rules(['required']), + ImportColumn::make('sequence_number') + ->requiredMapping() + ->exampleHeader('Sequence Number') + ->example('1') + ->label('Sequence Number') + ->integer() + ->rules(['required']), + ImportColumn::make('time_lapse') + ->requiredMapping() + ->exampleHeader('Time Lapse') + ->example('1') + ->label('Time Lapse') + ->integer() + ->rules(['required']), + ImportColumn::make('time_lapse_cushioning') + ->requiredMapping() + ->exampleHeader('Time Lapse Cushioning') + ->example('1') + ->label('Time Lapse Cushioning') + ->integer() + ->rules(['required']), + ImportColumn::make('created_by') + ->requiredMapping() + ->exampleHeader('Created By') + ->example(Filament::auth()->user()->name ?? 'Admin') + ->label('Created By') + ->rules(['required']), + ]; + } + + public function resolveRecord(): ?CheckPointTime + { + $warnMsg = []; + $plant = Plant::where('name', $this->data['plant'])->first(); + $checkPointNames1 = null; + $checkPointNames2 = null; + if (!$plant) { + $warnMsg[] = "Plant not found"; // '" . $this->data['plant'] . "' + } + else + { + $checkPointNames1 = CheckPointName::where('name', $this->data['checkPointNames1'])->first(); + if (!$checkPointNames1) { + $warnMsg[] = "Check point 1 not found"; + } + else + { + $checkPointNames2 = CheckPointName::where('name', $this->data['checkPointNames2'])->first(); + if (!$checkPointNames2) { + $warnMsg[] = "Check point 2 not found"; + } + else + { + if ($checkPointNames1->id === $checkPointNames2->id) { + $warnMsg[] = "Check point 1 and 2 can't be the same"; + } + } + } + } + + if (Str::length($this->data['sequence_number']) < 1 || !is_numeric($this->data['sequence_number']) || $this->data['sequence_number'] <= 0) { + $warnMsg[] = "Invalid sequence number found"; + } + + if (Str::length($this->data['time_lapse']) < 1 || !is_numeric($this->data['time_lapse']) || $this->data['time_lapse'] <= 0) { + $warnMsg[] = "Invalid time lapse found"; + } + + if (Str::length($this->data['time_lapse_cushioning']) < 1 || !is_numeric($this->data['time_lapse_cushioning']) || $this->data['time_lapse_cushioning'] <= 0) { + $warnMsg[] = "Invalid time lapse cushioning found"; + } + + $createdBy = $this->data['created_by']; + if (Str::length($createdBy) < 3) { // || !ctype_alnum($createdBy) + $warnMsg[] = "Invalid created by name found"; + } + if (!empty($warnMsg)) { + throw new RowImportFailedException(implode(', ', $warnMsg)); + } + + return CheckPointTime::updateOrCreate([ + 'plant_id' => $plant->id, + 'check_point1_id' => $this->data['checkPointNames1'], + 'check_point2_id' => $this->data['checkPointNames2'], + 'sequence_number' => $this->data['sequence_number'] + ], + [ + 'time_lapse' => $this->data['time_lapse'], + 'time_lapse_cushioning' => $this->data['time_lapse_cushioning'], + 'created_by' => $this->data['created_by'] + ] + ); + // // return CheckPointTime::firstOrNew([ + // // // Update existing records, matching them by `$this->data['column_name']` + // // 'email' => $this->data['email'], + // // ]); + + // return new CheckPointTime(); + } + + public static function getCompletedNotificationBody(Import $import): string + { + $body = 'Your check point time 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; + } +}