Andrew Wallo 6 个月前
父节点
当前提交
e63072e1c9

+ 46
- 14
app/Filament/Company/Resources/Accounting/BudgetResource/RelationManagers/BudgetItemsRelationManager.php 查看文件

@@ -19,6 +19,7 @@ use Filament\Tables\Grouping\Group;
19 19
 use Filament\Tables\Table;
20 20
 use Illuminate\Database\Eloquent\Builder;
21 21
 use Illuminate\Database\Eloquent\Collection;
22
+use Illuminate\Support\Str;
22 23
 
23 24
 class BudgetItemsRelationManager extends RelationManager
24 25
 {
@@ -26,8 +27,40 @@ class BudgetItemsRelationManager extends RelationManager
26 27
 
27 28
     protected static bool $isLazy = false;
28 29
 
30
+    protected const AMOUNT_PREFIX = 'amount_';
31
+
32
+    protected const TOTAL_COLUMN = 'total';
33
+
29 34
     public array $batchChanges = [];
30 35
 
36
+    /**
37
+     * Generate a consistent key for the budget item and period
38
+     */
39
+    protected static function generatePeriodKey(int $recordId, string $period): string
40
+    {
41
+        return "{$recordId}." . self::AMOUNT_PREFIX . Str::snake($period);
42
+    }
43
+
44
+    /**
45
+     * Generate a consistent key for the budget item's total
46
+     */
47
+    protected static function generateTotalKey(int $recordId): string
48
+    {
49
+        return "{$recordId}." . self::TOTAL_COLUMN;
50
+    }
51
+
52
+    /**
53
+     * Extract the period from a column name
54
+     */
55
+    protected static function extractPeriodFromColumn(string $columnName): ?string
56
+    {
57
+        if (preg_match('/' . self::AMOUNT_PREFIX . '(.+)/', $columnName, $matches)) {
58
+            return str_replace('_', ' ', $matches[1] ?? '');
59
+        }
60
+
61
+        return null;
62
+    }
63
+
31 64
     public function handleBatchColumnChanged($data): void
32 65
     {
33 66
         $key = "{$data['recordKey']}.{$data['name']}";
@@ -39,9 +72,7 @@ class BudgetItemsRelationManager extends RelationManager
39 72
         foreach ($this->batchChanges as $key => $value) {
40 73
             [$recordKey, $column] = explode('.', $key, 2);
41 74
 
42
-            preg_match('/amount_(.+)/', $column, $matches);
43
-            $period = str_replace('_', ' ', $matches[1] ?? '');
44
-
75
+            $period = self::extractPeriodFromColumn($column);
45 76
             if (! $period) {
46 77
                 continue;
47 78
             }
@@ -94,7 +125,7 @@ class BudgetItemsRelationManager extends RelationManager
94 125
 
95 126
         $batchTotal = 0;
96 127
         foreach ($budgetItemIds as $itemId) {
97
-            $key = "{$itemId}.amount_" . str_replace(['-', '.', ' '], '_', $period);
128
+            $key = self::generatePeriodKey($itemId, $period);
98 129
             if (isset($this->batchChanges[$key])) {
99 130
                 $batchValue = CurrencyConverter::convertToCents($this->batchChanges[$key]);
100 131
                 $existingAmount = BudgetAllocation::where('budget_item_id', $itemId)
@@ -124,7 +155,7 @@ class BudgetItemsRelationManager extends RelationManager
124 155
                     ->leftJoin('budget_allocations', 'budget_allocations.budget_item_id', '=', 'budget_items.id');
125 156
 
126 157
                 foreach ($periods as $period) {
127
-                    $alias = 'amount_' . str_replace(['-', '.', ' '], '_', $period);
158
+                    $alias = self::AMOUNT_PREFIX . Str::snake($period);
128 159
                     $query->selectRaw(
129 160
                         "SUM(CASE WHEN budget_allocations.period = ? THEN budget_allocations.amount ELSE 0 END) as {$alias}",
130 161
                         [$period]
@@ -151,13 +182,14 @@ class BudgetItemsRelationManager extends RelationManager
151 182
                     ->label('Account')
152 183
                     ->limit(30)
153 184
                     ->searchable(),
154
-                DeferredTextInputColumn::make('total')
185
+                DeferredTextInputColumn::make(self::TOTAL_COLUMN)
155 186
                     ->label('Total')
156 187
                     ->alignRight()
157 188
                     ->mask(RawJs::make('$money($input)'))
158
-                    ->getStateUsing(function (BudgetItem $record, DeferredTextInputColumn $column) {
159
-                        if (isset($this->batchChanges["{$record->getKey()}.{$column->getName()}"])) {
160
-                            return $this->batchChanges["{$record->getKey()}.{$column->getName()}"];
189
+                    ->getStateUsing(function (BudgetItem $record) {
190
+                        $key = self::generateTotalKey($record->getKey());
191
+                        if (isset($this->batchChanges[$key])) {
192
+                            return $this->batchChanges[$key];
161 193
                         }
162 194
 
163 195
                         $total = $record->allocations->sum(
@@ -190,7 +222,7 @@ class BudgetItemsRelationManager extends RelationManager
190 222
                                     return;
191 223
                                 }
192 224
 
193
-                                $totalKey = "{$record->getKey()}.total";
225
+                                $totalKey = self::generateTotalKey($record->getKey());
194 226
                                 $totalAmount = $this->batchChanges[$totalKey] ?? null;
195 227
 
196 228
                                 if (isset($totalAmount)) {
@@ -209,7 +241,7 @@ class BudgetItemsRelationManager extends RelationManager
209 241
 
210 242
                                 if ($totalCents <= 0) {
211 243
                                     foreach ($periods as $period) {
212
-                                        $periodKey = "{$record->getKey()}.amount_" . str_replace(['-', '.', ' '], '_', $period);
244
+                                        $periodKey = self::generatePeriodKey($record->getKey(), $period);
213 245
                                         $this->batchChanges[$periodKey] = CurrencyConverter::convertCentsToFormatSimple(0);
214 246
                                     }
215 247
 
@@ -223,13 +255,13 @@ class BudgetItemsRelationManager extends RelationManager
223 255
                                     $amount = $baseAmount + ($index === 0 ? $remainder : 0);
224 256
                                     $formattedAmount = CurrencyConverter::convertCentsToFormatSimple($amount);
225 257
 
226
-                                    $periodKey = "{$record->getKey()}." . 'amount_' . str_replace(['-', '.', ' '], '_', $period);
258
+                                    $periodKey = self::generatePeriodKey($record->getKey(), $period);
227 259
                                     $this->batchChanges[$periodKey] = $formattedAmount;
228 260
                                 }
229 261
                             }),
230 262
                     ),
231 263
                 ...array_map(function (string $period) {
232
-                    $alias = 'amount_' . str_replace(['-', '.', ' '], '_', $period); // Also replace space
264
+                    $alias = self::AMOUNT_PREFIX . Str::snake($period);
233 265
 
234 266
                     return DeferredTextInputColumn::make($alias)
235 267
                         ->label($period)
@@ -262,7 +294,7 @@ class BudgetItemsRelationManager extends RelationManager
262 294
                     ->action(function (Collection $records) use ($periods) {
263 295
                         foreach ($records as $record) {
264 296
                             foreach ($periods as $period) {
265
-                                $periodKey = "{$record->getKey()}.amount_" . str_replace(['-', '.', ' '], '_', $period);
297
+                                $periodKey = self::generatePeriodKey($record->getKey(), $period);
266 298
                                 $this->batchChanges[$periodKey] = CurrencyConverter::convertCentsToFormatSimple(0);
267 299
                             }
268 300
                         }

正在加载...
取消
保存