Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TaxResource.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\Category;
  6. use App\Models\Setting\Tax;
  7. use Closure;
  8. use Exception;
  9. use Filament\Forms;
  10. use Filament\Forms\Components\TextInput\Mask;
  11. use Filament\Notifications\Notification;
  12. use Filament\Resources\Form;
  13. use Filament\Resources\Resource;
  14. use Filament\Resources\Table;
  15. use Filament\Tables;
  16. use Illuminate\Support\Collection;
  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. if (str_contains($rate, '.')) {
  103. $rate = rtrim(rtrim($rate, '0'), '.');
  104. }
  105. return $rate . ($record->computation === 'percentage' || $record->computation === 'compound' ? '%' : null);
  106. })
  107. ->searchable()
  108. ->sortable(),
  109. Tables\Columns\BadgeColumn::make('type')
  110. ->label('Type')
  111. ->formatStateUsing(static fn (Tax $record) => ucwords($record->type))
  112. ->colors([
  113. 'success' => 'sales',
  114. 'warning' => 'purchase',
  115. 'secondary' => 'none',
  116. ])
  117. ->icons([
  118. 'heroicon-o-cash' => 'sales',
  119. 'heroicon-o-shopping-bag' => 'purchase',
  120. 'heroicon-o-x-circle' => 'none',
  121. ])
  122. ->searchable()
  123. ->sortable(),
  124. ])
  125. ->filters([
  126. //
  127. ])
  128. ->actions([
  129. Tables\Actions\EditAction::make(),
  130. Tables\Actions\DeleteAction::make()
  131. ->before(static function (Tables\Actions\DeleteAction $action, Tax $record) {
  132. if ($record->enabled) {
  133. Notification::make()
  134. ->danger()
  135. ->title('Action Denied')
  136. ->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)]))
  137. ->persistent()
  138. ->send();
  139. $action->cancel();
  140. }
  141. }),
  142. ])
  143. ->bulkActions([
  144. Tables\Actions\DeleteBulkAction::make()
  145. ->before(static function (Collection $records, Tables\Actions\DeleteBulkAction $action) {
  146. $defaultTaxes = $records->filter(static function (Tax $record) {
  147. return $record->enabled;
  148. });
  149. if ($defaultTaxes->isNotEmpty()) {
  150. $defaultTaxNames = $defaultTaxes->pluck('name')->toArray();
  151. Notification::make()
  152. ->danger()
  153. ->title('Action Denied')
  154. ->body(static function () use ($defaultTaxNames) {
  155. $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>";
  156. $message .= implode("<br>", array_map(static function ($name) {
  157. return "&bull; " . $name;
  158. }, $defaultTaxNames));
  159. return $message;
  160. })
  161. ->persistent()
  162. ->send();
  163. $action->cancel();
  164. }
  165. }),
  166. ]);
  167. }
  168. public static function getRelations(): array
  169. {
  170. return [
  171. //
  172. ];
  173. }
  174. public static function getPages(): array
  175. {
  176. return [
  177. 'index' => Pages\ListTaxes::route('/'),
  178. 'create' => Pages\CreateTax::route('/create'),
  179. 'edit' => Pages\EditTax::route('/{record}/edit'),
  180. ];
  181. }
  182. }