You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransactionResource.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\BankAccount;
  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. $bankAccount = $get('bank_account_id');
  48. $bankAccount = BankAccount::find($bankAccount);
  49. $account = $bankAccount->account ?? null;
  50. if ($account) {
  51. return $account->currency_code;
  52. }
  53. return 'USD';
  54. })
  55. ->required(),
  56. Forms\Components\Select::make('category_id')
  57. ->label('Category')
  58. ->relationship('category', 'name')
  59. ->searchable()
  60. ->preload()
  61. ->required(),
  62. ]);
  63. }
  64. public static function table(Table $table): Table
  65. {
  66. return $table
  67. ->columns([
  68. Tables\Columns\TextColumn::make('posted_at')
  69. ->label('Date')
  70. ->formatStateUsing(static function ($state) {
  71. $dateFormat = Localization::firstOrFail()->date_format->value ?? DateFormat::DEFAULT;
  72. return Carbon::parse($state)->translatedFormat($dateFormat);
  73. }),
  74. Tables\Columns\TextColumn::make('bankAccount.account.name')
  75. ->label('Account')
  76. ->sortable(),
  77. Tables\Columns\TextColumn::make('description')
  78. ->limit(50)
  79. ->label('Description'),
  80. Tables\Columns\TextColumn::make('category.name')
  81. ->label('Category')
  82. ->html()
  83. ->formatStateUsing(static function ($state, Transaction $record) {
  84. $color = $record->category->color ?? '#000000';
  85. return "<span style='display: inline-block; width: 8px; height: 8px; background-color: {$color}; border-radius: 50%; margin-right: 3px;'></span> {$state}";
  86. }),
  87. Tables\Columns\TextColumn::make('amount')
  88. ->label('Amount')
  89. ->sortable()
  90. ->weight(FontWeight::Medium)
  91. ->color(static fn (Transaction $record) => $record->type === 'expense' ? 'danger' : null)
  92. ->currency(static fn (Transaction $record) => $record->bankAccount->account->currency_code ?? 'USD', true),
  93. ])
  94. ->defaultSort('posted_at', 'desc')
  95. ->filters([
  96. //
  97. ])
  98. ->actions([
  99. Tables\Actions\EditAction::make(),
  100. ])
  101. ->bulkActions([
  102. Tables\Actions\BulkActionGroup::make([
  103. Tables\Actions\DeleteBulkAction::make(),
  104. ]),
  105. ]);
  106. }
  107. public static function getRelations(): array
  108. {
  109. return [
  110. //
  111. ];
  112. }
  113. public static function getPages(): array
  114. {
  115. return [
  116. 'index' => Pages\ListTransactions::route('/'),
  117. 'create' => Pages\CreateTransaction::route('/create'),
  118. 'edit' => Pages\EditTransaction::route('/{record}/edit'),
  119. ];
  120. }
  121. }