Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CurrencyResource.php 12KB

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