|
@@ -5,11 +5,19 @@ namespace App\Filament\Company\Resources\Accounting\BudgetResource\RelationManag
|
5
|
5
|
use App\Filament\Tables\Columns\DeferredTextInputColumn;
|
6
|
6
|
use App\Models\Accounting\BudgetAllocation;
|
7
|
7
|
use App\Models\Accounting\BudgetItem;
|
|
8
|
+use App\Utilities\Currency\CurrencyConverter;
|
8
|
9
|
use Filament\Notifications\Notification;
|
9
|
10
|
use Filament\Resources\RelationManagers\RelationManager;
|
|
11
|
+use Filament\Support\RawJs;
|
10
|
12
|
use Filament\Tables\Actions\Action;
|
|
13
|
+use Filament\Tables\Actions\BulkAction;
|
|
14
|
+use Filament\Tables\Columns\IconColumn;
|
|
15
|
+use Filament\Tables\Columns\Summarizers\Summarizer;
|
11
|
16
|
use Filament\Tables\Columns\TextColumn;
|
|
17
|
+use Filament\Tables\Grouping\Group;
|
12
|
18
|
use Filament\Tables\Table;
|
|
19
|
+use Illuminate\Database\Eloquent\Builder;
|
|
20
|
+use Illuminate\Database\Eloquent\Collection;
|
13
|
21
|
use Livewire\Attributes\On;
|
14
|
22
|
|
15
|
23
|
class BudgetItemsRelationManager extends RelationManager
|
|
@@ -21,12 +29,9 @@ class BudgetItemsRelationManager extends RelationManager
|
21
|
29
|
// Store changes that are pending
|
22
|
30
|
public array $batchChanges = [];
|
23
|
31
|
|
24
|
|
- // Listen for events from the custom column
|
25
|
|
-
|
26
|
32
|
#[On('batch-column-changed')]
|
27
|
33
|
public function handleBatchColumnChanged($data): void
|
28
|
34
|
{
|
29
|
|
- ray($data);
|
30
|
35
|
// Store the changed value
|
31
|
36
|
$key = "{$data['recordKey']}.{$data['name']}";
|
32
|
37
|
$this->batchChanges[$key] = $data['value'];
|
|
@@ -35,7 +40,6 @@ class BudgetItemsRelationManager extends RelationManager
|
35
|
40
|
#[On('save-batch-changes')]
|
36
|
41
|
public function saveBatchChanges(): void
|
37
|
42
|
{
|
38
|
|
- ray('Saving batch changes');
|
39
|
43
|
foreach ($this->batchChanges as $key => $value) {
|
40
|
44
|
// Parse the composite key
|
41
|
45
|
[$recordKey, $column] = explode('.', $key, 2);
|
|
@@ -77,6 +81,54 @@ class BudgetItemsRelationManager extends RelationManager
|
77
|
81
|
->send();
|
78
|
82
|
}
|
79
|
83
|
|
|
84
|
+ protected function calculateTotalSum(array $budgetItemIds): int
|
|
85
|
+ {
|
|
86
|
+ // Get all applicable periods
|
|
87
|
+ $periods = BudgetAllocation::whereIn('budget_item_id', $budgetItemIds)
|
|
88
|
+ ->pluck('period')
|
|
89
|
+ ->unique()
|
|
90
|
+ ->values()
|
|
91
|
+ ->toArray();
|
|
92
|
+
|
|
93
|
+ $total = 0;
|
|
94
|
+
|
|
95
|
+ // Sum up each period
|
|
96
|
+ foreach ($periods as $period) {
|
|
97
|
+ $total += $this->calculatePeriodSum($budgetItemIds, $period);
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ return $total;
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ protected function calculatePeriodSum(array $budgetItemIds, string $period): int
|
|
104
|
+ {
|
|
105
|
+ // First get database values
|
|
106
|
+ $dbTotal = BudgetAllocation::whereIn('budget_item_id', $budgetItemIds)
|
|
107
|
+ ->where('period', $period)
|
|
108
|
+ ->sum('amount');
|
|
109
|
+
|
|
110
|
+ // Now add any batch changes
|
|
111
|
+ $batchTotal = 0;
|
|
112
|
+ foreach ($budgetItemIds as $itemId) {
|
|
113
|
+ $key = "{$itemId}.allocations_by_period.{$period}";
|
|
114
|
+ if (isset($this->batchChanges[$key])) {
|
|
115
|
+ // Get the current value from batch changes
|
|
116
|
+ $batchValue = CurrencyConverter::convertToCents($this->batchChanges[$key]);
|
|
117
|
+
|
|
118
|
+ // Find if there's a current allocation in DB
|
|
119
|
+ $existingAmount = BudgetAllocation::where('budget_item_id', $itemId)
|
|
120
|
+ ->where('period', $period)
|
|
121
|
+ ->first()
|
|
122
|
+ ->getRawOriginal('amount');
|
|
123
|
+
|
|
124
|
+ // Add the difference to our running total
|
|
125
|
+ $batchTotal += ($batchValue - $existingAmount);
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ return $dbTotal + $batchTotal;
|
|
130
|
+ }
|
|
131
|
+
|
80
|
132
|
public function table(Table $table): Table
|
81
|
133
|
{
|
82
|
134
|
$budget = $this->getOwnerRecord();
|
|
@@ -91,13 +143,11 @@ class BudgetItemsRelationManager extends RelationManager
|
91
|
143
|
->values()
|
92
|
144
|
->toArray();
|
93
|
145
|
|
94
|
|
- ray($this->batchChanges);
|
95
|
|
-
|
96
|
146
|
return $table
|
97
|
147
|
->recordTitleAttribute('account_id')
|
98
|
148
|
->paginated(false)
|
99
|
149
|
->modifyQueryUsing(
|
100
|
|
- fn ($query) => $query->with(['account', 'allocations'])
|
|
150
|
+ fn (Builder $query) => $query->with(['account', 'allocations'])
|
101
|
151
|
)
|
102
|
152
|
->headerActions([
|
103
|
153
|
Action::make('saveBatchChanges')
|
|
@@ -106,14 +156,130 @@ class BudgetItemsRelationManager extends RelationManager
|
106
|
156
|
->color('primary')
|
107
|
157
|
->icon('heroicon-o-check-circle'),
|
108
|
158
|
])
|
|
159
|
+ ->groups([
|
|
160
|
+ Group::make('account.category')
|
|
161
|
+ ->titlePrefixedWithLabel(false)
|
|
162
|
+ ->collapsible(),
|
|
163
|
+ ])
|
|
164
|
+ ->defaultGroup('account.category')
|
|
165
|
+ ->bulkActions([
|
|
166
|
+ BulkAction::make('clearAllocations')
|
|
167
|
+ ->label('Clear Allocations')
|
|
168
|
+ ->icon('heroicon-o-trash')
|
|
169
|
+ ->color('danger')
|
|
170
|
+ ->requiresConfirmation()
|
|
171
|
+ ->deselectRecordsAfterCompletion()
|
|
172
|
+ ->action(function (Collection $records) use ($periods) {
|
|
173
|
+ foreach ($records as $record) {
|
|
174
|
+ foreach ($periods as $period) {
|
|
175
|
+ $periodKey = "{$record->getKey()}.allocations_by_period.{$period}";
|
|
176
|
+ $this->batchChanges[$periodKey] = CurrencyConverter::convertCentsToFormatSimple(0);
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ $totalKey = "{$record->getKey()}.total";
|
|
180
|
+ $this->batchChanges[$totalKey] = CurrencyConverter::convertCentsToFormatSimple(0);
|
|
181
|
+ }
|
|
182
|
+ }),
|
|
183
|
+ ])
|
109
|
184
|
->columns(array_merge([
|
110
|
185
|
TextColumn::make('account.name')
|
111
|
186
|
->label('Accounts')
|
112
|
|
- ->sortable()
|
|
187
|
+ ->limit(30)
|
113
|
188
|
->searchable(),
|
|
189
|
+ DeferredTextInputColumn::make('total')
|
|
190
|
+ ->label('Total')
|
|
191
|
+ ->alignRight()
|
|
192
|
+ ->mask(RawJs::make('$money($input)'))
|
|
193
|
+ ->getStateUsing(function (BudgetItem $record, DeferredTextInputColumn $column) {
|
|
194
|
+ if (isset($this->batchChanges["{$record->getKey()}.{$column->getName()}"])) {
|
|
195
|
+ return $this->batchChanges["{$record->getKey()}.{$column->getName()}"];
|
|
196
|
+ }
|
|
197
|
+
|
|
198
|
+ $total = $record->allocations->sum(function (BudgetAllocation $budgetAllocation) {
|
|
199
|
+ return $budgetAllocation->getRawOriginal('amount');
|
|
200
|
+ });
|
|
201
|
+
|
|
202
|
+ return CurrencyConverter::convertCentsToFormatSimple($total);
|
|
203
|
+ })
|
|
204
|
+ ->batchMode()
|
|
205
|
+ ->summarize(
|
|
206
|
+ Summarizer::make()
|
|
207
|
+ ->using(function (\Illuminate\Database\Query\Builder $query) {
|
|
208
|
+ $budgetItemIds = $query->pluck('id')->toArray();
|
|
209
|
+ $total = $this->calculateTotalSum($budgetItemIds);
|
|
210
|
+
|
|
211
|
+ return CurrencyConverter::convertCentsToFormatSimple($total);
|
|
212
|
+ })
|
|
213
|
+ ),
|
|
214
|
+ IconColumn::make('disperseAction')
|
|
215
|
+ ->icon('heroicon-m-chevron-double-right')
|
|
216
|
+ ->color('primary')
|
|
217
|
+ ->label('')
|
|
218
|
+ ->default('')
|
|
219
|
+ ->action(
|
|
220
|
+ Action::make('disperse')
|
|
221
|
+ ->label('Disperse')
|
|
222
|
+ ->icon('heroicon-m-chevron-double-right')
|
|
223
|
+ ->color('primary')
|
|
224
|
+ ->iconButton()
|
|
225
|
+ ->action(function (BudgetItem $record) use ($periods) {
|
|
226
|
+ if (empty($periods)) {
|
|
227
|
+ return;
|
|
228
|
+ }
|
|
229
|
+
|
|
230
|
+ $totalKey = "{$record->getKey()}.total";
|
|
231
|
+ $totalAmount = $this->batchChanges[$totalKey] ?? null;
|
|
232
|
+
|
|
233
|
+ if (isset($totalAmount)) {
|
|
234
|
+ $totalCents = CurrencyConverter::convertToCents($totalAmount);
|
|
235
|
+ } else {
|
|
236
|
+ $totalCents = $record->allocations->sum(function (BudgetAllocation $budgetAllocation) {
|
|
237
|
+ return $budgetAllocation->getRawOriginal('amount');
|
|
238
|
+ });
|
|
239
|
+ }
|
|
240
|
+
|
|
241
|
+ $numPeriods = count($periods);
|
|
242
|
+
|
|
243
|
+ if ($numPeriods === 0) {
|
|
244
|
+ return;
|
|
245
|
+ }
|
|
246
|
+
|
|
247
|
+ if ($totalCents <= 0) {
|
|
248
|
+ foreach ($periods as $period) {
|
|
249
|
+ $periodKey = "{$record->getKey()}.allocations_by_period.{$period}";
|
|
250
|
+ $this->batchChanges[$periodKey] = CurrencyConverter::convertCentsToFormatSimple(0);
|
|
251
|
+ }
|
|
252
|
+
|
|
253
|
+ return;
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ $baseAmount = floor($totalCents / $numPeriods);
|
|
257
|
+ $remainder = $totalCents - ($baseAmount * $numPeriods);
|
|
258
|
+
|
|
259
|
+ foreach ($periods as $index => $period) {
|
|
260
|
+ $amount = $baseAmount + ($index === 0 ? $remainder : 0);
|
|
261
|
+ $formattedAmount = CurrencyConverter::convertCentsToFormatSimple($amount);
|
|
262
|
+
|
|
263
|
+ $periodKey = "{$record->getKey()}.allocations_by_period.{$period}";
|
|
264
|
+ $this->batchChanges[$periodKey] = $formattedAmount;
|
|
265
|
+ }
|
|
266
|
+ }),
|
|
267
|
+ ),
|
114
|
268
|
], collect($periods)->map(
|
115
|
269
|
fn ($period) => DeferredTextInputColumn::make("allocations_by_period.{$period}")
|
116
|
270
|
->label($period)
|
|
271
|
+ ->alignRight()
|
|
272
|
+ ->mask(RawJs::make('$money($input)'))
|
|
273
|
+ ->summarize(
|
|
274
|
+ Summarizer::make()
|
|
275
|
+ ->label($period)
|
|
276
|
+ ->using(function (\Illuminate\Database\Query\Builder $query) use ($period) {
|
|
277
|
+ $budgetItemIds = $query->pluck('id')->toArray();
|
|
278
|
+ $total = $this->calculatePeriodSum($budgetItemIds, $period);
|
|
279
|
+
|
|
280
|
+ return CurrencyConverter::convertCentsToFormatSimple($total);
|
|
281
|
+ })
|
|
282
|
+ )
|
117
|
283
|
->getStateUsing(function ($record, DeferredTextInputColumn $column) use ($period) {
|
118
|
284
|
$key = "{$record->getKey()}.{$column->getName()}";
|
119
|
285
|
|