diff --git a/app/Filament/Resources/ExpenseResource.php b/app/Filament/Resources/ExpenseResource.php index 28ced381..0d743b15 100644 --- a/app/Filament/Resources/ExpenseResource.php +++ b/app/Filament/Resources/ExpenseResource.php @@ -73,6 +73,11 @@ public static function form(Forms\Form $form): Forms\Form Forms\Components\DatePicker::make('date') ->required() ->maxDate(now()), + Forms\Components\Select::make('categories') + ->multiple() + ->relationship('categories', 'name') + ->preload() + ->required(), Forms\Components\Select::make('approval_status') ->options([ 'pending' => 'Pending', @@ -135,6 +140,10 @@ public static function table(Tables\Table $table): Tables\Table Tables\Columns\TextColumn::make('date') ->date() ->sortable(), + Tables\Columns\TextColumn::make('categories.name') + ->badge() + ->separator(',') + ->searchable(), Tables\Columns\TextColumn::make('user.name') ->label('Submitted By') ->searchable(), @@ -156,6 +165,10 @@ public static function table(Tables\Table $table): Tables\Table 'approved' => 'Approved', 'rejected' => 'Rejected', ]), + Tables\Filters\SelectFilter::make('categories') + ->relationship('categories', 'name') + ->multiple() + ->preload(), ]) ->actions([ Tables\Actions\ViewAction::make(), diff --git a/app/Models/Expense.php b/app/Models/Expense.php index 57182b63..5c5c3386 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Support\Facades\Auth; use App\Notifications\ExpenseApprovalNotification; use App\Services\ExchangeRateService; @@ -56,6 +57,10 @@ public function costCenter(): BelongsTo return $this->belongsTo(CostCenter::class); } + public function categories(): BelongsToMany + { + return $this->belongsToMany(Category::class); + } public function supplier(): BelongsTo { return $this->belongsTo(Supplier::class, 'supplier_id', 'supplier_id'); diff --git a/database/migrations/2024_02_20_000000_add_category_support_to_expenses.php b/database/migrations/2024_02_20_000000_add_category_support_to_expenses.php new file mode 100644 index 00000000..e4b25867 --- /dev/null +++ b/database/migrations/2024_02_20_000000_add_category_support_to_expenses.php @@ -0,0 +1,25 @@ + + +id(); + $table->foreignId('expense_id')->constrained()->onDelete('cascade'); + $table->foreignId('category_id')->constrained()->onDelete('cascade'); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('expense_category'); + } +}; \ No newline at end of file