Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CurrencyResource.php 11KB

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