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.

BudgetItemsRelationManager.php 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Filament\Company\Resources\Accounting\BudgetResource\RelationManagers;
  3. use App\Models\Accounting\BudgetAllocation;
  4. use Filament\Forms\Components\Grid;
  5. use Filament\Forms\Components\TextInput;
  6. use Filament\Resources\RelationManagers\RelationManager;
  7. use Filament\Tables;
  8. use Filament\Tables\Table;
  9. class BudgetItemsRelationManager extends RelationManager
  10. {
  11. protected static string $relationship = 'budgetItems';
  12. protected static bool $isLazy = false;
  13. public function table(Table $table): Table
  14. {
  15. return $table
  16. ->recordTitleAttribute('account_id')
  17. ->columns([
  18. Tables\Columns\TextColumn::make('account.name')
  19. ->label('Account')
  20. ->sortable()
  21. ->searchable(),
  22. Tables\Columns\TextColumn::make('allocations_sum_amount')
  23. ->label('Total Allocations')
  24. ->sortable()
  25. ->alignEnd()
  26. ->sum('allocations', 'amount')
  27. ->money(divideBy: 100),
  28. ])
  29. ->filters([
  30. //
  31. ])
  32. ->headerActions([
  33. // Tables\Actions\CreateAction::make(),
  34. ])
  35. ->actions([
  36. Tables\Actions\Action::make('editAllocations')
  37. ->label('Edit Allocations')
  38. ->icon('heroicon-o-pencil')
  39. ->modalHeading(fn ($record) => "Edit Allocations for {$record->account->name}")
  40. ->modalWidth('xl')
  41. ->form(function ($record) {
  42. $fields = [];
  43. // Get allocations ordered by date
  44. $allocations = $record->allocations()->orderBy('start_date')->get();
  45. foreach ($allocations as $allocation) {
  46. $fields[] = TextInput::make("allocations.{$allocation->id}")
  47. ->label($allocation->period)
  48. ->numeric()
  49. ->default(function () use ($allocation) {
  50. return $allocation->amount;
  51. })
  52. ->prefix('$')
  53. ->live(debounce: 500)
  54. ->afterStateUpdated(function (TextInput $component, $state) {
  55. // Format the value as needed
  56. $component->state(number_format($state, 2, '.', ''));
  57. });
  58. }
  59. return [
  60. Grid::make()
  61. ->schema($fields)
  62. ->columns(3),
  63. ];
  64. })
  65. ->action(function (array $data, $record) {
  66. foreach ($data['allocations'] as $allocationId => $amount) {
  67. BudgetAllocation::find($allocationId)->update([
  68. 'amount' => $amount,
  69. ]);
  70. }
  71. }),
  72. ])
  73. ->bulkActions([
  74. // Tables\Actions\BulkActionGroup::make([
  75. // Tables\Actions\DeleteBulkAction::make(),
  76. // ]),
  77. ]);
  78. }
  79. }