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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 getEloquentQuery(): Builder
  24. {
  25. return parent::getEloquentQuery()
  26. ->where('company_id', Auth::user()->currentCompany->id);
  27. }
  28. public static function form(Form $form): Form
  29. {
  30. return $form
  31. ->schema([
  32. Forms\Components\Section::make('General')
  33. ->schema([
  34. Forms\Components\TextInput::make('name')
  35. ->label('Name')
  36. ->required(),
  37. Forms\Components\TextInput::make('description')
  38. ->label('Description'),
  39. Forms\Components\Select::make('computation')
  40. ->label('Computation')
  41. ->options(Tax::getComputationTypes())
  42. ->reactive()
  43. ->searchable()
  44. ->default('percentage')
  45. ->required(),
  46. Forms\Components\TextInput::make('rate')
  47. ->label('Rate')
  48. ->mask(static fn (Mask $mask) => $mask
  49. ->numeric()
  50. ->decimalPlaces(4)
  51. ->decimalSeparator('.')
  52. ->thousandsSeparator(',')
  53. ->minValue(0)
  54. ->normalizeZeros()
  55. ->padFractionalZeros()
  56. )
  57. ->suffix(static function (callable $get) {
  58. $computation = $get('computation');
  59. if ($computation === 'percentage' || $computation === 'compound') {
  60. return '%';
  61. }
  62. return null;
  63. })
  64. ->default(0.0000)
  65. ->required(),
  66. Forms\Components\Select::make('type')
  67. ->label('Type')
  68. ->options(Tax::getTaxTypes())
  69. ->searchable()
  70. ->default('sales')
  71. ->required(),
  72. Forms\Components\Select::make('scope')
  73. ->label('Scope')
  74. ->options(Tax::getTaxScopes())
  75. ->searchable(),
  76. ToggleButton::make('enabled')
  77. ->label('Default')
  78. ->offColor('danger')
  79. ->onColor('primary'),
  80. ])->columns(),
  81. ]);
  82. }
  83. /**
  84. * @throws Exception
  85. */
  86. public static function table(Table $table): Table
  87. {
  88. return $table
  89. ->columns([
  90. Tables\Columns\TextColumn::make('name')
  91. ->label('Name')
  92. ->weight('semibold')
  93. ->icon(static fn (Tax $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
  94. ->tooltip(static fn (Tax $record) => $record->enabled ? "Default " .ucwords($record->type) . " Tax" : null)
  95. ->iconPosition('after')
  96. ->searchable()
  97. ->sortable(),
  98. Tables\Columns\TextColumn::make('computation')
  99. ->label('Computation')
  100. ->formatStateUsing(static fn (Tax $record) => ucwords($record->computation))
  101. ->searchable()
  102. ->sortable(),
  103. Tables\Columns\TextColumn::make('rate')
  104. ->label('Rate')
  105. ->formatStateUsing(static function (Tax $record) {
  106. $rate = $record->rate;
  107. return $rate . ($record->computation === 'percentage' || $record->computation === 'compound' ? '%' : null);
  108. })
  109. ->searchable()
  110. ->sortable(),
  111. Tables\Columns\BadgeColumn::make('type')
  112. ->label('Type')
  113. ->formatStateUsing(static fn (Tax $record) => ucwords($record->type))
  114. ->colors([
  115. 'success' => 'sales',
  116. 'warning' => 'purchase',
  117. 'secondary' => 'none',
  118. ])
  119. ->icons([
  120. 'heroicon-o-cash' => 'sales',
  121. 'heroicon-o-shopping-bag' => 'purchase',
  122. 'heroicon-o-x-circle' => 'none',
  123. ])
  124. ->searchable()
  125. ->sortable(),
  126. ])
  127. ->filters([
  128. //
  129. ])
  130. ->actions([
  131. Tables\Actions\EditAction::make(),
  132. Tables\Actions\DeleteAction::make()
  133. ->before(static function (Tables\Actions\DeleteAction $action, Tax $record) {
  134. if ($record->enabled) {
  135. Notification::make()
  136. ->danger()
  137. ->title('Action Denied')
  138. ->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)]))
  139. ->persistent()
  140. ->send();
  141. $action->cancel();
  142. }
  143. }),
  144. ])
  145. ->bulkActions([
  146. Tables\Actions\DeleteBulkAction::make()
  147. ->before(static function (Collection $records, Tables\Actions\DeleteBulkAction $action) {
  148. $defaultTaxes = $records->filter(static function (Tax $record) {
  149. return $record->enabled;
  150. });
  151. if ($defaultTaxes->isNotEmpty()) {
  152. $defaultTaxNames = $defaultTaxes->pluck('name')->toArray();
  153. Notification::make()
  154. ->danger()
  155. ->title('Action Denied')
  156. ->body(static function () use ($defaultTaxNames) {
  157. $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>";
  158. $message .= implode("<br>", array_map(static function ($name) {
  159. return "&bull; " . $name;
  160. }, $defaultTaxNames));
  161. return $message;
  162. })
  163. ->persistent()
  164. ->send();
  165. $action->cancel();
  166. }
  167. }),
  168. ]);
  169. }
  170. public static function getRelations(): array
  171. {
  172. return [
  173. //
  174. ];
  175. }
  176. public static function getPages(): array
  177. {
  178. return [
  179. 'index' => Pages\ListTaxes::route('/'),
  180. 'create' => Pages\CreateTax::route('/create'),
  181. 'edit' => Pages\EditTax::route('/{record}/edit'),
  182. ];
  183. }
  184. }