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

CompanyDefault.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace App\Filament\Company\Pages\Setting;
  3. use App\Events\CompanyDefaultUpdated;
  4. use App\Models\Banking\BankAccount;
  5. use App\Models\Setting\CompanyDefault as CompanyDefaultModel;
  6. use App\Models\Setting\Currency;
  7. use App\Models\Setting\Discount;
  8. use App\Models\Setting\Tax;
  9. use Filament\Actions\Action;
  10. use Filament\Actions\ActionGroup;
  11. use Filament\Facades\Filament;
  12. use Filament\Forms\Components\Component;
  13. use Filament\Forms\Components\Section;
  14. use Filament\Forms\Components\Select;
  15. use Filament\Forms\Form;
  16. use Filament\Notifications\Notification;
  17. use Filament\Pages\Concerns\InteractsWithFormActions;
  18. use Filament\Pages\Page;
  19. use Filament\Support\Exceptions\Halt;
  20. use Illuminate\Auth\Access\AuthorizationException;
  21. use Illuminate\Contracts\Support\Htmlable;
  22. use Illuminate\Database\Eloquent\Model;
  23. use Illuminate\Support\Facades\Blade;
  24. use Livewire\Attributes\Locked;
  25. use function Filament\authorize;
  26. /**
  27. * @property Form $form
  28. */
  29. class CompanyDefault extends Page
  30. {
  31. use InteractsWithFormActions;
  32. protected static ?string $title = 'Default';
  33. protected static ?string $slug = 'settings/default';
  34. protected static string $view = 'filament.company.pages.setting.company-default';
  35. /**
  36. * @var array<string, mixed> | null
  37. */
  38. public ?array $data = [];
  39. #[Locked]
  40. public ?CompanyDefaultModel $record = null;
  41. public function getTitle(): string | Htmlable
  42. {
  43. return translate(static::$title);
  44. }
  45. public static function getNavigationLabel(): string
  46. {
  47. return translate(static::$title);
  48. }
  49. public static function getNavigationParentItem(): ?string
  50. {
  51. if (Filament::hasTopNavigation()) {
  52. return translate('Personalization');
  53. }
  54. return null;
  55. }
  56. public function mount(): void
  57. {
  58. $this->record = CompanyDefaultModel::firstOrNew([
  59. 'company_id' => auth()->user()->currentCompany->id,
  60. ]);
  61. abort_unless(static::canView($this->record), 404);
  62. $this->fillForm();
  63. }
  64. public function fillForm(): void
  65. {
  66. $data = $this->record->attributesToArray();
  67. $this->form->fill($data);
  68. }
  69. public function save(): void
  70. {
  71. try {
  72. $data = $this->form->getState();
  73. $this->handleRecordUpdate($this->record, $data);
  74. } catch (Halt $exception) {
  75. return;
  76. }
  77. $this->getSavedNotification()->send();
  78. }
  79. protected function getSavedNotification(): Notification
  80. {
  81. return Notification::make()
  82. ->success()
  83. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  84. }
  85. public function form(Form $form): Form
  86. {
  87. return $form
  88. ->schema([
  89. $this->getGeneralSection(),
  90. $this->getModifiersSection(),
  91. ])
  92. ->model($this->record)
  93. ->statePath('data')
  94. ->operation('edit');
  95. }
  96. protected function getGeneralSection(): Component
  97. {
  98. return Section::make('General')
  99. ->schema([
  100. Select::make('bank_account_id')
  101. ->localizeLabel()
  102. ->relationship('bankAccount', 'name')
  103. ->getOptionLabelFromRecordUsing(function (BankAccount $record) {
  104. $name = $record->account->name;
  105. $currency = $this->renderBadgeOptionLabel($record->account->currency_code);
  106. return "{$name} ⁓ {$currency}";
  107. })
  108. ->allowHtml()
  109. ->saveRelationshipsUsing(null)
  110. ->selectablePlaceholder(false)
  111. ->searchable()
  112. ->preload(),
  113. Select::make('currency_code')
  114. ->softRequired()
  115. ->localizeLabel('Currency')
  116. ->relationship('currency', 'name')
  117. ->getOptionLabelFromRecordUsing(static fn (Currency $record) => "{$record->code} {$record->symbol} - {$record->name}")
  118. ->saveRelationshipsUsing(null)
  119. ->searchable()
  120. ->preload(),
  121. ])->columns();
  122. }
  123. protected function getModifiersSection(): Component
  124. {
  125. return Section::make('Taxes & Discounts')
  126. ->schema([
  127. Select::make('sales_tax_id')
  128. ->softRequired()
  129. ->localizeLabel()
  130. ->relationship('salesTax', 'name')
  131. ->getOptionLabelFromRecordUsing(function (Tax $record) {
  132. $currencyCode = $this->record->currency_code;
  133. $rate = rateFormat($record->rate, $record->computation->value, $currencyCode);
  134. $rateBadge = $this->renderBadgeOptionLabel($rate);
  135. return "{$record->name} ⁓ {$rateBadge}";
  136. })
  137. ->allowHtml()
  138. ->saveRelationshipsUsing(null)
  139. ->searchable(),
  140. Select::make('purchase_tax_id')
  141. ->softRequired()
  142. ->localizeLabel()
  143. ->relationship('purchaseTax', 'name')
  144. ->getOptionLabelFromRecordUsing(function (Tax $record) {
  145. $currencyCode = $this->record->currency_code;
  146. $rate = rateFormat($record->rate, $record->computation->value, $currencyCode);
  147. $rateBadge = $this->renderBadgeOptionLabel($rate);
  148. return "{$record->name} ⁓ {$rateBadge}";
  149. })
  150. ->allowHtml()
  151. ->saveRelationshipsUsing(null)
  152. ->searchable(),
  153. Select::make('sales_discount_id')
  154. ->softRequired()
  155. ->localizeLabel()
  156. ->relationship('salesDiscount', 'name')
  157. ->getOptionLabelFromRecordUsing(function (Discount $record) {
  158. $currencyCode = $this->record->currency_code;
  159. $rate = rateFormat($record->rate, $record->computation->value, $currencyCode);
  160. $rateBadge = $this->renderBadgeOptionLabel($rate);
  161. return "{$record->name} ⁓ {$rateBadge}";
  162. })
  163. ->saveRelationshipsUsing(null)
  164. ->allowHtml()
  165. ->searchable(),
  166. Select::make('purchase_discount_id')
  167. ->softRequired()
  168. ->localizeLabel()
  169. ->relationship('purchaseDiscount', 'name')
  170. ->getOptionLabelFromRecordUsing(function (Discount $record) {
  171. $currencyCode = $this->record->currency_code;
  172. $rate = rateFormat($record->rate, $record->computation->value, $currencyCode);
  173. $rateBadge = $this->renderBadgeOptionLabel($rate);
  174. return "{$record->name} ⁓ {$rateBadge}";
  175. })
  176. ->allowHtml()
  177. ->saveRelationshipsUsing(null)
  178. ->searchable(),
  179. ])->columns();
  180. }
  181. public function renderBadgeOptionLabel(string $label, string $color = 'primary', string $size = 'sm'): string
  182. {
  183. return Blade::render('<x-filament::badge color="' . $color . '" size="' . $size . '">' . e($label) . '</x-filament::badge>');
  184. }
  185. protected function handleRecordUpdate(CompanyDefaultModel $record, array $data): CompanyDefaultModel
  186. {
  187. CompanyDefaultUpdated::dispatch($record, $data);
  188. $record->update($data);
  189. return $record;
  190. }
  191. /**
  192. * @return array<Action | ActionGroup>
  193. */
  194. protected function getFormActions(): array
  195. {
  196. return [
  197. $this->getSaveFormAction(),
  198. ];
  199. }
  200. protected function getSaveFormAction(): Action
  201. {
  202. return Action::make('save')
  203. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  204. ->submit('save')
  205. ->keyBindings(['mod+s']);
  206. }
  207. public static function canView(Model $record): bool
  208. {
  209. try {
  210. return authorize('update', $record)->allowed();
  211. } catch (AuthorizationException $exception) {
  212. return $exception->toResponse()->allowed();
  213. }
  214. }
  215. }