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

CompanyDefault.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Pages;
  3. use App\Events\CompanyDefaultUpdated;
  4. use App\Filament\Company\Clusters\Settings;
  5. use App\Models\Banking\BankAccount;
  6. use App\Models\Setting\CompanyDefault as CompanyDefaultModel;
  7. use Filament\Actions\Action;
  8. use Filament\Actions\ActionGroup;
  9. use Filament\Forms\Components\Component;
  10. use Filament\Forms\Components\Placeholder;
  11. use Filament\Forms\Components\Section;
  12. use Filament\Forms\Components\Select;
  13. use Filament\Forms\Form;
  14. use Filament\Notifications\Notification;
  15. use Filament\Pages\Concerns\InteractsWithFormActions;
  16. use Filament\Pages\Page;
  17. use Filament\Support\Enums\MaxWidth;
  18. use Filament\Support\Exceptions\Halt;
  19. use Illuminate\Auth\Access\AuthorizationException;
  20. use Illuminate\Contracts\Support\Htmlable;
  21. use Illuminate\Database\Eloquent\Model;
  22. use Illuminate\Support\Facades\Blade;
  23. use Livewire\Attributes\Locked;
  24. use function Filament\authorize;
  25. /**
  26. * @property Form $form
  27. */
  28. class CompanyDefault extends Page
  29. {
  30. use InteractsWithFormActions;
  31. protected static ?string $title = 'Default';
  32. protected static string $view = 'filament.company.pages.setting.company-default';
  33. protected static ?string $cluster = Settings::class;
  34. /**
  35. * @var array<string, mixed> | null
  36. */
  37. public ?array $data = [];
  38. #[Locked]
  39. public ?CompanyDefaultModel $record = null;
  40. public function getTitle(): string | Htmlable
  41. {
  42. return translate(static::$title);
  43. }
  44. public static function getNavigationLabel(): string
  45. {
  46. return translate(static::$title);
  47. }
  48. public function getMaxContentWidth(): MaxWidth | string | null
  49. {
  50. return MaxWidth::ScreenTwoExtraLarge;
  51. }
  52. public function mount(): void
  53. {
  54. $this->record = CompanyDefaultModel::firstOrNew([
  55. 'company_id' => auth()->user()->currentCompany->id,
  56. ]);
  57. abort_unless(static::canView($this->record), 404);
  58. $this->fillForm();
  59. }
  60. public function fillForm(): void
  61. {
  62. $data = $this->record->attributesToArray();
  63. $this->form->fill($data);
  64. }
  65. public function save(): void
  66. {
  67. try {
  68. $data = $this->form->getState();
  69. $this->handleRecordUpdate($this->record, $data);
  70. } catch (Halt $exception) {
  71. return;
  72. }
  73. $this->getSavedNotification()->send();
  74. }
  75. protected function getSavedNotification(): Notification
  76. {
  77. return Notification::make()
  78. ->success()
  79. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  80. }
  81. public function form(Form $form): Form
  82. {
  83. return $form
  84. ->schema([
  85. $this->getGeneralSection(),
  86. ])
  87. ->model($this->record)
  88. ->statePath('data')
  89. ->operation('edit');
  90. }
  91. protected function getGeneralSection(): Component
  92. {
  93. return Section::make('General')
  94. ->schema([
  95. Select::make('bank_account_id')
  96. ->localizeLabel()
  97. ->relationship('bankAccount', 'name')
  98. ->getOptionLabelFromRecordUsing(function (BankAccount $record) {
  99. $name = $record->account->name;
  100. $currency = $this->renderBadgeOptionLabel($record->account->currency_code);
  101. return "{$name} ⁓ {$currency}";
  102. })
  103. ->allowHtml()
  104. ->saveRelationshipsUsing(null)
  105. ->selectablePlaceholder(false)
  106. ->searchable()
  107. ->preload(),
  108. Placeholder::make('currency_code')
  109. ->label(translate('Currency'))
  110. ->hintIcon('heroicon-o-question-mark-circle', 'You cannot change this after your company has been created. You can still use other currencies for transactions.')
  111. ->content(static fn (CompanyDefaultModel $record) => "{$record->currency->code} {$record->currency->symbol} - {$record->currency->name}"),
  112. ])->columns();
  113. }
  114. public function renderBadgeOptionLabel(string $label): string
  115. {
  116. return Blade::render('filament::components.badge', [
  117. 'color' => 'primary',
  118. 'size' => 'sm',
  119. 'slot' => $label,
  120. ]);
  121. }
  122. protected function handleRecordUpdate(CompanyDefaultModel $record, array $data): CompanyDefaultModel
  123. {
  124. CompanyDefaultUpdated::dispatch($record, $data);
  125. $record->update($data);
  126. return $record;
  127. }
  128. /**
  129. * @return array<Action | ActionGroup>
  130. */
  131. protected function getFormActions(): array
  132. {
  133. return [
  134. $this->getSaveFormAction(),
  135. ];
  136. }
  137. protected function getSaveFormAction(): Action
  138. {
  139. return Action::make('save')
  140. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  141. ->submit('save')
  142. ->keyBindings(['mod+s']);
  143. }
  144. public static function canView(Model $record): bool
  145. {
  146. try {
  147. return authorize('update', $record)->allowed();
  148. } catch (AuthorizationException $exception) {
  149. return $exception->toResponse()->allowed();
  150. }
  151. }
  152. }