Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BudgetItemsRelationManager.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Filament\Company\Resources\Accounting\BudgetResource\RelationManagers;
  3. use App\Filament\Tables\Columns\DeferredTextInputColumn;
  4. use App\Models\Accounting\BudgetAllocation;
  5. use App\Models\Accounting\BudgetItem;
  6. use Filament\Notifications\Notification;
  7. use Filament\Resources\RelationManagers\RelationManager;
  8. use Filament\Tables\Actions\Action;
  9. use Filament\Tables\Columns\TextColumn;
  10. use Filament\Tables\Table;
  11. use Livewire\Attributes\On;
  12. class BudgetItemsRelationManager extends RelationManager
  13. {
  14. protected static string $relationship = 'budgetItems';
  15. protected static bool $isLazy = false;
  16. // Store changes that are pending
  17. public array $batchChanges = [];
  18. // Listen for events from the custom column
  19. #[On('batch-column-changed')]
  20. public function handleBatchColumnChanged($data): void
  21. {
  22. ray($data);
  23. // Store the changed value
  24. $key = "{$data['recordKey']}.{$data['name']}";
  25. $this->batchChanges[$key] = $data['value'];
  26. }
  27. #[On('save-batch-changes')]
  28. public function saveBatchChanges(): void
  29. {
  30. ray('Saving batch changes');
  31. foreach ($this->batchChanges as $key => $value) {
  32. // Parse the composite key
  33. [$recordKey, $column] = explode('.', $key, 2);
  34. // Extract period from the column name (e.g., "allocations_by_period.2023-Q1")
  35. preg_match('/allocations_by_period\.(.+)/', $column, $matches);
  36. $period = $matches[1] ?? null;
  37. if (! $period) {
  38. continue;
  39. }
  40. // Find the record
  41. $record = BudgetItem::find($recordKey);
  42. if (! $record) {
  43. continue;
  44. }
  45. // Update the allocation
  46. $allocation = $record->allocations->firstWhere('period', $period);
  47. if ($allocation) {
  48. $allocation->update(['amount' => $value]);
  49. } else {
  50. $record->allocations()->create([
  51. 'period' => $period,
  52. 'amount' => $value,
  53. // Add other required fields
  54. ]);
  55. }
  56. }
  57. // Clear the batch changes
  58. $this->batchChanges = [];
  59. // Notify the user
  60. Notification::make()
  61. ->title('Budget allocations updated')
  62. ->success()
  63. ->send();
  64. }
  65. public function table(Table $table): Table
  66. {
  67. $budget = $this->getOwnerRecord();
  68. // Get distinct periods for this budget
  69. $periods = BudgetAllocation::query()
  70. ->join('budget_items', 'budget_allocations.budget_item_id', '=', 'budget_items.id')
  71. ->where('budget_items.budget_id', $budget->id)
  72. ->orderBy('start_date')
  73. ->pluck('period')
  74. ->unique()
  75. ->values()
  76. ->toArray();
  77. ray($this->batchChanges);
  78. return $table
  79. ->recordTitleAttribute('account_id')
  80. ->paginated(false)
  81. ->modifyQueryUsing(
  82. fn ($query) => $query->with(['account', 'allocations'])
  83. )
  84. ->headerActions([
  85. Action::make('saveBatchChanges')
  86. ->label('Save All Changes')
  87. ->action('saveBatchChanges')
  88. ->color('primary')
  89. ->icon('heroicon-o-check-circle'),
  90. ])
  91. ->columns(array_merge([
  92. TextColumn::make('account.name')
  93. ->label('Accounts')
  94. ->sortable()
  95. ->searchable(),
  96. ], collect($periods)->map(
  97. fn ($period) => DeferredTextInputColumn::make("allocations_by_period.{$period}")
  98. ->label($period)
  99. ->getStateUsing(function ($record, DeferredTextInputColumn $column) use ($period) {
  100. $key = "{$record->getKey()}.{$column->getName()}";
  101. // Check if batch change exists
  102. if (isset($this->batchChanges[$key])) {
  103. return $this->batchChanges[$key];
  104. }
  105. return $record->allocations->firstWhere('period', $period)?->amount;
  106. })
  107. ->batchMode(),
  108. )->all()));
  109. }
  110. }