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

CurrencyResource.php 11KB

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