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 9.8KB

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