You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CurrencyResource.php 11KB

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