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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 Filament\Forms;
  9. use Filament\Forms\Form;
  10. use Filament\Notifications\Notification;
  11. use Filament\Resources\Resource;
  12. use Filament\Support\Colors\Color;
  13. use Filament\Tables;
  14. use Filament\Tables\Table;
  15. use Illuminate\Database\Eloquent\Collection;
  16. use Wallo\FilamentSelectify\Components\ToggleButton;
  17. class CurrencyResource extends Resource
  18. {
  19. use ChecksForeignKeyConstraints;
  20. protected static ?string $model = Currency::class;
  21. protected static ?string $navigationIcon = 'heroicon-o-currency-dollar';
  22. protected static ?string $navigationGroup = 'Settings';
  23. protected static ?string $slug = 'settings/currencies';
  24. public static function form(Form $form): Form
  25. {
  26. return $form
  27. ->schema([
  28. Forms\Components\Section::make('General')
  29. ->schema([
  30. Forms\Components\Select::make('code')
  31. ->label('Code')
  32. ->options(Currency::getAvailableCurrencyCodes())
  33. ->searchable()
  34. ->placeholder('Select a currency code...')
  35. ->live()
  36. ->required()
  37. ->hidden(static fn (Forms\Get $get): bool => $get('enabled'))
  38. ->afterStateUpdated(static function (Forms\Set $set, $state) {
  39. if ($state === null) {
  40. return;
  41. }
  42. $code = $state;
  43. $allCurrencies = Currency::getAllCurrencies();
  44. $selectedCurrencyCode = $allCurrencies[$code] ?? [];
  45. $currencyService = app(CurrencyService::class);
  46. $defaultCurrencyCode = Currency::getDefaultCurrencyCode();
  47. $rate = 1;
  48. if ($defaultCurrencyCode !== null) {
  49. $rate = $currencyService->getCachedExchangeRate($defaultCurrencyCode, $code);
  50. }
  51. $set('name', $selectedCurrencyCode['name'] ?? '');
  52. $set('rate', $rate);
  53. $set('precision', $selectedCurrencyCode['precision'] ?? '');
  54. $set('symbol', $selectedCurrencyCode['symbol'] ?? '');
  55. $set('symbol_first', $selectedCurrencyCode['symbol_first'] ?? '');
  56. $set('decimal_mark', $selectedCurrencyCode['decimal_mark'] ?? '');
  57. $set('thousands_separator', $selectedCurrencyCode['thousands_separator'] ?? '');
  58. }),
  59. Forms\Components\TextInput::make('code')
  60. ->label('Code')
  61. ->hidden(static fn (Forms\Get $get): bool => !$get('enabled'))
  62. ->disabled(static fn (Forms\Get $get): bool => $get('enabled'))
  63. ->required(),
  64. Forms\Components\TextInput::make('name')
  65. ->label('Name')
  66. ->maxLength(50)
  67. ->required(),
  68. Forms\Components\TextInput::make('rate')
  69. ->label('Rate')
  70. ->dehydrateStateUsing(static fn (Forms\Get $get, $state): float => $get('enabled') ? '1.0' : (float) $state)
  71. ->numeric()
  72. ->live()
  73. ->disabled(static fn (Forms\Get $get): bool => $get('enabled'))
  74. ->required(),
  75. Forms\Components\Select::make('precision')
  76. ->label('Precision')
  77. ->searchable()
  78. ->placeholder('Select a precision...')
  79. ->options(['0', '1', '2', '3', '4'])
  80. ->required(),
  81. Forms\Components\TextInput::make('symbol')
  82. ->label('Symbol')
  83. ->maxLength(5)
  84. ->required(),
  85. Forms\Components\Select::make('symbol_first')
  86. ->label('Symbol Position')
  87. ->searchable()
  88. ->boolean('Before Amount', 'After Amount', 'Select the currency symbol position...')
  89. ->required(),
  90. Forms\Components\TextInput::make('decimal_mark')
  91. ->label('Decimal Separator')
  92. ->maxLength(1)
  93. ->required(),
  94. Forms\Components\TextInput::make('thousands_separator')
  95. ->label('Thousands Separator')
  96. ->maxLength(1)
  97. ->required(),
  98. ToggleButton::make('enabled')
  99. ->label('Default Currency')
  100. ->live()
  101. ->offColor(Color::Red)
  102. ->onColor(Color::Indigo)
  103. ->afterStateUpdated(static function (Forms\Set $set, Forms\Get $get, $state) {
  104. $enabled = $state;
  105. $code = $get('code');
  106. $currencyService = app(CurrencyService::class);
  107. if ($enabled) {
  108. $rate = 1;
  109. } else {
  110. $defaultCurrencyCode = Currency::getDefaultCurrencyCode();
  111. $rate = $defaultCurrencyCode ? $currencyService->getCachedExchangeRate($defaultCurrencyCode, $code) : 1;
  112. }
  113. $set('rate', $rate);
  114. }),
  115. ])->columns(),
  116. ]);
  117. }
  118. public static function table(Table $table): Table
  119. {
  120. return $table
  121. ->columns([
  122. Tables\Columns\TextColumn::make('name')
  123. ->label('Name')
  124. ->weight('semibold')
  125. ->icon(static fn (Currency $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
  126. ->tooltip(static fn (Currency $record) => $record->enabled ? 'Default Currency' : null)
  127. ->iconPosition('after')
  128. ->searchable()
  129. ->sortable(),
  130. Tables\Columns\TextColumn::make('code')
  131. ->label('Code')
  132. ->searchable()
  133. ->sortable(),
  134. Tables\Columns\TextColumn::make('symbol')
  135. ->label('Symbol')
  136. ->searchable()
  137. ->sortable(),
  138. Tables\Columns\TextColumn::make('rate')
  139. ->label('Rate')
  140. ->searchable()
  141. ->sortable(),
  142. ])
  143. ->filters([
  144. //
  145. ])
  146. ->actions([
  147. Tables\Actions\EditAction::make(),
  148. Tables\Actions\DeleteAction::make()
  149. ->before(static function (Tables\Actions\DeleteAction $action, Currency $record) {
  150. $defaultCurrency = $record->enabled;
  151. $modelsToCheck = [
  152. Account::class,
  153. ];
  154. $isUsed = self::isForeignKeyUsed('currency_code', $record->code, $modelsToCheck);
  155. if ($defaultCurrency) {
  156. Notification::make()
  157. ->danger()
  158. ->title('Action Denied')
  159. ->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]))
  160. ->persistent()
  161. ->send();
  162. $action->cancel();
  163. } elseif ($isUsed) {
  164. Notification::make()
  165. ->danger()
  166. ->title('Action Denied')
  167. ->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]))
  168. ->persistent()
  169. ->send();
  170. $action->cancel();
  171. }
  172. }),
  173. ])
  174. ->bulkActions([
  175. Tables\Actions\BulkActionGroup::make([
  176. Tables\Actions\DeleteBulkAction::make()
  177. ->before(static function (Tables\Actions\DeleteBulkAction $action, Collection $records) {
  178. foreach ($records as $record) {
  179. $defaultCurrency = $record->enabled;
  180. $modelsToCheck = [
  181. Account::class,
  182. ];
  183. $isUsed = self::isForeignKeyUsed('currency_code', $record->code, $modelsToCheck);
  184. if ($defaultCurrency) {
  185. Notification::make()
  186. ->danger()
  187. ->title('Action Denied')
  188. ->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]))
  189. ->persistent()
  190. ->send();
  191. $action->cancel();
  192. } elseif ($isUsed) {
  193. Notification::make()
  194. ->danger()
  195. ->title('Action Denied')
  196. ->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]))
  197. ->persistent()
  198. ->send();
  199. $action->cancel();
  200. }
  201. }
  202. }),
  203. ]),
  204. ])
  205. ->emptyStateActions([
  206. Tables\Actions\CreateAction::make(),
  207. ]);
  208. }
  209. public static function getRelations(): array
  210. {
  211. return [
  212. //
  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. }