Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DiscountResource.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Resources;
  3. use App\Concerns\NotifiesOnDelete;
  4. use App\Enums\Setting\DateFormat;
  5. use App\Enums\Setting\DiscountComputation;
  6. use App\Enums\Setting\DiscountScope;
  7. use App\Enums\Setting\DiscountType;
  8. use App\Enums\Setting\TimeFormat;
  9. use App\Filament\Company\Clusters\Settings;
  10. use App\Filament\Company\Clusters\Settings\Resources\DiscountResource\Pages;
  11. use App\Models\Setting\Discount;
  12. use App\Models\Setting\Localization;
  13. use Closure;
  14. use Filament\Forms;
  15. use Filament\Forms\Form;
  16. use Filament\Resources\Resource;
  17. use Filament\Support\Enums\FontWeight;
  18. use Filament\Tables;
  19. use Filament\Tables\Table;
  20. use Wallo\FilamentSelectify\Components\ToggleButton;
  21. class DiscountResource extends Resource
  22. {
  23. use NotifiesOnDelete;
  24. protected static ?string $model = Discount::class;
  25. protected static ?string $modelLabel = 'Discount';
  26. protected static ?string $cluster = Settings::class;
  27. public static function getModelLabel(): string
  28. {
  29. $modelLabel = static::$modelLabel;
  30. return translate($modelLabel);
  31. }
  32. public static function form(Form $form): Form
  33. {
  34. return $form
  35. ->schema([
  36. Forms\Components\Section::make('General')
  37. ->schema([
  38. Forms\Components\TextInput::make('name')
  39. ->autofocus()
  40. ->required()
  41. ->localizeLabel()
  42. ->maxLength(255)
  43. ->rule(static function (Forms\Get $get, Forms\Components\Component $component): Closure {
  44. return static function (string $attribute, $value, Closure $fail) use ($component, $get) {
  45. $existingDiscount = Discount::where('name', $value)
  46. ->whereKeyNot($component->getRecord()?->getKey())
  47. ->where('type', $get('type'))
  48. ->first();
  49. if ($existingDiscount) {
  50. $message = translate('The :Type :record ":name" already exists.', [
  51. 'Type' => $existingDiscount->type->getLabel(),
  52. 'record' => strtolower(static::getModelLabel()),
  53. 'name' => $value,
  54. ]);
  55. $fail($message);
  56. }
  57. };
  58. }),
  59. Forms\Components\TextInput::make('description')
  60. ->localizeLabel(),
  61. Forms\Components\Select::make('computation')
  62. ->localizeLabel()
  63. ->options(DiscountComputation::class)
  64. ->default(DiscountComputation::Percentage)
  65. ->live()
  66. ->required(),
  67. Forms\Components\TextInput::make('rate')
  68. ->localizeLabel()
  69. ->rate(static fn (Forms\Get $get) => $get('computation'))
  70. ->required(),
  71. Forms\Components\Select::make('type')
  72. ->localizeLabel()
  73. ->options(DiscountType::class)
  74. ->default(DiscountType::Sales)
  75. ->required(),
  76. Forms\Components\Select::make('scope')
  77. ->localizeLabel()
  78. ->options(DiscountScope::class)
  79. ->nullable(),
  80. Forms\Components\DateTimePicker::make('start_date')
  81. ->localizeLabel()
  82. ->beforeOrEqual('end_date')
  83. ->seconds(false)
  84. ->disabled(static fn (string $operation, ?Discount $record = null) => $operation === 'edit' && $record?->start_date?->isPast() ?? false)
  85. ->helperText(static fn (Forms\Components\DateTimePicker $component) => $component->isDisabled() ? 'Start date cannot be changed after the discount has begun.' : null),
  86. Forms\Components\DateTimePicker::make('end_date')
  87. ->localizeLabel()
  88. ->afterOrEqual('start_date')
  89. ->seconds(false),
  90. ToggleButton::make('enabled')
  91. ->localizeLabel('Default')
  92. ->onLabel(Discount::enabledLabel())
  93. ->offLabel(Discount::disabledLabel()),
  94. ])->columns(),
  95. ]);
  96. }
  97. public static function table(Table $table): Table
  98. {
  99. return $table
  100. ->columns([
  101. Tables\Columns\TextColumn::make('name')
  102. ->localizeLabel()
  103. ->weight(FontWeight::Medium)
  104. ->icon(static fn (Discount $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  105. ->tooltip(static function (Discount $record) {
  106. if ($record->isDisabled()) {
  107. return null;
  108. }
  109. return translate('Default :Type :Record', [
  110. 'Type' => $record->type->getLabel(),
  111. 'Record' => static::getModelLabel(),
  112. ]);
  113. })
  114. ->iconPosition('after')
  115. ->searchable()
  116. ->sortable(),
  117. Tables\Columns\TextColumn::make('rate')
  118. ->localizeLabel()
  119. ->rate(static fn (Discount $record) => $record->computation->value)
  120. ->searchable()
  121. ->sortable(),
  122. Tables\Columns\TextColumn::make('type')
  123. ->localizeLabel()
  124. ->badge()
  125. ->searchable()
  126. ->sortable(),
  127. Tables\Columns\TextColumn::make('start_date')
  128. ->localizeLabel()
  129. ->formatStateUsing(static function (Discount $record) {
  130. $dateFormat = Localization::firstOrFail()->date_format->value ?? DateFormat::DEFAULT;
  131. $timeFormat = Localization::firstOrFail()->time_format->value ?? TimeFormat::DEFAULT;
  132. return $record->start_date ? $record->start_date->format("{$dateFormat} {$timeFormat}") : 'N/A';
  133. })
  134. ->searchable()
  135. ->sortable(),
  136. Tables\Columns\TextColumn::make('end_date')
  137. ->localizeLabel()
  138. ->formatStateUsing(static function (Discount $record) {
  139. $dateFormat = Localization::firstOrFail()->date_format->value ?? DateFormat::DEFAULT;
  140. $timeFormat = Localization::firstOrFail()->time_format->value ?? TimeFormat::DEFAULT;
  141. return $record->end_date ? $record->end_date->format("{$dateFormat} {$timeFormat}") : 'N/A';
  142. })
  143. ->color(static fn (Discount $record) => $record->end_date?->isPast() ? 'danger' : null)
  144. ->searchable()
  145. ->sortable(),
  146. ])
  147. ->filters([
  148. //
  149. ])
  150. ->actions([
  151. Tables\Actions\EditAction::make(),
  152. ])
  153. ->bulkActions([
  154. Tables\Actions\BulkActionGroup::make([
  155. Tables\Actions\DeleteBulkAction::make(),
  156. ]),
  157. ])
  158. ->checkIfRecordIsSelectableUsing(static function (Discount $record) {
  159. return $record->isDisabled();
  160. })
  161. ->emptyStateActions([
  162. Tables\Actions\CreateAction::make(),
  163. ]);
  164. }
  165. public static function getPages(): array
  166. {
  167. return [
  168. 'index' => Pages\ListDiscounts::route('/'),
  169. 'create' => Pages\CreateDiscount::route('/create'),
  170. 'edit' => Pages\EditDiscount::route('/{record}/edit'),
  171. ];
  172. }
  173. }