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.

BudgetResource.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Filament\Company\Resources\Accounting;
  3. use App\Filament\Company\Resources\Accounting\BudgetResource\Pages;
  4. use App\Models\Accounting\Account;
  5. use App\Models\Accounting\Budget;
  6. use Awcodes\TableRepeater\Components\TableRepeater;
  7. use Awcodes\TableRepeater\Header;
  8. use Filament\Forms;
  9. use Filament\Forms\Form;
  10. use Filament\Resources\Resource;
  11. use Filament\Tables;
  12. use Filament\Tables\Table;
  13. class BudgetResource extends Resource
  14. {
  15. protected static ?string $model = Budget::class;
  16. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  17. public static function form(Form $form): Form
  18. {
  19. return $form
  20. ->schema([
  21. Forms\Components\Section::make('Budget Details')
  22. ->schema([
  23. Forms\Components\TextInput::make('name')
  24. ->required()
  25. ->maxLength(255),
  26. Forms\Components\Grid::make(2)->schema([
  27. Forms\Components\DatePicker::make('start_date')->required(),
  28. Forms\Components\DatePicker::make('end_date')->required(),
  29. ]),
  30. Forms\Components\Select::make('interval_type')
  31. ->label('Budget Interval')
  32. ->options([
  33. 'day' => 'Daily',
  34. 'week' => 'Weekly',
  35. 'month' => 'Monthly',
  36. 'quarter' => 'Quarterly',
  37. 'year' => 'Yearly',
  38. ])
  39. ->default('month')
  40. ->required()
  41. ->live(),
  42. Forms\Components\Textarea::make('notes')->columnSpanFull(),
  43. ]),
  44. Forms\Components\Section::make('Budget Items')
  45. ->schema([
  46. TableRepeater::make('budgetItems')
  47. ->relationship()
  48. ->saveRelationshipsUsing(null)
  49. ->dehydrated(true)
  50. ->headers(fn (Forms\Get $get) => self::getHeaders($get('interval_type')))
  51. ->schema([
  52. Forms\Components\Select::make('account_id')
  53. ->label('Account')
  54. ->options(Account::query()->pluck('name', 'id'))
  55. ->searchable()
  56. ->required(),
  57. Forms\Components\Grid::make(2)->schema([
  58. Forms\Components\DatePicker::make('start_date')->required(),
  59. Forms\Components\DatePicker::make('end_date')->required(),
  60. ]),
  61. Forms\Components\TextInput::make('amount')
  62. ->numeric()
  63. ->suffix('USD')
  64. ->required(),
  65. ])
  66. ->defaultItems(1)
  67. ->addActionLabel('Add Budget Item'),
  68. ]),
  69. ]);
  70. }
  71. public static function table(Table $table): Table
  72. {
  73. return $table
  74. ->columns([
  75. //
  76. ])
  77. ->filters([
  78. //
  79. ])
  80. ->actions([
  81. Tables\Actions\ViewAction::make(),
  82. Tables\Actions\EditAction::make(),
  83. ])
  84. ->bulkActions([
  85. Tables\Actions\BulkActionGroup::make([
  86. Tables\Actions\DeleteBulkAction::make(),
  87. ]),
  88. ]);
  89. }
  90. private static function getHeaders(?string $intervalType): array
  91. {
  92. $headers = [
  93. Header::make('Account')->width('20%'),
  94. Header::make('Start Date')->width('15%'),
  95. Header::make('End Date')->width('15%'),
  96. ];
  97. // Adjust the number of columns dynamically based on interval type
  98. switch ($intervalType) {
  99. case 'day':
  100. $headers[] = Header::make('Daily Budget')->width('20%')->align('right');
  101. break;
  102. case 'week':
  103. $headers[] = Header::make('Weekly Budget')->width('20%')->align('right');
  104. break;
  105. case 'month':
  106. $headers[] = Header::make('Monthly Budget')->width('20%')->align('right');
  107. break;
  108. case 'quarter':
  109. $headers[] = Header::make('Quarterly Budget')->width('20%')->align('right');
  110. break;
  111. case 'year':
  112. $headers[] = Header::make('Yearly Budget')->width('20%')->align('right');
  113. break;
  114. }
  115. return $headers;
  116. }
  117. public static function getRelations(): array
  118. {
  119. return [
  120. //
  121. ];
  122. }
  123. public static function getPages(): array
  124. {
  125. return [
  126. 'index' => Pages\ListBudgets::route('/'),
  127. 'create' => Pages\CreateBudget::route('/create'),
  128. 'view' => Pages\ViewBudget::route('/{record}'),
  129. 'edit' => Pages\EditBudget::route('/{record}/edit'),
  130. ];
  131. }
  132. }