feat(wallets): implement wallet and transaction management with associated models, policies, and resources
This commit is contained in:
19
app/Filament/Resources/Wallets/Pages/EditWallet.php
Normal file
19
app/Filament/Resources/Wallets/Pages/EditWallet.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Wallets\Pages;
|
||||
|
||||
use App\Filament\Resources\Wallets\WalletResource;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditWallet extends EditRecord
|
||||
{
|
||||
protected static string $resource = WalletResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
ViewAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
11
app/Filament/Resources/Wallets/Pages/ListWallets.php
Normal file
11
app/Filament/Resources/Wallets/Pages/ListWallets.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Wallets\Pages;
|
||||
|
||||
use App\Filament\Resources\Wallets\WalletResource;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListWallets extends ListRecords
|
||||
{
|
||||
protected static string $resource = WalletResource::class;
|
||||
}
|
||||
19
app/Filament/Resources/Wallets/Pages/ViewWallet.php
Normal file
19
app/Filament/Resources/Wallets/Pages/ViewWallet.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Wallets\Pages;
|
||||
|
||||
use App\Filament\Resources\Wallets\WalletResource;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewWallet extends ViewRecord
|
||||
{
|
||||
protected static string $resource = WalletResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Wallets\RelationManagers;
|
||||
|
||||
use App\Enums\WalletTransactionType;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class TransactionsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'transactions';
|
||||
|
||||
protected static ?string $title = 'تراکنشها';
|
||||
|
||||
protected static ?string $modelLabel = 'تراکنش';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'تراکنشها';
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('type')
|
||||
->label('نوع')
|
||||
->badge()
|
||||
->formatStateUsing(fn (WalletTransactionType $state): string => $state->label())
|
||||
->color(fn (WalletTransactionType $state): string => $state === WalletTransactionType::Credit ? 'success' : 'danger'),
|
||||
TextColumn::make('amount')
|
||||
->label('مبلغ')
|
||||
->formatStateUsing(fn (int $state): string => number_format($state).' تومان'),
|
||||
TextColumn::make('balance_before')
|
||||
->label('قبل')
|
||||
->formatStateUsing(fn (int $state): string => number_format($state)),
|
||||
TextColumn::make('balance_after')
|
||||
->label('بعد')
|
||||
->formatStateUsing(fn (int $state): string => number_format($state)),
|
||||
TextColumn::make('description')
|
||||
->label('توضیحات')
|
||||
->limit(40),
|
||||
TextColumn::make('creator.name')
|
||||
->label('ثبتکننده')
|
||||
->placeholder('سیستم'),
|
||||
TextColumn::make('created_at')
|
||||
->label('تاریخ')
|
||||
->dateTime()
|
||||
->sortable(),
|
||||
])
|
||||
->defaultSort('created_at', 'desc')
|
||||
->filters([
|
||||
SelectFilter::make('type')
|
||||
->label('نوع')
|
||||
->options([
|
||||
WalletTransactionType::Credit->value => WalletTransactionType::Credit->label(),
|
||||
WalletTransactionType::Debit->value => WalletTransactionType::Debit->label(),
|
||||
]),
|
||||
])
|
||||
->recordActions([])
|
||||
->toolbarActions([]);
|
||||
}
|
||||
}
|
||||
19
app/Filament/Resources/Wallets/Schemas/WalletForm.php
Normal file
19
app/Filament/Resources/Wallets/Schemas/WalletForm.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Wallets\Schemas;
|
||||
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class WalletForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Toggle::make('is_active')
|
||||
->label('فعال')
|
||||
->default(true),
|
||||
]);
|
||||
}
|
||||
}
|
||||
106
app/Filament/Resources/Wallets/Tables/WalletsTable.php
Normal file
106
app/Filament/Resources/Wallets/Tables/WalletsTable.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Wallets\Tables;
|
||||
|
||||
use App\Models\Wallet;
|
||||
use App\Services\WalletService;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
use RuntimeException;
|
||||
|
||||
class WalletsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('user.name')
|
||||
->label('کاربر')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('user.email')
|
||||
->label('ایمیل')
|
||||
->searchable()
|
||||
->toggleable(),
|
||||
TextColumn::make('user.mobile')
|
||||
->label('موبایل')
|
||||
->searchable()
|
||||
->toggleable(),
|
||||
TextColumn::make('balance')
|
||||
->label('موجودی (تومان)')
|
||||
->numeric()
|
||||
->sortable()
|
||||
->formatStateUsing(fn (int $state): string => number_format($state).' تومان'),
|
||||
IconColumn::make('is_active')
|
||||
->label('فعال')
|
||||
->boolean(),
|
||||
TextColumn::make('updated_at')
|
||||
->label('آخرین بروزرسانی')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->defaultSort('balance', 'desc')
|
||||
->filters([
|
||||
TernaryFilter::make('is_active')
|
||||
->label('وضعیت')
|
||||
->placeholder('همه')
|
||||
->trueLabel('فعال')
|
||||
->falseLabel('غیرفعال'),
|
||||
])
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
EditAction::make(),
|
||||
self::adjustAction('credit', 'واریز', Heroicon::OutlinedPlusCircle, 'success'),
|
||||
self::adjustAction('debit', 'برداشت', Heroicon::OutlinedMinusCircle, 'danger'),
|
||||
]);
|
||||
}
|
||||
|
||||
private static function adjustAction(string $type, string $label, Heroicon $icon, string $color): Action
|
||||
{
|
||||
return Action::make($type)
|
||||
->label($label)
|
||||
->icon($icon)
|
||||
->color($color)
|
||||
->schema([
|
||||
TextInput::make('amount')
|
||||
->label('مبلغ (تومان)')
|
||||
->numeric()
|
||||
->required()
|
||||
->minValue(1)
|
||||
->integer(),
|
||||
Textarea::make('description')
|
||||
->label('توضیحات')
|
||||
->maxLength(500)
|
||||
->rows(2),
|
||||
])
|
||||
->action(function (Wallet $record, array $data, WalletService $walletService): void {
|
||||
try {
|
||||
$transaction = $type === 'credit'
|
||||
? $walletService->credit($record, (int) $data['amount'], $data['description'] ?? null, auth()->user())
|
||||
: $walletService->debit($record, (int) $data['amount'], $data['description'] ?? null, auth()->user());
|
||||
|
||||
Notification::make()
|
||||
->title($type === 'credit' ? 'واریز انجام شد' : 'برداشت انجام شد')
|
||||
->body('موجودی جدید: '.number_format($transaction->balance_after).' تومان')
|
||||
->success()
|
||||
->send();
|
||||
} catch (RuntimeException $exception) {
|
||||
Notification::make()
|
||||
->title('خطا')
|
||||
->body($exception->getMessage())
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
67
app/Filament/Resources/Wallets/WalletResource.php
Normal file
67
app/Filament/Resources/Wallets/WalletResource.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Wallets;
|
||||
|
||||
use App\Filament\Resources\Wallets\Pages\EditWallet;
|
||||
use App\Filament\Resources\Wallets\Pages\ListWallets;
|
||||
use App\Filament\Resources\Wallets\Pages\ViewWallet;
|
||||
use App\Filament\Resources\Wallets\RelationManagers\TransactionsRelationManager;
|
||||
use App\Filament\Resources\Wallets\Schemas\WalletForm;
|
||||
use App\Filament\Resources\Wallets\Tables\WalletsTable;
|
||||
use App\Models\Wallet;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class WalletResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Wallet::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedWallet;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'مالی';
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'id';
|
||||
|
||||
protected static ?string $modelLabel = 'کیف پول';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'کیف پولها';
|
||||
|
||||
protected static ?string $navigationLabel = 'کیف پولها';
|
||||
|
||||
protected static ?int $navigationSort = 10;
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return WalletForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return WalletsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
TransactionsRelationManager::class,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListWallets::route('/'),
|
||||
'view' => ViewWallet::route('/{record}'),
|
||||
'edit' => EditWallet::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function canCreate(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user