Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DiscountResource.php 9.7KB

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