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.

DiscountResource.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 ($get, $component) {
  45. $existingDiscount = Discount::where('company_id', auth()->user()->currentCompany->id)
  46. ->where('name', $value)
  47. ->where('type', $get('type'))
  48. ->first();
  49. if ($existingDiscount && $existingDiscount->getKey() !== $component->getRecord()?->getKey()) {
  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. ->minDate(static function ($context, ?Discount $record = null) {
  83. if ($context === 'create' || $record?->start_date?->isFuture()) {
  84. return today()->addDay();
  85. }
  86. return $record?->start_date;
  87. })
  88. ->maxDate(static function (callable $get, ?Discount $record = null) {
  89. $end_date = $get('end_date') ?? $record?->end_date;
  90. return $end_date ?: today()->addYear();
  91. })
  92. ->format('Y-m-d H:i:s')
  93. ->displayFormat('F d, Y H:i')
  94. ->seconds(false)
  95. ->live()
  96. ->disabled(static fn ($context, ?Discount $record = null) => $context === 'edit' && $record?->start_date?->isPast() ?? false)
  97. ->helperText(static fn (Forms\Components\DateTimePicker $component) => $component->isDisabled() ? 'Start date cannot be changed after the discount has begun.' : null),
  98. Forms\Components\DateTimePicker::make('end_date')
  99. ->live()
  100. ->localizeLabel()
  101. ->minDate(static function (callable $get, ?Discount $record = null) {
  102. $start_date = $get('start_date') ?? $record?->start_date;
  103. return $start_date ?: today()->addDay();
  104. })
  105. ->maxDate(today()->addYear())
  106. ->format('Y-m-d H:i:s')
  107. ->displayFormat('F d, Y H:i')
  108. ->seconds(false),
  109. ToggleButton::make('enabled')
  110. ->localizeLabel('Default')
  111. ->onLabel(Discount::enabledLabel())
  112. ->offLabel(Discount::disabledLabel()),
  113. ])->columns(),
  114. ]);
  115. }
  116. public static function table(Table $table): Table
  117. {
  118. return $table
  119. ->columns([
  120. Tables\Columns\TextColumn::make('name')
  121. ->localizeLabel()
  122. ->weight(FontWeight::Medium)
  123. ->icon(static fn (Discount $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  124. ->tooltip(static function (Discount $record) {
  125. $tooltipMessage = translate('Default :Type :Record', [
  126. 'Type' => $record->type->getLabel(),
  127. 'Record' => static::getModelLabel(),
  128. ]);
  129. return $record->isEnabled() ? $tooltipMessage : null;
  130. })
  131. ->iconPosition('after')
  132. ->searchable()
  133. ->sortable(),
  134. Tables\Columns\TextColumn::make('rate')
  135. ->localizeLabel()
  136. ->rate(static fn (Discount $record) => $record->computation->value)
  137. ->searchable()
  138. ->sortable(),
  139. Tables\Columns\TextColumn::make('type')
  140. ->localizeLabel()
  141. ->badge()
  142. ->searchable()
  143. ->sortable(),
  144. Tables\Columns\TextColumn::make('start_date')
  145. ->localizeLabel()
  146. ->formatStateUsing(static function (Discount $record) {
  147. $dateFormat = Localization::firstOrFail()->date_format->value ?? DateFormat::DEFAULT;
  148. $timeFormat = Localization::firstOrFail()->time_format->value ?? TimeFormat::DEFAULT;
  149. return $record->start_date ? $record->start_date->format("{$dateFormat} {$timeFormat}") : 'N/A';
  150. })
  151. ->searchable()
  152. ->sortable(),
  153. Tables\Columns\TextColumn::make('end_date')
  154. ->localizeLabel()
  155. ->formatStateUsing(static function (Discount $record) {
  156. $dateFormat = Localization::firstOrFail()->date_format->value ?? DateFormat::DEFAULT;
  157. $timeFormat = Localization::firstOrFail()->time_format->value ?? TimeFormat::DEFAULT;
  158. return $record->end_date ? $record->end_date->format("{$dateFormat} {$timeFormat}") : 'N/A';
  159. })
  160. ->color(static fn (Discount $record) => $record->end_date?->isPast() ? 'danger' : null)
  161. ->searchable()
  162. ->sortable(),
  163. ])
  164. ->filters([
  165. //
  166. ])
  167. ->actions([
  168. Tables\Actions\EditAction::make(),
  169. ])
  170. ->bulkActions([
  171. Tables\Actions\BulkActionGroup::make([
  172. Tables\Actions\DeleteBulkAction::make(),
  173. ]),
  174. ])
  175. ->checkIfRecordIsSelectableUsing(static function (Discount $record) {
  176. return $record->isDisabled();
  177. })
  178. ->emptyStateActions([
  179. Tables\Actions\CreateAction::make(),
  180. ]);
  181. }
  182. public static function getPages(): array
  183. {
  184. return [
  185. 'index' => Pages\ListDiscounts::route('/'),
  186. 'create' => Pages\CreateDiscount::route('/create'),
  187. 'edit' => Pages\EditDiscount::route('/{record}/edit'),
  188. ];
  189. }
  190. }