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.

TaxResource.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\TaxResource\Pages;
  4. use App\Filament\Resources\TaxResource\RelationManagers;
  5. use App\Models\Setting\Tax;
  6. use Exception;
  7. use Filament\Forms;
  8. use Filament\Forms\Components\TextInput\Mask;
  9. use Filament\Notifications\Notification;
  10. use Filament\Resources\Form;
  11. use Filament\Resources\Resource;
  12. use Filament\Resources\Table;
  13. use Filament\Tables;
  14. use Illuminate\Database\Eloquent\Builder;
  15. use Illuminate\Support\Collection;
  16. use Illuminate\Support\Facades\Auth;
  17. use Wallo\FilamentSelectify\Components\ToggleButton;
  18. class TaxResource extends Resource
  19. {
  20. protected static ?string $model = Tax::class;
  21. protected static ?string $navigationIcon = 'heroicon-o-receipt-tax';
  22. protected static ?string $navigationGroup = 'Settings';
  23. public static function form(Form $form): Form
  24. {
  25. return $form
  26. ->schema([
  27. Forms\Components\Section::make('General')
  28. ->schema([
  29. Forms\Components\TextInput::make('name')
  30. ->label('Name')
  31. ->required(),
  32. Forms\Components\TextInput::make('description')
  33. ->label('Description'),
  34. Forms\Components\Select::make('computation')
  35. ->label('Computation')
  36. ->options(Tax::getComputationTypes())
  37. ->reactive()
  38. ->searchable()
  39. ->default('percentage')
  40. ->required(),
  41. Forms\Components\TextInput::make('rate')
  42. ->label('Rate')
  43. ->mask(static fn (Mask $mask) => $mask
  44. ->numeric()
  45. ->decimalPlaces(4)
  46. ->decimalSeparator('.')
  47. ->thousandsSeparator(',')
  48. ->minValue(0)
  49. ->normalizeZeros()
  50. ->padFractionalZeros()
  51. )
  52. ->suffix(static function (callable $get) {
  53. $computation = $get('computation');
  54. if ($computation === 'percentage' || $computation === 'compound') {
  55. return '%';
  56. }
  57. return null;
  58. })
  59. ->default(0.0000)
  60. ->required(),
  61. Forms\Components\Select::make('type')
  62. ->label('Type')
  63. ->options(Tax::getTaxTypes())
  64. ->searchable()
  65. ->default('sales')
  66. ->required(),
  67. Forms\Components\Select::make('scope')
  68. ->label('Scope')
  69. ->options(Tax::getTaxScopes())
  70. ->searchable(),
  71. ToggleButton::make('enabled')
  72. ->label('Default')
  73. ->offColor('danger')
  74. ->onColor('primary'),
  75. ])->columns(),
  76. ]);
  77. }
  78. /**
  79. * @throws Exception
  80. */
  81. public static function table(Table $table): Table
  82. {
  83. return $table
  84. ->columns([
  85. Tables\Columns\TextColumn::make('name')
  86. ->label('Name')
  87. ->weight('semibold')
  88. ->icon(static fn (Tax $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
  89. ->tooltip(static fn (Tax $record) => $record->enabled ? "Default " .ucwords($record->type) . " Tax" : null)
  90. ->iconPosition('after')
  91. ->searchable()
  92. ->sortable(),
  93. Tables\Columns\TextColumn::make('computation')
  94. ->label('Computation')
  95. ->formatStateUsing(static fn (Tax $record) => ucwords($record->computation))
  96. ->searchable()
  97. ->sortable(),
  98. Tables\Columns\TextColumn::make('rate')
  99. ->label('Rate')
  100. ->formatStateUsing(static function (Tax $record) {
  101. $rate = $record->rate;
  102. return $rate . ($record->computation === 'percentage' || $record->computation === 'compound' ? '%' : null);
  103. })
  104. ->searchable()
  105. ->sortable(),
  106. Tables\Columns\BadgeColumn::make('type')
  107. ->label('Type')
  108. ->formatStateUsing(static fn (Tax $record) => ucwords($record->type))
  109. ->colors([
  110. 'success' => 'sales',
  111. 'warning' => 'purchase',
  112. 'secondary' => 'none',
  113. ])
  114. ->icons([
  115. 'heroicon-o-cash' => 'sales',
  116. 'heroicon-o-shopping-bag' => 'purchase',
  117. 'heroicon-o-x-circle' => 'none',
  118. ])
  119. ->searchable()
  120. ->sortable(),
  121. ])
  122. ->filters([
  123. //
  124. ])
  125. ->actions([
  126. Tables\Actions\EditAction::make(),
  127. Tables\Actions\DeleteAction::make()
  128. ->before(static function (Tables\Actions\DeleteAction $action, Tax $record) {
  129. if ($record->enabled) {
  130. Notification::make()
  131. ->danger()
  132. ->title('Action Denied')
  133. ->body(__('The :name tax is currently set as your default :Type tax and cannot be deleted. Please set a different tax as your default before attempting to delete this one.', ['name' => $record->name, 'Type' => ucwords($record->type)]))
  134. ->persistent()
  135. ->send();
  136. $action->cancel();
  137. }
  138. }),
  139. ])
  140. ->bulkActions([
  141. Tables\Actions\DeleteBulkAction::make()
  142. ->before(static function (Collection $records, Tables\Actions\DeleteBulkAction $action) {
  143. $defaultTaxes = $records->filter(static function (Tax $record) {
  144. return $record->enabled;
  145. });
  146. if ($defaultTaxes->isNotEmpty()) {
  147. $defaultTaxNames = $defaultTaxes->pluck('name')->toArray();
  148. Notification::make()
  149. ->danger()
  150. ->title('Action Denied')
  151. ->body(static function () use ($defaultTaxNames) {
  152. $message = __('The following taxes are currently set as your default and cannot be deleted. Please set a different tax as your default before attempting to delete these ones.') . "<br><br>";
  153. $message .= implode("<br>", array_map(static function ($name) {
  154. return "&bull; " . $name;
  155. }, $defaultTaxNames));
  156. return $message;
  157. })
  158. ->persistent()
  159. ->send();
  160. $action->cancel();
  161. }
  162. }),
  163. ]);
  164. }
  165. public static function getRelations(): array
  166. {
  167. return [
  168. //
  169. ];
  170. }
  171. public static function getPages(): array
  172. {
  173. return [
  174. 'index' => Pages\ListTaxes::route('/'),
  175. 'create' => Pages\CreateTax::route('/create'),
  176. 'edit' => Pages\EditTax::route('/{record}/edit'),
  177. ];
  178. }
  179. }