Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CreateAdjustmentSelect.php 7.2KB

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