| 
				
			 | 
			
			
				
				@@ -6,7 +6,6 @@ use App\Filament\Company\Resources\Accounting\BudgetResource\Pages; 
			 | 
		
		
	
		
			
			| 
				6
			 | 
			
				6
			 | 
			
			
				
				 use App\Filament\Forms\Components\CustomSection; 
			 | 
		
		
	
		
			
			| 
				7
			 | 
			
				7
			 | 
			
			
				
				 use App\Models\Accounting\Account; 
			 | 
		
		
	
		
			
			| 
				8
			 | 
			
				8
			 | 
			
			
				
				 use App\Models\Accounting\Budget; 
			 | 
		
		
	
		
			
			| 
				9
			 | 
			
				
			 | 
			
			
				
				-use App\Models\Accounting\BudgetItem; 
			 | 
		
		
	
		
			
			| 
				10
			 | 
			
				9
			 | 
			
			
				
				 use Filament\Forms; 
			 | 
		
		
	
		
			
			| 
				11
			 | 
			
				10
			 | 
			
			
				
				 use Filament\Forms\Form; 
			 | 
		
		
	
		
			
			| 
				12
			 | 
			
				11
			 | 
			
			
				
				 use Filament\Resources\Resource; 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -56,7 +55,6 @@ class BudgetResource extends Resource 
			 | 
		
		
	
		
			
			| 
				56
			 | 
			
				55
			 | 
			
			
				
				                 Forms\Components\Section::make('Budget Items') 
			 | 
		
		
	
		
			
			| 
				57
			 | 
			
				56
			 | 
			
			
				
				                     ->schema([ 
			 | 
		
		
	
		
			
			| 
				58
			 | 
			
				57
			 | 
			
			
				
				                         Forms\Components\Repeater::make('budgetItems') 
			 | 
		
		
	
		
			
			| 
				59
			 | 
			
				
			 | 
			
			
				
				-                            ->relationship() 
			 | 
		
		
	
		
			
			| 
				60
			 | 
			
				58
			 | 
			
			
				
				                             ->columns(4) 
			 | 
		
		
	
		
			
			| 
				61
			 | 
			
				59
			 | 
			
			
				
				                             ->hiddenLabel() 
			 | 
		
		
	
		
			
			| 
				62
			 | 
			
				60
			 | 
			
			
				
				                             ->schema([ 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -95,14 +93,41 @@ class BudgetResource extends Resource 
			 | 
		
		
	
		
			
			| 
				95
			 | 
			
				93
			 | 
			
			
				
				     { 
			 | 
		
		
	
		
			
			| 
				96
			 | 
			
				94
			 | 
			
			
				
				         return $table 
			 | 
		
		
	
		
			
			| 
				97
			 | 
			
				95
			 | 
			
			
				
				             ->columns([ 
			 | 
		
		
	
		
			
			| 
				98
			 | 
			
				
			 | 
			
			
				
				-                // 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				96
			 | 
			
			
				
				+                Tables\Columns\TextColumn::make('name') 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				97
			 | 
			
			
				
				+                    ->sortable() 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				98
			 | 
			
			
				
				+                    ->searchable(), 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				99
			 | 
			
			
				
				+ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				100
			 | 
			
			
				
				+                Tables\Columns\TextColumn::make('interval_type') 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				101
			 | 
			
			
				
				+                    ->label('Interval') 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				102
			 | 
			
			
				
				+                    ->sortable() 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				103
			 | 
			
			
				
				+                    ->badge() 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				104
			 | 
			
			
				
				+                    ->formatStateUsing(fn (string $state) => ucfirst($state)), 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				105
			 | 
			
			
				
				+ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				106
			 | 
			
			
				
				+                Tables\Columns\TextColumn::make('start_date') 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				107
			 | 
			
			
				
				+                    ->label('Start Date') 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				108
			 | 
			
			
				
				+                    ->date() 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				109
			 | 
			
			
				
				+                    ->sortable(), 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				110
			 | 
			
			
				
				+ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				111
			 | 
			
			
				
				+                Tables\Columns\TextColumn::make('end_date') 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				112
			 | 
			
			
				
				+                    ->label('End Date') 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				113
			 | 
			
			
				
				+                    ->date() 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				114
			 | 
			
			
				
				+                    ->sortable(), 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				115
			 | 
			
			
				
				+ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				116
			 | 
			
			
				
				+                Tables\Columns\TextColumn::make('total_budgeted_amount') 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				117
			 | 
			
			
				
				+                    ->label('Total Budgeted') 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				118
			 | 
			
			
				
				+                    ->money() 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				119
			 | 
			
			
				
				+                    ->sortable() 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				120
			 | 
			
			
				
				+                    ->alignEnd() 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				121
			 | 
			
			
				
				+                    ->formatStateUsing(fn (Budget $record) => $record->budgetItems->sum(fn ($item) => $item->allocations->sum('amount'))), 
			 | 
		
		
	
		
			
			| 
				99
			 | 
			
				122
			 | 
			
			
				
				             ]) 
			 | 
		
		
	
		
			
			| 
				100
			 | 
			
				123
			 | 
			
			
				
				             ->filters([ 
			 | 
		
		
	
		
			
			| 
				101
			 | 
			
				124
			 | 
			
			
				
				                 // 
			 | 
		
		
	
		
			
			| 
				102
			 | 
			
				125
			 | 
			
			
				
				             ]) 
			 | 
		
		
	
		
			
			| 
				103
			 | 
			
				126
			 | 
			
			
				
				             ->actions([ 
			 | 
		
		
	
		
			
			| 
				104
			 | 
			
				
			 | 
			
			
				
				-                Tables\Actions\ViewAction::make(), 
			 | 
		
		
	
		
			
			| 
				105
			 | 
			
				
			 | 
			
			
				
				-                Tables\Actions\EditAction::make(), 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				127
			 | 
			
			
				
				+                Tables\Actions\ActionGroup::make([ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				128
			 | 
			
			
				
				+                    Tables\Actions\ViewAction::make(), 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				129
			 | 
			
			
				
				+                    Tables\Actions\EditAction::make(), 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				130
			 | 
			
			
				
				+                ]), 
			 | 
		
		
	
		
			
			| 
				106
			 | 
			
				131
			 | 
			
			
				
				             ]) 
			 | 
		
		
	
		
			
			| 
				107
			 | 
			
				132
			 | 
			
			
				
				             ->bulkActions([ 
			 | 
		
		
	
		
			
			| 
				108
			 | 
			
				133
			 | 
			
			
				
				                 Tables\Actions\BulkActionGroup::make([ 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -206,53 +231,6 @@ class BudgetResource extends Resource 
			 | 
		
		
	
		
			
			| 
				206
			 | 
			
				231
			 | 
			
			
				
				         return $fields; 
			 | 
		
		
	
		
			
			| 
				207
			 | 
			
				232
			 | 
			
			
				
				     } 
			 | 
		
		
	
		
			
			| 
				208
			 | 
			
				233
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				209
			 | 
			
				
			 | 
			
			
				
				-    /** 
			 | 
		
		
	
		
			
			| 
				210
			 | 
			
				
			 | 
			
			
				
				-     * Generates an array of interval labels (e.g., Jan 2024, Q1 2024, etc.). 
			 | 
		
		
	
		
			
			| 
				211
			 | 
			
				
			 | 
			
			
				
				-     */ 
			 | 
		
		
	
		
			
			| 
				212
			 | 
			
				
			 | 
			
			
				
				-    private static function generateIntervals(string $startDate, string $endDate, string $intervalType): array 
			 | 
		
		
	
		
			
			| 
				213
			 | 
			
				
			 | 
			
			
				
				-    { 
			 | 
		
		
	
		
			
			| 
				214
			 | 
			
				
			 | 
			
			
				
				-        $start = Carbon::parse($startDate); 
			 | 
		
		
	
		
			
			| 
				215
			 | 
			
				
			 | 
			
			
				
				-        $end = Carbon::parse($endDate); 
			 | 
		
		
	
		
			
			| 
				216
			 | 
			
				
			 | 
			
			
				
				-        $intervals = []; 
			 | 
		
		
	
		
			
			| 
				217
			 | 
			
				
			 | 
			
			
				
				- 
			 | 
		
		
	
		
			
			| 
				218
			 | 
			
				
			 | 
			
			
				
				-        while ($start->lte($end)) { 
			 | 
		
		
	
		
			
			| 
				219
			 | 
			
				
			 | 
			
			
				
				-            if ($intervalType === 'month') { 
			 | 
		
		
	
		
			
			| 
				220
			 | 
			
				
			 | 
			
			
				
				-                $intervals[] = $start->format('M Y'); // Example: Jan 2024 
			 | 
		
		
	
		
			
			| 
				221
			 | 
			
				
			 | 
			
			
				
				-                $start->addMonth(); 
			 | 
		
		
	
		
			
			| 
				222
			 | 
			
				
			 | 
			
			
				
				-            } elseif ($intervalType === 'quarter') { 
			 | 
		
		
	
		
			
			| 
				223
			 | 
			
				
			 | 
			
			
				
				-                $intervals[] = 'Q' . $start->quarter . ' ' . $start->year; // Example: Q1 2024 
			 | 
		
		
	
		
			
			| 
				224
			 | 
			
				
			 | 
			
			
				
				-                $start->addQuarter(); 
			 | 
		
		
	
		
			
			| 
				225
			 | 
			
				
			 | 
			
			
				
				-            } elseif ($intervalType === 'year') { 
			 | 
		
		
	
		
			
			| 
				226
			 | 
			
				
			 | 
			
			
				
				-                $intervals[] = $start->year; // Example: 2024 
			 | 
		
		
	
		
			
			| 
				227
			 | 
			
				
			 | 
			
			
				
				-                $start->addYear(); 
			 | 
		
		
	
		
			
			| 
				228
			 | 
			
				
			 | 
			
			
				
				-            } 
			 | 
		
		
	
		
			
			| 
				229
			 | 
			
				
			 | 
			
			
				
				-        } 
			 | 
		
		
	
		
			
			| 
				230
			 | 
			
				
			 | 
			
			
				
				- 
			 | 
		
		
	
		
			
			| 
				231
			 | 
			
				
			 | 
			
			
				
				-        return $intervals; 
			 | 
		
		
	
		
			
			| 
				232
			 | 
			
				
			 | 
			
			
				
				-    } 
			 | 
		
		
	
		
			
			| 
				233
			 | 
			
				
			 | 
			
			
				
				- 
			 | 
		
		
	
		
			
			| 
				234
			 | 
			
				
			 | 
			
			
				
				-    /** 
			 | 
		
		
	
		
			
			| 
				235
			 | 
			
				
			 | 
			
			
				
				-     * Saves budget allocations correctly in `budget_allocations` table. 
			 | 
		
		
	
		
			
			| 
				236
			 | 
			
				
			 | 
			
			
				
				-     */ 
			 | 
		
		
	
		
			
			| 
				237
			 | 
			
				
			 | 
			
			
				
				-    public static function saveBudgetAllocations(BudgetItem $record, array $data): void 
			 | 
		
		
	
		
			
			| 
				238
			 | 
			
				
			 | 
			
			
				
				-    { 
			 | 
		
		
	
		
			
			| 
				239
			 | 
			
				
			 | 
			
			
				
				-        $record->update($data); 
			 | 
		
		
	
		
			
			| 
				240
			 | 
			
				
			 | 
			
			
				
				- 
			 | 
		
		
	
		
			
			| 
				241
			 | 
			
				
			 | 
			
			
				
				-        $intervals = self::generateIntervals($data['start_date'], $data['end_date'], $data['interval_type']); 
			 | 
		
		
	
		
			
			| 
				242
			 | 
			
				
			 | 
			
			
				
				- 
			 | 
		
		
	
		
			
			| 
				243
			 | 
			
				
			 | 
			
			
				
				-        foreach ($intervals as $interval) { 
			 | 
		
		
	
		
			
			| 
				244
			 | 
			
				
			 | 
			
			
				
				-            $record->allocations()->updateOrCreate( 
			 | 
		
		
	
		
			
			| 
				245
			 | 
			
				
			 | 
			
			
				
				-                ['period' => $interval], 
			 | 
		
		
	
		
			
			| 
				246
			 | 
			
				
			 | 
			
			
				
				-                [ 
			 | 
		
		
	
		
			
			| 
				247
			 | 
			
				
			 | 
			
			
				
				-                    'interval_type' => $data['interval_type'], 
			 | 
		
		
	
		
			
			| 
				248
			 | 
			
				
			 | 
			
			
				
				-                    'start_date' => Carbon::parse($interval)->startOfMonth(), 
			 | 
		
		
	
		
			
			| 
				249
			 | 
			
				
			 | 
			
			
				
				-                    'end_date' => Carbon::parse($interval)->endOfMonth(), 
			 | 
		
		
	
		
			
			| 
				250
			 | 
			
				
			 | 
			
			
				
				-                    'amount' => $data['allocations'][$interval] ?? 0, 
			 | 
		
		
	
		
			
			| 
				251
			 | 
			
				
			 | 
			
			
				
				-                ] 
			 | 
		
		
	
		
			
			| 
				252
			 | 
			
				
			 | 
			
			
				
				-            ); 
			 | 
		
		
	
		
			
			| 
				253
			 | 
			
				
			 | 
			
			
				
				-        } 
			 | 
		
		
	
		
			
			| 
				254
			 | 
			
				
			 | 
			
			
				
				-    } 
			 | 
		
		
	
		
			
			| 
				255
			 | 
			
				
			 | 
			
			
				
				- 
			 | 
		
		
	
		
			
			| 
				256
			 | 
			
				234
			 | 
			
			
				
				     public static function getRelations(): array 
			 | 
		
		
	
		
			
			| 
				257
			 | 
			
				235
			 | 
			
			
				
				     { 
			 | 
		
		
	
		
			
			| 
				258
			 | 
			
				236
			 | 
			
			
				
				         return [ 
			 |