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

CurrencyResource.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace App\Filament\Company\Resources\Setting;
  3. use App\Filament\Company\Resources\Setting\CurrencyResource\Pages;
  4. use App\Models\Banking\Account;
  5. use App\Models\Setting\Currency;
  6. use App\Services\CurrencyService;
  7. use App\Traits\ChecksForeignKeyConstraints;
  8. use Closure;
  9. use Filament\Forms\Form;
  10. use Filament\Notifications\Notification;
  11. use Filament\Resources\Resource;
  12. use Filament\Tables\Table;
  13. use Filament\{Forms, Tables};
  14. use Illuminate\Database\Eloquent\Collection;
  15. use Wallo\FilamentSelectify\Components\ToggleButton;
  16. class CurrencyResource extends Resource
  17. {
  18. use ChecksForeignKeyConstraints;
  19. protected static ?string $model = Currency::class;
  20. protected static ?string $navigationIcon = 'heroicon-o-currency-dollar';
  21. protected static ?string $navigationGroup = 'Settings';
  22. protected static ?string $slug = 'settings/currencies';
  23. public static function form(Form $form): Form
  24. {
  25. return $form
  26. ->schema([
  27. Forms\Components\Section::make('General')
  28. ->schema([
  29. Forms\Components\Select::make('code')
  30. ->label('Code')
  31. ->options(Currency::getAvailableCurrencyCodes())
  32. ->searchable()
  33. ->placeholder('Select a currency code...')
  34. ->live()
  35. ->required()
  36. ->hidden(static fn (Forms\Get $get, $state): bool => $get('enabled') && $state !== null)
  37. ->afterStateUpdated(static function (Forms\Set $set, $state) {
  38. $fields = ['name', 'rate', 'precision', 'symbol', 'symbol_first', 'decimal_mark', 'thousands_separator'];
  39. if ($state === null) {
  40. foreach ($fields as $field) {
  41. $set($field, null);
  42. }
  43. return;
  44. }
  45. $defaultCurrencyCode = Currency::getDefaultCurrencyCode();
  46. $currencyService = app(CurrencyService::class);
  47. $code = $state;
  48. $allCurrencies = Currency::getAllCurrencies();
  49. $selectedCurrencyCode = $allCurrencies[$code] ?? [];
  50. $rate = $defaultCurrencyCode ? $currencyService->getCachedExchangeRate($defaultCurrencyCode, $code) : 1;
  51. foreach ($fields as $field) {
  52. $set($field, $selectedCurrencyCode[$field] ?? ($field === 'rate' ? $rate : ''));
  53. }
  54. }),
  55. Forms\Components\TextInput::make('code')
  56. ->label('Code')
  57. ->hidden(static fn (Forms\Get $get): bool => ! ($get('enabled') && $get('code') !== null))
  58. ->disabled(static fn (Forms\Get $get): bool => $get('enabled'))
  59. ->dehydrated()
  60. ->required(),
  61. Forms\Components\TextInput::make('name')
  62. ->label('Name')
  63. ->maxLength(50)
  64. ->required(),
  65. Forms\Components\TextInput::make('rate')
  66. ->label('Rate')
  67. ->numeric()
  68. ->rule('gt:0')
  69. ->live()
  70. ->required(),
  71. Forms\Components\Select::make('precision')
  72. ->label('Precision')
  73. ->native(false)
  74. ->selectablePlaceholder(false)
  75. ->placeholder('Select a precision...')
  76. ->options(['0', '1', '2', '3', '4'])
  77. ->required(),
  78. Forms\Components\TextInput::make('symbol')
  79. ->label('Symbol')
  80. ->maxLength(5)
  81. ->required(),
  82. Forms\Components\Select::make('symbol_first')
  83. ->label('Symbol Position')
  84. ->native(false)
  85. ->selectablePlaceholder(false)
  86. ->formatStateUsing(static fn ($state) => isset($state) ? (int) $state : null)
  87. ->boolean('Before Amount', 'After Amount', 'Select a symbol position...')
  88. ->required(),
  89. Forms\Components\TextInput::make('decimal_mark')
  90. ->label('Decimal Separator')
  91. ->maxLength(1)
  92. ->required(),
  93. Forms\Components\TextInput::make('thousands_separator')
  94. ->label('Thousands Separator')
  95. ->maxLength(1)
  96. ->rule(static function (Forms\Get $get): Closure {
  97. return static function ($attribute, $value, Closure $fail) use ($get) {
  98. $decimalMark = $get('decimal_mark');
  99. if ($value === $decimalMark) {
  100. $fail('The thousands separator and decimal separator must be different.');
  101. }
  102. };
  103. })
  104. ->nullable(),
  105. ToggleButton::make('enabled')
  106. ->label('Default Currency')
  107. ->live()
  108. ->offColor('danger')
  109. ->onColor('primary')
  110. ->afterStateUpdated(static function (Forms\Set $set, Forms\Get $get, $state) {
  111. $enabledState = (bool) $state;
  112. $code = $get('code');
  113. $defaultCurrencyCode = Currency::getDefaultCurrencyCode();
  114. $currencyService = app(CurrencyService::class);
  115. if ($enabledState) {
  116. $set('rate', 1);
  117. } else {
  118. if ($code === null) {
  119. return;
  120. }
  121. $rate = $currencyService->getCachedExchangeRate($defaultCurrencyCode, $code);
  122. $set('rate', $rate ?? '');
  123. }
  124. }),
  125. ])->columns(),
  126. ]);
  127. }
  128. public static function table(Table $table): Table
  129. {
  130. return $table
  131. ->columns([
  132. Tables\Columns\TextColumn::make('name')
  133. ->label('Name')
  134. ->weight('semibold')
  135. ->icon(static fn (Currency $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
  136. ->tooltip(static fn (Currency $record) => $record->enabled ? 'Default Currency' : null)
  137. ->iconPosition('after')
  138. ->searchable()
  139. ->sortable(),
  140. Tables\Columns\TextColumn::make('code')
  141. ->label('Code')
  142. ->searchable()
  143. ->sortable(),
  144. Tables\Columns\TextColumn::make('symbol')
  145. ->label('Symbol')
  146. ->searchable()
  147. ->sortable(),
  148. Tables\Columns\TextColumn::make('rate')
  149. ->label('Rate')
  150. ->searchable()
  151. ->sortable(),
  152. ])
  153. ->filters([
  154. //
  155. ])
  156. ->actions([
  157. Tables\Actions\EditAction::make(),
  158. Tables\Actions\DeleteAction::make()
  159. ->before(static function (Tables\Actions\DeleteAction $action, Currency $record) {
  160. $defaultCurrency = $record->enabled;
  161. $modelsToCheck = [
  162. Account::class,
  163. ];
  164. $isUsed = self::isForeignKeyUsed('currency_code', $record->code, $modelsToCheck);
  165. if ($defaultCurrency) {
  166. Notification::make()
  167. ->danger()
  168. ->title('Action Denied')
  169. ->body(__('The :name currency is currently set as the default currency and cannot be deleted. Please set a different currency as your default before attempting to delete this one.', ['name' => $record->name]))
  170. ->persistent()
  171. ->send();
  172. $action->cancel();
  173. } elseif ($isUsed) {
  174. Notification::make()
  175. ->danger()
  176. ->title('Action Denied')
  177. ->body(__('The :name currency is currently in use by one or more accounts and cannot be deleted. Please remove this currency from all accounts before attempting to delete it.', ['name' => $record->name]))
  178. ->persistent()
  179. ->send();
  180. $action->cancel();
  181. }
  182. }),
  183. ])
  184. ->bulkActions([
  185. Tables\Actions\BulkActionGroup::make([
  186. Tables\Actions\DeleteBulkAction::make()
  187. ->before(static function (Tables\Actions\DeleteBulkAction $action, Collection $records) {
  188. foreach ($records as $record) {
  189. $defaultCurrency = $record->enabled;
  190. $modelsToCheck = [
  191. Account::class,
  192. ];
  193. $isUsed = self::isForeignKeyUsed('currency_code', $record->code, $modelsToCheck);
  194. if ($defaultCurrency) {
  195. Notification::make()
  196. ->danger()
  197. ->title('Action Denied')
  198. ->body(__('The :name currency is currently set as the default currency and cannot be deleted. Please set a different currency as your default before attempting to delete this one.', ['name' => $record->name]))
  199. ->persistent()
  200. ->send();
  201. $action->cancel();
  202. } elseif ($isUsed) {
  203. Notification::make()
  204. ->danger()
  205. ->title('Action Denied')
  206. ->body(__('The :name currency is currently in use by one or more accounts and cannot be deleted. Please remove this currency from all accounts before attempting to delete it.', ['name' => $record->name]))
  207. ->persistent()
  208. ->send();
  209. $action->cancel();
  210. }
  211. }
  212. }),
  213. ]),
  214. ])
  215. ->emptyStateActions([
  216. Tables\Actions\CreateAction::make(),
  217. ]);
  218. }
  219. public static function getRelations(): array
  220. {
  221. return [
  222. //
  223. ];
  224. }
  225. public static function getPages(): array
  226. {
  227. return [
  228. 'index' => Pages\ListCurrencies::route('/'),
  229. 'create' => Pages\CreateCurrency::route('/create'),
  230. 'edit' => Pages\EditCurrency::route('/{record}/edit'),
  231. ];
  232. }
  233. }