您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CreateAdjustmentSelect.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Filament\Forms\Components;
  3. use App\Enums\Accounting\AdjustmentCategory;
  4. use App\Enums\Accounting\AdjustmentComputation;
  5. use App\Enums\Accounting\AdjustmentScope;
  6. use App\Enums\Accounting\AdjustmentStatus;
  7. use App\Enums\Accounting\AdjustmentType;
  8. use App\Models\Accounting\Adjustment;
  9. use Filament\Forms\Components\Actions\Action;
  10. use Filament\Forms\Components\Checkbox;
  11. use Filament\Forms\Components\DateTimePicker;
  12. use Filament\Forms\Components\Group;
  13. use Filament\Forms\Components\Select;
  14. use Filament\Forms\Components\Textarea;
  15. use Filament\Forms\Components\TextInput;
  16. use Filament\Forms\Get;
  17. use Filament\Support\Enums\MaxWidth;
  18. use Illuminate\Database\Eloquent\Builder;
  19. use Illuminate\Support\Facades\DB;
  20. class CreateAdjustmentSelect extends Select
  21. {
  22. protected ?AdjustmentCategory $category = null;
  23. protected ?AdjustmentType $type = null;
  24. protected bool $includeInactive = false;
  25. public function category(AdjustmentCategory $category): static
  26. {
  27. $this->category = $category;
  28. return $this;
  29. }
  30. public function type(AdjustmentType $type): static
  31. {
  32. $this->type = $type;
  33. return $this;
  34. }
  35. public function includeInactive(bool $includeInactive = true): static
  36. {
  37. $this->includeInactive = $includeInactive;
  38. return $this;
  39. }
  40. public function getCategory(): ?AdjustmentCategory
  41. {
  42. return $this->category;
  43. }
  44. public function getType(): ?AdjustmentType
  45. {
  46. return $this->type;
  47. }
  48. public function includesInactive(): bool
  49. {
  50. return $this->includeInactive;
  51. }
  52. protected function setUp(): void
  53. {
  54. parent::setUp();
  55. $this
  56. ->searchable()
  57. ->preload()
  58. ->createOptionForm($this->createAdjustmentForm())
  59. ->createOptionAction(fn (Action $action) => $this->createAdjustmentAction($action));
  60. $this->relationship(
  61. name: 'adjustments',
  62. titleAttribute: 'name',
  63. modifyQueryUsing: function (Builder $query) {
  64. if ($this->getCategory()) {
  65. $query->where('category', $this->getCategory());
  66. }
  67. if ($this->getType()) {
  68. $query->where('type', $this->getType());
  69. }
  70. if (! $this->includesInactive()) {
  71. $query->where('status', AdjustmentStatus::Active);
  72. }
  73. return $query->orderBy('name');
  74. },
  75. );
  76. $this->createOptionUsing(static function (array $data, CreateAdjustmentSelect $component) {
  77. return DB::transaction(static function () use ($data, $component) {
  78. $category = $data['category'] ?? $component->getCategory();
  79. $type = $data['type'] ?? $component->getType();
  80. $adjustment = Adjustment::create([
  81. 'name' => $data['name'],
  82. 'description' => $data['description'] ?? null,
  83. 'category' => $category,
  84. 'type' => $type,
  85. 'computation' => $data['computation'],
  86. 'rate' => $data['rate'],
  87. 'scope' => $data['scope'] ?? null,
  88. 'recoverable' => $data['recoverable'] ?? false,
  89. 'start_date' => $data['start_date'] ?? null,
  90. 'end_date' => $data['end_date'] ?? null,
  91. ]);
  92. return $adjustment->getKey();
  93. });
  94. });
  95. }
  96. protected function createAdjustmentForm(): array
  97. {
  98. return [
  99. TextInput::make('name')
  100. ->label('Name')
  101. ->required()
  102. ->maxLength(255),
  103. Textarea::make('description')
  104. ->label('Description'),
  105. Select::make('category')
  106. ->label('Category')
  107. ->options(AdjustmentCategory::class)
  108. ->default(AdjustmentCategory::Tax)
  109. ->hidden(fn () => (bool) $this->getCategory())
  110. ->live()
  111. ->required(),
  112. Select::make('type')
  113. ->label('Type')
  114. ->options(AdjustmentType::class)
  115. ->default(AdjustmentType::Sales)
  116. ->hidden(fn () => (bool) $this->getType())
  117. ->live()
  118. ->required(),
  119. Select::make('computation')
  120. ->label('Computation')
  121. ->options(AdjustmentComputation::class)
  122. ->default(AdjustmentComputation::Percentage)
  123. ->live()
  124. ->required(),
  125. TextInput::make('rate')
  126. ->label('Rate')
  127. ->rate(static fn (Get $get) => $get('computation'))
  128. ->required(),
  129. Select::make('scope')
  130. ->label('Scope')
  131. ->options(AdjustmentScope::class),
  132. Checkbox::make('recoverable')
  133. ->label('Recoverable')
  134. ->default(false)
  135. ->helperText('When enabled, tax is tracked separately as claimable from the government. Non-recoverable taxes are treated as part of the expense.')
  136. ->visible(function (Get $get) {
  137. $category = $this->getCategory() ?? AdjustmentCategory::parse($get('category'));
  138. $type = $this->getType() ?? AdjustmentType::parse($get('type'));
  139. return $category->isTax() && $type->isPurchase();
  140. }),
  141. Group::make()
  142. ->schema([
  143. DateTimePicker::make('start_date'),
  144. DateTimePicker::make('end_date')
  145. ->after('start_date'),
  146. ])
  147. ->visible(function (Get $get) {
  148. $category = $this->getCategory() ?? AdjustmentCategory::parse($get('category'));
  149. return $category->isDiscount();
  150. }),
  151. ];
  152. }
  153. protected function createAdjustmentAction(Action $action): Action
  154. {
  155. $categoryLabel = $this->getCategory()?->getLabel() ?? 'Adjustment';
  156. $typeLabel = $this->getType()?->getLabel() ?? '';
  157. $label = trim($typeLabel . ' ' . $categoryLabel);
  158. return $action
  159. ->label('Create ' . $label)
  160. ->slideOver()
  161. ->modalWidth(MaxWidth::ExtraLarge)
  162. ->modalHeading('Create a new ' . strtolower($label));
  163. }
  164. }