diff --git a/app/Filament/Exports/CheckPointNameExporter.php b/app/Filament/Exports/CheckPointNameExporter.php new file mode 100644 index 000000000..315c90d50 --- /dev/null +++ b/app/Filament/Exports/CheckPointNameExporter.php @@ -0,0 +1,53 @@ +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('name') + ->label('CHECK POINT NAME'), + 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 name 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/CheckPointNameImporter.php b/app/Filament/Imports/CheckPointNameImporter.php new file mode 100644 index 000000000..5a81ee470 --- /dev/null +++ b/app/Filament/Imports/CheckPointNameImporter.php @@ -0,0 +1,87 @@ +requiredMapping() + ->exampleHeader('Plant Name') + ->example('Ransar Industries-I') + ->label('Plant Name') + ->relationship(resolveUsing: 'name') + ->rules(['required']), + ImportColumn::make('name') + ->requiredMapping() + ->exampleHeader('Guard Name') + ->example('STP BACKSIDE') + ->label('Check Point Name') + ->rules(['required']), + ImportColumn::make('created_by') + ->requiredMapping() + ->exampleHeader('Created By') + ->example(Filament::auth()->user()->name ?? 'Admin') + ->label('Created By') + ->rules(['required']), + ]; + } + + public function resolveRecord(): ?CheckPointName + { + $warnMsg = []; + $plant = Plant::where('name', $this->data['plant'])->first(); + if (!$plant) { + $warnMsg[] = "Plant not found"; // '" . $this->data['plant'] . "' + } + if (Str::length($this->data['name']) < 3) { // || !ctype_alnum($this->data['name']) + $warnMsg[] = "Invalid check point name 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 CheckPointName::updateOrCreate([ + 'name' => $this->data['name'], + 'plant_id' => $plant->id + ], + [ + 'created_by' => $this->data['created_by'] + ] + ); + // // return CheckPointName::firstOrNew([ + // // // Update existing records, matching them by `$this->data['column_name']` + // // 'email' => $this->data['email'], + // // ]); + + // return new CheckPointName(); + } + + public static function getCompletedNotificationBody(Import $import): string + { + $body = 'Your check point name 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; + } +}