Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TransactionResource.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Filament\Company\Resources\Accounting;
  3. use App\Enums\DateFormat;
  4. use App\Filament\Company\Resources\Accounting\TransactionResource\Pages;
  5. use App\Models\Accounting\Transaction;
  6. use App\Models\Banking\Account;
  7. use App\Models\Setting\Localization;
  8. use Filament\Forms;
  9. use Filament\Forms\Form;
  10. use Filament\Resources\Resource;
  11. use Filament\Support\Enums\FontWeight;
  12. use Filament\Tables;
  13. use Filament\Tables\Table;
  14. use Illuminate\Support\Carbon;
  15. class TransactionResource extends Resource
  16. {
  17. protected static ?string $model = Transaction::class;
  18. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  19. public static function form(Form $form): Form
  20. {
  21. return $form
  22. ->schema([
  23. Forms\Components\DatePicker::make('posted_at')
  24. ->label('Posted At')
  25. ->required()
  26. ->default(now()),
  27. Forms\Components\TextInput::make('description')
  28. ->label('Description'),
  29. Forms\Components\Select::make('type')
  30. ->label('Type')
  31. ->options([
  32. 'expense' => 'Expense',
  33. 'income' => 'Income',
  34. 'transfer' => 'Transfer',
  35. ])
  36. ->required(),
  37. Forms\Components\Select::make('method')
  38. ->label('Method')
  39. ->options([
  40. 'deposit' => 'Deposit',
  41. 'withdrawal' => 'Withdrawal',
  42. ])
  43. ->required(),
  44. Forms\Components\TextInput::make('amount')
  45. ->label('Amount')
  46. ->money(static function (Forms\Get $get) {
  47. $account = $get('account_id');
  48. if ($account) {
  49. $account = Account::find($account);
  50. if ($account) {
  51. return $account->currency_code;
  52. }
  53. }
  54. return 'USD';
  55. })
  56. ->required(),
  57. Forms\Components\Select::make('category_id')
  58. ->label('Category')
  59. ->relationship('category', 'name')
  60. ->searchable()
  61. ->preload()
  62. ->required(),
  63. ]);
  64. }
  65. public static function table(Table $table): Table
  66. {
  67. return $table
  68. ->columns([
  69. Tables\Columns\TextColumn::make('posted_at')
  70. ->label('Date')
  71. ->formatStateUsing(static function ($state) {
  72. $dateFormat = Localization::firstOrFail()->date_format->value ?? DateFormat::DEFAULT;
  73. return Carbon::parse($state)->translatedFormat($dateFormat);
  74. }),
  75. Tables\Columns\TextColumn::make('description')
  76. ->wrap()
  77. ->label('Description'),
  78. Tables\Columns\TextColumn::make('category.name')
  79. ->label('Category')
  80. ->html()
  81. ->formatStateUsing(function ($state, Transaction $record) {
  82. $color = $record->category->color ?? '#000000';
  83. return "<span style='display: inline-block; width: 8px; height: 8px; background-color: {$color}; border-radius: 50%; margin-right: 3px;'></span> {$state}";
  84. }),
  85. Tables\Columns\TextColumn::make('amount')
  86. ->label('Amount')
  87. ->sortable()
  88. ->weight(FontWeight::Medium)
  89. ->color(static fn (Transaction $record) => $record->type === 'expense' ? 'danger' : null)
  90. ->currency(static fn (Transaction $record) => $record->account->currency_code, true),
  91. ])
  92. ->filters([
  93. //
  94. ])
  95. ->actions([
  96. Tables\Actions\EditAction::make(),
  97. ])
  98. ->bulkActions([
  99. Tables\Actions\BulkActionGroup::make([
  100. Tables\Actions\DeleteBulkAction::make(),
  101. ]),
  102. ]);
  103. }
  104. public static function getRelations(): array
  105. {
  106. return [
  107. //
  108. ];
  109. }
  110. public static function getPages(): array
  111. {
  112. return [
  113. 'index' => Pages\ListTransactions::route('/'),
  114. 'create' => Pages\CreateTransaction::route('/create'),
  115. 'edit' => Pages\EditTransaction::route('/{record}/edit'),
  116. ];
  117. }
  118. }