Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filament fare resource #10

Merged
merged 4 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions app/Filament/Resources/FareResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\FareResource\Pages;
use App\Models\Enums\FareType;
use App\Models\Fare;
use Filament\Forms;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class FareResource extends Resource
{
protected static ?string $model = Fare::class;

protected static ?string $navigationGroup = 'operations';
protected static ?int $navigationSort = 4;

protected static ?string $navigationLabel = 'Fares';
protected static ?string $navigationIcon = 'heroicon-o-presentation-chart-line';

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make('Fare Information')
->description('When a fare is assigned to a subfleet, the price, cost and capacity can be overridden, so you can create default values that will apply to most of your subfleets, and change them where they will differ.')
->schema([
TextInput::make('code')->hint('How this fare class will show up on a ticket')->required()->string(),
TextInput::make('name')->hint('The fare class name, E.g "Economy" or "First"')->required()->string(),
Select::make('type')->hint('If this is a passenger or cargo fare')->options(FareType::labels())->native(false)->required(),
Toggle::make('active')->offIcon('heroicon-m-x-circle')->offColor('danger')->onIcon('heroicon-m-check-circle')->onColor('success')->default(true),
])->columns(3),
Forms\Components\Section::make('Base Fare Finances')
->schema([
TextInput::make('price')->hint('This is the price of a ticket or price per kg')->numeric(),
TextInput::make('cost')->hint('The operating cost per unit (passenger or kg)')->numeric(),
TextInput::make('capacity')->hint('Max seats or capacity available. This can be adjusted in the subfleet')->numeric(),
TextInput::make('notes')->hint('Any notes about this fare')->string(),
])->columns(2),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('code')->label('Code'),
TextColumn::make('name')->label('Name')->searchable(),
TextColumn::make('type')->label('Type')->formatStateUsing(fn ($state): string => FareType::label($state)),
TextColumn::make('price')->label('Price')->money(setting('units.currency')),
TextColumn::make('cost')->label('Cost')->money(setting('units.currency')),
TextColumn::make('notes')->label('Notes'),
IconColumn::make('active')->label('Active')->color(fn (Fare $record) => $record->active ? 'success' : 'danger')->icon(fn ($state) => $state ? 'heroicon-o-check-circle' : 'heroicon-o-x-circle'),
])
->filters([
Tables\Filters\TrashedFilter::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
Tables\Actions\ForceDeleteAction::make(),
Tables\Actions\RestoreAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
]),
])
->emptyStateActions([
Tables\Actions\CreateAction::make()->label('Add Fare'),
]);
}

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListFares::route('/'),
'create' => Pages\CreateFare::route('/create'),
'edit' => Pages\EditFare::route('/{record}/edit'),
];
}
}
16 changes: 16 additions & 0 deletions app/Filament/Resources/FareResource/Pages/CreateFare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Filament\Resources\FareResource\Pages;

use App\Filament\Resources\FareResource;
use Filament\Resources\Pages\CreateRecord;

class CreateFare extends CreateRecord
{
protected static string $resource = FareResource::class;

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
26 changes: 26 additions & 0 deletions app/Filament/Resources/FareResource/Pages/EditFare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Filament\Resources\FareResource\Pages;

use App\Filament\Resources\FareResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditFare extends EditRecord
{
protected static string $resource = FareResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\ForceDeleteAction::make(),
Actions\RestoreAction::make(),
];
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
24 changes: 24 additions & 0 deletions app/Filament/Resources/FareResource/Pages/ListFares.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Filament\Resources\FareResource\Pages;

use App\Filament\Actions\ExportAction;
use App\Filament\Actions\ImportAction;
use App\Filament\Resources\FareResource;
use App\Models\Enums\ImportExportType;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListFares extends ListRecords
{
protected static string $resource = FareResource::class;

protected function getHeaderActions(): array
{
return [
ExportAction::make('export')->arguments(['resourceTitle' => 'fares', 'exportType' => ImportExportType::FARES]),
ImportAction::make('import')->arguments(['resourceTitle' => 'fares', 'importType' => ImportExportType::FARES]),
Actions\CreateAction::make()->label('Add Fare')->icon('heroicon-o-plus-circle'),
];
}
}