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

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