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.8KB

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