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

CurrencyResource.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace App\Filament\Company\Resources\Setting;
  3. use App\Facades\Forex;
  4. use App\Filament\Company\Resources\Setting\CurrencyResource\Pages;
  5. use App\Models\Setting\Currency;
  6. use App\Models\Setting\Currency as CurrencyModel;
  7. use App\Traits\ChecksForeignKeyConstraints;
  8. use App\Traits\NotifiesOnDelete;
  9. use App\Utilities\Currency\CurrencyAccessor;
  10. use Closure;
  11. use Filament\Facades\Filament;
  12. use Filament\Forms;
  13. use Filament\Forms\Form;
  14. use Filament\Resources\Resource;
  15. use Filament\Support\Enums\FontWeight;
  16. use Filament\Tables;
  17. use Filament\Tables\Table;
  18. use Illuminate\Database\Eloquent\Collection;
  19. use Wallo\FilamentSelectify\Components\ToggleButton;
  20. class CurrencyResource extends Resource
  21. {
  22. use ChecksForeignKeyConstraints;
  23. use NotifiesOnDelete;
  24. protected static ?string $model = CurrencyModel::class;
  25. protected static ?string $modelLabel = 'Currency';
  26. protected static ?string $slug = 'settings/currencies';
  27. public static function getModelLabel(): string
  28. {
  29. $modelLabel = static::$modelLabel;
  30. return translate($modelLabel);
  31. }
  32. public static function getNavigationParentItem(): ?string
  33. {
  34. if (Filament::hasTopNavigation()) {
  35. return translate('Finance');
  36. }
  37. return null;
  38. }
  39. public static function form(Form $form): Form
  40. {
  41. return $form
  42. ->schema([
  43. Forms\Components\Section::make('General')
  44. ->schema([
  45. Forms\Components\Select::make('code')
  46. ->options(CurrencyAccessor::getAvailableCurrencies())
  47. ->searchable()
  48. ->live()
  49. ->required()
  50. ->localizeLabel()
  51. ->hidden(static fn (Forms\Get $get, $state): bool => $get('enabled') && $state !== null)
  52. ->afterStateUpdated(static function (Forms\Set $set, $state) {
  53. $fields = ['name', 'precision', 'symbol', 'symbol_first', 'decimal_mark', 'thousands_separator'];
  54. if ($state === null) {
  55. array_walk($fields, static fn ($field) => $set($field, null));
  56. return;
  57. }
  58. $currencyDetails = CurrencyAccessor::getAllCurrencies()[$state] ?? [];
  59. $defaultCurrencyCode = CurrencyAccessor::getDefaultCurrency();
  60. $exchangeRate = Forex::getCachedExchangeRate($defaultCurrencyCode, $state);
  61. if ($exchangeRate !== null) {
  62. $set('rate', $exchangeRate);
  63. }
  64. array_walk($fields, static fn ($field) => $set($field, $currencyDetails[$field] ?? null));
  65. }),
  66. Forms\Components\TextInput::make('code')
  67. ->localizeLabel()
  68. ->hidden(static fn (Forms\Get $get): bool => ! ($get('enabled') && $get('code') !== null))
  69. ->disabled(static fn (Forms\Get $get): bool => $get('enabled'))
  70. ->dehydrated()
  71. ->required(),
  72. Forms\Components\TextInput::make('name')
  73. ->localizeLabel()
  74. ->maxLength(50)
  75. ->required(),
  76. Forms\Components\TextInput::make('rate')
  77. ->numeric()
  78. ->rule('gt:0')
  79. ->live()
  80. ->localizeLabel()
  81. ->disabled(static fn (?CurrencyModel $record): bool => $record?->isEnabled() ?? false)
  82. ->dehydrated()
  83. ->required(),
  84. Forms\Components\Select::make('precision')
  85. ->localizeLabel()
  86. ->options(['0', '1', '2', '3', '4'])
  87. ->required(),
  88. Forms\Components\TextInput::make('symbol')
  89. ->localizeLabel()
  90. ->maxLength(5)
  91. ->required(),
  92. Forms\Components\Select::make('symbol_first')
  93. ->localizeLabel('Symbol Position')
  94. ->boolean(translate('Before Amount'), translate('After Amount'), translate('Select a symbol position'))
  95. ->required(),
  96. Forms\Components\TextInput::make('decimal_mark')
  97. ->localizeLabel('Decimal Separator')
  98. ->maxLength(1)
  99. ->rule(static function (Forms\Get $get): Closure {
  100. return static function ($attribute, $value, Closure $fail) use ($get) {
  101. if ($value === $get('thousands_separator')) {
  102. $fail(translate('Separators must be unique.'));
  103. }
  104. };
  105. })
  106. ->required(),
  107. Forms\Components\TextInput::make('thousands_separator')
  108. ->localizeLabel()
  109. ->maxLength(1)
  110. ->rule(static function (Forms\Get $get): Closure {
  111. return static function ($attribute, $value, Closure $fail) use ($get) {
  112. if ($value === $get('decimal_mark')) {
  113. $fail(translate('Separators must be unique.'));
  114. }
  115. };
  116. })
  117. ->nullable(),
  118. ToggleButton::make('enabled')
  119. ->localizeLabel('Default')
  120. ->onLabel(CurrencyModel::enabledLabel())
  121. ->offLabel(CurrencyModel::disabledLabel())
  122. ->disabled(static fn (?CurrencyModel $record): bool => $record?->isEnabled() ?? false)
  123. ->dehydrated()
  124. ->live()
  125. ->afterStateUpdated(static function (Forms\Set $set, Forms\Get $get, $state) {
  126. $enabledState = (bool) $state;
  127. $code = $get('code');
  128. if (! $code) {
  129. return;
  130. }
  131. if ($enabledState) {
  132. $set('rate', 1);
  133. return;
  134. }
  135. $forexEnabled = Forex::isEnabled();
  136. if ($forexEnabled) {
  137. $defaultCurrencyCode = CurrencyAccessor::getDefaultCurrency();
  138. $exchangeRate = Forex::getCachedExchangeRate($defaultCurrencyCode, $code);
  139. if ($exchangeRate !== null) {
  140. $set('rate', $exchangeRate);
  141. }
  142. }
  143. }),
  144. ])->columns(),
  145. ]);
  146. }
  147. public static function table(Table $table): Table
  148. {
  149. return $table
  150. ->columns([
  151. Tables\Columns\TextColumn::make('name')
  152. ->localizeLabel()
  153. ->weight(FontWeight::Medium)
  154. ->icon(static fn (CurrencyModel $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  155. ->tooltip(static function (CurrencyModel $record) {
  156. $tooltipMessage = translate('Default :Record', [
  157. 'Record' => static::getModelLabel(),
  158. ]);
  159. return $record->isEnabled() ? $tooltipMessage : null;
  160. })
  161. ->iconPosition('after')
  162. ->searchable()
  163. ->sortable(),
  164. Tables\Columns\TextColumn::make('code')
  165. ->localizeLabel()
  166. ->searchable()
  167. ->sortable(),
  168. Tables\Columns\TextColumn::make('symbol')
  169. ->localizeLabel()
  170. ->searchable()
  171. ->sortable(),
  172. Tables\Columns\TextColumn::make('rate')
  173. ->localizeLabel()
  174. ->searchable()
  175. ->sortable(),
  176. ])
  177. ->filters([
  178. //
  179. ])
  180. ->actions([
  181. Tables\Actions\EditAction::make(),
  182. Tables\Actions\DeleteAction::make()
  183. ->before(function (Tables\Actions\DeleteAction $action, Currency $record) {
  184. $modelsToCheck = [
  185. Account::class,
  186. ];
  187. $isUsed = self::isForeignKeyUsed('currency_code', $record->code, $modelsToCheck);
  188. if ($isUsed) {
  189. $reason = 'in use';
  190. self::notifyBeforeDelete($record, $reason);
  191. $action->cancel();
  192. }
  193. }),
  194. ])
  195. ->bulkActions([
  196. Tables\Actions\BulkActionGroup::make([
  197. Tables\Actions\DeleteBulkAction::make()
  198. ->before(static function (Tables\Actions\DeleteBulkAction $action, Collection $records) {
  199. foreach ($records as $record) {
  200. $modelsToCheck = [
  201. Account::class,
  202. ];
  203. $isUsed = self::isForeignKeyUsed('currency_code', $record->code, $modelsToCheck);
  204. if ($isUsed) {
  205. $reason = 'in use';
  206. self::notifyBeforeDelete($record, $reason);
  207. $action->cancel();
  208. }
  209. }
  210. }),
  211. ]),
  212. ])
  213. ->checkIfRecordIsSelectableUsing(static function (CurrencyModel $record) {
  214. return $record->isDisabled();
  215. })
  216. ->emptyStateActions([
  217. Tables\Actions\CreateAction::make(),
  218. ]);
  219. }
  220. public static function getPages(): array
  221. {
  222. return [
  223. 'index' => Pages\ListCurrencies::route('/'),
  224. 'create' => Pages\CreateCurrency::route('/create'),
  225. 'edit' => Pages\EditCurrency::route('/{record}/edit'),
  226. ];
  227. }
  228. }