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

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