Merge pull request 'ranjith-dev' (#657) from ranjith-dev into master
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled

Reviewed-on: #657
This commit was merged in pull request #657.
This commit is contained in:
2026-05-27 06:02:56 +00:00
10 changed files with 681 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Filament\Exports;
use App\Models\EmployeeMaster;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;
class EmployeeMasterExporter extends Exporter
{
protected static ?string $model = EmployeeMaster::class;
public static function getColumns(): array
{
static $rowNumber = 0;
return [
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('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;
}
}

View File

@@ -0,0 +1,147 @@
<?php
namespace App\Filament\Imports;
use App\Models\EmployeeMaster;
use App\Models\Plant;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Filament\Facades\Filament;
use Str;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Carbon\Carbon;
class EmployeeMasterImporter extends Importer
{
protected static ?string $model = EmployeeMaster::class;
public static function getColumns(): array
{
return [
ImportColumn::make('plant')
->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;
}
}

View File

@@ -0,0 +1,217 @@
<?php
namespace App\Filament\Resources;
use App\Filament\Exports\EmployeeMasterExporter;
use App\Filament\Imports\EmployeeMasterImporter;
use App\Filament\Resources\EmployeeMasterResource\Pages;
use App\Filament\Resources\EmployeeMasterResource\RelationManagers;
use App\Models\EmployeeMaster;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Actions\ImportAction;
use Illuminate\Validation\Rule;
class EmployeeMasterResource extends Resource
{
protected static ?string $model = EmployeeMaster::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('plant_id')
->label('Plant')
->relationship('plant', 'name')
->required(),
Forms\Components\TextInput::make('name')
->label('Name')
->required()
->reactive()
->extraInputAttributes([
'oninput' => 'this.value = this.value.replace(/[^a-zA-Z\s]/g, "")',
]),
Forms\Components\TextInput::make('code')
->label('ID')
->extraInputAttributes([
'oninput' => 'this.value = this.value.replace(/[^a-zA-Z0-9]/g, "")',])
->required()
->unique(
table: 'employee_masters',
column: 'code',
ignoreRecord: true
)
->validationMessages([
'unique' => 'Duplicate employee code already exists.',
]),
Forms\Components\TextInput::make('department')
->label('Department')
->required(),
Forms\Components\TextInput::make('designation')
->label('Designation')
->extraInputAttributes([
'oninput' => 'this.value = this.value.replace(/[^a-zA-Z\s]/g, "")',])
->required(),
Forms\Components\TextInput::make('email')
->label('Email')
->email()
->required(),
Forms\Components\TextInput::make('mobile_number')
->label('Mobile Number')
->length(10)
->reactive()
->extraInputAttributes([
'oninput' => 'this.value = this.value.replace(/[^0-9]/g, "").slice(0, 10)', // blocks non-numbers + limits to 10 chars
'maxlength' => 10,
])
->required()
->unique(
table: 'employee_masters',
column: 'mobile_number',
ignoreRecord: true
)
->validationMessages([
'unique' => 'Duplicate mobile number already exists.',
]),
Forms\Components\Hidden::make('created_by')
->label('Created by')
->default(Filament::auth()->user()?->name ?? ''),
Forms\Components\Hidden::make('updated_by')
->label('Updated by')
->default(Filament::auth()->user()?->name ?? ''),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('No.')
->label('No.')
->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
$paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
})
->alignCenter(),
Tables\Columns\TextColumn::make('plant.name')
->numeric()
->sortable()
->alignCenter(),
Tables\Columns\TextColumn::make('name')
->label('Name')
->sortable()
->alignCenter()
->searchable(),
Tables\Columns\TextColumn::make('code')
->label('Employee ID')
->sortable()
->alignCenter()
->searchable(),
Tables\Columns\TextColumn::make('department')
->label('Department')
->sortable()
->alignCenter()
->searchable(),
Tables\Columns\TextColumn::make('designation')
->label('Designation')
->sortable()
->alignCenter()
->searchable(),
Tables\Columns\TextColumn::make('email')
->label('Email')
->sortable()
->alignCenter()
->searchable(),
Tables\Columns\TextColumn::make('mobile_number')
->label('Mobile Number')
->sortable()
->alignCenter()
->searchable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->alignCenter(),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true)
->alignCenter(),
Tables\Columns\TextColumn::make('created_by')
->label('Created by')
->sortable()
->alignCenter(),
Tables\Columns\TextColumn::make('updated_by')
->label('Updated by')
->sortable()
->toggleable(isToggledHiddenByDefault: true)
->alignCenter(),
Tables\Columns\TextColumn::make('deleted_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true)
->alignCenter(),
])
->filters([
Tables\Filters\TrashedFilter::make(),
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
]),
])
->headerActions([
ImportAction::make()
->importer(EmployeeMasterImporter::class)
->visible(function() {
return Filament::auth()->user()->can('view import employee master');
}),
ExportAction::make()
->exporter(EmployeeMasterExporter::class)
->visible(function() {
return Filament::auth()->user()->can('view export employee master');
}),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListEmployeeMasters::route('/'),
'create' => Pages\CreateEmployeeMaster::route('/create'),
'view' => Pages\ViewEmployeeMaster::route('/{record}'),
'edit' => Pages\EditEmployeeMaster::route('/{record}/edit'),
];
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Filament\Resources\EmployeeMasterResource\Pages;
use App\Filament\Resources\EmployeeMasterResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
class CreateEmployeeMaster extends CreateRecord
{
protected static string $resource = EmployeeMasterResource::class;
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Filament\Resources\EmployeeMasterResource\Pages;
use App\Filament\Resources\EmployeeMasterResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditEmployeeMaster extends EditRecord
{
protected static string $resource = EmployeeMasterResource::class;
protected function getHeaderActions(): array
{
return [
Actions\ViewAction::make(),
Actions\DeleteAction::make(),
Actions\ForceDeleteAction::make(),
Actions\RestoreAction::make(),
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\EmployeeMasterResource\Pages;
use App\Filament\Resources\EmployeeMasterResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListEmployeeMasters extends ListRecords
{
protected static string $resource = EmployeeMasterResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\EmployeeMasterResource\Pages;
use App\Filament\Resources\EmployeeMasterResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;
class ViewEmployeeMaster extends ViewRecord
{
protected static string $resource = EmployeeMasterResource::class;
protected function getHeaderActions(): array
{
return [
Actions\EditAction::make(),
];
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class EmployeeMaster extends Model
{
use SoftDeletes;
protected $fillable = [
'plant_id',
'name',
'code',
'department',
'designation',
'email',
'mobile_number',
'created_at',
'updated_at',
'created_by',
'updated_by',
];
public function plant(): BelongsTo
{
return $this->belongsTo(Plant::class);
}
}

View File

@@ -0,0 +1,106 @@
<?php
namespace App\Policies;
use Illuminate\Auth\Access\Response;
use App\Models\EmployeeMaster;
use App\Models\User;
class EmployeeMasterPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return $user->checkPermissionTo('view-any EmployeeMaster');
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, EmployeeMaster $employeemaster): bool
{
return $user->checkPermissionTo('view EmployeeMaster');
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return $user->checkPermissionTo('create EmployeeMaster');
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, EmployeeMaster $employeemaster): bool
{
return $user->checkPermissionTo('update EmployeeMaster');
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, EmployeeMaster $employeemaster): bool
{
return $user->checkPermissionTo('delete EmployeeMaster');
}
/**
* Determine whether the user can delete any models.
*/
public function deleteAny(User $user): bool
{
return $user->checkPermissionTo('delete-any EmployeeMaster');
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, EmployeeMaster $employeemaster): bool
{
return $user->checkPermissionTo('restore EmployeeMaster');
}
/**
* Determine whether the user can restore any models.
*/
public function restoreAny(User $user): bool
{
return $user->checkPermissionTo('restore-any EmployeeMaster');
}
/**
* Determine whether the user can replicate the model.
*/
public function replicate(User $user, EmployeeMaster $employeemaster): bool
{
return $user->checkPermissionTo('replicate EmployeeMaster');
}
/**
* Determine whether the user can reorder the models.
*/
public function reorder(User $user): bool
{
return $user->checkPermissionTo('reorder EmployeeMaster');
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, EmployeeMaster $employeemaster): bool
{
return $user->checkPermissionTo('force-delete EmployeeMaster');
}
/**
* Determine whether the user can permanently delete any models.
*/
public function forceDeleteAny(User $user): bool
{
return $user->checkPermissionTo('force-delete-any EmployeeMaster');
}
}

View File

@@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$sql = <<<'SQL'
CREATE TABLE employee_masters (
id BIGINT GENERATED always AS IDENTITY PRIMARY KEY,
plant_id BIGINT NOT NULL,
name TEXT DEFAULT NULL,
code TEXT DEFAULT NULL,
department TEXT DEFAULT NULL,
designation TEXT DEFAULT NULL,
email TEXT DEFAULT NULL,
mobile_number TEXT DEFAULT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
created_by TEXT DEFAULT NULL,
updated_by TEXT DEFAULT NULL,
deleted_at TIMESTAMP,
FOREIGN KEY (plant_id) REFERENCES plants (id)
);
SQL;
DB::statement($sql);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('employee_masters');
}
};