選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DiscountResource.php 9.9KB

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