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.

CreateAdjustmentSelect.php 7.4KB

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