選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AdjustmentResource.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Resources;
  3. use App\Enums\Accounting\AdjustmentCategory;
  4. use App\Enums\Accounting\AdjustmentComputation;
  5. use App\Enums\Accounting\AdjustmentScope;
  6. use App\Enums\Accounting\AdjustmentStatus;
  7. use App\Enums\Accounting\AdjustmentType;
  8. use App\Filament\Company\Clusters\Settings;
  9. use App\Filament\Company\Clusters\Settings\Resources\AdjustmentResource\Pages;
  10. use App\Models\Accounting\Adjustment;
  11. use Filament\Forms;
  12. use Filament\Forms\Form;
  13. use Filament\Resources\Resource;
  14. use Filament\Tables;
  15. use Filament\Tables\Table;
  16. class AdjustmentResource extends Resource
  17. {
  18. protected static ?string $model = Adjustment::class;
  19. protected static ?string $cluster = Settings::class;
  20. public static function form(Form $form): Form
  21. {
  22. return $form
  23. ->schema([
  24. Forms\Components\Section::make('General')
  25. ->schema([
  26. Forms\Components\TextInput::make('name')
  27. ->autofocus()
  28. ->required()
  29. ->maxLength(255),
  30. Forms\Components\Textarea::make('description')
  31. ->label('Description'),
  32. ]),
  33. Forms\Components\Section::make('Configuration')
  34. ->schema([
  35. Forms\Components\Select::make('category')
  36. ->localizeLabel()
  37. ->options(AdjustmentCategory::class)
  38. ->default(AdjustmentCategory::Tax)
  39. ->live()
  40. ->required(),
  41. Forms\Components\Select::make('type')
  42. ->localizeLabel()
  43. ->options(AdjustmentType::class)
  44. ->default(AdjustmentType::Sales)
  45. ->live()
  46. ->required(),
  47. Forms\Components\Checkbox::make('recoverable')
  48. ->label('Recoverable')
  49. ->default(false)
  50. ->helperText('When enabled, tax is tracked separately as claimable from the government. Non-recoverable taxes are treated as part of the expense.')
  51. ->visible(fn (Forms\Get $get) => AdjustmentCategory::parse($get('category'))->isTax() && AdjustmentType::parse($get('type'))->isPurchase()),
  52. ])
  53. ->columns()
  54. ->visibleOn('create'),
  55. Forms\Components\Section::make('Adjustment Details')
  56. ->schema([
  57. Forms\Components\Select::make('computation')
  58. ->localizeLabel()
  59. ->options(AdjustmentComputation::class)
  60. ->default(AdjustmentComputation::Percentage)
  61. ->live()
  62. ->required(),
  63. Forms\Components\TextInput::make('rate')
  64. ->localizeLabel()
  65. ->rate(static fn (Forms\Get $get) => $get('computation'))
  66. ->required(),
  67. Forms\Components\Select::make('scope')
  68. ->localizeLabel()
  69. ->options(AdjustmentScope::class),
  70. ])
  71. ->columns(),
  72. Forms\Components\Section::make('Dates')
  73. ->schema([
  74. Forms\Components\DateTimePicker::make('start_date'),
  75. Forms\Components\DateTimePicker::make('end_date')
  76. ->after('start_date'),
  77. ])
  78. ->columns()
  79. ->visible(fn (Forms\Get $get) => AdjustmentCategory::parse($get('category'))->isDiscount()),
  80. ]);
  81. }
  82. public static function table(Table $table): Table
  83. {
  84. return $table
  85. ->columns([
  86. Tables\Columns\TextColumn::make('name')
  87. ->label('Name')
  88. ->sortable(),
  89. Tables\Columns\TextColumn::make('category')
  90. ->searchable(),
  91. Tables\Columns\TextColumn::make('type')
  92. ->searchable(),
  93. Tables\Columns\TextColumn::make('status')
  94. ->badge(),
  95. Tables\Columns\TextColumn::make('rate')
  96. ->localizeLabel()
  97. ->rate(static fn (Adjustment $record) => $record->computation->value)
  98. ->searchable()
  99. ->sortable(),
  100. ])
  101. ->filters([
  102. //
  103. ])
  104. ->actions([
  105. Tables\Actions\ActionGroup::make([
  106. Tables\Actions\EditAction::make(),
  107. Tables\Actions\Action::make('archive')
  108. ->label('Archive')
  109. ->icon('heroicon-o-archive-box-x-mark')
  110. ->color('danger')
  111. ->requiresConfirmation()
  112. ->visible(static fn (Adjustment $record) => $record->status !== AdjustmentStatus::Archived)
  113. ->action(fn (Adjustment $record) => $record->update(['status' => AdjustmentStatus::Archived])),
  114. ]),
  115. ])
  116. ->bulkActions([
  117. //
  118. ]);
  119. }
  120. public static function getRelations(): array
  121. {
  122. return [
  123. //
  124. ];
  125. }
  126. public static function getPages(): array
  127. {
  128. return [
  129. 'index' => Pages\ListAdjustments::route('/'),
  130. 'create' => Pages\CreateAdjustment::route('/create'),
  131. 'edit' => Pages\EditAdjustment::route('/{record}/edit'),
  132. ];
  133. }
  134. }