diff --git a/app/Filament/Exports/EmployeeMasterExporter.php b/app/Filament/Exports/EmployeeMasterExporter.php new file mode 100644 index 0000000..3122260 --- /dev/null +++ b/app/Filament/Exports/EmployeeMasterExporter.php @@ -0,0 +1,63 @@ +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('NAME'), + ExportColumn::make('code') + ->label('CODE'), + ExportColumn::make('department') + ->label('DEPARTMENT'), + ExportColumn::make('designation') + ->label('DESIGNATION'), + ExportColumn::make('email') + ->label('EMAIL'), + ExportColumn::make('mobile_number') + ->label('MOBILE 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 employee master 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/EmployeeMasterImporter.php b/app/Filament/Imports/EmployeeMasterImporter.php new file mode 100644 index 0000000..4c103df --- /dev/null +++ b/app/Filament/Imports/EmployeeMasterImporter.php @@ -0,0 +1,147 @@ +requiredMapping() + ->exampleHeader('PLANT CODE') + ->example('1000') + ->label('PLANT CODE') + ->relationship(resolveUsing: 'code') + ->rules(['required']), + ImportColumn::make('name') + ->exampleHeader('EMPLOYEE NAME') + ->example('John Doe') + ->label('EMPLOYEE NAME'), + ImportColumn::make('code') + ->exampleHeader('EMPLOYEE ID') + ->example('EMP001') + ->label('EMPLOYEE ID'), + ImportColumn::make('department') + ->exampleHeader('DEPARTMENT') + ->example('HR') + ->label('DEPARTMENT'), + ImportColumn::make('designation') + ->exampleHeader('DESIGNATION') + ->example('Manager') + ->label('DESIGNATION'), + ImportColumn::make('email') + ->exampleHeader('EMAIL') + ->example('john.doe@example.com') + ->label('EMAIL'), + ImportColumn::make('mobile_number') + ->exampleHeader('MOBILE NUMBER') + ->example('1234567890') + ->label('MOBILE NUMBER'), + ]; + } + + public function resolveRecord(): ?EmployeeMaster + { + $warnMsg = []; + $plantCod = trim($this->data['plant']) ?? ''; + $employeeName = trim($this->data['name']) ?? ''; + $employeeCode = trim($this->data['code']) ?? ''; + $employeeDepartment = trim($this->data['department']) ?? ''; + $employeeDesignation = trim($this->data['designation']) ?? ''; + $employeeEmail = trim($this->data['email']) ?? ''; + $employeeMobile = trim($this->data['mobile_number']) ?? ''; + $createdBy = Filament::auth()->user()?->name ?? ''; + $updatedBy = $createdBy; + + $plantId = null; + + if ($plantCod == null || $plantCod == '' || ! $plantCod) { + $warnMsg[] = "Plant code can't be empty!"; + } elseif (! is_numeric($plantCod)) { + $warnMsg[] = "Plant code '{$plantCod}' should contain only numeric values!"; + } elseif (Str::length($plantCod) < 4 || Str::length($plantCod) > 7) { + $warnMsg[] = "Plant code '{$plantCod}' must be between 4 and 7 digits only!"; + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCod)) { + $warnMsg[] = "Invalid plant code '{$plantCod}' found!"; + } else { + $plant = Plant::where('code', $plantCod)->first(); + if (! $plant) { + $warnMsg[] = 'Plant not found!'; + } else { + $plantId = $plant->id; + } + } + + if ($employeeCode == null || $employeeCode == '' || ! $employeeCode) { + $warnMsg[] = "Employee ID can't be empty!"; + } elseif (Str::length($employeeCode) < 3 || Str::length($employeeCode) > 20) { + $warnMsg[] = "Employee ID '{$employeeCode}' must be between 3 and 20 characters only!"; + } + if ($employeeEmail != null && $employeeEmail != '' && ! filter_var($employeeEmail, FILTER_VALIDATE_EMAIL)) { + $warnMsg[] = "Invalid email address '{$employeeEmail}' found!"; + } + if ($employeeMobile != null && $employeeMobile != '' && ! is_numeric($employeeMobile)) { + $warnMsg[] = "Mobile number '{$employeeMobile}' should contain only numeric values!"; + } elseif (Str::length($employeeMobile) < 10 || Str::length($employeeMobile) > 10) { + $warnMsg[] = "Mobile number '{$employeeMobile}' must be exactly 10 digits!"; + } + else + { + $existingMobile = EmployeeMaster::where('mobile_number', $employeeMobile) + ->where('code', '!=', $employeeCode) + ->first(); + + if ($existingMobile) { + $warnMsg[] = "Mobile number '{$employeeMobile}' is already assigned to another employee!"; + } + } + + if (! empty($warnMsg)) { + throw new RowImportFailedException(implode(', ', $warnMsg)); + } + else + { + EmployeeMaster::updateOrCreate( + [ + 'plant_id' => $plantId ?? null, + 'code' => $employeeCode ?? null, + ], + [ + 'name' => $employeeName ?? null, + 'email' => $employeeEmail ?? null, + 'mobile_number' => $employeeMobile ?? null, + 'department' => $employeeDepartment ?? null, + 'designation' => $employeeDesignation ?? null, + 'created_by' => $createdBy ?? null, + 'updated_by' => $updatedBy ?? null, + ] + ); + } + + return null; + } + + public static function getCompletedNotificationBody(Import $import): string + { + $body = 'Your employee master 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; + } +}