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 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Filament\Company\Resources\Accounting\BudgetResource\RelationManagers;
  3. use App\Models\Accounting\BudgetItem;
  4. use Filament\Resources\RelationManagers\RelationManager;
  5. use Filament\Tables\Columns\TextColumn;
  6. use Filament\Tables\Columns\TextInputColumn;
  7. use Filament\Tables\Table;
  8. class BudgetItemsRelationManager extends RelationManager
  9. {
  10. protected static string $relationship = 'budgetItems';
  11. protected static bool $isLazy = false;
  12. public function table(Table $table): Table
  13. {
  14. $budget = $this->getOwnerRecord();
  15. // Get distinct periods for this budget
  16. $periods = \App\Models\Accounting\BudgetAllocation::query()
  17. ->join('budget_items', 'budget_allocations.budget_item_id', '=', 'budget_items.id')
  18. ->where('budget_items.budget_id', $budget->id)
  19. ->orderBy('start_date')
  20. ->pluck('period')
  21. ->unique()
  22. ->values()
  23. ->toArray();
  24. return $table
  25. ->recordTitleAttribute('account_id')
  26. ->paginated(false)
  27. ->modifyQueryUsing(
  28. fn ($query) => $query->with(['account', 'allocations'])
  29. )
  30. ->columns(array_merge([
  31. TextColumn::make('account.name')
  32. ->label('Accounts')
  33. ->sortable()
  34. ->searchable(),
  35. ], collect($periods)->map(
  36. fn ($period) => TextInputColumn::make("allocations_by_period.{$period}")
  37. ->label($period)
  38. ->getStateUsing(
  39. fn ($record) => $record->allocations->firstWhere('period', $period)?->amount
  40. )
  41. ->updateStateUsing(function (BudgetItem $record, $state) use ($period) {
  42. $allocation = $record->allocations->firstWhere('period', $period);
  43. if ($allocation) {
  44. $allocation->update(['amount' => $state]);
  45. }
  46. })
  47. )->all()));
  48. }
  49. }